From f8d1473824b7120a1d0f1440cf0179ffebebbc0e Mon Sep 17 00:00:00 2001 From: Zachary Kanfer Date: Sat, 25 Jun 2022 01:48:38 -0400 Subject: [PATCH] Add a function to rename the file visited by the current buffer. * lisp/files.el (rename-visited-file) --- etc/NEWS | 3 +++ lisp/files.el | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index 6c04ae164c..78e810ac9f 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -339,6 +339,9 @@ match those regexps will be ignored by 'switch-to-prev-buffer' and ** Menus +** New command 'move-file'. +This command moves a file to a new location. + --- *** The entries following the buffers in the "Buffers" menu can now be altered. Change the 'menu-bar-buffers-menu-command-entries' variable to alter diff --git a/lisp/files.el b/lisp/files.el index a804f0088e..278f80679a 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -4817,6 +4817,25 @@ write-file ;; It's likely that the VC status at the new location is different from ;; the one at the old location. (vc-refresh-state))) + +(defun rename-visited-file (new-location) + "Rename the file visited by the current buffer to NEW-LOCATION. + +Interactively, this prompts for NEW-LOCATION. + +If the file has not been visited, this works similarly to #'write-file". + (interactive (list (if buffer-file-name + (read-file-name "Rename file to: ") + (read-file-name "Rename file to: " + default-directory + (expand-file-name (file-name-nondirectory (buffer-name)) + default-directory))))) + (let ((old-location (buffer-file-name))) + (write-file new-location t) + (when (and old-location + (file-exists-p new-location)) + (delete-file old-location)))) + (defun file-extended-attributes (filename) "Return an alist of extended attributes of file FILENAME. -- 2.25.1