-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave-desktop.el
77 lines (65 loc) · 2.58 KB
/
save-desktop.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(require 'desktop)
(defvar my-desktop-session-dir
(concat (getenv "HOME") "/.emacs.d/desktop-sessions/")
"*Directory to save desktop sessions in")
(defvar my-desktop-session-name-hist nil
"Desktop session name history")
(defun my-desktop-save (&optional name)
"Save desktop by name."
(interactive)
(unless name
(setq name (my-desktop-get-session-name "Save session" t)))
(when name
(make-directory (concat my-desktop-session-dir name) t)
(desktop-save (concat my-desktop-session-dir name) t)))
(defun my-desktop-save-and-clear ()
"Save and clear desktop."
(interactive)
(call-interactively 'my-desktop-save)
(desktop-clear)
(setq desktop-dirname nil))
(defun my-desktop-read (&optional name)
"Read desktop by name."
(interactive)
(unless name
(setq name (my-desktop-get-session-name "Load session")))
(when name
(desktop-clear)
(desktop-read (concat my-desktop-session-dir name))))
(defun my-desktop-change (&optional name)
"Change desktops by name."
(interactive)
(let ((name (my-desktop-get-current-name)))
(when name
(my-desktop-save name))
(call-interactively 'my-desktop-read)))
(defun my-desktop-name ()
"Return the current desktop name."
(interactive)
(let ((name (my-desktop-get-current-name)))
(if name
(message (concat "Desktop name: " name))
(message "No named desktop loaded"))))
(defun my-desktop-get-current-name ()
"Get the current desktop name."
(when desktop-dirname
(let ((dirname (substring desktop-dirname 0 -1)))
(when (string= (file-name-directory dirname) my-desktop-session-dir)
(file-name-nondirectory dirname)))))
(defun my-desktop-get-session-name (prompt &optional use-default)
"Get a session name."
(let* ((default (and use-default (my-desktop-get-current-name)))
(full-prompt (concat prompt (if default
(concat " (default " default "): ")
": "))))
(completing-read full-prompt (and (file-exists-p my-desktop-session-dir)
(directory-files my-desktop-session-dir))
nil nil nil my-desktop-session-name-hist default)))
(defun my-desktop-kill-emacs-hook ()
"Save desktop before killing emacs."
(when (file-exists-p (concat my-desktop-session-dir "last-session"))
(setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name (concat my-desktop-session-dir "last-session"))))))
(my-desktop-save "last-session"))
(add-hook 'kill-emacs-hook 'my-desktop-kill-emacs-hook)
(provide 'save-desktop)