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

From: Manuel Giraud <[email-masked]>
Date: Tue, 17 Sep 2024 15:15:26 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Manuel Giraud <[email-masked]>
>> Cc: [email-masked]
>> Date: Tue, 17 Sep 2024 10:19:41 +0200
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> > Can you try this and see if it solves the problem, and doesn't break
>> > the other uses of midnight-mode and or clean-buffer-list?
>> 
>> Thanks.  It solves the issue of activacting the midnight-mode: now
>> `clean-buffer-list' is just done once and won't run the next day.  But
>> now, when I do M-x midnight-mode, I get the following error:
>> 
>> --8<---------------cut here---------------start------------->8---
>> timer--activate: Invalid or uninitialized timer
>> --8<---------------cut here---------------end--------------->8---
>
> So not only did it activate the timer when loading the package, it
> also relied on that for the mode to work...
>
> The fix is to add
>
>   (midnight-delay-set 'midnight-delay midnight-delay)
>
> to midnight-mode function.

I tried building with the following patch but I get the same error when
doing M-x midnight-mode.

diff --git a/lisp/midnight.el b/lisp/midnight.el
index 6e1e7b5ccb7..a4b0ac00c83 100644
--- a/lisp/midnight.el
+++ b/lisp/midnight.el
@@ -57,6 +57,7 @@ midnight-mode
   ;; via setting 'midnight-delay', which calls 'midnight-delay-set',
   ;; which starts the timer.
   (when (timerp midnight-timer) (cancel-timer midnight-timer))
+  (midnight-delay-set 'midnight-delay midnight-delay)
   (if midnight-mode (timer-activate midnight-timer)))
 
 ;;; clean-buffer-list stuff
@@ -206,10 +207,11 @@ midnight-delay-set
   (cl-assert (eq symb 'midnight-delay) t
              "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)))
+  (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))))
 
 (defcustom midnight-delay 3600
   "The number of seconds after the midnight when the `midnight-timer' is run.
-- 
Manuel Giraud