From: Lars Ingebrigtsen Date: Mon, 23 May 2022 07:45:35 +0000 (+0200) Subject: Allow extending 'save-some-buffers' X-Git-Tag: emacs-29.0.90~1910^2~491 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=1cfb89dc79b76923c1c93cb44e3e4836b6a8c2bf;p=emacs.git Allow extending 'save-some-buffers' * lisp/abbrev.el (abbrev--possibly-save): Separated out from `save-some-buffers'. (save-some-buffers-functions): Add to the save function. * lisp/files.el (save-some-buffers-functions): New variable. (save-some-buffers): Use it. (save-buffers-kill-emacs): Also use it to see if we have something to save (bug#55579). --- diff --git a/etc/NEWS b/etc/NEWS index 80e867135e3..c146b746cfe 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1859,6 +1859,12 @@ functions. * Lisp Changes in Emacs 29.1 +** 'save-some-buffers' can now be extended to save other things. +Traditionally, 'save-some-buffers' saved buffers, and also saved +abbrevs. This has been generalized via the +'save-some-buffers-functions', and packages can now register things to +be saved. + ** Themes --- diff --git a/lisp/abbrev.el b/lisp/abbrev.el index 214f7435d91..3ee972869bc 100644 --- a/lisp/abbrev.el +++ b/lisp/abbrev.el @@ -1197,6 +1197,28 @@ This mode is for editing abbrevs in a buffer prepared by `edit-abbrevs', which see." :interactive nil) +(defun abbrev--possibly-save (query &optional arg) + ;; Query mode. + (if (eq query 'query) + (and save-abbrevs abbrevs-changed) + ;; Maybe save abbrevs, and record whether we either saved them or + ;; asked to. + (and save-abbrevs + abbrevs-changed + (progn + (if (or arg + (eq save-abbrevs 'silently) + (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name))) + (progn + (write-abbrev-file nil) + nil) + ;; Don't keep bothering user if they say no. + (setq abbrevs-changed nil) + ;; Inhibit message in `save-some-buffers'. + t))))) + +(add-hook 'save-some-buffers-functions #'abbrev--possibly-save) + (provide 'abbrev) ;;; abbrev.el ends here diff --git a/lisp/files.el b/lisp/files.el index 2b0dc54db8f..2aef4d9230a 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5849,7 +5849,18 @@ See `save-some-buffers' for PRED values." (funcall pred)))) buffer)) (buffer-list)))) - (delq nil buffers))) + (delq nil buffers))) + +(defvar save-some-buffers-functions nil + "Functions to be run by `save-some-buffers' after saving the buffers. +The functions can be called in two \"modes\", depending on the +first argument. If the first argument is `query', then the +function should return non-nil if there is something to be +saved (but it should not actually save anything). + +If the first argument is something else, then the function should +save according to the value of the second argument, which is the +ARG argument from `save-some-buffers'.") (defun save-some-buffers (&optional arg pred) "Save some modified file-visiting buffers. Asks user about each one. @@ -5875,7 +5886,10 @@ should return non-nil if that buffer should be considered. PRED defaults to the value of `save-some-buffers-default-predicate'. See `save-some-buffers-action-alist' if you want to -change the additional actions you can take on files." +change the additional actions you can take on files. + +The functions in `save-some-buffers-functions' will be called +after saving the buffers." (interactive "P") (unless pred (setq pred @@ -5891,7 +5905,7 @@ change the additional actions you can take on files." (lambda (buffer) (setq switched-buffer buffer))) queried autosaved-buffers - files-done abbrevs-done) + files-done inhibit-message) (unwind-protect (save-window-excursion (dolist (buffer (buffer-list)) @@ -5939,19 +5953,10 @@ change the additional actions you can take on files." (files--buffers-needing-to-be-saved pred) '("buffer" "buffers" "save") save-some-buffers-action-alist)) - ;; Maybe to save abbrevs, and record whether - ;; we either saved them or asked to. - (and save-abbrevs abbrevs-changed - (progn - (if (or arg - (eq save-abbrevs 'silently) - (y-or-n-p (format "Save abbrevs in %s? " - abbrev-file-name))) - (write-abbrev-file nil)) - ;; Don't keep bothering user if he says no. - (setq abbrevs-changed nil) - (setq abbrevs-done t))) - (or queried (> files-done 0) abbrevs-done + ;; Allow other things to be saved at this time, like abbrevs. + (dolist (func save-some-buffers-functions) + (setq inhibit-message (or (funcall func nil arg) inhibit-message))) + (or queried (> files-done 0) inhibit-message (cond ((null autosaved-buffers) (when (called-interactively-p 'any) @@ -7779,7 +7784,11 @@ If RESTART, restart Emacs after killing the current Emacs process." (interactive "P") ;; Don't use save-some-buffers-default-predicate, because we want ;; to ask about all the buffers before killing Emacs. - (when (files--buffers-needing-to-be-saved t) + (when (or (files--buffers-needing-to-be-saved t) + (catch 'need-save + (dolist (func save-some-buffers-functions) + (when (funcall func 'query) + (throw 'need-save t))))) (if (use-dialog-box-p) (pcase (x-popup-dialog t `("Unsaved Buffers"