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

From: Manuel Giraud <[email-masked]>
Date: Sat, 21 Sep 2024 14:55:05 +0200

Stefan Kangas <[email-masked]> writes:

> Eli Zaretskii <[email-masked]> writes:
>
>> So please install all the other changes on master and this one on the
>> release branch.
>
> I did the second part.
>
> Manuel, could you please resubmit your patch as formatted by

Here.  I also add the NEWS entry as suggested by Eli.  Thanks.
>From 8d668972fad20b1baba3d7a56f3612e0d7689e25 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Sat, 21 Sep 2024 14:51:21 +0200
Subject: [PATCH] clean-buffer-list don't activate midnight-mode

Bug#73291

* lisp/midnight.el (midnight-mode): Call `midnight-delay-set'
again when mode is activated.
(midnight-delay-set): Only start the timer if the mode is turned
on.
* etc/NEWS: Document the change in behaviour.
---
 etc/NEWS         |  7 +++++++
 lisp/midnight.el | 19 ++++++++++---------
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 6c97ffacd15..46abc908c6e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -390,6 +390,13 @@ functionality of the standard 'xref' commands in TeX buffers.  You can
 restore the standard 'etags' backend with the 'M-x xref-etags-mode'
 toggle.
 
+** Midnight mode
+
+---
+*** Change for activating the mode
+Putting (require 'midnight) in your init file no longer activates the
+mode.  Now, one needs to say (midnight-mode +1) instead.
+
 
 * New Modes and Packages in Emacs 31.1
 
diff --git a/lisp/midnight.el b/lisp/midnight.el
index ea30f5462f2..e5dd7221591 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.
-- 
2.46.0

-- 
Manuel Giraud