Subject: bug#73291: 31.0.50; `clean-buffer-list' activates `midnight-mode'

From: Manuel Giraud <[email-masked]>
Date: Wed, 18 Sep 2024 11:06:32 +0200

Hi,

I think the following patch does the trick.  Here is what I tested with
it:

        - A call to `clean-buffer-list' cleans up the list once and does
          not activate the midnight-timer.

        - The midnight mode is toggled on/off by calling
          `midnight-mode'.

        - Calling (customize-variable 'midnight-delay) stops and
          restarts the midnight-timer with the updated delay.

Maybe the line ":initialize #'custom-initialize-default" could be
removed, I don't know.  WDYT?

diff --git a/lisp/midnight.el b/lisp/midnight.el
index 6e1e7b5ccb7..ff1392abc05 100644
--- a/lisp/midnight.el
+++ b/lisp/midnight.el
@@ -52,12 +52,11 @@ midnight-mode
   "Non-nil means run `midnight-hook' at midnight."
   :global t
   :initialize #'custom-initialize-default
-  ;; Disable first, since the ':initialize' function above already
-  ;; starts the timer when the mode is turned on for the first time,
-  ;; via setting 'midnight-delay', which calls 'midnight-delay-set',
-  ;; which starts the timer.
-  (when (timerp midnight-timer) (cancel-timer midnight-timer))
-  (if midnight-mode (timer-activate midnight-timer)))
+  ;; Call `midnight-delay-set' again because it takes care of starting
+  ;; the timer if the mode is on.  The ':initialize' function above
+  ;; (which ends up calling `midnight-delay-set') did not know yet if
+  ;; the mode was on or not.
+  (midnight-delay-set 'midnight-delay midnight-delay))
 
 ;;; clean-buffer-list stuff
 
@@ -207,9 +206,11 @@ midnight-delay-set
              "Invalid argument to `midnight-delay-set': `%s'")
   (set symb tm)
   (when (timerp midnight-timer) (cancel-timer midnight-timer))
-  (setq midnight-timer
-        (run-at-time (if (numberp tm) (+ (midnight-next) tm) tm)
-                     midnight-period #'run-hooks 'midnight-hook)))
+  ;; Only start the timer if the mode is turned on.
+  (when midnight-mode
+    (setq midnight-timer
+          (run-at-time (if (numberp tm) (+ (midnight-next) tm) tm)
+                       midnight-period #'run-hooks 'midnight-hook))))
 
 (defcustom midnight-delay 3600
   "The number of seconds after the midnight when the `midnight-timer' is run.
-- 
Manuel Giraud