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