tobeannounced
tobeannounced

Reputation: 1999

Some emacs desktop-save questions: how to change it to save in ~/.emacs.d/.emacs.desktop

I have this set in my init.el

(desktop-save-mode 1)

This works great, only I was wondering:

Upvotes: 23

Views: 8884

Answers (3)

phils
phils

Reputation: 73355

I use the following, which works for me:

;; Automatically save and restore sessions
(setq desktop-dirname             "~/.emacs.d/desktop/"
      desktop-base-file-name      "emacs.desktop"
      desktop-base-lock-name      "lock"
      desktop-path                (list desktop-dirname)
      desktop-save                t
      desktop-files-not-to-save   "^$" ;reload tramp paths
      desktop-load-locked-desktop nil
      desktop-auto-save-timeout   30)
(desktop-save-mode 1)

Well, I actually set (desktop-save-mode 0) and then use M-x my-desktop to kick things off:

(defun my-desktop ()
  "Load the desktop and enable autosaving"
  (interactive)
  (let ((desktop-load-locked-desktop "ask"))
    (desktop-read)
    (desktop-save-mode 1)))

But that's because my session is frequently in excess of 100 files, mostly via tramp, and so I prefer to make loading it a manual task, and not clobber the desktop file otherwise :)

I recommend checking out the Emacs Wiki: http://www.emacswiki.org/emacs/DeskTop

There are some useful enhancements to the default functionality. In particular, I recommend adding some method of auto-saving your desktop mid-session, as it's really annoying if your system crashes when Emacs has been running for several days, and your desktop hasn't been saved in the interim.

Since Emacs 24.4 the desktop file is auto-saved periodically by default. See the desktop-auto-save-timeout variable (which I've also added to the block above). Thanks to GDP2 and Dexter Morgan for their comments on this.

Upvotes: 46

nexayq
nexayq

Reputation: 656

Wanted to share how my sessions are organized.

Requirements:

  1. Send file from terminal to custom emacs session
  2. Restore session from terminal
  3. Save session

Solution:

  1. Install package(plugin) workgroups2 -> https://github.com/pashinin/workgroups2

  2. Add following lisp code to your ~/.emacs.d/init.el or ~/.emacs:

->

(setq server-socket-dir "~/.emacs-local/server")

(defun nk-server-start (custom-server)
    ; (nk-server-start "abe")
    (setq server-name custom-server)
    (server-start) ; run emacs server
    (setq wg-session-file (concat "~/.emacs-local/sessions/" custom-server))
    ; (setq wg-session-file "~/.emacs-local/sessions/foo")
    (workgroups-mode 1)
    (wg-switch-to-workgroup custom-server)
    )
; Run file in specific server (foo)
    ; emacsclient -n callback.sh -s ~/.emacs-local/server/foo

; Show server name in title bar
(setq frame-title-format '("" "%b @ " server-name))
    ; https://www.emacswiki.org/emacs/FrameTitle

; ;; What to do on Emacs exit / workgroups-mode exit?
(setq wg-emacs-exit-save-behavior           'save)      ; Options: 'save 'ask nil
(setq wg-workgroups-mode-exit-save-behavior 'save)      ; Options: 'save 'ask nil

Function nk-server-start is called when emacs is started. It has one argument - sesssion name.

We can start emacs-session foo by running following command from terminal:

setsid emacs --eval '(nk-server-start "foo")' &

If we want to open file in session foo from terminal we need to run:

setsid emacsclient -n -s ~/.emacs-local/server/foo file.txt >> /dev/null &

When we close session, all buffers,settings, etc. are saved in file ~/.emacs-local/sessions/foo

When we run command setsid emacs --eval '(nk-server-start "foo")' & next time, all buffers will be restored

Because commands are large and I am lazy :) I made some scripts and added them to my $PATH in order to ease this:

em_start_foo.sh - Run session, used only once to start session

#!/bin/bash

setsid emacs --eval '(nk-server-start "foo")' &

em_foo.sh - Add files to session

#!/bin/bash

setsid emacsclient -n -s ~/.emacs-local/server/foo "$@" >> /dev/null &

Now we just run from terminal:

$ em_start_foo.sh                 # start foo session
$ em_foo.sh file_1.txt            # open file_1.txt in foo session
$ em_foo.sh file_2.txt file_3.txt # open file_2.txt and file_3.txt in foo session

Multiple sessions can be run in parallel of course.

Let's say we created also scripts em_start_foo_2.sh, em_start_foo_2.sh, em_start_foo_3.sh, em_start_foo_3.sh (somewhere in $PATH of course)

Then we can do something like this:

$ em_start_foo.sh          # start foo session
$ em_start_foo_2.sh        # start foo_2 session in separate emacs
$ em_foo.sh file_1.txt     # open file_1.txt in foo session
$ em_foo_2.sh a.txt b.txt  # open a.txt and b.txt in foo_2 session

$ em_start_foo_3.sh        # start foo_3 session
$ em_foo_3.sh tmp.txt      # open tmp.txt in foo_3 session

##### Close emacs foo_2 from gui - session is automatically saved ###

$ em_start_foo_2.sh        # start foo_2 session with all buffers restored

Package workgroups2 is really great!

My emacs init file with session options is available at: https://github.com/nexayq/dot_files/blob/master/emacs/dot_emacs_d/init.el

Upvotes: 0

jfs
jfs

Reputation: 414855

how can I change it to save the .emacs.desktop files into ~/.emacs.d instead of ~/

Customize desktop-dirname variable.

how can I stop it from asking me if I want to save

Customize desktop-save variable.

Upvotes: 3

Related Questions