1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993-1995, 1997, 2000-2013 Free Software Foundation,
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Keywords: convenience
8 ;; Favorite-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 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
27 ;; Save the Desktop, i.e.,
28 ;; - some global variables
29 ;; - the list of buffers with associated files. For each buffer also
31 ;; - the default directory
33 ;; - the mark & mark-active
35 ;; - some local variables
37 ;; To use this, use customize to turn on desktop-save-mode or add the
38 ;; following line somewhere in your init file:
40 ;; (desktop-save-mode 1)
42 ;; For further usage information, look at the section
43 ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
45 ;; When the desktop module is loaded, the function `desktop-kill' is
46 ;; added to the `kill-emacs-hook'. This function is responsible for
47 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
48 ;; function is added to the `after-init-hook'. This function is
49 ;; responsible for loading the desktop when Emacs is started.
53 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
54 ;; are supplied to handle special major and minor modes respectively.
55 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
56 ;; to restore a desktop buffer. Elements must have the form
58 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
60 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
61 ;; evaluates the desktop file. Buffers with a major mode not specified here,
62 ;; are restored by the default handler `desktop-restore-file-buffer'.
63 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
64 ;; non-standard minor modes. Elements must have the form
66 ;; (MINOR-MODE . RESTORE-FUNCTION).
68 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
69 ;; Minor modes not specified here, are restored by the standard minor mode
70 ;; function. If you write a module that defines a major or minor mode that
71 ;; needs a special handler, then place code like
73 ;; (defun foo-restore-desktop-buffer
75 ;; (add-to-list 'desktop-buffer-mode-handlers
76 ;; '(foo-mode . foo-restore-desktop-buffer))
80 ;; (defun bar-desktop-restore
82 ;; (add-to-list 'desktop-minor-mode-handlers
83 ;; '(bar-mode . bar-desktop-restore))
85 ;; in the module itself, and make sure that the mode function is
86 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
87 ;; `desktop-minor-mode-handlers' for more info.
91 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
92 ;; manual) are handled in the following way:
93 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
94 ;; saves as `desktop-minor-modes' the list of names of those variables in
95 ;; `minor-mode-alist' that have a non-nil value.
96 ;; When `desktop-create' restores the buffer, each of the symbols in
97 ;; `desktop-minor-modes' is called as function with parameter 1.
98 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
99 ;; are used to handle non-conventional minor modes. `desktop-save' uses
100 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
101 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
102 ;; variable name that is different form its function name, an entry
104 ;; (NAME RESTORE-FUNCTION)
106 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
107 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
108 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
109 ;; function different from the usual minor mode function.
110 ;; ---------------------------------------------------------------------------
112 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
113 ;; in your home directory is used for that. Saving global default values
114 ;; for buffers is an example of misuse.
116 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
117 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
118 ;; things you did not mean to keep. Use M-x desktop-clear RET.
120 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
121 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
122 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
123 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
124 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
125 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
126 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
127 ;; ---------------------------------------------------------------------------
130 ;; Save window configuration.
131 ;; Recognize more minor modes.
136 (defvar desktop-file-version
"206"
137 "Version number of desktop file format.
138 Written into the desktop file and used at desktop read to provide
139 backward compatibility.")
141 ;; ----------------------------------------------------------------------------
142 ;; USER OPTIONS -- settings you might want to play with.
143 ;; ----------------------------------------------------------------------------
145 (defgroup desktop nil
146 "Save status of Emacs when you exit."
149 ;; Maintained for backward compatibility
150 (define-obsolete-variable-alias 'desktop-enable
'desktop-save-mode
"22.1")
152 (define-minor-mode desktop-save-mode
153 "Toggle desktop saving (Desktop Save mode).
154 With a prefix argument ARG, enable Desktop Save mode if ARG is
155 positive, and disable it otherwise. If called from Lisp, enable
156 the mode if ARG is omitted or nil.
158 If Desktop Save mode is enabled, the state of Emacs is saved from
159 one session to another. See variable `desktop-save' and function
160 `desktop-read' for details."
164 (defun desktop-save-mode-off ()
165 "Disable `desktop-save-mode'. Provided for use in hooks."
166 (desktop-save-mode 0))
168 (defcustom desktop-save
'ask-if-new
169 "Specifies whether the desktop should be saved when it is killed.
170 A desktop is killed when the user changes desktop or quits Emacs.
174 ask-if-new -- ask if no desktop file exists, otherwise just save.
175 ask-if-exists -- ask if desktop file exists, otherwise don't save.
176 if-exists -- save if desktop file exists, otherwise don't save.
178 The desktop is never saved when `desktop-save-mode' is nil.
179 The variables `desktop-dirname' and `desktop-base-file-name'
180 determine where the desktop is saved."
183 (const :tag
"Always save" t
)
184 (const :tag
"Always ask" ask
)
185 (const :tag
"Ask if desktop file is new, else do save" ask-if-new
)
186 (const :tag
"Ask if desktop file exists, else don't save" ask-if-exists
)
187 (const :tag
"Save if desktop file exists, else don't" if-exists
)
188 (const :tag
"Never save" nil
))
192 (defcustom desktop-auto-save-timeout nil
193 "Number of seconds between auto-saves of the desktop.
194 Zero or nil means disable timer-based auto-saving."
195 :type
'(choice (const :tag
"Off" nil
)
196 (integer :tag
"Seconds"))
197 :set
(lambda (symbol value
)
198 (set-default symbol value
)
200 (desktop-auto-save-set-timer)
205 (defcustom desktop-load-locked-desktop
'ask
206 "Specifies whether the desktop should be loaded if locked.
211 If the value is nil, or `ask' and the user chooses not to load the desktop,
212 the normal hook `desktop-not-loaded-hook' is run."
215 (const :tag
"Load anyway" t
)
216 (const :tag
"Don't load" nil
)
217 (const :tag
"Ask the user" ask
))
221 (define-obsolete-variable-alias 'desktop-basefilename
222 'desktop-base-file-name
"22.1")
224 (defcustom desktop-base-file-name
225 (convert-standard-filename ".emacs.desktop")
226 "Name of file for Emacs desktop, excluding the directory part."
230 (defcustom desktop-base-lock-name
231 (convert-standard-filename ".emacs.desktop.lock")
232 "Name of lock file for Emacs desktop, excluding the directory part."
237 (defcustom desktop-path
(list user-emacs-directory
"~")
238 "List of directories to search for the desktop file.
239 The base name of the file is specified in `desktop-base-file-name'."
240 :type
'(repeat directory
)
242 :version
"23.2") ; user-emacs-directory added
244 (defcustom desktop-missing-file-warning nil
245 "If non-nil, offer to recreate the buffer of a deleted file.
246 Also pause for a moment to display message about errors signaled in
247 `desktop-buffer-mode-handlers'.
249 If nil, just print error messages in the message buffer."
254 (defcustom desktop-no-desktop-file-hook nil
255 "Normal hook run when `desktop-read' can't find a desktop file.
256 Run in the directory in which the desktop file was sought.
257 May be used to show a dired buffer."
262 (defcustom desktop-not-loaded-hook nil
263 "Normal hook run when the user declines to re-use a desktop file.
264 Run in the directory in which the desktop file was found.
265 May be used to deal with accidental multiple Emacs jobs."
268 :options
'(desktop-save-mode-off save-buffers-kill-emacs
)
271 (defcustom desktop-after-read-hook nil
272 "Normal hook run after a successful `desktop-read'.
273 May be used to show a buffer list."
276 :options
'(list-buffers)
279 (defcustom desktop-save-hook nil
280 "Normal hook run before the desktop is saved in a desktop file.
281 Run with the desktop buffer current with only the header present.
282 May be used to add to the desktop code or to truncate history lists,
287 (defcustom desktop-globals-to-save
288 '(desktop-missing-file-warning
295 "List of global variables saved by `desktop-save'.
296 An element may be variable name (a symbol) or a cons cell of the form
297 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
298 MAX-SIZE elements (if the value is a list) before saving the value.
299 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
300 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
303 (defcustom desktop-globals-to-clear
305 kill-ring-yank-pointer
307 search-ring-yank-pointer
309 regexp-search-ring-yank-pointer
)
310 "List of global variables that `desktop-clear' will clear.
311 An element may be variable name (a symbol) or a cons cell of the form
312 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
313 to the value obtained by evaluating FORM."
314 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
318 (defcustom desktop-clear-preserve-buffers
319 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
321 "List of buffers that `desktop-clear' should not delete.
322 Each element is a regular expression. Buffers with a name matched by any of
323 these won't be deleted."
324 :version
"23.3" ; added Warnings - bug#6336
325 :type
'(repeat string
)
329 (defcustom desktop-locals-to-save
330 '(desktop-locals-to-save ; Itself! Think it over.
336 change-log-default-name
340 buffer-file-coding-system
343 indicate-buffer-boundaries
345 show-trailing-whitespace
)
346 "List of local variables to save for each buffer.
347 The variables are saved only when they really are local. Conventional minor
348 modes are restored automatically; they should not be listed here."
349 :type
'(repeat symbol
)
352 (defcustom desktop-buffers-not-to-save nil
353 "Regexp identifying buffers that are to be excluded from saving."
354 :type
'(choice (const :tag
"None" nil
)
356 :version
"23.2" ; set to nil
359 ;; Skip tramp and ange-ftp files
360 (defcustom desktop-files-not-to-save
361 "\\(^/[^/:]*:\\|(ftp)$\\)"
362 "Regexp identifying files whose buffers are to be excluded from saving."
363 :type
'(choice (const :tag
"None" nil
)
367 ;; We skip TAGS files to save time (tags-file-name is saved instead).
368 (defcustom desktop-modes-not-to-save
370 "List of major modes whose buffers should not be saved."
371 :type
'(repeat symbol
)
374 (defcustom desktop-save-windows nil
375 "When non-nil, save window/frame configuration to desktop file."
380 (defcustom desktop-file-name-format
'absolute
381 "Format in which desktop file names should be saved.
383 absolute -- Absolute file name.
384 tilde -- Relative to ~.
385 local -- Relative to directory of desktop file."
386 :type
'(choice (const absolute
) (const tilde
) (const local
))
390 (defcustom desktop-restore-eager t
391 "Number of buffers to restore immediately.
392 Remaining buffers are restored lazily (when Emacs is idle).
393 If value is t, all buffers are restored immediately."
394 :type
'(choice (const t
) integer
)
398 (defcustom desktop-lazy-verbose t
399 "Verbose reporting of lazily created buffers."
404 (defcustom desktop-lazy-idle-delay
5
405 "Idle delay before starting to create buffers.
406 See `desktop-restore-eager'."
412 (defvar desktop-save-buffer nil
413 "When non-nil, save buffer status in desktop file.
414 This variable becomes buffer local when set.
416 If the value is a function, it is called by `desktop-save' with argument
417 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
418 file along with the state of the buffer for which it was called.
420 When file names are returned, they should be formatted using the call
421 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
423 Later, when `desktop-read' evaluates the desktop file, auxiliary information
424 is passed as the argument DESKTOP-BUFFER-MISC to functions in
425 `desktop-buffer-mode-handlers'.")
426 (make-variable-buffer-local 'desktop-save-buffer
)
427 (make-obsolete-variable 'desktop-buffer-modes-to-save
428 'desktop-save-buffer
"22.1")
429 (make-obsolete-variable 'desktop-buffer-misc-functions
430 'desktop-save-buffer
"22.1")
433 (defvar desktop-buffer-mode-handlers nil
434 "Alist of major mode specific functions to restore a desktop buffer.
435 Functions listed are called by `desktop-create-buffer' when `desktop-read'
436 evaluates the desktop file. List elements must have the form
438 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
440 Buffers with a major mode not specified here, are restored by the default
441 handler `desktop-restore-file-buffer'.
443 Handlers are called with argument list
445 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
447 Furthermore, they may use the following variables:
450 desktop-buffer-major-mode
451 desktop-buffer-minor-modes
454 desktop-buffer-read-only
455 desktop-buffer-locals
457 If a handler returns a buffer, then the saved mode settings
458 and variable values for that buffer are copied into it.
460 Modules that define a major mode that needs a special handler should contain
463 (defun foo-restore-desktop-buffer
465 (add-to-list 'desktop-buffer-mode-handlers
466 '(foo-mode . foo-restore-desktop-buffer))
468 Furthermore the major mode function must be autoloaded.")
471 (put 'desktop-buffer-mode-handlers
'risky-local-variable t
)
472 (make-obsolete-variable 'desktop-buffer-handlers
473 'desktop-buffer-mode-handlers
"22.1")
475 (defcustom desktop-minor-mode-table
476 '((auto-fill-function auto-fill-mode
)
479 (erc-track-minor-mode nil
)
481 "Table mapping minor mode variables to minor mode functions.
482 Each entry has the form (NAME RESTORE-FUNCTION).
483 NAME is the name of the buffer-local variable indicating that the minor
484 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
485 RESTORE-FUNCTION nil means don't try to restore the minor mode.
486 Only minor modes for which the name of the buffer-local variable
487 and the name of the minor mode function are different have to be added to
488 this table. See also `desktop-minor-mode-handlers'."
493 (defvar desktop-minor-mode-handlers nil
494 "Alist of functions to restore non-standard minor modes.
495 Functions are called by `desktop-create-buffer' to restore minor modes.
496 List elements must have the form
498 (MINOR-MODE . RESTORE-FUNCTION).
500 Minor modes not specified here, are restored by the standard minor mode
503 Handlers are called with argument list
505 (DESKTOP-BUFFER-LOCALS)
507 Furthermore, they may use the following variables:
510 desktop-buffer-file-name
512 desktop-buffer-major-mode
513 desktop-buffer-minor-modes
516 desktop-buffer-read-only
519 When a handler is called, the buffer has been created and the major mode has
520 been set, but local variables listed in desktop-buffer-locals has not yet been
523 Modules that define a minor mode that needs a special handler should contain
526 (defun foo-desktop-restore
528 (add-to-list 'desktop-minor-mode-handlers
529 '(foo-mode . foo-desktop-restore))
531 Furthermore the minor mode function must be autoloaded.
533 See also `desktop-minor-mode-table'.")
536 (put 'desktop-minor-mode-handlers
'risky-local-variable t
)
538 ;; ----------------------------------------------------------------------------
539 (defvar desktop-dirname nil
540 "The directory in which the desktop file should be saved.")
542 (defun desktop-full-file-name (&optional dirname
)
543 "Return the full name of the desktop file in DIRNAME.
544 DIRNAME omitted or nil means use `desktop-dirname'."
545 (expand-file-name desktop-base-file-name
(or dirname desktop-dirname
)))
547 (defun desktop-full-lock-name (&optional dirname
)
548 "Return the full name of the desktop lock file in DIRNAME.
549 DIRNAME omitted or nil means use `desktop-dirname'."
550 (expand-file-name desktop-base-lock-name
(or dirname desktop-dirname
)))
552 (defconst desktop-header
553 ";; --------------------------------------------------------------------------
554 ;; Desktop File for Emacs
555 ;; --------------------------------------------------------------------------
556 " "*Header to place in Desktop file.")
558 (defvar desktop-delay-hook nil
559 "Hooks run after all buffers are loaded; intended for internal use.")
561 (defvar desktop-file-checksum nil
562 "Checksum of the last auto-saved contents of the desktop file.
563 Used to avoid writing contents unchanged between auto-saves.")
565 (defvar desktop--saved-states nil
566 "Internal use only.")
568 ;; ----------------------------------------------------------------------------
569 ;; Desktop file conflict detection
570 (defvar desktop-file-modtime nil
571 "When the desktop file was last modified to the knowledge of this Emacs.
572 Used to detect desktop file conflicts.")
574 (defun desktop-owner (&optional dirname
)
575 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
576 Return nil if no desktop file found or no Emacs process is using it.
577 DIRNAME omitted or nil means use `desktop-dirname'."
579 (and (file-exists-p (desktop-full-lock-name dirname
))
582 (insert-file-contents-literally (desktop-full-lock-name dirname
))
583 (goto-char (point-min))
584 (setq owner
(read (current-buffer)))
589 (defun desktop-claim-lock (&optional dirname
)
590 "Record this Emacs process as the owner of the desktop file in DIRNAME.
591 DIRNAME omitted or nil means use `desktop-dirname'."
592 (write-region (number-to-string (emacs-pid)) nil
593 (desktop-full-lock-name dirname
)))
595 (defun desktop-release-lock (&optional dirname
)
596 "Remove the lock file for the desktop in DIRNAME.
597 DIRNAME omitted or nil means use `desktop-dirname'."
598 (let ((file (desktop-full-lock-name dirname
)))
599 (when (file-exists-p file
) (delete-file file
))))
601 ;; ----------------------------------------------------------------------------
602 (defun desktop-truncate (list n
)
603 "Truncate LIST to at most N elements destructively."
604 (let ((here (nthcdr (1- n
) list
)))
608 ;; ----------------------------------------------------------------------------
610 (defun desktop-clear ()
612 This kills all buffers except for internal ones and those with names matched by
613 a regular expression in the list `desktop-clear-preserve-buffers'.
614 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
617 (dolist (var desktop-globals-to-clear
)
619 (eval `(setq-default ,var nil
))
620 (eval `(setq-default ,(car var
) ,(cdr var
)))))
621 (let ((buffers (buffer-list))
622 (preserve-regexp (concat "^\\("
623 (mapconcat (lambda (regexp)
624 (concat "\\(" regexp
"\\)"))
625 desktop-clear-preserve-buffers
629 (let ((bufname (buffer-name (car buffers
))))
632 (string-match preserve-regexp bufname
)
633 ;; Don't kill buffers made for internal purposes.
634 (and (not (equal bufname
"")) (eq (aref bufname
0) ?\s
))
635 (kill-buffer (car buffers
))))
636 (setq buffers
(cdr buffers
))))
637 (delete-other-windows))
639 ;; ----------------------------------------------------------------------------
640 (unless noninteractive
641 (add-hook 'kill-emacs-hook
'desktop-kill
))
643 (defun desktop-kill ()
644 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
645 If the desktop should be saved and `desktop-dirname'
646 is nil, ask the user where to save the desktop."
647 (when (and desktop-save-mode
648 (let ((exists (file-exists-p (desktop-full-file-name))))
649 (or (eq desktop-save t
)
650 (and exists
(eq desktop-save
'if-exists
))
651 ;; If it exists, but we aren't using it, we are going
652 ;; to ask for a new directory below.
653 (and exists desktop-dirname
(eq desktop-save
'ask-if-new
))
655 (or (memq desktop-save
'(ask ask-if-new
))
656 (and exists
(eq desktop-save
'ask-if-exists
)))
657 (y-or-n-p "Save desktop? ")))))
658 (unless desktop-dirname
659 (setq desktop-dirname
660 (file-name-as-directory
662 (read-directory-name "Directory for desktop file: " nil nil t
)))))
664 (desktop-save desktop-dirname t
)
666 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
667 (signal (car err
) (cdr err
))))))
668 ;; If we own it, we don't anymore.
669 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
671 ;; ----------------------------------------------------------------------------
672 (defun desktop-list* (&rest args
)
673 (if (null (cdr args
))
675 (setq args
(nreverse args
))
676 (let ((value (cons (nth 1 args
) (car args
))))
677 (setq args
(cdr (cdr args
)))
679 (setq value
(cons (car args
) value
))
680 (setq args
(cdr args
)))
683 ;; ----------------------------------------------------------------------------
684 (defun desktop-buffer-info (buffer)
687 ;; base name of the buffer; replaces the buffer name if managed by uniquify
688 (and (fboundp 'uniquify-buffer-base-name
) (uniquify-buffer-base-name))
690 (desktop-file-name (buffer-file-name) desktop-dirname
)
696 #'(lambda (minor-mode)
697 (and (boundp minor-mode
)
698 (symbol-value minor-mode
)
699 (let* ((special (assq minor-mode desktop-minor-mode-table
))
700 (value (cond (special (cadr special
))
701 ((functionp minor-mode
) minor-mode
))))
702 (when value
(add-to-list 'ret value
)))))
703 (mapcar #'car minor-mode-alist
))
705 ;; point and mark, and read-only status
707 (list (mark t
) mark-active
)
709 ;; auxiliary information
710 (when (functionp desktop-save-buffer
)
711 (funcall desktop-save-buffer desktop-dirname
))
713 (let ((locals desktop-locals-to-save
)
714 (loclist (buffer-local-variables))
717 (let ((here (assq (car locals
) loclist
)))
719 (setq ll
(cons here ll
))
720 (when (member (car locals
) loclist
)
721 (setq ll
(cons (car locals
) ll
)))))
722 (setq locals
(cdr locals
)))
725 ;; ----------------------------------------------------------------------------
726 (defun desktop--v2s (value)
727 "Convert VALUE to a pair (QUOTE . SEXP); (eval SEXP) gives VALUE.
728 SEXP is an sexp that when evaluated yields VALUE.
729 QUOTE may be `may' (value may be quoted),
730 `must' (value must be quoted), or nil (value must not be quoted)."
732 ((or (numberp value
) (null value
) (eq t value
) (keywordp value
))
735 (let ((copy (copy-sequence value
)))
736 (set-text-properties 0 (length copy
) nil copy
)
737 ;; Get rid of text properties because we cannot read them.
742 (let* ((pass1 (mapcar #'desktop--v2s value
))
743 (special (assq nil pass1
)))
746 ,@(mapcar (lambda (el)
747 (if (eq (car el
) 'must
)
748 `',(cdr el
) (cdr el
)))
750 (cons 'may
`[,@(mapcar #'cdr pass1
)]))))
757 (let ((q.sexp
(desktop--v2s (car p
))))
758 (push q.sexp newlist
))
761 (let ((last (desktop--v2s p
)))
763 (push last newlist
)))
764 (if (assq nil newlist
)
766 `(,(if use-list
* 'desktop-list
* 'list
)
767 ,@(mapcar (lambda (el)
768 (if (eq (car el
) 'must
)
769 `',(cdr el
) (cdr el
)))
770 (nreverse newlist
))))
773 (nreverse (if use-list
* (cdr newlist
) newlist
)))
774 ,@(if use-list
* (cdar newlist
)))))))
776 (cons nil
`(symbol-function
777 ',(intern-soft (substring (prin1-to-string value
) 7 -
1)))))
779 (let ((pos (marker-position value
))
780 (buf (buffer-name (marker-buffer value
))))
782 `(let ((mk (make-marker)))
783 (add-hook 'desktop-delay-hook
785 (set-marker ,mk
,,pos
(get-buffer ,,buf
))))
788 (cons 'may
"Unprintable entity"))))
790 ;; ----------------------------------------------------------------------------
791 (defun desktop-value-to-string (value)
792 "Convert VALUE to a string that when read evaluates to the same value.
793 Not all types of values are supported."
794 (let* ((print-escape-newlines t
)
795 (float-output-format nil
)
796 (quote.sexp
(desktop--v2s value
))
797 (quote (car quote.sexp
))
799 (let ((print-quoted t
))
800 (prin1-to-string (cdr quote.sexp
)))))
805 ;; ----------------------------------------------------------------------------
806 (defun desktop-outvar (varspec)
807 "Output a setq statement for variable VAR to the desktop file.
808 The argument VARSPEC may be the variable name VAR (a symbol),
809 or a cons cell of the form (VAR . MAX-SIZE),
810 which means to truncate VAR's value to at most MAX-SIZE elements
811 \(if the value is a list) before saving the value."
814 (setq var
(car varspec
) size
(cdr varspec
))
817 (when (and (integerp size
)
820 (desktop-truncate (eval var
) size
))
824 (desktop-value-to-string (symbol-value var
))
827 ;; ----------------------------------------------------------------------------
828 (defun desktop-save-buffer-p (filename bufname mode
&rest _dummy
)
829 "Return t if buffer should have its state saved in the desktop file.
830 FILENAME is the visited file name, BUFNAME is the buffer name, and
831 MODE is the major mode.
832 \n\(fn FILENAME BUFNAME MODE)"
833 (let ((case-fold-search nil
)
835 (and (not (and (stringp desktop-buffers-not-to-save
)
837 (string-match desktop-buffers-not-to-save bufname
)))
838 (not (memq mode desktop-modes-not-to-save
))
839 ;; FIXME this is broken if desktop-files-not-to-save is nil.
841 (stringp desktop-files-not-to-save
)
842 (not (string-match desktop-files-not-to-save filename
)))
843 (and (memq mode
'(dired-mode vc-dir-mode
))
844 (with-current-buffer bufname
845 (not (setq dired-skip
846 (string-match desktop-files-not-to-save
847 default-directory
)))))
849 (null dired-skip
) ; bug#5755
850 (with-current-buffer bufname desktop-save-buffer
))))))
852 ;; ----------------------------------------------------------------------------
853 (defun desktop-file-name (filename dirname
)
854 "Convert FILENAME to format specified in `desktop-file-name-format'.
855 DIRNAME must be the directory in which the desktop file will be saved."
858 ((eq desktop-file-name-format
'tilde
)
859 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
861 ((file-name-absolute-p relative-name
) relative-name
)
862 ((string= "./" relative-name
) "~/")
863 ((string= "." relative-name
) "~")
864 (t (concat "~/" relative-name
)))))
865 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
866 (t (expand-file-name filename
))))
869 ;; ----------------------------------------------------------------------------
870 (defconst desktop--excluded-frame-parameters
882 "Frame parameters not saved or restored.")
884 (defun desktop--filter-frame-parms (frame)
885 "Return frame parameters of FRAME.
886 Parameters in `desktop--excluded-frame-parameters' are excluded.
889 (dolist (param (frame-parameters frame
))
890 (unless (memq (car param
) desktop--excluded-frame-parameters
)
891 (push param params
)))
894 (defun desktop--save-windows ()
895 "Save window/frame state, as a global variable.
896 Intended to be called from `desktop-save'.
898 (setq desktop--saved-states
899 (and desktop-save-windows
900 (mapcar (lambda (frame)
901 (cons (desktop--filter-frame-parms frame
)
902 (window-state-get (frame-root-window frame
) t
)))
904 (desktop-outvar 'desktop--saved-states
))
907 (defun desktop-save (dirname &optional release auto-save
)
908 "Save the desktop in a desktop file.
909 Parameter DIRNAME specifies where to save the desktop file.
910 Optional parameter RELEASE says whether we're done with this desktop.
911 If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
912 and don't save the buffer if they are the same."
913 (interactive "DDirectory to save desktop file in: ")
914 (setq desktop-dirname
(file-name-as-directory (expand-file-name dirname
)))
916 (let ((eager desktop-restore-eager
)
917 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
919 (or (not new-modtime
) ; nothing to overwrite
920 (equal desktop-file-modtime new-modtime
)
921 (yes-or-no-p (if desktop-file-modtime
922 (if (> (float-time new-modtime
) (float-time desktop-file-modtime
))
923 "Desktop file is more recent than the one loaded. Save anyway? "
924 "Desktop file isn't the one loaded. Overwrite it? ")
925 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
926 (unless release
(error "Desktop file conflict")))
928 ;; If we're done with it, release the lock.
929 ;; Otherwise, claim it if it's unclaimed or if we created it.
931 (desktop-release-lock)
932 (unless (and new-modtime
(desktop-owner)) (desktop-claim-lock)))
936 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
938 ";; Created " (current-time-string) "\n"
939 ";; Desktop file format version " desktop-file-version
"\n"
940 ";; Emacs version " emacs-version
"\n")
941 (save-excursion (run-hooks 'desktop-save-hook
))
942 (goto-char (point-max))
943 (insert "\n;; Global section:\n")
944 ;; Called here because we save the window/frame state as a global
945 ;; variable for compatibility with previous Emacsen.
946 (desktop--save-windows)
947 (mapc (function desktop-outvar
) desktop-globals-to-save
)
948 (when (memq 'kill-ring desktop-globals-to-save
)
950 "(setq kill-ring-yank-pointer (nthcdr "
951 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
954 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
955 (dolist (l (mapcar 'desktop-buffer-info
(buffer-list)))
956 (let ((base (pop l
)))
957 (when (apply 'desktop-save-buffer-p l
)
959 (if (or (not (integerp eager
))
962 (setq eager
(1- eager
))))
963 "desktop-create-buffer"
964 "desktop-append-buffer-args")
966 desktop-file-version
)
967 ;; If there's a non-empty base name, we save it instead of the buffer name
968 (when (and base
(not (string= base
"")))
969 (setcar (nthcdr 1 l
) base
))
971 (insert "\n " (desktop-value-to-string e
)))
974 (setq default-directory desktop-dirname
)
975 ;; If auto-saving, avoid writing if nothing has changed since the last write.
976 ;; Don't check 300 characters of the header that contains the timestamp.
977 (let ((checksum (and auto-save
(md5 (current-buffer)
978 (+ (point-min) 300) (point-max)
980 (unless (and auto-save
(equal checksum desktop-file-checksum
))
981 (let ((coding-system-for-write 'emacs-mule
))
982 (write-region (point-min) (point-max) (desktop-full-file-name) nil
'nomessage
))
983 (setq desktop-file-checksum checksum
)
984 ;; We remember when it was modified (which is presumably just now).
985 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name)))))))))))
987 ;; ----------------------------------------------------------------------------
989 (defun desktop-remove ()
990 "Delete desktop file in `desktop-dirname'.
991 This function also sets `desktop-dirname' to nil."
993 (when desktop-dirname
994 (let ((filename (desktop-full-file-name)))
995 (setq desktop-dirname nil
)
996 (when (file-exists-p filename
)
997 (delete-file filename
)))))
999 (defvar desktop-buffer-args-list nil
1000 "List of args for `desktop-create-buffer'.")
1002 (defvar desktop-lazy-timer nil
)
1004 ;; ----------------------------------------------------------------------------
1005 (defun desktop--find-frame-in-display (frames display
)
1007 (while (and frames
(not result
))
1008 (if (equal display
(frame-parameter (car frames
) 'display
))
1009 (setq result
(car frames
))
1010 (setq frames
(cdr frames
))))
1013 (defun desktop--restore-windows ()
1014 "Restore window/frame configuration.
1016 (when (and desktop-save-windows desktop--saved-states
)
1018 (let ((frames (frame-list)))
1019 (dolist (state desktop--saved-states
)
1020 (let* ((fconfig (car state
))
1021 (display (cdr (assq 'display fconfig
)))
1022 (frame (desktop--find-frame-in-display frames display
)))
1024 ;; no frames in the display -- make a new one
1025 (setq frame
(make-frame-on-display display fconfig
))
1026 ;; found one -- reuse and remove from list
1027 (setq frames
(delq frame frames
))
1028 (modify-frame-parameters frame fconfig
))
1030 (window-state-put (cdr state
) (frame-root-window frame
) 'safe
)))
1031 ;; delete any remaining frames
1032 (mapc #'delete-frame frames
))
1034 (message "Error loading window configuration from desktop file")))))
1037 (defun desktop-read (&optional dirname
)
1038 "Read and process the desktop file in directory DIRNAME.
1039 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
1040 directories listed in `desktop-path'. If a desktop file is found, it
1041 is processed and `desktop-after-read-hook' is run. If no desktop file
1042 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
1043 This function is a no-op when Emacs is running in batch mode.
1044 It returns t if a desktop file was loaded, nil otherwise."
1046 (unless noninteractive
1047 (setq desktop-dirname
1048 (file-name-as-directory
1051 ;; If DIRNAME is specified, use it.
1052 (and (< 0 (length dirname
)) dirname
)
1053 ;; Otherwise search desktop file in desktop-path.
1054 (let ((dirs desktop-path
))
1057 (desktop-full-file-name (car dirs
)))))
1058 (setq dirs
(cdr dirs
)))
1059 (and dirs
(car dirs
)))
1060 ;; If not found and `desktop-path' is non-nil, use its first element.
1061 (and desktop-path
(car desktop-path
))
1062 ;; Default: .emacs.d.
1063 user-emacs-directory
))))
1064 (if (file-exists-p (desktop-full-file-name))
1065 ;; Desktop file found, but is it already in use?
1066 (let ((desktop-first-buffer nil
)
1067 (desktop-buffer-ok-count 0)
1068 (desktop-buffer-fail-count 0)
1069 (owner (desktop-owner))
1070 ;; Avoid desktop saving during evaluation of desktop buffer.
1073 (memq desktop-load-locked-desktop
'(nil ask
))
1074 (or (null desktop-load-locked-desktop
)
1076 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
1077 Using it may cause conflicts. Use it anyway? " owner
)))))
1078 (let ((default-directory desktop-dirname
))
1079 (setq desktop-dirname nil
)
1080 (run-hooks 'desktop-not-loaded-hook
)
1081 (unless desktop-dirname
1082 (message "Desktop file in use; not loaded.")))
1083 (desktop-lazy-abort)
1084 ;; Evaluate desktop buffer and remember when it was modified.
1085 (load (desktop-full-file-name) t t t
)
1086 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name))))
1087 ;; If it wasn't already, mark it as in-use, to bother other
1088 ;; desktop instances.
1091 (desktop-claim-lock)
1092 (file-error (message "Couldn't record use of desktop file")
1095 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1096 ;; We want buffers existing prior to evaluating the desktop (and
1097 ;; not reused) to be placed at the end of the buffer list, so we
1100 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
1101 (switch-to-buffer (car (buffer-list)))
1102 (run-hooks 'desktop-delay-hook
)
1103 (setq desktop-delay-hook nil
)
1104 (desktop--restore-windows)
1105 (run-hooks 'desktop-after-read-hook
)
1106 (message "Desktop: %d buffer%s restored%s%s."
1107 desktop-buffer-ok-count
1108 (if (= 1 desktop-buffer-ok-count
) "" "s")
1109 (if (< 0 desktop-buffer-fail-count
)
1110 (format ", %d failed to restore" desktop-buffer-fail-count
)
1112 (if desktop-buffer-args-list
1113 (format ", %d to restore lazily"
1114 (length desktop-buffer-args-list
))
1116 ;; Bury the *Messages* buffer to not reshow it when burying
1117 ;; the buffer we switched to above.
1118 (when (buffer-live-p (get-buffer "*Messages*"))
1119 (bury-buffer "*Messages*"))
1120 ;; Clear all windows' previous and next buffers, these have
1121 ;; been corrupted by the `switch-to-buffer' calls in
1122 ;; `desktop-restore-file-buffer' (bug#11556). This is a
1123 ;; brute force fix and should be replaced by a more subtle
1124 ;; strategy eventually.
1125 (walk-window-tree (lambda (window)
1126 (set-window-prev-buffers window nil
)
1127 (set-window-next-buffers window nil
)))
1129 ;; No desktop file found.
1131 (let ((default-directory desktop-dirname
))
1132 (run-hooks 'desktop-no-desktop-file-hook
))
1133 (message "No desktop file.")
1136 ;; ----------------------------------------------------------------------------
1137 ;; Maintained for backward compatibility
1139 (defun desktop-load-default ()
1140 "Load the `default' start-up library manually.
1141 Also inhibit further loading of it."
1142 (declare (obsolete desktop-save-mode
"22.1"))
1143 (unless inhibit-default-init
; safety check
1144 (load "default" t t
)
1145 (setq inhibit-default-init t
)))
1147 ;; ----------------------------------------------------------------------------
1149 (defun desktop-change-dir (dirname)
1150 "Change to desktop saved in DIRNAME.
1151 Kill the desktop as specified by variables `desktop-save-mode' and
1152 `desktop-save', then clear the desktop and load the desktop file in
1154 (interactive "DChange to directory: ")
1155 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
1158 (desktop-read dirname
))
1160 ;; ----------------------------------------------------------------------------
1162 (defun desktop-save-in-desktop-dir ()
1163 "Save the desktop in directory `desktop-dirname'."
1166 (desktop-save desktop-dirname
)
1167 (call-interactively 'desktop-save
))
1168 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname
)))
1170 ;; ----------------------------------------------------------------------------
1172 (defvar desktop-auto-save-timer nil
)
1174 (defun desktop-auto-save ()
1175 "Save the desktop periodically.
1176 Called by the timer created in `desktop-auto-save-set-timer'."
1177 (when (and desktop-save-mode
1178 (integerp desktop-auto-save-timeout
)
1179 (> desktop-auto-save-timeout
0)
1180 ;; Avoid desktop saving during lazy loading.
1181 (not desktop-lazy-timer
)
1182 ;; Save only to own desktop file.
1183 (eq (emacs-pid) (desktop-owner))
1185 (desktop-save desktop-dirname nil t
))
1186 (desktop-auto-save-set-timer))
1188 (defun desktop-auto-save-set-timer ()
1189 "Reset the auto-save timer.
1190 Cancel any previous timer. When `desktop-auto-save-timeout' is a positive
1191 integer, start a new timer to call `desktop-auto-save' in that many seconds."
1192 (when desktop-auto-save-timer
1193 (cancel-timer desktop-auto-save-timer
)
1194 (setq desktop-auto-save-timer nil
))
1195 (when (and (integerp desktop-auto-save-timeout
)
1196 (> desktop-auto-save-timeout
0))
1197 (setq desktop-auto-save-timer
1198 (run-with-timer desktop-auto-save-timeout nil
1199 'desktop-auto-save
))))
1201 ;; ----------------------------------------------------------------------------
1203 (defun desktop-revert ()
1204 "Revert to the last loaded desktop."
1206 (unless desktop-dirname
1207 (error "Unknown desktop directory"))
1208 (unless (file-exists-p (desktop-full-file-name))
1209 (error "No desktop file found"))
1211 (desktop-read desktop-dirname
))
1213 (defvar desktop-buffer-major-mode
)
1214 (defvar desktop-buffer-locals
)
1215 (defvar auto-insert
) ; from autoinsert.el
1216 ;; ----------------------------------------------------------------------------
1217 (defun desktop-restore-file-buffer (buffer-filename
1220 "Restore a file buffer."
1221 (when buffer-filename
1222 (if (or (file-exists-p buffer-filename
)
1223 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1225 (if desktop-missing-file-warning
1226 (y-or-n-p (concat msg
" Re-create buffer? "))
1229 (let* ((auto-insert nil
) ; Disable auto insertion
1230 (coding-system-for-read
1231 (or coding-system-for-read
1232 (cdr (assq 'buffer-file-coding-system
1233 desktop-buffer-locals
))))
1234 (buf (find-file-noselect buffer-filename
)))
1236 (switch-to-buffer buf
)
1237 (error (pop-to-buffer buf
)))
1238 (and (not (eq major-mode desktop-buffer-major-mode
))
1239 (functionp desktop-buffer-major-mode
)
1240 (funcall desktop-buffer-major-mode
))
1244 (defun desktop-load-file (function)
1245 "Load the file where auto loaded FUNCTION is defined."
1246 (when (fboundp function
)
1247 (autoload-do-load (symbol-function function
) function
)))
1249 ;; ----------------------------------------------------------------------------
1250 ;; Create a buffer, load its file, set its mode, ...;
1251 ;; called from Desktop file only.
1253 ;; Just to silence the byte compiler.
1255 (defvar desktop-first-buffer
) ; Dynamically bound in `desktop-read'
1257 ;; Bound locally in `desktop-read'.
1258 (defvar desktop-buffer-ok-count
)
1259 (defvar desktop-buffer-fail-count
)
1261 (defun desktop-create-buffer
1274 (let ((desktop-file-version file-version
)
1275 (desktop-buffer-file-name buffer-filename
)
1276 (desktop-buffer-name buffer-name
)
1277 (desktop-buffer-major-mode buffer-majormode
)
1278 (desktop-buffer-minor-modes buffer-minormodes
)
1279 (desktop-buffer-point buffer-point
)
1280 (desktop-buffer-mark buffer-mark
)
1281 (desktop-buffer-read-only buffer-readonly
)
1282 (desktop-buffer-misc buffer-misc
)
1283 (desktop-buffer-locals buffer-locals
))
1284 ;; To make desktop files with relative file names possible, we cannot
1285 ;; allow `default-directory' to change. Therefore we save current buffer.
1286 (save-current-buffer
1287 ;; Give major mode module a chance to add a handler.
1288 (desktop-load-file desktop-buffer-major-mode
)
1289 (let ((buffer-list (buffer-list))
1291 (condition-case-unless-debug err
1292 (funcall (or (cdr (assq desktop-buffer-major-mode
1293 desktop-buffer-mode-handlers
))
1294 'desktop-restore-file-buffer
)
1295 desktop-buffer-file-name
1297 desktop-buffer-misc
)
1299 (message "Desktop: Can't load buffer %s: %s"
1301 (error-message-string err
))
1302 (when desktop-missing-file-warning
(sit-for 1))
1304 (if (bufferp result
)
1305 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
1306 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
1308 ;; Restore buffer list order with new buffer at end. Don't change
1309 ;; the order for old desktop files (old desktop module behavior).
1310 (unless (< desktop-file-version
206)
1311 (mapc 'bury-buffer buffer-list
)
1312 (when result
(bury-buffer result
)))
1314 (unless (or desktop-first-buffer
(< desktop-file-version
206))
1315 (setq desktop-first-buffer result
))
1317 (unless (equal (buffer-name) desktop-buffer-name
)
1318 (rename-buffer desktop-buffer-name t
))
1320 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
1322 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
1325 (dolist (minor-mode desktop-buffer-minor-modes
)
1326 ;; Give minor mode module a chance to add a handler.
1327 (desktop-load-file minor-mode
)
1328 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers
))))
1330 (funcall handler desktop-buffer-locals
)
1331 (when (functionp minor-mode
)
1332 (funcall minor-mode
1)))))))
1333 ;; Even though point and mark are non-nil when written by
1334 ;; `desktop-save', they may be modified by handlers wanting to set
1335 ;; point or mark themselves.
1336 (when desktop-buffer-point
1339 ;; Evaluate point. Thus point can be something like
1340 ;; '(search-forward ...
1341 (eval desktop-buffer-point
)
1342 (error (message "%s" (error-message-string err
)) 1))))
1343 (when desktop-buffer-mark
1344 (if (consp desktop-buffer-mark
)
1346 (set-mark (car desktop-buffer-mark
))
1347 (setq mark-active
(car (cdr desktop-buffer-mark
))))
1348 (set-mark desktop-buffer-mark
)))
1349 ;; Never override file system if the file really is read-only marked.
1350 (when desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
1351 (while desktop-buffer-locals
1352 (let ((this (car desktop-buffer-locals
)))
1354 ;; an entry of this form `(symbol . value)'
1356 (make-local-variable (car this
))
1357 (set (car this
) (cdr this
)))
1358 ;; an entry of the form `symbol'
1359 (make-local-variable this
)
1361 (setq desktop-buffer-locals
(cdr desktop-buffer-locals
))))))))
1363 ;; ----------------------------------------------------------------------------
1364 ;; Backward compatibility -- update parameters to 205 standards.
1365 (defun desktop-buffer (buffer-filename buffer-name buffer-majormode
1366 mim pt mk ro tl fc cfs cr buffer-misc
)
1367 (desktop-create-buffer 205 buffer-filename buffer-name
1368 buffer-majormode
(cdr mim
) pt mk ro
1370 (list (cons 'truncate-lines tl
)
1371 (cons 'fill-column fc
)
1372 (cons 'case-fold-search cfs
)
1373 (cons 'case-replace cr
)
1374 (cons 'overwrite-mode
(car mim
)))))
1376 (defun desktop-append-buffer-args (&rest args
)
1377 "Append ARGS at end of `desktop-buffer-args-list'.
1378 ARGS must be an argument list for `desktop-create-buffer'."
1379 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
1380 (unless desktop-lazy-timer
1381 (setq desktop-lazy-timer
1382 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
1384 (defun desktop-lazy-create-buffer ()
1385 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1386 (when desktop-buffer-args-list
1387 (let* ((remaining (length desktop-buffer-args-list
))
1388 (args (pop desktop-buffer-args-list
))
1389 (buffer-name (nth 2 args
))
1390 (msg (format "Desktop lazily opening %s (%s remaining)..."
1391 buffer-name remaining
)))
1392 (when desktop-lazy-verbose
1394 (let ((desktop-first-buffer nil
)
1395 (desktop-buffer-ok-count 0)
1396 (desktop-buffer-fail-count 0))
1397 (apply 'desktop-create-buffer args
)
1398 (run-hooks 'desktop-delay-hook
)
1399 (setq desktop-delay-hook nil
)
1400 (bury-buffer (get-buffer buffer-name
))
1401 (when desktop-lazy-verbose
1402 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
1404 (defun desktop-idle-create-buffers ()
1405 "Create buffers until the user does something, then stop.
1406 If there are no buffers left to create, kill the timer."
1408 (while (and repeat desktop-buffer-args-list
)
1409 (save-window-excursion
1410 (desktop-lazy-create-buffer))
1411 (setq repeat
(sit-for 0.2))
1412 (unless desktop-buffer-args-list
1413 (cancel-timer desktop-lazy-timer
)
1414 (setq desktop-lazy-timer nil
)
1415 (message "Lazy desktop load complete")
1419 (defun desktop-lazy-complete ()
1420 "Run the desktop load to completion."
1422 (let ((desktop-lazy-verbose t
))
1423 (while desktop-buffer-args-list
1424 (save-window-excursion
1425 (desktop-lazy-create-buffer)))
1426 (message "Lazy desktop load complete")))
1428 (defun desktop-lazy-abort ()
1429 "Abort lazy loading of the desktop."
1431 (when desktop-lazy-timer
1432 (cancel-timer desktop-lazy-timer
)
1433 (setq desktop-lazy-timer nil
))
1434 (when desktop-buffer-args-list
1435 (setq desktop-buffer-args-list nil
)
1436 (when (called-interactively-p 'interactive
)
1437 (message "Lazy desktop load aborted"))))
1439 ;; ----------------------------------------------------------------------------
1440 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1441 ;; command line, we do the rest of what it takes to use desktop, but do it
1442 ;; after finishing loading the init file.
1443 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1444 ;; functions are processed after `after-init-hook'.
1448 (let ((key "--no-desktop"))
1449 (when (member key command-line-args
)
1450 (setq command-line-args
(delete key command-line-args
))
1451 (setq desktop-save-mode nil
)))
1452 (when desktop-save-mode
1454 (desktop-auto-save-set-timer)
1455 (setq inhibit-startup-screen t
))))
1457 ;; So we can restore vc-dir buffers.
1458 (autoload 'vc-dir-mode
"vc-dir" nil t
)
1462 ;;; desktop.el ends here