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