Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get emacs to automatically load and save desktop from initial directory?

Tags:

emacs

I usually have 3-4 different projects that I work on at once. So I am trying to figure out how to get emacs to load the desktop from the folder that I open emacs from and also save to that file when I exit from that emacs instance.

All of the docs I have seen either describe how to get emacs to automatically open and save from a default location (which makes multiple desktops impossible), or to manually load and save the desktop to a specific directory (which I am doing now).

Thanks!

like image 774
patrickkidd Avatar asked Jul 23 '14 21:07

patrickkidd


2 Answers

Put this to your .emacs:

(setq your-own-path default-directory)
(if (file-exists-p
     (concat your-own-path ".emacs.desktop"))
    (desktop-read your-own-path))

(add-hook 'kill-emacs-hook
      `(lambda ()
        (desktop-save ,your-own-path t)))

Upd.: v. 2, ignore on demand.

(setq your-own-path default-directory)
(if (file-exists-p
     (concat your-own-path ".emacs.desktop"))
    (if (y-or-n-p "Read .emacs.desktop and add hook?")
    (progn
      (desktop-read your-own-path)
      (add-hook 'kill-emacs-hook
            `(lambda ()
               (desktop-save ,your-own-path t))))))
like image 74
artscan Avatar answered Sep 29 '22 09:09

artscan


I have developed a small set of functions to manage multiple desktops: desktop+

You might want to check it out. My workflow is not exactly the same as yours, though:

  1. I always run emacs from the same directory (I run it from a key binding in my window manager), meaning that I can not rely on the starting directory to know which desktop I want to work with

  2. the first time I work on a new project, I call M-xdesktop-create and provide a name. The desktop is then saved to a central location (under "~/.emacs.d/desktops" by default)

  3. each subsequent time I want to work with a saved desktop, I run M-xdesktop-load, and am provided with a list of saved sessions in which I can quickly retrieve the name of the desired session.

Sessions are always saved when emacs exits or you load another session.

like image 35
François Févotte Avatar answered Sep 29 '22 09:09

François Févotte