Commit | Line | Data |
---|---|---|
6343ed61 RS |
1 | ;;; desktop.el --- save partial status of Emacs when killed |
2 | ||
ab422c4d PE |
3 | ;; Copyright (C) 1993-1995, 1997, 2000-2013 Free Software Foundation, |
4 | ;; Inc. | |
6343ed61 RS |
5 | |
6 | ;; Author: Morten Welinder <terra@diku.dk> | |
3ea96cac | 7 | ;; Keywords: convenience |
c5e87d10 | 8 | ;; Favorite-brand-of-beer: None, I hate beer. |
6343ed61 RS |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
6343ed61 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
6343ed61 RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
6343ed61 RS |
24 | |
25 | ;;; Commentary: | |
26 | ||
0b9bd504 RS |
27 | ;; Save the Desktop, i.e., |
28 | ;; - some global variables | |
29 | ;; - the list of buffers with associated files. For each buffer also | |
30 | ;; - the major mode | |
31 | ;; - the default directory | |
32 | ;; - the point | |
33 | ;; - the mark & mark-active | |
34 | ;; - buffer-read-only | |
ec4c6f22 | 35 | ;; - some local variables |
6343ed61 | 36 | |
6b61353c | 37 | ;; To use this, use customize to turn on desktop-save-mode or add the |
865fe16f | 38 | ;; following line somewhere in your init file: |
0b9bd504 | 39 | ;; |
6b61353c | 40 | ;; (desktop-save-mode 1) |
0b9bd504 | 41 | ;; |
6b61353c | 42 | ;; For further usage information, look at the section |
20535d5b | 43 | ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual. |
6b61353c KH |
44 | |
45 | ;; When the desktop module is loaded, the function `desktop-kill' is | |
341c2f07 | 46 | ;; added to the `kill-emacs-hook'. This function is responsible for |
6b61353c | 47 | ;; saving the desktop when Emacs is killed. Furthermore an anonymous |
341c2f07 | 48 | ;; function is added to the `after-init-hook'. This function is |
6b61353c | 49 | ;; responsible for loading the desktop when Emacs is started. |
ec4c6f22 | 50 | |
0f4804e7 LH |
51 | ;; Special handling. |
52 | ;; ----------------- | |
53 | ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers' | |
54 | ;; are supplied to handle special major and minor modes respectively. | |
55 | ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions | |
341c2f07 | 56 | ;; to restore a desktop buffer. Elements must have the form |
be617bbf | 57 | ;; |
0f4804e7 | 58 | ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION). |
be617bbf | 59 | ;; |
0f4804e7 | 60 | ;; Functions listed are called by `desktop-create-buffer' when `desktop-read' |
341c2f07 | 61 | ;; evaluates the desktop file. Buffers with a major mode not specified here, |
0f4804e7 LH |
62 | ;; are restored by the default handler `desktop-restore-file-buffer'. |
63 | ;; `desktop-minor-mode-handlers' is an alist of functions to restore | |
64 | ;; non-standard minor modes. Elements must have the form | |
be617bbf | 65 | ;; |
0f4804e7 | 66 | ;; (MINOR-MODE . RESTORE-FUNCTION). |
be617bbf | 67 | ;; |
0f4804e7 LH |
68 | ;; Functions are called by `desktop-create-buffer' to restore minor modes. |
69 | ;; Minor modes not specified here, are restored by the standard minor mode | |
70 | ;; function. If you write a module that defines a major or minor mode that | |
71 | ;; needs a special handler, then place code like | |
72 | ||
73 | ;; (defun foo-restore-desktop-buffer | |
74 | ;; ... | |
75 | ;; (add-to-list 'desktop-buffer-mode-handlers | |
76 | ;; '(foo-mode . foo-restore-desktop-buffer)) | |
77 | ||
78 | ;; or | |
79 | ||
80 | ;; (defun bar-desktop-restore | |
81 | ;; ... | |
82 | ;; (add-to-list 'desktop-minor-mode-handlers | |
83 | ;; '(bar-mode . bar-desktop-restore)) | |
84 | ||
fb8a6326 | 85 | ;; in the module itself, and make sure that the mode function is |
341c2f07 | 86 | ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and |
0f4804e7 LH |
87 | ;; `desktop-minor-mode-handlers' for more info. |
88 | ||
89 | ;; Minor modes. | |
90 | ;; ------------ | |
91 | ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp | |
92 | ;; manual) are handled in the following way: | |
93 | ;; When `desktop-save' saves the state of a buffer to the desktop file, it | |
94 | ;; saves as `desktop-minor-modes' the list of names of those variables in | |
95 | ;; `minor-mode-alist' that have a non-nil value. | |
96 | ;; When `desktop-create' restores the buffer, each of the symbols in | |
97 | ;; `desktop-minor-modes' is called as function with parameter 1. | |
98 | ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers' | |
99 | ;; are used to handle non-conventional minor modes. `desktop-save' uses | |
100 | ;; `desktop-minor-mode-table' to map minor mode variables to minor mode | |
341c2f07 | 101 | ;; functions before writing `desktop-minor-modes'. If a minor mode has a |
0f4804e7 LH |
102 | ;; variable name that is different form its function name, an entry |
103 | ||
104 | ;; (NAME RESTORE-FUNCTION) | |
105 | ||
106 | ;; should be added to `desktop-minor-mode-table'. If a minor mode should not | |
107 | ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses | |
108 | ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore | |
109 | ;; function different from the usual minor mode function. | |
110 | ;; --------------------------------------------------------------------------- | |
47640244 | 111 | |
ec4c6f22 RS |
112 | ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs |
113 | ;; in your home directory is used for that. Saving global default values | |
114 | ;; for buffers is an example of misuse. | |
115 | ||
0b9bd504 RS |
116 | ;; PLEASE NOTE: The kill ring can be saved as specified by the variable |
117 | ;; `desktop-globals-to-save' (by default it isn't). This may result in saving | |
118 | ;; things you did not mean to keep. Use M-x desktop-clear RET. | |
ec4c6f22 | 119 | |
fa379636 KH |
120 | ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas. |
121 | ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip. | |
122 | ;; chris@tecc.co.uk (Chris Boucher) for a mark tip. | |
123 | ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip. | |
124 | ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt. | |
f4c73d07 | 125 | ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips. |
253406b9 | 126 | ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips. |
0b9bd504 RS |
127 | ;; --------------------------------------------------------------------------- |
128 | ;; TODO: | |
129 | ;; | |
130 | ;; Save window configuration. | |
131 | ;; Recognize more minor modes. | |
132 | ;; Save mark rings. | |
6343ed61 RS |
133 | |
134 | ;;; Code: | |
135 | ||
4a2fce7a | 136 | (defvar desktop-file-version "206" |
6b61353c | 137 | "Version number of desktop file format. |
4a2fce7a RS |
138 | Written into the desktop file and used at desktop read to provide |
139 | backward compatibility.") | |
140 | ||
ec4c6f22 | 141 | ;; ---------------------------------------------------------------------------- |
0b9bd504 RS |
142 | ;; USER OPTIONS -- settings you might want to play with. |
143 | ;; ---------------------------------------------------------------------------- | |
bbf5eb28 RS |
144 | |
145 | (defgroup desktop nil | |
146 | "Save status of Emacs when you exit." | |
147 | :group 'frames) | |
148 | ||
e5bd0a28 SM |
149 | ;; Maintained for backward compatibility |
150 | (define-obsolete-variable-alias 'desktop-enable 'desktop-save-mode "22.1") | |
6b61353c KH |
151 | ;;;###autoload |
152 | (define-minor-mode desktop-save-mode | |
06e21633 CY |
153 | "Toggle desktop saving (Desktop Save mode). |
154 | With a prefix argument ARG, enable Desktop Save mode if ARG is | |
155 | positive, and disable it otherwise. If called from Lisp, enable | |
156 | the mode if ARG is omitted or nil. | |
157 | ||
158 | If Desktop Save mode is enabled, the state of Emacs is saved from | |
159 | one session to another. See variable `desktop-save' and function | |
160 | `desktop-read' for details." | |
6b61353c KH |
161 | :global t |
162 | :group 'desktop) | |
163 | ||
e88110db JB |
164 | (defun desktop-save-mode-off () |
165 | "Disable `desktop-save-mode'. Provided for use in hooks." | |
166 | (desktop-save-mode 0)) | |
167 | ||
4a2fce7a | 168 | (defcustom desktop-save 'ask-if-new |
9201cc28 | 169 | "Specifies whether the desktop should be saved when it is killed. |
6b61353c KH |
170 | A desktop is killed when the user changes desktop or quits Emacs. |
171 | Possible values are: | |
4a2fce7a RS |
172 | t -- always save. |
173 | ask -- always ask. | |
174 | ask-if-new -- ask if no desktop file exists, otherwise just save. | |
175 | ask-if-exists -- ask if desktop file exists, otherwise don't save. | |
176 | if-exists -- save if desktop file exists, otherwise don't save. | |
177 | nil -- never save. | |
6b61353c | 178 | The desktop is never saved when `desktop-save-mode' is nil. |
cc8b76bf | 179 | The variables `desktop-dirname' and `desktop-base-file-name' |
6b61353c | 180 | determine where the desktop is saved." |
11425834 LH |
181 | :type |
182 | '(choice | |
4a2fce7a RS |
183 | (const :tag "Always save" t) |
184 | (const :tag "Always ask" ask) | |
185 | (const :tag "Ask if desktop file is new, else do save" ask-if-new) | |
186 | (const :tag "Ask if desktop file exists, else don't save" ask-if-exists) | |
187 | (const :tag "Save if desktop file exists, else don't" if-exists) | |
188 | (const :tag "Never save" nil)) | |
af61551b | 189 | :group 'desktop |
bf247b6e | 190 | :version "22.1") |
4a2fce7a | 191 | |
5db9dace JL |
192 | (defcustom desktop-auto-save-timeout nil |
193 | "Number of seconds between auto-saves of the desktop. | |
194 | Zero or nil means disable timer-based auto-saving." | |
195 | :type '(choice (const :tag "Off" nil) | |
196 | (integer :tag "Seconds")) | |
197 | :set (lambda (symbol value) | |
198 | (set-default symbol value) | |
199 | (condition-case nil | |
200 | (desktop-auto-save-set-timer) | |
201 | (error nil))) | |
202 | :group 'desktop | |
203 | :version "24.4") | |
204 | ||
1f7efe1b JB |
205 | (defcustom desktop-load-locked-desktop 'ask |
206 | "Specifies whether the desktop should be loaded if locked. | |
207 | Possible values are: | |
208 | t -- load anyway. | |
209 | nil -- don't load. | |
210 | ask -- ask the user. | |
211 | If the value is nil, or `ask' and the user chooses not to load the desktop, | |
212 | the normal hook `desktop-not-loaded-hook' is run." | |
213 | :type | |
214 | '(choice | |
215 | (const :tag "Load anyway" t) | |
216 | (const :tag "Don't load" nil) | |
217 | (const :tag "Ask the user" ask)) | |
218 | :group 'desktop | |
8f3f313d | 219 | :version "22.2") |
1f7efe1b | 220 | |
cd6ef82d GM |
221 | (define-obsolete-variable-alias 'desktop-basefilename |
222 | 'desktop-base-file-name "22.1") | |
223 | ||
4a2fce7a | 224 | (defcustom desktop-base-file-name |
fc715501 | 225 | (convert-standard-filename ".emacs.desktop") |
6b61353c | 226 | "Name of file for Emacs desktop, excluding the directory part." |
813dbb2d RS |
227 | :type 'file |
228 | :group 'desktop) | |
6343ed61 | 229 | |
e88110db JB |
230 | (defcustom desktop-base-lock-name |
231 | (convert-standard-filename ".emacs.desktop.lock") | |
232 | "Name of lock file for Emacs desktop, excluding the directory part." | |
233 | :type 'file | |
234 | :group 'desktop | |
8f3f313d | 235 | :version "22.2") |
e88110db | 236 | |
e76f0800 | 237 | (defcustom desktop-path (list user-emacs-directory "~") |
4a2fce7a RS |
238 | "List of directories to search for the desktop file. |
239 | The base name of the file is specified in `desktop-base-file-name'." | |
240 | :type '(repeat directory) | |
af61551b | 241 | :group 'desktop |
a3b337cd | 242 | :version "23.2") ; user-emacs-directory added |
4a2fce7a | 243 | |
bbf5eb28 | 244 | (defcustom desktop-missing-file-warning nil |
6c27fdb9 | 245 | "If non-nil, offer to recreate the buffer of a deleted file. |
ebb39555 LH |
246 | Also pause for a moment to display message about errors signaled in |
247 | `desktop-buffer-mode-handlers'. | |
248 | ||
249 | If nil, just print error messages in the message buffer." | |
bbf5eb28 | 250 | :type 'boolean |
af61551b | 251 | :group 'desktop |
bf247b6e | 252 | :version "22.1") |
6343ed61 | 253 | |
4a2fce7a | 254 | (defcustom desktop-no-desktop-file-hook nil |
6b61353c | 255 | "Normal hook run when `desktop-read' can't find a desktop file. |
11425834 | 256 | Run in the directory in which the desktop file was sought. |
8649a87b | 257 | May be used to show a dired buffer." |
4a2fce7a | 258 | :type 'hook |
af61551b | 259 | :group 'desktop |
bf247b6e | 260 | :version "22.1") |
4a2fce7a | 261 | |
e88110db JB |
262 | (defcustom desktop-not-loaded-hook nil |
263 | "Normal hook run when the user declines to re-use a desktop file. | |
264 | Run in the directory in which the desktop file was found. | |
265 | May be used to deal with accidental multiple Emacs jobs." | |
266 | :type 'hook | |
267 | :group 'desktop | |
268 | :options '(desktop-save-mode-off save-buffers-kill-emacs) | |
8f3f313d | 269 | :version "22.2") |
e88110db | 270 | |
4a2fce7a | 271 | (defcustom desktop-after-read-hook nil |
6b61353c | 272 | "Normal hook run after a successful `desktop-read'. |
8649a87b | 273 | May be used to show a buffer list." |
4a2fce7a | 274 | :type 'hook |
af61551b | 275 | :group 'desktop |
11425834 | 276 | :options '(list-buffers) |
bf247b6e | 277 | :version "22.1") |
4a2fce7a RS |
278 | |
279 | (defcustom desktop-save-hook nil | |
6b61353c | 280 | "Normal hook run before the desktop is saved in a desktop file. |
11425834 LH |
281 | Run with the desktop buffer current with only the header present. |
282 | May be used to add to the desktop code or to truncate history lists, | |
283 | for example." | |
4a2fce7a RS |
284 | :type 'hook |
285 | :group 'desktop) | |
286 | ||
340db502 LH |
287 | (defcustom desktop-globals-to-save |
288 | '(desktop-missing-file-warning | |
289 | tags-file-name | |
290 | tags-table-list | |
291 | search-ring | |
292 | regexp-search-ring | |
c760f19e CY |
293 | register-alist |
294 | file-name-history) | |
6b61353c KH |
295 | "List of global variables saved by `desktop-save'. |
296 | An element may be variable name (a symbol) or a cons cell of the form | |
297 | \(VAR . MAX-SIZE), which means to truncate VAR's value to at most | |
298 | MAX-SIZE elements (if the value is a list) before saving the value. | |
4a2fce7a RS |
299 | Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'." |
300 | :type '(repeat (restricted-sexp :match-alternatives (symbolp consp))) | |
301 | :group 'desktop) | |
302 | ||
340db502 LH |
303 | (defcustom desktop-globals-to-clear |
304 | '(kill-ring | |
305 | kill-ring-yank-pointer | |
306 | search-ring | |
307 | search-ring-yank-pointer | |
308 | regexp-search-ring | |
309 | regexp-search-ring-yank-pointer) | |
150f39ee | 310 | "List of global variables that `desktop-clear' will clear. |
4a2fce7a | 311 | An element may be variable name (a symbol) or a cons cell of the form |
cc8b76bf JB |
312 | \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set |
313 | to the value obtained by evaluating FORM." | |
4a2fce7a | 314 | :type '(repeat (restricted-sexp :match-alternatives (symbolp consp))) |
af61551b | 315 | :group 'desktop |
bf247b6e | 316 | :version "22.1") |
4a2fce7a | 317 | |
0f4804e7 | 318 | (defcustom desktop-clear-preserve-buffers |
3845c322 GM |
319 | '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*" |
320 | "\\*Warnings\\*") | |
9201cc28 | 321 | "List of buffers that `desktop-clear' should not delete. |
0f4804e7 LH |
322 | Each element is a regular expression. Buffers with a name matched by any of |
323 | these won't be deleted." | |
3845c322 | 324 | :version "23.3" ; added Warnings - bug#6336 |
4a2fce7a RS |
325 | :type '(repeat string) |
326 | :group 'desktop) | |
340db502 | 327 | |
0f4804e7 | 328 | ;;;###autoload |
340db502 LH |
329 | (defcustom desktop-locals-to-save |
330 | '(desktop-locals-to-save ; Itself! Think it over. | |
331 | truncate-lines | |
332 | case-fold-search | |
333 | case-replace | |
334 | fill-column | |
335 | overwrite-mode | |
336 | change-log-default-name | |
337 | line-number-mode | |
0f4804e7 LH |
338 | column-number-mode |
339 | size-indication-mode | |
340 | buffer-file-coding-system | |
341 | indent-tabs-mode | |
11425834 | 342 | tab-width |
0f4804e7 LH |
343 | indicate-buffer-boundaries |
344 | indicate-empty-lines | |
345 | show-trailing-whitespace) | |
577ed2b2 | 346 | "List of local variables to save for each buffer. |
0f4804e7 LH |
347 | The variables are saved only when they really are local. Conventional minor |
348 | modes are restored automatically; they should not be listed here." | |
6b61353c KH |
349 | :type '(repeat symbol) |
350 | :group 'desktop) | |
ec4c6f22 | 351 | |
a6c2c80c | 352 | (defcustom desktop-buffers-not-to-save nil |
4a2fce7a | 353 | "Regexp identifying buffers that are to be excluded from saving." |
a6c2c80c EZ |
354 | :type '(choice (const :tag "None" nil) |
355 | regexp) | |
1a8d3541 | 356 | :version "23.2" ; set to nil |
4a2fce7a | 357 | :group 'desktop) |
6343ed61 | 358 | |
6b61353c | 359 | ;; Skip tramp and ange-ftp files |
bbf5eb28 | 360 | (defcustom desktop-files-not-to-save |
a6c2c80c | 361 | "\\(^/[^/:]*:\\|(ftp)$\\)" |
bbf5eb28 | 362 | "Regexp identifying files whose buffers are to be excluded from saving." |
a6c2c80c EZ |
363 | :type '(choice (const :tag "None" nil) |
364 | regexp) | |
bbf5eb28 | 365 | :group 'desktop) |
fa379636 | 366 | |
df410295 JL |
367 | ;; We skip TAGS files to save time (tags-file-name is saved instead). |
368 | (defcustom desktop-modes-not-to-save | |
369 | '(tags-table-mode) | |
80f9f3db SM |
370 | "List of major modes whose buffers should not be saved." |
371 | :type '(repeat symbol) | |
372 | :group 'desktop) | |
373 | ||
39c0e36f JB |
374 | (defcustom desktop-save-windows nil |
375 | "When non-nil, save window/frame configuration to desktop file." | |
376 | :type 'boolean | |
377 | :group 'desktop | |
378 | :version "24.4") | |
379 | ||
4a2fce7a | 380 | (defcustom desktop-file-name-format 'absolute |
9201cc28 | 381 | "Format in which desktop file names should be saved. |
4a2fce7a RS |
382 | Possible values are: |
383 | absolute -- Absolute file name. | |
384 | tilde -- Relative to ~. | |
385 | local -- Relative to directory of desktop file." | |
386 | :type '(choice (const absolute) (const tilde) (const local)) | |
af61551b | 387 | :group 'desktop |
bf247b6e | 388 | :version "22.1") |
569c754e | 389 | |
150f39ee LH |
390 | (defcustom desktop-restore-eager t |
391 | "Number of buffers to restore immediately. | |
392 | Remaining buffers are restored lazily (when Emacs is idle). | |
393 | If value is t, all buffers are restored immediately." | |
0ba9bc53 | 394 | :type '(choice (const t) integer) |
150f39ee | 395 | :group 'desktop |
bf247b6e | 396 | :version "22.1") |
150f39ee LH |
397 | |
398 | (defcustom desktop-lazy-verbose t | |
399 | "Verbose reporting of lazily created buffers." | |
400 | :type 'boolean | |
401 | :group 'desktop | |
bf247b6e | 402 | :version "22.1") |
150f39ee LH |
403 | |
404 | (defcustom desktop-lazy-idle-delay 5 | |
405 | "Idle delay before starting to create buffers. | |
406 | See `desktop-restore-eager'." | |
407 | :type 'integer | |
408 | :group 'desktop | |
bf247b6e | 409 | :version "22.1") |
150f39ee | 410 | |
e5780ae1 | 411 | ;;;###autoload |
ebb39555 LH |
412 | (defvar desktop-save-buffer nil |
413 | "When non-nil, save buffer status in desktop file. | |
e5780ae1 | 414 | This variable becomes buffer local when set. |
ebb39555 | 415 | |
b89c5a72 JB |
416 | If the value is a function, it is called by `desktop-save' with argument |
417 | DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop | |
ebb39555 | 418 | file along with the state of the buffer for which it was called. |
b6b70cda | 419 | |
6b61353c | 420 | When file names are returned, they should be formatted using the call |
e5780ae1 | 421 | \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\". |
4a2fce7a | 422 | |
0f4804e7 LH |
423 | Later, when `desktop-read' evaluates the desktop file, auxiliary information |
424 | is passed as the argument DESKTOP-BUFFER-MISC to functions in | |
425 | `desktop-buffer-mode-handlers'.") | |
ebb39555 LH |
426 | (make-variable-buffer-local 'desktop-save-buffer) |
427 | (make-obsolete-variable 'desktop-buffer-modes-to-save | |
cc8b76bf | 428 | 'desktop-save-buffer "22.1") |
e5780ae1 | 429 | (make-obsolete-variable 'desktop-buffer-misc-functions |
cc8b76bf | 430 | 'desktop-save-buffer "22.1") |
b6b70cda | 431 | |
0f4804e7 | 432 | ;;;###autoload |
e76f0800 | 433 | (defvar desktop-buffer-mode-handlers nil |
e5780ae1 | 434 | "Alist of major mode specific functions to restore a desktop buffer. |
0f4804e7 LH |
435 | Functions listed are called by `desktop-create-buffer' when `desktop-read' |
436 | evaluates the desktop file. List elements must have the form | |
437 | ||
438 | (MAJOR-MODE . RESTORE-BUFFER-FUNCTION). | |
e5780ae1 LH |
439 | |
440 | Buffers with a major mode not specified here, are restored by the default | |
441 | handler `desktop-restore-file-buffer'. | |
442 | ||
55775448 | 443 | Handlers are called with argument list |
4a2fce7a | 444 | |
9bcabb45 | 445 | (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC) |
e5780ae1 LH |
446 | |
447 | Furthermore, they may use the following variables: | |
4a2fce7a RS |
448 | |
449 | desktop-file-version | |
4a2fce7a RS |
450 | desktop-buffer-major-mode |
451 | desktop-buffer-minor-modes | |
452 | desktop-buffer-point | |
453 | desktop-buffer-mark | |
454 | desktop-buffer-read-only | |
4a2fce7a RS |
455 | desktop-buffer-locals |
456 | ||
e5780ae1 | 457 | If a handler returns a buffer, then the saved mode settings |
0f4804e7 LH |
458 | and variable values for that buffer are copied into it. |
459 | ||
460 | Modules that define a major mode that needs a special handler should contain | |
461 | code like | |
ec4c6f22 | 462 | |
0f4804e7 LH |
463 | (defun foo-restore-desktop-buffer |
464 | ... | |
465 | (add-to-list 'desktop-buffer-mode-handlers | |
466 | '(foo-mode . foo-restore-desktop-buffer)) | |
467 | ||
468 | Furthermore the major mode function must be autoloaded.") | |
ec4c6f22 | 469 | |
498eb267 | 470 | ;;;###autoload |
e5780ae1 LH |
471 | (put 'desktop-buffer-mode-handlers 'risky-local-variable t) |
472 | (make-obsolete-variable 'desktop-buffer-handlers | |
cc8b76bf | 473 | 'desktop-buffer-mode-handlers "22.1") |
b6b70cda | 474 | |
47640244 GM |
475 | (defcustom desktop-minor-mode-table |
476 | '((auto-fill-function auto-fill-mode) | |
5fff0265 | 477 | (vc-mode nil) |
f2168a4c | 478 | (vc-dired-mode nil) |
fb8a6326 JB |
479 | (erc-track-minor-mode nil) |
480 | (savehist-mode nil)) | |
47640244 GM |
481 | "Table mapping minor mode variables to minor mode functions. |
482 | Each entry has the form (NAME RESTORE-FUNCTION). | |
483 | NAME is the name of the buffer-local variable indicating that the minor | |
484 | mode is active. RESTORE-FUNCTION is the function to activate the minor mode. | |
7d6ee4df | 485 | RESTORE-FUNCTION nil means don't try to restore the minor mode. |
47640244 | 486 | Only minor modes for which the name of the buffer-local variable |
7bfa55b3 | 487 | and the name of the minor mode function are different have to be added to |
0f4804e7 | 488 | this table. See also `desktop-minor-mode-handlers'." |
47640244 GM |
489 | :type 'sexp |
490 | :group 'desktop) | |
491 | ||
0f4804e7 | 492 | ;;;###autoload |
e76f0800 | 493 | (defvar desktop-minor-mode-handlers nil |
0f4804e7 LH |
494 | "Alist of functions to restore non-standard minor modes. |
495 | Functions are called by `desktop-create-buffer' to restore minor modes. | |
496 | List elements must have the form | |
497 | ||
498 | (MINOR-MODE . RESTORE-FUNCTION). | |
499 | ||
500 | Minor modes not specified here, are restored by the standard minor mode | |
501 | function. | |
502 | ||
503 | Handlers are called with argument list | |
504 | ||
505 | (DESKTOP-BUFFER-LOCALS) | |
506 | ||
507 | Furthermore, they may use the following variables: | |
508 | ||
509 | desktop-file-version | |
510 | desktop-buffer-file-name | |
511 | desktop-buffer-name | |
512 | desktop-buffer-major-mode | |
513 | desktop-buffer-minor-modes | |
514 | desktop-buffer-point | |
515 | desktop-buffer-mark | |
516 | desktop-buffer-read-only | |
517 | desktop-buffer-misc | |
518 | ||
519 | When a handler is called, the buffer has been created and the major mode has | |
520 | been set, but local variables listed in desktop-buffer-locals has not yet been | |
521 | created and set. | |
522 | ||
523 | Modules that define a minor mode that needs a special handler should contain | |
524 | code like | |
525 | ||
526 | (defun foo-desktop-restore | |
527 | ... | |
528 | (add-to-list 'desktop-minor-mode-handlers | |
529 | '(foo-mode . foo-desktop-restore)) | |
530 | ||
531 | Furthermore the minor mode function must be autoloaded. | |
532 | ||
533 | See also `desktop-minor-mode-table'.") | |
534 | ||
498eb267 | 535 | ;;;###autoload |
0f4804e7 LH |
536 | (put 'desktop-minor-mode-handlers 'risky-local-variable t) |
537 | ||
0b9bd504 RS |
538 | ;; ---------------------------------------------------------------------------- |
539 | (defvar desktop-dirname nil | |
6b61353c | 540 | "The directory in which the desktop file should be saved.") |
6343ed61 | 541 | |
11425834 LH |
542 | (defun desktop-full-file-name (&optional dirname) |
543 | "Return the full name of the desktop file in DIRNAME. | |
544 | DIRNAME omitted or nil means use `desktop-dirname'." | |
545 | (expand-file-name desktop-base-file-name (or dirname desktop-dirname))) | |
546 | ||
e88110db JB |
547 | (defun desktop-full-lock-name (&optional dirname) |
548 | "Return the full name of the desktop lock file in DIRNAME. | |
549 | DIRNAME omitted or nil means use `desktop-dirname'." | |
550 | (expand-file-name desktop-base-lock-name (or dirname desktop-dirname))) | |
551 | ||
6343ed61 | 552 | (defconst desktop-header |
0b9bd504 RS |
553 | ";; -------------------------------------------------------------------------- |
554 | ;; Desktop File for Emacs | |
555 | ;; -------------------------------------------------------------------------- | |
6343ed61 | 556 | " "*Header to place in Desktop file.") |
de9e2828 RS |
557 | |
558 | (defvar desktop-delay-hook nil | |
559 | "Hooks run after all buffers are loaded; intended for internal use.") | |
813dbb2d | 560 | |
5db9dace JL |
561 | (defvar desktop-file-checksum nil |
562 | "Checksum of the last auto-saved contents of the desktop file. | |
563 | Used to avoid writing contents unchanged between auto-saves.") | |
564 | ||
39c0e36f JB |
565 | (defvar desktop--saved-states nil |
566 | "Internal use only.") | |
567 | ||
e88110db JB |
568 | ;; ---------------------------------------------------------------------------- |
569 | ;; Desktop file conflict detection | |
570 | (defvar desktop-file-modtime nil | |
571 | "When the desktop file was last modified to the knowledge of this Emacs. | |
572 | Used to detect desktop file conflicts.") | |
573 | ||
574 | (defun desktop-owner (&optional dirname) | |
575 | "Return the PID of the Emacs process that owns the desktop file in DIRNAME. | |
576 | Return nil if no desktop file found or no Emacs process is using it. | |
577 | DIRNAME omitted or nil means use `desktop-dirname'." | |
578 | (let (owner) | |
579 | (and (file-exists-p (desktop-full-lock-name dirname)) | |
580 | (condition-case nil | |
581 | (with-temp-buffer | |
582 | (insert-file-contents-literally (desktop-full-lock-name dirname)) | |
583 | (goto-char (point-min)) | |
584 | (setq owner (read (current-buffer))) | |
585 | (integerp owner)) | |
586 | (error nil)) | |
587 | owner))) | |
588 | ||
589 | (defun desktop-claim-lock (&optional dirname) | |
590 | "Record this Emacs process as the owner of the desktop file in DIRNAME. | |
591 | DIRNAME omitted or nil means use `desktop-dirname'." | |
592 | (write-region (number-to-string (emacs-pid)) nil | |
593 | (desktop-full-lock-name dirname))) | |
594 | ||
595 | (defun desktop-release-lock (&optional dirname) | |
596 | "Remove the lock file for the desktop in DIRNAME. | |
597 | DIRNAME omitted or nil means use `desktop-dirname'." | |
598 | (let ((file (desktop-full-lock-name dirname))) | |
599 | (when (file-exists-p file) (delete-file file)))) | |
600 | ||
0b9bd504 | 601 | ;; ---------------------------------------------------------------------------- |
6b61353c | 602 | (defun desktop-truncate (list n) |
ec4c6f22 | 603 | "Truncate LIST to at most N elements destructively." |
6b61353c | 604 | (let ((here (nthcdr (1- n) list))) |
1f7efe1b JB |
605 | (when (consp here) |
606 | (setcdr here nil)))) | |
f9be4574 | 607 | |
4a2fce7a | 608 | ;; ---------------------------------------------------------------------------- |
11425834 | 609 | ;;;###autoload |
f9be4574 RS |
610 | (defun desktop-clear () |
611 | "Empty the Desktop. | |
0f4804e7 LH |
612 | This kills all buffers except for internal ones and those with names matched by |
613 | a regular expression in the list `desktop-clear-preserve-buffers'. | |
614 | Furthermore, it clears the variables listed in `desktop-globals-to-clear'." | |
6343ed61 | 615 | (interactive) |
150f39ee | 616 | (desktop-lazy-abort) |
4a2fce7a RS |
617 | (dolist (var desktop-globals-to-clear) |
618 | (if (symbolp var) | |
1f7efe1b | 619 | (eval `(setq-default ,var nil)) |
4a2fce7a | 620 | (eval `(setq-default ,(car var) ,(cdr var))))) |
0f4804e7 LH |
621 | (let ((buffers (buffer-list)) |
622 | (preserve-regexp (concat "^\\(" | |
623 | (mapconcat (lambda (regexp) | |
624 | (concat "\\(" regexp "\\)")) | |
625 | desktop-clear-preserve-buffers | |
626 | "\\|") | |
627 | "\\)$"))) | |
f9be4574 | 628 | (while buffers |
4a2fce7a RS |
629 | (let ((bufname (buffer-name (car buffers)))) |
630 | (or | |
631 | (null bufname) | |
0f4804e7 | 632 | (string-match preserve-regexp bufname) |
4a2fce7a | 633 | ;; Don't kill buffers made for internal purposes. |
3f32d6a3 | 634 | (and (not (equal bufname "")) (eq (aref bufname 0) ?\s)) |
4a2fce7a | 635 | (kill-buffer (car buffers)))) |
f9be4574 | 636 | (setq buffers (cdr buffers)))) |
b6105c76 | 637 | (delete-other-windows)) |
4a2fce7a | 638 | |
0b9bd504 | 639 | ;; ---------------------------------------------------------------------------- |
845fc5e5 JB |
640 | (unless noninteractive |
641 | (add-hook 'kill-emacs-hook 'desktop-kill)) | |
ec4c6f22 | 642 | |
6343ed61 | 643 | (defun desktop-kill () |
6b61353c | 644 | "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do. |
4a2fce7a RS |
645 | If the desktop should be saved and `desktop-dirname' |
646 | is nil, ask the user where to save the desktop." | |
11425834 LH |
647 | (when (and desktop-save-mode |
648 | (let ((exists (file-exists-p (desktop-full-file-name)))) | |
649 | (or (eq desktop-save t) | |
7dd7fbb9 JD |
650 | (and exists (eq desktop-save 'if-exists)) |
651 | ;; If it exists, but we aren't using it, we are going | |
652 | ;; to ask for a new directory below. | |
653 | (and exists desktop-dirname (eq desktop-save 'ask-if-new)) | |
11425834 LH |
654 | (and |
655 | (or (memq desktop-save '(ask ask-if-new)) | |
656 | (and exists (eq desktop-save 'ask-if-exists))) | |
657 | (y-or-n-p "Save desktop? "))))) | |
4a2fce7a RS |
658 | (unless desktop-dirname |
659 | (setq desktop-dirname | |
11425834 LH |
660 | (file-name-as-directory |
661 | (expand-file-name | |
794855ca | 662 | (read-directory-name "Directory for desktop file: " nil nil t))))) |
4a2fce7a | 663 | (condition-case err |
e88110db | 664 | (desktop-save desktop-dirname t) |
4a2fce7a | 665 | (file-error |
11425834 | 666 | (unless (yes-or-no-p "Error while saving the desktop. Ignore? ") |
e88110db JB |
667 | (signal (car err) (cdr err)))))) |
668 | ;; If we own it, we don't anymore. | |
669 | (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock))) | |
4a2fce7a | 670 | |
0b9bd504 | 671 | ;; ---------------------------------------------------------------------------- |
ed2f7fc8 RS |
672 | (defun desktop-list* (&rest args) |
673 | (if (null (cdr args)) | |
674 | (car args) | |
675 | (setq args (nreverse args)) | |
676 | (let ((value (cons (nth 1 args) (car args)))) | |
677 | (setq args (cdr (cdr args))) | |
678 | (while args | |
679 | (setq value (cons (car args) value)) | |
680 | (setq args (cdr args))) | |
681 | value))) | |
682 | ||
e88110db JB |
683 | ;; ---------------------------------------------------------------------------- |
684 | (defun desktop-buffer-info (buffer) | |
685 | (set-buffer buffer) | |
686 | (list | |
a8049a30 JB |
687 | ;; base name of the buffer; replaces the buffer name if managed by uniquify |
688 | (and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name)) | |
e88110db | 689 | ;; basic information |
9e29c91c | 690 | (desktop-file-name (buffer-file-name) desktop-dirname) |
a8049a30 | 691 | (buffer-name) |
e88110db JB |
692 | major-mode |
693 | ;; minor modes | |
694 | (let (ret) | |
695 | (mapc | |
696 | #'(lambda (minor-mode) | |
697 | (and (boundp minor-mode) | |
698 | (symbol-value minor-mode) | |
699 | (let* ((special (assq minor-mode desktop-minor-mode-table)) | |
700 | (value (cond (special (cadr special)) | |
701 | ((functionp minor-mode) minor-mode)))) | |
702 | (when value (add-to-list 'ret value))))) | |
703 | (mapcar #'car minor-mode-alist)) | |
704 | ret) | |
705 | ;; point and mark, and read-only status | |
706 | (point) | |
707 | (list (mark t) mark-active) | |
708 | buffer-read-only | |
709 | ;; auxiliary information | |
710 | (when (functionp desktop-save-buffer) | |
9e29c91c | 711 | (funcall desktop-save-buffer desktop-dirname)) |
e88110db JB |
712 | ;; local variables |
713 | (let ((locals desktop-locals-to-save) | |
714 | (loclist (buffer-local-variables)) | |
715 | (ll)) | |
716 | (while locals | |
717 | (let ((here (assq (car locals) loclist))) | |
718 | (if here | |
719 | (setq ll (cons here ll)) | |
720 | (when (member (car locals) loclist) | |
721 | (setq ll (cons (car locals) ll))))) | |
722 | (setq locals (cdr locals))) | |
723 | ll))) | |
724 | ||
4a2fce7a | 725 | ;; ---------------------------------------------------------------------------- |
69b2c07e SM |
726 | (defun desktop--v2s (value) |
727 | "Convert VALUE to a pair (QUOTE . SEXP); (eval SEXP) gives VALUE. | |
728 | SEXP is an sexp that when evaluated yields VALUE. | |
577ed2b2 | 729 | QUOTE may be `may' (value may be quoted), |
60ff536c | 730 | `must' (value must be quoted), or nil (value must not be quoted)." |
de9e2828 | 731 | (cond |
1f7efe1b | 732 | ((or (numberp value) (null value) (eq t value) (keywordp value)) |
69b2c07e | 733 | (cons 'may value)) |
1f7efe1b JB |
734 | ((stringp value) |
735 | (let ((copy (copy-sequence value))) | |
736 | (set-text-properties 0 (length copy) nil copy) | |
69b2c07e SM |
737 | ;; Get rid of text properties because we cannot read them. |
738 | (cons 'may copy))) | |
1f7efe1b | 739 | ((symbolp value) |
69b2c07e | 740 | (cons 'must value)) |
1f7efe1b | 741 | ((vectorp value) |
69b2c07e SM |
742 | (let* ((pass1 (mapcar #'desktop--v2s value)) |
743 | (special (assq nil pass1))) | |
1f7efe1b | 744 | (if special |
69b2c07e SM |
745 | (cons nil `(vector |
746 | ,@(mapcar (lambda (el) | |
747 | (if (eq (car el) 'must) | |
748 | `',(cdr el) (cdr el))) | |
749 | pass1))) | |
750 | (cons 'may `[,@(mapcar #'cdr pass1)])))) | |
1f7efe1b JB |
751 | ((consp value) |
752 | (let ((p value) | |
753 | newlist | |
754 | use-list* | |
755 | anynil) | |
756 | (while (consp p) | |
69b2c07e SM |
757 | (let ((q.sexp (desktop--v2s (car p)))) |
758 | (push q.sexp newlist)) | |
1f7efe1b | 759 | (setq p (cdr p))) |
69b2c07e SM |
760 | (when p |
761 | (let ((last (desktop--v2s p))) | |
762 | (setq use-list* t) | |
763 | (push last newlist))) | |
764 | (if (assq nil newlist) | |
1f7efe1b | 765 | (cons nil |
69b2c07e SM |
766 | `(,(if use-list* 'desktop-list* 'list) |
767 | ,@(mapcar (lambda (el) | |
768 | (if (eq (car el) 'must) | |
769 | `',(cdr el) (cdr el))) | |
770 | (nreverse newlist)))) | |
1f7efe1b | 771 | (cons 'must |
69b2c07e SM |
772 | `(,@(mapcar #'cdr |
773 | (nreverse (if use-list* (cdr newlist) newlist))) | |
774 | ,@(if use-list* (cdar newlist))))))) | |
1f7efe1b | 775 | ((subrp value) |
69b2c07e SM |
776 | (cons nil `(symbol-function |
777 | ',(intern-soft (substring (prin1-to-string value) 7 -1))))) | |
1f7efe1b | 778 | ((markerp value) |
69b2c07e SM |
779 | (let ((pos (marker-position value)) |
780 | (buf (buffer-name (marker-buffer value)))) | |
781 | (cons nil | |
782 | `(let ((mk (make-marker))) | |
783 | (add-hook 'desktop-delay-hook | |
784 | `(lambda () | |
785 | (set-marker ,mk ,,pos (get-buffer ,,buf)))) | |
786 | mk)))) | |
787 | (t ; Save as text. | |
788 | (cons 'may "Unprintable entity")))) | |
de9e2828 | 789 | |
4a2fce7a | 790 | ;; ---------------------------------------------------------------------------- |
6b61353c | 791 | (defun desktop-value-to-string (value) |
577ed2b2 RS |
792 | "Convert VALUE to a string that when read evaluates to the same value. |
793 | Not all types of values are supported." | |
de9e2828 RS |
794 | (let* ((print-escape-newlines t) |
795 | (float-output-format nil) | |
69b2c07e SM |
796 | (quote.sexp (desktop--v2s value)) |
797 | (quote (car quote.sexp)) | |
798 | (txt | |
799 | (let ((print-quoted t)) | |
800 | (prin1-to-string (cdr quote.sexp))))) | |
de9e2828 RS |
801 | (if (eq quote 'must) |
802 | (concat "'" txt) | |
803 | txt))) | |
4a2fce7a | 804 | |
ec4c6f22 | 805 | ;; ---------------------------------------------------------------------------- |
0e7c8611 RS |
806 | (defun desktop-outvar (varspec) |
807 | "Output a setq statement for variable VAR to the desktop file. | |
808 | The argument VARSPEC may be the variable name VAR (a symbol), | |
be617bbf | 809 | or a cons cell of the form (VAR . MAX-SIZE), |
0e7c8611 RS |
810 | which means to truncate VAR's value to at most MAX-SIZE elements |
811 | \(if the value is a list) before saving the value." | |
812 | (let (var size) | |
813 | (if (consp varspec) | |
814 | (setq var (car varspec) size (cdr varspec)) | |
815 | (setq var varspec)) | |
1f7efe1b JB |
816 | (when (boundp var) |
817 | (when (and (integerp size) | |
818 | (> size 0) | |
819 | (listp (eval var))) | |
820 | (desktop-truncate (eval var) size)) | |
821 | (insert "(setq " | |
822 | (symbol-name var) | |
823 | " " | |
824 | (desktop-value-to-string (symbol-value var)) | |
825 | ")\n")))) | |
4a2fce7a | 826 | |
0b9bd504 | 827 | ;; ---------------------------------------------------------------------------- |
06b60517 | 828 | (defun desktop-save-buffer-p (filename bufname mode &rest _dummy) |
ebb39555 | 829 | "Return t if buffer should have its state saved in the desktop file. |
0b9bd504 | 830 | FILENAME is the visited file name, BUFNAME is the buffer name, and |
be617bbf JB |
831 | MODE is the major mode. |
832 | \n\(fn FILENAME BUFNAME MODE)" | |
b23caf75 GM |
833 | (let ((case-fold-search nil) |
834 | dired-skip) | |
a6c2c80c EZ |
835 | (and (not (and (stringp desktop-buffers-not-to-save) |
836 | (not filename) | |
837 | (string-match desktop-buffers-not-to-save bufname))) | |
ebb39555 | 838 | (not (memq mode desktop-modes-not-to-save)) |
b23caf75 | 839 | ;; FIXME this is broken if desktop-files-not-to-save is nil. |
ebb39555 | 840 | (or (and filename |
a6c2c80c | 841 | (stringp desktop-files-not-to-save) |
ebb39555 | 842 | (not (string-match desktop-files-not-to-save filename))) |
4ad900d9 | 843 | (and (memq mode '(dired-mode vc-dir-mode)) |
ebb39555 | 844 | (with-current-buffer bufname |
b23caf75 GM |
845 | (not (setq dired-skip |
846 | (string-match desktop-files-not-to-save | |
847 | default-directory))))) | |
ebb39555 | 848 | (and (null filename) |
b23caf75 | 849 | (null dired-skip) ; bug#5755 |
ebb39555 | 850 | (with-current-buffer bufname desktop-save-buffer)))))) |
4a2fce7a | 851 | |
0b9bd504 | 852 | ;; ---------------------------------------------------------------------------- |
4a2fce7a RS |
853 | (defun desktop-file-name (filename dirname) |
854 | "Convert FILENAME to format specified in `desktop-file-name-format'. | |
855 | DIRNAME must be the directory in which the desktop file will be saved." | |
856 | (cond | |
857 | ((not filename) nil) | |
858 | ((eq desktop-file-name-format 'tilde) | |
859 | (let ((relative-name (file-relative-name (expand-file-name filename) "~"))) | |
860 | (cond | |
861 | ((file-name-absolute-p relative-name) relative-name) | |
862 | ((string= "./" relative-name) "~/") | |
863 | ((string= "." relative-name) "~") | |
864 | (t (concat "~/" relative-name))))) | |
865 | ((eq desktop-file-name-format 'local) (file-relative-name filename dirname)) | |
866 | (t (expand-file-name filename)))) | |
e5714620 | 867 | |
b3615392 | 868 | |
4a2fce7a | 869 | ;; ---------------------------------------------------------------------------- |
39c0e36f JB |
870 | (defconst desktop--excluded-frame-parameters |
871 | '(buffer-list | |
872 | buffer-predicate | |
873 | buried-buffer-list | |
874 | explicit-name | |
875 | font-backend | |
876 | minibuffer | |
877 | name | |
878 | outer-window-id | |
879 | parent-id | |
880 | window-id | |
881 | window-system) | |
882 | "Frame parameters not saved or restored.") | |
883 | ||
884 | (defun desktop--filter-frame-parms (frame) | |
885 | "Return frame parameters of FRAME. | |
886 | Parameters in `desktop--excluded-frame-parameters' are excluded. | |
887 | Internal use only." | |
888 | (let (params) | |
889 | (dolist (param (frame-parameters frame)) | |
890 | (unless (memq (car param) desktop--excluded-frame-parameters) | |
891 | (push param params))) | |
892 | params)) | |
893 | ||
894 | (defun desktop--save-windows () | |
895 | "Save window/frame state, as a global variable. | |
896 | Intended to be called from `desktop-save'. | |
897 | Internal use only." | |
898 | (setq desktop--saved-states | |
899 | (and desktop-save-windows | |
900 | (mapcar (lambda (frame) | |
901 | (cons (desktop--filter-frame-parms frame) | |
902 | (window-state-get (frame-root-window frame) t))) | |
903 | (frame-list)))) | |
904 | (desktop-outvar 'desktop--saved-states)) | |
905 | ||
11425834 | 906 | ;;;###autoload |
5db9dace | 907 | (defun desktop-save (dirname &optional release auto-save) |
6b61353c KH |
908 | "Save the desktop in a desktop file. |
909 | Parameter DIRNAME specifies where to save the desktop file. | |
e88110db | 910 | Optional parameter RELEASE says whether we're done with this desktop. |
5db9dace JL |
911 | If AUTO-SAVE is non-nil, compare the saved contents to the one last saved, |
912 | and don't save the buffer if they are the same." | |
6343ed61 | 913 | (interactive "DDirectory to save desktop file in: ") |
e88110db | 914 | (setq desktop-dirname (file-name-as-directory (expand-file-name dirname))) |
6343ed61 | 915 | (save-excursion |
e88110db JB |
916 | (let ((eager desktop-restore-eager) |
917 | (new-modtime (nth 5 (file-attributes (desktop-full-file-name))))) | |
918 | (when | |
919 | (or (not new-modtime) ; nothing to overwrite | |
920 | (equal desktop-file-modtime new-modtime) | |
921 | (yes-or-no-p (if desktop-file-modtime | |
922 | (if (> (float-time new-modtime) (float-time desktop-file-modtime)) | |
923 | "Desktop file is more recent than the one loaded. Save anyway? " | |
924 | "Desktop file isn't the one loaded. Overwrite it? ") | |
925 | "Current desktop was not loaded from a file. Overwrite this desktop file? ")) | |
926 | (unless release (error "Desktop file conflict"))) | |
927 | ||
928 | ;; If we're done with it, release the lock. | |
929 | ;; Otherwise, claim it if it's unclaimed or if we created it. | |
930 | (if release | |
931 | (desktop-release-lock) | |
932 | (unless (and new-modtime (desktop-owner)) (desktop-claim-lock))) | |
933 | ||
934 | (with-temp-buffer | |
935 | (insert | |
936 | ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n" | |
937 | desktop-header | |
938 | ";; Created " (current-time-string) "\n" | |
939 | ";; Desktop file format version " desktop-file-version "\n" | |
940 | ";; Emacs version " emacs-version "\n") | |
941 | (save-excursion (run-hooks 'desktop-save-hook)) | |
942 | (goto-char (point-max)) | |
943 | (insert "\n;; Global section:\n") | |
39c0e36f JB |
944 | ;; Called here because we save the window/frame state as a global |
945 | ;; variable for compatibility with previous Emacsen. | |
946 | (desktop--save-windows) | |
e88110db JB |
947 | (mapc (function desktop-outvar) desktop-globals-to-save) |
948 | (when (memq 'kill-ring desktop-globals-to-save) | |
949 | (insert | |
950 | "(setq kill-ring-yank-pointer (nthcdr " | |
951 | (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer))) | |
952 | " kill-ring))\n")) | |
953 | ||
954 | (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n") | |
955 | (dolist (l (mapcar 'desktop-buffer-info (buffer-list))) | |
a8049a30 JB |
956 | (let ((base (pop l))) |
957 | (when (apply 'desktop-save-buffer-p l) | |
958 | (insert "(" | |
959 | (if (or (not (integerp eager)) | |
960 | (if (zerop eager) | |
961 | nil | |
962 | (setq eager (1- eager)))) | |
963 | "desktop-create-buffer" | |
964 | "desktop-append-buffer-args") | |
965 | " " | |
966 | desktop-file-version) | |
ae4370a8 JB |
967 | ;; If there's a non-empty base name, we save it instead of the buffer name |
968 | (when (and base (not (string= base ""))) | |
969 | (setcar (nthcdr 1 l) base)) | |
a8049a30 JB |
970 | (dolist (e l) |
971 | (insert "\n " (desktop-value-to-string e))) | |
972 | (insert ")\n\n")))) | |
e88110db | 973 | |
9e29c91c | 974 | (setq default-directory desktop-dirname) |
5db9dace JL |
975 | ;; If auto-saving, avoid writing if nothing has changed since the last write. |
976 | ;; Don't check 300 characters of the header that contains the timestamp. | |
977 | (let ((checksum (and auto-save (md5 (current-buffer) | |
978 | (+ (point-min) 300) (point-max) | |
979 | 'emacs-mule)))) | |
980 | (unless (and auto-save (equal checksum desktop-file-checksum)) | |
981 | (let ((coding-system-for-write 'emacs-mule)) | |
982 | (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage)) | |
983 | (setq desktop-file-checksum checksum) | |
984 | ;; We remember when it was modified (which is presumably just now). | |
985 | (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))))))))) | |
4a2fce7a | 986 | |
0b9bd504 | 987 | ;; ---------------------------------------------------------------------------- |
11425834 | 988 | ;;;###autoload |
6343ed61 | 989 | (defun desktop-remove () |
6b61353c KH |
990 | "Delete desktop file in `desktop-dirname'. |
991 | This function also sets `desktop-dirname' to nil." | |
6343ed61 | 992 | (interactive) |
6b61353c | 993 | (when desktop-dirname |
11425834 | 994 | (let ((filename (desktop-full-file-name))) |
6b61353c KH |
995 | (setq desktop-dirname nil) |
996 | (when (file-exists-p filename) | |
73b0b745 | 997 | (delete-file filename))))) |
6b61353c | 998 | |
150f39ee LH |
999 | (defvar desktop-buffer-args-list nil |
1000 | "List of args for `desktop-create-buffer'.") | |
1001 | ||
1002 | (defvar desktop-lazy-timer nil) | |
1003 | ||
0b9bd504 | 1004 | ;; ---------------------------------------------------------------------------- |
39c0e36f JB |
1005 | (defun desktop--find-frame-in-display (frames display) |
1006 | (let (result) | |
1007 | (while (and frames (not result)) | |
1008 | (if (equal display (frame-parameter (car frames) 'display)) | |
1009 | (setq result (car frames)) | |
1010 | (setq frames (cdr frames)))) | |
1011 | result)) | |
1012 | ||
1013 | (defun desktop--restore-windows () | |
1014 | "Restore window/frame configuration. | |
1015 | Internal use only." | |
1016 | (when (and desktop-save-windows desktop--saved-states) | |
1017 | (condition-case nil | |
1018 | (let ((frames (frame-list))) | |
1019 | (dolist (state desktop--saved-states) | |
1020 | (let* ((fconfig (car state)) | |
1021 | (display (cdr (assq 'display fconfig))) | |
1022 | (frame (desktop--find-frame-in-display frames display))) | |
1023 | (if (not frame) | |
1024 | ;; no frames in the display -- make a new one | |
1025 | (setq frame (make-frame-on-display display fconfig)) | |
1026 | ;; found one -- reuse and remove from list | |
1027 | (setq frames (delq frame frames)) | |
1028 | (modify-frame-parameters frame fconfig)) | |
1029 | ;; restore windows | |
1030 | (window-state-put (cdr state) (frame-root-window frame) 'safe))) | |
1031 | ;; delete any remaining frames | |
1032 | (mapc #'delete-frame frames)) | |
1033 | (error | |
1034 | (message "Error loading window configuration from desktop file"))))) | |
1035 | ||
478653c9 | 1036 | ;;;###autoload |
6b61353c KH |
1037 | (defun desktop-read (&optional dirname) |
1038 | "Read and process the desktop file in directory DIRNAME. | |
1039 | Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in | |
1040 | directories listed in `desktop-path'. If a desktop file is found, it | |
cc8b76bf | 1041 | is processed and `desktop-after-read-hook' is run. If no desktop file |
6b61353c KH |
1042 | is found, clear the desktop and run `desktop-no-desktop-file-hook'. |
1043 | This function is a no-op when Emacs is running in batch mode. | |
1044 | It returns t if a desktop file was loaded, nil otherwise." | |
6343ed61 | 1045 | (interactive) |
4a2fce7a | 1046 | (unless noninteractive |
6b61353c | 1047 | (setq desktop-dirname |
11425834 LH |
1048 | (file-name-as-directory |
1049 | (expand-file-name | |
1050 | (or | |
1051 | ;; If DIRNAME is specified, use it. | |
1052 | (and (< 0 (length dirname)) dirname) | |
1053 | ;; Otherwise search desktop file in desktop-path. | |
1054 | (let ((dirs desktop-path)) | |
1055 | (while (and dirs | |
1056 | (not (file-exists-p | |
1057 | (desktop-full-file-name (car dirs))))) | |
1058 | (setq dirs (cdr dirs))) | |
1059 | (and dirs (car dirs))) | |
1060 | ;; If not found and `desktop-path' is non-nil, use its first element. | |
1061 | (and desktop-path (car desktop-path)) | |
6b67c0d4 CY |
1062 | ;; Default: .emacs.d. |
1063 | user-emacs-directory)))) | |
11425834 | 1064 | (if (file-exists-p (desktop-full-file-name)) |
e88110db JB |
1065 | ;; Desktop file found, but is it already in use? |
1066 | (let ((desktop-first-buffer nil) | |
1067 | (desktop-buffer-ok-count 0) | |
1068 | (desktop-buffer-fail-count 0) | |
1069 | (owner (desktop-owner)) | |
1070 | ;; Avoid desktop saving during evaluation of desktop buffer. | |
1071 | (desktop-save nil)) | |
1072 | (if (and owner | |
1f7efe1b JB |
1073 | (memq desktop-load-locked-desktop '(nil ask)) |
1074 | (or (null desktop-load-locked-desktop) | |
e76f0800 | 1075 | (daemonp) |
1f7efe1b JB |
1076 | (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\ |
1077 | Using it may cause conflicts. Use it anyway? " owner))))) | |
c41cf130 | 1078 | (let ((default-directory desktop-dirname)) |
794855ca | 1079 | (setq desktop-dirname nil) |
c41cf130 JB |
1080 | (run-hooks 'desktop-not-loaded-hook) |
1081 | (unless desktop-dirname | |
1082 | (message "Desktop file in use; not loaded."))) | |
e88110db JB |
1083 | (desktop-lazy-abort) |
1084 | ;; Evaluate desktop buffer and remember when it was modified. | |
1085 | (load (desktop-full-file-name) t t t) | |
1086 | (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))) | |
1087 | ;; If it wasn't already, mark it as in-use, to bother other | |
1088 | ;; desktop instances. | |
1089 | (unless owner | |
1090 | (condition-case nil | |
1091 | (desktop-claim-lock) | |
1092 | (file-error (message "Couldn't record use of desktop file") | |
1093 | (sit-for 1)))) | |
1094 | ||
1095 | ;; `desktop-create-buffer' puts buffers at end of the buffer list. | |
1096 | ;; We want buffers existing prior to evaluating the desktop (and | |
1097 | ;; not reused) to be placed at the end of the buffer list, so we | |
1098 | ;; move them here. | |
1099 | (mapc 'bury-buffer | |
1100 | (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list)))))) | |
1101 | (switch-to-buffer (car (buffer-list))) | |
1102 | (run-hooks 'desktop-delay-hook) | |
1103 | (setq desktop-delay-hook nil) | |
39c0e36f | 1104 | (desktop--restore-windows) |
e88110db JB |
1105 | (run-hooks 'desktop-after-read-hook) |
1106 | (message "Desktop: %d buffer%s restored%s%s." | |
1107 | desktop-buffer-ok-count | |
1108 | (if (= 1 desktop-buffer-ok-count) "" "s") | |
1109 | (if (< 0 desktop-buffer-fail-count) | |
1110 | (format ", %d failed to restore" desktop-buffer-fail-count) | |
1111 | "") | |
1112 | (if desktop-buffer-args-list | |
1113 | (format ", %d to restore lazily" | |
1114 | (length desktop-buffer-args-list)) | |
1115 | "")) | |
5221ccb9 MR |
1116 | ;; Bury the *Messages* buffer to not reshow it when burying |
1117 | ;; the buffer we switched to above. | |
1118 | (when (buffer-live-p (get-buffer "*Messages*")) | |
1119 | (bury-buffer "*Messages*")) | |
1120 | ;; Clear all windows' previous and next buffers, these have | |
1121 | ;; been corrupted by the `switch-to-buffer' calls in | |
1122 | ;; `desktop-restore-file-buffer' (bug#11556). This is a | |
1123 | ;; brute force fix and should be replaced by a more subtle | |
1124 | ;; strategy eventually. | |
1125 | (walk-window-tree (lambda (window) | |
1126 | (set-window-prev-buffers window nil) | |
1127 | (set-window-next-buffers window nil))) | |
e88110db | 1128 | t)) |
6b61353c KH |
1129 | ;; No desktop file found. |
1130 | (desktop-clear) | |
1131 | (let ((default-directory desktop-dirname)) | |
1132 | (run-hooks 'desktop-no-desktop-file-hook)) | |
1133 | (message "No desktop file.") | |
1134 | nil))) | |
4a2fce7a | 1135 | |
0b9bd504 | 1136 | ;; ---------------------------------------------------------------------------- |
6b61353c | 1137 | ;; Maintained for backward compatibility |
478653c9 | 1138 | ;;;###autoload |
6343ed61 | 1139 | (defun desktop-load-default () |
577ed2b2 | 1140 | "Load the `default' start-up library manually. |
6b61353c | 1141 | Also inhibit further loading of it." |
59f7af81 | 1142 | (declare (obsolete desktop-save-mode "22.1")) |
b89c5a72 JB |
1143 | (unless inhibit-default-init ; safety check |
1144 | (load "default" t t) | |
1145 | (setq inhibit-default-init t))) | |
4a2fce7a RS |
1146 | |
1147 | ;; ---------------------------------------------------------------------------- | |
1148 | ;;;###autoload | |
6b61353c KH |
1149 | (defun desktop-change-dir (dirname) |
1150 | "Change to desktop saved in DIRNAME. | |
1151 | Kill the desktop as specified by variables `desktop-save-mode' and | |
1152 | `desktop-save', then clear the desktop and load the desktop file in | |
1153 | directory DIRNAME." | |
1154 | (interactive "DChange to directory: ") | |
1155 | (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname))) | |
4a2fce7a RS |
1156 | (desktop-kill) |
1157 | (desktop-clear) | |
6b61353c | 1158 | (desktop-read dirname)) |
bf247b6e | 1159 | |
6b61353c | 1160 | ;; ---------------------------------------------------------------------------- |
4a2fce7a | 1161 | ;;;###autoload |
6b61353c KH |
1162 | (defun desktop-save-in-desktop-dir () |
1163 | "Save the desktop in directory `desktop-dirname'." | |
4a2fce7a RS |
1164 | (interactive) |
1165 | (if desktop-dirname | |
b89c5a72 | 1166 | (desktop-save desktop-dirname) |
4a2fce7a | 1167 | (call-interactively 'desktop-save)) |
a609d13b | 1168 | (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname))) |
4a2fce7a | 1169 | |
5db9dace JL |
1170 | ;; ---------------------------------------------------------------------------- |
1171 | ;; Auto-Saving. | |
1172 | (defvar desktop-auto-save-timer nil) | |
1173 | ||
1174 | (defun desktop-auto-save () | |
1175 | "Save the desktop periodically. | |
1176 | Called by the timer created in `desktop-auto-save-set-timer'." | |
1177 | (when (and desktop-save-mode | |
1178 | (integerp desktop-auto-save-timeout) | |
1179 | (> desktop-auto-save-timeout 0) | |
1180 | ;; Avoid desktop saving during lazy loading. | |
1181 | (not desktop-lazy-timer) | |
1182 | ;; Save only to own desktop file. | |
1183 | (eq (emacs-pid) (desktop-owner)) | |
1184 | desktop-dirname) | |
1185 | (desktop-save desktop-dirname nil t)) | |
1186 | (desktop-auto-save-set-timer)) | |
1187 | ||
1188 | (defun desktop-auto-save-set-timer () | |
1189 | "Reset the auto-save timer. | |
1190 | Cancel any previous timer. When `desktop-auto-save-timeout' is a positive | |
1191 | integer, start a new timer to call `desktop-auto-save' in that many seconds." | |
1192 | (when desktop-auto-save-timer | |
1193 | (cancel-timer desktop-auto-save-timer) | |
1194 | (setq desktop-auto-save-timer nil)) | |
1195 | (when (and (integerp desktop-auto-save-timeout) | |
1196 | (> desktop-auto-save-timeout 0)) | |
1197 | (setq desktop-auto-save-timer | |
1198 | (run-with-timer desktop-auto-save-timeout nil | |
1199 | 'desktop-auto-save)))) | |
1200 | ||
4a2fce7a RS |
1201 | ;; ---------------------------------------------------------------------------- |
1202 | ;;;###autoload | |
1203 | (defun desktop-revert () | |
1204 | "Revert to the last loaded desktop." | |
1205 | (interactive) | |
6b61353c KH |
1206 | (unless desktop-dirname |
1207 | (error "Unknown desktop directory")) | |
11425834 | 1208 | (unless (file-exists-p (desktop-full-file-name)) |
6b61353c KH |
1209 | (error "No desktop file found")) |
1210 | (desktop-clear) | |
1211 | (desktop-read desktop-dirname)) | |
4a2fce7a | 1212 | |
341c2f07 SM |
1213 | (defvar desktop-buffer-major-mode) |
1214 | (defvar desktop-buffer-locals) | |
06b60517 | 1215 | (defvar auto-insert) ; from autoinsert.el |
0b9bd504 | 1216 | ;; ---------------------------------------------------------------------------- |
06b60517 JB |
1217 | (defun desktop-restore-file-buffer (buffer-filename |
1218 | _buffer-name | |
1219 | _buffer-misc) | |
e5780ae1 | 1220 | "Restore a file buffer." |
06b60517 JB |
1221 | (when buffer-filename |
1222 | (if (or (file-exists-p buffer-filename) | |
1f7efe1b | 1223 | (let ((msg (format "Desktop: File \"%s\" no longer exists." |
06b60517 | 1224 | buffer-filename))) |
1f7efe1b JB |
1225 | (if desktop-missing-file-warning |
1226 | (y-or-n-p (concat msg " Re-create buffer? ")) | |
1227 | (message "%s" msg) | |
1228 | nil))) | |
1229 | (let* ((auto-insert nil) ; Disable auto insertion | |
1230 | (coding-system-for-read | |
1231 | (or coding-system-for-read | |
1232 | (cdr (assq 'buffer-file-coding-system | |
1233 | desktop-buffer-locals)))) | |
06b60517 | 1234 | (buf (find-file-noselect buffer-filename))) |
1f7efe1b JB |
1235 | (condition-case nil |
1236 | (switch-to-buffer buf) | |
1237 | (error (pop-to-buffer buf))) | |
1238 | (and (not (eq major-mode desktop-buffer-major-mode)) | |
1239 | (functionp desktop-buffer-major-mode) | |
1240 | (funcall desktop-buffer-major-mode)) | |
1241 | buf) | |
1242 | nil))) | |
4a2fce7a | 1243 | |
0f4804e7 LH |
1244 | (defun desktop-load-file (function) |
1245 | "Load the file where auto loaded FUNCTION is defined." | |
7abaf5cc SM |
1246 | (when (fboundp function) |
1247 | (autoload-do-load (symbol-function function) function))) | |
0f4804e7 | 1248 | |
0b9bd504 | 1249 | ;; ---------------------------------------------------------------------------- |
eb982898 JL |
1250 | ;; Create a buffer, load its file, set its mode, ...; |
1251 | ;; called from Desktop file only. | |
1d500ca6 | 1252 | |
0f4804e7 | 1253 | ;; Just to silence the byte compiler. |
341c2f07 SM |
1254 | |
1255 | (defvar desktop-first-buffer) ; Dynamically bound in `desktop-read' | |
1256 | ||
1257 | ;; Bound locally in `desktop-read'. | |
1258 | (defvar desktop-buffer-ok-count) | |
1259 | (defvar desktop-buffer-fail-count) | |
4a2fce7a | 1260 | |
340db502 | 1261 | (defun desktop-create-buffer |
06b60517 JB |
1262 | (file-version |
1263 | buffer-filename | |
1264 | buffer-name | |
1265 | buffer-majormode | |
1266 | buffer-minormodes | |
1267 | buffer-point | |
1268 | buffer-mark | |
1269 | buffer-readonly | |
1270 | buffer-misc | |
1271 | &optional | |
1272 | buffer-locals) | |
1273 | ||
1274 | (let ((desktop-file-version file-version) | |
1275 | (desktop-buffer-file-name buffer-filename) | |
1276 | (desktop-buffer-name buffer-name) | |
1277 | (desktop-buffer-major-mode buffer-majormode) | |
1278 | (desktop-buffer-minor-modes buffer-minormodes) | |
1279 | (desktop-buffer-point buffer-point) | |
1280 | (desktop-buffer-mark buffer-mark) | |
1281 | (desktop-buffer-read-only buffer-readonly) | |
1282 | (desktop-buffer-misc buffer-misc) | |
1283 | (desktop-buffer-locals buffer-locals)) | |
1284 | ;; To make desktop files with relative file names possible, we cannot | |
1285 | ;; allow `default-directory' to change. Therefore we save current buffer. | |
1286 | (save-current-buffer | |
1287 | ;; Give major mode module a chance to add a handler. | |
1288 | (desktop-load-file desktop-buffer-major-mode) | |
1289 | (let ((buffer-list (buffer-list)) | |
1290 | (result | |
1be3ca5a | 1291 | (condition-case-unless-debug err |
06b60517 JB |
1292 | (funcall (or (cdr (assq desktop-buffer-major-mode |
1293 | desktop-buffer-mode-handlers)) | |
1294 | 'desktop-restore-file-buffer) | |
1295 | desktop-buffer-file-name | |
1296 | desktop-buffer-name | |
1297 | desktop-buffer-misc) | |
1298 | (error | |
1299 | (message "Desktop: Can't load buffer %s: %s" | |
1300 | desktop-buffer-name | |
1301 | (error-message-string err)) | |
1302 | (when desktop-missing-file-warning (sit-for 1)) | |
1303 | nil)))) | |
1304 | (if (bufferp result) | |
1305 | (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count)) | |
1306 | (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count)) | |
1307 | (setq result nil)) | |
1308 | ;; Restore buffer list order with new buffer at end. Don't change | |
91af3942 | 1309 | ;; the order for old desktop files (old desktop module behavior). |
06b60517 JB |
1310 | (unless (< desktop-file-version 206) |
1311 | (mapc 'bury-buffer buffer-list) | |
1312 | (when result (bury-buffer result))) | |
1313 | (when result | |
1314 | (unless (or desktop-first-buffer (< desktop-file-version 206)) | |
1315 | (setq desktop-first-buffer result)) | |
1316 | (set-buffer result) | |
1317 | (unless (equal (buffer-name) desktop-buffer-name) | |
1318 | (rename-buffer desktop-buffer-name t)) | |
1319 | ;; minor modes | |
1320 | (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible | |
1321 | (auto-fill-mode 1)) | |
1322 | ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible | |
1323 | (auto-fill-mode 0)) | |
1324 | (t | |
1325 | (dolist (minor-mode desktop-buffer-minor-modes) | |
1326 | ;; Give minor mode module a chance to add a handler. | |
1327 | (desktop-load-file minor-mode) | |
1328 | (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers)))) | |
1329 | (if handler | |
1330 | (funcall handler desktop-buffer-locals) | |
1331 | (when (functionp minor-mode) | |
1332 | (funcall minor-mode 1))))))) | |
1333 | ;; Even though point and mark are non-nil when written by | |
1334 | ;; `desktop-save', they may be modified by handlers wanting to set | |
1335 | ;; point or mark themselves. | |
1336 | (when desktop-buffer-point | |
1337 | (goto-char | |
1338 | (condition-case err | |
1339 | ;; Evaluate point. Thus point can be something like | |
1340 | ;; '(search-forward ... | |
1341 | (eval desktop-buffer-point) | |
1342 | (error (message "%s" (error-message-string err)) 1)))) | |
1343 | (when desktop-buffer-mark | |
1344 | (if (consp desktop-buffer-mark) | |
1f7efe1b | 1345 | (progn |
06b60517 JB |
1346 | (set-mark (car desktop-buffer-mark)) |
1347 | (setq mark-active (car (cdr desktop-buffer-mark)))) | |
1348 | (set-mark desktop-buffer-mark))) | |
1349 | ;; Never override file system if the file really is read-only marked. | |
1350 | (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only)) | |
1351 | (while desktop-buffer-locals | |
1352 | (let ((this (car desktop-buffer-locals))) | |
1353 | (if (consp this) | |
1354 | ;; an entry of this form `(symbol . value)' | |
1355 | (progn | |
1356 | (make-local-variable (car this)) | |
1357 | (set (car this) (cdr this))) | |
1358 | ;; an entry of the form `symbol' | |
1359 | (make-local-variable this) | |
1360 | (makunbound this))) | |
1361 | (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))) | |
ec4c6f22 | 1362 | |
4a2fce7a | 1363 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 | 1364 | ;; Backward compatibility -- update parameters to 205 standards. |
06b60517 JB |
1365 | (defun desktop-buffer (buffer-filename buffer-name buffer-majormode |
1366 | mim pt mk ro tl fc cfs cr buffer-misc) | |
1367 | (desktop-create-buffer 205 buffer-filename buffer-name | |
1368 | buffer-majormode (cdr mim) pt mk ro | |
1369 | buffer-misc | |
ec4c6f22 RS |
1370 | (list (cons 'truncate-lines tl) |
1371 | (cons 'fill-column fc) | |
1372 | (cons 'case-fold-search cfs) | |
1373 | (cons 'case-replace cr) | |
1374 | (cons 'overwrite-mode (car mim))))) | |
84b538ec | 1375 | |
150f39ee | 1376 | (defun desktop-append-buffer-args (&rest args) |
cc8b76bf | 1377 | "Append ARGS at end of `desktop-buffer-args-list'. |
150f39ee LH |
1378 | ARGS must be an argument list for `desktop-create-buffer'." |
1379 | (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args))) | |
1380 | (unless desktop-lazy-timer | |
1381 | (setq desktop-lazy-timer | |
1382 | (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers)))) | |
1383 | ||
1384 | (defun desktop-lazy-create-buffer () | |
1385 | "Pop args from `desktop-buffer-args-list', create buffer and bury it." | |
1386 | (when desktop-buffer-args-list | |
1387 | (let* ((remaining (length desktop-buffer-args-list)) | |
1388 | (args (pop desktop-buffer-args-list)) | |
1389 | (buffer-name (nth 2 args)) | |
1390 | (msg (format "Desktop lazily opening %s (%s remaining)..." | |
1391 | buffer-name remaining))) | |
1392 | (when desktop-lazy-verbose | |
8a26c165 | 1393 | (message "%s" msg)) |
150f39ee LH |
1394 | (let ((desktop-first-buffer nil) |
1395 | (desktop-buffer-ok-count 0) | |
1396 | (desktop-buffer-fail-count 0)) | |
1397 | (apply 'desktop-create-buffer args) | |
1398 | (run-hooks 'desktop-delay-hook) | |
1399 | (setq desktop-delay-hook nil) | |
1400 | (bury-buffer (get-buffer buffer-name)) | |
1401 | (when desktop-lazy-verbose | |
1402 | (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed"))))))) | |
1403 | ||
1404 | (defun desktop-idle-create-buffers () | |
1405 | "Create buffers until the user does something, then stop. | |
1406 | If there are no buffers left to create, kill the timer." | |
1407 | (let ((repeat 1)) | |
1408 | (while (and repeat desktop-buffer-args-list) | |
1409 | (save-window-excursion | |
1410 | (desktop-lazy-create-buffer)) | |
1411 | (setq repeat (sit-for 0.2)) | |
1412 | (unless desktop-buffer-args-list | |
1413 | (cancel-timer desktop-lazy-timer) | |
1414 | (setq desktop-lazy-timer nil) | |
1415 | (message "Lazy desktop load complete") | |
1416 | (sit-for 3) | |
1417 | (message ""))))) | |
1418 | ||
1419 | (defun desktop-lazy-complete () | |
1420 | "Run the desktop load to completion." | |
1421 | (interactive) | |
1422 | (let ((desktop-lazy-verbose t)) | |
1423 | (while desktop-buffer-args-list | |
1424 | (save-window-excursion | |
1425 | (desktop-lazy-create-buffer))) | |
1426 | (message "Lazy desktop load complete"))) | |
1427 | ||
1428 | (defun desktop-lazy-abort () | |
1429 | "Abort lazy loading of the desktop." | |
1430 | (interactive) | |
1431 | (when desktop-lazy-timer | |
1432 | (cancel-timer desktop-lazy-timer) | |
1433 | (setq desktop-lazy-timer nil)) | |
1434 | (when desktop-buffer-args-list | |
1435 | (setq desktop-buffer-args-list nil) | |
32226619 | 1436 | (when (called-interactively-p 'interactive) |
150f39ee LH |
1437 | (message "Lazy desktop load aborted")))) |
1438 | ||
0b9bd504 | 1439 | ;; ---------------------------------------------------------------------------- |
6b61353c | 1440 | ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the |
4a2fce7a RS |
1441 | ;; command line, we do the rest of what it takes to use desktop, but do it |
1442 | ;; after finishing loading the init file. | |
1443 | ;; We cannot use `command-switch-alist' to process "--no-desktop" because these | |
1444 | ;; functions are processed after `after-init-hook'. | |
1445 | (add-hook | |
1446 | 'after-init-hook | |
341c2f07 | 1447 | (lambda () |
4a2fce7a | 1448 | (let ((key "--no-desktop")) |
4b217d46 LH |
1449 | (when (member key command-line-args) |
1450 | (setq command-line-args (delete key command-line-args)) | |
1451 | (setq desktop-save-mode nil))) | |
54d22a6f JL |
1452 | (when desktop-save-mode |
1453 | (desktop-read) | |
5db9dace | 1454 | (desktop-auto-save-set-timer) |
54d22a6f | 1455 | (setq inhibit-startup-screen t)))) |
813dbb2d | 1456 | |
f12ad6ec GM |
1457 | ;; So we can restore vc-dir buffers. |
1458 | (autoload 'vc-dir-mode "vc-dir" nil t) | |
1459 | ||
ab1d55ea | 1460 | (provide 'desktop) |
6343ed61 | 1461 | |
80f9f3db | 1462 | ;;; desktop.el ends here |