Subject: bug#70806: [PATCH] Add rename-file-and-open function

From: Charalampos Mitrodimas <[email-masked]>
Date: Mon, 06 May 2024 18:43:40 +0000

Tags: patch

Hi,

The new function is similar to "rename-file", but it automatically opens
the file once it's renamed (or throws a relevant error).

I'm not sure if this addition is necessary, but I've had it for so many
years that I thought it might be useful to have it in Emacs by
default. I'm also not sure if it's in the correct position within
lisp/files.el. In any case, I'm submitting this patch for consideration.

--
C. Mitrodimas

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.38, cairo version 1.16.0) of 2024-05-06 built on
 Debian-bookworm-latest-amd64-base
Repository revision: 71395b7888f4d7b64be36a8cedb5b2b8c4819259
Repository branch: master
System Description: Debian GNU/Linux 12 (bookworm)

Configured using:
 'configure --with-native-compilation'

>From 6c91170beb549240325e04ed1f08de833c6bfd88 Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <[email-masked]>
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)