From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: 28412@debbugs.gnu.org
Subject: bug#28412: 26.0.50; Let save-some-buffers accept write-contents-functions
Date: Sun, 10 Sep 2017 14:50:15 -0700 [thread overview]
Message-ID: <87bmmikyug.fsf@ericabrahamsen.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 400 bytes --]
This is about letting `save-some-buffers' check
`write-contents-functions' before insisting on a buffer having a file to
write to.
There was a conversations about this on emacs.devel:
https://lists.gnu.org/archive/html/emacs-devel/2017-05/msg00653.html
The patch I ended up with is attached.
I'm reporting this just so the patch (and the idea) doesn't get lost. I
still think it's worth doing.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-First-whack-at-write-contents-functions-for-non-file.patch --]
[-- Type: text/x-diff, Size: 7052 bytes --]
From af4f811439785113fe2be71f499006776958755b Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Thu, 25 May 2017 15:28:19 +0800
Subject: [PATCH] First whack at write-contents-functions for non-file buffers
---
lisp/files.el | 106 +++++++++++++++++++++++++++++++---------------------------
1 file changed, 56 insertions(+), 50 deletions(-)
diff --git a/lisp/files.el b/lisp/files.el
index 8ac1993754..c074fa7995 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4943,29 +4943,14 @@ basic-save-buffer
(if (buffer-base-buffer)
(set-buffer (buffer-base-buffer)))
(if (or (buffer-modified-p)
- ;; handle the case when no modification has been made but
- ;; the file disappeared since visited
+ ;; Handle the case when no modification has been made but
+ ;; the file disappeared since visited.
(and buffer-file-name
(not (file-exists-p buffer-file-name))))
(let ((recent-save (recent-auto-save-p))
setmodes)
- ;; If buffer has no file name, ask user for one.
- (or buffer-file-name
- (let ((filename
- (expand-file-name
- (read-file-name "File to save in: "
- nil (expand-file-name (buffer-name))))))
- (if (file-exists-p filename)
- (if (file-directory-p filename)
- ;; Signal an error if the user specified the name of an
- ;; existing directory.
- (error "%s is a directory" filename)
- (unless (y-or-n-p (format-message
- "File `%s' exists; overwrite? "
- filename))
- (error "Canceled"))))
- (set-visited-file-name filename)))
- (or (verify-visited-file-modtime (current-buffer))
+ (or (null buffer-file-name)
+ (verify-visited-file-modtime (current-buffer))
(not (file-exists-p buffer-file-name))
(yes-or-no-p
(format
@@ -4977,6 +4962,7 @@ basic-save-buffer
(save-excursion
(and (> (point-max) (point-min))
(not find-file-literally)
+ (null buffer-read-only)
(/= (char-after (1- (point-max))) ?\n)
(not (and (eq selective-display t)
(= (char-after (1- (point-max))) ?\r)))
@@ -4989,41 +4975,60 @@ basic-save-buffer
(save-excursion
(goto-char (point-max))
(insert ?\n))))
- ;; Support VC version backups.
- (vc-before-save)
;; Don't let errors prevent saving the buffer.
(with-demoted-errors (run-hooks 'before-save-hook))
- (or (run-hook-with-args-until-success 'write-contents-functions)
- (run-hook-with-args-until-success 'local-write-file-hooks)
- (run-hook-with-args-until-success 'write-file-functions)
- ;; If a hook returned t, file is already "written".
- ;; Otherwise, write it the usual way now.
- (let ((dir (file-name-directory
- (expand-file-name buffer-file-name))))
- (unless (file-exists-p dir)
- (if (y-or-n-p
- (format-message
- "Directory `%s' does not exist; create? " dir))
- (make-directory dir t)
- (error "Canceled")))
- (setq setmodes (basic-save-buffer-1))))
+ ;; Give `write-contents-functions' a chance to
+ ;; short-circuit the whole process.
+ (unless (run-hook-with-args-until-success 'write-contents-functions)
+ ;; If buffer has no file name, ask user for one.
+ (or buffer-file-name
+ (let ((filename
+ (expand-file-name
+ (read-file-name "File to save in: "
+ nil (expand-file-name (buffer-name))))))
+ (if (file-exists-p filename)
+ (if (file-directory-p filename)
+ ;; Signal an error if the user specified the name of an
+ ;; existing directory.
+ (error "%s is a directory" filename)
+ (unless (y-or-n-p (format-message
+ "File `%s' exists; overwrite? "
+ filename))
+ (error "Canceled"))))
+ (set-visited-file-name filename)))
+ ;; Support VC version backups.
+ (vc-before-save)
+ (or (run-hook-with-args-until-success 'local-write-file-hooks)
+ (run-hook-with-args-until-success 'write-file-functions)
+ ;; If a hook returned t, file is already "written".
+ ;; Otherwise, write it the usual way now.
+ (let ((dir (file-name-directory
+ (expand-file-name buffer-file-name))))
+ (unless (file-exists-p dir)
+ (if (y-or-n-p
+ (format-message
+ "Directory `%s' does not exist; create? " dir))
+ (make-directory dir t)
+ (error "Canceled")))
+ (setq setmodes (basic-save-buffer-1)))))
;; Now we have saved the current buffer. Let's make sure
;; that buffer-file-coding-system is fixed to what
;; actually used for saving by binding it locally.
- (if save-buffer-coding-system
- (setq save-buffer-coding-system last-coding-system-used)
- (setq buffer-file-coding-system last-coding-system-used))
- (setq buffer-file-number
- (nthcdr 10 (file-attributes buffer-file-name)))
- (if setmodes
- (condition-case ()
- (progn
- (unless
- (with-demoted-errors
- (set-file-modes buffer-file-name (car setmodes)))
- (set-file-extended-attributes buffer-file-name
- (nth 1 setmodes))))
- (error nil))))
+ (when buffer-file-name
+ (if save-buffer-coding-system
+ (setq save-buffer-coding-system last-coding-system-used)
+ (setq buffer-file-coding-system last-coding-system-used))
+ (setq buffer-file-number
+ (nthcdr 10 (file-attributes buffer-file-name)))
+ (if setmodes
+ (condition-case ()
+ (progn
+ (unless
+ (with-demoted-errors
+ (set-file-modes buffer-file-name (car setmodes)))
+ (set-file-extended-attributes buffer-file-name
+ (nth 1 setmodes))))
+ (error nil)))))
;; If the auto-save file was recent before this command,
;; delete it now.
(delete-auto-save-file-if-necessary recent-save)
@@ -5255,7 +5260,8 @@ save-some-buffers
(and pred
(progn
(set-buffer buffer)
- (and buffer-offer-save (> (buffer-size) 0)))))
+ (and buffer-offer-save (> (buffer-size) 0))))
+ write-contents-functions)
(or (not (functionp pred))
(with-current-buffer buffer (funcall pred)))
(if arg
--
2.13.0
next reply other threads:[~2017-09-10 21:50 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-10 21:50 Eric Abrahamsen [this message]
[not found] ` <handler.28412.B.15050803219580.ack@debbugs.gnu.org>
2017-09-10 22:01 ` bug#28412: Acknowledgement (26.0.50; Let save-some-buffers accept write-contents-functions) Eric Abrahamsen
2017-09-11 15:03 ` Eli Zaretskii
2017-09-11 21:41 ` Eric Abrahamsen
2017-09-12 14:41 ` Eli Zaretskii
2017-09-12 23:18 ` Eric Abrahamsen
2017-09-18 16:16 ` bug#28412: 26.0.50; Let save-some-buffers accept write-contents-functions Kaushal Modi
2017-09-18 18:04 ` Eli Zaretskii
2017-09-18 18:14 ` Eric Abrahamsen
2017-09-18 19:25 ` Eli Zaretskii
2017-09-18 20:30 ` Eric Abrahamsen
2017-09-18 19:12 ` Eric Abrahamsen
2017-09-18 19:23 ` Kaushal Modi
2017-09-18 20:53 ` Eric Abrahamsen
2017-09-18 21:48 ` Kaushal Modi
2017-09-19 16:13 ` Eli Zaretskii
2017-09-25 14:46 ` Kaushal Modi
2017-10-19 15:25 ` Eric Abrahamsen
2017-09-19 16:09 ` Eli Zaretskii
2017-09-18 19:28 ` Eli Zaretskii
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=87bmmikyug.fsf@ericabrahamsen.net \
--to=eric@ericabrahamsen.net \
--cc=28412@debbugs.gnu.org \
/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).