From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jeremy Bryant via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#67313: [PATCH] New command write-file-no-switch Date: Tue, 21 Nov 2023 22:52:13 +0000 Message-ID: <87v89uemrx.fsf@jeremybryant.net> References: <87zfz8dmk5.fsf@jeremybryant.net> <83h6lfibye.fsf@gnu.org> Reply-To: Jeremy Bryant Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38740"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 67313@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Nov 22 00:00:15 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r5Zj4-0009sm-SU for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 22 Nov 2023 00:00:15 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r5Zir-00035k-Kd; Tue, 21 Nov 2023 18:00:01 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r5Ziq-00034u-MO for bug-gnu-emacs@gnu.org; Tue, 21 Nov 2023 18:00:00 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r5Ziq-0002Kk-Cz for bug-gnu-emacs@gnu.org; Tue, 21 Nov 2023 18:00:00 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r5Zis-0001hi-Vx for bug-gnu-emacs@gnu.org; Tue, 21 Nov 2023 18:00:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Jeremy Bryant Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 21 Nov 2023 23:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67313 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 67313-submit@debbugs.gnu.org id=B67313.17006075496453 (code B ref 67313); Tue, 21 Nov 2023 23:00:02 +0000 Original-Received: (at 67313) by debbugs.gnu.org; 21 Nov 2023 22:59:09 +0000 Original-Received: from localhost ([127.0.0.1]:57668 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r5Zi0-0001g0-KZ for submit@debbugs.gnu.org; Tue, 21 Nov 2023 17:59:09 -0500 Original-Received: from out-180.mta0.migadu.com ([2001:41d0:1004:224b::b4]:39954) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r5Zhy-0001fr-GE for 67313@debbugs.gnu.org; Tue, 21 Nov 2023 17:59:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jeremybryant.net; s=key1; t=1700607541; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=1adk6BUhEVxMKy6y18b/OjeizZNVBVZsnyD0m/IKACY=; b=U2WUv40ZyJg7wS+OZZ0bU/5dZjGgmy9kiHV/dt4d9mDw2c4ZWnfhPtnqWfX41bHhPlyuhT 9Xhol6q6iYaIxhw0dkd15pic2fo2OdzI50WK7ZD8q9fNhiK1IieHFJlmhklKvxnCiPlq6Q Z0CvYOdngnBuObzkoXUGOB7IpTvCM4ZPrJqBec4p2FneQJPdZsmVoz6Z7NW0MfCd3KBW5H Ip8RIQevmydu4nivmeM0NjHYmstp7JqeDCPcUFu3mpsnVuQhAvE9+kIyeYHzidaYQnrDgp WgYlH6xkLEE1stQq6axbMBIy4r0F0YqcyH3XYRmKicpsMnbByOlE0sZo3irltg== X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: <83h6lfibye.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:274744 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii 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" >> >> 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. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-write-file-no-visit-Like-write-file-but-without-visi.patch >From bc8c42fc8a2c6f9a1aa52712bfa1f81eab8d52ba Mon Sep 17 00:00:00 2001 From: Jeremy Bryant 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 \\\\[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 --=-=-=--