0

I'm using persp-mode to save my workspace/desktop/opened buffers on Emacs close and restore it on Emacs start (configured via doom-emacs. However, sometimes I can't properly shutdown Emacs (e.g. laptop's sleep failure, empty battery etc.), so I would like persp-mode to save the workspace on every buffer list change. I did not find an appropriate configuration option in the persp-mode.el, but it's probably hackable, but I have no clue where to start looking for the right hook.

So how can I save my desktop on every change?

0

You could try (add-hook 'doom-switch-buffer-hook #'doom-save-session), however, serializing your workspace is a fairly expensive process and will add a notable delay to your everyday use of Emacs/Doom.

A less intrusive alternative might be to save on a timer, e.g.

;; runs after 10s of idle time
(run-with-idle-timer 10 t #'doom-save-session)

;; or every 10s, period
(run-at-time 10 t #'doom-save-session)

Combined with a timer, a third possibility is to save only when your battery is low, using the battery.el package (built into Emacs):

(defun save-on-low-battery ()
  (require 'battery)
  (let* ((data (and battery-status-function (funcall battery-status-function)))
         (percentage (car (read-from-string (cdr (assq ?p data))))))
    (when (and (numberp percentage)
               (<= percentage battery-load-critical))
      (doom-save-session))))

(run-at-time 10 t #'save-on-low-battery)

Some other potential hook candidates:

  • doom-switch-window-hook
  • window-configuration-change-hook
  • focus-out-hook

Hope that helps!

  • 1
    Running on timer is excellent, thanks (for the answer and for doom-emacs)! – Nikolai Prokoschenko Jul 14 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.