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