>From 6c91170beb549240325e04ed1f08de833c6bfd88 Mon Sep 17 00:00:00 2001 From: Charalampos Mitrodimas Date: Mon, 6 May 2024 21:31:08 +0300 Subject: [PATCH] Add rename-file-and-open function Introduce rename-file-and-open, a new function that renames a file and opens the renamed file immediately. This function was developed to make the renaming of a file more automated: when using rename-file, the renamed file is not automatically opened, requiring an additional step to open it manually. The rename-file-and-open function takes two required arguments: - file: the source file name (a string) - newname: the destination file name (a string) - ok-if-already-exists: control the behavior when the destination file already exists It calls rename-file to perform the actual renaming, handling any file-error that may occur by signaling a user-error with an appropriate error message. * lisp/files.el (rename-file-and-open): New function to rename a file and open it, since `rename-file' doesn't open the renamed file. --- lisp/files.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lisp/files.el b/lisp/files.el index c24e48e3db2..ded6e1b130f 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -7380,6 +7380,25 @@ rename-auto-save-file (recent-auto-save-p)) (rename-file osave buffer-auto-save-file-name t)))) +(defun rename-file-and-open (file newname &optional ok-if-already-exists) + "Rename FILE as NEWNAME and open the renamed file. +Both args must be strings. + +If file has names other than FILE, it continues to have those names. +If NEWNAME is a directory name, rename FILE to a like-named file under +NEWNAME. For NEWNAME to be recognized as a directory name, it should +end in a slash. + +Signal a `file-already-exists' error if a file NEWNAME already exists +unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. +An integer third arg means request confirmation if NEWNAME already exists." + (interactive "fRename file: \nGRename to file: \np") + (condition-case err + (rename-file file newname ok-if-already-exists) + (file-error + (user-error "Failed to rename file: %s" (error-message-string err)))) + (find-file newname)) + (defun make-auto-save-file-name () "Return file name to use for auto-saves of current buffer. Does not consider `auto-save-visited-file-name' as that variable is checked -- 2.39.3 (Apple Git-146)