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