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