]> git.eshelyaron.com Git - emacs.git/commitdiff
New user option auto-save-visited-mode-predicate
authorStefan Kangas <stefan@marxist.se>
Tue, 5 Jul 2022 11:34:24 +0000 (13:34 +0200)
committerStefan Kangas <stefan@marxist.se>
Tue, 5 Jul 2022 12:05:43 +0000 (14:05 +0200)
* 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
lisp/files.el

index 7967190c6e72ebf6e2a9b3142a688bb64c7251f6..5926148648b434fecd2948408192eb2140a2ff47 100644 (file)
--- 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.
index 881217501416daeab9cf0c115c73ef62ff95e24b..55c50c33b4da1f5a122bb1918821262a122c2055 100644 (file)
@@ -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