1

Writes to saveplace are not persisting after the emacs daemon stops.

Saveplace is configured in my init file as:

;; saveplace remembers your location in a file when saving files
(use-package saveplace
  :config
  (setq save-place-file (expand-file-name "saveplace" savefile-dir))
  ;; activate it for all buffers
  (save-place-mode t))

I'm using systemd for the emacs daemon:

[Unit]
Description=Emacs text editor
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/

[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

When I run kill-emacs or save-buffers-kill-emacs and then query the contents of saveplace, its contents accurately reflect the last cursor position in each buffer. However, if I then run $ systemctl --user restart emacs, the file reverts to the contents it had before the last emacs session. If I open saveplace in vim and write the file contents before restarting, they persist past the daemon restart. So, I thought it might be an issue with the buffers not flushing, but if I run $ sync saveplace and then restart the daemon, the correct contents do not persist.

What's going wrong here and how do I fix it?

MattHusz
  • 177
  • 8

2 Answers2

1

I fixed this by changing ExecStart in the systemd service file to:

ExecStart=/usr/bin/emacs --daemon

--daemon is equivalent to --bg-daemon, which starts the emacs daemon in the background (disconnects it from a terminal). I'm still unclear on why this works, however.

MattHusz
  • 177
  • 8
0

Untested, but this might be why:

(defun save-place--setup-hooks (add)
  [...]
    (unless noninteractive
      (add-hook 'kill-emacs-hook #'save-place-kill-emacs-hook))

Presumably save-place.el doesn't think it's OK for that hook to happen in a non-interactive context.

If you disagree, you could add the hook in your own init code after enabling the mode, and see how you get on?

phils
  • 48,657
  • 3
  • 76
  • 115
  • `save-place-kill-emacs-hook` is called when I run `kill-emacs`. This is consistent with the code snippet you provided. It also explains why the saveplace file contents are correct just prior to restarting the emacs systemd service. However, the file reverts to the old version when I stop the service. Maybe I'm missing your point here? – MattHusz Apr 01 '19 at 21:37
  • If `save-place-kill-emacs-hook` is running then my guess was wrong. (It seems that I misunderstood part of the question.) – phils Apr 01 '19 at 22:01
  • I would suggest taking systemd out of the equation entirely in order to establish whether or not this issue occurs regardless of that. – phils Apr 01 '19 at 22:02
  • I just confirmed that it's unrelated to systemd (ran `$ emacs --fg-daemon` directly). Interestingly, I'm seeing the message: `Wrote /home/matt/.emacs.d/savefile/saveplace` when I terminate the daemon with `C-c`. This makes it look like its deliberately overwriting the correct contents with the old contents when the daemon terminates. – MattHusz Apr 02 '19 at 00:51