;;; gpolonkai/file-utils.el --- File manipulation utilities ;;; ;;; SPDX-License-Identifier: GPL-3.0-or-later ;;; Copyright © 2025 Gergely Polonkai ;;; This library is free software; you can redistribute it and/or ;;; modify it under the terms of the GNU Lesser General Public ;;; License as published by the Free Software Foundation; either ;;; version 3 of the License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this library; if not, write to the ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;;; Boston, MA 02111-1307, USA. ;;; ;;; Commentary: ;;; ;;; Utility functions stolen from the Internet or written by me ;;; ;;; Code: ;; From whattheemacsd.org: http://whattheemacsd.com/file-defuns.el-01.html (defun wted/rename-current-buffer-file () "Renames current buffer and the file it is visiting." (interactive) (let ((name (buffer-name)) (filename (buffer-file-name))) (if (not (and filename (file-exists-p filename))) (error "Buffer '%s' is not visiting a file!" name) (let ((new-name (read-file-name "New name: " filename))) (if (get-buffer new-name) (error "A buffer named '%s' already exists!" new-name) (rename-file filename new-name 1) (rename-buffer new-name) (set-visited-file-name new-name) ; TODO: this is suspicious for me… (set-buffer-modified-p nil) (message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name))))))) ;; From whattheemacsd.org: http://whattheemacsd.com/file-defuns.el-02.html (defun wted/delete-current-buffer-file () "Remove file the current buffer is visiting and kill the buffer." (interactive) (let ((filename (buffer-file-name)) (name (buffer-name)) (buffer (current-buffer))) (if (not (and filename (file-exists-p filename))) (kill-buffer buffer) (when (yes-or-no-p "Are you sure you want to remove this file? ") (delete-file filename) (kill-buffer buffer) (message "File '%s' successfully removed" filename))))) (provide 'gpolonkai/file-utils) ;;; file-utils.el ends here