Commit | Line | Data |
---|---|---|
6343ed61 RS |
1 | ;;; desktop.el --- save partial status of Emacs when killed |
2 | ||
0d30b337 TTN |
3 | ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003, |
4 | ;; 2004, 2005 Free Software Foundation, Inc. | |
6343ed61 RS |
5 | |
6 | ;; Author: Morten Welinder <terra@diku.dk> | |
247c3649 | 7 | ;; Maintainter: Lars Hansen <larsh@soem.dk> |
3ea96cac | 8 | ;; Keywords: convenience |
b6105c76 | 9 | ;; Favourite-brand-of-beer: None, I hate beer. |
6343ed61 RS |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 | ;; it under the terms of the GNU General Public License as published by | |
15 | ;; the Free Software Foundation; either version 2, or (at your option) | |
16 | ;; any later version. | |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 | ;; Boston, MA 02110-1301, USA. | |
6343ed61 RS |
27 | |
28 | ;;; Commentary: | |
29 | ||
0b9bd504 RS |
30 | ;; Save the Desktop, i.e., |
31 | ;; - some global variables | |
32 | ;; - the list of buffers with associated files. For each buffer also | |
33 | ;; - the major mode | |
34 | ;; - the default directory | |
35 | ;; - the point | |
36 | ;; - the mark & mark-active | |
37 | ;; - buffer-read-only | |
ec4c6f22 | 38 | ;; - some local variables |
6343ed61 | 39 | |
c5b31c4d LH |
40 | ;; To use this, use customize to turn on desktop-save-mode or add the |
41 | ;; following line somewhere in your .emacs file: | |
0b9bd504 | 42 | ;; |
c5b31c4d | 43 | ;; (desktop-save-mode 1) |
0b9bd504 | 44 | ;; |
c5b31c4d LH |
45 | ;; For further usage information, look at the section |
46 | ;; "Saving Emacs Sessions" in the GNU Emacs Manual. | |
47 | ||
48 | ;; When the desktop module is loaded, the function `desktop-kill' is | |
49 | ;; added to the `kill-emacs-hook'. This function is responsible for | |
50 | ;; saving the desktop when Emacs is killed. Furthermore an anonymous | |
51 | ;; function is added to the `after-init-hook'. This function is | |
52 | ;; responsible for loading the desktop when Emacs is started. | |
ec4c6f22 | 53 | |
0f4804e7 LH |
54 | ;; Special handling. |
55 | ;; ----------------- | |
56 | ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers' | |
57 | ;; are supplied to handle special major and minor modes respectively. | |
58 | ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions | |
59 | ;; to restore a desktop buffer. Elements must have the form | |
60 | ;; | |
61 | ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION). | |
62 | ;; | |
63 | ;; Functions listed are called by `desktop-create-buffer' when `desktop-read' | |
64 | ;; evaluates the desktop file. Buffers with a major mode not specified here, | |
65 | ;; are restored by the default handler `desktop-restore-file-buffer'. | |
66 | ;; `desktop-minor-mode-handlers' is an alist of functions to restore | |
67 | ;; non-standard minor modes. Elements must have the form | |
68 | ;; | |
69 | ;; (MINOR-MODE . RESTORE-FUNCTION). | |
70 | ;; | |
71 | ;; Functions are called by `desktop-create-buffer' to restore minor modes. | |
72 | ;; Minor modes not specified here, are restored by the standard minor mode | |
73 | ;; function. If you write a module that defines a major or minor mode that | |
74 | ;; needs a special handler, then place code like | |
75 | ||
76 | ;; (defun foo-restore-desktop-buffer | |
77 | ;; ... | |
78 | ;; (add-to-list 'desktop-buffer-mode-handlers | |
79 | ;; '(foo-mode . foo-restore-desktop-buffer)) | |
80 | ||
81 | ;; or | |
82 | ||
83 | ;; (defun bar-desktop-restore | |
84 | ;; ... | |
85 | ;; (add-to-list 'desktop-minor-mode-handlers | |
86 | ;; '(bar-mode . bar-desktop-restore)) | |
87 | ||
88 | ;; in the module itself, and make shure that the mode function is | |
89 | ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and | |
90 | ;; `desktop-minor-mode-handlers' for more info. | |
91 | ||
92 | ;; Minor modes. | |
93 | ;; ------------ | |
94 | ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp | |
95 | ;; manual) are handled in the following way: | |
96 | ;; When `desktop-save' saves the state of a buffer to the desktop file, it | |
97 | ;; saves as `desktop-minor-modes' the list of names of those variables in | |
98 | ;; `minor-mode-alist' that have a non-nil value. | |
99 | ;; When `desktop-create' restores the buffer, each of the symbols in | |
100 | ;; `desktop-minor-modes' is called as function with parameter 1. | |
101 | ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers' | |
102 | ;; are used to handle non-conventional minor modes. `desktop-save' uses | |
103 | ;; `desktop-minor-mode-table' to map minor mode variables to minor mode | |
104 | ;; functions before writing `desktop-minor-modes'. If a minor mode has a | |
105 | ;; variable name that is different form its function name, an entry | |
106 | ||
107 | ;; (NAME RESTORE-FUNCTION) | |
108 | ||
109 | ;; should be added to `desktop-minor-mode-table'. If a minor mode should not | |
110 | ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses | |
111 | ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore | |
112 | ;; function different from the usual minor mode function. | |
113 | ;; --------------------------------------------------------------------------- | |
47640244 | 114 | |
ec4c6f22 RS |
115 | ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs |
116 | ;; in your home directory is used for that. Saving global default values | |
117 | ;; for buffers is an example of misuse. | |
118 | ||
0b9bd504 RS |
119 | ;; PLEASE NOTE: The kill ring can be saved as specified by the variable |
120 | ;; `desktop-globals-to-save' (by default it isn't). This may result in saving | |
121 | ;; things you did not mean to keep. Use M-x desktop-clear RET. | |
ec4c6f22 | 122 | |
fa379636 KH |
123 | ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas. |
124 | ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip. | |
125 | ;; chris@tecc.co.uk (Chris Boucher) for a mark tip. | |
126 | ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip. | |
127 | ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt. | |
f4c73d07 | 128 | ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips. |
253406b9 | 129 | ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips. |
0b9bd504 RS |
130 | ;; --------------------------------------------------------------------------- |
131 | ;; TODO: | |
132 | ;; | |
133 | ;; Save window configuration. | |
134 | ;; Recognize more minor modes. | |
135 | ;; Save mark rings. | |
6343ed61 RS |
136 | |
137 | ;;; Code: | |
138 | ||
4a2fce7a | 139 | (defvar desktop-file-version "206" |
05f1c4ec | 140 | "Version number of desktop file format. |
4a2fce7a RS |
141 | Written into the desktop file and used at desktop read to provide |
142 | backward compatibility.") | |
143 | ||
ec4c6f22 | 144 | ;; ---------------------------------------------------------------------------- |
0b9bd504 RS |
145 | ;; USER OPTIONS -- settings you might want to play with. |
146 | ;; ---------------------------------------------------------------------------- | |
bbf5eb28 RS |
147 | |
148 | (defgroup desktop nil | |
149 | "Save status of Emacs when you exit." | |
150 | :group 'frames) | |
151 | ||
c5b31c4d LH |
152 | ;;;###autoload |
153 | (define-minor-mode desktop-save-mode | |
154 | "Toggle desktop saving mode. | |
155 | With numeric ARG, turn desktop saving on if ARG is positive, off | |
156 | otherwise. See variable `desktop-save' for a description of when the | |
157 | desktop is saved." | |
158 | :global t | |
159 | :group 'desktop) | |
160 | ||
161 | ;; Maintained for backward compatibility | |
b89c5a72 JB |
162 | (define-obsolete-variable-alias 'desktop-enable |
163 | 'desktop-save-mode "22.1") | |
813dbb2d | 164 | |
4a2fce7a | 165 | (defcustom desktop-save 'ask-if-new |
c5b31c4d LH |
166 | "*Specifies whether the desktop should be saved when it is killed. |
167 | A desktop is killed when the user changes desktop or quits Emacs. | |
168 | Possible values are: | |
4a2fce7a RS |
169 | t -- always save. |
170 | ask -- always ask. | |
171 | ask-if-new -- ask if no desktop file exists, otherwise just save. | |
172 | ask-if-exists -- ask if desktop file exists, otherwise don't save. | |
173 | if-exists -- save if desktop file exists, otherwise don't save. | |
174 | nil -- never save. | |
c5b31c4d | 175 | The desktop is never saved when `desktop-save-mode' is nil. |
cc8b76bf | 176 | The variables `desktop-dirname' and `desktop-base-file-name' |
c5b31c4d | 177 | determine where the desktop is saved." |
4a2fce7a RS |
178 | :type '(choice |
179 | (const :tag "Always save" t) | |
180 | (const :tag "Always ask" ask) | |
181 | (const :tag "Ask if desktop file is new, else do save" ask-if-new) | |
182 | (const :tag "Ask if desktop file exists, else don't save" ask-if-exists) | |
183 | (const :tag "Save if desktop file exists, else don't" if-exists) | |
184 | (const :tag "Never save" nil)) | |
af61551b | 185 | :group 'desktop |
bf247b6e | 186 | :version "22.1") |
4a2fce7a RS |
187 | |
188 | (defcustom desktop-base-file-name | |
fc715501 | 189 | (convert-standard-filename ".emacs.desktop") |
c5b31c4d | 190 | "Name of file for Emacs desktop, excluding the directory part." |
813dbb2d RS |
191 | :type 'file |
192 | :group 'desktop) | |
b89c5a72 JB |
193 | (define-obsolete-variable-alias 'desktop-basefilename |
194 | 'desktop-base-file-name "22.1") | |
6343ed61 | 195 | |
4a2fce7a RS |
196 | (defcustom desktop-path '("." "~") |
197 | "List of directories to search for the desktop file. | |
198 | The base name of the file is specified in `desktop-base-file-name'." | |
199 | :type '(repeat directory) | |
af61551b | 200 | :group 'desktop |
bf247b6e | 201 | :version "22.1") |
4a2fce7a | 202 | |
bbf5eb28 | 203 | (defcustom desktop-missing-file-warning nil |
ebb39555 LH |
204 | "*If non-nil then `desktop-read' asks if a non-existent file should be recreated. |
205 | Also pause for a moment to display message about errors signaled in | |
206 | `desktop-buffer-mode-handlers'. | |
207 | ||
208 | If nil, just print error messages in the message buffer." | |
bbf5eb28 | 209 | :type 'boolean |
af61551b | 210 | :group 'desktop |
bf247b6e | 211 | :version "22.1") |
6343ed61 | 212 | |
4a2fce7a | 213 | (defcustom desktop-no-desktop-file-hook nil |
c5b31c4d | 214 | "Normal hook run when `desktop-read' can't find a desktop file. |
8649a87b | 215 | May be used to show a dired buffer." |
4a2fce7a | 216 | :type 'hook |
af61551b | 217 | :group 'desktop |
bf247b6e | 218 | :version "22.1") |
4a2fce7a RS |
219 | |
220 | (defcustom desktop-after-read-hook nil | |
05f1c4ec | 221 | "Normal hook run after a successful `desktop-read'. |
8649a87b | 222 | May be used to show a buffer list." |
4a2fce7a | 223 | :type 'hook |
af61551b | 224 | :group 'desktop |
bf247b6e | 225 | :version "22.1") |
4a2fce7a RS |
226 | |
227 | (defcustom desktop-save-hook nil | |
c5b31c4d | 228 | "Normal hook run before the desktop is saved in a desktop file. |
4a2fce7a RS |
229 | This is useful for truncating history lists, for example." |
230 | :type 'hook | |
231 | :group 'desktop) | |
232 | ||
340db502 LH |
233 | (defcustom desktop-globals-to-save |
234 | '(desktop-missing-file-warning | |
235 | tags-file-name | |
236 | tags-table-list | |
237 | search-ring | |
238 | regexp-search-ring | |
239 | register-alist) | |
c5b31c4d LH |
240 | "List of global variables saved by `desktop-save'. |
241 | An element may be variable name (a symbol) or a cons cell of the form | |
242 | \(VAR . MAX-SIZE), which means to truncate VAR's value to at most | |
243 | MAX-SIZE elements (if the value is a list) before saving the value. | |
4a2fce7a RS |
244 | Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'." |
245 | :type '(repeat (restricted-sexp :match-alternatives (symbolp consp))) | |
246 | :group 'desktop) | |
247 | ||
340db502 LH |
248 | (defcustom desktop-globals-to-clear |
249 | '(kill-ring | |
250 | kill-ring-yank-pointer | |
251 | search-ring | |
252 | search-ring-yank-pointer | |
253 | regexp-search-ring | |
254 | regexp-search-ring-yank-pointer) | |
150f39ee | 255 | "List of global variables that `desktop-clear' will clear. |
4a2fce7a | 256 | An element may be variable name (a symbol) or a cons cell of the form |
cc8b76bf JB |
257 | \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set |
258 | to the value obtained by evaluating FORM." | |
4a2fce7a | 259 | :type '(repeat (restricted-sexp :match-alternatives (symbolp consp))) |
af61551b | 260 | :group 'desktop |
bf247b6e | 261 | :version "22.1") |
4a2fce7a | 262 | |
0f4804e7 LH |
263 | (defcustom desktop-clear-preserve-buffers |
264 | '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*") | |
265 | "*List of buffers that `desktop-clear' should not delete. | |
266 | Each element is a regular expression. Buffers with a name matched by any of | |
267 | these won't be deleted." | |
4a2fce7a RS |
268 | :type '(repeat string) |
269 | :group 'desktop) | |
340db502 | 270 | |
0f4804e7 | 271 | ;;;###autoload |
340db502 LH |
272 | (defcustom desktop-locals-to-save |
273 | '(desktop-locals-to-save ; Itself! Think it over. | |
274 | truncate-lines | |
275 | case-fold-search | |
276 | case-replace | |
277 | fill-column | |
278 | overwrite-mode | |
279 | change-log-default-name | |
280 | line-number-mode | |
0f4804e7 LH |
281 | column-number-mode |
282 | size-indication-mode | |
283 | buffer-file-coding-system | |
284 | indent-tabs-mode | |
285 | indicate-buffer-boundaries | |
286 | indicate-empty-lines | |
287 | show-trailing-whitespace) | |
577ed2b2 | 288 | "List of local variables to save for each buffer. |
0f4804e7 LH |
289 | The variables are saved only when they really are local. Conventional minor |
290 | modes are restored automatically; they should not be listed here." | |
c5b31c4d LH |
291 | :type '(repeat symbol) |
292 | :group 'desktop) | |
ec4c6f22 | 293 | |
0b9bd504 | 294 | ;; We skip .log files because they are normally temporary. |
a7acbbe4 | 295 | ;; (ftp) files because they require passwords and whatnot. |
0b9bd504 | 296 | ;; TAGS files to save time (tags-file-name is saved instead). |
bbf5eb28 | 297 | (defcustom desktop-buffers-not-to-save |
4a2fce7a RS |
298 | "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$" |
299 | "Regexp identifying buffers that are to be excluded from saving." | |
300 | :type 'regexp | |
301 | :group 'desktop) | |
6343ed61 | 302 | |
c5b31c4d | 303 | ;; Skip tramp and ange-ftp files |
bbf5eb28 | 304 | (defcustom desktop-files-not-to-save |
fa379636 | 305 | "^/[^/:]*:" |
bbf5eb28 RS |
306 | "Regexp identifying files whose buffers are to be excluded from saving." |
307 | :type 'regexp | |
308 | :group 'desktop) | |
fa379636 | 309 | |
80f9f3db SM |
310 | (defcustom desktop-modes-not-to-save nil |
311 | "List of major modes whose buffers should not be saved." | |
312 | :type '(repeat symbol) | |
313 | :group 'desktop) | |
314 | ||
4a2fce7a RS |
315 | (defcustom desktop-file-name-format 'absolute |
316 | "*Format in which desktop file names should be saved. | |
317 | Possible values are: | |
318 | absolute -- Absolute file name. | |
319 | tilde -- Relative to ~. | |
320 | local -- Relative to directory of desktop file." | |
321 | :type '(choice (const absolute) (const tilde) (const local)) | |
af61551b | 322 | :group 'desktop |
bf247b6e | 323 | :version "22.1") |
569c754e | 324 | |
150f39ee LH |
325 | (defcustom desktop-restore-eager t |
326 | "Number of buffers to restore immediately. | |
327 | Remaining buffers are restored lazily (when Emacs is idle). | |
328 | If value is t, all buffers are restored immediately." | |
0ba9bc53 | 329 | :type '(choice (const t) integer) |
150f39ee | 330 | :group 'desktop |
bf247b6e | 331 | :version "22.1") |
150f39ee LH |
332 | |
333 | (defcustom desktop-lazy-verbose t | |
334 | "Verbose reporting of lazily created buffers." | |
335 | :type 'boolean | |
336 | :group 'desktop | |
bf247b6e | 337 | :version "22.1") |
150f39ee LH |
338 | |
339 | (defcustom desktop-lazy-idle-delay 5 | |
340 | "Idle delay before starting to create buffers. | |
341 | See `desktop-restore-eager'." | |
342 | :type 'integer | |
343 | :group 'desktop | |
bf247b6e | 344 | :version "22.1") |
150f39ee | 345 | |
e5780ae1 | 346 | ;;;###autoload |
ebb39555 LH |
347 | (defvar desktop-save-buffer nil |
348 | "When non-nil, save buffer status in desktop file. | |
e5780ae1 | 349 | This variable becomes buffer local when set. |
ebb39555 | 350 | |
b89c5a72 JB |
351 | If the value is a function, it is called by `desktop-save' with argument |
352 | DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop | |
ebb39555 | 353 | file along with the state of the buffer for which it was called. |
b6b70cda | 354 | |
c5b31c4d | 355 | When file names are returned, they should be formatted using the call |
e5780ae1 | 356 | \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\". |
4a2fce7a | 357 | |
0f4804e7 LH |
358 | Later, when `desktop-read' evaluates the desktop file, auxiliary information |
359 | is passed as the argument DESKTOP-BUFFER-MISC to functions in | |
360 | `desktop-buffer-mode-handlers'.") | |
ebb39555 LH |
361 | (make-variable-buffer-local 'desktop-save-buffer) |
362 | (make-obsolete-variable 'desktop-buffer-modes-to-save | |
cc8b76bf | 363 | 'desktop-save-buffer "22.1") |
e5780ae1 | 364 | (make-obsolete-variable 'desktop-buffer-misc-functions |
cc8b76bf | 365 | 'desktop-save-buffer "22.1") |
b6b70cda | 366 | |
0f4804e7 LH |
367 | ;;;###autoload |
368 | (defvar desktop-buffer-mode-handlers | |
369 | nil | |
e5780ae1 | 370 | "Alist of major mode specific functions to restore a desktop buffer. |
0f4804e7 LH |
371 | Functions listed are called by `desktop-create-buffer' when `desktop-read' |
372 | evaluates the desktop file. List elements must have the form | |
373 | ||
374 | (MAJOR-MODE . RESTORE-BUFFER-FUNCTION). | |
e5780ae1 LH |
375 | |
376 | Buffers with a major mode not specified here, are restored by the default | |
377 | handler `desktop-restore-file-buffer'. | |
378 | ||
55775448 | 379 | Handlers are called with argument list |
4a2fce7a | 380 | |
9bcabb45 | 381 | (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC) |
e5780ae1 LH |
382 | |
383 | Furthermore, they may use the following variables: | |
384 | ||
385 | desktop-file-version | |
4a2fce7a RS |
386 | desktop-buffer-major-mode |
387 | desktop-buffer-minor-modes | |
388 | desktop-buffer-point | |
389 | desktop-buffer-mark | |
390 | desktop-buffer-read-only | |
4a2fce7a RS |
391 | desktop-buffer-locals |
392 | ||
e5780ae1 | 393 | If a handler returns a buffer, then the saved mode settings |
0f4804e7 LH |
394 | and variable values for that buffer are copied into it. |
395 | ||
396 | Modules that define a major mode that needs a special handler should contain | |
397 | code like | |
398 | ||
399 | (defun foo-restore-desktop-buffer | |
400 | ... | |
401 | (add-to-list 'desktop-buffer-mode-handlers | |
402 | '(foo-mode . foo-restore-desktop-buffer)) | |
403 | ||
404 | Furthermore the major mode function must be autoloaded.") | |
ec4c6f22 | 405 | |
498eb267 | 406 | ;;;###autoload |
e5780ae1 LH |
407 | (put 'desktop-buffer-mode-handlers 'risky-local-variable t) |
408 | (make-obsolete-variable 'desktop-buffer-handlers | |
cc8b76bf | 409 | 'desktop-buffer-mode-handlers "22.1") |
b6b70cda | 410 | |
47640244 GM |
411 | (defcustom desktop-minor-mode-table |
412 | '((auto-fill-function auto-fill-mode) | |
413 | (vc-mode nil)) | |
414 | "Table mapping minor mode variables to minor mode functions. | |
415 | Each entry has the form (NAME RESTORE-FUNCTION). | |
416 | NAME is the name of the buffer-local variable indicating that the minor | |
417 | mode is active. RESTORE-FUNCTION is the function to activate the minor mode. | |
418 | called. RESTORE-FUNCTION nil means don't try to restore the minor mode. | |
419 | Only minor modes for which the name of the buffer-local variable | |
7bfa55b3 | 420 | and the name of the minor mode function are different have to be added to |
0f4804e7 | 421 | this table. See also `desktop-minor-mode-handlers'." |
47640244 GM |
422 | :type 'sexp |
423 | :group 'desktop) | |
424 | ||
0f4804e7 LH |
425 | ;;;###autoload |
426 | (defvar desktop-minor-mode-handlers | |
427 | nil | |
428 | "Alist of functions to restore non-standard minor modes. | |
429 | Functions are called by `desktop-create-buffer' to restore minor modes. | |
430 | List elements must have the form | |
431 | ||
432 | (MINOR-MODE . RESTORE-FUNCTION). | |
433 | ||
434 | Minor modes not specified here, are restored by the standard minor mode | |
435 | function. | |
436 | ||
437 | Handlers are called with argument list | |
438 | ||
439 | (DESKTOP-BUFFER-LOCALS) | |
440 | ||
441 | Furthermore, they may use the following variables: | |
442 | ||
443 | desktop-file-version | |
444 | desktop-buffer-file-name | |
445 | desktop-buffer-name | |
446 | desktop-buffer-major-mode | |
447 | desktop-buffer-minor-modes | |
448 | desktop-buffer-point | |
449 | desktop-buffer-mark | |
450 | desktop-buffer-read-only | |
451 | desktop-buffer-misc | |
452 | ||
453 | When a handler is called, the buffer has been created and the major mode has | |
454 | been set, but local variables listed in desktop-buffer-locals has not yet been | |
455 | created and set. | |
456 | ||
457 | Modules that define a minor mode that needs a special handler should contain | |
458 | code like | |
459 | ||
460 | (defun foo-desktop-restore | |
461 | ... | |
462 | (add-to-list 'desktop-minor-mode-handlers | |
463 | '(foo-mode . foo-desktop-restore)) | |
464 | ||
465 | Furthermore the minor mode function must be autoloaded. | |
466 | ||
467 | See also `desktop-minor-mode-table'.") | |
468 | ||
498eb267 | 469 | ;;;###autoload |
0f4804e7 LH |
470 | (put 'desktop-minor-mode-handlers 'risky-local-variable t) |
471 | ||
0b9bd504 RS |
472 | ;; ---------------------------------------------------------------------------- |
473 | (defvar desktop-dirname nil | |
c5b31c4d | 474 | "The directory in which the desktop file should be saved.") |
6343ed61 RS |
475 | |
476 | (defconst desktop-header | |
0b9bd504 RS |
477 | ";; -------------------------------------------------------------------------- |
478 | ;; Desktop File for Emacs | |
479 | ;; -------------------------------------------------------------------------- | |
6343ed61 | 480 | " "*Header to place in Desktop file.") |
de9e2828 RS |
481 | |
482 | (defvar desktop-delay-hook nil | |
483 | "Hooks run after all buffers are loaded; intended for internal use.") | |
813dbb2d | 484 | |
0b9bd504 | 485 | ;; ---------------------------------------------------------------------------- |
05f1c4ec | 486 | (defun desktop-truncate (list n) |
ec4c6f22 | 487 | "Truncate LIST to at most N elements destructively." |
05f1c4ec | 488 | (let ((here (nthcdr (1- n) list))) |
ec4c6f22 | 489 | (if (consp here) |
de9e2828 | 490 | (setcdr here nil)))) |
f9be4574 | 491 | |
4a2fce7a | 492 | ;; ---------------------------------------------------------------------------- |
f9be4574 RS |
493 | (defun desktop-clear () |
494 | "Empty the Desktop. | |
0f4804e7 LH |
495 | This kills all buffers except for internal ones and those with names matched by |
496 | a regular expression in the list `desktop-clear-preserve-buffers'. | |
497 | Furthermore, it clears the variables listed in `desktop-globals-to-clear'." | |
6343ed61 | 498 | (interactive) |
150f39ee | 499 | (desktop-lazy-abort) |
4a2fce7a RS |
500 | (dolist (var desktop-globals-to-clear) |
501 | (if (symbolp var) | |
502 | (eval `(setq-default ,var nil)) | |
503 | (eval `(setq-default ,(car var) ,(cdr var))))) | |
0f4804e7 LH |
504 | (let ((buffers (buffer-list)) |
505 | (preserve-regexp (concat "^\\(" | |
506 | (mapconcat (lambda (regexp) | |
507 | (concat "\\(" regexp "\\)")) | |
508 | desktop-clear-preserve-buffers | |
509 | "\\|") | |
510 | "\\)$"))) | |
f9be4574 | 511 | (while buffers |
4a2fce7a RS |
512 | (let ((bufname (buffer-name (car buffers)))) |
513 | (or | |
514 | (null bufname) | |
0f4804e7 | 515 | (string-match preserve-regexp bufname) |
4a2fce7a | 516 | ;; Don't kill buffers made for internal purposes. |
3f32d6a3 | 517 | (and (not (equal bufname "")) (eq (aref bufname 0) ?\s)) |
4a2fce7a | 518 | (kill-buffer (car buffers)))) |
f9be4574 | 519 | (setq buffers (cdr buffers)))) |
b6105c76 | 520 | (delete-other-windows)) |
4a2fce7a | 521 | |
0b9bd504 | 522 | ;; ---------------------------------------------------------------------------- |
de9e2828 | 523 | (add-hook 'kill-emacs-hook 'desktop-kill) |
ec4c6f22 | 524 | |
6343ed61 | 525 | (defun desktop-kill () |
c5b31c4d | 526 | "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do. |
4a2fce7a RS |
527 | If the desktop should be saved and `desktop-dirname' |
528 | is nil, ask the user where to save the desktop." | |
529 | (when | |
530 | (and | |
c5b31c4d | 531 | desktop-save-mode |
73b0b745 | 532 | (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname)))) |
4a2fce7a | 533 | (or |
d12d9396 | 534 | (eq desktop-save t) |
4a2fce7a RS |
535 | (and exists (memq desktop-save '(ask-if-new if-exists))) |
536 | (and | |
537 | (or | |
538 | (memq desktop-save '(ask ask-if-new)) | |
539 | (and exists (eq desktop-save 'ask-if-exists))) | |
540 | (y-or-n-p "Save desktop? "))))) | |
541 | (unless desktop-dirname | |
542 | (setq desktop-dirname | |
73b0b745 JB |
543 | (file-name-as-directory |
544 | (expand-file-name | |
545 | (call-interactively | |
546 | (lambda (dir) (interactive "DDirectory for desktop file: ") dir)))))) | |
4a2fce7a RS |
547 | (condition-case err |
548 | (desktop-save desktop-dirname) | |
549 | (file-error | |
cc8b76bf | 550 | (unless (yes-or-no-p "Error while saving the desktop. Ignore? ") |
4a2fce7a RS |
551 | (signal (car err) (cdr err))))))) |
552 | ||
0b9bd504 | 553 | ;; ---------------------------------------------------------------------------- |
ed2f7fc8 RS |
554 | (defun desktop-list* (&rest args) |
555 | (if (null (cdr args)) | |
556 | (car args) | |
557 | (setq args (nreverse args)) | |
558 | (let ((value (cons (nth 1 args) (car args)))) | |
559 | (setq args (cdr (cdr args))) | |
560 | (while args | |
561 | (setq value (cons (car args) value)) | |
562 | (setq args (cdr args))) | |
563 | value))) | |
564 | ||
4a2fce7a | 565 | ;; ---------------------------------------------------------------------------- |
05f1c4ec | 566 | (defun desktop-internal-v2s (value) |
577ed2b2 RS |
567 | "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE. |
568 | TXT is a string that when read and evaluated yields value. | |
569 | QUOTE may be `may' (value may be quoted), | |
570 | `must' (values must be quoted), or nil (value may not be quoted)." | |
de9e2828 | 571 | (cond |
3f32d6a3 | 572 | ((or (numberp value) (null value) (eq t value) (keywordp value)) |
05f1c4ec JB |
573 | (cons 'may (prin1-to-string value))) |
574 | ((stringp value) | |
575 | (let ((copy (copy-sequence value))) | |
95bf6435 RS |
576 | (set-text-properties 0 (length copy) nil copy) |
577 | ;; Get rid of text properties because we cannot read them | |
578 | (cons 'may (prin1-to-string copy)))) | |
05f1c4ec JB |
579 | ((symbolp value) |
580 | (cons 'must (prin1-to-string value))) | |
581 | ((vectorp value) | |
de9e2828 RS |
582 | (let* ((special nil) |
583 | (pass1 (mapcar | |
584 | (lambda (el) | |
585 | (let ((res (desktop-internal-v2s el))) | |
586 | (if (null (car res)) | |
587 | (setq special t)) | |
588 | res)) | |
05f1c4ec | 589 | value))) |
de9e2828 RS |
590 | (if special |
591 | (cons nil (concat "(vector " | |
592 | (mapconcat (lambda (el) | |
593 | (if (eq (car el) 'must) | |
594 | (concat "'" (cdr el)) | |
595 | (cdr el))) | |
596 | pass1 | |
597 | " ") | |
598 | ")")) | |
599 | (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]"))))) | |
05f1c4ec JB |
600 | ((consp value) |
601 | (let ((p value) | |
b4929f75 | 602 | newlist |
ed2f7fc8 | 603 | use-list* |
b4929f75 RS |
604 | anynil) |
605 | (while (consp p) | |
606 | (let ((q.txt (desktop-internal-v2s (car p)))) | |
607 | (or anynil (setq anynil (null (car q.txt)))) | |
608 | (setq newlist (cons q.txt newlist))) | |
609 | (setq p (cdr p))) | |
610 | (if p | |
611 | (let ((last (desktop-internal-v2s p)) | |
612 | (el (car newlist))) | |
ed2f7fc8 RS |
613 | (or anynil (setq anynil (null (car last)))) |
614 | (or anynil | |
615 | (setq newlist (cons '(must . ".") newlist))) | |
616 | (setq use-list* t) | |
617 | (setq newlist (cons last newlist)))) | |
b4929f75 RS |
618 | (setq newlist (nreverse newlist)) |
619 | (if anynil | |
620 | (cons nil | |
ed2f7fc8 | 621 | (concat (if use-list* "(desktop-list* " "(list ") |
b4929f75 RS |
622 | (mapconcat (lambda (el) |
623 | (if (eq (car el) 'must) | |
624 | (concat "'" (cdr el)) | |
625 | (cdr el))) | |
626 | newlist | |
627 | " ") | |
628 | ")")) | |
629 | (cons 'must | |
630 | (concat "(" (mapconcat 'cdr newlist " ") ")"))))) | |
05f1c4ec | 631 | ((subrp value) |
de9e2828 | 632 | (cons nil (concat "(symbol-function '" |
05f1c4ec | 633 | (substring (prin1-to-string value) 7 -1) |
de9e2828 | 634 | ")"))) |
05f1c4ec JB |
635 | ((markerp value) |
636 | (let ((pos (prin1-to-string (marker-position value))) | |
637 | (buf (prin1-to-string (buffer-name (marker-buffer value))))) | |
de9e2828 RS |
638 | (cons nil (concat "(let ((mk (make-marker)))" |
639 | " (add-hook 'desktop-delay-hook" | |
640 | " (list 'lambda '() (list 'set-marker mk " | |
641 | pos " (get-buffer " buf ")))) mk)")))) | |
642 | (t ; save as text | |
fa379636 | 643 | (cons 'may "\"Unprintable entity\"")))) |
de9e2828 | 644 | |
4a2fce7a | 645 | ;; ---------------------------------------------------------------------------- |
05f1c4ec | 646 | (defun desktop-value-to-string (value) |
577ed2b2 RS |
647 | "Convert VALUE to a string that when read evaluates to the same value. |
648 | Not all types of values are supported." | |
de9e2828 RS |
649 | (let* ((print-escape-newlines t) |
650 | (float-output-format nil) | |
05f1c4ec | 651 | (quote.txt (desktop-internal-v2s value)) |
de9e2828 RS |
652 | (quote (car quote.txt)) |
653 | (txt (cdr quote.txt))) | |
654 | (if (eq quote 'must) | |
655 | (concat "'" txt) | |
656 | txt))) | |
4a2fce7a | 657 | |
ec4c6f22 | 658 | ;; ---------------------------------------------------------------------------- |
0e7c8611 RS |
659 | (defun desktop-outvar (varspec) |
660 | "Output a setq statement for variable VAR to the desktop file. | |
661 | The argument VARSPEC may be the variable name VAR (a symbol), | |
662 | or a cons cell of the form (VAR . MAX-SIZE), | |
663 | which means to truncate VAR's value to at most MAX-SIZE elements | |
664 | \(if the value is a list) before saving the value." | |
665 | (let (var size) | |
666 | (if (consp varspec) | |
667 | (setq var (car varspec) size (cdr varspec)) | |
668 | (setq var varspec)) | |
669 | (if (boundp var) | |
670 | (progn | |
671 | (if (and (integerp size) | |
672 | (> size 0) | |
673 | (listp (eval var))) | |
47640244 | 674 | (desktop-truncate (eval var) size)) |
0e7c8611 RS |
675 | (insert "(setq " |
676 | (symbol-name var) | |
677 | " " | |
678 | (desktop-value-to-string (symbol-value var)) | |
679 | ")\n"))))) | |
4a2fce7a | 680 | |
0b9bd504 | 681 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 | 682 | (defun desktop-save-buffer-p (filename bufname mode &rest dummy) |
ebb39555 | 683 | "Return t if buffer should have its state saved in the desktop file. |
0b9bd504 | 684 | FILENAME is the visited file name, BUFNAME is the buffer name, and |
6343ed61 | 685 | MODE is the major mode." |
fa379636 | 686 | (let ((case-fold-search nil)) |
80f9f3db | 687 | (and (not (string-match desktop-buffers-not-to-save bufname)) |
ebb39555 LH |
688 | (not (memq mode desktop-modes-not-to-save)) |
689 | (or (and filename | |
690 | (not (string-match desktop-files-not-to-save filename))) | |
691 | (and (eq mode 'dired-mode) | |
692 | (with-current-buffer bufname | |
693 | (not (string-match desktop-files-not-to-save | |
694 | default-directory)))) | |
695 | (and (null filename) | |
696 | (with-current-buffer bufname desktop-save-buffer)))))) | |
4a2fce7a | 697 | |
0b9bd504 | 698 | ;; ---------------------------------------------------------------------------- |
4a2fce7a RS |
699 | (defun desktop-file-name (filename dirname) |
700 | "Convert FILENAME to format specified in `desktop-file-name-format'. | |
701 | DIRNAME must be the directory in which the desktop file will be saved." | |
702 | (cond | |
703 | ((not filename) nil) | |
704 | ((eq desktop-file-name-format 'tilde) | |
705 | (let ((relative-name (file-relative-name (expand-file-name filename) "~"))) | |
706 | (cond | |
707 | ((file-name-absolute-p relative-name) relative-name) | |
708 | ((string= "./" relative-name) "~/") | |
709 | ((string= "." relative-name) "~") | |
710 | (t (concat "~/" relative-name))))) | |
711 | ((eq desktop-file-name-format 'local) (file-relative-name filename dirname)) | |
712 | (t (expand-file-name filename)))) | |
e5714620 | 713 | |
4a2fce7a | 714 | ;; ---------------------------------------------------------------------------- |
6343ed61 | 715 | (defun desktop-save (dirname) |
c5b31c4d LH |
716 | "Save the desktop in a desktop file. |
717 | Parameter DIRNAME specifies where to save the desktop file. | |
718 | See also `desktop-base-file-name'." | |
6343ed61 | 719 | (interactive "DDirectory to save desktop file in: ") |
fa379636 | 720 | (run-hooks 'desktop-save-hook) |
7bcbf3c2 | 721 | (setq dirname (file-name-as-directory (expand-file-name dirname))) |
6343ed61 | 722 | (save-excursion |
73b0b745 | 723 | (let ((filename (expand-file-name desktop-base-file-name dirname)) |
7bcbf3c2 JB |
724 | (info |
725 | (mapcar | |
340db502 | 726 | #'(lambda (b) |
7bcbf3c2 JB |
727 | (set-buffer b) |
728 | (list | |
729 | (desktop-file-name (buffer-file-name) dirname) | |
730 | (buffer-name) | |
731 | major-mode | |
732 | ;; minor modes | |
733 | (let (ret) | |
7bfa55b3 LH |
734 | (mapc |
735 | #'(lambda (minor-mode) | |
7bcbf3c2 | 736 | (and |
7bfa55b3 LH |
737 | (boundp minor-mode) |
738 | (symbol-value minor-mode) | |
0f4804e7 LH |
739 | (let* ((special (assq minor-mode desktop-minor-mode-table)) |
740 | (value (cond (special (cadr special)) | |
741 | ((functionp minor-mode) minor-mode)))) | |
742 | (when value (add-to-list 'ret value))))) | |
7bcbf3c2 JB |
743 | (mapcar #'car minor-mode-alist)) |
744 | ret) | |
745 | (point) | |
746 | (list (mark t) mark-active) | |
747 | buffer-read-only | |
e5780ae1 | 748 | ;; Auxiliary information |
ebb39555 LH |
749 | (when (functionp desktop-save-buffer) |
750 | (funcall desktop-save-buffer dirname)) | |
7bcbf3c2 JB |
751 | (let ((locals desktop-locals-to-save) |
752 | (loclist (buffer-local-variables)) | |
753 | (ll)) | |
754 | (while locals | |
755 | (let ((here (assq (car locals) loclist))) | |
756 | (if here | |
757 | (setq ll (cons here ll)) | |
758 | (when (member (car locals) loclist) | |
759 | (setq ll (cons (car locals) ll))))) | |
760 | (setq locals (cdr locals))) | |
340db502 | 761 | ll))) |
7bcbf3c2 | 762 | (buffer-list))) |
150f39ee | 763 | (eager desktop-restore-eager) |
7bcbf3c2 | 764 | (buf (get-buffer-create "*desktop*"))) |
6343ed61 RS |
765 | (set-buffer buf) |
766 | (erase-buffer) | |
fa379636 | 767 | |
4a2fce7a | 768 | (insert |
aac42a1b | 769 | ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n" |
4a2fce7a RS |
770 | desktop-header |
771 | ";; Created " (current-time-string) "\n" | |
772 | ";; Desktop file format version " desktop-file-version "\n" | |
773 | ";; Emacs version " emacs-version "\n\n" | |
774 | ";; Global section:\n") | |
63cd5734 | 775 | (mapc (function desktop-outvar) desktop-globals-to-save) |
6343ed61 | 776 | (if (memq 'kill-ring desktop-globals-to-save) |
4a2fce7a RS |
777 | (insert |
778 | "(setq kill-ring-yank-pointer (nthcdr " | |
779 | (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer))) | |
780 | " kill-ring))\n")) | |
6343ed61 | 781 | |
4a2fce7a | 782 | (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n") |
63cd5734 | 783 | (mapc #'(lambda (l) |
150f39ee LH |
784 | (when (apply 'desktop-save-buffer-p l) |
785 | (insert "(" | |
786 | (if (or (not (integerp eager)) | |
787 | (unless (zerop eager) | |
788 | (setq eager (1- eager)) | |
789 | t)) | |
790 | "desktop-create-buffer" | |
791 | "desktop-append-buffer-args") | |
792 | " " | |
793 | desktop-file-version) | |
794 | (mapc #'(lambda (e) | |
795 | (insert "\n " (desktop-value-to-string e))) | |
796 | l) | |
797 | (insert ")\n\n"))) | |
798 | info) | |
6343ed61 | 799 | (setq default-directory dirname) |
7642acca | 800 | (let ((coding-system-for-write 'emacs-mule)) |
4a2fce7a | 801 | (write-region (point-min) (point-max) filename nil 'nomessage)))) |
6343ed61 | 802 | (setq desktop-dirname dirname)) |
4a2fce7a | 803 | |
0b9bd504 | 804 | ;; ---------------------------------------------------------------------------- |
6343ed61 | 805 | (defun desktop-remove () |
c5b31c4d LH |
806 | "Delete desktop file in `desktop-dirname'. |
807 | This function also sets `desktop-dirname' to nil." | |
6343ed61 | 808 | (interactive) |
c5b31c4d LH |
809 | (when desktop-dirname |
810 | (let ((filename (expand-file-name desktop-base-file-name desktop-dirname))) | |
811 | (setq desktop-dirname nil) | |
812 | (when (file-exists-p filename) | |
73b0b745 | 813 | (delete-file filename))))) |
c5b31c4d | 814 | |
150f39ee LH |
815 | (defvar desktop-buffer-args-list nil |
816 | "List of args for `desktop-create-buffer'.") | |
817 | ||
818 | (defvar desktop-lazy-timer nil) | |
819 | ||
0b9bd504 | 820 | ;; ---------------------------------------------------------------------------- |
478653c9 | 821 | ;;;###autoload |
c5b31c4d LH |
822 | (defun desktop-read (&optional dirname) |
823 | "Read and process the desktop file in directory DIRNAME. | |
824 | Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in | |
825 | directories listed in `desktop-path'. If a desktop file is found, it | |
cc8b76bf | 826 | is processed and `desktop-after-read-hook' is run. If no desktop file |
c5b31c4d LH |
827 | is found, clear the desktop and run `desktop-no-desktop-file-hook'. |
828 | This function is a no-op when Emacs is running in batch mode. | |
829 | It returns t if a desktop file was loaded, nil otherwise." | |
6343ed61 | 830 | (interactive) |
4a2fce7a | 831 | (unless noninteractive |
c5b31c4d LH |
832 | (setq desktop-dirname |
833 | (file-name-as-directory | |
834 | (expand-file-name | |
835 | (or | |
836 | ;; If DIRNAME is specified, use it. | |
837 | (and (< 0 (length dirname)) dirname) | |
838 | ;; Otherwise search desktop file in desktop-path. | |
839 | (let ((dirs desktop-path)) | |
840 | (while | |
841 | (and | |
842 | dirs | |
843 | (not | |
844 | (file-exists-p (expand-file-name desktop-base-file-name (car dirs))))) | |
845 | (setq dirs (cdr dirs))) | |
846 | (and dirs (car dirs))) | |
847 | ;; If not found and `desktop-path' is non-nil, use its first element. | |
848 | (and desktop-path (car desktop-path)) | |
849 | ;; Default: Home directory. | |
850 | "~")))) | |
851 | (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname)) | |
852 | ;; Desktop file found, process it. | |
e5780ae1 LH |
853 | (let ((desktop-first-buffer nil) |
854 | (desktop-buffer-ok-count 0) | |
855 | (desktop-buffer-fail-count 0)) | |
150f39ee | 856 | (setq desktop-lazy-timer nil) |
c5b31c4d LH |
857 | ;; Evaluate desktop buffer. |
858 | (load (expand-file-name desktop-base-file-name desktop-dirname) t t t) | |
859 | ;; `desktop-create-buffer' puts buffers at end of the buffer list. | |
860 | ;; We want buffers existing prior to evaluating the desktop (and not reused) | |
861 | ;; to be placed at the end of the buffer list, so we move them here. | |
48aa4dfc LH |
862 | (mapc 'bury-buffer |
863 | (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list)))))) | |
c5b31c4d LH |
864 | (switch-to-buffer (car (buffer-list))) |
865 | (run-hooks 'desktop-delay-hook) | |
866 | (setq desktop-delay-hook nil) | |
867 | (run-hooks 'desktop-after-read-hook) | |
150f39ee | 868 | (message "Desktop: %d buffer%s restored%s%s." |
e5780ae1 LH |
869 | desktop-buffer-ok-count |
870 | (if (= 1 desktop-buffer-ok-count) "" "s") | |
871 | (if (< 0 desktop-buffer-fail-count) | |
872 | (format ", %d failed to restore" desktop-buffer-fail-count) | |
150f39ee LH |
873 | "") |
874 | (if desktop-buffer-args-list | |
875 | (format ", %d to restore lazily" | |
876 | (length desktop-buffer-args-list)) | |
e5780ae1 | 877 | "")) |
c5b31c4d LH |
878 | t) |
879 | ;; No desktop file found. | |
880 | (desktop-clear) | |
881 | (let ((default-directory desktop-dirname)) | |
882 | (run-hooks 'desktop-no-desktop-file-hook)) | |
883 | (message "No desktop file.") | |
884 | nil))) | |
4a2fce7a | 885 | |
0b9bd504 | 886 | ;; ---------------------------------------------------------------------------- |
c5b31c4d | 887 | ;; Maintained for backward compatibility |
478653c9 | 888 | ;;;###autoload |
6343ed61 | 889 | (defun desktop-load-default () |
577ed2b2 | 890 | "Load the `default' start-up library manually. |
c5b31c4d | 891 | Also inhibit further loading of it." |
b89c5a72 JB |
892 | (unless inhibit-default-init ; safety check |
893 | (load "default" t t) | |
894 | (setq inhibit-default-init t))) | |
895 | (make-obsolete 'desktop-load-default | |
896 | 'desktop-save-mode "22.1") | |
4a2fce7a RS |
897 | |
898 | ;; ---------------------------------------------------------------------------- | |
899 | ;;;###autoload | |
c5b31c4d LH |
900 | (defun desktop-change-dir (dirname) |
901 | "Change to desktop saved in DIRNAME. | |
902 | Kill the desktop as specified by variables `desktop-save-mode' and | |
903 | `desktop-save', then clear the desktop and load the desktop file in | |
904 | directory DIRNAME." | |
905 | (interactive "DChange to directory: ") | |
906 | (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname))) | |
4a2fce7a RS |
907 | (desktop-kill) |
908 | (desktop-clear) | |
c5b31c4d | 909 | (desktop-read dirname)) |
bf247b6e | 910 | |
c5b31c4d | 911 | ;; ---------------------------------------------------------------------------- |
4a2fce7a | 912 | ;;;###autoload |
c5b31c4d LH |
913 | (defun desktop-save-in-desktop-dir () |
914 | "Save the desktop in directory `desktop-dirname'." | |
4a2fce7a RS |
915 | (interactive) |
916 | (if desktop-dirname | |
b89c5a72 | 917 | (desktop-save desktop-dirname) |
4a2fce7a RS |
918 | (call-interactively 'desktop-save)) |
919 | (message "Desktop saved in %s" desktop-dirname)) | |
920 | ||
921 | ;; ---------------------------------------------------------------------------- | |
922 | ;;;###autoload | |
923 | (defun desktop-revert () | |
924 | "Revert to the last loaded desktop." | |
925 | (interactive) | |
c5b31c4d LH |
926 | (unless desktop-dirname |
927 | (error "Unknown desktop directory")) | |
928 | (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname)) | |
929 | (error "No desktop file found")) | |
930 | (desktop-clear) | |
931 | (desktop-read desktop-dirname)) | |
4a2fce7a | 932 | |
0b9bd504 | 933 | ;; ---------------------------------------------------------------------------- |
e5780ae1 LH |
934 | (defun desktop-restore-file-buffer (desktop-buffer-file-name |
935 | desktop-buffer-name | |
936 | desktop-buffer-misc) | |
937 | "Restore a file buffer." | |
938 | (eval-when-compile ; Just to silence the byte compiler | |
939 | (defvar desktop-buffer-major-mode) | |
940 | (defvar desktop-buffer-locals)) | |
569c754e RS |
941 | (if desktop-buffer-file-name |
942 | (if (or (file-exists-p desktop-buffer-file-name) | |
e5780ae1 LH |
943 | (let ((msg (format "Desktop: File \"%s\" no longer exists." |
944 | desktop-buffer-file-name))) | |
945 | (if desktop-missing-file-warning | |
946 | (y-or-n-p (concat msg " Re-create? ")) | |
8a26c165 | 947 | (message "%s" msg) |
e5780ae1 | 948 | nil))) |
bd382a27 LH |
949 | (let* ((auto-insert nil) ; Disable auto insertion |
950 | (coding-system-for-read | |
951 | (or coding-system-for-read | |
952 | (cdr (assq 'buffer-file-coding-system | |
953 | desktop-buffer-locals)))) | |
954 | (buf (find-file-noselect desktop-buffer-file-name))) | |
80f9f3db SM |
955 | (condition-case nil |
956 | (switch-to-buffer buf) | |
0a8c8225 | 957 | (error (pop-to-buffer buf))) |
84b538ec JB |
958 | (and (not (eq major-mode desktop-buffer-major-mode)) |
959 | (functionp desktop-buffer-major-mode) | |
960 | (funcall desktop-buffer-major-mode)) | |
0a8c8225 | 961 | buf) |
e5780ae1 | 962 | nil))) |
4a2fce7a | 963 | |
0f4804e7 LH |
964 | (defun desktop-load-file (function) |
965 | "Load the file where auto loaded FUNCTION is defined." | |
966 | (let ((fcell (symbol-function function))) | |
967 | (when (and (listp fcell) | |
968 | (eq 'autoload (car fcell))) | |
969 | (load (cadr fcell))))) | |
970 | ||
0b9bd504 | 971 | ;; ---------------------------------------------------------------------------- |
eb982898 JL |
972 | ;; Create a buffer, load its file, set its mode, ...; |
973 | ;; called from Desktop file only. | |
1d500ca6 | 974 | |
0f4804e7 LH |
975 | ;; Just to silence the byte compiler. |
976 | (eval-when-compile | |
977 | (defvar desktop-first-buffer)) ; Dynamically bound in `desktop-read' | |
4a2fce7a | 978 | |
340db502 LH |
979 | (defun desktop-create-buffer |
980 | (desktop-file-version | |
981 | desktop-buffer-file-name | |
982 | desktop-buffer-name | |
983 | desktop-buffer-major-mode | |
984 | desktop-buffer-minor-modes | |
985 | desktop-buffer-point | |
986 | desktop-buffer-mark | |
987 | desktop-buffer-read-only | |
988 | desktop-buffer-misc | |
989 | &optional | |
990 | desktop-buffer-locals) | |
e5780ae1 LH |
991 | ;; Just to silence the byte compiler. Bound locally in `desktop-read'. |
992 | (eval-when-compile | |
993 | (defvar desktop-buffer-ok-count) | |
994 | (defvar desktop-buffer-fail-count)) | |
4a2fce7a RS |
995 | ;; To make desktop files with relative file names possible, we cannot |
996 | ;; allow `default-directory' to change. Therefore we save current buffer. | |
997 | (save-current-buffer | |
0f4804e7 LH |
998 | ;; Give major mode module a chance to add a handler. |
999 | (desktop-load-file desktop-buffer-major-mode) | |
340db502 LH |
1000 | (let ((buffer-list (buffer-list)) |
1001 | (result | |
1002 | (condition-case err | |
1003 | (funcall (or (cdr (assq desktop-buffer-major-mode | |
1004 | desktop-buffer-mode-handlers)) | |
1005 | 'desktop-restore-file-buffer) | |
1006 | desktop-buffer-file-name | |
1007 | desktop-buffer-name | |
1008 | desktop-buffer-misc) | |
1009 | (error | |
1010 | (message "Desktop: Can't load buffer %s: %s" | |
1011 | desktop-buffer-name | |
1012 | (error-message-string err)) | |
1013 | (when desktop-missing-file-warning (sit-for 1)) | |
1014 | nil)))) | |
e5780ae1 LH |
1015 | (if (bufferp result) |
1016 | (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count)) | |
1017 | (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count)) | |
1018 | (setq result nil)) | |
7bcbf3c2 JB |
1019 | ;; Restore buffer list order with new buffer at end. Don't change |
1020 | ;; the order for old desktop files (old desktop module behaviour). | |
4a2fce7a | 1021 | (unless (< desktop-file-version 206) |
48aa4dfc | 1022 | (mapc 'bury-buffer buffer-list) |
7bcbf3c2 | 1023 | (when result (bury-buffer result))) |
4a2fce7a | 1024 | (when result |
7bcbf3c2 JB |
1025 | (unless (or desktop-first-buffer (< desktop-file-version 206)) |
1026 | (setq desktop-first-buffer result)) | |
4a2fce7a RS |
1027 | (set-buffer result) |
1028 | (unless (equal (buffer-name) desktop-buffer-name) | |
1029 | (rename-buffer desktop-buffer-name)) | |
1030 | ;; minor modes | |
340db502 LH |
1031 | (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible |
1032 | (auto-fill-mode 1)) | |
1033 | ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible | |
1034 | (auto-fill-mode 0)) | |
1035 | (t | |
63cd5734 | 1036 | (mapc #'(lambda (minor-mode) |
0f4804e7 LH |
1037 | ;; Give minor mode module a chance to add a handler. |
1038 | (desktop-load-file minor-mode) | |
1039 | (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers)))) | |
1040 | (if handler | |
1041 | (funcall handler desktop-buffer-locals) | |
1042 | (when (functionp minor-mode) | |
1043 | (funcall minor-mode 1))))) | |
63cd5734 | 1044 | desktop-buffer-minor-modes))) |
0f4804e7 | 1045 | ;; Even though point and mark are non-nil when written by `desktop-save', |
73b0b745 | 1046 | ;; they may be modified by handlers wanting to set point or mark themselves. |
e5780ae1 LH |
1047 | (when desktop-buffer-point |
1048 | (goto-char | |
1049 | (condition-case err | |
1050 | ;; Evaluate point. Thus point can be something like '(search-forward ... | |
1051 | (eval desktop-buffer-point) | |
1052 | (error (message "%s" (error-message-string err)) 1)))) | |
4a2fce7a RS |
1053 | (when desktop-buffer-mark |
1054 | (if (consp desktop-buffer-mark) | |
1055 | (progn | |
1056 | (set-mark (car desktop-buffer-mark)) | |
1057 | (setq mark-active (car (cdr desktop-buffer-mark)))) | |
1058 | (set-mark desktop-buffer-mark))) | |
1059 | ;; Never override file system if the file really is read-only marked. | |
1060 | (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only)) | |
1061 | (while desktop-buffer-locals | |
1062 | (let ((this (car desktop-buffer-locals))) | |
1063 | (if (consp this) | |
1064 | ;; an entry of this form `(symbol . value)' | |
1065 | (progn | |
1066 | (make-local-variable (car this)) | |
1067 | (set (car this) (cdr this))) | |
1068 | ;; an entry of the form `symbol' | |
1069 | (make-local-variable this) | |
1070 | (makunbound this))) | |
1071 | (setq desktop-buffer-locals (cdr desktop-buffer-locals))))))) | |
ec4c6f22 | 1072 | |
4a2fce7a | 1073 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 | 1074 | ;; Backward compatibility -- update parameters to 205 standards. |
569c754e RS |
1075 | (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name |
1076 | desktop-buffer-major-mode | |
2699e23e | 1077 | mim pt mk ro tl fc cfs cr desktop-buffer-misc) |
569c754e | 1078 | (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name |
2699e23e RS |
1079 | desktop-buffer-major-mode (cdr mim) pt mk ro |
1080 | desktop-buffer-misc | |
ec4c6f22 RS |
1081 | (list (cons 'truncate-lines tl) |
1082 | (cons 'fill-column fc) | |
1083 | (cons 'case-fold-search cfs) | |
1084 | (cons 'case-replace cr) | |
1085 | (cons 'overwrite-mode (car mim))))) | |
84b538ec | 1086 | |
150f39ee | 1087 | (defun desktop-append-buffer-args (&rest args) |
cc8b76bf | 1088 | "Append ARGS at end of `desktop-buffer-args-list'. |
150f39ee LH |
1089 | ARGS must be an argument list for `desktop-create-buffer'." |
1090 | (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args))) | |
1091 | (unless desktop-lazy-timer | |
1092 | (setq desktop-lazy-timer | |
1093 | (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers)))) | |
1094 | ||
1095 | (defun desktop-lazy-create-buffer () | |
1096 | "Pop args from `desktop-buffer-args-list', create buffer and bury it." | |
1097 | (when desktop-buffer-args-list | |
1098 | (let* ((remaining (length desktop-buffer-args-list)) | |
1099 | (args (pop desktop-buffer-args-list)) | |
1100 | (buffer-name (nth 2 args)) | |
1101 | (msg (format "Desktop lazily opening %s (%s remaining)..." | |
1102 | buffer-name remaining))) | |
1103 | (when desktop-lazy-verbose | |
8a26c165 | 1104 | (message "%s" msg)) |
150f39ee LH |
1105 | (let ((desktop-first-buffer nil) |
1106 | (desktop-buffer-ok-count 0) | |
1107 | (desktop-buffer-fail-count 0)) | |
1108 | (apply 'desktop-create-buffer args) | |
1109 | (run-hooks 'desktop-delay-hook) | |
1110 | (setq desktop-delay-hook nil) | |
1111 | (bury-buffer (get-buffer buffer-name)) | |
1112 | (when desktop-lazy-verbose | |
1113 | (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed"))))))) | |
1114 | ||
1115 | (defun desktop-idle-create-buffers () | |
1116 | "Create buffers until the user does something, then stop. | |
1117 | If there are no buffers left to create, kill the timer." | |
1118 | (let ((repeat 1)) | |
1119 | (while (and repeat desktop-buffer-args-list) | |
1120 | (save-window-excursion | |
1121 | (desktop-lazy-create-buffer)) | |
1122 | (setq repeat (sit-for 0.2)) | |
1123 | (unless desktop-buffer-args-list | |
1124 | (cancel-timer desktop-lazy-timer) | |
1125 | (setq desktop-lazy-timer nil) | |
1126 | (message "Lazy desktop load complete") | |
1127 | (sit-for 3) | |
1128 | (message ""))))) | |
1129 | ||
1130 | (defun desktop-lazy-complete () | |
1131 | "Run the desktop load to completion." | |
1132 | (interactive) | |
1133 | (let ((desktop-lazy-verbose t)) | |
1134 | (while desktop-buffer-args-list | |
1135 | (save-window-excursion | |
1136 | (desktop-lazy-create-buffer))) | |
1137 | (message "Lazy desktop load complete"))) | |
1138 | ||
1139 | (defun desktop-lazy-abort () | |
1140 | "Abort lazy loading of the desktop." | |
1141 | (interactive) | |
1142 | (when desktop-lazy-timer | |
1143 | (cancel-timer desktop-lazy-timer) | |
1144 | (setq desktop-lazy-timer nil)) | |
1145 | (when desktop-buffer-args-list | |
1146 | (setq desktop-buffer-args-list nil) | |
1147 | (when (interactive-p) | |
1148 | (message "Lazy desktop load aborted")))) | |
1149 | ||
0b9bd504 | 1150 | ;; ---------------------------------------------------------------------------- |
c5b31c4d | 1151 | ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the |
4a2fce7a RS |
1152 | ;; command line, we do the rest of what it takes to use desktop, but do it |
1153 | ;; after finishing loading the init file. | |
1154 | ;; We cannot use `command-switch-alist' to process "--no-desktop" because these | |
1155 | ;; functions are processed after `after-init-hook'. | |
1156 | (add-hook | |
1157 | 'after-init-hook | |
1158 | '(lambda () | |
1159 | (let ((key "--no-desktop")) | |
4b217d46 LH |
1160 | (when (member key command-line-args) |
1161 | (setq command-line-args (delete key command-line-args)) | |
1162 | (setq desktop-save-mode nil))) | |
1163 | (when desktop-save-mode (desktop-read)))) | |
813dbb2d | 1164 | |
ab1d55ea | 1165 | (provide 'desktop) |
6343ed61 | 1166 | |
ab5796a9 | 1167 | ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9 |
80f9f3db | 1168 | ;;; desktop.el ends here |