From: Jeremy Bryant via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 67313@debbugs.gnu.org
Subject: bug#67313: [PATCH] New command write-file-no-switch
Date: Mon, 20 Nov 2023 23:33:27 +0000 [thread overview]
Message-ID: <87zfz8dmk5.fsf@jeremybryant.net> (raw)
[-- 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
next reply other threads:[~2023-11-20 23:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-20 23:33 Jeremy Bryant via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-11-21 11:26 ` bug#67313: [PATCH] New command write-file-no-switch 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
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=87zfz8dmk5.fsf@jeremybryant.net \
--to=bug-gnu-emacs@gnu.org \
--cc=67313@debbugs.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 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).