From: Jeremy Bryant via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 67313@debbugs.gnu.org
Subject: bug#67313: [PATCH] New command write-file-no-switch
Date: Tue, 21 Nov 2023 22:52:13 +0000 [thread overview]
Message-ID: <87v89uemrx.fsf@jeremybryant.net> (raw)
In-Reply-To: <83h6lfibye.fsf@gnu.org>
[-- 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
next prev parent reply other threads:[~2023-11-21 22:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87v89uemrx.fsf@jeremybryant.net \
--to=bug-gnu-emacs@gnu.org \
--cc=67313@debbugs.gnu.org \
--cc=eliz@gnu.org \
--cc=jb@jeremybryant.net \
/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 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.