(custom-buffer-create-internal): Use widget type
[bpt/emacs.git] / lisp / desktop.el
CommitLineData
6343ed61
RS
1;;; desktop.el --- save partial status of Emacs when killed
2
0d30b337
TTN
3;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003,
4;; 2004, 2005 Free Software Foundation, Inc.
6343ed61
RS
5
6;; Author: Morten Welinder <terra@diku.dk>
247c3649 7;; Maintainter: Lars Hansen <larsh@soem.dk>
3ea96cac 8;; Keywords: convenience
b6105c76 9;; Favourite-brand-of-beer: None, I hate beer.
6343ed61
RS
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b578f267 24;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
6343ed61
RS
27
28;;; Commentary:
29
0b9bd504
RS
30;; Save the Desktop, i.e.,
31;; - some global variables
32;; - the list of buffers with associated files. For each buffer also
33;; - the major mode
34;; - the default directory
35;; - the point
36;; - the mark & mark-active
37;; - buffer-read-only
ec4c6f22 38;; - some local variables
6343ed61 39
c5b31c4d
LH
40;; To use this, use customize to turn on desktop-save-mode or add the
41;; following line somewhere in your .emacs file:
0b9bd504 42;;
c5b31c4d 43;; (desktop-save-mode 1)
0b9bd504 44;;
c5b31c4d
LH
45;; For further usage information, look at the section
46;; "Saving Emacs Sessions" in the GNU Emacs Manual.
47
48;; When the desktop module is loaded, the function `desktop-kill' is
49;; added to the `kill-emacs-hook'. This function is responsible for
50;; saving the desktop when Emacs is killed. Furthermore an anonymous
51;; function is added to the `after-init-hook'. This function is
52;; responsible for loading the desktop when Emacs is started.
ec4c6f22 53
0f4804e7
LH
54;; Special handling.
55;; -----------------
56;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
57;; are supplied to handle special major and minor modes respectively.
58;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
59;; to restore a desktop buffer. Elements must have the form
60;;
61;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
62;;
63;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
64;; evaluates the desktop file. Buffers with a major mode not specified here,
65;; are restored by the default handler `desktop-restore-file-buffer'.
66;; `desktop-minor-mode-handlers' is an alist of functions to restore
67;; non-standard minor modes. Elements must have the form
68;;
69;; (MINOR-MODE . RESTORE-FUNCTION).
70;;
71;; Functions are called by `desktop-create-buffer' to restore minor modes.
72;; Minor modes not specified here, are restored by the standard minor mode
73;; function. If you write a module that defines a major or minor mode that
74;; needs a special handler, then place code like
75
76;; (defun foo-restore-desktop-buffer
77;; ...
78;; (add-to-list 'desktop-buffer-mode-handlers
79;; '(foo-mode . foo-restore-desktop-buffer))
80
81;; or
82
83;; (defun bar-desktop-restore
84;; ...
85;; (add-to-list 'desktop-minor-mode-handlers
86;; '(bar-mode . bar-desktop-restore))
87
88;; in the module itself, and make shure that the mode function is
89;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
90;; `desktop-minor-mode-handlers' for more info.
91
92;; Minor modes.
93;; ------------
94;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
95;; manual) are handled in the following way:
96;; When `desktop-save' saves the state of a buffer to the desktop file, it
97;; saves as `desktop-minor-modes' the list of names of those variables in
98;; `minor-mode-alist' that have a non-nil value.
99;; When `desktop-create' restores the buffer, each of the symbols in
100;; `desktop-minor-modes' is called as function with parameter 1.
101;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
102;; are used to handle non-conventional minor modes. `desktop-save' uses
103;; `desktop-minor-mode-table' to map minor mode variables to minor mode
104;; functions before writing `desktop-minor-modes'. If a minor mode has a
105;; variable name that is different form its function name, an entry
106
107;; (NAME RESTORE-FUNCTION)
108
109;; should be added to `desktop-minor-mode-table'. If a minor mode should not
110;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
111;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
112;; function different from the usual minor mode function.
113;; ---------------------------------------------------------------------------
47640244 114
ec4c6f22
RS
115;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
116;; in your home directory is used for that. Saving global default values
117;; for buffers is an example of misuse.
118
0b9bd504
RS
119;; PLEASE NOTE: The kill ring can be saved as specified by the variable
120;; `desktop-globals-to-save' (by default it isn't). This may result in saving
121;; things you did not mean to keep. Use M-x desktop-clear RET.
ec4c6f22 122
fa379636
KH
123;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
124;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
125;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
126;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
127;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
f4c73d07 128;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
253406b9 129;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
0b9bd504
RS
130;; ---------------------------------------------------------------------------
131;; TODO:
132;;
133;; Save window configuration.
134;; Recognize more minor modes.
135;; Save mark rings.
6343ed61
RS
136
137;;; Code:
138
4a2fce7a 139(defvar desktop-file-version "206"
05f1c4ec 140 "Version number of desktop file format.
4a2fce7a
RS
141Written into the desktop file and used at desktop read to provide
142backward compatibility.")
143
ec4c6f22 144;; ----------------------------------------------------------------------------
0b9bd504
RS
145;; USER OPTIONS -- settings you might want to play with.
146;; ----------------------------------------------------------------------------
bbf5eb28
RS
147
148(defgroup desktop nil
149 "Save status of Emacs when you exit."
150 :group 'frames)
151
c5b31c4d
LH
152;;;###autoload
153(define-minor-mode desktop-save-mode
154 "Toggle desktop saving mode.
155With numeric ARG, turn desktop saving on if ARG is positive, off
156otherwise. See variable `desktop-save' for a description of when the
157desktop is saved."
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
4a2fce7a 165(defcustom desktop-save 'ask-if-new
c5b31c4d
LH
166 "*Specifies whether the desktop should be saved when it is killed.
167A desktop is killed when the user changes desktop or quits Emacs.
168Possible values are:
4a2fce7a
RS
169 t -- always save.
170 ask -- always ask.
171 ask-if-new -- ask if no desktop file exists, otherwise just save.
172 ask-if-exists -- ask if desktop file exists, otherwise don't save.
173 if-exists -- save if desktop file exists, otherwise don't save.
174 nil -- never save.
c5b31c4d 175The desktop is never saved when `desktop-save-mode' is nil.
cc8b76bf 176The variables `desktop-dirname' and `desktop-base-file-name'
c5b31c4d 177determine where the desktop is saved."
4a2fce7a
RS
178 :type '(choice
179 (const :tag "Always save" t)
180 (const :tag "Always ask" ask)
181 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
182 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
183 (const :tag "Save if desktop file exists, else don't" if-exists)
184 (const :tag "Never save" nil))
af61551b 185 :group 'desktop
bf247b6e 186 :version "22.1")
4a2fce7a
RS
187
188(defcustom desktop-base-file-name
fc715501 189 (convert-standard-filename ".emacs.desktop")
c5b31c4d 190 "Name of file for Emacs desktop, excluding the directory part."
813dbb2d
RS
191 :type 'file
192 :group 'desktop)
b89c5a72
JB
193(define-obsolete-variable-alias 'desktop-basefilename
194 'desktop-base-file-name "22.1")
6343ed61 195
4a2fce7a
RS
196(defcustom desktop-path '("." "~")
197 "List of directories to search for the desktop file.
198The base name of the file is specified in `desktop-base-file-name'."
199 :type '(repeat directory)
af61551b 200 :group 'desktop
bf247b6e 201 :version "22.1")
4a2fce7a 202
bbf5eb28 203(defcustom desktop-missing-file-warning nil
ebb39555
LH
204 "*If non-nil then `desktop-read' asks if a non-existent file should be recreated.
205Also pause for a moment to display message about errors signaled in
206`desktop-buffer-mode-handlers'.
207
208If nil, just print error messages in the message buffer."
bbf5eb28 209 :type 'boolean
af61551b 210 :group 'desktop
bf247b6e 211 :version "22.1")
6343ed61 212
4a2fce7a 213(defcustom desktop-no-desktop-file-hook nil
c5b31c4d 214 "Normal hook run when `desktop-read' can't find a desktop file.
8649a87b 215May be used to show a dired buffer."
4a2fce7a 216 :type 'hook
af61551b 217 :group 'desktop
bf247b6e 218 :version "22.1")
4a2fce7a
RS
219
220(defcustom desktop-after-read-hook nil
05f1c4ec 221 "Normal hook run after a successful `desktop-read'.
8649a87b 222May be used to show a buffer list."
4a2fce7a 223 :type 'hook
af61551b 224 :group 'desktop
bf247b6e 225 :version "22.1")
4a2fce7a
RS
226
227(defcustom desktop-save-hook nil
c5b31c4d 228 "Normal hook run before the desktop is saved in a desktop file.
4a2fce7a
RS
229This is useful for truncating history lists, for example."
230 :type 'hook
231 :group 'desktop)
232
340db502
LH
233(defcustom desktop-globals-to-save
234 '(desktop-missing-file-warning
235 tags-file-name
236 tags-table-list
237 search-ring
238 regexp-search-ring
239 register-alist)
c5b31c4d
LH
240 "List of global variables saved by `desktop-save'.
241An element may be variable name (a symbol) or a cons cell of the form
242\(VAR . MAX-SIZE), which means to truncate VAR's value to at most
243MAX-SIZE elements (if the value is a list) before saving the value.
4a2fce7a
RS
244Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
245 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
246 :group 'desktop)
247
340db502
LH
248(defcustom desktop-globals-to-clear
249 '(kill-ring
250 kill-ring-yank-pointer
251 search-ring
252 search-ring-yank-pointer
253 regexp-search-ring
254 regexp-search-ring-yank-pointer)
150f39ee 255 "List of global variables that `desktop-clear' will clear.
4a2fce7a 256An element may be variable name (a symbol) or a cons cell of the form
cc8b76bf
JB
257\(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
258to the value obtained by evaluating FORM."
4a2fce7a 259 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
af61551b 260 :group 'desktop
bf247b6e 261 :version "22.1")
4a2fce7a 262
0f4804e7
LH
263(defcustom desktop-clear-preserve-buffers
264 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*")
265 "*List of buffers that `desktop-clear' should not delete.
266Each element is a regular expression. Buffers with a name matched by any of
267these won't be deleted."
4a2fce7a
RS
268 :type '(repeat string)
269 :group 'desktop)
340db502 270
0f4804e7 271;;;###autoload
340db502
LH
272(defcustom desktop-locals-to-save
273 '(desktop-locals-to-save ; Itself! Think it over.
274 truncate-lines
275 case-fold-search
276 case-replace
277 fill-column
278 overwrite-mode
279 change-log-default-name
280 line-number-mode
0f4804e7
LH
281 column-number-mode
282 size-indication-mode
283 buffer-file-coding-system
284 indent-tabs-mode
285 indicate-buffer-boundaries
286 indicate-empty-lines
287 show-trailing-whitespace)
577ed2b2 288 "List of local variables to save for each buffer.
0f4804e7
LH
289The variables are saved only when they really are local. Conventional minor
290modes are restored automatically; they should not be listed here."
c5b31c4d
LH
291 :type '(repeat symbol)
292 :group 'desktop)
ec4c6f22 293
0b9bd504 294;; We skip .log files because they are normally temporary.
a7acbbe4 295;; (ftp) files because they require passwords and whatnot.
bbf5eb28 296(defcustom desktop-buffers-not-to-save
df410295 297 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\)$"
4a2fce7a
RS
298 "Regexp identifying buffers that are to be excluded from saving."
299 :type 'regexp
300 :group 'desktop)
6343ed61 301
c5b31c4d 302;; Skip tramp and ange-ftp files
bbf5eb28 303(defcustom desktop-files-not-to-save
fa379636 304 "^/[^/:]*:"
bbf5eb28
RS
305 "Regexp identifying files whose buffers are to be excluded from saving."
306 :type 'regexp
307 :group 'desktop)
fa379636 308
df410295
JL
309;; We skip TAGS files to save time (tags-file-name is saved instead).
310(defcustom desktop-modes-not-to-save
311 '(tags-table-mode)
80f9f3db
SM
312 "List of major modes whose buffers should not be saved."
313 :type '(repeat symbol)
314 :group 'desktop)
315
4a2fce7a
RS
316(defcustom desktop-file-name-format 'absolute
317 "*Format in which desktop file names should be saved.
318Possible values are:
319 absolute -- Absolute file name.
320 tilde -- Relative to ~.
321 local -- Relative to directory of desktop file."
322 :type '(choice (const absolute) (const tilde) (const local))
af61551b 323 :group 'desktop
bf247b6e 324 :version "22.1")
569c754e 325
150f39ee
LH
326(defcustom desktop-restore-eager t
327 "Number of buffers to restore immediately.
328Remaining buffers are restored lazily (when Emacs is idle).
329If value is t, all buffers are restored immediately."
0ba9bc53 330 :type '(choice (const t) integer)
150f39ee 331 :group 'desktop
bf247b6e 332 :version "22.1")
150f39ee
LH
333
334(defcustom desktop-lazy-verbose t
335 "Verbose reporting of lazily created buffers."
336 :type 'boolean
337 :group 'desktop
bf247b6e 338 :version "22.1")
150f39ee
LH
339
340(defcustom desktop-lazy-idle-delay 5
341 "Idle delay before starting to create buffers.
342See `desktop-restore-eager'."
343 :type 'integer
344 :group 'desktop
bf247b6e 345 :version "22.1")
150f39ee 346
e5780ae1 347;;;###autoload
ebb39555
LH
348(defvar desktop-save-buffer nil
349 "When non-nil, save buffer status in desktop file.
e5780ae1 350This variable becomes buffer local when set.
ebb39555 351
b89c5a72
JB
352If the value is a function, it is called by `desktop-save' with argument
353DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
ebb39555 354file along with the state of the buffer for which it was called.
b6b70cda 355
c5b31c4d 356When file names are returned, they should be formatted using the call
e5780ae1 357\"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
4a2fce7a 358
0f4804e7
LH
359Later, when `desktop-read' evaluates the desktop file, auxiliary information
360is passed as the argument DESKTOP-BUFFER-MISC to functions in
361`desktop-buffer-mode-handlers'.")
ebb39555
LH
362(make-variable-buffer-local 'desktop-save-buffer)
363(make-obsolete-variable 'desktop-buffer-modes-to-save
cc8b76bf 364 'desktop-save-buffer "22.1")
e5780ae1 365(make-obsolete-variable 'desktop-buffer-misc-functions
cc8b76bf 366 'desktop-save-buffer "22.1")
b6b70cda 367
0f4804e7
LH
368;;;###autoload
369(defvar desktop-buffer-mode-handlers
370 nil
e5780ae1 371 "Alist of major mode specific functions to restore a desktop buffer.
0f4804e7
LH
372Functions listed are called by `desktop-create-buffer' when `desktop-read'
373evaluates the desktop file. List elements must have the form
374
375 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
e5780ae1
LH
376
377Buffers with a major mode not specified here, are restored by the default
378handler `desktop-restore-file-buffer'.
379
55775448 380Handlers are called with argument list
4a2fce7a 381
9bcabb45 382 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
e5780ae1
LH
383
384Furthermore, they may use the following variables:
385
386 desktop-file-version
4a2fce7a
RS
387 desktop-buffer-major-mode
388 desktop-buffer-minor-modes
389 desktop-buffer-point
390 desktop-buffer-mark
391 desktop-buffer-read-only
4a2fce7a
RS
392 desktop-buffer-locals
393
e5780ae1 394If a handler returns a buffer, then the saved mode settings
0f4804e7
LH
395and variable values for that buffer are copied into it.
396
397Modules that define a major mode that needs a special handler should contain
398code like
399
400 (defun foo-restore-desktop-buffer
401 ...
402 (add-to-list 'desktop-buffer-mode-handlers
403 '(foo-mode . foo-restore-desktop-buffer))
404
405Furthermore the major mode function must be autoloaded.")
ec4c6f22 406
498eb267 407;;;###autoload
e5780ae1
LH
408(put 'desktop-buffer-mode-handlers 'risky-local-variable t)
409(make-obsolete-variable 'desktop-buffer-handlers
cc8b76bf 410 'desktop-buffer-mode-handlers "22.1")
b6b70cda 411
47640244
GM
412(defcustom desktop-minor-mode-table
413 '((auto-fill-function auto-fill-mode)
5fff0265
JL
414 (vc-mode nil)
415 (vc-dired-mode nil))
47640244
GM
416 "Table mapping minor mode variables to minor mode functions.
417Each entry has the form (NAME RESTORE-FUNCTION).
418NAME is the name of the buffer-local variable indicating that the minor
419mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
420called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
421Only minor modes for which the name of the buffer-local variable
7bfa55b3 422and the name of the minor mode function are different have to be added to
0f4804e7 423this table. See also `desktop-minor-mode-handlers'."
47640244
GM
424 :type 'sexp
425 :group 'desktop)
426
0f4804e7
LH
427;;;###autoload
428(defvar desktop-minor-mode-handlers
429 nil
430 "Alist of functions to restore non-standard minor modes.
431Functions are called by `desktop-create-buffer' to restore minor modes.
432List elements must have the form
433
434 (MINOR-MODE . RESTORE-FUNCTION).
435
436Minor modes not specified here, are restored by the standard minor mode
437function.
438
439Handlers are called with argument list
440
441 (DESKTOP-BUFFER-LOCALS)
442
443Furthermore, they may use the following variables:
444
445 desktop-file-version
446 desktop-buffer-file-name
447 desktop-buffer-name
448 desktop-buffer-major-mode
449 desktop-buffer-minor-modes
450 desktop-buffer-point
451 desktop-buffer-mark
452 desktop-buffer-read-only
453 desktop-buffer-misc
454
455When a handler is called, the buffer has been created and the major mode has
456been set, but local variables listed in desktop-buffer-locals has not yet been
457created and set.
458
459Modules that define a minor mode that needs a special handler should contain
460code like
461
462 (defun foo-desktop-restore
463 ...
464 (add-to-list 'desktop-minor-mode-handlers
465 '(foo-mode . foo-desktop-restore))
466
467Furthermore the minor mode function must be autoloaded.
468
469See also `desktop-minor-mode-table'.")
470
498eb267 471;;;###autoload
0f4804e7
LH
472(put 'desktop-minor-mode-handlers 'risky-local-variable t)
473
0b9bd504
RS
474;; ----------------------------------------------------------------------------
475(defvar desktop-dirname nil
c5b31c4d 476 "The directory in which the desktop file should be saved.")
6343ed61
RS
477
478(defconst desktop-header
0b9bd504
RS
479";; --------------------------------------------------------------------------
480;; Desktop File for Emacs
481;; --------------------------------------------------------------------------
6343ed61 482" "*Header to place in Desktop file.")
de9e2828
RS
483
484(defvar desktop-delay-hook nil
485 "Hooks run after all buffers are loaded; intended for internal use.")
813dbb2d 486
0b9bd504 487;; ----------------------------------------------------------------------------
05f1c4ec 488(defun desktop-truncate (list n)
ec4c6f22 489 "Truncate LIST to at most N elements destructively."
05f1c4ec 490 (let ((here (nthcdr (1- n) list)))
ec4c6f22 491 (if (consp here)
de9e2828 492 (setcdr here nil))))
f9be4574 493
4a2fce7a 494;; ----------------------------------------------------------------------------
f9be4574
RS
495(defun desktop-clear ()
496 "Empty the Desktop.
0f4804e7
LH
497This kills all buffers except for internal ones and those with names matched by
498a regular expression in the list `desktop-clear-preserve-buffers'.
499Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
6343ed61 500 (interactive)
150f39ee 501 (desktop-lazy-abort)
4a2fce7a
RS
502 (dolist (var desktop-globals-to-clear)
503 (if (symbolp var)
504 (eval `(setq-default ,var nil))
505 (eval `(setq-default ,(car var) ,(cdr var)))))
0f4804e7
LH
506 (let ((buffers (buffer-list))
507 (preserve-regexp (concat "^\\("
508 (mapconcat (lambda (regexp)
509 (concat "\\(" regexp "\\)"))
510 desktop-clear-preserve-buffers
511 "\\|")
512 "\\)$")))
f9be4574 513 (while buffers
4a2fce7a
RS
514 (let ((bufname (buffer-name (car buffers))))
515 (or
516 (null bufname)
0f4804e7 517 (string-match preserve-regexp bufname)
4a2fce7a 518 ;; Don't kill buffers made for internal purposes.
3f32d6a3 519 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
4a2fce7a 520 (kill-buffer (car buffers))))
f9be4574 521 (setq buffers (cdr buffers))))
b6105c76 522 (delete-other-windows))
4a2fce7a 523
0b9bd504 524;; ----------------------------------------------------------------------------
de9e2828 525(add-hook 'kill-emacs-hook 'desktop-kill)
ec4c6f22 526
6343ed61 527(defun desktop-kill ()
c5b31c4d 528 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
4a2fce7a
RS
529If the desktop should be saved and `desktop-dirname'
530is nil, ask the user where to save the desktop."
531 (when
532 (and
c5b31c4d 533 desktop-save-mode
73b0b745 534 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
4a2fce7a 535 (or
d12d9396 536 (eq desktop-save t)
4a2fce7a
RS
537 (and exists (memq desktop-save '(ask-if-new if-exists)))
538 (and
539 (or
540 (memq desktop-save '(ask ask-if-new))
541 (and exists (eq desktop-save 'ask-if-exists)))
542 (y-or-n-p "Save desktop? ")))))
543 (unless desktop-dirname
544 (setq desktop-dirname
73b0b745
JB
545 (file-name-as-directory
546 (expand-file-name
547 (call-interactively
548 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
4a2fce7a
RS
549 (condition-case err
550 (desktop-save desktop-dirname)
551 (file-error
cc8b76bf 552 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
4a2fce7a
RS
553 (signal (car err) (cdr err)))))))
554
0b9bd504 555;; ----------------------------------------------------------------------------
ed2f7fc8
RS
556(defun desktop-list* (&rest args)
557 (if (null (cdr args))
558 (car args)
559 (setq args (nreverse args))
560 (let ((value (cons (nth 1 args) (car args))))
561 (setq args (cdr (cdr args)))
562 (while args
563 (setq value (cons (car args) value))
564 (setq args (cdr args)))
565 value)))
566
4a2fce7a 567;; ----------------------------------------------------------------------------
05f1c4ec 568(defun desktop-internal-v2s (value)
577ed2b2
RS
569 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
570TXT is a string that when read and evaluated yields value.
571QUOTE may be `may' (value may be quoted),
572`must' (values must be quoted), or nil (value may not be quoted)."
de9e2828 573 (cond
3f32d6a3 574 ((or (numberp value) (null value) (eq t value) (keywordp value))
05f1c4ec
JB
575 (cons 'may (prin1-to-string value)))
576 ((stringp value)
577 (let ((copy (copy-sequence value)))
95bf6435
RS
578 (set-text-properties 0 (length copy) nil copy)
579 ;; Get rid of text properties because we cannot read them
580 (cons 'may (prin1-to-string copy))))
05f1c4ec
JB
581 ((symbolp value)
582 (cons 'must (prin1-to-string value)))
583 ((vectorp value)
de9e2828
RS
584 (let* ((special nil)
585 (pass1 (mapcar
586 (lambda (el)
587 (let ((res (desktop-internal-v2s el)))
588 (if (null (car res))
589 (setq special t))
590 res))
05f1c4ec 591 value)))
de9e2828
RS
592 (if special
593 (cons nil (concat "(vector "
594 (mapconcat (lambda (el)
595 (if (eq (car el) 'must)
596 (concat "'" (cdr el))
597 (cdr el)))
598 pass1
599 " ")
600 ")"))
601 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
05f1c4ec
JB
602 ((consp value)
603 (let ((p value)
b4929f75 604 newlist
ed2f7fc8 605 use-list*
b4929f75
RS
606 anynil)
607 (while (consp p)
608 (let ((q.txt (desktop-internal-v2s (car p))))
609 (or anynil (setq anynil (null (car q.txt))))
610 (setq newlist (cons q.txt newlist)))
611 (setq p (cdr p)))
612 (if p
613 (let ((last (desktop-internal-v2s p))
614 (el (car newlist)))
ed2f7fc8
RS
615 (or anynil (setq anynil (null (car last))))
616 (or anynil
617 (setq newlist (cons '(must . ".") newlist)))
618 (setq use-list* t)
619 (setq newlist (cons last newlist))))
b4929f75
RS
620 (setq newlist (nreverse newlist))
621 (if anynil
622 (cons nil
ed2f7fc8 623 (concat (if use-list* "(desktop-list* " "(list ")
b4929f75
RS
624 (mapconcat (lambda (el)
625 (if (eq (car el) 'must)
626 (concat "'" (cdr el))
627 (cdr el)))
628 newlist
629 " ")
630 ")"))
631 (cons 'must
632 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
05f1c4ec 633 ((subrp value)
de9e2828 634 (cons nil (concat "(symbol-function '"
05f1c4ec 635 (substring (prin1-to-string value) 7 -1)
de9e2828 636 ")")))
05f1c4ec
JB
637 ((markerp value)
638 (let ((pos (prin1-to-string (marker-position value)))
639 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
de9e2828
RS
640 (cons nil (concat "(let ((mk (make-marker)))"
641 " (add-hook 'desktop-delay-hook"
642 " (list 'lambda '() (list 'set-marker mk "
643 pos " (get-buffer " buf ")))) mk)"))))
644 (t ; save as text
fa379636 645 (cons 'may "\"Unprintable entity\""))))
de9e2828 646
4a2fce7a 647;; ----------------------------------------------------------------------------
05f1c4ec 648(defun desktop-value-to-string (value)
577ed2b2
RS
649 "Convert VALUE to a string that when read evaluates to the same value.
650Not all types of values are supported."
de9e2828
RS
651 (let* ((print-escape-newlines t)
652 (float-output-format nil)
05f1c4ec 653 (quote.txt (desktop-internal-v2s value))
de9e2828
RS
654 (quote (car quote.txt))
655 (txt (cdr quote.txt)))
656 (if (eq quote 'must)
657 (concat "'" txt)
658 txt)))
4a2fce7a 659
ec4c6f22 660;; ----------------------------------------------------------------------------
0e7c8611
RS
661(defun desktop-outvar (varspec)
662 "Output a setq statement for variable VAR to the desktop file.
663The argument VARSPEC may be the variable name VAR (a symbol),
664or a cons cell of the form (VAR . MAX-SIZE),
665which means to truncate VAR's value to at most MAX-SIZE elements
666\(if the value is a list) before saving the value."
667 (let (var size)
668 (if (consp varspec)
669 (setq var (car varspec) size (cdr varspec))
670 (setq var varspec))
671 (if (boundp var)
672 (progn
673 (if (and (integerp size)
674 (> size 0)
675 (listp (eval var)))
47640244 676 (desktop-truncate (eval var) size))
0e7c8611
RS
677 (insert "(setq "
678 (symbol-name var)
679 " "
680 (desktop-value-to-string (symbol-value var))
681 ")\n")))))
4a2fce7a 682
0b9bd504 683;; ----------------------------------------------------------------------------
ec4c6f22 684(defun desktop-save-buffer-p (filename bufname mode &rest dummy)
ebb39555 685 "Return t if buffer should have its state saved in the desktop file.
0b9bd504 686FILENAME is the visited file name, BUFNAME is the buffer name, and
6343ed61 687MODE is the major mode."
fa379636 688 (let ((case-fold-search nil))
80f9f3db 689 (and (not (string-match desktop-buffers-not-to-save bufname))
ebb39555
LH
690 (not (memq mode desktop-modes-not-to-save))
691 (or (and filename
692 (not (string-match desktop-files-not-to-save filename)))
693 (and (eq mode 'dired-mode)
694 (with-current-buffer bufname
695 (not (string-match desktop-files-not-to-save
696 default-directory))))
697 (and (null filename)
698 (with-current-buffer bufname desktop-save-buffer))))))
4a2fce7a 699
0b9bd504 700;; ----------------------------------------------------------------------------
4a2fce7a
RS
701(defun desktop-file-name (filename dirname)
702 "Convert FILENAME to format specified in `desktop-file-name-format'.
703DIRNAME must be the directory in which the desktop file will be saved."
704 (cond
705 ((not filename) nil)
706 ((eq desktop-file-name-format 'tilde)
707 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
708 (cond
709 ((file-name-absolute-p relative-name) relative-name)
710 ((string= "./" relative-name) "~/")
711 ((string= "." relative-name) "~")
712 (t (concat "~/" relative-name)))))
713 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
714 (t (expand-file-name filename))))
e5714620 715
4a2fce7a 716;; ----------------------------------------------------------------------------
6343ed61 717(defun desktop-save (dirname)
c5b31c4d
LH
718 "Save the desktop in a desktop file.
719Parameter DIRNAME specifies where to save the desktop file.
720See also `desktop-base-file-name'."
6343ed61 721 (interactive "DDirectory to save desktop file in: ")
fa379636 722 (run-hooks 'desktop-save-hook)
7bcbf3c2 723 (setq dirname (file-name-as-directory (expand-file-name dirname)))
6343ed61 724 (save-excursion
73b0b745 725 (let ((filename (expand-file-name desktop-base-file-name dirname))
7bcbf3c2
JB
726 (info
727 (mapcar
340db502 728 #'(lambda (b)
7bcbf3c2
JB
729 (set-buffer b)
730 (list
731 (desktop-file-name (buffer-file-name) dirname)
732 (buffer-name)
733 major-mode
734 ;; minor modes
735 (let (ret)
7bfa55b3
LH
736 (mapc
737 #'(lambda (minor-mode)
7bcbf3c2 738 (and
7bfa55b3
LH
739 (boundp minor-mode)
740 (symbol-value minor-mode)
0f4804e7
LH
741 (let* ((special (assq minor-mode desktop-minor-mode-table))
742 (value (cond (special (cadr special))
743 ((functionp minor-mode) minor-mode))))
744 (when value (add-to-list 'ret value)))))
7bcbf3c2
JB
745 (mapcar #'car minor-mode-alist))
746 ret)
747 (point)
748 (list (mark t) mark-active)
749 buffer-read-only
e5780ae1 750 ;; Auxiliary information
ebb39555
LH
751 (when (functionp desktop-save-buffer)
752 (funcall desktop-save-buffer dirname))
7bcbf3c2
JB
753 (let ((locals desktop-locals-to-save)
754 (loclist (buffer-local-variables))
755 (ll))
756 (while locals
757 (let ((here (assq (car locals) loclist)))
758 (if here
759 (setq ll (cons here ll))
760 (when (member (car locals) loclist)
761 (setq ll (cons (car locals) ll)))))
762 (setq locals (cdr locals)))
340db502 763 ll)))
7bcbf3c2 764 (buffer-list)))
150f39ee 765 (eager desktop-restore-eager)
7bcbf3c2 766 (buf (get-buffer-create "*desktop*")))
6343ed61
RS
767 (set-buffer buf)
768 (erase-buffer)
fa379636 769
4a2fce7a 770 (insert
aac42a1b 771 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
4a2fce7a
RS
772 desktop-header
773 ";; Created " (current-time-string) "\n"
774 ";; Desktop file format version " desktop-file-version "\n"
775 ";; Emacs version " emacs-version "\n\n"
776 ";; Global section:\n")
63cd5734 777 (mapc (function desktop-outvar) desktop-globals-to-save)
6343ed61 778 (if (memq 'kill-ring desktop-globals-to-save)
4a2fce7a
RS
779 (insert
780 "(setq kill-ring-yank-pointer (nthcdr "
781 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
782 " kill-ring))\n"))
6343ed61 783
4a2fce7a 784 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
63cd5734 785 (mapc #'(lambda (l)
150f39ee
LH
786 (when (apply 'desktop-save-buffer-p l)
787 (insert "("
788 (if (or (not (integerp eager))
789 (unless (zerop eager)
790 (setq eager (1- eager))
791 t))
792 "desktop-create-buffer"
793 "desktop-append-buffer-args")
794 " "
795 desktop-file-version)
796 (mapc #'(lambda (e)
797 (insert "\n " (desktop-value-to-string e)))
798 l)
799 (insert ")\n\n")))
800 info)
6343ed61 801 (setq default-directory dirname)
7642acca 802 (let ((coding-system-for-write 'emacs-mule))
4a2fce7a 803 (write-region (point-min) (point-max) filename nil 'nomessage))))
6343ed61 804 (setq desktop-dirname dirname))
4a2fce7a 805
0b9bd504 806;; ----------------------------------------------------------------------------
6343ed61 807(defun desktop-remove ()
c5b31c4d
LH
808 "Delete desktop file in `desktop-dirname'.
809This function also sets `desktop-dirname' to nil."
6343ed61 810 (interactive)
c5b31c4d
LH
811 (when desktop-dirname
812 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname)))
813 (setq desktop-dirname nil)
814 (when (file-exists-p filename)
73b0b745 815 (delete-file filename)))))
c5b31c4d 816
150f39ee
LH
817(defvar desktop-buffer-args-list nil
818 "List of args for `desktop-create-buffer'.")
819
820(defvar desktop-lazy-timer nil)
821
0b9bd504 822;; ----------------------------------------------------------------------------
478653c9 823;;;###autoload
c5b31c4d
LH
824(defun desktop-read (&optional dirname)
825 "Read and process the desktop file in directory DIRNAME.
826Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
827directories listed in `desktop-path'. If a desktop file is found, it
cc8b76bf 828is processed and `desktop-after-read-hook' is run. If no desktop file
c5b31c4d
LH
829is found, clear the desktop and run `desktop-no-desktop-file-hook'.
830This function is a no-op when Emacs is running in batch mode.
831It returns t if a desktop file was loaded, nil otherwise."
6343ed61 832 (interactive)
4a2fce7a 833 (unless noninteractive
c5b31c4d
LH
834 (setq desktop-dirname
835 (file-name-as-directory
836 (expand-file-name
837 (or
838 ;; If DIRNAME is specified, use it.
839 (and (< 0 (length dirname)) dirname)
840 ;; Otherwise search desktop file in desktop-path.
841 (let ((dirs desktop-path))
842 (while
843 (and
844 dirs
845 (not
846 (file-exists-p (expand-file-name desktop-base-file-name (car dirs)))))
847 (setq dirs (cdr dirs)))
848 (and dirs (car dirs)))
849 ;; If not found and `desktop-path' is non-nil, use its first element.
850 (and desktop-path (car desktop-path))
851 ;; Default: Home directory.
852 "~"))))
853 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
854 ;; Desktop file found, process it.
e5780ae1
LH
855 (let ((desktop-first-buffer nil)
856 (desktop-buffer-ok-count 0)
857 (desktop-buffer-fail-count 0))
150f39ee 858 (setq desktop-lazy-timer nil)
c5b31c4d
LH
859 ;; Evaluate desktop buffer.
860 (load (expand-file-name desktop-base-file-name desktop-dirname) t t t)
861 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
862 ;; We want buffers existing prior to evaluating the desktop (and not reused)
863 ;; to be placed at the end of the buffer list, so we move them here.
48aa4dfc
LH
864 (mapc 'bury-buffer
865 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
c5b31c4d
LH
866 (switch-to-buffer (car (buffer-list)))
867 (run-hooks 'desktop-delay-hook)
868 (setq desktop-delay-hook nil)
869 (run-hooks 'desktop-after-read-hook)
150f39ee 870 (message "Desktop: %d buffer%s restored%s%s."
e5780ae1
LH
871 desktop-buffer-ok-count
872 (if (= 1 desktop-buffer-ok-count) "" "s")
873 (if (< 0 desktop-buffer-fail-count)
874 (format ", %d failed to restore" desktop-buffer-fail-count)
150f39ee
LH
875 "")
876 (if desktop-buffer-args-list
877 (format ", %d to restore lazily"
878 (length desktop-buffer-args-list))
e5780ae1 879 ""))
c5b31c4d
LH
880 t)
881 ;; No desktop file found.
882 (desktop-clear)
883 (let ((default-directory desktop-dirname))
884 (run-hooks 'desktop-no-desktop-file-hook))
885 (message "No desktop file.")
886 nil)))
4a2fce7a 887
0b9bd504 888;; ----------------------------------------------------------------------------
c5b31c4d 889;; Maintained for backward compatibility
478653c9 890;;;###autoload
6343ed61 891(defun desktop-load-default ()
577ed2b2 892 "Load the `default' start-up library manually.
c5b31c4d 893Also inhibit further loading of it."
b89c5a72
JB
894 (unless inhibit-default-init ; safety check
895 (load "default" t t)
896 (setq inhibit-default-init t)))
897(make-obsolete 'desktop-load-default
898 'desktop-save-mode "22.1")
4a2fce7a
RS
899
900;; ----------------------------------------------------------------------------
901;;;###autoload
c5b31c4d
LH
902(defun desktop-change-dir (dirname)
903 "Change to desktop saved in DIRNAME.
904Kill the desktop as specified by variables `desktop-save-mode' and
905`desktop-save', then clear the desktop and load the desktop file in
906directory DIRNAME."
907 (interactive "DChange to directory: ")
908 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
4a2fce7a
RS
909 (desktop-kill)
910 (desktop-clear)
c5b31c4d 911 (desktop-read dirname))
bf247b6e 912
c5b31c4d 913;; ----------------------------------------------------------------------------
4a2fce7a 914;;;###autoload
c5b31c4d
LH
915(defun desktop-save-in-desktop-dir ()
916 "Save the desktop in directory `desktop-dirname'."
4a2fce7a
RS
917 (interactive)
918 (if desktop-dirname
b89c5a72 919 (desktop-save desktop-dirname)
4a2fce7a
RS
920 (call-interactively 'desktop-save))
921 (message "Desktop saved in %s" desktop-dirname))
922
923;; ----------------------------------------------------------------------------
924;;;###autoload
925(defun desktop-revert ()
926 "Revert to the last loaded desktop."
927 (interactive)
c5b31c4d
LH
928 (unless desktop-dirname
929 (error "Unknown desktop directory"))
930 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
931 (error "No desktop file found"))
932 (desktop-clear)
933 (desktop-read desktop-dirname))
4a2fce7a 934
0b9bd504 935;; ----------------------------------------------------------------------------
e5780ae1
LH
936(defun desktop-restore-file-buffer (desktop-buffer-file-name
937 desktop-buffer-name
938 desktop-buffer-misc)
939 "Restore a file buffer."
940 (eval-when-compile ; Just to silence the byte compiler
941 (defvar desktop-buffer-major-mode)
942 (defvar desktop-buffer-locals))
569c754e
RS
943 (if desktop-buffer-file-name
944 (if (or (file-exists-p desktop-buffer-file-name)
e5780ae1
LH
945 (let ((msg (format "Desktop: File \"%s\" no longer exists."
946 desktop-buffer-file-name)))
947 (if desktop-missing-file-warning
948 (y-or-n-p (concat msg " Re-create? "))
8a26c165 949 (message "%s" msg)
e5780ae1 950 nil)))
bd382a27
LH
951 (let* ((auto-insert nil) ; Disable auto insertion
952 (coding-system-for-read
953 (or coding-system-for-read
954 (cdr (assq 'buffer-file-coding-system
955 desktop-buffer-locals))))
956 (buf (find-file-noselect desktop-buffer-file-name)))
80f9f3db
SM
957 (condition-case nil
958 (switch-to-buffer buf)
0a8c8225 959 (error (pop-to-buffer buf)))
84b538ec
JB
960 (and (not (eq major-mode desktop-buffer-major-mode))
961 (functionp desktop-buffer-major-mode)
962 (funcall desktop-buffer-major-mode))
0a8c8225 963 buf)
e5780ae1 964 nil)))
4a2fce7a 965
0f4804e7
LH
966(defun desktop-load-file (function)
967 "Load the file where auto loaded FUNCTION is defined."
97b3a214
LH
968 (when function
969 (let ((fcell (symbol-function function)))
970 (when (and (listp fcell)
971 (eq 'autoload (car fcell)))
972 (load (cadr fcell))))))
0f4804e7 973
0b9bd504 974;; ----------------------------------------------------------------------------
eb982898
JL
975;; Create a buffer, load its file, set its mode, ...;
976;; called from Desktop file only.
1d500ca6 977
0f4804e7
LH
978;; Just to silence the byte compiler.
979(eval-when-compile
980 (defvar desktop-first-buffer)) ; Dynamically bound in `desktop-read'
4a2fce7a 981
340db502
LH
982(defun desktop-create-buffer
983 (desktop-file-version
984 desktop-buffer-file-name
985 desktop-buffer-name
986 desktop-buffer-major-mode
987 desktop-buffer-minor-modes
988 desktop-buffer-point
989 desktop-buffer-mark
990 desktop-buffer-read-only
991 desktop-buffer-misc
992 &optional
993 desktop-buffer-locals)
e5780ae1
LH
994 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
995 (eval-when-compile
996 (defvar desktop-buffer-ok-count)
997 (defvar desktop-buffer-fail-count))
4a2fce7a
RS
998 ;; To make desktop files with relative file names possible, we cannot
999 ;; allow `default-directory' to change. Therefore we save current buffer.
1000 (save-current-buffer
0f4804e7
LH
1001 ;; Give major mode module a chance to add a handler.
1002 (desktop-load-file desktop-buffer-major-mode)
340db502
LH
1003 (let ((buffer-list (buffer-list))
1004 (result
1005 (condition-case err
1006 (funcall (or (cdr (assq desktop-buffer-major-mode
1007 desktop-buffer-mode-handlers))
1008 'desktop-restore-file-buffer)
1009 desktop-buffer-file-name
1010 desktop-buffer-name
1011 desktop-buffer-misc)
1012 (error
1013 (message "Desktop: Can't load buffer %s: %s"
1014 desktop-buffer-name
1015 (error-message-string err))
1016 (when desktop-missing-file-warning (sit-for 1))
1017 nil))))
e5780ae1
LH
1018 (if (bufferp result)
1019 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1020 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1021 (setq result nil))
7bcbf3c2
JB
1022 ;; Restore buffer list order with new buffer at end. Don't change
1023 ;; the order for old desktop files (old desktop module behaviour).
4a2fce7a 1024 (unless (< desktop-file-version 206)
48aa4dfc 1025 (mapc 'bury-buffer buffer-list)
7bcbf3c2 1026 (when result (bury-buffer result)))
4a2fce7a 1027 (when result
7bcbf3c2
JB
1028 (unless (or desktop-first-buffer (< desktop-file-version 206))
1029 (setq desktop-first-buffer result))
4a2fce7a
RS
1030 (set-buffer result)
1031 (unless (equal (buffer-name) desktop-buffer-name)
1032 (rename-buffer desktop-buffer-name))
1033 ;; minor modes
340db502
LH
1034 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1035 (auto-fill-mode 1))
1036 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1037 (auto-fill-mode 0))
1038 (t
63cd5734 1039 (mapc #'(lambda (minor-mode)
0f4804e7
LH
1040 ;; Give minor mode module a chance to add a handler.
1041 (desktop-load-file minor-mode)
1042 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1043 (if handler
1044 (funcall handler desktop-buffer-locals)
1045 (when (functionp minor-mode)
1046 (funcall minor-mode 1)))))
63cd5734 1047 desktop-buffer-minor-modes)))
0f4804e7 1048 ;; Even though point and mark are non-nil when written by `desktop-save',
73b0b745 1049 ;; they may be modified by handlers wanting to set point or mark themselves.
e5780ae1
LH
1050 (when desktop-buffer-point
1051 (goto-char
1052 (condition-case err
1053 ;; Evaluate point. Thus point can be something like '(search-forward ...
1054 (eval desktop-buffer-point)
1055 (error (message "%s" (error-message-string err)) 1))))
4a2fce7a
RS
1056 (when desktop-buffer-mark
1057 (if (consp desktop-buffer-mark)
1058 (progn
1059 (set-mark (car desktop-buffer-mark))
1060 (setq mark-active (car (cdr desktop-buffer-mark))))
1061 (set-mark desktop-buffer-mark)))
1062 ;; Never override file system if the file really is read-only marked.
1063 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1064 (while desktop-buffer-locals
1065 (let ((this (car desktop-buffer-locals)))
1066 (if (consp this)
1067 ;; an entry of this form `(symbol . value)'
1068 (progn
1069 (make-local-variable (car this))
1070 (set (car this) (cdr this)))
1071 ;; an entry of the form `symbol'
1072 (make-local-variable this)
1073 (makunbound this)))
1074 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
ec4c6f22 1075
4a2fce7a 1076;; ----------------------------------------------------------------------------
ec4c6f22 1077;; Backward compatibility -- update parameters to 205 standards.
569c754e
RS
1078(defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1079 desktop-buffer-major-mode
2699e23e 1080 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
569c754e 1081 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
2699e23e
RS
1082 desktop-buffer-major-mode (cdr mim) pt mk ro
1083 desktop-buffer-misc
ec4c6f22
RS
1084 (list (cons 'truncate-lines tl)
1085 (cons 'fill-column fc)
1086 (cons 'case-fold-search cfs)
1087 (cons 'case-replace cr)
1088 (cons 'overwrite-mode (car mim)))))
84b538ec 1089
150f39ee 1090(defun desktop-append-buffer-args (&rest args)
cc8b76bf 1091 "Append ARGS at end of `desktop-buffer-args-list'.
150f39ee
LH
1092ARGS must be an argument list for `desktop-create-buffer'."
1093 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1094 (unless desktop-lazy-timer
1095 (setq desktop-lazy-timer
1096 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1097
1098(defun desktop-lazy-create-buffer ()
1099 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1100 (when desktop-buffer-args-list
1101 (let* ((remaining (length desktop-buffer-args-list))
1102 (args (pop desktop-buffer-args-list))
1103 (buffer-name (nth 2 args))
1104 (msg (format "Desktop lazily opening %s (%s remaining)..."
1105 buffer-name remaining)))
1106 (when desktop-lazy-verbose
8a26c165 1107 (message "%s" msg))
150f39ee
LH
1108 (let ((desktop-first-buffer nil)
1109 (desktop-buffer-ok-count 0)
1110 (desktop-buffer-fail-count 0))
1111 (apply 'desktop-create-buffer args)
1112 (run-hooks 'desktop-delay-hook)
1113 (setq desktop-delay-hook nil)
1114 (bury-buffer (get-buffer buffer-name))
1115 (when desktop-lazy-verbose
1116 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1117
1118(defun desktop-idle-create-buffers ()
1119 "Create buffers until the user does something, then stop.
1120If there are no buffers left to create, kill the timer."
1121 (let ((repeat 1))
1122 (while (and repeat desktop-buffer-args-list)
1123 (save-window-excursion
1124 (desktop-lazy-create-buffer))
1125 (setq repeat (sit-for 0.2))
1126 (unless desktop-buffer-args-list
1127 (cancel-timer desktop-lazy-timer)
1128 (setq desktop-lazy-timer nil)
1129 (message "Lazy desktop load complete")
1130 (sit-for 3)
1131 (message "")))))
1132
1133(defun desktop-lazy-complete ()
1134 "Run the desktop load to completion."
1135 (interactive)
1136 (let ((desktop-lazy-verbose t))
1137 (while desktop-buffer-args-list
1138 (save-window-excursion
1139 (desktop-lazy-create-buffer)))
1140 (message "Lazy desktop load complete")))
1141
1142(defun desktop-lazy-abort ()
1143 "Abort lazy loading of the desktop."
1144 (interactive)
1145 (when desktop-lazy-timer
1146 (cancel-timer desktop-lazy-timer)
1147 (setq desktop-lazy-timer nil))
1148 (when desktop-buffer-args-list
1149 (setq desktop-buffer-args-list nil)
1150 (when (interactive-p)
1151 (message "Lazy desktop load aborted"))))
1152
0b9bd504 1153;; ----------------------------------------------------------------------------
c5b31c4d 1154;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
4a2fce7a
RS
1155;; command line, we do the rest of what it takes to use desktop, but do it
1156;; after finishing loading the init file.
1157;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1158;; functions are processed after `after-init-hook'.
1159(add-hook
1160 'after-init-hook
1161 '(lambda ()
1162 (let ((key "--no-desktop"))
4b217d46
LH
1163 (when (member key command-line-args)
1164 (setq command-line-args (delete key command-line-args))
1165 (setq desktop-save-mode nil)))
1166 (when desktop-save-mode (desktop-read))))
813dbb2d 1167
ab1d55ea 1168(provide 'desktop)
6343ed61 1169
ab5796a9 1170;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
80f9f3db 1171;;; desktop.el ends here