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