(define-minor-mode desktop-save-mode "Toggle desktop saving (Desktop Save mode).
With a prefix argument ARG, enable Desktop Save mode if ARG is positive, and disable it otherwise. If called from Lisp, enable the mode if ARG is omitted or nil.
If Desktop Save mode is enabled, the state of Emacs is saved from
one session to another. See variable `desktop-save' and function `desktop-read' for details." :global t :group 'desktop (if desktop-save-mode (if desktop-auto-save (add-hook 'auto-save-hook 'desktop-owner-save-in-desktop-dir))
(remove-hook 'auto-save-hook 'desktop-owner-save-in-desktop-dir)))
Remaining issues:
1. Here I have added the function desktop-owner-save-in-desktop-dir which only saves the desktop if the current process owns it.
2. There was some discussion over whether by default we should use a separate timer or auto-save-hook. For now I've used the latter.
3. It was desired to save the desktop only when it changed. Juri Linkov previously said "This is easy to do once we will decide what auto-saving method to use", so, since I don't know how to do it anyway, and since the auto-saving method is not yet decided, I didn't implement that yet.
I've been happily using essentially the above code (mostly Juri's, with a handful of lines by me) since this thread went quiet over five years ago.