1

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.

Daanturo
  • 180
  • 8
  • Well, adding a script to KDE System Settings > Startup and Shutdown > Auto Start and choosing Run On: Logout work for me, and I guess ~/.bash_logout can handle ttys – Daanturo Feb 14 '20 at 15:47

1 Answers1

1

I am not quite sure about this answer, so I originally want to post it as a comment, but as you see, I don't have enough reputation to add a comment, so I have to post it as an answer.

Remember that I have made other changes to my .emacs.d, so it is possible that my answer is totally wrong. If it doesn't work for you, you may follow this link.

I have run into similar case as yours. I run emacs as daemon using systemd. And when I poweroff, recentf can't be save, but when I stop daemon manually, it can. Then I found something I ignored here.

To save time, the core is that Emacs built with gtk support is not a good choice when using Emacs as a daemon, because there is some bug.

You can also see Emacs itself say something about this, if you journalctl it:

Emacs might crash when run in daemon mode and the X11 connection is unexpectedly lost.
Using an Emacs configured with --with-x-toolkit=lucid does not have this problem.

So the point is clear now. Don't build your Emacs with gtk when using it as daemon.

For the solution, I think it may be interrelated to your distribution. As for me, on Gentoo, it is to enable useflag "motif" and disable useflag "athena" "gtk" "Xaw3d".

For others distribution, you may find it yourself.

The link I found say this:

USE flags for toolkits gtk, motif, athena, and aqua are mutually exclusive. Generally, USE="gtk" is a good choice. However, if intending to use Emacs as a daemon, USE="motif -athena -gtk -Xaw3d" or USE="athena Xaw3d -gtk -motif" is recommended instead because of bug #292471. USE="athena Xaw3d" resembles USE="gtk" very well. USE="aqua" is a special case applying only to Mac OS X.

C-Entropy
  • 143
  • 5