unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#67313: [PATCH] New command write-file-no-switch
@ 2023-11-20 23:33 Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-11-21 11:26 ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-11-20 23:33 UTC (permalink / raw)
  To: 67313

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

Tags: patch

By default write-file changes the buffer to point to the new filename,
but a user may wish to simply do the equivalent of write-file without
switching, as a sort of backup file.

The attached is a patch concept to introduce the idea.  Please let me
know if this makes sense to introduce a new command rather than
modifying the existing one (which may be less convenient).  I understand
there would be additional things to do for new commands, but would like
to propose the idea first.  Any suggestions welcome.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-New-command-write-file-no-switch.patch --]
[-- Type: text/x-diff, Size: 3913 bytes --]

From d6eec95e5a5bd31c10f4031b4febd65021938973 Mon Sep 17 00:00:00 2001
From: Jeremy Bryant <jb@jeremybryant.net>
Date: Mon, 20 Nov 2023 23:30:49 +0000
Subject: [PATCH] New command write-file-no-switch

* lisp/files.el (write-file-no-switch): New command like write-file
but without switching to the new filename.
---
 lisp/files.el | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/lisp/files.el b/lisp/files.el
index 1cdcec23b11..bc314b8fb95 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4949,6 +4949,78 @@ write-file
     ;; the one at the old location.
     (vc-refresh-state)))
 
+(defun write-file-no-switch (filename &optional confirm)
+  "Like write-file but without switching to the new filename.
+...
+Write current buffer into file FILENAME.
+This makes the buffer visit that file, and marks it as not modified.
+
+Interactively, prompt for FILENAME.
+If you specify just a directory name as FILENAME, that means to write
+to a file in that directory.  In this case, the base name of the file
+is the same as that of the file visited in the buffer, or the buffer
+name sans leading directories, if any, if the buffer is not already
+visiting a file.
+
+You can also yank the file name into the minibuffer to edit it,
+using \\<minibuffer-local-map>\\[next-history-element].
+
+If optional second arg CONFIRM is non-nil, this function
+asks for confirmation before overwriting an existing file.
+Interactively, confirmation is required unless you supply a prefix argument."
+  (interactive
+   (list (if buffer-file-name
+	     (read-file-name "Write file: "
+			     nil nil nil nil)
+	   (read-file-name "Write file: " default-directory
+			   (expand-file-name
+			    (file-name-nondirectory (buffer-name))
+			    default-directory)
+			   nil nil))
+	 (not current-prefix-arg)))
+
+  (copy-file buffer-file-name filename)
+  ;; (let ((old-modes
+  ;;        (and buffer-file-name
+  ;;             ;; File may have gone away; ignore errors in that case.
+  ;;             (ignore-errors (file-modes buffer-file-name)))))
+  ;;   (or (null filename) (string-equal filename "")
+  ;;       (progn
+  ;;         ;; If arg is a directory name,
+  ;;         ;; use the default file name, but in that directory.
+  ;;         (if (directory-name-p filename)
+  ;;             (setq filename (concat filename
+  ;;       			     (file-name-nondirectory
+  ;;       			      (or buffer-file-name (buffer-name))))))
+  ;;         (and confirm
+  ;;              (file-exists-p filename)
+  ;;              ;; NS does its own confirm dialog.
+  ;;              (not (and (eq (framep-on-display) 'ns)
+  ;;       	         (listp last-nonmenu-event)
+  ;;       	         use-dialog-box))
+  ;;              (or (y-or-n-p (format-message
+  ;;                             "File `%s' exists; overwrite? " filename))
+  ;;       	   (user-error "Canceled")))
+  ;;         (set-visited-file-name filename (not confirm))))
+  ;;   (set-buffer-modified-p t)
+  ;;   ;; Make buffer writable if file is writable.
+  ;;   (and buffer-file-name
+  ;;        (file-writable-p buffer-file-name)
+  ;;        (setq buffer-read-only nil))
+  ;;   (save-buffer)
+  ;;   ;; If the old file was executable, then make the new file
+  ;;   ;; executable, too.
+  ;;   (when (and old-modes
+  ;;              (not (zerop (logand #o111 old-modes))))
+  ;;     (set-file-modes buffer-file-name
+  ;;                     (logior (logand #o111 old-modes)
+  ;;                             (file-modes buffer-file-name))))
+  ;;   ;; 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.
 This command also sets the visited file name.  If the buffer
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* bug#67313: [PATCH] New command write-file-no-switch
  2023-11-20 23:33 bug#67313: [PATCH] New command write-file-no-switch Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-11-21 11:26 ` Eli Zaretskii
  2023-11-21 22:52   ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2023-11-21 11:26 UTC (permalink / raw)
  To: Jeremy Bryant; +Cc: 67313

> Date: Mon, 20 Nov 2023 23:33:27 +0000
> From:  Jeremy Bryant via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
> 
> By default write-file changes the buffer to point to the new filename,
> but a user may wish to simply do the equivalent of write-file without
> switching, as a sort of backup file.
> 
> The attached is a patch concept to introduce the idea.  Please let me
> know if this makes sense to introduce a new command rather than
> modifying the existing one (which may be less convenient).  I understand
> there would be additional things to do for new commands, but would like
> to propose the idea first.  Any suggestions welcome.

Why is it not enough to be able to use "M-x write-region"?

In any case, a new command is definitely overkill, IMO.

> +  (copy-file buffer-file-name filename)

This doesn't write the buffer to the file, it copies the file visited
by the buffer to the new file.  So if the buffer has unsaved edits,
they will not be written, unlike what write-file does.

Also, what if this command is invoked from a buffer that doesn't visit
any file, and thus buffer-file-name is nil?

Thanks.





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#67313: [PATCH] New command write-file-no-switch
  2023-11-21 11:26 ` Eli Zaretskii
@ 2023-11-21 22:52   ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-11-24  8:04     ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-11-21 22:52 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 67313

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


Eli Zaretskii <eliz@gnu.org> writes:

>> Date: Mon, 20 Nov 2023 23:33:27 +0000
>> From:  Jeremy Bryant via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>> 
>> By default write-file changes the buffer to point to the new filename,
>> but a user may wish to simply do the equivalent of write-file without
>> switching, as a sort of backup file.
>> 
>> The attached is a patch concept to introduce the idea.  Please let me
>> know if this makes sense to introduce a new command rather than
>> modifying the existing one (which may be less convenient).  I understand
>> there would be additional things to do for new commands, but would like
>> to propose the idea first.  Any suggestions welcome.
>
> Why is it not enough to be able to use "M-x write-region"?
>
> In any case, a new command is definitely overkill, IMO.
>
>> +  (copy-file buffer-file-name filename)
>
> This doesn't write the buffer to the file, it copies the file visited
> by the buffer to the new file.  So if the buffer has unsaved edits,
> they will not be written, unlike what write-file does.
>
> Also, what if this command is invoked from a buffer that doesn't visit
> any file, and thus buffer-file-name is nil?
>
> Thanks.

Thank you for pointing out write-region, it is indeed more appropriate,
replacing the line above by

(write-region nil nil filename)


The attached patch is the prototype of how this may be used.




[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-write-file-no-visit-Like-write-file-but-without-visi.patch --]
[-- Type: text/x-diff, Size: 3842 bytes --]

From bc8c42fc8a2c6f9a1aa52712bfa1f81eab8d52ba Mon Sep 17 00:00:00 2001
From: Jeremy Bryant <jb@jeremybryant.net>
Date: Tue, 21 Nov 2023 22:53:35 +0000
Subject: [PATCH] write-file-no-visit:  Like write-file but without visiting
 file

* lisp/files.el (write-file-no-visit):  New function
---
 lisp/files.el | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/lisp/files.el b/lisp/files.el
index bc314b8fb95..28bea7b853a 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5021,6 +5021,78 @@ write-file-no-switch
   ;;   )
   )
 
+(defun write-file-no-visit (filename &optional confirm)
+  "Like write-file but without switching to the new filename.
+...
+Write current buffer into file FILENAME.
+This makes the buffer visit that file, and marks it as not modified.
+
+Interactively, prompt for FILENAME.
+If you specify just a directory name as FILENAME, that means to write
+to a file in that directory.  In this case, the base name of the file
+is the same as that of the file visited in the buffer, or the buffer
+name sans leading directories, if any, if the buffer is not already
+visiting a file.
+
+You can also yank the file name into the minibuffer to edit it,
+using \\<minibuffer-local-map>\\[next-history-element].
+
+If optional second arg CONFIRM is non-nil, this function
+asks for confirmation before overwriting an existing file.
+Interactively, confirmation is required unless you supply a prefix argument."
+  (interactive
+   (list (if buffer-file-name
+	     (read-file-name "Write file: "
+			     nil nil nil nil)
+	   (read-file-name "Write file: " default-directory
+			   (expand-file-name
+			    (file-name-nondirectory (buffer-name))
+			    default-directory)
+			   nil nil))
+	 (not current-prefix-arg)))
+
+  (write-region nil nil filename)
+  ;; (let ((old-modes
+  ;;        (and buffer-file-name
+  ;;             ;; File may have gone away; ignore errors in that case.
+  ;;             (ignore-errors (file-modes buffer-file-name)))))
+  ;;   (or (null filename) (string-equal filename "")
+  ;;       (progn
+  ;;         ;; If arg is a directory name,
+  ;;         ;; use the default file name, but in that directory.
+  ;;         (if (directory-name-p filename)
+  ;;             (setq filename (concat filename
+  ;;       			     (file-name-nondirectory
+  ;;       			      (or buffer-file-name (buffer-name))))))
+  ;;         (and confirm
+  ;;              (file-exists-p filename)
+  ;;              ;; NS does its own confirm dialog.
+  ;;              (not (and (eq (framep-on-display) 'ns)
+  ;;       	         (listp last-nonmenu-event)
+  ;;       	         use-dialog-box))
+  ;;              (or (y-or-n-p (format-message
+  ;;                             "File `%s' exists; overwrite? " filename))
+  ;;       	   (user-error "Canceled")))
+  ;;         (set-visited-file-name filename (not confirm))))
+  ;;   (set-buffer-modified-p t)
+  ;;   ;; Make buffer writable if file is writable.
+  ;;   (and buffer-file-name
+  ;;        (file-writable-p buffer-file-name)
+  ;;        (setq buffer-read-only nil))
+  ;;   (save-buffer)
+  ;;   ;; If the old file was executable, then make the new file
+  ;;   ;; executable, too.
+  ;;   (when (and old-modes
+  ;;              (not (zerop (logand #o111 old-modes))))
+  ;;     (set-file-modes buffer-file-name
+  ;;                     (logior (logand #o111 old-modes)
+  ;;                             (file-modes buffer-file-name))))
+  ;;   ;; 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.
 This command also sets the visited file name.  If the buffer
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* bug#67313: [PATCH] New command write-file-no-switch
  2023-11-21 22:52   ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-11-24  8:04     ` Eli Zaretskii
  2023-11-30 23:18       ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2023-11-24  8:04 UTC (permalink / raw)
  To: Jeremy Bryant; +Cc: 67313

> From: Jeremy Bryant <jb@jeremybryant.net>
> Cc: 67313@debbugs.gnu.org
> Date: Tue, 21 Nov 2023 22:52:13 +0000
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > Why is it not enough to be able to use "M-x write-region"?
> >
> > In any case, a new command is definitely overkill, IMO.
> 
> Thank you for pointing out write-region, it is indeed more appropriate,
> replacing the line above by
> 
> (write-region nil nil filename)
> 
> 
> The attached patch is the prototype of how this may be used.

Thanks.  As stated above, I'm not sure I agree we need a new command.
write-region already exists, as a command, and I personally use it
quite a lot in these cases.

Does anyone else think we need an additional command for saving a
buffer to a different file?





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#67313: [PATCH] New command write-file-no-switch
  2023-11-24  8:04     ` Eli Zaretskii
@ 2023-11-30 23:18       ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-12-01  8:38         ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-11-30 23:18 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 67313

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



>> > Why is it not enough to be able to use "M-x write-region"?
>> >
>> > In any case, a new command is definitely overkill, IMO.
>> 
>> Thank you for pointing out write-region, it is indeed more appropriate,
>> replacing the line above by
>> 
>> (write-region nil nil filename)
>> 
>> 
>> The attached patch is the prototype of how this may be used.
>
> Thanks.  As stated above, I'm not sure I agree we need a new command.
> write-region already exists, as a command, and I personally use it
> quite a lot in these cases.
>
> Does anyone else think we need an additional command for saving a
> buffer to a different file?


Eli, another way is also to clarify the manual to explain write-region.
Second patch attached for consideration.  This may help users.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Elisp-manual-Mention-write-region-for-whole-buffer.patch --]
[-- Type: text/x-diff, Size: 1069 bytes --]

From 1c9569bb702b20490dc5f9333f14e14b3d4c2f38 Mon Sep 17 00:00:00 2001
From: Jeremy Bryant <jb@jeremybryant.net>
Date: Thu, 30 Nov 2023 23:14:46 +0000
Subject: [PATCH] Elisp manual:  Mention write-region for whole buffer

* doc/emacs/files.texi (Save Commands):  Mention write-region
---
 doc/emacs/files.texi | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi
index 29cc22e7557..f0160c692da 100644
--- a/doc/emacs/files.texi
+++ b/doc/emacs/files.texi
@@ -547,6 +547,10 @@ Save Commands
 to that major mode, in most cases.  The command
 @code{set-visited-file-name} also does this.  @xref{Choosing Modes}.
 
+   If you wish to save the current buffer to a different file without
+visiting it, use @code{mark-whole-buffer} (@kbd C-x h), then @kbd{M-x
+write-region} (@pxref{Misc File Ops}
+
   If Emacs is about to save a file and sees that the date of the latest
 version on disk does not match what Emacs last read or wrote, Emacs
 notifies you of this fact, because it probably indicates a problem caused
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* bug#67313: [PATCH] New command write-file-no-switch
  2023-11-30 23:18       ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-12-01  8:38         ` Eli Zaretskii
  2023-12-01 23:11           ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2023-12-01  8:38 UTC (permalink / raw)
  To: Jeremy Bryant; +Cc: 67313

> From: Jeremy Bryant <jb@jeremybryant.net>
> Cc: 67313@debbugs.gnu.org
> Date: Thu, 30 Nov 2023 23:18:59 +0000
> 
> > Thanks.  As stated above, I'm not sure I agree we need a new command.
> > write-region already exists, as a command, and I personally use it
> > quite a lot in these cases.
> >
> > Does anyone else think we need an additional command for saving a
> > buffer to a different file?
> 
> 
> Eli, another way is also to clarify the manual to explain write-region.
> Second patch attached for consideration.  This may help users.

Thanks, that's a no-brainer.  Installed on the emacs-29 branch (after
fixing some markup and punctuation issues).

Should we close this bug now?





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#67313: [PATCH] New command write-file-no-switch
  2023-12-01  8:38         ` Eli Zaretskii
@ 2023-12-01 23:11           ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 7+ messages in thread
From: Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-12-01 23:11 UTC (permalink / raw)
  To: Eli Zaretskii, 67313-done; +Cc: 67313



Eli Zaretskii <eliz@gnu.org> writes:

>> From: Jeremy Bryant <jb@jeremybryant.net>
>> Cc: 67313@debbugs.gnu.org
>> Date: Thu, 30 Nov 2023 23:18:59 +0000
>> 
>> > Thanks.  As stated above, I'm not sure I agree we need a new command.
>> > write-region already exists, as a command, and I personally use it
>> > quite a lot in these cases.
>> >
>> > Does anyone else think we need an additional command for saving a
>> > buffer to a different file?
>> 
>> 
>> Eli, another way is also to clarify the manual to explain write-region.
>> Second patch attached for consideration.  This may help users.
>
> Thanks, that's a no-brainer.  Installed on the emacs-29 branch (after
> fixing some markup and punctuation issues).
>
> Should we close this bug now?

Yes, closing bug as fixed by this addition to the manual.

(noted usage of @w)





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-12-01 23:11 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-20 23:33 bug#67313: [PATCH] New command write-file-no-switch Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-21 11:26 ` Eli Zaretskii
2023-11-21 22:52   ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-24  8:04     ` Eli Zaretskii
2023-11-30 23:18       ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-12-01  8:38         ` Eli Zaretskii
2023-12-01 23:11           ` Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors

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).