From: Zachary Kanfer <zkanfer@gmail.com>
To: 56229@debbugs.gnu.org
Subject: bug#56229: title: add a function to move a file from one place to another
Date: Sun, 26 Jun 2022 00:49:33 -0400 [thread overview]
Message-ID: <CAFXT+RPqa-PN18tPAuSewr8Y10ue65CGJFX68i=uc+NDLNM2Vg@mail.gmail.com> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 1133 bytes --]
There's currently no good way to move a file from one place to another.
Emacs has #'write-file, which writes a buffer to a new location. But if the
buffer was previously associated with a file, it leaves that old file there.
There is #'rename-file, but there are some problems with it.
1. It is file-based. That is, it prompts twice: once for a file to move,
and a second time for the file location to move to. It would be easier if
we had a function that moved the current buffer.
2. It does not modify any buffers. After running #'rename-file, any buffers
with the old file are left open associated with the *old* file. Saving one
of these buffers results in the old location being written again.
So, attached is a patch for an interactive function that prompts for a new
location, then writes the current buffer to that location. Assuming it's
successful, and the buffer previously was associated with a file, it
deletes the old file.
The result is to move the file from one location to another. Because it
delegates to #'write-file, we get a lot of behavior for free, like ending
up in a buffer associated with the new file.
[-- Attachment #1.2: Type: text/html, Size: 1217 bytes --]
[-- Attachment #2: 0001-Add-a-function-to-move-a-file.patch --]
[-- Type: text/x-patch, Size: 1964 bytes --]
From 57078be72bc19ed693faa9bdf1a39408534a9e61 Mon Sep 17 00:00:00 2001
From: Zachary Kanfer <zkanfer@gmail.com>
Date: Sat, 25 Jun 2022 01:48:38 -0400
Subject: [PATCH] Add a function to move a file.
* lisp/files.el (move-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..7c96f05a99 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 move-file (new-location)
+ "Move the current file to NEW-LOCATION.
+
+Interactively, this prompts for NEW-LOCATION.
+
+This works whether or not the buffer was previously saved to a file."
+ (interactive (list (if buffer-file-name
+ (read-file-name "Move file to: ")
+ (read-file-name "Move 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))))
+
\f
(defun file-extended-attributes (filename)
"Return an alist of extended attributes of file FILENAME.
--
2.25.1
next reply other threads:[~2022-06-26 4:49 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-26 4:49 Zachary Kanfer [this message]
2022-06-26 14:50 ` bug#56229: title: add a function to move a file from one place to another Lars Ingebrigtsen
2022-06-26 15:16 ` Zachary Kanfer
2022-06-26 15:31 ` Lars Ingebrigtsen
2022-06-26 15:41 ` Visuwesh
2022-06-27 4:42 ` Zachary Kanfer
2022-06-27 7:55 ` Lars Ingebrigtsen
2022-06-28 3:24 ` Zachary Kanfer
2022-06-28 11:10 ` Eli Zaretskii
2022-06-28 3:24 ` Richard Stallman
2022-06-28 4:23 ` Zachary Kanfer
2022-06-28 12:15 ` Lars Ingebrigtsen
2022-06-29 1:17 ` Zachary Kanfer
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='CAFXT+RPqa-PN18tPAuSewr8Y10ue65CGJFX68i=uc+NDLNM2Vg@mail.gmail.com' \
--to=zkanfer@gmail.com \
--cc=56229@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).