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