I am trying to use desktop-save-mode together with emacs daemon.
;; save desktop before quitting
(add-hook 'kill-emacs-hook (lambda () (desktop-save "~/.emacs.d/")))
But when I log out of the graphical session (KDE/SDDM) or shut down/reboot, emacs doesn't automatically save current desktop, and leaves .emacs.desktop.lock intact.
I thought about defining a shortcut that execute "(kill-emacs)" and then bring leaving options up, but then I have to exclusively use this one to shutdown, which is not good in case other methods are needed. Or bind (kill-emacs) to a key, but sometimes I only temporarily close Emacs using meta + f4 or C-x C-c, then quitting without remembering to save the desktop.
Currently, I'm struggling to modify a systemd service as Arch Wiki suggests:
[Service]
Type=simple
ExecStart=/usr/bin/emacs --fg-daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=on-failure
[Install]
WantedBy=default.target
But "ExecStop" is executed only when the service is manually stopped, leaving desktop unsaved after logging out.
Or perhaps is there a way to save desktop every time a buffer is close? I tried
(add-hook 'kill-buffer-hook (lambda () (desktop-save "~/.emacs.d/")))
but it didn't work.