Include window.h.
[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
6b61353c
KH
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;;
6b61353c 43;; (desktop-save-mode 1)
0b9bd504 44;;
6b61353c
KH
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"
6b61353c 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
6b61353c
KH
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."
6b61353c
KH
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
6b61353c
KH
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.
6b61353c 176The desktop is never saved when `desktop-save-mode' is nil.
cc8b76bf 177The variables `desktop-dirname' and `desktop-base-file-name'
6b61353c 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")
6b61353c 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
6b61353c 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
6b61353c 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
6b61353c 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)
6b61353c
KH
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."
6b61353c
KH
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
6b61353c 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
6b61353c 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:
4a2fce7a
RS
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
ec4c6f22 406
0f4804e7
LH
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
6b61353c 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;; ----------------------------------------------------------------------------
6b61353c 500(defun desktop-truncate (list n)
ec4c6f22 501 "Truncate LIST to at most N elements destructively."
6b61353c 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 ()
6b61353c 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;; ----------------------------------------------------------------------------
6b61353c 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))
6b61353c
KH
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))))
6b61353c
KH
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))
6b61353c 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 " ") "]")))))
6b61353c
KH
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 " ") ")")))))
6b61353c 643 ((subrp value)
de9e2828 644 (cons nil (concat "(symbol-function '"
6b61353c 645 (substring (prin1-to-string value) 7 -1)
de9e2828 646 ")")))
6b61353c
KH
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;; ----------------------------------------------------------------------------
6b61353c 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)
6b61353c 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)
6b61353c
KH
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
4ed925c6
MB
779 (insert
780 ";; -*- mode: emacs-lisp; coding: utf-8-emacs; -*-\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 'utf-8-emacs))
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 ()
6b61353c
KH
818 "Delete desktop file in `desktop-dirname'.
819This function also sets `desktop-dirname' to nil."
6343ed61 820 (interactive)
6b61353c 821 (when desktop-dirname
11425834 822 (let ((filename (desktop-full-file-name)))
6b61353c
KH
823 (setq desktop-dirname nil)
824 (when (file-exists-p filename)
73b0b745 825 (delete-file filename)))))
6b61353c 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
6b61353c
KH
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
6b61353c
KH
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
6b61353c 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))
6b61353c 862 ;; Desktop file found, process it.
e5780ae1
LH
863 (let ((desktop-first-buffer nil)
864 (desktop-buffer-ok-count 0)
865 (desktop-buffer-fail-count 0))
150f39ee 866 (setq desktop-lazy-timer nil)
6b61353c 867 ;; Evaluate desktop buffer.
11425834 868 (load (desktop-full-file-name) t t t)
6b61353c
KH
869 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
870 ;; We want buffers existing prior to evaluating the desktop (and not reused)
871 ;; to be placed at the end of the buffer list, so we move them here.
48aa4dfc
LH
872 (mapc 'bury-buffer
873 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
6b61353c
KH
874 (switch-to-buffer (car (buffer-list)))
875 (run-hooks 'desktop-delay-hook)
876 (setq desktop-delay-hook nil)
877 (run-hooks 'desktop-after-read-hook)
150f39ee 878 (message "Desktop: %d buffer%s restored%s%s."
e5780ae1
LH
879 desktop-buffer-ok-count
880 (if (= 1 desktop-buffer-ok-count) "" "s")
881 (if (< 0 desktop-buffer-fail-count)
882 (format ", %d failed to restore" desktop-buffer-fail-count)
150f39ee
LH
883 "")
884 (if desktop-buffer-args-list
885 (format ", %d to restore lazily"
886 (length desktop-buffer-args-list))
e5780ae1 887 ""))
6b61353c
KH
888 t)
889 ;; No desktop file found.
890 (desktop-clear)
891 (let ((default-directory desktop-dirname))
892 (run-hooks 'desktop-no-desktop-file-hook))
893 (message "No desktop file.")
894 nil)))
4a2fce7a 895
0b9bd504 896;; ----------------------------------------------------------------------------
6b61353c 897;; Maintained for backward compatibility
478653c9 898;;;###autoload
6343ed61 899(defun desktop-load-default ()
577ed2b2 900 "Load the `default' start-up library manually.
6b61353c 901Also inhibit further loading of it."
b89c5a72
JB
902 (unless inhibit-default-init ; safety check
903 (load "default" t t)
904 (setq inhibit-default-init t)))
905(make-obsolete 'desktop-load-default
906 'desktop-save-mode "22.1")
4a2fce7a
RS
907
908;; ----------------------------------------------------------------------------
909;;;###autoload
6b61353c
KH
910(defun desktop-change-dir (dirname)
911 "Change to desktop saved in DIRNAME.
912Kill the desktop as specified by variables `desktop-save-mode' and
913`desktop-save', then clear the desktop and load the desktop file in
914directory DIRNAME."
915 (interactive "DChange to directory: ")
916 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
4a2fce7a
RS
917 (desktop-kill)
918 (desktop-clear)
6b61353c 919 (desktop-read dirname))
bf247b6e 920
6b61353c 921;; ----------------------------------------------------------------------------
4a2fce7a 922;;;###autoload
6b61353c
KH
923(defun desktop-save-in-desktop-dir ()
924 "Save the desktop in directory `desktop-dirname'."
4a2fce7a
RS
925 (interactive)
926 (if desktop-dirname
b89c5a72 927 (desktop-save desktop-dirname)
4a2fce7a
RS
928 (call-interactively 'desktop-save))
929 (message "Desktop saved in %s" desktop-dirname))
930
931;; ----------------------------------------------------------------------------
932;;;###autoload
933(defun desktop-revert ()
934 "Revert to the last loaded desktop."
935 (interactive)
6b61353c
KH
936 (unless desktop-dirname
937 (error "Unknown desktop directory"))
11425834 938 (unless (file-exists-p (desktop-full-file-name))
6b61353c
KH
939 (error "No desktop file found"))
940 (desktop-clear)
941 (desktop-read desktop-dirname))
4a2fce7a 942
0b9bd504 943;; ----------------------------------------------------------------------------
e5780ae1
LH
944(defun desktop-restore-file-buffer (desktop-buffer-file-name
945 desktop-buffer-name
946 desktop-buffer-misc)
947 "Restore a file buffer."
948 (eval-when-compile ; Just to silence the byte compiler
949 (defvar desktop-buffer-major-mode)
950 (defvar desktop-buffer-locals))
569c754e
RS
951 (if desktop-buffer-file-name
952 (if (or (file-exists-p desktop-buffer-file-name)
e5780ae1
LH
953 (let ((msg (format "Desktop: File \"%s\" no longer exists."
954 desktop-buffer-file-name)))
955 (if desktop-missing-file-warning
6c27fdb9 956 (y-or-n-p (concat msg " Re-create buffer? "))
8a26c165 957 (message "%s" msg)
e5780ae1 958 nil)))
6b61353c
KH
959 (let* ((auto-insert nil) ; Disable auto insertion
960 (coding-system-for-read
961 (or coding-system-for-read
962 (cdr (assq 'buffer-file-coding-system
963 desktop-buffer-locals))))
964 (buf (find-file-noselect desktop-buffer-file-name)))
80f9f3db
SM
965 (condition-case nil
966 (switch-to-buffer buf)
0a8c8225 967 (error (pop-to-buffer buf)))
84b538ec
JB
968 (and (not (eq major-mode desktop-buffer-major-mode))
969 (functionp desktop-buffer-major-mode)
970 (funcall desktop-buffer-major-mode))
0a8c8225 971 buf)
e5780ae1 972 nil)))
4a2fce7a 973
0f4804e7
LH
974(defun desktop-load-file (function)
975 "Load the file where auto loaded FUNCTION is defined."
97b3a214 976 (when function
07696982 977 (let ((fcell (and (fboundp function) (symbol-function function))))
97b3a214
LH
978 (when (and (listp fcell)
979 (eq 'autoload (car fcell)))
980 (load (cadr fcell))))))
0f4804e7 981
0b9bd504 982;; ----------------------------------------------------------------------------
eb982898
JL
983;; Create a buffer, load its file, set its mode, ...;
984;; called from Desktop file only.
1d500ca6 985
0f4804e7
LH
986;; Just to silence the byte compiler.
987(eval-when-compile
988 (defvar desktop-first-buffer)) ; Dynamically bound in `desktop-read'
4a2fce7a 989
340db502
LH
990(defun desktop-create-buffer
991 (desktop-file-version
992 desktop-buffer-file-name
993 desktop-buffer-name
994 desktop-buffer-major-mode
995 desktop-buffer-minor-modes
996 desktop-buffer-point
997 desktop-buffer-mark
998 desktop-buffer-read-only
999 desktop-buffer-misc
1000 &optional
1001 desktop-buffer-locals)
e5780ae1
LH
1002 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
1003 (eval-when-compile
1004 (defvar desktop-buffer-ok-count)
1005 (defvar desktop-buffer-fail-count))
4a2fce7a
RS
1006 ;; To make desktop files with relative file names possible, we cannot
1007 ;; allow `default-directory' to change. Therefore we save current buffer.
1008 (save-current-buffer
0f4804e7
LH
1009 ;; Give major mode module a chance to add a handler.
1010 (desktop-load-file desktop-buffer-major-mode)
340db502
LH
1011 (let ((buffer-list (buffer-list))
1012 (result
1013 (condition-case err
1014 (funcall (or (cdr (assq desktop-buffer-major-mode
1015 desktop-buffer-mode-handlers))
1016 'desktop-restore-file-buffer)
1017 desktop-buffer-file-name
1018 desktop-buffer-name
1019 desktop-buffer-misc)
1020 (error
1021 (message "Desktop: Can't load buffer %s: %s"
1022 desktop-buffer-name
1023 (error-message-string err))
1024 (when desktop-missing-file-warning (sit-for 1))
1025 nil))))
e5780ae1
LH
1026 (if (bufferp result)
1027 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1028 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1029 (setq result nil))
7bcbf3c2
JB
1030 ;; Restore buffer list order with new buffer at end. Don't change
1031 ;; the order for old desktop files (old desktop module behaviour).
4a2fce7a 1032 (unless (< desktop-file-version 206)
48aa4dfc 1033 (mapc 'bury-buffer buffer-list)
7bcbf3c2 1034 (when result (bury-buffer result)))
4a2fce7a 1035 (when result
7bcbf3c2
JB
1036 (unless (or desktop-first-buffer (< desktop-file-version 206))
1037 (setq desktop-first-buffer result))
4a2fce7a
RS
1038 (set-buffer result)
1039 (unless (equal (buffer-name) desktop-buffer-name)
1040 (rename-buffer desktop-buffer-name))
1041 ;; minor modes
340db502
LH
1042 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1043 (auto-fill-mode 1))
1044 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1045 (auto-fill-mode 0))
1046 (t
63cd5734 1047 (mapc #'(lambda (minor-mode)
0f4804e7
LH
1048 ;; Give minor mode module a chance to add a handler.
1049 (desktop-load-file minor-mode)
1050 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1051 (if handler
1052 (funcall handler desktop-buffer-locals)
1053 (when (functionp minor-mode)
1054 (funcall minor-mode 1)))))
63cd5734 1055 desktop-buffer-minor-modes)))
0f4804e7 1056 ;; Even though point and mark are non-nil when written by `desktop-save',
73b0b745 1057 ;; they may be modified by handlers wanting to set point or mark themselves.
e5780ae1
LH
1058 (when desktop-buffer-point
1059 (goto-char
1060 (condition-case err
1061 ;; Evaluate point. Thus point can be something like '(search-forward ...
1062 (eval desktop-buffer-point)
1063 (error (message "%s" (error-message-string err)) 1))))
4a2fce7a
RS
1064 (when desktop-buffer-mark
1065 (if (consp desktop-buffer-mark)
1066 (progn
1067 (set-mark (car desktop-buffer-mark))
1068 (setq mark-active (car (cdr desktop-buffer-mark))))
1069 (set-mark desktop-buffer-mark)))
1070 ;; Never override file system if the file really is read-only marked.
1071 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1072 (while desktop-buffer-locals
1073 (let ((this (car desktop-buffer-locals)))
1074 (if (consp this)
1075 ;; an entry of this form `(symbol . value)'
1076 (progn
1077 (make-local-variable (car this))
1078 (set (car this) (cdr this)))
1079 ;; an entry of the form `symbol'
1080 (make-local-variable this)
1081 (makunbound this)))
1082 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
ec4c6f22 1083
4a2fce7a 1084;; ----------------------------------------------------------------------------
ec4c6f22 1085;; Backward compatibility -- update parameters to 205 standards.
569c754e
RS
1086(defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1087 desktop-buffer-major-mode
2699e23e 1088 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
569c754e 1089 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
2699e23e
RS
1090 desktop-buffer-major-mode (cdr mim) pt mk ro
1091 desktop-buffer-misc
ec4c6f22
RS
1092 (list (cons 'truncate-lines tl)
1093 (cons 'fill-column fc)
1094 (cons 'case-fold-search cfs)
1095 (cons 'case-replace cr)
1096 (cons 'overwrite-mode (car mim)))))
84b538ec 1097
150f39ee 1098(defun desktop-append-buffer-args (&rest args)
cc8b76bf 1099 "Append ARGS at end of `desktop-buffer-args-list'.
150f39ee
LH
1100ARGS must be an argument list for `desktop-create-buffer'."
1101 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1102 (unless desktop-lazy-timer
1103 (setq desktop-lazy-timer
1104 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1105
1106(defun desktop-lazy-create-buffer ()
1107 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1108 (when desktop-buffer-args-list
1109 (let* ((remaining (length desktop-buffer-args-list))
1110 (args (pop desktop-buffer-args-list))
1111 (buffer-name (nth 2 args))
1112 (msg (format "Desktop lazily opening %s (%s remaining)..."
1113 buffer-name remaining)))
1114 (when desktop-lazy-verbose
8a26c165 1115 (message "%s" msg))
150f39ee
LH
1116 (let ((desktop-first-buffer nil)
1117 (desktop-buffer-ok-count 0)
1118 (desktop-buffer-fail-count 0))
1119 (apply 'desktop-create-buffer args)
1120 (run-hooks 'desktop-delay-hook)
1121 (setq desktop-delay-hook nil)
1122 (bury-buffer (get-buffer buffer-name))
1123 (when desktop-lazy-verbose
1124 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1125
1126(defun desktop-idle-create-buffers ()
1127 "Create buffers until the user does something, then stop.
1128If there are no buffers left to create, kill the timer."
1129 (let ((repeat 1))
1130 (while (and repeat desktop-buffer-args-list)
1131 (save-window-excursion
1132 (desktop-lazy-create-buffer))
1133 (setq repeat (sit-for 0.2))
1134 (unless desktop-buffer-args-list
1135 (cancel-timer desktop-lazy-timer)
1136 (setq desktop-lazy-timer nil)
1137 (message "Lazy desktop load complete")
1138 (sit-for 3)
1139 (message "")))))
1140
1141(defun desktop-lazy-complete ()
1142 "Run the desktop load to completion."
1143 (interactive)
1144 (let ((desktop-lazy-verbose t))
1145 (while desktop-buffer-args-list
1146 (save-window-excursion
1147 (desktop-lazy-create-buffer)))
1148 (message "Lazy desktop load complete")))
1149
1150(defun desktop-lazy-abort ()
1151 "Abort lazy loading of the desktop."
1152 (interactive)
1153 (when desktop-lazy-timer
1154 (cancel-timer desktop-lazy-timer)
1155 (setq desktop-lazy-timer nil))
1156 (when desktop-buffer-args-list
1157 (setq desktop-buffer-args-list nil)
1158 (when (interactive-p)
1159 (message "Lazy desktop load aborted"))))
1160
0b9bd504 1161;; ----------------------------------------------------------------------------
6b61353c 1162;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
4a2fce7a
RS
1163;; command line, we do the rest of what it takes to use desktop, but do it
1164;; after finishing loading the init file.
1165;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1166;; functions are processed after `after-init-hook'.
1167(add-hook
1168 'after-init-hook
1169 '(lambda ()
1170 (let ((key "--no-desktop"))
4b217d46
LH
1171 (when (member key command-line-args)
1172 (setq command-line-args (delete key command-line-args))
1173 (setq desktop-save-mode nil)))
1174 (when desktop-save-mode (desktop-read))))
813dbb2d 1175
ab1d55ea 1176(provide 'desktop)
6343ed61 1177
6b61353c 1178;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
80f9f3db 1179;;; desktop.el ends here