* bug#56229: title: add a function to move a file from one place to another
@ 2022-06-26 4:49 Zachary Kanfer
2022-06-26 14:50 ` Lars Ingebrigtsen
0 siblings, 1 reply; 13+ messages in thread
From: Zachary Kanfer @ 2022-06-26 4:49 UTC (permalink / raw)
To: 56229
[-- 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
^ permalink raw reply related [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-26 4:49 bug#56229: title: add a function to move a file from one place to another Zachary Kanfer
@ 2022-06-26 14:50 ` Lars Ingebrigtsen
2022-06-26 15:16 ` Zachary Kanfer
0 siblings, 1 reply; 13+ messages in thread
From: Lars Ingebrigtsen @ 2022-06-26 14:50 UTC (permalink / raw)
To: Zachary Kanfer; +Cc: 56229
Zachary Kanfer <zkanfer@gmail.com> writes:
> 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.
I think this sounds like a useful command, but I'm not sure about the
name:
[...]
> +(defun move-file (new-location)
> + "Move the current file to NEW-LOCATION.
This is about moving the file the current buffer is visiting -- there's
no such thing as a "current file" in Emacs -- so the command name (and
doc string) should reflect that. I don't really have a suggestion for a
better command name, though. Anybody?
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-26 14:50 ` Lars Ingebrigtsen
@ 2022-06-26 15:16 ` Zachary Kanfer
2022-06-26 15:31 ` Lars Ingebrigtsen
0 siblings, 1 reply; 13+ messages in thread
From: Zachary Kanfer @ 2022-06-26 15:16 UTC (permalink / raw)
To: Lars Ingebrigtsen; +Cc: 56229
[-- Attachment #1: Type: text/plain, Size: 1300 bytes --]
Thanks! I agree the name is not exactly right.
It's more complicated because if the buffer is *not* visiting a file, the
right thing for this function to just save the buffer to the location
selected. I don't know if the name that needs to take that into account.
Is there a name for the file a buffer is visiting? Maybe
"move-associated-file"? "move-visited-file"?
On Sun, Jun 26, 2022, 10:50 AM Lars Ingebrigtsen <larsi@gnus.org> wrote:
> Zachary Kanfer <zkanfer@gmail.com> writes:
>
> > 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.
>
> I think this sounds like a useful command, but I'm not sure about the
> name:
>
> [...]
>
> > +(defun move-file (new-location)
> > + "Move the current file to NEW-LOCATION.
>
> This is about moving the file the current buffer is visiting -- there's
> no such thing as a "current file" in Emacs -- so the command name (and
> doc string) should reflect that. I don't really have a suggestion for a
> better command name, though. Anybody?
>
> --
> (domestic pets only, the antidote for overdose, milk.)
> bloggy blog: http://lars.ingebrigtsen.no
>
[-- Attachment #2: Type: text/html, Size: 1994 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-26 15:16 ` Zachary Kanfer
@ 2022-06-26 15:31 ` Lars Ingebrigtsen
2022-06-26 15:41 ` Visuwesh
0 siblings, 1 reply; 13+ messages in thread
From: Lars Ingebrigtsen @ 2022-06-26 15:31 UTC (permalink / raw)
To: Zachary Kanfer; +Cc: 56229
Zachary Kanfer <zkanfer@gmail.com> writes:
> It's more complicated because if the buffer is *not* visiting a file,
> the right thing for this function to just save the buffer to the
> location selected. I don't know if the name that needs to take that
> into account.
>
> Is there a name for the file a buffer is visiting? Maybe
> "move-associated-file"? "move-visited-file"?
I like `move-visited-file'. Or perhaps `rename-visited-file'?
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-26 15:31 ` Lars Ingebrigtsen
@ 2022-06-26 15:41 ` Visuwesh
2022-06-27 4:42 ` Zachary Kanfer
0 siblings, 1 reply; 13+ messages in thread
From: Visuwesh @ 2022-06-26 15:41 UTC (permalink / raw)
To: Lars Ingebrigtsen; +Cc: Zachary Kanfer, 56229
[ஞாயிறு ஜூன் 26, 2022] Lars Ingebrigtsen wrote:
> Zachary Kanfer <zkanfer@gmail.com> writes:
>
>> It's more complicated because if the buffer is *not* visiting a file,
>> the right thing for this function to just save the buffer to the
>> location selected. I don't know if the name that needs to take that
>> into account.
>>
>> Is there a name for the file a buffer is visiting? Maybe
>> "move-associated-file"? "move-visited-file"?
>
> I like `move-visited-file'. Or perhaps `rename-visited-file'?
I think `rename-visited-file' would fit in better especially since we
already have `rename-file'; it also parallels dired's R command.
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-26 15:41 ` Visuwesh
@ 2022-06-27 4:42 ` Zachary Kanfer
2022-06-27 7:55 ` Lars Ingebrigtsen
0 siblings, 1 reply; 13+ messages in thread
From: Zachary Kanfer @ 2022-06-27 4:42 UTC (permalink / raw)
To: Visuwesh; +Cc: Lars Ingebrigtsen, 56229
[-- Attachment #1.1: Type: text/plain, Size: 902 bytes --]
Attached a patch with the function name changed to `rename-visited-file`.
I like the analogy to `rename-file`.
On Sun, Jun 26, 2022 at 11:41 AM Visuwesh <visuweshm@gmail.com> wrote:
> [ஞாயிறு ஜூன் 26, 2022] Lars Ingebrigtsen wrote:
>
> > Zachary Kanfer <zkanfer@gmail.com> writes:
> >
> >> It's more complicated because if the buffer is *not* visiting a file,
> >> the right thing for this function to just save the buffer to the
> >> location selected. I don't know if the name that needs to take that
> >> into account.
> >>
> >> Is there a name for the file a buffer is visiting? Maybe
> >> "move-associated-file"? "move-visited-file"?
> >
> > I like `move-visited-file'. Or perhaps `rename-visited-file'?
>
> I think `rename-visited-file' would fit in better especially since we
> already have `rename-file'; it also parallels dired's R command.
>
[-- Attachment #1.2: Type: text/html, Size: 1412 bytes --]
[-- Attachment #2: 0001-Add-a-function-to-rename-the-file-visited-by-the-cur.patch --]
[-- Type: text/x-patch, Size: 2050 bytes --]
From f8d1473824b7120a1d0f1440cf0179ffebebbc0e 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 rename the file visited by the current
buffer.
* lisp/files.el (rename-visited-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..278f80679a 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 rename-visited-file (new-location)
+ "Rename the file visited by the current buffer to NEW-LOCATION.
+
+Interactively, this prompts for NEW-LOCATION.
+
+If the file has not been visited, this works similarly to #'write-file".
+ (interactive (list (if buffer-file-name
+ (read-file-name "Rename file to: ")
+ (read-file-name "Rename 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
^ permalink raw reply related [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-27 4:42 ` Zachary Kanfer
@ 2022-06-27 7:55 ` Lars Ingebrigtsen
2022-06-28 3:24 ` Zachary Kanfer
2022-06-28 3:24 ` Richard Stallman
0 siblings, 2 replies; 13+ messages in thread
From: Lars Ingebrigtsen @ 2022-06-27 7:55 UTC (permalink / raw)
To: Zachary Kanfer; +Cc: 56229, Visuwesh
Zachary Kanfer <zkanfer@gmail.com> writes:
> I think `rename-visited-file' would fit in better especially since we
> already have `rename-file'; it also parallels dired's R command.
Thanks. A couple of things:
[...]
> +** New command 'move-file'.
> +This command moves a file to a new location.
Should be updated.
[...]
> + (let ((old-location (buffer-file-name)))
> + (write-file new-location t)
> + (when (and old-location
> + (file-exists-p new-location))
> + (delete-file old-location))))
Probably want to check whether old-location exists?
Anyway, apparently there's something up with the copyright
assignment/disclaimer? Your entry in the file says "**NEEDS New
DISCLAIMER**" -- do you know what that's about?
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
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
1 sibling, 1 reply; 13+ messages in thread
From: Zachary Kanfer @ 2022-06-28 3:24 UTC (permalink / raw)
To: Lars Ingebrigtsen; +Cc: 56229, Visuwesh
[-- Attachment #1.1: Type: text/plain, Size: 2299 bytes --]
> > +** New command 'move-file'.
> > +This command moves a file to a new location.
> Should be updated.
+1
> > + (let ((old-location (buffer-file-name)))
> > + (write-file new-location t)
> > + (when (and old-location
> > + (file-exists-p new-location))
> > + (delete-file old-location))))
>
> Probably want to check whether old-location exists?
I was checking whether the new locations exists to ensure the write
succeeded. If the write fails, I think we should leave the old file in
place.
#'delete-file does not error when given a nonexistent location, but I
suppose it could do so in the future. So I don't think we *need* to check
whether old-location exists, but it's certainly safer to do so. I'll add it!
Attached is a patch with the changes.
> Anyway, apparently there's something up with the copyright
> assignment/disclaimer? Your entry in the file says "**NEEDS New
> DISCLAIMER**" -- do you know what that's about?
I had moved jobs, and was attempting to get my new company to sign the
disclaimer. (My manager had not wanted it to get signed). I've since
changed teams inside the company, and got the disclaimer signed. After
sending the signed disclaimer a week or two ago, Craig Topham confirmed I
was good to go -- perhaps something didn't get updated here?
On Mon, Jun 27, 2022 at 3:55 AM Lars Ingebrigtsen <larsi@gnus.org> wrote:
> Zachary Kanfer <zkanfer@gmail.com> writes:
>
> > I think `rename-visited-file' would fit in better especially since we
> > already have `rename-file'; it also parallels dired's R command.
>
> Thanks. A couple of things:
>
> [...]
>
> > +** New command 'move-file'.
> > +This command moves a file to a new location.
>
> Should be updated.
>
>
> [...]
>
> > + (let ((old-location (buffer-file-name)))
> > + (write-file new-location t)
> > + (when (and old-location
> > + (file-exists-p new-location))
> > + (delete-file old-location))))
>
> Probably want to check whether old-location exists?
>
> Anyway, apparently there's something up with the copyright
> assignment/disclaimer? Your entry in the file says "**NEEDS New
> DISCLAIMER**" -- do you know what that's about?
>
> --
> (domestic pets only, the antidote for overdose, milk.)
> bloggy blog: http://lars.ingebrigtsen.no
>
[-- Attachment #1.2: Type: text/html, Size: 3298 bytes --]
[-- Attachment #2: 0001-Add-a-function-to-rename-the-file-visited-by-the-cur.patch --]
[-- Type: application/x-patch, Size: 2156 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-28 3:24 ` Zachary Kanfer
@ 2022-06-28 11:10 ` Eli Zaretskii
0 siblings, 0 replies; 13+ messages in thread
From: Eli Zaretskii @ 2022-06-28 11:10 UTC (permalink / raw)
To: Zachary Kanfer; +Cc: larsi, 56229, visuweshm
> Cc: 56229@debbugs.gnu.org, Visuwesh <visuweshm@gmail.com>
> From: Zachary Kanfer <zkanfer@gmail.com>
> Date: Mon, 27 Jun 2022 23:24:29 -0400
>
> > Anyway, apparently there's something up with the copyright
> > assignment/disclaimer? Your entry in the file says "**NEEDS New
> > DISCLAIMER**" -- do you know what that's about?
>
> I had moved jobs, and was attempting to get my new company to sign the disclaimer. (My manager had not
> wanted it to get signed). I've since changed teams inside the company, and got the disclaimer signed. After
> sending the signed disclaimer a week or two ago, Craig Topham confirmed I was good to go -- perhaps
> something didn't get updated here?
The latest copyright file reflects what you describe, so we are good
in that department.
Thanks.
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-27 7:55 ` Lars Ingebrigtsen
2022-06-28 3:24 ` Zachary Kanfer
@ 2022-06-28 3:24 ` Richard Stallman
2022-06-28 4:23 ` Zachary Kanfer
1 sibling, 1 reply; 13+ messages in thread
From: Richard Stallman @ 2022-06-28 3:24 UTC (permalink / raw)
To: Lars Ingebrigtsen; +Cc: visuweshm, zkanfer, 56229
[[[ To any NSA and FBI agents reading my email: please consider ]]]
[[[ whether defending the US Constitution against all enemies, ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]
We already have a command, `set-visited-file-name'. Does that do
what people are looking for? If not, why not?
--
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-28 3:24 ` Richard Stallman
@ 2022-06-28 4:23 ` Zachary Kanfer
2022-06-28 12:15 ` Lars Ingebrigtsen
0 siblings, 1 reply; 13+ messages in thread
From: Zachary Kanfer @ 2022-06-28 4:23 UTC (permalink / raw)
To: rms; +Cc: Lars Ingebrigtsen, 56229, Visuwesh
[-- Attachment #1: Type: text/plain, Size: 801 bytes --]
It differs in a few ways:
1. set-visited-file-name doesn't save the file to the new location.
2. set-visited-file-name leaves the file on disk in the old location.
On Mon, Jun 27, 2022 at 11:24 PM Richard Stallman <rms@gnu.org> wrote:
> [[[ To any NSA and FBI agents reading my email: please consider ]]]
> [[[ whether defending the US Constitution against all enemies, ]]]
> [[[ foreign or domestic, requires you to follow Snowden's example. ]]]
>
> We already have a command, `set-visited-file-name'. Does that do
> what people are looking for? If not, why not?
>
> --
> Dr Richard Stallman (https://stallman.org)
> Chief GNUisance of the GNU Project (https://gnu.org)
> Founder, Free Software Foundation (https://fsf.org)
> Internet Hall-of-Famer (https://internethalloffame.org)
>
>
>
[-- Attachment #2: Type: text/html, Size: 1461 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-28 4:23 ` Zachary Kanfer
@ 2022-06-28 12:15 ` Lars Ingebrigtsen
2022-06-29 1:17 ` Zachary Kanfer
0 siblings, 1 reply; 13+ messages in thread
From: Lars Ingebrigtsen @ 2022-06-28 12:15 UTC (permalink / raw)
To: Zachary Kanfer; +Cc: Visuwesh, rms, 56229
Zachary Kanfer <zkanfer@gmail.com> writes:
> It differs in a few ways:
>
> 1. set-visited-file-name doesn't save the file to the new location.
> 2. set-visited-file-name leaves the file on disk in the old location.
But Richard has a good point -- we can punt to set-visited-file-name to
change the visited file name (and this also fixes changing the major
mode etc if we're changing the name from .c to .el or something).
So I've now adjusted your function to use `rename-file' and then call
`set-visited-file-name' and pushed to Emacs 29.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 13+ messages in thread
* bug#56229: title: add a function to move a file from one place to another
2022-06-28 12:15 ` Lars Ingebrigtsen
@ 2022-06-29 1:17 ` Zachary Kanfer
0 siblings, 0 replies; 13+ messages in thread
From: Zachary Kanfer @ 2022-06-29 1:17 UTC (permalink / raw)
To: Lars Ingebrigtsen; +Cc: Visuwesh, rms, 56229
[-- Attachment #1: Type: text/plain, Size: 792 bytes --]
Cool, thanks for applying it! Cheers.
On Tue, Jun 28, 2022 at 8:15 AM Lars Ingebrigtsen <larsi@gnus.org> wrote:
> Zachary Kanfer <zkanfer@gmail.com> writes:
>
> > It differs in a few ways:
> >
> > 1. set-visited-file-name doesn't save the file to the new location.
> > 2. set-visited-file-name leaves the file on disk in the old location.
>
> But Richard has a good point -- we can punt to set-visited-file-name to
> change the visited file name (and this also fixes changing the major
> mode etc if we're changing the name from .c to .el or something).
>
> So I've now adjusted your function to use `rename-file' and then call
> `set-visited-file-name' and pushed to Emacs 29.
>
> --
> (domestic pets only, the antidote for overdose, milk.)
> bloggy blog: http://lars.ingebrigtsen.no
>
[-- Attachment #2: Type: text/html, Size: 1294 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2022-06-29 1:17 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-26 4:49 bug#56229: title: add a function to move a file from one place to another Zachary Kanfer
2022-06-26 14:50 ` 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
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.