3

I am using desktop-save-mode and desktop-change-dir to handle multiple desktops. This works fine, except for the case when I have modified some buffers, and then run desktop-change-dir before having saved the changes.

I thought that desktop-change-dir would save any buffers before switching desktops, instead I get a question Buffer <name> modified; kill anyway ? (yes/no). (Note that this question is somewhat different from the question you get when killing emacs, in that it does not ask if you would like to save the buffer or not.)

If I type yes to the question, the modification to the buffer is lost, if I type no the buffer is carried on to the next desktop. (Neither of these options is desired, so I have to press C-g to abort and save the file manually)

Instead I would like to have the modified buffers saved automatically. Is this possible?

Here is a minimal example:

$ emacs -Q -l init.el &

where init.el is:

(defvar my-desktop-dir1
  (file-name-as-directory (expand-file-name "desktop1")))

(defvar my-desktop-dir2
  (file-name-as-directory (expand-file-name "desktop2")))

(defun my-switch-desktop ()
  (interactive)
  ;; this line is commented out since `desktop-save' should be
  ;; called by `desktop-change-dir'
  ;;(desktop-save my-desktop-dir1 t t)
  (desktop-change-dir my-desktop-dir2))

(require 'desktop)
(setq desktop-path (list my-desktop-dir1))
(setq desktop-save t)
(desktop-save-mode 1)
(desktop-read)
  • 1
    as an aside, since you seem to be using multiple desktops and switching from one to another, you might want to try desktop+: it's a small package that I wrote to do just that. If you like it, I can easily incorporate your save-some-buffers solution to it. – ffevotte Apr 25 '15 at 8:47
1

As a workaround, you can use save-some-buffers:

(defun my-switch-desktop ()
  (interactive)
  ;; If save-silently-p is non-nil, saves all the file-visiting buffers
  ;;  without querying the user.
  (let ((save-silently t)) 
    (save-some-buffers save-silently))
  (desktop-change-dir my-desktop-dir2))

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.