emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 772b189143: Add new command `rename-visited-file'


From: Lars Ingebrigtsen
Subject: master 772b189143: Add new command `rename-visited-file'
Date: Tue, 28 Jun 2022 08:15:53 -0400 (EDT)

branch: master
commit 772b189143453745a8e014e21d4b6b78f855bba3
Author: Zachary Kanfer <zkanfer@gmail.com>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add new command `rename-visited-file'
    
    * doc/emacs/files.texi (Save Commands): Document it.
    
    * lisp/files.el (rename-visited-file): New command (bug#56229).
---
 doc/emacs/files.texi |  3 +++
 etc/NEWS             |  4 ++++
 lisp/files.el        | 20 ++++++++++++++++++++
 3 files changed, 27 insertions(+)

diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi
index 5c80cfe190..2296bacada 100644
--- a/doc/emacs/files.texi
+++ b/doc/emacs/files.texi
@@ -420,6 +420,9 @@ With prefix argument (@kbd{C-u}), mark the current buffer 
as changed.
 Save the current buffer with a specified file name (@code{write-file}).
 @item M-x set-visited-file-name
 Change the file name under which the current buffer will be saved.
+@item M-x rename-visited-file
+The same as @kbd{M-x set-visited-file-name}, but also rename the file
+the buffer if visiting (if any).
 @end table
 
 @kindex C-x C-s
diff --git a/etc/NEWS b/etc/NEWS
index 831486bb79..c7e0d03d6e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -352,6 +352,10 @@ match those regexps will be ignored by 
'switch-to-prev-buffer' and
 
 ** Menus
 
+** New command 'rename-visited-file'.
+This command renames the file visited by the current buffer by moving
+it to a new location, and also makes the buffer visit this new file.
+
 ---
 *** 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 cc38f4e921..1295c24c93 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4824,6 +4824,26 @@ Interactively, confirmation is required unless you 
supply a prefix argument."
     ;; 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.
+This command also sets the visited file name.  If the buffer
+isn't visiting any file, that's all it does.
+
+Interactively, this prompts for NEW-LOCATION."
+  (interactive
+   (list (if buffer-file-name
+             (read-file-name "Rename visited file to: ")
+           (read-file-name "Set visited file name: "
+                           default-directory
+                           (expand-file-name
+                            (file-name-nondirectory (buffer-name))
+                            default-directory)))))
+  (when (and buffer-file-name
+             (file-exists-p buffer-file-name))
+    (rename-file buffer-file-name new-location))
+  (set-visited-file-name new-location nil t))
+
 
 (defun file-extended-attributes (filename)
   "Return an alist of extended attributes of file FILENAME.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]