From 8edc3aa6156ff61c3659274cc9235d2bc5ac2313 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 27 Apr 2022 08:35:48 -0500 Subject: Also inhibit user-save-mode with predicates --- lisp/user-save.el | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'lisp') diff --git a/lisp/user-save.el b/lisp/user-save.el index 15e61a0..1c04c9d 100644 --- a/lisp/user-save.el +++ b/lisp/user-save.el @@ -29,10 +29,16 @@ This option is only useful is `user-save-mode' is active when Emacs is killed." :type 'boolean) -(defcustom user-save-inhibit '(special-mode) +(defcustom user-save-inhibit-modes '(special-mode) "List of modes to inhibit `user-save-mode' from activation in." :type '(repeat symbol)) +(defcustom user-save-inhibit-predicates '(user-save-non-file-buffer-p) + "List of predicates to inhibit `user-save-mode' from activation. +Each predicate will be called with no arguments, and if it +returns t, will inhibit `user-save-mode' from activating." + :type '(repeat function)) + (defvar user-save-hook nil "Hook to run when the user, not Emacs, saves the buffer.") @@ -47,7 +53,13 @@ This map shadows the default map for `save-buffer'.") "Run the hooks in `user-save-hook'. This does /not/ also save the buffer." (with-demoted-errors "User-save-hook error: %S" - (run-hooks 'user-save-hook))) + (run-hooks 'user-save-hook))) + +(defun user-save-non-file-buffer-p (&optional buffer-or-name) + "Return whether BUFFER-OR-NAME is a non-file buffer. +BUFFER-OR-NAME, if omitted, defaults to the current buffer." + (with-current-buffer (or buffer-or-name (current-buffer)) + (not (buffer-file-name)))) (defun user-save-buffer (&optional arg) "Save current buffer in visited file if modified. @@ -99,9 +111,11 @@ whether the buffer needs to be saved." (defun user-save-mode-in-some-buffers () "Enable `user-save-mode', but only in some buffers. The mode will not be enabled in buffers derived from modes in -`user-save-inhibit', or in the minibuffer." - (unless (or (cl-some #'derived-mode-p user-save-inhibit) - (minibufferp)) +`user-save-inhibit-modes', those for which +`user-save-inhibit-predicates' return t, or in the minibuffer." + (unless (or (minibufferp) + (cl-some #'derived-mode-p user-save-inhibit-modes) + (run-hook-with-args-until-failure 'user-save-inhibit-predicates)) (user-save-mode +1))) ;;;###autoload -- cgit 1.4.1-21-gabe81