unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Charalampos Mitrodimas <charmitro@posteo.net>
To: 70806@debbugs.gnu.org
Subject: bug#70806: [PATCH] Add rename-file-and-open function
Date: Mon, 06 May 2024 18:43:40 +0000	[thread overview]
Message-ID: <87cypyn5lv.fsf@posteo.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 796 bytes --]

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'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-rename-file-and-open-function.patch --]
[-- Type: text/patch, Size: 2452 bytes --]

From 6c91170beb549240325e04ed1f08de833c6bfd88 Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <charmitro@posteo.net>
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)


             reply	other threads:[~2024-05-06 18:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-06 18:43 Charalampos Mitrodimas [this message]
2024-05-06 18:59 ` bug#70806: [PATCH] Add rename-file-and-open function Eli Zaretskii
2024-05-06 23:48   ` Charalampos Mitrodimas
2024-05-18  8:39     ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87cypyn5lv.fsf@posteo.net \
    --to=charmitro@posteo.net \
    --cc=70806@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).