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