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