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