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