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