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

From: Eli Zaretskii <[email-masked]>
Date: Tue, 17 Sep 2024 16:46:02 +0300

> From: Michael Heerdegen <[email-masked]>
> Cc: Eli Zaretskii <[email-masked]>,  Manuel Giraud <[email-masked]>,
>   [email-masked]
> Date: Tue, 17 Sep 2024 15:17:45 +0200
> 
> Manuel Giraud via "Bug reports for GNU Emacs, the Swiss army knife of
> text editors" <[email-masked]> writes:
> 
> > > -  (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?
> >
> > 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:
> >
> > timer--activate: Invalid or uninitialized timer
> 
> Maybe we could instead check whether `midnight-mode' is on, Eli?

Sorry, I don't understand what you have in mind: check where?

What needs to be done is to make sure midnight-timer is a timer
(calling timer-create if not) before we call midnight-delay-set,
because it will only initialize the delay if midnight-timer is already
a timer.

Alternatively, we could call run-at-time directly from midnight-mode.

But I guess you meant some third possibility?