(file-remote-p): Docstring fix.
[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,
409cc4a3 4;; 2004, 2005, 2006, 2007, 2008 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
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
b4aa6026 14;; the Free Software Foundation; either version 3, or (at your option)
6343ed61
RS
15;; any later version.
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
b578f267 23;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
6343ed61
RS
26
27;;; Commentary:
28
0b9bd504
RS
29;; Save the Desktop, i.e.,
30;; - some global variables
31;; - the list of buffers with associated files. For each buffer also
32;; - the major mode
33;; - the default directory
34;; - the point
35;; - the mark & mark-active
36;; - buffer-read-only
ec4c6f22 37;; - some local variables
6343ed61 38
c5b31c4d
LH
39;; To use this, use customize to turn on desktop-save-mode or add the
40;; following line somewhere in your .emacs file:
0b9bd504 41;;
c5b31c4d 42;; (desktop-save-mode 1)
0b9bd504 43;;
c5b31c4d
LH
44;; For further usage information, look at the section
45;; "Saving Emacs Sessions" in the GNU Emacs Manual.
46
47;; When the desktop module is loaded, the function `desktop-kill' is
341c2f07 48;; added to the `kill-emacs-hook'. This function is responsible for
c5b31c4d 49;; saving the desktop when Emacs is killed. Furthermore an anonymous
341c2f07 50;; function is added to the `after-init-hook'. This function is
c5b31c4d 51;; responsible for loading the desktop when Emacs is started.
ec4c6f22 52
0f4804e7
LH
53;; Special handling.
54;; -----------------
55;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
56;; are supplied to handle special major and minor modes respectively.
57;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
341c2f07 58;; to restore a desktop buffer. Elements must have the form
be617bbf 59;;
0f4804e7 60;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
be617bbf 61;;
0f4804e7 62;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
341c2f07 63;; evaluates the desktop file. Buffers with a major mode not specified here,
0f4804e7
LH
64;; are restored by the default handler `desktop-restore-file-buffer'.
65;; `desktop-minor-mode-handlers' is an alist of functions to restore
66;; non-standard minor modes. Elements must have the form
be617bbf 67;;
0f4804e7 68;; (MINOR-MODE . RESTORE-FUNCTION).
be617bbf 69;;
0f4804e7
LH
70;; Functions are called by `desktop-create-buffer' to restore minor modes.
71;; Minor modes not specified here, are restored by the standard minor mode
72;; function. If you write a module that defines a major or minor mode that
73;; needs a special handler, then place code like
74
75;; (defun foo-restore-desktop-buffer
76;; ...
77;; (add-to-list 'desktop-buffer-mode-handlers
78;; '(foo-mode . foo-restore-desktop-buffer))
79
80;; or
81
82;; (defun bar-desktop-restore
83;; ...
84;; (add-to-list 'desktop-minor-mode-handlers
85;; '(bar-mode . bar-desktop-restore))
86
87;; in the module itself, and make shure that the mode function is
341c2f07 88;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
0f4804e7
LH
89;; `desktop-minor-mode-handlers' for more info.
90
91;; Minor modes.
92;; ------------
93;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
94;; manual) are handled in the following way:
95;; When `desktop-save' saves the state of a buffer to the desktop file, it
96;; saves as `desktop-minor-modes' the list of names of those variables in
97;; `minor-mode-alist' that have a non-nil value.
98;; When `desktop-create' restores the buffer, each of the symbols in
99;; `desktop-minor-modes' is called as function with parameter 1.
100;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
101;; are used to handle non-conventional minor modes. `desktop-save' uses
102;; `desktop-minor-mode-table' to map minor mode variables to minor mode
341c2f07 103;; functions before writing `desktop-minor-modes'. If a minor mode has a
0f4804e7
LH
104;; variable name that is different form its function name, an entry
105
106;; (NAME RESTORE-FUNCTION)
107
108;; should be added to `desktop-minor-mode-table'. If a minor mode should not
109;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
110;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
111;; function different from the usual minor mode function.
112;; ---------------------------------------------------------------------------
47640244 113
ec4c6f22
RS
114;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
115;; in your home directory is used for that. Saving global default values
116;; for buffers is an example of misuse.
117
0b9bd504
RS
118;; PLEASE NOTE: The kill ring can be saved as specified by the variable
119;; `desktop-globals-to-save' (by default it isn't). This may result in saving
120;; things you did not mean to keep. Use M-x desktop-clear RET.
ec4c6f22 121
fa379636
KH
122;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
123;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
124;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
125;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
126;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
f4c73d07 127;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
253406b9 128;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
0b9bd504
RS
129;; ---------------------------------------------------------------------------
130;; TODO:
131;;
132;; Save window configuration.
133;; Recognize more minor modes.
134;; Save mark rings.
6343ed61
RS
135
136;;; Code:
137
93c5a976
JB
138(defvar uniquify-managed)
139
4a2fce7a 140(defvar desktop-file-version "206"
05f1c4ec 141 "Version number of desktop file format.
4a2fce7a
RS
142Written into the desktop file and used at desktop read to provide
143backward compatibility.")
144
ec4c6f22 145;; ----------------------------------------------------------------------------
0b9bd504
RS
146;; USER OPTIONS -- settings you might want to play with.
147;; ----------------------------------------------------------------------------
bbf5eb28
RS
148
149(defgroup desktop nil
150 "Save status of Emacs when you exit."
151 :group 'frames)
152
c5b31c4d
LH
153;;;###autoload
154(define-minor-mode desktop-save-mode
155 "Toggle desktop saving mode.
156With numeric ARG, turn desktop saving on if ARG is positive, off
11425834
LH
157otherwise. If desktop saving is turned on, the state of Emacs is
158saved from one session to another. See variable `desktop-save'
159and function `desktop-read' for details."
c5b31c4d
LH
160 :global t
161 :group 'desktop)
162
163;; Maintained for backward compatibility
b89c5a72
JB
164(define-obsolete-variable-alias 'desktop-enable
165 'desktop-save-mode "22.1")
813dbb2d 166
e88110db
JB
167(defun desktop-save-mode-off ()
168 "Disable `desktop-save-mode'. Provided for use in hooks."
169 (desktop-save-mode 0))
170
4a2fce7a 171(defcustom desktop-save 'ask-if-new
c5b31c4d
LH
172 "*Specifies whether the desktop should be saved when it is killed.
173A desktop is killed when the user changes desktop or quits Emacs.
174Possible values are:
4a2fce7a
RS
175 t -- always save.
176 ask -- always ask.
177 ask-if-new -- ask if no desktop file exists, otherwise just save.
178 ask-if-exists -- ask if desktop file exists, otherwise don't save.
179 if-exists -- save if desktop file exists, otherwise don't save.
180 nil -- never save.
c5b31c4d 181The desktop is never saved when `desktop-save-mode' is nil.
cc8b76bf 182The variables `desktop-dirname' and `desktop-base-file-name'
c5b31c4d 183determine where the desktop is saved."
11425834
LH
184 :type
185 '(choice
4a2fce7a
RS
186 (const :tag "Always save" t)
187 (const :tag "Always ask" ask)
188 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
189 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
190 (const :tag "Save if desktop file exists, else don't" if-exists)
191 (const :tag "Never save" nil))
af61551b 192 :group 'desktop
bf247b6e 193 :version "22.1")
4a2fce7a 194
1f7efe1b
JB
195(defcustom desktop-load-locked-desktop 'ask
196 "Specifies whether the desktop should be loaded if locked.
197Possible values are:
198 t -- load anyway.
199 nil -- don't load.
200 ask -- ask the user.
201If the value is nil, or `ask' and the user chooses not to load the desktop,
202the normal hook `desktop-not-loaded-hook' is run."
203 :type
204 '(choice
205 (const :tag "Load anyway" t)
206 (const :tag "Don't load" nil)
207 (const :tag "Ask the user" ask))
208 :group 'desktop
8f3f313d 209 :version "22.2")
1f7efe1b 210
4a2fce7a 211(defcustom desktop-base-file-name
fc715501 212 (convert-standard-filename ".emacs.desktop")
c5b31c4d 213 "Name of file for Emacs desktop, excluding the directory part."
813dbb2d
RS
214 :type 'file
215 :group 'desktop)
b89c5a72
JB
216(define-obsolete-variable-alias 'desktop-basefilename
217 'desktop-base-file-name "22.1")
6343ed61 218
e88110db
JB
219(defcustom desktop-base-lock-name
220 (convert-standard-filename ".emacs.desktop.lock")
221 "Name of lock file for Emacs desktop, excluding the directory part."
222 :type 'file
223 :group 'desktop
8f3f313d 224 :version "22.2")
e88110db 225
4a2fce7a
RS
226(defcustom desktop-path '("." "~")
227 "List of directories to search for the desktop file.
228The base name of the file is specified in `desktop-base-file-name'."
229 :type '(repeat directory)
af61551b 230 :group 'desktop
bf247b6e 231 :version "22.1")
4a2fce7a 232
bbf5eb28 233(defcustom desktop-missing-file-warning nil
6c27fdb9 234 "If non-nil, offer to recreate the buffer of a deleted file.
ebb39555
LH
235Also pause for a moment to display message about errors signaled in
236`desktop-buffer-mode-handlers'.
237
238If nil, just print error messages in the message buffer."
bbf5eb28 239 :type 'boolean
af61551b 240 :group 'desktop
bf247b6e 241 :version "22.1")
6343ed61 242
4a2fce7a 243(defcustom desktop-no-desktop-file-hook nil
c5b31c4d 244 "Normal hook run when `desktop-read' can't find a desktop file.
11425834 245Run in the directory in which the desktop file was sought.
8649a87b 246May be used to show a dired buffer."
4a2fce7a 247 :type 'hook
af61551b 248 :group 'desktop
bf247b6e 249 :version "22.1")
4a2fce7a 250
e88110db
JB
251(defcustom desktop-not-loaded-hook nil
252 "Normal hook run when the user declines to re-use a desktop file.
253Run in the directory in which the desktop file was found.
254May be used to deal with accidental multiple Emacs jobs."
255 :type 'hook
256 :group 'desktop
257 :options '(desktop-save-mode-off save-buffers-kill-emacs)
8f3f313d 258 :version "22.2")
e88110db 259
4a2fce7a 260(defcustom desktop-after-read-hook nil
05f1c4ec 261 "Normal hook run after a successful `desktop-read'.
8649a87b 262May be used to show a buffer list."
4a2fce7a 263 :type 'hook
af61551b 264 :group 'desktop
11425834 265 :options '(list-buffers)
bf247b6e 266 :version "22.1")
4a2fce7a
RS
267
268(defcustom desktop-save-hook nil
c5b31c4d 269 "Normal hook run before the desktop is saved in a desktop file.
11425834
LH
270Run with the desktop buffer current with only the header present.
271May be used to add to the desktop code or to truncate history lists,
272for example."
4a2fce7a
RS
273 :type 'hook
274 :group 'desktop)
275
340db502
LH
276(defcustom desktop-globals-to-save
277 '(desktop-missing-file-warning
278 tags-file-name
279 tags-table-list
280 search-ring
281 regexp-search-ring
282 register-alist)
c5b31c4d
LH
283 "List of global variables saved by `desktop-save'.
284An element may be variable name (a symbol) or a cons cell of the form
285\(VAR . MAX-SIZE), which means to truncate VAR's value to at most
286MAX-SIZE elements (if the value is a list) before saving the value.
4a2fce7a
RS
287Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
288 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
289 :group 'desktop)
290
340db502
LH
291(defcustom desktop-globals-to-clear
292 '(kill-ring
293 kill-ring-yank-pointer
294 search-ring
295 search-ring-yank-pointer
296 regexp-search-ring
297 regexp-search-ring-yank-pointer)
150f39ee 298 "List of global variables that `desktop-clear' will clear.
4a2fce7a 299An element may be variable name (a symbol) or a cons cell of the form
cc8b76bf
JB
300\(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
301to the value obtained by evaluating FORM."
4a2fce7a 302 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
af61551b 303 :group 'desktop
bf247b6e 304 :version "22.1")
4a2fce7a 305
0f4804e7
LH
306(defcustom desktop-clear-preserve-buffers
307 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*")
308 "*List of buffers that `desktop-clear' should not delete.
309Each element is a regular expression. Buffers with a name matched by any of
310these won't be deleted."
4a2fce7a
RS
311 :type '(repeat string)
312 :group 'desktop)
340db502 313
0f4804e7 314;;;###autoload
340db502
LH
315(defcustom desktop-locals-to-save
316 '(desktop-locals-to-save ; Itself! Think it over.
317 truncate-lines
318 case-fold-search
319 case-replace
320 fill-column
321 overwrite-mode
322 change-log-default-name
323 line-number-mode
0f4804e7
LH
324 column-number-mode
325 size-indication-mode
326 buffer-file-coding-system
327 indent-tabs-mode
11425834 328 tab-width
0f4804e7
LH
329 indicate-buffer-boundaries
330 indicate-empty-lines
331 show-trailing-whitespace)
577ed2b2 332 "List of local variables to save for each buffer.
0f4804e7
LH
333The variables are saved only when they really are local. Conventional minor
334modes are restored automatically; they should not be listed here."
c5b31c4d
LH
335 :type '(repeat symbol)
336 :group 'desktop)
ec4c6f22 337
0b9bd504 338;; We skip .log files because they are normally temporary.
a7acbbe4 339;; (ftp) files because they require passwords and whatnot.
bbf5eb28 340(defcustom desktop-buffers-not-to-save
df410295 341 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\)$"
4a2fce7a
RS
342 "Regexp identifying buffers that are to be excluded from saving."
343 :type 'regexp
344 :group 'desktop)
6343ed61 345
c5b31c4d 346;; Skip tramp and ange-ftp files
bbf5eb28 347(defcustom desktop-files-not-to-save
fa379636 348 "^/[^/:]*:"
bbf5eb28
RS
349 "Regexp identifying files whose buffers are to be excluded from saving."
350 :type 'regexp
351 :group 'desktop)
fa379636 352
df410295
JL
353;; We skip TAGS files to save time (tags-file-name is saved instead).
354(defcustom desktop-modes-not-to-save
355 '(tags-table-mode)
80f9f3db
SM
356 "List of major modes whose buffers should not be saved."
357 :type '(repeat symbol)
358 :group 'desktop)
359
4a2fce7a
RS
360(defcustom desktop-file-name-format 'absolute
361 "*Format in which desktop file names should be saved.
362Possible values are:
363 absolute -- Absolute file name.
364 tilde -- Relative to ~.
365 local -- Relative to directory of desktop file."
366 :type '(choice (const absolute) (const tilde) (const local))
af61551b 367 :group 'desktop
bf247b6e 368 :version "22.1")
569c754e 369
150f39ee
LH
370(defcustom desktop-restore-eager t
371 "Number of buffers to restore immediately.
372Remaining buffers are restored lazily (when Emacs is idle).
373If value is t, all buffers are restored immediately."
0ba9bc53 374 :type '(choice (const t) integer)
150f39ee 375 :group 'desktop
bf247b6e 376 :version "22.1")
150f39ee
LH
377
378(defcustom desktop-lazy-verbose t
379 "Verbose reporting of lazily created buffers."
380 :type 'boolean
381 :group 'desktop
bf247b6e 382 :version "22.1")
150f39ee
LH
383
384(defcustom desktop-lazy-idle-delay 5
385 "Idle delay before starting to create buffers.
386See `desktop-restore-eager'."
387 :type 'integer
388 :group 'desktop
bf247b6e 389 :version "22.1")
150f39ee 390
e5780ae1 391;;;###autoload
ebb39555
LH
392(defvar desktop-save-buffer nil
393 "When non-nil, save buffer status in desktop file.
e5780ae1 394This variable becomes buffer local when set.
ebb39555 395
b89c5a72
JB
396If the value is a function, it is called by `desktop-save' with argument
397DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
ebb39555 398file along with the state of the buffer for which it was called.
b6b70cda 399
c5b31c4d 400When file names are returned, they should be formatted using the call
e5780ae1 401\"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
4a2fce7a 402
0f4804e7
LH
403Later, when `desktop-read' evaluates the desktop file, auxiliary information
404is passed as the argument DESKTOP-BUFFER-MISC to functions in
405`desktop-buffer-mode-handlers'.")
ebb39555
LH
406(make-variable-buffer-local 'desktop-save-buffer)
407(make-obsolete-variable 'desktop-buffer-modes-to-save
cc8b76bf 408 'desktop-save-buffer "22.1")
e5780ae1 409(make-obsolete-variable 'desktop-buffer-misc-functions
cc8b76bf 410 'desktop-save-buffer "22.1")
b6b70cda 411
0f4804e7
LH
412;;;###autoload
413(defvar desktop-buffer-mode-handlers
414 nil
e5780ae1 415 "Alist of major mode specific functions to restore a desktop buffer.
0f4804e7
LH
416Functions listed are called by `desktop-create-buffer' when `desktop-read'
417evaluates the desktop file. List elements must have the form
418
419 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
e5780ae1
LH
420
421Buffers with a major mode not specified here, are restored by the default
422handler `desktop-restore-file-buffer'.
423
55775448 424Handlers are called with argument list
4a2fce7a 425
9bcabb45 426 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
e5780ae1
LH
427
428Furthermore, they may use the following variables:
429
430 desktop-file-version
4a2fce7a
RS
431 desktop-buffer-major-mode
432 desktop-buffer-minor-modes
433 desktop-buffer-point
434 desktop-buffer-mark
435 desktop-buffer-read-only
4a2fce7a
RS
436 desktop-buffer-locals
437
e5780ae1 438If a handler returns a buffer, then the saved mode settings
0f4804e7
LH
439and variable values for that buffer are copied into it.
440
441Modules that define a major mode that needs a special handler should contain
442code like
443
444 (defun foo-restore-desktop-buffer
445 ...
446 (add-to-list 'desktop-buffer-mode-handlers
447 '(foo-mode . foo-restore-desktop-buffer))
448
449Furthermore the major mode function must be autoloaded.")
ec4c6f22 450
498eb267 451;;;###autoload
e5780ae1
LH
452(put 'desktop-buffer-mode-handlers 'risky-local-variable t)
453(make-obsolete-variable 'desktop-buffer-handlers
cc8b76bf 454 'desktop-buffer-mode-handlers "22.1")
b6b70cda 455
47640244
GM
456(defcustom desktop-minor-mode-table
457 '((auto-fill-function auto-fill-mode)
5fff0265 458 (vc-mode nil)
f2168a4c
MO
459 (vc-dired-mode nil)
460 (erc-track-minor-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
c5b31c4d 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;; ----------------------------------------------------------------------------
05f1c4ec 576(defun desktop-truncate (list n)
ec4c6f22 577 "Truncate LIST to at most N elements destructively."
05f1c4ec 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 ()
c5b31c4d 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 653;; ----------------------------------------------------------------------------
c3ef64a4 654(declare-function uniquify-item-base "uniquify" (cl-x) t) ; defstruct
70b0a693 655
e88110db
JB
656(defun desktop-buffer-info (buffer)
657 (set-buffer buffer)
658 (list
659 ;; basic information
9e29c91c 660 (desktop-file-name (buffer-file-name) desktop-dirname)
93c5a976
JB
661 (if (bound-and-true-p uniquify-managed)
662 (uniquify-item-base (car uniquify-managed))
663 (buffer-name))
e88110db
JB
664 major-mode
665 ;; minor modes
666 (let (ret)
667 (mapc
668 #'(lambda (minor-mode)
669 (and (boundp minor-mode)
670 (symbol-value minor-mode)
671 (let* ((special (assq minor-mode desktop-minor-mode-table))
672 (value (cond (special (cadr special))
673 ((functionp minor-mode) minor-mode))))
674 (when value (add-to-list 'ret value)))))
675 (mapcar #'car minor-mode-alist))
676 ret)
677 ;; point and mark, and read-only status
678 (point)
679 (list (mark t) mark-active)
680 buffer-read-only
681 ;; auxiliary information
682 (when (functionp desktop-save-buffer)
9e29c91c 683 (funcall desktop-save-buffer desktop-dirname))
e88110db
JB
684 ;; local variables
685 (let ((locals desktop-locals-to-save)
686 (loclist (buffer-local-variables))
687 (ll))
688 (while locals
689 (let ((here (assq (car locals) loclist)))
690 (if here
691 (setq ll (cons here ll))
692 (when (member (car locals) loclist)
693 (setq ll (cons (car locals) ll)))))
694 (setq locals (cdr locals)))
695 ll)))
696
4a2fce7a 697;; ----------------------------------------------------------------------------
05f1c4ec 698(defun desktop-internal-v2s (value)
577ed2b2
RS
699 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
700TXT is a string that when read and evaluated yields value.
701QUOTE may be `may' (value may be quoted),
702`must' (values must be quoted), or nil (value may not be quoted)."
de9e2828 703 (cond
1f7efe1b
JB
704 ((or (numberp value) (null value) (eq t value) (keywordp value))
705 (cons 'may (prin1-to-string value)))
706 ((stringp value)
707 (let ((copy (copy-sequence value)))
708 (set-text-properties 0 (length copy) nil copy)
709 ;; Get rid of text properties because we cannot read them
710 (cons 'may (prin1-to-string copy))))
711 ((symbolp value)
712 (cons 'must (prin1-to-string value)))
713 ((vectorp value)
714 (let* ((special nil)
715 (pass1 (mapcar
716 (lambda (el)
717 (let ((res (desktop-internal-v2s el)))
718 (if (null (car res))
719 (setq special t))
720 res))
721 value)))
722 (if special
723 (cons nil (concat "(vector "
724 (mapconcat (lambda (el)
725 (if (eq (car el) 'must)
726 (concat "'" (cdr el))
727 (cdr el)))
728 pass1
729 " ")
730 ")"))
731 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
732 ((consp value)
733 (let ((p value)
734 newlist
735 use-list*
736 anynil)
737 (while (consp p)
738 (let ((q.txt (desktop-internal-v2s (car p))))
739 (or anynil (setq anynil (null (car q.txt))))
740 (setq newlist (cons q.txt newlist)))
741 (setq p (cdr p)))
742 (if p
743 (let ((last (desktop-internal-v2s p)))
744 (or anynil (setq anynil (null (car last))))
745 (or anynil
746 (setq newlist (cons '(must . ".") newlist)))
747 (setq use-list* t)
748 (setq newlist (cons last newlist))))
749 (setq newlist (nreverse newlist))
750 (if anynil
751 (cons nil
752 (concat (if use-list* "(desktop-list* " "(list ")
753 (mapconcat (lambda (el)
754 (if (eq (car el) 'must)
755 (concat "'" (cdr el))
756 (cdr el)))
757 newlist
758 " ")
759 ")"))
760 (cons 'must
761 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
762 ((subrp value)
763 (cons nil (concat "(symbol-function '"
764 (substring (prin1-to-string value) 7 -1)
765 ")")))
766 ((markerp value)
767 (let ((pos (prin1-to-string (marker-position value)))
768 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
769 (cons nil (concat "(let ((mk (make-marker)))"
770 " (add-hook 'desktop-delay-hook"
771 " (list 'lambda '() (list 'set-marker mk "
772 pos " (get-buffer " buf ")))) mk)"))))
773 (t ; save as text
774 (cons 'may "\"Unprintable entity\""))))
de9e2828 775
4a2fce7a 776;; ----------------------------------------------------------------------------
05f1c4ec 777(defun desktop-value-to-string (value)
577ed2b2
RS
778 "Convert VALUE to a string that when read evaluates to the same value.
779Not all types of values are supported."
de9e2828
RS
780 (let* ((print-escape-newlines t)
781 (float-output-format nil)
05f1c4ec 782 (quote.txt (desktop-internal-v2s value))
de9e2828
RS
783 (quote (car quote.txt))
784 (txt (cdr quote.txt)))
785 (if (eq quote 'must)
786 (concat "'" txt)
787 txt)))
4a2fce7a 788
ec4c6f22 789;; ----------------------------------------------------------------------------
0e7c8611
RS
790(defun desktop-outvar (varspec)
791 "Output a setq statement for variable VAR to the desktop file.
792The argument VARSPEC may be the variable name VAR (a symbol),
be617bbf 793or a cons cell of the form (VAR . MAX-SIZE),
0e7c8611
RS
794which means to truncate VAR's value to at most MAX-SIZE elements
795\(if the value is a list) before saving the value."
796 (let (var size)
797 (if (consp varspec)
798 (setq var (car varspec) size (cdr varspec))
799 (setq var varspec))
1f7efe1b
JB
800 (when (boundp var)
801 (when (and (integerp size)
802 (> size 0)
803 (listp (eval var)))
804 (desktop-truncate (eval var) size))
805 (insert "(setq "
806 (symbol-name var)
807 " "
808 (desktop-value-to-string (symbol-value var))
809 ")\n"))))
4a2fce7a 810
0b9bd504 811;; ----------------------------------------------------------------------------
ec4c6f22 812(defun desktop-save-buffer-p (filename bufname mode &rest dummy)
ebb39555 813 "Return t if buffer should have its state saved in the desktop file.
0b9bd504 814FILENAME is the visited file name, BUFNAME is the buffer name, and
be617bbf
JB
815MODE is the major mode.
816\n\(fn FILENAME BUFNAME MODE)"
fa379636 817 (let ((case-fold-search nil))
80f9f3db 818 (and (not (string-match desktop-buffers-not-to-save bufname))
ebb39555
LH
819 (not (memq mode desktop-modes-not-to-save))
820 (or (and filename
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
4a2fce7a 845;; ----------------------------------------------------------------------------
11425834 846;;;###autoload
e88110db 847(defun desktop-save (dirname &optional release)
c5b31c4d
LH
848 "Save the desktop in a desktop file.
849Parameter DIRNAME specifies where to save the desktop file.
e88110db 850Optional parameter RELEASE says whether we're done with this desktop.
c5b31c4d 851See also `desktop-base-file-name'."
6343ed61 852 (interactive "DDirectory to save desktop file in: ")
e88110db 853 (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
6343ed61 854 (save-excursion
e88110db
JB
855 (let ((eager desktop-restore-eager)
856 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
857 (when
858 (or (not new-modtime) ; nothing to overwrite
859 (equal desktop-file-modtime new-modtime)
860 (yes-or-no-p (if desktop-file-modtime
861 (if (> (float-time new-modtime) (float-time desktop-file-modtime))
862 "Desktop file is more recent than the one loaded. Save anyway? "
863 "Desktop file isn't the one loaded. Overwrite it? ")
864 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
865 (unless release (error "Desktop file conflict")))
866
867 ;; If we're done with it, release the lock.
868 ;; Otherwise, claim it if it's unclaimed or if we created it.
869 (if release
870 (desktop-release-lock)
871 (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
872
873 (with-temp-buffer
874 (insert
875 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
876 desktop-header
877 ";; Created " (current-time-string) "\n"
878 ";; Desktop file format version " desktop-file-version "\n"
879 ";; Emacs version " emacs-version "\n")
880 (save-excursion (run-hooks 'desktop-save-hook))
881 (goto-char (point-max))
882 (insert "\n;; Global section:\n")
883 (mapc (function desktop-outvar) desktop-globals-to-save)
884 (when (memq 'kill-ring desktop-globals-to-save)
885 (insert
886 "(setq kill-ring-yank-pointer (nthcdr "
887 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
888 " kill-ring))\n"))
889
890 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
891 (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
892 (when (apply 'desktop-save-buffer-p l)
893 (insert "("
894 (if (or (not (integerp eager))
895 (if (zerop eager)
896 nil
897 (setq eager (1- eager))))
898 "desktop-create-buffer"
899 "desktop-append-buffer-args")
900 " "
901 desktop-file-version)
902 (dolist (e l)
903 (insert "\n " (desktop-value-to-string e)))
904 (insert ")\n\n")))
905
9e29c91c 906 (setq default-directory desktop-dirname)
e88110db
JB
907 (let ((coding-system-for-write 'emacs-mule))
908 (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
909 ;; We remember when it was modified (which is presumably just now).
910 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))
4a2fce7a 911
0b9bd504 912;; ----------------------------------------------------------------------------
11425834 913;;;###autoload
6343ed61 914(defun desktop-remove ()
c5b31c4d
LH
915 "Delete desktop file in `desktop-dirname'.
916This function also sets `desktop-dirname' to nil."
6343ed61 917 (interactive)
c5b31c4d 918 (when desktop-dirname
11425834 919 (let ((filename (desktop-full-file-name)))
c5b31c4d
LH
920 (setq desktop-dirname nil)
921 (when (file-exists-p filename)
73b0b745 922 (delete-file filename)))))
c5b31c4d 923
150f39ee
LH
924(defvar desktop-buffer-args-list nil
925 "List of args for `desktop-create-buffer'.")
926
927(defvar desktop-lazy-timer nil)
928
0b9bd504 929;; ----------------------------------------------------------------------------
478653c9 930;;;###autoload
c5b31c4d
LH
931(defun desktop-read (&optional dirname)
932 "Read and process the desktop file in directory DIRNAME.
933Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
934directories listed in `desktop-path'. If a desktop file is found, it
cc8b76bf 935is processed and `desktop-after-read-hook' is run. If no desktop file
c5b31c4d
LH
936is found, clear the desktop and run `desktop-no-desktop-file-hook'.
937This function is a no-op when Emacs is running in batch mode.
938It returns t if a desktop file was loaded, nil otherwise."
6343ed61 939 (interactive)
4a2fce7a 940 (unless noninteractive
c5b31c4d 941 (setq desktop-dirname
11425834
LH
942 (file-name-as-directory
943 (expand-file-name
944 (or
945 ;; If DIRNAME is specified, use it.
946 (and (< 0 (length dirname)) dirname)
947 ;; Otherwise search desktop file in desktop-path.
948 (let ((dirs desktop-path))
949 (while (and dirs
950 (not (file-exists-p
951 (desktop-full-file-name (car dirs)))))
952 (setq dirs (cdr dirs)))
953 (and dirs (car dirs)))
954 ;; If not found and `desktop-path' is non-nil, use its first element.
955 (and desktop-path (car desktop-path))
956 ;; Default: Home directory.
957 "~"))))
958 (if (file-exists-p (desktop-full-file-name))
e88110db
JB
959 ;; Desktop file found, but is it already in use?
960 (let ((desktop-first-buffer nil)
961 (desktop-buffer-ok-count 0)
962 (desktop-buffer-fail-count 0)
963 (owner (desktop-owner))
964 ;; Avoid desktop saving during evaluation of desktop buffer.
965 (desktop-save nil))
966 (if (and owner
1f7efe1b
JB
967 (memq desktop-load-locked-desktop '(nil ask))
968 (or (null desktop-load-locked-desktop)
969 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
970Using it may cause conflicts. Use it anyway? " owner)))))
971 (progn
1f7efe1b
JB
972 (let ((default-directory desktop-dirname))
973 (run-hooks 'desktop-not-loaded-hook))
794855ca 974 (setq desktop-dirname nil)
1f7efe1b 975 (message "Desktop file in use; not loaded."))
e88110db
JB
976 (desktop-lazy-abort)
977 ;; Evaluate desktop buffer and remember when it was modified.
978 (load (desktop-full-file-name) t t t)
979 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
980 ;; If it wasn't already, mark it as in-use, to bother other
981 ;; desktop instances.
982 (unless owner
983 (condition-case nil
984 (desktop-claim-lock)
985 (file-error (message "Couldn't record use of desktop file")
986 (sit-for 1))))
987
988 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
989 ;; We want buffers existing prior to evaluating the desktop (and
990 ;; not reused) to be placed at the end of the buffer list, so we
991 ;; move them here.
992 (mapc 'bury-buffer
993 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
994 (switch-to-buffer (car (buffer-list)))
995 (run-hooks 'desktop-delay-hook)
996 (setq desktop-delay-hook nil)
997 (run-hooks 'desktop-after-read-hook)
998 (message "Desktop: %d buffer%s restored%s%s."
999 desktop-buffer-ok-count
1000 (if (= 1 desktop-buffer-ok-count) "" "s")
1001 (if (< 0 desktop-buffer-fail-count)
1002 (format ", %d failed to restore" desktop-buffer-fail-count)
1003 "")
1004 (if desktop-buffer-args-list
1005 (format ", %d to restore lazily"
1006 (length desktop-buffer-args-list))
1007 ""))
1008 t))
c5b31c4d
LH
1009 ;; No desktop file found.
1010 (desktop-clear)
1011 (let ((default-directory desktop-dirname))
1012 (run-hooks 'desktop-no-desktop-file-hook))
1013 (message "No desktop file.")
1014 nil)))
4a2fce7a 1015
0b9bd504 1016;; ----------------------------------------------------------------------------
c5b31c4d 1017;; Maintained for backward compatibility
478653c9 1018;;;###autoload
6343ed61 1019(defun desktop-load-default ()
577ed2b2 1020 "Load the `default' start-up library manually.
c5b31c4d 1021Also inhibit further loading of it."
b89c5a72
JB
1022 (unless inhibit-default-init ; safety check
1023 (load "default" t t)
1024 (setq inhibit-default-init t)))
1025(make-obsolete 'desktop-load-default
1026 'desktop-save-mode "22.1")
4a2fce7a
RS
1027
1028;; ----------------------------------------------------------------------------
1029;;;###autoload
c5b31c4d
LH
1030(defun desktop-change-dir (dirname)
1031 "Change to desktop saved in DIRNAME.
1032Kill the desktop as specified by variables `desktop-save-mode' and
1033`desktop-save', then clear the desktop and load the desktop file in
1034directory DIRNAME."
1035 (interactive "DChange to directory: ")
1036 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
4a2fce7a
RS
1037 (desktop-kill)
1038 (desktop-clear)
c5b31c4d 1039 (desktop-read dirname))
bf247b6e 1040
c5b31c4d 1041;; ----------------------------------------------------------------------------
4a2fce7a 1042;;;###autoload
c5b31c4d
LH
1043(defun desktop-save-in-desktop-dir ()
1044 "Save the desktop in directory `desktop-dirname'."
4a2fce7a
RS
1045 (interactive)
1046 (if desktop-dirname
b89c5a72 1047 (desktop-save desktop-dirname)
4a2fce7a
RS
1048 (call-interactively 'desktop-save))
1049 (message "Desktop saved in %s" desktop-dirname))
1050
1051;; ----------------------------------------------------------------------------
1052;;;###autoload
1053(defun desktop-revert ()
1054 "Revert to the last loaded desktop."
1055 (interactive)
c5b31c4d
LH
1056 (unless desktop-dirname
1057 (error "Unknown desktop directory"))
11425834 1058 (unless (file-exists-p (desktop-full-file-name))
c5b31c4d
LH
1059 (error "No desktop file found"))
1060 (desktop-clear)
1061 (desktop-read desktop-dirname))
4a2fce7a 1062
341c2f07
SM
1063(defvar desktop-buffer-major-mode)
1064(defvar desktop-buffer-locals)
0b9bd504 1065;; ----------------------------------------------------------------------------
e5780ae1
LH
1066(defun desktop-restore-file-buffer (desktop-buffer-file-name
1067 desktop-buffer-name
1068 desktop-buffer-misc)
1069 "Restore a file buffer."
1f7efe1b
JB
1070 (when desktop-buffer-file-name
1071 (if (or (file-exists-p desktop-buffer-file-name)
1072 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1073 desktop-buffer-file-name)))
1074 (if desktop-missing-file-warning
1075 (y-or-n-p (concat msg " Re-create buffer? "))
1076 (message "%s" msg)
1077 nil)))
1078 (let* ((auto-insert nil) ; Disable auto insertion
1079 (coding-system-for-read
1080 (or coding-system-for-read
1081 (cdr (assq 'buffer-file-coding-system
1082 desktop-buffer-locals))))
1083 (buf (find-file-noselect desktop-buffer-file-name)))
1084 (condition-case nil
1085 (switch-to-buffer buf)
1086 (error (pop-to-buffer buf)))
1087 (and (not (eq major-mode desktop-buffer-major-mode))
1088 (functionp desktop-buffer-major-mode)
1089 (funcall desktop-buffer-major-mode))
1090 buf)
1091 nil)))
4a2fce7a 1092
0f4804e7
LH
1093(defun desktop-load-file (function)
1094 "Load the file where auto loaded FUNCTION is defined."
97b3a214 1095 (when function
07696982 1096 (let ((fcell (and (fboundp function) (symbol-function function))))
97b3a214
LH
1097 (when (and (listp fcell)
1098 (eq 'autoload (car fcell)))
1099 (load (cadr fcell))))))
0f4804e7 1100
0b9bd504 1101;; ----------------------------------------------------------------------------
eb982898
JL
1102;; Create a buffer, load its file, set its mode, ...;
1103;; called from Desktop file only.
1d500ca6 1104
0f4804e7 1105;; Just to silence the byte compiler.
341c2f07
SM
1106
1107(defvar desktop-first-buffer) ; Dynamically bound in `desktop-read'
1108
1109;; Bound locally in `desktop-read'.
1110(defvar desktop-buffer-ok-count)
1111(defvar desktop-buffer-fail-count)
4a2fce7a 1112
340db502
LH
1113(defun desktop-create-buffer
1114 (desktop-file-version
1115 desktop-buffer-file-name
1116 desktop-buffer-name
1117 desktop-buffer-major-mode
1118 desktop-buffer-minor-modes
1119 desktop-buffer-point
1120 desktop-buffer-mark
1121 desktop-buffer-read-only
1122 desktop-buffer-misc
1123 &optional
1124 desktop-buffer-locals)
4a2fce7a
RS
1125 ;; To make desktop files with relative file names possible, we cannot
1126 ;; allow `default-directory' to change. Therefore we save current buffer.
1127 (save-current-buffer
0f4804e7
LH
1128 ;; Give major mode module a chance to add a handler.
1129 (desktop-load-file desktop-buffer-major-mode)
340db502
LH
1130 (let ((buffer-list (buffer-list))
1131 (result
1132 (condition-case err
1133 (funcall (or (cdr (assq desktop-buffer-major-mode
1134 desktop-buffer-mode-handlers))
1135 'desktop-restore-file-buffer)
1136 desktop-buffer-file-name
1137 desktop-buffer-name
1138 desktop-buffer-misc)
1139 (error
1140 (message "Desktop: Can't load buffer %s: %s"
1141 desktop-buffer-name
1142 (error-message-string err))
1143 (when desktop-missing-file-warning (sit-for 1))
1144 nil))))
e5780ae1
LH
1145 (if (bufferp result)
1146 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1147 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1148 (setq result nil))
7bcbf3c2
JB
1149 ;; Restore buffer list order with new buffer at end. Don't change
1150 ;; the order for old desktop files (old desktop module behaviour).
4a2fce7a 1151 (unless (< desktop-file-version 206)
48aa4dfc 1152 (mapc 'bury-buffer buffer-list)
7bcbf3c2 1153 (when result (bury-buffer result)))
4a2fce7a 1154 (when result
7bcbf3c2
JB
1155 (unless (or desktop-first-buffer (< desktop-file-version 206))
1156 (setq desktop-first-buffer result))
4a2fce7a
RS
1157 (set-buffer result)
1158 (unless (equal (buffer-name) desktop-buffer-name)
93c5a976 1159 (rename-buffer desktop-buffer-name t))
4a2fce7a 1160 ;; minor modes
340db502
LH
1161 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1162 (auto-fill-mode 1))
1163 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1164 (auto-fill-mode 0))
1165 (t
341c2f07
SM
1166 (dolist (minor-mode desktop-buffer-minor-modes)
1167 ;; Give minor mode module a chance to add a handler.
1168 (desktop-load-file minor-mode)
1169 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1170 (if handler
1171 (funcall handler desktop-buffer-locals)
1172 (when (functionp minor-mode)
1173 (funcall minor-mode 1)))))))
1174 ;; Even though point and mark are non-nil when written by
1175 ;; `desktop-save', they may be modified by handlers wanting to set
1176 ;; point or mark themselves.
e5780ae1
LH
1177 (when desktop-buffer-point
1178 (goto-char
1179 (condition-case err
341c2f07
SM
1180 ;; Evaluate point. Thus point can be something like
1181 ;; '(search-forward ...
e5780ae1
LH
1182 (eval desktop-buffer-point)
1183 (error (message "%s" (error-message-string err)) 1))))
4a2fce7a
RS
1184 (when desktop-buffer-mark
1185 (if (consp desktop-buffer-mark)
1f7efe1b
JB
1186 (progn
1187 (set-mark (car desktop-buffer-mark))
1188 (setq mark-active (car (cdr desktop-buffer-mark))))
4a2fce7a
RS
1189 (set-mark desktop-buffer-mark)))
1190 ;; Never override file system if the file really is read-only marked.
1f7efe1b 1191 (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
4a2fce7a
RS
1192 (while desktop-buffer-locals
1193 (let ((this (car desktop-buffer-locals)))
1194 (if (consp this)
1f7efe1b
JB
1195 ;; an entry of this form `(symbol . value)'
1196 (progn
1197 (make-local-variable (car this))
1198 (set (car this) (cdr this)))
4a2fce7a
RS
1199 ;; an entry of the form `symbol'
1200 (make-local-variable this)
1201 (makunbound this)))
1202 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
ec4c6f22 1203
4a2fce7a 1204;; ----------------------------------------------------------------------------
ec4c6f22 1205;; Backward compatibility -- update parameters to 205 standards.
569c754e
RS
1206(defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1207 desktop-buffer-major-mode
2699e23e 1208 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
569c754e 1209 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
2699e23e
RS
1210 desktop-buffer-major-mode (cdr mim) pt mk ro
1211 desktop-buffer-misc
ec4c6f22
RS
1212 (list (cons 'truncate-lines tl)
1213 (cons 'fill-column fc)
1214 (cons 'case-fold-search cfs)
1215 (cons 'case-replace cr)
1216 (cons 'overwrite-mode (car mim)))))
84b538ec 1217
150f39ee 1218(defun desktop-append-buffer-args (&rest args)
cc8b76bf 1219 "Append ARGS at end of `desktop-buffer-args-list'.
150f39ee
LH
1220ARGS must be an argument list for `desktop-create-buffer'."
1221 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1222 (unless desktop-lazy-timer
1223 (setq desktop-lazy-timer
1224 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1225
1226(defun desktop-lazy-create-buffer ()
1227 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1228 (when desktop-buffer-args-list
1229 (let* ((remaining (length desktop-buffer-args-list))
1230 (args (pop desktop-buffer-args-list))
1231 (buffer-name (nth 2 args))
1232 (msg (format "Desktop lazily opening %s (%s remaining)..."
1233 buffer-name remaining)))
1234 (when desktop-lazy-verbose
8a26c165 1235 (message "%s" msg))
150f39ee
LH
1236 (let ((desktop-first-buffer nil)
1237 (desktop-buffer-ok-count 0)
1238 (desktop-buffer-fail-count 0))
1239 (apply 'desktop-create-buffer args)
1240 (run-hooks 'desktop-delay-hook)
1241 (setq desktop-delay-hook nil)
1242 (bury-buffer (get-buffer buffer-name))
1243 (when desktop-lazy-verbose
1244 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1245
1246(defun desktop-idle-create-buffers ()
1247 "Create buffers until the user does something, then stop.
1248If there are no buffers left to create, kill the timer."
1249 (let ((repeat 1))
1250 (while (and repeat desktop-buffer-args-list)
1251 (save-window-excursion
1252 (desktop-lazy-create-buffer))
1253 (setq repeat (sit-for 0.2))
1254 (unless desktop-buffer-args-list
1255 (cancel-timer desktop-lazy-timer)
1256 (setq desktop-lazy-timer nil)
1257 (message "Lazy desktop load complete")
1258 (sit-for 3)
1259 (message "")))))
1260
1261(defun desktop-lazy-complete ()
1262 "Run the desktop load to completion."
1263 (interactive)
1264 (let ((desktop-lazy-verbose t))
1265 (while desktop-buffer-args-list
1266 (save-window-excursion
1267 (desktop-lazy-create-buffer)))
1268 (message "Lazy desktop load complete")))
1269
1270(defun desktop-lazy-abort ()
1271 "Abort lazy loading of the desktop."
1272 (interactive)
1273 (when desktop-lazy-timer
1274 (cancel-timer desktop-lazy-timer)
1275 (setq desktop-lazy-timer nil))
1276 (when desktop-buffer-args-list
1277 (setq desktop-buffer-args-list nil)
1278 (when (interactive-p)
1279 (message "Lazy desktop load aborted"))))
1280
0b9bd504 1281;; ----------------------------------------------------------------------------
c5b31c4d 1282;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
4a2fce7a
RS
1283;; command line, we do the rest of what it takes to use desktop, but do it
1284;; after finishing loading the init file.
1285;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1286;; functions are processed after `after-init-hook'.
1287(add-hook
1288 'after-init-hook
341c2f07 1289 (lambda ()
4a2fce7a 1290 (let ((key "--no-desktop"))
4b217d46
LH
1291 (when (member key command-line-args)
1292 (setq command-line-args (delete key command-line-args))
1293 (setq desktop-save-mode nil)))
1294 (when desktop-save-mode (desktop-read))))
813dbb2d 1295
ab1d55ea 1296(provide 'desktop)
6343ed61 1297
341c2f07 1298;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
80f9f3db 1299;;; desktop.el ends here