gnus-art.el (gnus-shr-put-image): Make it work as well for shr.el's that the old...
[emacs/old-mirror.git] / lisp / desktop.el
blobc31cbead2b94e5f9172bc68f5ccd2a294b020b50
1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993-1995, 1997, 2000-2013 Free Software Foundation,
4 ;; Inc.
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/>.
25 ;;; Commentary:
27 ;; Save the Desktop, i.e.,
28 ;; - some global variables
29 ;; - the list of buffers with associated files. For each buffer also
30 ;; - the major mode
31 ;; - the default directory
32 ;; - the point
33 ;; - the mark & mark-active
34 ;; - buffer-read-only
35 ;; - some local variables
36 ;; - frame and window configuration
38 ;; To use this, use customize to turn on desktop-save-mode or add the
39 ;; following line somewhere in your init file:
41 ;; (desktop-save-mode 1)
43 ;; For further usage information, look at the section
44 ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
46 ;; When the desktop module is loaded, the function `desktop-kill' is
47 ;; added to the `kill-emacs-hook'. This function is responsible for
48 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
49 ;; function is added to the `after-init-hook'. This function is
50 ;; responsible for loading the desktop when Emacs is started.
52 ;; Special handling.
53 ;; -----------------
54 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
55 ;; are supplied to handle special major and minor modes respectively.
56 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
57 ;; to restore a desktop buffer. Elements must have the form
59 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
61 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
62 ;; evaluates the desktop file. Buffers with a major mode not specified here,
63 ;; are restored by the default handler `desktop-restore-file-buffer'.
64 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
65 ;; non-standard minor modes. Elements must have the form
67 ;; (MINOR-MODE . RESTORE-FUNCTION).
69 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
70 ;; Minor modes not specified here, are restored by the standard minor mode
71 ;; function. If you write a module that defines a major or minor mode that
72 ;; needs a special handler, then place code like
74 ;; (defun foo-restore-desktop-buffer
75 ;; ...
76 ;; (add-to-list 'desktop-buffer-mode-handlers
77 ;; '(foo-mode . foo-restore-desktop-buffer))
79 ;; or
81 ;; (defun bar-desktop-restore
82 ;; ...
83 ;; (add-to-list 'desktop-minor-mode-handlers
84 ;; '(bar-mode . bar-desktop-restore))
86 ;; in the module itself, and make sure that the mode function is
87 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
88 ;; `desktop-minor-mode-handlers' for more info.
90 ;; Minor modes.
91 ;; ------------
92 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
93 ;; manual) are handled in the following way:
94 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
95 ;; saves as `desktop-minor-modes' the list of names of those variables in
96 ;; `minor-mode-alist' that have a non-nil value.
97 ;; When `desktop-create' restores the buffer, each of the symbols in
98 ;; `desktop-minor-modes' is called as function with parameter 1.
99 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
100 ;; are used to handle non-conventional minor modes. `desktop-save' uses
101 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
102 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
103 ;; variable name that is different form its function name, an entry
105 ;; (NAME RESTORE-FUNCTION)
107 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
108 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
109 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
110 ;; function different from the usual minor mode function.
111 ;; ---------------------------------------------------------------------------
113 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
114 ;; in your home directory is used for that. Saving global default values
115 ;; for buffers is an example of misuse.
117 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
118 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
119 ;; things you did not mean to keep. Use M-x desktop-clear RET.
121 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
122 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
123 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
124 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
125 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
126 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
127 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
128 ;; ---------------------------------------------------------------------------
129 ;; TODO:
131 ;; Recognize more minor modes.
132 ;; Save mark rings.
134 ;;; Code:
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."
147 :group 'frames)
149 ;; Maintained for backward compatibility
150 (define-obsolete-variable-alias 'desktop-enable 'desktop-save-mode "22.1")
151 ;;;###autoload
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."
161 :global t
162 :group 'desktop)
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.
171 Possible values are:
172 t -- always save.
173 ask -- always ask.
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.
177 nil -- never 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."
181 :type
182 '(choice
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))
189 :group 'desktop
190 :version "22.1")
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)
199 (ignore-errors (desktop-auto-save-set-timer)))
200 :group 'desktop
201 :version "24.4")
203 (defcustom desktop-load-locked-desktop 'ask
204 "Specifies whether the desktop should be loaded if locked.
205 Possible values are:
206 t -- load anyway.
207 nil -- don't load.
208 ask -- ask the user.
209 If the value is nil, or `ask' and the user chooses not to load the desktop,
210 the normal hook `desktop-not-loaded-hook' is run."
211 :type
212 '(choice
213 (const :tag "Load anyway" t)
214 (const :tag "Don't load" nil)
215 (const :tag "Ask the user" ask))
216 :group 'desktop
217 :version "22.2")
219 (define-obsolete-variable-alias 'desktop-basefilename
220 'desktop-base-file-name "22.1")
222 (defcustom desktop-base-file-name
223 (convert-standard-filename ".emacs.desktop")
224 "Name of file for Emacs desktop, excluding the directory part."
225 :type 'file
226 :group 'desktop)
228 (defcustom desktop-base-lock-name
229 (convert-standard-filename ".emacs.desktop.lock")
230 "Name of lock file for Emacs desktop, excluding the directory part."
231 :type 'file
232 :group 'desktop
233 :version "22.2")
235 (defcustom desktop-path (list user-emacs-directory "~")
236 "List of directories to search for the desktop file.
237 The base name of the file is specified in `desktop-base-file-name'."
238 :type '(repeat directory)
239 :group 'desktop
240 :version "23.2") ; user-emacs-directory added
242 (defcustom desktop-missing-file-warning nil
243 "If non-nil, offer to recreate the buffer of a deleted file.
244 Also pause for a moment to display message about errors signaled in
245 `desktop-buffer-mode-handlers'.
247 If nil, just print error messages in the message buffer."
248 :type 'boolean
249 :group 'desktop
250 :version "22.1")
252 (defcustom desktop-no-desktop-file-hook nil
253 "Normal hook run when `desktop-read' can't find a desktop file.
254 Run in the directory in which the desktop file was sought.
255 May be used to show a dired buffer."
256 :type 'hook
257 :group 'desktop
258 :version "22.1")
260 (defcustom desktop-not-loaded-hook nil
261 "Normal hook run when the user declines to re-use a desktop file.
262 Run in the directory in which the desktop file was found.
263 May be used to deal with accidental multiple Emacs jobs."
264 :type 'hook
265 :group 'desktop
266 :options '(desktop-save-mode-off save-buffers-kill-emacs)
267 :version "22.2")
269 (defcustom desktop-after-read-hook nil
270 "Normal hook run after a successful `desktop-read'.
271 May be used to show a buffer list."
272 :type 'hook
273 :group 'desktop
274 :options '(list-buffers)
275 :version "22.1")
277 (defcustom desktop-save-hook nil
278 "Normal hook run before the desktop is saved in a desktop file.
279 Run with the desktop buffer current with only the header present.
280 May be used to add to the desktop code or to truncate history lists,
281 for example."
282 :type 'hook
283 :group 'desktop)
285 (defcustom desktop-globals-to-save
286 '(desktop-missing-file-warning
287 tags-file-name
288 tags-table-list
289 search-ring
290 regexp-search-ring
291 register-alist
292 file-name-history)
293 "List of global variables saved by `desktop-save'.
294 An element may be variable name (a symbol) or a cons cell of the form
295 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
296 MAX-SIZE elements (if the value is a list) before saving the value.
297 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
298 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
299 :group 'desktop)
301 (defcustom desktop-globals-to-clear
302 '(kill-ring
303 kill-ring-yank-pointer
304 search-ring
305 search-ring-yank-pointer
306 regexp-search-ring
307 regexp-search-ring-yank-pointer)
308 "List of global variables that `desktop-clear' will clear.
309 An element may be variable name (a symbol) or a cons cell of the form
310 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
311 to the value obtained by evaluating FORM."
312 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
313 :group 'desktop
314 :version "22.1")
316 (defcustom desktop-clear-preserve-buffers
317 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
318 "\\*Warnings\\*")
319 "List of buffers that `desktop-clear' should not delete.
320 Each element is a regular expression. Buffers with a name matched by any of
321 these won't be deleted."
322 :version "23.3" ; added Warnings - bug#6336
323 :type '(repeat string)
324 :group 'desktop)
326 ;;;###autoload
327 (defcustom desktop-locals-to-save
328 '(desktop-locals-to-save ; Itself! Think it over.
329 truncate-lines
330 case-fold-search
331 case-replace
332 fill-column
333 overwrite-mode
334 change-log-default-name
335 line-number-mode
336 column-number-mode
337 size-indication-mode
338 buffer-file-coding-system
339 indent-tabs-mode
340 tab-width
341 indicate-buffer-boundaries
342 indicate-empty-lines
343 show-trailing-whitespace)
344 "List of local variables to save for each buffer.
345 The variables are saved only when they really are local. Conventional minor
346 modes are restored automatically; they should not be listed here."
347 :type '(repeat symbol)
348 :group 'desktop)
350 (defcustom desktop-buffers-not-to-save nil
351 "Regexp identifying buffers that are to be excluded from saving."
352 :type '(choice (const :tag "None" nil)
353 regexp)
354 :version "23.2" ; set to nil
355 :group 'desktop)
357 ;; Skip tramp and ange-ftp files
358 (defcustom desktop-files-not-to-save
359 "\\(^/[^/:]*:\\|(ftp)$\\)"
360 "Regexp identifying files whose buffers are to be excluded from saving."
361 :type '(choice (const :tag "None" nil)
362 regexp)
363 :group 'desktop)
365 ;; We skip TAGS files to save time (tags-file-name is saved instead).
366 (defcustom desktop-modes-not-to-save
367 '(tags-table-mode)
368 "List of major modes whose buffers should not be saved."
369 :type '(repeat symbol)
370 :group 'desktop)
372 (defcustom desktop-restore-frames t
373 "When non-nil, save window/frame configuration to desktop file."
374 :type 'boolean
375 :group 'desktop
376 :version "24.4")
378 (defcustom desktop-restore-in-current-display nil
379 "If t, frames are restored in the current display.
380 If nil, frames are restored, if possible, in their original displays.
381 If `delete', frames on other displays are deleted instead of restored."
382 :type '(choice (const :tag "Restore in current display" t)
383 (const :tag "Restore in original display" nil)
384 (const :tag "Delete frames in other displays" 'delete))
385 :group 'desktop
386 :version "24.4")
388 (defcustom desktop-restoring-reuses-frames t
389 "If t, restoring frames reuses existing frames.
390 If nil, existing frames are deleted.
391 If `keep', existing frames are kept and not reused."
392 :type '(choice (const :tag "Reuse existing frames" t)
393 (const :tag "Delete existing frames" nil)
394 (const :tag "Keep existing frames" 'keep))
395 :group 'desktop
396 :version "24.4")
398 (defcustom desktop-file-name-format 'absolute
399 "Format in which desktop file names should be saved.
400 Possible values are:
401 absolute -- Absolute file name.
402 tilde -- Relative to ~.
403 local -- Relative to directory of desktop file."
404 :type '(choice (const absolute) (const tilde) (const local))
405 :group 'desktop
406 :version "22.1")
408 (defcustom desktop-restore-eager t
409 "Number of buffers to restore immediately.
410 Remaining buffers are restored lazily (when Emacs is idle).
411 If value is t, all buffers are restored immediately."
412 :type '(choice (const t) integer)
413 :group 'desktop
414 :version "22.1")
416 (defcustom desktop-lazy-verbose t
417 "Verbose reporting of lazily created buffers."
418 :type 'boolean
419 :group 'desktop
420 :version "22.1")
422 (defcustom desktop-lazy-idle-delay 5
423 "Idle delay before starting to create buffers.
424 See `desktop-restore-eager'."
425 :type 'integer
426 :group 'desktop
427 :version "22.1")
429 ;;;###autoload
430 (defvar-local desktop-save-buffer nil
431 "When non-nil, save buffer status in desktop file.
433 If the value is a function, it is called by `desktop-save' with argument
434 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
435 file along with the state of the buffer for which it was called.
437 When file names are returned, they should be formatted using the call
438 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
440 Later, when `desktop-read' evaluates the desktop file, auxiliary information
441 is passed as the argument DESKTOP-BUFFER-MISC to functions in
442 `desktop-buffer-mode-handlers'.")
443 (make-obsolete-variable 'desktop-buffer-modes-to-save
444 'desktop-save-buffer "22.1")
445 (make-obsolete-variable 'desktop-buffer-misc-functions
446 'desktop-save-buffer "22.1")
448 ;;;###autoload
449 (defvar desktop-buffer-mode-handlers nil
450 "Alist of major mode specific functions to restore a desktop buffer.
451 Functions listed are called by `desktop-create-buffer' when `desktop-read'
452 evaluates the desktop file. List elements must have the form
454 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
456 Buffers with a major mode not specified here, are restored by the default
457 handler `desktop-restore-file-buffer'.
459 Handlers are called with argument list
461 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
463 Furthermore, they may use the following variables:
465 desktop-file-version
466 desktop-buffer-major-mode
467 desktop-buffer-minor-modes
468 desktop-buffer-point
469 desktop-buffer-mark
470 desktop-buffer-read-only
471 desktop-buffer-locals
473 If a handler returns a buffer, then the saved mode settings
474 and variable values for that buffer are copied into it.
476 Modules that define a major mode that needs a special handler should contain
477 code like
479 (defun foo-restore-desktop-buffer
481 (add-to-list 'desktop-buffer-mode-handlers
482 '(foo-mode . foo-restore-desktop-buffer))
484 Furthermore the major mode function must be autoloaded.")
486 ;;;###autoload
487 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
488 (make-obsolete-variable 'desktop-buffer-handlers
489 'desktop-buffer-mode-handlers "22.1")
491 (defcustom desktop-minor-mode-table
492 '((auto-fill-function auto-fill-mode)
493 (vc-mode nil)
494 (vc-dired-mode nil)
495 (erc-track-minor-mode nil)
496 (savehist-mode nil))
497 "Table mapping minor mode variables to minor mode functions.
498 Each entry has the form (NAME RESTORE-FUNCTION).
499 NAME is the name of the buffer-local variable indicating that the minor
500 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
501 RESTORE-FUNCTION nil means don't try to restore the minor mode.
502 Only minor modes for which the name of the buffer-local variable
503 and the name of the minor mode function are different have to be added to
504 this table. See also `desktop-minor-mode-handlers'."
505 :type 'sexp
506 :group 'desktop)
508 ;;;###autoload
509 (defvar desktop-minor-mode-handlers nil
510 "Alist of functions to restore non-standard minor modes.
511 Functions are called by `desktop-create-buffer' to restore minor modes.
512 List elements must have the form
514 (MINOR-MODE . RESTORE-FUNCTION).
516 Minor modes not specified here, are restored by the standard minor mode
517 function.
519 Handlers are called with argument list
521 (DESKTOP-BUFFER-LOCALS)
523 Furthermore, they may use the following variables:
525 desktop-file-version
526 desktop-buffer-file-name
527 desktop-buffer-name
528 desktop-buffer-major-mode
529 desktop-buffer-minor-modes
530 desktop-buffer-point
531 desktop-buffer-mark
532 desktop-buffer-read-only
533 desktop-buffer-misc
535 When a handler is called, the buffer has been created and the major mode has
536 been set, but local variables listed in desktop-buffer-locals has not yet been
537 created and set.
539 Modules that define a minor mode that needs a special handler should contain
540 code like
542 (defun foo-desktop-restore
544 (add-to-list 'desktop-minor-mode-handlers
545 '(foo-mode . foo-desktop-restore))
547 Furthermore the minor mode function must be autoloaded.
549 See also `desktop-minor-mode-table'.")
551 ;;;###autoload
552 (put 'desktop-minor-mode-handlers 'risky-local-variable t)
554 ;; ----------------------------------------------------------------------------
555 (defvar desktop-dirname nil
556 "The directory in which the desktop file should be saved.")
558 (defun desktop-full-file-name (&optional dirname)
559 "Return the full name of the desktop file in DIRNAME.
560 DIRNAME omitted or nil means use `desktop-dirname'."
561 (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
563 (defun desktop-full-lock-name (&optional dirname)
564 "Return the full name of the desktop lock file in DIRNAME.
565 DIRNAME omitted or nil means use `desktop-dirname'."
566 (expand-file-name desktop-base-lock-name (or dirname desktop-dirname)))
568 (defconst desktop-header
569 ";; --------------------------------------------------------------------------
570 ;; Desktop File for Emacs
571 ;; --------------------------------------------------------------------------
572 " "*Header to place in Desktop file.")
574 (defvar desktop-delay-hook nil
575 "Hooks run after all buffers are loaded; intended for internal use.")
577 (defvar desktop-file-checksum nil
578 "Checksum of the last auto-saved contents of the desktop file.
579 Used to avoid writing contents unchanged between auto-saves.")
581 (defvar desktop--saved-states nil
582 "Saved window/frame state. Internal use only.")
584 ;; ----------------------------------------------------------------------------
585 ;; Desktop file conflict detection
586 (defvar desktop-file-modtime nil
587 "When the desktop file was last modified to the knowledge of this Emacs.
588 Used to detect desktop file conflicts.")
590 (defun desktop-owner (&optional dirname)
591 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
592 Return nil if no desktop file found or no Emacs process is using it.
593 DIRNAME omitted or nil means use `desktop-dirname'."
594 (let (owner
595 (file (desktop-full-lock-name dirname)))
596 (and (file-exists-p file)
597 (ignore-errors
598 (with-temp-buffer
599 (insert-file-contents-literally file)
600 (goto-char (point-min))
601 (setq owner (read (current-buffer)))
602 (integerp owner)))
603 owner)))
605 (defun desktop-claim-lock (&optional dirname)
606 "Record this Emacs process as the owner of the desktop file in DIRNAME.
607 DIRNAME omitted or nil means use `desktop-dirname'."
608 (write-region (number-to-string (emacs-pid)) nil
609 (desktop-full-lock-name dirname)))
611 (defun desktop-release-lock (&optional dirname)
612 "Remove the lock file for the desktop in DIRNAME.
613 DIRNAME omitted or nil means use `desktop-dirname'."
614 (let ((file (desktop-full-lock-name dirname)))
615 (when (file-exists-p file) (delete-file file))))
617 ;; ----------------------------------------------------------------------------
618 (defun desktop-truncate (list n)
619 "Truncate LIST to at most N elements destructively."
620 (let ((here (nthcdr (1- n) list)))
621 (when (consp here)
622 (setcdr here nil))))
624 ;; ----------------------------------------------------------------------------
625 ;;;###autoload
626 (defun desktop-clear ()
627 "Empty the Desktop.
628 This kills all buffers except for internal ones and those with names matched by
629 a regular expression in the list `desktop-clear-preserve-buffers'.
630 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
631 (interactive)
632 (desktop-lazy-abort)
633 (dolist (var desktop-globals-to-clear)
634 (if (symbolp var)
635 (eval `(setq-default ,var nil))
636 (eval `(setq-default ,(car var) ,(cdr var)))))
637 (let ((buffers (buffer-list))
638 (preserve-regexp (concat "^\\("
639 (mapconcat (lambda (regexp)
640 (concat "\\(" regexp "\\)"))
641 desktop-clear-preserve-buffers
642 "\\|")
643 "\\)$")))
644 (while buffers
645 (let ((bufname (buffer-name (car buffers))))
647 (null bufname)
648 (string-match-p preserve-regexp bufname)
649 ;; Don't kill buffers made for internal purposes.
650 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
651 (kill-buffer (car buffers))))
652 (setq buffers (cdr buffers))))
653 (delete-other-windows))
655 ;; ----------------------------------------------------------------------------
656 (unless noninteractive
657 (add-hook 'kill-emacs-hook 'desktop-kill))
659 (defun desktop-kill ()
660 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
661 If the desktop should be saved and `desktop-dirname'
662 is nil, ask the user where to save the desktop."
663 (when (and desktop-save-mode
664 (let ((exists (file-exists-p (desktop-full-file-name))))
665 (or (eq desktop-save t)
666 (and exists (eq desktop-save 'if-exists))
667 ;; If it exists, but we aren't using it, we are going
668 ;; to ask for a new directory below.
669 (and exists desktop-dirname (eq desktop-save 'ask-if-new))
670 (and
671 (or (memq desktop-save '(ask ask-if-new))
672 (and exists (eq desktop-save 'ask-if-exists)))
673 (y-or-n-p "Save desktop? ")))))
674 (unless desktop-dirname
675 (setq desktop-dirname
676 (file-name-as-directory
677 (expand-file-name
678 (read-directory-name "Directory for desktop file: " nil nil t)))))
679 (condition-case err
680 (desktop-save desktop-dirname t)
681 (file-error
682 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
683 (signal (car err) (cdr err))))))
684 ;; If we own it, we don't anymore.
685 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
687 ;; ----------------------------------------------------------------------------
688 (defun desktop-list* (&rest args)
689 (if (null (cdr args))
690 (car args)
691 (setq args (nreverse args))
692 (let ((value (cons (nth 1 args) (car args))))
693 (setq args (cdr (cdr args)))
694 (while args
695 (setq value (cons (car args) value))
696 (setq args (cdr args)))
697 value)))
699 ;; ----------------------------------------------------------------------------
700 (defun desktop-buffer-info (buffer)
701 (set-buffer buffer)
702 (list
703 ;; base name of the buffer; replaces the buffer name if managed by uniquify
704 (and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name))
705 ;; basic information
706 (desktop-file-name (buffer-file-name) desktop-dirname)
707 (buffer-name)
708 major-mode
709 ;; minor modes
710 (let (ret)
711 (mapc
712 #'(lambda (minor-mode)
713 (and (boundp minor-mode)
714 (symbol-value minor-mode)
715 (let* ((special (assq minor-mode desktop-minor-mode-table))
716 (value (cond (special (cadr special))
717 ((functionp minor-mode) minor-mode))))
718 (when value (add-to-list 'ret value)))))
719 (mapcar #'car minor-mode-alist))
720 ret)
721 ;; point and mark, and read-only status
722 (point)
723 (list (mark t) mark-active)
724 buffer-read-only
725 ;; auxiliary information
726 (when (functionp desktop-save-buffer)
727 (funcall desktop-save-buffer desktop-dirname))
728 ;; local variables
729 (let ((locals desktop-locals-to-save)
730 (loclist (buffer-local-variables))
731 (ll))
732 (while locals
733 (let ((here (assq (car locals) loclist)))
734 (if here
735 (setq ll (cons here ll))
736 (when (member (car locals) loclist)
737 (setq ll (cons (car locals) ll)))))
738 (setq locals (cdr locals)))
739 ll)))
741 ;; ----------------------------------------------------------------------------
742 (defun desktop--v2s (value)
743 "Convert VALUE to a pair (QUOTE . SEXP); (eval SEXP) gives VALUE.
744 SEXP is an sexp that when evaluated yields VALUE.
745 QUOTE may be `may' (value may be quoted),
746 `must' (value must be quoted), or nil (value must not be quoted)."
747 (cond
748 ((or (numberp value) (null value) (eq t value) (keywordp value))
749 (cons 'may value))
750 ((stringp value)
751 (let ((copy (copy-sequence value)))
752 (set-text-properties 0 (length copy) nil copy)
753 ;; Get rid of text properties because we cannot read them.
754 (cons 'may copy)))
755 ((symbolp value)
756 (cons 'must value))
757 ((vectorp value)
758 (let* ((pass1 (mapcar #'desktop--v2s value))
759 (special (assq nil pass1)))
760 (if special
761 (cons nil `(vector
762 ,@(mapcar (lambda (el)
763 (if (eq (car el) 'must)
764 `',(cdr el) (cdr el)))
765 pass1)))
766 (cons 'may `[,@(mapcar #'cdr pass1)]))))
767 ((consp value)
768 (let ((p value)
769 newlist
770 use-list*)
771 (while (consp p)
772 (let ((q.sexp (desktop--v2s (car p))))
773 (push q.sexp newlist))
774 (setq p (cdr p)))
775 (when p
776 (let ((last (desktop--v2s p)))
777 (setq use-list* t)
778 (push last newlist)))
779 (if (assq nil newlist)
780 (cons nil
781 `(,(if use-list* 'desktop-list* 'list)
782 ,@(mapcar (lambda (el)
783 (if (eq (car el) 'must)
784 `',(cdr el) (cdr el)))
785 (nreverse newlist))))
786 (cons 'must
787 `(,@(mapcar #'cdr
788 (nreverse (if use-list* (cdr newlist) newlist)))
789 ,@(if use-list* (cdar newlist)))))))
790 ((subrp value)
791 (cons nil `(symbol-function
792 ',(intern-soft (substring (prin1-to-string value) 7 -1)))))
793 ((markerp value)
794 (let ((pos (marker-position value))
795 (buf (buffer-name (marker-buffer value))))
796 (cons nil
797 `(let ((mk (make-marker)))
798 (add-hook 'desktop-delay-hook
799 `(lambda ()
800 (set-marker ,mk ,,pos (get-buffer ,,buf))))
801 mk))))
802 (t ; Save as text.
803 (cons 'may "Unprintable entity"))))
805 ;; ----------------------------------------------------------------------------
806 (defun desktop-value-to-string (value)
807 "Convert VALUE to a string that when read evaluates to the same value.
808 Not all types of values are supported."
809 (let* ((print-escape-newlines t)
810 (float-output-format nil)
811 (quote.sexp (desktop--v2s value))
812 (quote (car quote.sexp))
813 (txt
814 (let ((print-quoted t))
815 (prin1-to-string (cdr quote.sexp)))))
816 (if (eq quote 'must)
817 (concat "'" txt)
818 txt)))
820 ;; ----------------------------------------------------------------------------
821 (defun desktop-outvar (varspec)
822 "Output a setq statement for variable VAR to the desktop file.
823 The argument VARSPEC may be the variable name VAR (a symbol),
824 or a cons cell of the form (VAR . MAX-SIZE),
825 which means to truncate VAR's value to at most MAX-SIZE elements
826 \(if the value is a list) before saving the value."
827 (let (var size)
828 (if (consp varspec)
829 (setq var (car varspec) size (cdr varspec))
830 (setq var varspec))
831 (when (boundp var)
832 (when (and (integerp size)
833 (> size 0)
834 (listp (eval var)))
835 (desktop-truncate (eval var) size))
836 (insert "(setq "
837 (symbol-name var)
839 (desktop-value-to-string (symbol-value var))
840 ")\n"))))
842 ;; ----------------------------------------------------------------------------
843 (defun desktop-save-buffer-p (filename bufname mode &rest _dummy)
844 "Return t if buffer should have its state saved in the desktop file.
845 FILENAME is the visited file name, BUFNAME is the buffer name, and
846 MODE is the major mode.
847 \n\(fn FILENAME BUFNAME MODE)"
848 (let ((case-fold-search nil)
849 dired-skip)
850 (and (not (and (stringp desktop-buffers-not-to-save)
851 (not filename)
852 (string-match-p desktop-buffers-not-to-save bufname)))
853 (not (memq mode desktop-modes-not-to-save))
854 ;; FIXME this is broken if desktop-files-not-to-save is nil.
855 (or (and filename
856 (stringp desktop-files-not-to-save)
857 (not (string-match-p desktop-files-not-to-save filename)))
858 (and (memq mode '(dired-mode vc-dir-mode))
859 (with-current-buffer bufname
860 (not (setq dired-skip
861 (string-match-p desktop-files-not-to-save
862 default-directory)))))
863 (and (null filename)
864 (null dired-skip) ; bug#5755
865 (with-current-buffer bufname desktop-save-buffer))))))
867 ;; ----------------------------------------------------------------------------
868 (defun desktop-file-name (filename dirname)
869 "Convert FILENAME to format specified in `desktop-file-name-format'.
870 DIRNAME must be the directory in which the desktop file will be saved."
871 (cond
872 ((not filename) nil)
873 ((eq desktop-file-name-format 'tilde)
874 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
875 (cond
876 ((file-name-absolute-p relative-name) relative-name)
877 ((string= "./" relative-name) "~/")
878 ((string= "." relative-name) "~")
879 (t (concat "~/" relative-name)))))
880 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
881 (t (expand-file-name filename))))
884 ;; ----------------------------------------------------------------------------
885 (defvar desktop-filter-parameters-alist
886 '((background-color . desktop--filter-*-color)
887 (buffer-list . t)
888 (buffer-predicate . t)
889 (buried-buffer-list . t)
890 (desktop-font . desktop--filter-restore-desktop-parm)
891 (desktop-fullscreen . desktop--filter-restore-desktop-parm)
892 (desktop-height . desktop--filter-restore-desktop-parm)
893 (desktop-width . desktop--filter-restore-desktop-parm)
894 (font . desktop--filter-save-desktop-parm)
895 (font-backend . t)
896 (foreground-color . desktop--filter-*-color)
897 (fullscreen . desktop--filter-save-desktop-parm)
898 (height . desktop--filter-save-desktop-parm)
899 (minibuffer . desktop--filter-minibuffer)
900 (name . t)
901 (outer-window-id . t)
902 (parent-id . t)
903 (tty . desktop--filter-tty*)
904 (tty-type . desktop--filter-tty*)
905 (width . desktop--filter-save-desktop-parm)
906 (window-id . t)
907 (window-system . t))
908 "Alist of frame parameters and filtering functions.
910 Each element is a cons (PARAM . FILTER), where PARAM is a parameter
911 name (a symbol identifying a frame parameter), and FILTER can be t
912 \(meaning the parameter is removed from the parameter list on saving
913 and restoring), or a function that will be called with three args:
915 CURRENT a cons (PARAM . VALUE), where PARAM is the one being
916 filtered and VALUE is its current value
917 PARAMETERS the complete alist of parameters being filtered
918 SAVING non-nil if filtering before saving state, nil otherwise
920 The FILTER function must return:
921 nil CURRENT is removed from the list
922 t CURRENT is left as is
923 (PARAM' . VALUE') replace CURRENT with this
925 Frame parameters not on this list are passed intact.")
927 (defvar desktop--target-display nil
928 "Either (minibuffer . VALUE) or nil.
929 This refers to the current frame config being processed inside
930 `frame--restore-frames' and its auxiliary functions (like filtering).
931 If nil, there is no need to change the display.
932 If non-nil, display parameter to use when creating the frame.
933 Internal use only.")
935 (defun desktop-switch-to-gui-p (parameters)
936 "True when switching to a graphic display.
937 Return t if PARAMETERS describes a text-only terminal and
938 the target is a graphic display; otherwise return nil.
939 Only meaningful when called from a filtering function in
940 `desktop-filter-parameters-alist'."
941 (and desktop--target-display ; we're switching
942 (null (cdr (assq 'display parameters))) ; from a tty
943 (cdr desktop--target-display))) ; to a GUI display
945 (defun desktop-switch-to-tty-p (parameters)
946 "True when switching to a text-only terminal.
947 Return t if PARAMETERS describes a graphic display and
948 the target is a text-only terminal; otherwise return nil.
949 Only meaningful when called from a filtering function in
950 `desktop-filter-parameters-alist'."
951 (and desktop--target-display ; we're switching
952 (cdr (assq 'display parameters)) ; from a GUI display
953 (null (cdr desktop--target-display)))) ; to a tty
955 (defun desktop--filter-tty* (_current parameters saving)
956 ;; Remove tty and tty-type parameters when switching
957 ;; to a GUI frame.
958 (or saving
959 (not (desktop-switch-to-gui-p parameters))))
961 (defun desktop--filter-*-color (current parameters saving)
962 ;; Remove (foreground|background)-color parameters
963 ;; when switching to a GUI frame if they denote an
964 ;; "unspecified" color.
965 (or saving
966 (not (desktop-switch-to-gui-p parameters))
967 (not (stringp (cdr current)))
968 (not (string-match-p "^unspecified-[fb]g$" (cdr current)))))
970 (defun desktop--filter-minibuffer (current _parameters saving)
971 ;; When minibuffer is a window, save it as minibuffer . t
972 (or (not saving)
973 (if (windowp (cdr current))
974 '(minibuffer . t)
975 t)))
977 (defun desktop--filter-restore-desktop-parm (current parameters saving)
978 ;; When switching to a GUI frame, convert desktop-XXX parameter to XXX
979 (or saving
980 (not (desktop-switch-to-gui-p parameters))
981 (let ((val (cdr current)))
982 (if (eq val :desktop-processed)
984 (cons (intern (substring (symbol-name (car current))
985 8)) ;; (length "desktop-")
986 val)))))
988 (defun desktop--filter-save-desktop-parm (current parameters saving)
989 ;; When switching to a tty frame, save parameter XXX as desktop-XXX so it
990 ;; can be restored in a subsequent GUI session, unless it already exists.
991 (cond (saving t)
992 ((desktop-switch-to-tty-p parameters)
993 (let ((sym (intern (format "desktop-%s" (car current)))))
994 (if (assq sym parameters)
996 (cons sym (cdr current)))))
997 ((desktop-switch-to-gui-p parameters)
998 (let* ((dtp (assq (intern (format "desktop-%s" (car current)))
999 parameters))
1000 (val (cdr dtp)))
1001 (if (eq val :desktop-processed)
1003 (setcdr dtp :desktop-processed)
1004 (cons (car current) val))))
1005 (t t)))
1007 (defun desktop-restore-in-original-display-p ()
1008 "True if saved frames' displays should be honored."
1009 (cond ((daemonp) t)
1010 ((eq system-type 'windows-nt) nil)
1011 (t (null desktop-restore-in-current-display))))
1013 (defun desktop--filter-frame-parms (parameters saving)
1014 "Filter frame parameters and return filtered list.
1015 PARAMETERS is a parameter alist as returned by `frame-parameters'.
1016 If SAVING is non-nil, filtering is happening before saving frame state;
1017 otherwise, filtering is being done before restoring frame state.
1018 Parameters are filtered according to the setting of
1019 `desktop-filter-parameters-alist' (which see).
1020 Internal use only."
1021 (let ((filtered nil))
1022 (dolist (param parameters)
1023 (let ((filter (cdr (assq (car param) desktop-filter-parameters-alist)))
1024 this)
1025 (cond (;; no filter: pass param
1026 (null filter)
1027 (push param filtered))
1028 (;; filter = t; skip param
1029 (eq filter t))
1030 (;; filter func returns nil: skip param
1031 (null (setq this (funcall filter param parameters saving))))
1032 (;; filter func returns t: pass param
1033 (eq this t)
1034 (push param filtered))
1035 (;; filter func returns a new param: use it
1037 (push this filtered)))))
1038 ;; Set the display parameter after filtering, so that filter functions
1039 ;; have access to its original value.
1040 (when desktop--target-display
1041 (let ((display (assq 'display filtered)))
1042 (if display
1043 (setcdr display (cdr desktop--target-display))
1044 (push desktop--target-display filtered))))
1045 filtered))
1047 (defun desktop--save-minibuffer-frames ()
1048 ;; Adds a desktop-mini parameter to frames
1049 ;; desktop-mini is a list (MINIBUFFER NUMBER DEFAULT?) where
1050 ;; MINIBUFFER t if the frame (including minibuffer-only) owns a minibuffer
1051 ;; NUMBER if MINIBUFFER = t, an ID for the frame; if nil, the ID of
1052 ;; the frame containing the minibuffer used by this frame
1053 ;; DEFAULT? if t, this frame is the value of default-minibuffer-frame
1054 ;; FIXME: What happens with multi-terminal sessions?
1055 (let ((frames (frame-list))
1056 (count 0))
1057 ;; Reset desktop-mini for all frames
1058 (dolist (frame frames)
1059 (set-frame-parameter frame 'desktop-mini nil))
1060 ;; Number all frames with its own minibuffer
1061 (dolist (frame (minibuffer-frame-list))
1062 (set-frame-parameter frame 'desktop-mini
1063 (list t
1064 (setq count (1+ count))
1065 (eq frame default-minibuffer-frame))))
1066 ;; Now link minibufferless frames with their minibuffer frames
1067 (dolist (frame frames)
1068 (unless (frame-parameter frame 'desktop-mini)
1069 (let* ((mb-frame (window-frame (minibuffer-window frame)))
1070 (this (cadr (frame-parameter mb-frame 'desktop-mini))))
1071 (set-frame-parameter frame 'desktop-mini (list nil this nil)))))))
1073 (defun desktop--save-frames ()
1074 "Save window/frame state, as a global variable.
1075 Intended to be called from `desktop-save'.
1076 Internal use only."
1077 (setq desktop--saved-states
1078 (and desktop-restore-frames
1079 (progn
1080 (desktop--save-minibuffer-frames)
1081 (mapcar (lambda (frame)
1082 (cons (desktop--filter-frame-parms (frame-parameters frame) t)
1083 (window-state-get (frame-root-window frame) t)))
1084 (frame-list)))))
1085 (unless (memq 'desktop--saved-states desktop-globals-to-save)
1086 (desktop-outvar 'desktop--saved-states)))
1088 ;;;###autoload
1089 (defun desktop-save (dirname &optional release auto-save)
1090 "Save the desktop in a desktop file.
1091 Parameter DIRNAME specifies where to save the desktop file.
1092 Optional parameter RELEASE says whether we're done with this desktop.
1093 If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
1094 and don't save the buffer if they are the same."
1095 (interactive "DDirectory to save desktop file in: ")
1096 (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
1097 (save-excursion
1098 (let ((eager desktop-restore-eager)
1099 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
1100 (when
1101 (or (not new-modtime) ; nothing to overwrite
1102 (equal desktop-file-modtime new-modtime)
1103 (yes-or-no-p (if desktop-file-modtime
1104 (if (> (float-time new-modtime) (float-time desktop-file-modtime))
1105 "Desktop file is more recent than the one loaded. Save anyway? "
1106 "Desktop file isn't the one loaded. Overwrite it? ")
1107 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
1108 (unless release (error "Desktop file conflict")))
1110 ;; If we're done with it, release the lock.
1111 ;; Otherwise, claim it if it's unclaimed or if we created it.
1112 (if release
1113 (desktop-release-lock)
1114 (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
1116 (with-temp-buffer
1117 (insert
1118 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
1119 desktop-header
1120 ";; Created " (current-time-string) "\n"
1121 ";; Desktop file format version " desktop-file-version "\n"
1122 ";; Emacs version " emacs-version "\n")
1123 (save-excursion (run-hooks 'desktop-save-hook))
1124 (goto-char (point-max))
1125 (insert "\n;; Global section:\n")
1126 ;; Called here because we save the window/frame state as a global
1127 ;; variable for compatibility with previous Emacsen.
1128 (desktop--save-frames)
1129 (mapc (function desktop-outvar) desktop-globals-to-save)
1130 (when (memq 'kill-ring desktop-globals-to-save)
1131 (insert
1132 "(setq kill-ring-yank-pointer (nthcdr "
1133 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
1134 " kill-ring))\n"))
1136 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
1137 (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
1138 (let ((base (pop l)))
1139 (when (apply 'desktop-save-buffer-p l)
1140 (insert "("
1141 (if (or (not (integerp eager))
1142 (if (zerop eager)
1144 (setq eager (1- eager))))
1145 "desktop-create-buffer"
1146 "desktop-append-buffer-args")
1148 desktop-file-version)
1149 ;; If there's a non-empty base name, we save it instead of the buffer name
1150 (when (and base (not (string= base "")))
1151 (setcar (nthcdr 1 l) base))
1152 (dolist (e l)
1153 (insert "\n " (desktop-value-to-string e)))
1154 (insert ")\n\n"))))
1156 (setq default-directory desktop-dirname)
1157 ;; If auto-saving, avoid writing if nothing has changed since the last write.
1158 ;; Don't check 300 characters of the header that contains the timestamp.
1159 (let ((checksum (and auto-save (md5 (current-buffer)
1160 (+ (point-min) 300) (point-max)
1161 'emacs-mule))))
1162 (unless (and auto-save (equal checksum desktop-file-checksum))
1163 (let ((coding-system-for-write 'emacs-mule))
1164 (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
1165 (setq desktop-file-checksum checksum)
1166 ;; We remember when it was modified (which is presumably just now).
1167 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))))
1169 ;; ----------------------------------------------------------------------------
1170 ;;;###autoload
1171 (defun desktop-remove ()
1172 "Delete desktop file in `desktop-dirname'.
1173 This function also sets `desktop-dirname' to nil."
1174 (interactive)
1175 (when desktop-dirname
1176 (let ((filename (desktop-full-file-name)))
1177 (setq desktop-dirname nil)
1178 (when (file-exists-p filename)
1179 (delete-file filename)))))
1181 (defvar desktop-buffer-args-list nil
1182 "List of args for `desktop-create-buffer'.")
1184 (defvar desktop-lazy-timer nil)
1186 ;; ----------------------------------------------------------------------------
1187 (defvar desktop--reuse-list nil
1188 "Internal use only.")
1190 (defun desktop--find-frame (predicate display &rest args)
1191 "Find a suitable frame in `desktop--reuse-list'.
1192 Look through frames whose display property matches DISPLAY and
1193 return the first one for which (PREDICATE frame ARGS) returns t.
1194 If PREDICATE is nil, it is always satisfied. Internal use only.
1195 This is an auxiliary function for `desktop--select-frame'."
1196 (catch :found
1197 (dolist (frame desktop--reuse-list)
1198 (when (and (equal (frame-parameter frame 'display) display)
1199 (or (null predicate)
1200 (apply predicate frame args)))
1201 (throw :found frame)))
1202 nil))
1204 (defun desktop--select-frame (display frame-cfg)
1205 "Look for an existing frame to reuse.
1206 DISPLAY is the display where the frame will be shown, and FRAME-CFG
1207 is the parameter list of the frame being restored. Internal use only."
1208 (if (eq desktop-restoring-reuses-frames t)
1209 (let ((frame nil)
1210 mini)
1211 ;; There are no fancy heuristics there. We could implement some
1212 ;; based on frame size and/or position, etc., but it is not clear
1213 ;; that any "gain" (in the sense of reduced flickering, etc.) is
1214 ;; worth the added complexity. In fact, the code below mainly
1215 ;; tries to work nicely when M-x desktop-read is used after a desktop
1216 ;; session has already been loaded. The other main use case, which
1217 ;; is the initial desktop-read upon starting Emacs, should usually
1218 ;; only have one, or very few, frame(s) to reuse.
1219 (cond (;; When the target is tty, every existing frame is reusable.
1220 (null display)
1221 (setq frame (desktop--find-frame nil display)))
1222 (;; If the frame has its own minibuffer, let's see whether
1223 ;; that frame has already been loaded (which can happen after
1224 ;; M-x desktop-read).
1225 (car (setq mini (cdr (assq 'desktop-mini frame-cfg))))
1226 (setq frame (or (desktop--find-frame
1227 (lambda (f m)
1228 (equal (frame-parameter f 'desktop-mini) m))
1229 display mini))))
1230 (;; For minibufferless frames, check whether they already exist,
1231 ;; and that they are linked to the right minibuffer frame.
1232 mini
1233 (setq frame (desktop--find-frame
1234 (lambda (f n)
1235 (let ((m (frame-parameter f 'desktop-mini)))
1236 (and m
1237 (null (car m))
1238 (= (cadr m) n)
1239 (equal (cadr (frame-parameter
1240 (window-frame (minibuffer-window f))
1241 'desktop-mini))
1242 n))))
1243 display (cadr mini))))
1244 (;; Default to just finding a frame in the same display.
1246 (setq frame (desktop--find-frame nil display))))
1247 ;; If found, remove from the list.
1248 (when frame
1249 (setq desktop--reuse-list (delq frame desktop--reuse-list)))
1250 frame)
1251 nil))
1253 (defun desktop--make-frame (frame-cfg window-cfg)
1254 "Set up a frame according to its saved state.
1255 That means either creating a new frame or reusing an existing one.
1256 FRAME-CFG is the parameter list of the new frame; WINDOW-CFG is
1257 its window state. Internal use only."
1258 (let* ((fullscreen (cdr (assq 'fullscreen frame-cfg)))
1259 (lines (assq 'tool-bar-lines frame-cfg))
1260 (filtered-cfg (desktop--filter-frame-parms frame-cfg nil))
1261 (display (cdr (assq 'display filtered-cfg))) ;; post-filtering
1262 alt-cfg frame)
1264 ;; This works around bug#14795 (or feature#14795, if not a bug :-)
1265 (setq filtered-cfg (assq-delete-all 'tool-bar-lines filtered-cfg))
1266 (push '(tool-bar-lines . 0) filtered-cfg)
1268 (when fullscreen
1269 ;; Currently Emacs has the limitation that it does not record the size
1270 ;; and position of a frame before maximizing it, so we cannot save &
1271 ;; restore that info. Instead, when restoring, we resort to creating
1272 ;; invisible "fullscreen" frames of default size and then maximizing them
1273 ;; (and making them visible) which at least is somewhat user-friendly
1274 ;; when these frames are later de-maximized.
1275 (let ((width (and (eq fullscreen 'fullheight) (cdr (assq 'width filtered-cfg))))
1276 (height (and (eq fullscreen 'fullwidth) (cdr (assq 'height filtered-cfg))))
1277 (visible (assq 'visibility filtered-cfg)))
1278 (dolist (parameter '(visibility fullscreen width height))
1279 (setq filtered-cfg (assq-delete-all parameter filtered-cfg)))
1280 (when width
1281 (setq filtered-cfg (append `((user-size . t) (width . ,width))
1282 filtered-cfg)))
1283 (when height
1284 (setq filtered-cfg (append `((user-size . t) (height . ,height))
1285 filtered-cfg)))
1286 ;; These are parameters to apply after creating/setting the frame.
1287 (push visible alt-cfg)
1288 (push (cons 'fullscreen fullscreen) alt-cfg)))
1290 ;; Time to select or create a frame an apply the big bunch of parameters
1291 (if (setq frame (desktop--select-frame display filtered-cfg))
1292 (modify-frame-parameters frame filtered-cfg)
1293 (setq frame (make-frame-on-display display filtered-cfg)))
1295 ;; Let's give the finishing touches (visibility, tool-bar, maximization).
1296 (when lines (push lines alt-cfg))
1297 (when alt-cfg (modify-frame-parameters frame alt-cfg))
1298 ;; Now restore window state.
1299 (window-state-put window-cfg (frame-root-window frame) 'safe)
1300 frame))
1302 (defun desktop--sort-states (state1 state2)
1303 ;; Order: default minibuffer frame
1304 ;; other frames with minibuffer, ascending ID
1305 ;; minibufferless frames, ascending ID
1306 (let ((dm1 (cdr (assq 'desktop-mini (car state1))))
1307 (dm2 (cdr (assq 'desktop-mini (car state2)))))
1308 (cond ((nth 2 dm1) t)
1309 ((nth 2 dm2) nil)
1310 ((null (car dm2)) t)
1311 ((null (car dm1)) nil)
1312 (t (< (cadr dm1) (cadr dm2))))))
1314 (defun desktop--restore-frames ()
1315 "Restore window/frame configuration.
1316 Internal use only."
1317 (when (and desktop-restore-frames desktop--saved-states)
1318 (let* ((frame-mb-map nil) ;; Alist of frames with their own minibuffer
1319 (visible nil)
1320 (delete-saved (eq desktop-restore-in-current-display 'delete))
1321 (forcing (not (desktop-restore-in-original-display-p)))
1322 (target (and forcing (cons 'display (frame-parameter nil 'display)))))
1324 ;; Sorting saved states allows us to easily restore minibuffer-owning frames
1325 ;; before minibufferless ones.
1326 (setq desktop--saved-states (sort desktop--saved-states #'desktop--sort-states))
1327 ;; Potentially all existing frames are reusable. Later we will decide which ones
1328 ;; to reuse, and how to deal with any leftover.
1329 (setq desktop--reuse-list (frame-list))
1331 (dolist (state desktop--saved-states)
1332 (condition-case err
1333 (let* ((frame-cfg (car state))
1334 (window-cfg (cdr state))
1335 (d-mini (cdr (assq 'desktop-mini frame-cfg)))
1336 num frame to-tty)
1337 ;; Only set target if forcing displays and the target display is different.
1338 (if (or (not forcing)
1339 (equal target (or (assq 'display frame-cfg) '(display . nil))))
1340 (setq desktop--target-display nil)
1341 (setq desktop--target-display target
1342 to-tty (null (cdr target))))
1343 ;; Time to restore frames and set up their minibuffers as they were.
1344 ;; We only skip a frame (thus deleting it) if either:
1345 ;; - we're switching displays, and the user chose the option to delete, or
1346 ;; - we're switching to tty, and the frame to restore is minibuffer-only.
1347 (unless (and desktop--target-display
1348 (or delete-saved
1349 (and to-tty
1350 (eq (cdr (assq 'minibuffer frame-cfg)) 'only))))
1352 ;; Restore minibuffers. Some of this stuff could be done in a filter
1353 ;; function, but it would be messy because restoring minibuffers affects
1354 ;; global state; it's best to do it here than add a bunch of global
1355 ;; variables to pass info back-and-forth to/from the filter function.
1356 (cond
1357 ((null d-mini)) ;; No desktop-mini. Process as normal frame.
1358 (to-tty) ;; Ignore minibuffer stuff and process as normal frame.
1359 ((car d-mini) ;; Frame has its own minibuffer (or it is minibuffer-only).
1360 (setq num (cadr d-mini))
1361 (when (eq (cdr (assq 'minibuffer frame-cfg)) 'only)
1362 (setq frame-cfg (append '((tool-bar-lines . 0) (menu-bar-lines . 0))
1363 frame-cfg))))
1364 (t ;; Frame depends on other frame's minibufer window.
1365 (let ((mb-frame (cdr (assq (cadr d-mini) frame-mb-map))))
1366 (unless (frame-live-p mb-frame)
1367 (error "Minibuffer frame %s not found" (cadr d-mini)))
1368 (let ((mb-param (assq 'minibuffer frame-cfg))
1369 (mb-window (minibuffer-window mb-frame)))
1370 (unless (and (window-live-p mb-window)
1371 (window-minibuffer-p mb-window))
1372 (error "Not a minibuffer window %s" mb-window))
1373 (if mb-param
1374 (setcdr mb-param mb-window)
1375 (push (cons 'minibuffer mb-window) frame-cfg))))))
1376 ;; OK, we're ready at last to create (or reuse) a frame and
1377 ;; restore the window config.
1378 (setq frame (desktop--make-frame frame-cfg window-cfg))
1379 ;; Set default-minibuffer if required.
1380 (when (nth 2 d-mini) (setq default-minibuffer-frame frame))
1381 ;; Store frame/NUM to assign to minibufferless frames.
1382 (when num (push (cons num frame) frame-mb-map))
1383 ;; Try to locate at least one visible frame.
1384 (when (and (not visible) (frame-visible-p frame))
1385 (setq visible frame))))
1386 (error
1387 (delay-warning 'desktop (error-message-string err) :error))))
1389 ;; Delete remaining frames, but do not fail if some resist being deleted.
1390 (unless (eq desktop-restoring-reuses-frames 'keep)
1391 (dolist (frame desktop--reuse-list)
1392 (ignore-errors (delete-frame frame))))
1393 (setq desktop--reuse-list nil)
1394 ;; Make sure there's at least one visible frame, and select it.
1395 (unless (or visible (daemonp))
1396 (setq visible (if (frame-live-p default-minibuffer-frame)
1397 default-minibuffer-frame
1398 (car (frame-list))))
1399 (make-frame-visible visible)
1400 (select-frame-set-input-focus visible)))))
1402 ;;;###autoload
1403 (defun desktop-read (&optional dirname)
1404 "Read and process the desktop file in directory DIRNAME.
1405 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
1406 directories listed in `desktop-path'. If a desktop file is found, it
1407 is processed and `desktop-after-read-hook' is run. If no desktop file
1408 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
1409 This function is a no-op when Emacs is running in batch mode.
1410 It returns t if a desktop file was loaded, nil otherwise."
1411 (interactive)
1412 (unless noninteractive
1413 (setq desktop-dirname
1414 (file-name-as-directory
1415 (expand-file-name
1417 ;; If DIRNAME is specified, use it.
1418 (and (< 0 (length dirname)) dirname)
1419 ;; Otherwise search desktop file in desktop-path.
1420 (let ((dirs desktop-path))
1421 (while (and dirs
1422 (not (file-exists-p
1423 (desktop-full-file-name (car dirs)))))
1424 (setq dirs (cdr dirs)))
1425 (and dirs (car dirs)))
1426 ;; If not found and `desktop-path' is non-nil, use its first element.
1427 (and desktop-path (car desktop-path))
1428 ;; Default: .emacs.d.
1429 user-emacs-directory))))
1430 (if (file-exists-p (desktop-full-file-name))
1431 ;; Desktop file found, but is it already in use?
1432 (let ((desktop-first-buffer nil)
1433 (desktop-buffer-ok-count 0)
1434 (desktop-buffer-fail-count 0)
1435 (owner (desktop-owner))
1436 ;; Avoid desktop saving during evaluation of desktop buffer.
1437 (desktop-save nil))
1438 (if (and owner
1439 (memq desktop-load-locked-desktop '(nil ask))
1440 (or (null desktop-load-locked-desktop)
1441 (daemonp)
1442 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
1443 Using it may cause conflicts. Use it anyway? " owner)))))
1444 (let ((default-directory desktop-dirname))
1445 (setq desktop-dirname nil)
1446 (run-hooks 'desktop-not-loaded-hook)
1447 (unless desktop-dirname
1448 (message "Desktop file in use; not loaded.")))
1449 (desktop-lazy-abort)
1450 ;; Evaluate desktop buffer and remember when it was modified.
1451 (load (desktop-full-file-name) t t t)
1452 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
1453 ;; If it wasn't already, mark it as in-use, to bother other
1454 ;; desktop instances.
1455 (unless owner
1456 (condition-case nil
1457 (desktop-claim-lock)
1458 (file-error (message "Couldn't record use of desktop file")
1459 (sit-for 1))))
1461 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1462 ;; We want buffers existing prior to evaluating the desktop (and
1463 ;; not reused) to be placed at the end of the buffer list, so we
1464 ;; move them here.
1465 (mapc 'bury-buffer
1466 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
1467 (switch-to-buffer (car (buffer-list)))
1468 (run-hooks 'desktop-delay-hook)
1469 (setq desktop-delay-hook nil)
1470 (desktop--restore-frames)
1471 (run-hooks 'desktop-after-read-hook)
1472 (message "Desktop: %d buffer%s restored%s%s."
1473 desktop-buffer-ok-count
1474 (if (= 1 desktop-buffer-ok-count) "" "s")
1475 (if (< 0 desktop-buffer-fail-count)
1476 (format ", %d failed to restore" desktop-buffer-fail-count)
1478 (if desktop-buffer-args-list
1479 (format ", %d to restore lazily"
1480 (length desktop-buffer-args-list))
1481 ""))
1482 ;; Bury the *Messages* buffer to not reshow it when burying
1483 ;; the buffer we switched to above.
1484 (when (buffer-live-p (get-buffer "*Messages*"))
1485 (bury-buffer "*Messages*"))
1486 ;; Clear all windows' previous and next buffers, these have
1487 ;; been corrupted by the `switch-to-buffer' calls in
1488 ;; `desktop-restore-file-buffer' (bug#11556). This is a
1489 ;; brute force fix and should be replaced by a more subtle
1490 ;; strategy eventually.
1491 (walk-window-tree (lambda (window)
1492 (set-window-prev-buffers window nil)
1493 (set-window-next-buffers window nil)))
1495 ;; No desktop file found.
1496 (desktop-clear)
1497 (let ((default-directory desktop-dirname))
1498 (run-hooks 'desktop-no-desktop-file-hook))
1499 (message "No desktop file.")
1500 nil)))
1502 ;; ----------------------------------------------------------------------------
1503 ;; Maintained for backward compatibility
1504 ;;;###autoload
1505 (defun desktop-load-default ()
1506 "Load the `default' start-up library manually.
1507 Also inhibit further loading of it."
1508 (declare (obsolete desktop-save-mode "22.1"))
1509 (unless inhibit-default-init ; safety check
1510 (load "default" t t)
1511 (setq inhibit-default-init t)))
1513 ;; ----------------------------------------------------------------------------
1514 ;;;###autoload
1515 (defun desktop-change-dir (dirname)
1516 "Change to desktop saved in DIRNAME.
1517 Kill the desktop as specified by variables `desktop-save-mode' and
1518 `desktop-save', then clear the desktop and load the desktop file in
1519 directory DIRNAME."
1520 (interactive "DChange to directory: ")
1521 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
1522 (desktop-kill)
1523 (desktop-clear)
1524 (desktop-read dirname))
1526 ;; ----------------------------------------------------------------------------
1527 ;;;###autoload
1528 (defun desktop-save-in-desktop-dir ()
1529 "Save the desktop in directory `desktop-dirname'."
1530 (interactive)
1531 (if desktop-dirname
1532 (desktop-save desktop-dirname)
1533 (call-interactively 'desktop-save))
1534 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname)))
1536 ;; ----------------------------------------------------------------------------
1537 ;; Auto-Saving.
1538 (defvar desktop-auto-save-timer nil)
1540 (defun desktop-auto-save ()
1541 "Save the desktop periodically.
1542 Called by the timer created in `desktop-auto-save-set-timer'."
1543 (when (and desktop-save-mode
1544 (integerp desktop-auto-save-timeout)
1545 (> desktop-auto-save-timeout 0)
1546 ;; Avoid desktop saving during lazy loading.
1547 (not desktop-lazy-timer)
1548 ;; Save only to own desktop file.
1549 (eq (emacs-pid) (desktop-owner))
1550 desktop-dirname)
1551 (desktop-save desktop-dirname nil t))
1552 (desktop-auto-save-set-timer))
1554 (defun desktop-auto-save-set-timer ()
1555 "Reset the auto-save timer.
1556 Cancel any previous timer. When `desktop-auto-save-timeout' is a positive
1557 integer, start a new timer to call `desktop-auto-save' in that many seconds."
1558 (when desktop-auto-save-timer
1559 (cancel-timer desktop-auto-save-timer)
1560 (setq desktop-auto-save-timer nil))
1561 (when (and (integerp desktop-auto-save-timeout)
1562 (> desktop-auto-save-timeout 0))
1563 (setq desktop-auto-save-timer
1564 (run-with-timer desktop-auto-save-timeout nil
1565 'desktop-auto-save))))
1567 ;; ----------------------------------------------------------------------------
1568 ;;;###autoload
1569 (defun desktop-revert ()
1570 "Revert to the last loaded desktop."
1571 (interactive)
1572 (unless desktop-dirname
1573 (error "Unknown desktop directory"))
1574 (unless (file-exists-p (desktop-full-file-name))
1575 (error "No desktop file found"))
1576 (desktop-clear)
1577 (desktop-read desktop-dirname))
1579 (defvar desktop-buffer-major-mode)
1580 (defvar desktop-buffer-locals)
1581 (defvar auto-insert) ; from autoinsert.el
1582 ;; ----------------------------------------------------------------------------
1583 (defun desktop-restore-file-buffer (buffer-filename
1584 _buffer-name
1585 _buffer-misc)
1586 "Restore a file buffer."
1587 (when buffer-filename
1588 (if (or (file-exists-p buffer-filename)
1589 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1590 buffer-filename)))
1591 (if desktop-missing-file-warning
1592 (y-or-n-p (concat msg " Re-create buffer? "))
1593 (message "%s" msg)
1594 nil)))
1595 (let* ((auto-insert nil) ; Disable auto insertion
1596 (coding-system-for-read
1597 (or coding-system-for-read
1598 (cdr (assq 'buffer-file-coding-system
1599 desktop-buffer-locals))))
1600 (buf (find-file-noselect buffer-filename)))
1601 (condition-case nil
1602 (switch-to-buffer buf)
1603 (error (pop-to-buffer buf)))
1604 (and (not (eq major-mode desktop-buffer-major-mode))
1605 (functionp desktop-buffer-major-mode)
1606 (funcall desktop-buffer-major-mode))
1607 buf)
1608 nil)))
1610 (defun desktop-load-file (function)
1611 "Load the file where auto loaded FUNCTION is defined."
1612 (when (fboundp function)
1613 (autoload-do-load (symbol-function function) function)))
1615 ;; ----------------------------------------------------------------------------
1616 ;; Create a buffer, load its file, set its mode, ...;
1617 ;; called from Desktop file only.
1619 ;; Just to silence the byte compiler.
1621 (defvar desktop-first-buffer) ; Dynamically bound in `desktop-read'
1623 ;; Bound locally in `desktop-read'.
1624 (defvar desktop-buffer-ok-count)
1625 (defvar desktop-buffer-fail-count)
1627 (defun desktop-create-buffer
1628 (file-version
1629 buffer-filename
1630 buffer-name
1631 buffer-majormode
1632 buffer-minormodes
1633 buffer-point
1634 buffer-mark
1635 buffer-readonly
1636 buffer-misc
1637 &optional
1638 buffer-locals)
1640 (let ((desktop-file-version file-version)
1641 (desktop-buffer-file-name buffer-filename)
1642 (desktop-buffer-name buffer-name)
1643 (desktop-buffer-major-mode buffer-majormode)
1644 (desktop-buffer-minor-modes buffer-minormodes)
1645 (desktop-buffer-point buffer-point)
1646 (desktop-buffer-mark buffer-mark)
1647 (desktop-buffer-read-only buffer-readonly)
1648 (desktop-buffer-misc buffer-misc)
1649 (desktop-buffer-locals buffer-locals))
1650 ;; To make desktop files with relative file names possible, we cannot
1651 ;; allow `default-directory' to change. Therefore we save current buffer.
1652 (save-current-buffer
1653 ;; Give major mode module a chance to add a handler.
1654 (desktop-load-file desktop-buffer-major-mode)
1655 (let ((buffer-list (buffer-list))
1656 (result
1657 (condition-case-unless-debug err
1658 (funcall (or (cdr (assq desktop-buffer-major-mode
1659 desktop-buffer-mode-handlers))
1660 'desktop-restore-file-buffer)
1661 desktop-buffer-file-name
1662 desktop-buffer-name
1663 desktop-buffer-misc)
1664 (error
1665 (message "Desktop: Can't load buffer %s: %s"
1666 desktop-buffer-name
1667 (error-message-string err))
1668 (when desktop-missing-file-warning (sit-for 1))
1669 nil))))
1670 (if (bufferp result)
1671 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1672 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1673 (setq result nil))
1674 ;; Restore buffer list order with new buffer at end. Don't change
1675 ;; the order for old desktop files (old desktop module behavior).
1676 (unless (< desktop-file-version 206)
1677 (mapc 'bury-buffer buffer-list)
1678 (when result (bury-buffer result)))
1679 (when result
1680 (unless (or desktop-first-buffer (< desktop-file-version 206))
1681 (setq desktop-first-buffer result))
1682 (set-buffer result)
1683 (unless (equal (buffer-name) desktop-buffer-name)
1684 (rename-buffer desktop-buffer-name t))
1685 ;; minor modes
1686 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1687 (auto-fill-mode 1))
1688 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1689 (auto-fill-mode 0))
1691 (dolist (minor-mode desktop-buffer-minor-modes)
1692 ;; Give minor mode module a chance to add a handler.
1693 (desktop-load-file minor-mode)
1694 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1695 (if handler
1696 (funcall handler desktop-buffer-locals)
1697 (when (functionp minor-mode)
1698 (funcall minor-mode 1)))))))
1699 ;; Even though point and mark are non-nil when written by
1700 ;; `desktop-save', they may be modified by handlers wanting to set
1701 ;; point or mark themselves.
1702 (when desktop-buffer-point
1703 (goto-char
1704 (condition-case err
1705 ;; Evaluate point. Thus point can be something like
1706 ;; '(search-forward ...
1707 (eval desktop-buffer-point)
1708 (error (message "%s" (error-message-string err)) 1))))
1709 (when desktop-buffer-mark
1710 (if (consp desktop-buffer-mark)
1711 (progn
1712 (set-mark (car desktop-buffer-mark))
1713 (setq mark-active (car (cdr desktop-buffer-mark))))
1714 (set-mark desktop-buffer-mark)))
1715 ;; Never override file system if the file really is read-only marked.
1716 (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1717 (while desktop-buffer-locals
1718 (let ((this (car desktop-buffer-locals)))
1719 (if (consp this)
1720 ;; an entry of this form `(symbol . value)'
1721 (progn
1722 (make-local-variable (car this))
1723 (set (car this) (cdr this)))
1724 ;; an entry of the form `symbol'
1725 (make-local-variable this)
1726 (makunbound this)))
1727 (setq desktop-buffer-locals (cdr desktop-buffer-locals))))))))
1729 ;; ----------------------------------------------------------------------------
1730 ;; Backward compatibility -- update parameters to 205 standards.
1731 (defun desktop-buffer (buffer-filename buffer-name buffer-majormode
1732 mim pt mk ro tl fc cfs cr buffer-misc)
1733 (desktop-create-buffer 205 buffer-filename buffer-name
1734 buffer-majormode (cdr mim) pt mk ro
1735 buffer-misc
1736 (list (cons 'truncate-lines tl)
1737 (cons 'fill-column fc)
1738 (cons 'case-fold-search cfs)
1739 (cons 'case-replace cr)
1740 (cons 'overwrite-mode (car mim)))))
1742 (defun desktop-append-buffer-args (&rest args)
1743 "Append ARGS at end of `desktop-buffer-args-list'.
1744 ARGS must be an argument list for `desktop-create-buffer'."
1745 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1746 (unless desktop-lazy-timer
1747 (setq desktop-lazy-timer
1748 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1750 (defun desktop-lazy-create-buffer ()
1751 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1752 (when desktop-buffer-args-list
1753 (let* ((remaining (length desktop-buffer-args-list))
1754 (args (pop desktop-buffer-args-list))
1755 (buffer-name (nth 2 args))
1756 (msg (format "Desktop lazily opening %s (%s remaining)..."
1757 buffer-name remaining)))
1758 (when desktop-lazy-verbose
1759 (message "%s" msg))
1760 (let ((desktop-first-buffer nil)
1761 (desktop-buffer-ok-count 0)
1762 (desktop-buffer-fail-count 0))
1763 (apply 'desktop-create-buffer args)
1764 (run-hooks 'desktop-delay-hook)
1765 (setq desktop-delay-hook nil)
1766 (bury-buffer (get-buffer buffer-name))
1767 (when desktop-lazy-verbose
1768 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1770 (defun desktop-idle-create-buffers ()
1771 "Create buffers until the user does something, then stop.
1772 If there are no buffers left to create, kill the timer."
1773 (let ((repeat 1))
1774 (while (and repeat desktop-buffer-args-list)
1775 (save-window-excursion
1776 (desktop-lazy-create-buffer))
1777 (setq repeat (sit-for 0.2))
1778 (unless desktop-buffer-args-list
1779 (cancel-timer desktop-lazy-timer)
1780 (setq desktop-lazy-timer nil)
1781 (message "Lazy desktop load complete")
1782 (sit-for 3)
1783 (message "")))))
1785 (defun desktop-lazy-complete ()
1786 "Run the desktop load to completion."
1787 (interactive)
1788 (let ((desktop-lazy-verbose t))
1789 (while desktop-buffer-args-list
1790 (save-window-excursion
1791 (desktop-lazy-create-buffer)))
1792 (message "Lazy desktop load complete")))
1794 (defun desktop-lazy-abort ()
1795 "Abort lazy loading of the desktop."
1796 (interactive)
1797 (when desktop-lazy-timer
1798 (cancel-timer desktop-lazy-timer)
1799 (setq desktop-lazy-timer nil))
1800 (when desktop-buffer-args-list
1801 (setq desktop-buffer-args-list nil)
1802 (when (called-interactively-p 'interactive)
1803 (message "Lazy desktop load aborted"))))
1805 ;; ----------------------------------------------------------------------------
1806 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1807 ;; command line, we do the rest of what it takes to use desktop, but do it
1808 ;; after finishing loading the init file.
1809 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1810 ;; functions are processed after `after-init-hook'.
1811 (add-hook
1812 'after-init-hook
1813 (lambda ()
1814 (let ((key "--no-desktop"))
1815 (when (member key command-line-args)
1816 (setq command-line-args (delete key command-line-args))
1817 (setq desktop-save-mode nil)))
1818 (when desktop-save-mode
1819 (desktop-read)
1820 (desktop-auto-save-set-timer)
1821 (setq inhibit-startup-screen t))))
1823 ;; So we can restore vc-dir buffers.
1824 (autoload 'vc-dir-mode "vc-dir" nil t)
1826 (provide 'desktop)
1828 ;;; desktop.el ends here