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

From: Eli Zaretskii <[email-masked]>
Date: Mon, 16 Sep 2024 16:09:22 +0300

> Date: Mon, 16 Sep 2024 09:49:43 +0200
> From:  Manuel Giraud via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> It seems that calling `clean-buffer-list' once activates
> `midnight-mode'.  How to reproduce it:
> 
>     - emacs -Q
>     - Open some buffers
>     - M-x clean-buffer-list ;; Obviously not much buffers are killed
>     - Wait one day and go into your emacs window
>     - In the *Messages* buffer, you can see that cleaning occured
>       recently
> 
> I think that one call to `clean-buffer-list' should only clean the
> buffer list once.  The documentation does not seem to imply otherwise.

This is because clean-buffer-list is autoloaded, and loading
midnight.el evaluates midnight-delay, which :set function activates
the midnight timer.

I think the solution is to modify midnight-delay-set to activate the
timer only if is already non-nil.  Like this:

diff --git a/lisp/midnight.el b/lisp/midnight.el
index bfa99d6..73d049a 100644
--- a/lisp/midnight.el
+++ b/lisp/midnight.el
@@ -205,10 +205,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.

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?