]> git.eshelyaron.com Git - emacs.git/commitdiff
Add missing custom :set to 'savehist-autosave-interval'
authorshipmints <shipmints@gmail.com>
Sat, 25 Jan 2025 18:12:56 +0000 (13:12 -0500)
committerEshel Yaron <me@eshelyaron.com>
Mon, 3 Feb 2025 11:15:04 +0000 (12:15 +0100)
* lisp/savehist.el (savehist-autosave-interval): Correctly
reset 'savehist-timer' when 'savehist-autosave-interval' changes
via setopt or a Customize command.  (Bug#75834)

(cherry picked from commit c7889d0545d5e684fc5cec5d50e249ab9c24da44)

lisp/savehist.el

index 153e2db87061ca09259f23675ff18658b4caea49..7d755bc50471f6b852a43454a09aade36047d098 100644 (file)
@@ -96,11 +96,37 @@ the user's privacy."
   :type '(choice (natnum :tag "Specify")
                  (const :tag "Use default" :value nil)))
 
+(defvar savehist-timer nil)
+
+(defun savehist--cancel-timer ()
+  "Cancel `savehist-autosave' timer, if set."
+  (when (timerp savehist-timer)
+    (cancel-timer savehist-timer))
+  (setq savehist-timer nil))
+
+(defun savehist--manage-timer ()
+  "Set or cancel an invocation of `savehist-autosave' on a timer.
+If `savehist-mode' is enabled, set the timer, otherwise cancel the timer.
+This should not cause noticeable delays for users -- `savehist-autosave'
+executes in under 5 ms on my system."
+  (if (and savehist-mode
+           savehist-autosave-interval
+           (null savehist-timer))
+      (setq savehist-timer
+           (run-with-timer savehist-autosave-interval
+                           savehist-autosave-interval #'savehist-autosave))
+    (savehist--cancel-timer)))
+
 (defcustom savehist-autosave-interval (* 5 60)
   "The interval between autosaves of minibuffer history.
-If set to nil, disables timer-based autosaving."
+If set to nil, disables timer-based autosaving.
+Use `setopt' or Customize commands to set this option."
   :type '(choice (const :tag "Disabled" nil)
-                 (integer :tag "Seconds")))
+                 (integer :tag "Seconds"))
+  :set (lambda (sym val)
+         (set-default sym val)
+         (savehist--cancel-timer)
+         (savehist--manage-timer)))
 
 (defcustom savehist-mode-hook nil
   "Hook called when Savehist mode is turned on."
@@ -122,8 +148,6 @@ unwise, unless you know what you are doing.")
 
 ;; Internal variables.
 
-(defvar savehist-timer nil)
-
 (defvar savehist-last-checksum nil)
 
 (defvar savehist-minibuffer-history-variables nil
@@ -197,23 +221,14 @@ Installs `savehist-autosave' in `kill-emacs-hook' and on a timer.
 To undo this, call `savehist-uninstall'."
   (add-hook 'minibuffer-setup-hook #'savehist-minibuffer-hook)
   (add-hook 'kill-emacs-hook #'savehist-autosave)
-  ;; Install an invocation of savehist-autosave on a timer.  This
-  ;; should not cause noticeable delays for users -- savehist-autosave
-  ;; executes in under 5 ms on my system.
-  (when (and savehist-autosave-interval
-            (null savehist-timer))
-    (setq savehist-timer
-         (run-with-timer savehist-autosave-interval
-                         savehist-autosave-interval #'savehist-autosave))))
+  (savehist--manage-timer))
 
 (defun savehist-uninstall ()
   "Undo installing savehist.
 Normally invoked by calling `savehist-mode' to unset the minor mode."
   (remove-hook 'minibuffer-setup-hook #'savehist-minibuffer-hook)
   (remove-hook 'kill-emacs-hook #'savehist-autosave)
-  (when savehist-timer
-    (cancel-timer savehist-timer)
-    (setq savehist-timer nil)))
+  (savehist--manage-timer))
 
 (defvar savehist--has-given-file-warning nil)
 (defun savehist-save (&optional auto-save)