Subject: bug#28945: 25.2; desktop auto save timer does not work

From: Eli Zaretskii <[email-masked]>
Date: Sat, 28 Oct 2017 15:12:13 +0300

> From: Peter Neidhardt <[email-masked]>
> Date: Sun, 22 Oct 2017 17:19:07 +0100
> 
> - Add (desktop-save-mode) to your init.el.
> 
> - Start Emacs.  Check the value of `window-configuration-change-hook':
> the _global_ value should contain `desktop-auto-save-set-timer'.
> 
> - Save the desktop.
> 
> - Quit Emacs.
> 
> - Restart Emacs.
> 
> - Check `window-configuration-change-hook': the timer is gone.

I tried this recipe, but couldn't reproduce the problem.  I wonder
what was missing from my reproduction experiment.  My .emacs for this
experiment had only one line:

  (desktop-save-mode 1)

Is this different from what you tried?  In my case, the timer is still
there after restarting Emacs.

> I've narrowed down the issue to `desktop-read':
> 
> 	;; ...
> 	(setq desktop-autosave-was-enabled
> 	  (memq 'desktop-auto-save-set-timer 'window-configuration-change-hook))
> 	(desktop-auto-save-disable)
> 	;; ...
> 	(if desktop-autosave-was-enabled (desktop-auto-save-enable))
> 
> The check is done on the _local_ value of
> `window-configuration-change-hook', thus `desktop-auto-save-enable' will
> never be re-run.

Did you somehow make changes to the local value of
window-configuration-change-hook?  In my case, both the local value
and the global value are identical.

> While we are at it, we might change the documentation and value of `desktop-auto-save-timeout':
> 
> 	Number of seconds idle time before auto-save of the desktop.
> 	The idle timer activates auto-saving only when window configuration changes.
> 
> This is terribly confusing, isn't it?  One might wonder whether it's a
> timer that saves every 30 seconds or the configuration change.
> 
> The actual logic is as follow:
> 
> - On every window configuration change, the timer is _reset_.
> 
> - When idle for `desktop-auto-save-timeout', the desktop is saved.  The
>   timer is run only _once_.

I've changed the doc string of desktop-auto-save-timeout as follows:

    "Number of seconds of idle time before auto-saving the desktop.
  The desktop will be auto-saved when this amount of idle time have
  passed after some change in the window configuration.
  This applies to an existing desktop file when `desktop-save-mode' is enabled.
  Zero or nil means disable auto-saving due to idleness."

And the doc string of desktop-auto-save-set-timer now says this:

    "Set the desktop auto-save timer.
  Cancel any previous timer.  When `desktop-auto-save-timeout' is a positive
  integer, start a new idle timer to call `desktop-auto-save' after that many
  seconds of idle time.
  This function is called from `window-configuration-change-hook'."

OK?

> In practice, this means that the user actually be idle for 30 seconds or
> else it won't get saved at all.

It will be saved when Emacs exits.

> Lastly, a minor nit: desktop.el adds a lambda to `after-init-hook'; can
> we turn this into a named function?

Why is that important?  This hook runs long before the user starts
interacting with Emacs, so there doesn't seem to be any good reason
for the user to look into what this function does.  Or am I missing
something?

Thanks.