From ac7f76528f2f2e2f6e77bc25e7040eb3b07e45c0 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Tue, 5 Jul 2022 13:34:24 +0200 Subject: [PATCH] New user option auto-save-visited-mode-predicate * lisp/files.el (auto-save-visited-mode-predicate): New defcustom. (auto-save-visited-mode): Use above new variable as a predicate to decide whether or not to save a buffer. --- etc/NEWS | 7 +++++++ lisp/files.el | 32 +++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index 7967190c6e7..5926148648b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2001,6 +2001,13 @@ Set it to nil to exclude line numbering from kills and copies. This option provides a mechanism to selectively disable font-lock keyword-driven fontifications. +--- +*** New user option 'auto-save-visited-predicate'. +This user option is a predicate function which is called by +'auto-save-visited-mode' to decide whether or not to save a buffer. +You can use it to automatically save only specific buffers, for +example buffers using a particular mode or in some directory. + +++ *** New package vtable.el for formatting tabular data. This package allows formatting data using variable-pitch fonts. diff --git a/lisp/files.el b/lisp/files.el index 88121750141..55c50c33b4d 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -443,6 +443,31 @@ idle for `auto-save-visited-interval' seconds." (when auto-save--timer (timer-set-idle-time auto-save--timer value :repeat)))) +(defcustom auto-save-visited-predicate nil + "Predicate function for `auto-save-visited-mode'. + +This function is called (with no argument) once in each +file-visiting buffer. Only those buffers are saved, where +the predicate function returns a non-nil value. + +For example, you could add this to your Init file to only save +files that are both in Org mode and in a particular directory: + + (setq auto-save-visited-predicate + (lambda () (and (eq major-mode \\='org-mode) + (string-match \"^/home/skangas/org/\" + buffer-file-name)))) + +If the value of this variable is not a function, it is ignored. +This is the same as having a predicate that always returns +true." + :group 'auto-save + :type '(choice :tag "Function:" + (const :tag "No extra predicate" :value nil) + (function :tag "Predicate function" :value always)) + :risky t + :version "29.1") + (define-minor-mode auto-save-visited-mode "Toggle automatic saving to file-visiting buffers on or off. @@ -453,6 +478,9 @@ Unlike `auto-save-mode', this mode will auto-save buffer contents to the visited files directly and will also run all save-related hooks. See Info node `Saving' for details of the save process. +You can use `auto-save-visited-predicate' to control which +buffers are saved. + You can also set the buffer-local value of the variable `auto-save-visited-mode' to nil. A buffer where the buffer-local value of this variable is nil is ignored for the purpose of @@ -472,7 +500,9 @@ For more details, see Info node `(emacs) Auto Save Files'." (and buffer-file-name auto-save-visited-mode (not (and buffer-auto-save-file-name - auto-save-visited-file-name)))))))) + auto-save-visited-file-name)) + (or (not (functionp auto-save-visited-predicate)) + (funcall auto-save-visited-predicate)))))))) ;; The 'set' part is so we don't get a warning for using this variable ;; above, while still catching code that _sets_ the variable to get -- 2.39.5