Add arch tagline
[emacs.git] / lisp / desktop.el
blobbddba82a9655548e39d26c63310f94b003e902e8
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 Free Software Foundation, Inc.
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Keywords: convenience
8 ;; Favourite-brand-of-beer: None, I hate beer.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Save the Desktop, i.e.,
30 ;; - some global variables
31 ;; - the list of buffers with associated files. For each buffer also
32 ;; - the major mode
33 ;; - the default directory
34 ;; - the point
35 ;; - the mark & mark-active
36 ;; - buffer-read-only
37 ;; - some local variables
39 ;; To use this, use customize to turn on desktop-save-mode or add the
40 ;; following line somewhere in your .emacs file:
42 ;; (desktop-save-mode 1)
44 ;; For further usage information, look at the section
45 ;; "Saving Emacs Sessions" in the GNU Emacs Manual.
47 ;; When the desktop module is loaded, the function `desktop-kill' is
48 ;; added to the `kill-emacs-hook'. This function is responsible for
49 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
50 ;; function is added to the `after-init-hook'. This function is
51 ;; responsible for loading the desktop when Emacs is started.
53 ;; Special handling.
54 ;; -----------------
55 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
56 ;; are supplied to handle special major and minor modes respectively.
57 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
58 ;; to restore a desktop buffer. Elements must have the form
60 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
62 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
63 ;; evaluates the desktop file. Buffers with a major mode not specified here,
64 ;; are restored by the default handler `desktop-restore-file-buffer'.
65 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
66 ;; non-standard minor modes. Elements must have the form
68 ;; (MINOR-MODE . RESTORE-FUNCTION).
70 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
71 ;; Minor modes not specified here, are restored by the standard minor mode
72 ;; function. If you write a module that defines a major or minor mode that
73 ;; needs a special handler, then place code like
75 ;; (defun foo-restore-desktop-buffer
76 ;; ...
77 ;; (add-to-list 'desktop-buffer-mode-handlers
78 ;; '(foo-mode . foo-restore-desktop-buffer))
80 ;; or
82 ;; (defun bar-desktop-restore
83 ;; ...
84 ;; (add-to-list 'desktop-minor-mode-handlers
85 ;; '(bar-mode . bar-desktop-restore))
87 ;; in the module itself, and make shure that the mode function is
88 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
89 ;; `desktop-minor-mode-handlers' for more info.
91 ;; Minor modes.
92 ;; ------------
93 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
94 ;; manual) are handled in the following way:
95 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
96 ;; saves as `desktop-minor-modes' the list of names of those variables in
97 ;; `minor-mode-alist' that have a non-nil value.
98 ;; When `desktop-create' restores the buffer, each of the symbols in
99 ;; `desktop-minor-modes' is called as function with parameter 1.
100 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
101 ;; are used to handle non-conventional minor modes. `desktop-save' uses
102 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
103 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
104 ;; variable name that is different form its function name, an entry
106 ;; (NAME RESTORE-FUNCTION)
108 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
109 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
110 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
111 ;; function different from the usual minor mode function.
112 ;; ---------------------------------------------------------------------------
114 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
115 ;; in your home directory is used for that. Saving global default values
116 ;; for buffers is an example of misuse.
118 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
119 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
120 ;; things you did not mean to keep. Use M-x desktop-clear RET.
122 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
123 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
124 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
125 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
126 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
127 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
128 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
129 ;; ---------------------------------------------------------------------------
130 ;; TODO:
132 ;; Save window configuration.
133 ;; Recognize more minor modes.
134 ;; Save mark rings.
136 ;;; Code:
138 (defvar desktop-file-version "206"
139 "Version number of desktop file format.
140 Written into the desktop file and used at desktop read to provide
141 backward compatibility.")
143 ;; ----------------------------------------------------------------------------
144 ;; USER OPTIONS -- settings you might want to play with.
145 ;; ----------------------------------------------------------------------------
147 (defgroup desktop nil
148 "Save status of Emacs when you exit."
149 :group 'frames)
151 ;;;###autoload
152 (define-minor-mode desktop-save-mode
153 "Toggle desktop saving mode.
154 With numeric ARG, turn desktop saving on if ARG is positive, off
155 otherwise. If desktop saving is turned on, the state of Emacs is
156 saved from one session to another. See variable `desktop-save'
157 and function `desktop-read' for details."
158 :global t
159 :group 'desktop)
161 ;; Maintained for backward compatibility
162 (define-obsolete-variable-alias 'desktop-enable
163 'desktop-save-mode "22.1")
165 (defun desktop-save-mode-off ()
166 "Disable `desktop-save-mode'. Provided for use in hooks."
167 (desktop-save-mode 0))
169 (defcustom desktop-save 'ask-if-new
170 "*Specifies whether the desktop should be saved when it is killed.
171 A desktop is killed when the user changes desktop or quits Emacs.
172 Possible values are:
173 t -- always save.
174 ask -- always ask.
175 ask-if-new -- ask if no desktop file exists, otherwise just save.
176 ask-if-exists -- ask if desktop file exists, otherwise don't save.
177 if-exists -- save if desktop file exists, otherwise don't save.
178 nil -- never save.
179 The desktop is never saved when `desktop-save-mode' is nil.
180 The variables `desktop-dirname' and `desktop-base-file-name'
181 determine where the desktop is saved."
182 :type
183 '(choice
184 (const :tag "Always save" t)
185 (const :tag "Always ask" ask)
186 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
187 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
188 (const :tag "Save if desktop file exists, else don't" if-exists)
189 (const :tag "Never save" nil))
190 :group 'desktop
191 :version "22.1")
193 (defcustom desktop-load-locked-desktop 'ask
194 "Specifies whether the desktop should be loaded if locked.
195 Possible values are:
196 t -- load anyway.
197 nil -- don't load.
198 ask -- ask the user.
199 If the value is nil, or `ask' and the user chooses not to load the desktop,
200 the normal hook `desktop-not-loaded-hook' is run."
201 :type
202 '(choice
203 (const :tag "Load anyway" t)
204 (const :tag "Don't load" nil)
205 (const :tag "Ask the user" ask))
206 :group 'desktop
207 :version "23.1")
209 (defcustom desktop-base-file-name
210 (convert-standard-filename ".emacs.desktop")
211 "Name of file for Emacs desktop, excluding the directory part."
212 :type 'file
213 :group 'desktop)
214 (define-obsolete-variable-alias 'desktop-basefilename
215 'desktop-base-file-name "22.1")
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 "23.1")
224 (defcustom desktop-path '("." "~")
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 "22.1")
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 "23.1")
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 "List of global variables saved by `desktop-save'.
282 An element may be variable name (a symbol) or a cons cell of the form
283 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
284 MAX-SIZE elements (if the value is a list) before saving the value.
285 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
286 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
287 :group 'desktop)
289 (defcustom desktop-globals-to-clear
290 '(kill-ring
291 kill-ring-yank-pointer
292 search-ring
293 search-ring-yank-pointer
294 regexp-search-ring
295 regexp-search-ring-yank-pointer)
296 "List of global variables that `desktop-clear' will clear.
297 An element may be variable name (a symbol) or a cons cell of the form
298 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
299 to the value obtained by evaluating FORM."
300 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
301 :group 'desktop
302 :version "22.1")
304 (defcustom desktop-clear-preserve-buffers
305 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*")
306 "*List of buffers that `desktop-clear' should not delete.
307 Each element is a regular expression. Buffers with a name matched by any of
308 these won't be deleted."
309 :type '(repeat string)
310 :group 'desktop)
312 ;;;###autoload
313 (defcustom desktop-locals-to-save
314 '(desktop-locals-to-save ; Itself! Think it over.
315 truncate-lines
316 case-fold-search
317 case-replace
318 fill-column
319 overwrite-mode
320 change-log-default-name
321 line-number-mode
322 column-number-mode
323 size-indication-mode
324 buffer-file-coding-system
325 indent-tabs-mode
326 tab-width
327 indicate-buffer-boundaries
328 indicate-empty-lines
329 show-trailing-whitespace)
330 "List of local variables to save for each buffer.
331 The variables are saved only when they really are local. Conventional minor
332 modes are restored automatically; they should not be listed here."
333 :type '(repeat symbol)
334 :group 'desktop)
336 ;; We skip .log files because they are normally temporary.
337 ;; (ftp) files because they require passwords and whatnot.
338 (defcustom desktop-buffers-not-to-save
339 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\)$"
340 "Regexp identifying buffers that are to be excluded from saving."
341 :type 'regexp
342 :group 'desktop)
344 ;; Skip tramp and ange-ftp files
345 (defcustom desktop-files-not-to-save
346 "^/[^/:]*:"
347 "Regexp identifying files whose buffers are to be excluded from saving."
348 :type 'regexp
349 :group 'desktop)
351 ;; We skip TAGS files to save time (tags-file-name is saved instead).
352 (defcustom desktop-modes-not-to-save
353 '(tags-table-mode)
354 "List of major modes whose buffers should not be saved."
355 :type '(repeat symbol)
356 :group 'desktop)
358 (defcustom desktop-file-name-format 'absolute
359 "*Format in which desktop file names should be saved.
360 Possible values are:
361 absolute -- Absolute file name.
362 tilde -- Relative to ~.
363 local -- Relative to directory of desktop file."
364 :type '(choice (const absolute) (const tilde) (const local))
365 :group 'desktop
366 :version "22.1")
368 (defcustom desktop-restore-eager t
369 "Number of buffers to restore immediately.
370 Remaining buffers are restored lazily (when Emacs is idle).
371 If value is t, all buffers are restored immediately."
372 :type '(choice (const t) integer)
373 :group 'desktop
374 :version "22.1")
376 (defcustom desktop-lazy-verbose t
377 "Verbose reporting of lazily created buffers."
378 :type 'boolean
379 :group 'desktop
380 :version "22.1")
382 (defcustom desktop-lazy-idle-delay 5
383 "Idle delay before starting to create buffers.
384 See `desktop-restore-eager'."
385 :type 'integer
386 :group 'desktop
387 :version "22.1")
389 ;;;###autoload
390 (defvar desktop-save-buffer nil
391 "When non-nil, save buffer status in desktop file.
392 This variable becomes buffer local when set.
394 If the value is a function, it is called by `desktop-save' with argument
395 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
396 file along with the state of the buffer for which it was called.
398 When file names are returned, they should be formatted using the call
399 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
401 Later, when `desktop-read' evaluates the desktop file, auxiliary information
402 is passed as the argument DESKTOP-BUFFER-MISC to functions in
403 `desktop-buffer-mode-handlers'.")
404 (make-variable-buffer-local 'desktop-save-buffer)
405 (make-obsolete-variable 'desktop-buffer-modes-to-save
406 'desktop-save-buffer "22.1")
407 (make-obsolete-variable 'desktop-buffer-misc-functions
408 'desktop-save-buffer "22.1")
410 ;;;###autoload
411 (defvar desktop-buffer-mode-handlers
413 "Alist of major mode specific functions to restore a desktop buffer.
414 Functions listed are called by `desktop-create-buffer' when `desktop-read'
415 evaluates the desktop file. List elements must have the form
417 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
419 Buffers with a major mode not specified here, are restored by the default
420 handler `desktop-restore-file-buffer'.
422 Handlers are called with argument list
424 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
426 Furthermore, they may use the following variables:
428 desktop-file-version
429 desktop-buffer-major-mode
430 desktop-buffer-minor-modes
431 desktop-buffer-point
432 desktop-buffer-mark
433 desktop-buffer-read-only
434 desktop-buffer-locals
436 If a handler returns a buffer, then the saved mode settings
437 and variable values for that buffer are copied into it.
439 Modules that define a major mode that needs a special handler should contain
440 code like
442 (defun foo-restore-desktop-buffer
444 (add-to-list 'desktop-buffer-mode-handlers
445 '(foo-mode . foo-restore-desktop-buffer))
447 Furthermore the major mode function must be autoloaded.")
449 ;;;###autoload
450 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
451 (make-obsolete-variable 'desktop-buffer-handlers
452 'desktop-buffer-mode-handlers "22.1")
454 (defcustom desktop-minor-mode-table
455 '((auto-fill-function auto-fill-mode)
456 (vc-mode nil)
457 (vc-dired-mode nil))
458 "Table mapping minor mode variables to minor mode functions.
459 Each entry has the form (NAME RESTORE-FUNCTION).
460 NAME is the name of the buffer-local variable indicating that the minor
461 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
462 RESTORE-FUNCTION nil means don't try to restore the minor mode.
463 Only minor modes for which the name of the buffer-local variable
464 and the name of the minor mode function are different have to be added to
465 this table. See also `desktop-minor-mode-handlers'."
466 :type 'sexp
467 :group 'desktop)
469 ;;;###autoload
470 (defvar desktop-minor-mode-handlers
472 "Alist of functions to restore non-standard minor modes.
473 Functions are called by `desktop-create-buffer' to restore minor modes.
474 List elements must have the form
476 (MINOR-MODE . RESTORE-FUNCTION).
478 Minor modes not specified here, are restored by the standard minor mode
479 function.
481 Handlers are called with argument list
483 (DESKTOP-BUFFER-LOCALS)
485 Furthermore, they may use the following variables:
487 desktop-file-version
488 desktop-buffer-file-name
489 desktop-buffer-name
490 desktop-buffer-major-mode
491 desktop-buffer-minor-modes
492 desktop-buffer-point
493 desktop-buffer-mark
494 desktop-buffer-read-only
495 desktop-buffer-misc
497 When a handler is called, the buffer has been created and the major mode has
498 been set, but local variables listed in desktop-buffer-locals has not yet been
499 created and set.
501 Modules that define a minor mode that needs a special handler should contain
502 code like
504 (defun foo-desktop-restore
506 (add-to-list 'desktop-minor-mode-handlers
507 '(foo-mode . foo-desktop-restore))
509 Furthermore the minor mode function must be autoloaded.
511 See also `desktop-minor-mode-table'.")
513 ;;;###autoload
514 (put 'desktop-minor-mode-handlers 'risky-local-variable t)
516 ;; ----------------------------------------------------------------------------
517 (defvar desktop-dirname nil
518 "The directory in which the desktop file should be saved.")
520 (defun desktop-full-file-name (&optional dirname)
521 "Return the full name of the desktop file in DIRNAME.
522 DIRNAME omitted or nil means use `desktop-dirname'."
523 (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
525 (defun desktop-full-lock-name (&optional dirname)
526 "Return the full name of the desktop lock file in DIRNAME.
527 DIRNAME omitted or nil means use `desktop-dirname'."
528 (expand-file-name desktop-base-lock-name (or dirname desktop-dirname)))
530 (defconst desktop-header
531 ";; --------------------------------------------------------------------------
532 ;; Desktop File for Emacs
533 ;; --------------------------------------------------------------------------
534 " "*Header to place in Desktop file.")
536 (defvar desktop-delay-hook nil
537 "Hooks run after all buffers are loaded; intended for internal use.")
539 ;; ----------------------------------------------------------------------------
540 ;; Desktop file conflict detection
541 (defvar desktop-file-modtime nil
542 "When the desktop file was last modified to the knowledge of this Emacs.
543 Used to detect desktop file conflicts.")
545 (defun desktop-owner (&optional dirname)
546 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
547 Return nil if no desktop file found or no Emacs process is using it.
548 DIRNAME omitted or nil means use `desktop-dirname'."
549 (let (owner)
550 (and (file-exists-p (desktop-full-lock-name dirname))
551 (condition-case nil
552 (with-temp-buffer
553 (insert-file-contents-literally (desktop-full-lock-name dirname))
554 (goto-char (point-min))
555 (setq owner (read (current-buffer)))
556 (integerp owner))
557 (error nil))
558 owner)))
560 (defun desktop-claim-lock (&optional dirname)
561 "Record this Emacs process as the owner of the desktop file in DIRNAME.
562 DIRNAME omitted or nil means use `desktop-dirname'."
563 (write-region (number-to-string (emacs-pid)) nil
564 (desktop-full-lock-name dirname)))
566 (defun desktop-release-lock (&optional dirname)
567 "Remove the lock file for the desktop in DIRNAME.
568 DIRNAME omitted or nil means use `desktop-dirname'."
569 (let ((file (desktop-full-lock-name dirname)))
570 (when (file-exists-p file) (delete-file file))))
572 ;; ----------------------------------------------------------------------------
573 (defun desktop-truncate (list n)
574 "Truncate LIST to at most N elements destructively."
575 (let ((here (nthcdr (1- n) list)))
576 (when (consp here)
577 (setcdr here nil))))
579 ;; ----------------------------------------------------------------------------
580 ;;;###autoload
581 (defun desktop-clear ()
582 "Empty the Desktop.
583 This kills all buffers except for internal ones and those with names matched by
584 a regular expression in the list `desktop-clear-preserve-buffers'.
585 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
586 (interactive)
587 (desktop-lazy-abort)
588 (dolist (var desktop-globals-to-clear)
589 (if (symbolp var)
590 (eval `(setq-default ,var nil))
591 (eval `(setq-default ,(car var) ,(cdr var)))))
592 (let ((buffers (buffer-list))
593 (preserve-regexp (concat "^\\("
594 (mapconcat (lambda (regexp)
595 (concat "\\(" regexp "\\)"))
596 desktop-clear-preserve-buffers
597 "\\|")
598 "\\)$")))
599 (while buffers
600 (let ((bufname (buffer-name (car buffers))))
602 (null bufname)
603 (string-match preserve-regexp bufname)
604 ;; Don't kill buffers made for internal purposes.
605 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
606 (kill-buffer (car buffers))))
607 (setq buffers (cdr buffers))))
608 (delete-other-windows))
610 ;; ----------------------------------------------------------------------------
611 (add-hook 'kill-emacs-hook 'desktop-kill)
613 (defun desktop-kill ()
614 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
615 If the desktop should be saved and `desktop-dirname'
616 is nil, ask the user where to save the desktop."
617 (when (and desktop-save-mode
618 (let ((exists (file-exists-p (desktop-full-file-name))))
619 (or (eq desktop-save t)
620 (and exists (memq desktop-save '(ask-if-new if-exists)))
621 (and
622 (or (memq desktop-save '(ask ask-if-new))
623 (and exists (eq desktop-save 'ask-if-exists)))
624 (y-or-n-p "Save desktop? ")))))
625 (unless desktop-dirname
626 (setq desktop-dirname
627 (file-name-as-directory
628 (expand-file-name
629 (read-directory-name "Directory for desktop file: " nil nil t)))))
630 (condition-case err
631 (desktop-save desktop-dirname t)
632 (file-error
633 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
634 (signal (car err) (cdr err))))))
635 ;; If we own it, we don't anymore.
636 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
638 ;; ----------------------------------------------------------------------------
639 (defun desktop-list* (&rest args)
640 (if (null (cdr args))
641 (car args)
642 (setq args (nreverse args))
643 (let ((value (cons (nth 1 args) (car args))))
644 (setq args (cdr (cdr args)))
645 (while args
646 (setq value (cons (car args) value))
647 (setq args (cdr args)))
648 value)))
650 ;; ----------------------------------------------------------------------------
651 (defun desktop-buffer-info (buffer)
652 (set-buffer buffer)
653 (list
654 ;; basic information
655 (desktop-file-name (buffer-file-name) desktop-dirname)
656 (buffer-name)
657 major-mode
658 ;; minor modes
659 (let (ret)
660 (mapc
661 #'(lambda (minor-mode)
662 (and (boundp minor-mode)
663 (symbol-value minor-mode)
664 (let* ((special (assq minor-mode desktop-minor-mode-table))
665 (value (cond (special (cadr special))
666 ((functionp minor-mode) minor-mode))))
667 (when value (add-to-list 'ret value)))))
668 (mapcar #'car minor-mode-alist))
669 ret)
670 ;; point and mark, and read-only status
671 (point)
672 (list (mark t) mark-active)
673 buffer-read-only
674 ;; auxiliary information
675 (when (functionp desktop-save-buffer)
676 (funcall desktop-save-buffer desktop-dirname))
677 ;; local variables
678 (let ((locals desktop-locals-to-save)
679 (loclist (buffer-local-variables))
680 (ll))
681 (while locals
682 (let ((here (assq (car locals) loclist)))
683 (if here
684 (setq ll (cons here ll))
685 (when (member (car locals) loclist)
686 (setq ll (cons (car locals) ll)))))
687 (setq locals (cdr locals)))
688 ll)))
690 ;; ----------------------------------------------------------------------------
691 (defun desktop-internal-v2s (value)
692 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
693 TXT is a string that when read and evaluated yields value.
694 QUOTE may be `may' (value may be quoted),
695 `must' (values must be quoted), or nil (value may not be quoted)."
696 (cond
697 ((or (numberp value) (null value) (eq t value) (keywordp value))
698 (cons 'may (prin1-to-string value)))
699 ((stringp value)
700 (let ((copy (copy-sequence value)))
701 (set-text-properties 0 (length copy) nil copy)
702 ;; Get rid of text properties because we cannot read them
703 (cons 'may (prin1-to-string copy))))
704 ((symbolp value)
705 (cons 'must (prin1-to-string value)))
706 ((vectorp value)
707 (let* ((special nil)
708 (pass1 (mapcar
709 (lambda (el)
710 (let ((res (desktop-internal-v2s el)))
711 (if (null (car res))
712 (setq special t))
713 res))
714 value)))
715 (if special
716 (cons nil (concat "(vector "
717 (mapconcat (lambda (el)
718 (if (eq (car el) 'must)
719 (concat "'" (cdr el))
720 (cdr el)))
721 pass1
722 " ")
723 ")"))
724 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
725 ((consp value)
726 (let ((p value)
727 newlist
728 use-list*
729 anynil)
730 (while (consp p)
731 (let ((q.txt (desktop-internal-v2s (car p))))
732 (or anynil (setq anynil (null (car q.txt))))
733 (setq newlist (cons q.txt newlist)))
734 (setq p (cdr p)))
735 (if p
736 (let ((last (desktop-internal-v2s p)))
737 (or anynil (setq anynil (null (car last))))
738 (or anynil
739 (setq newlist (cons '(must . ".") newlist)))
740 (setq use-list* t)
741 (setq newlist (cons last newlist))))
742 (setq newlist (nreverse newlist))
743 (if anynil
744 (cons nil
745 (concat (if use-list* "(desktop-list* " "(list ")
746 (mapconcat (lambda (el)
747 (if (eq (car el) 'must)
748 (concat "'" (cdr el))
749 (cdr el)))
750 newlist
751 " ")
752 ")"))
753 (cons 'must
754 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
755 ((subrp value)
756 (cons nil (concat "(symbol-function '"
757 (substring (prin1-to-string value) 7 -1)
758 ")")))
759 ((markerp value)
760 (let ((pos (prin1-to-string (marker-position value)))
761 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
762 (cons nil (concat "(let ((mk (make-marker)))"
763 " (add-hook 'desktop-delay-hook"
764 " (list 'lambda '() (list 'set-marker mk "
765 pos " (get-buffer " buf ")))) mk)"))))
766 (t ; save as text
767 (cons 'may "\"Unprintable entity\""))))
769 ;; ----------------------------------------------------------------------------
770 (defun desktop-value-to-string (value)
771 "Convert VALUE to a string that when read evaluates to the same value.
772 Not all types of values are supported."
773 (let* ((print-escape-newlines t)
774 (float-output-format nil)
775 (quote.txt (desktop-internal-v2s value))
776 (quote (car quote.txt))
777 (txt (cdr quote.txt)))
778 (if (eq quote 'must)
779 (concat "'" txt)
780 txt)))
782 ;; ----------------------------------------------------------------------------
783 (defun desktop-outvar (varspec)
784 "Output a setq statement for variable VAR to the desktop file.
785 The argument VARSPEC may be the variable name VAR (a symbol),
786 or a cons cell of the form (VAR . MAX-SIZE),
787 which means to truncate VAR's value to at most MAX-SIZE elements
788 \(if the value is a list) before saving the value."
789 (let (var size)
790 (if (consp varspec)
791 (setq var (car varspec) size (cdr varspec))
792 (setq var varspec))
793 (when (boundp var)
794 (when (and (integerp size)
795 (> size 0)
796 (listp (eval var)))
797 (desktop-truncate (eval var) size))
798 (insert "(setq "
799 (symbol-name var)
801 (desktop-value-to-string (symbol-value var))
802 ")\n"))))
804 ;; ----------------------------------------------------------------------------
805 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
806 "Return t if buffer should have its state saved in the desktop file.
807 FILENAME is the visited file name, BUFNAME is the buffer name, and
808 MODE is the major mode.
809 \n\(fn FILENAME BUFNAME MODE)"
810 (let ((case-fold-search nil))
811 (and (not (string-match desktop-buffers-not-to-save bufname))
812 (not (memq mode desktop-modes-not-to-save))
813 (or (and filename
814 (not (string-match desktop-files-not-to-save filename)))
815 (and (eq mode 'dired-mode)
816 (with-current-buffer bufname
817 (not (string-match desktop-files-not-to-save
818 default-directory))))
819 (and (null filename)
820 (with-current-buffer bufname desktop-save-buffer))))))
822 ;; ----------------------------------------------------------------------------
823 (defun desktop-file-name (filename dirname)
824 "Convert FILENAME to format specified in `desktop-file-name-format'.
825 DIRNAME must be the directory in which the desktop file will be saved."
826 (cond
827 ((not filename) nil)
828 ((eq desktop-file-name-format 'tilde)
829 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
830 (cond
831 ((file-name-absolute-p relative-name) relative-name)
832 ((string= "./" relative-name) "~/")
833 ((string= "." relative-name) "~")
834 (t (concat "~/" relative-name)))))
835 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
836 (t (expand-file-name filename))))
838 ;; ----------------------------------------------------------------------------
839 ;;;###autoload
840 (defun desktop-save (dirname &optional release)
841 "Save the desktop in a desktop file.
842 Parameter DIRNAME specifies where to save the desktop file.
843 Optional parameter RELEASE says whether we're done with this desktop.
844 See also `desktop-base-file-name'."
845 (interactive "DDirectory to save desktop file in: ")
846 (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
847 (save-excursion
848 (let ((eager desktop-restore-eager)
849 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
850 (when
851 (or (not new-modtime) ; nothing to overwrite
852 (equal desktop-file-modtime new-modtime)
853 (yes-or-no-p (if desktop-file-modtime
854 (if (> (float-time new-modtime) (float-time desktop-file-modtime))
855 "Desktop file is more recent than the one loaded. Save anyway? "
856 "Desktop file isn't the one loaded. Overwrite it? ")
857 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
858 (unless release (error "Desktop file conflict")))
860 ;; If we're done with it, release the lock.
861 ;; Otherwise, claim it if it's unclaimed or if we created it.
862 (if release
863 (desktop-release-lock)
864 (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
866 (with-temp-buffer
867 (insert
868 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
869 desktop-header
870 ";; Created " (current-time-string) "\n"
871 ";; Desktop file format version " desktop-file-version "\n"
872 ";; Emacs version " emacs-version "\n")
873 (save-excursion (run-hooks 'desktop-save-hook))
874 (goto-char (point-max))
875 (insert "\n;; Global section:\n")
876 (mapc (function desktop-outvar) desktop-globals-to-save)
877 (when (memq 'kill-ring desktop-globals-to-save)
878 (insert
879 "(setq kill-ring-yank-pointer (nthcdr "
880 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
881 " kill-ring))\n"))
883 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
884 (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
885 (when (apply 'desktop-save-buffer-p l)
886 (insert "("
887 (if (or (not (integerp eager))
888 (if (zerop eager)
890 (setq eager (1- eager))))
891 "desktop-create-buffer"
892 "desktop-append-buffer-args")
894 desktop-file-version)
895 (dolist (e l)
896 (insert "\n " (desktop-value-to-string e)))
897 (insert ")\n\n")))
899 (setq default-directory desktop-dirname)
900 (let ((coding-system-for-write 'emacs-mule))
901 (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
902 ;; We remember when it was modified (which is presumably just now).
903 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))
905 ;; ----------------------------------------------------------------------------
906 ;;;###autoload
907 (defun desktop-remove ()
908 "Delete desktop file in `desktop-dirname'.
909 This function also sets `desktop-dirname' to nil."
910 (interactive)
911 (when desktop-dirname
912 (let ((filename (desktop-full-file-name)))
913 (setq desktop-dirname nil)
914 (when (file-exists-p filename)
915 (delete-file filename)))))
917 (defvar desktop-buffer-args-list nil
918 "List of args for `desktop-create-buffer'.")
920 (defvar desktop-lazy-timer nil)
922 ;; ----------------------------------------------------------------------------
923 ;;;###autoload
924 (defun desktop-read (&optional dirname)
925 "Read and process the desktop file in directory DIRNAME.
926 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
927 directories listed in `desktop-path'. If a desktop file is found, it
928 is processed and `desktop-after-read-hook' is run. If no desktop file
929 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
930 This function is a no-op when Emacs is running in batch mode.
931 It returns t if a desktop file was loaded, nil otherwise."
932 (interactive)
933 (unless noninteractive
934 (setq desktop-dirname
935 (file-name-as-directory
936 (expand-file-name
938 ;; If DIRNAME is specified, use it.
939 (and (< 0 (length dirname)) dirname)
940 ;; Otherwise search desktop file in desktop-path.
941 (let ((dirs desktop-path))
942 (while (and dirs
943 (not (file-exists-p
944 (desktop-full-file-name (car dirs)))))
945 (setq dirs (cdr dirs)))
946 (and dirs (car dirs)))
947 ;; If not found and `desktop-path' is non-nil, use its first element.
948 (and desktop-path (car desktop-path))
949 ;; Default: Home directory.
950 "~"))))
951 (if (file-exists-p (desktop-full-file-name))
952 ;; Desktop file found, but is it already in use?
953 (let ((desktop-first-buffer nil)
954 (desktop-buffer-ok-count 0)
955 (desktop-buffer-fail-count 0)
956 (owner (desktop-owner))
957 ;; Avoid desktop saving during evaluation of desktop buffer.
958 (desktop-save nil))
959 (if (and owner
960 (memq desktop-load-locked-desktop '(nil ask))
961 (or (null desktop-load-locked-desktop)
962 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
963 Using it may cause conflicts. Use it anyway? " owner)))))
964 (progn
965 (let ((default-directory desktop-dirname))
966 (run-hooks 'desktop-not-loaded-hook))
967 (setq desktop-dirname nil)
968 (message "Desktop file in use; not loaded."))
969 (desktop-lazy-abort)
970 ;; Evaluate desktop buffer and remember when it was modified.
971 (load (desktop-full-file-name) t t t)
972 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
973 ;; If it wasn't already, mark it as in-use, to bother other
974 ;; desktop instances.
975 (unless owner
976 (condition-case nil
977 (desktop-claim-lock)
978 (file-error (message "Couldn't record use of desktop file")
979 (sit-for 1))))
981 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
982 ;; We want buffers existing prior to evaluating the desktop (and
983 ;; not reused) to be placed at the end of the buffer list, so we
984 ;; move them here.
985 (mapc 'bury-buffer
986 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
987 (switch-to-buffer (car (buffer-list)))
988 (run-hooks 'desktop-delay-hook)
989 (setq desktop-delay-hook nil)
990 (run-hooks 'desktop-after-read-hook)
991 (message "Desktop: %d buffer%s restored%s%s."
992 desktop-buffer-ok-count
993 (if (= 1 desktop-buffer-ok-count) "" "s")
994 (if (< 0 desktop-buffer-fail-count)
995 (format ", %d failed to restore" desktop-buffer-fail-count)
997 (if desktop-buffer-args-list
998 (format ", %d to restore lazily"
999 (length desktop-buffer-args-list))
1000 ""))
1002 ;; No desktop file found.
1003 (desktop-clear)
1004 (let ((default-directory desktop-dirname))
1005 (run-hooks 'desktop-no-desktop-file-hook))
1006 (message "No desktop file.")
1007 nil)))
1009 ;; ----------------------------------------------------------------------------
1010 ;; Maintained for backward compatibility
1011 ;;;###autoload
1012 (defun desktop-load-default ()
1013 "Load the `default' start-up library manually.
1014 Also inhibit further loading of it."
1015 (unless inhibit-default-init ; safety check
1016 (load "default" t t)
1017 (setq inhibit-default-init t)))
1018 (make-obsolete 'desktop-load-default
1019 'desktop-save-mode "22.1")
1021 ;; ----------------------------------------------------------------------------
1022 ;;;###autoload
1023 (defun desktop-change-dir (dirname)
1024 "Change to desktop saved in DIRNAME.
1025 Kill the desktop as specified by variables `desktop-save-mode' and
1026 `desktop-save', then clear the desktop and load the desktop file in
1027 directory DIRNAME."
1028 (interactive "DChange to directory: ")
1029 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
1030 (desktop-kill)
1031 (desktop-clear)
1032 (desktop-read dirname))
1034 ;; ----------------------------------------------------------------------------
1035 ;;;###autoload
1036 (defun desktop-save-in-desktop-dir ()
1037 "Save the desktop in directory `desktop-dirname'."
1038 (interactive)
1039 (if desktop-dirname
1040 (desktop-save desktop-dirname)
1041 (call-interactively 'desktop-save))
1042 (message "Desktop saved in %s" desktop-dirname))
1044 ;; ----------------------------------------------------------------------------
1045 ;;;###autoload
1046 (defun desktop-revert ()
1047 "Revert to the last loaded desktop."
1048 (interactive)
1049 (unless desktop-dirname
1050 (error "Unknown desktop directory"))
1051 (unless (file-exists-p (desktop-full-file-name))
1052 (error "No desktop file found"))
1053 (desktop-clear)
1054 (desktop-read desktop-dirname))
1056 (defvar desktop-buffer-major-mode)
1057 (defvar desktop-buffer-locals)
1058 ;; ----------------------------------------------------------------------------
1059 (defun desktop-restore-file-buffer (desktop-buffer-file-name
1060 desktop-buffer-name
1061 desktop-buffer-misc)
1062 "Restore a file buffer."
1063 (when desktop-buffer-file-name
1064 (if (or (file-exists-p desktop-buffer-file-name)
1065 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1066 desktop-buffer-file-name)))
1067 (if desktop-missing-file-warning
1068 (y-or-n-p (concat msg " Re-create buffer? "))
1069 (message "%s" msg)
1070 nil)))
1071 (let* ((auto-insert nil) ; Disable auto insertion
1072 (coding-system-for-read
1073 (or coding-system-for-read
1074 (cdr (assq 'buffer-file-coding-system
1075 desktop-buffer-locals))))
1076 (buf (find-file-noselect desktop-buffer-file-name)))
1077 (condition-case nil
1078 (switch-to-buffer buf)
1079 (error (pop-to-buffer buf)))
1080 (and (not (eq major-mode desktop-buffer-major-mode))
1081 (functionp desktop-buffer-major-mode)
1082 (funcall desktop-buffer-major-mode))
1083 buf)
1084 nil)))
1086 (defun desktop-load-file (function)
1087 "Load the file where auto loaded FUNCTION is defined."
1088 (when function
1089 (let ((fcell (and (fboundp function) (symbol-function function))))
1090 (when (and (listp fcell)
1091 (eq 'autoload (car fcell)))
1092 (load (cadr fcell))))))
1094 ;; ----------------------------------------------------------------------------
1095 ;; Create a buffer, load its file, set its mode, ...;
1096 ;; called from Desktop file only.
1098 ;; Just to silence the byte compiler.
1100 (defvar desktop-first-buffer) ; Dynamically bound in `desktop-read'
1102 ;; Bound locally in `desktop-read'.
1103 (defvar desktop-buffer-ok-count)
1104 (defvar desktop-buffer-fail-count)
1106 (defun desktop-create-buffer
1107 (desktop-file-version
1108 desktop-buffer-file-name
1109 desktop-buffer-name
1110 desktop-buffer-major-mode
1111 desktop-buffer-minor-modes
1112 desktop-buffer-point
1113 desktop-buffer-mark
1114 desktop-buffer-read-only
1115 desktop-buffer-misc
1116 &optional
1117 desktop-buffer-locals)
1118 ;; To make desktop files with relative file names possible, we cannot
1119 ;; allow `default-directory' to change. Therefore we save current buffer.
1120 (save-current-buffer
1121 ;; Give major mode module a chance to add a handler.
1122 (desktop-load-file desktop-buffer-major-mode)
1123 (let ((buffer-list (buffer-list))
1124 (result
1125 (condition-case err
1126 (funcall (or (cdr (assq desktop-buffer-major-mode
1127 desktop-buffer-mode-handlers))
1128 'desktop-restore-file-buffer)
1129 desktop-buffer-file-name
1130 desktop-buffer-name
1131 desktop-buffer-misc)
1132 (error
1133 (message "Desktop: Can't load buffer %s: %s"
1134 desktop-buffer-name
1135 (error-message-string err))
1136 (when desktop-missing-file-warning (sit-for 1))
1137 nil))))
1138 (if (bufferp result)
1139 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1140 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1141 (setq result nil))
1142 ;; Restore buffer list order with new buffer at end. Don't change
1143 ;; the order for old desktop files (old desktop module behaviour).
1144 (unless (< desktop-file-version 206)
1145 (mapc 'bury-buffer buffer-list)
1146 (when result (bury-buffer result)))
1147 (when result
1148 (unless (or desktop-first-buffer (< desktop-file-version 206))
1149 (setq desktop-first-buffer result))
1150 (set-buffer result)
1151 (unless (equal (buffer-name) desktop-buffer-name)
1152 (rename-buffer desktop-buffer-name))
1153 ;; minor modes
1154 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1155 (auto-fill-mode 1))
1156 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1157 (auto-fill-mode 0))
1159 (dolist (minor-mode desktop-buffer-minor-modes)
1160 ;; Give minor mode module a chance to add a handler.
1161 (desktop-load-file minor-mode)
1162 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1163 (if handler
1164 (funcall handler desktop-buffer-locals)
1165 (when (functionp minor-mode)
1166 (funcall minor-mode 1)))))))
1167 ;; Even though point and mark are non-nil when written by
1168 ;; `desktop-save', they may be modified by handlers wanting to set
1169 ;; point or mark themselves.
1170 (when desktop-buffer-point
1171 (goto-char
1172 (condition-case err
1173 ;; Evaluate point. Thus point can be something like
1174 ;; '(search-forward ...
1175 (eval desktop-buffer-point)
1176 (error (message "%s" (error-message-string err)) 1))))
1177 (when desktop-buffer-mark
1178 (if (consp desktop-buffer-mark)
1179 (progn
1180 (set-mark (car desktop-buffer-mark))
1181 (setq mark-active (car (cdr desktop-buffer-mark))))
1182 (set-mark desktop-buffer-mark)))
1183 ;; Never override file system if the file really is read-only marked.
1184 (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1185 (while desktop-buffer-locals
1186 (let ((this (car desktop-buffer-locals)))
1187 (if (consp this)
1188 ;; an entry of this form `(symbol . value)'
1189 (progn
1190 (make-local-variable (car this))
1191 (set (car this) (cdr this)))
1192 ;; an entry of the form `symbol'
1193 (make-local-variable this)
1194 (makunbound this)))
1195 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
1197 ;; ----------------------------------------------------------------------------
1198 ;; Backward compatibility -- update parameters to 205 standards.
1199 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1200 desktop-buffer-major-mode
1201 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
1202 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1203 desktop-buffer-major-mode (cdr mim) pt mk ro
1204 desktop-buffer-misc
1205 (list (cons 'truncate-lines tl)
1206 (cons 'fill-column fc)
1207 (cons 'case-fold-search cfs)
1208 (cons 'case-replace cr)
1209 (cons 'overwrite-mode (car mim)))))
1211 (defun desktop-append-buffer-args (&rest args)
1212 "Append ARGS at end of `desktop-buffer-args-list'.
1213 ARGS must be an argument list for `desktop-create-buffer'."
1214 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1215 (unless desktop-lazy-timer
1216 (setq desktop-lazy-timer
1217 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1219 (defun desktop-lazy-create-buffer ()
1220 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1221 (when desktop-buffer-args-list
1222 (let* ((remaining (length desktop-buffer-args-list))
1223 (args (pop desktop-buffer-args-list))
1224 (buffer-name (nth 2 args))
1225 (msg (format "Desktop lazily opening %s (%s remaining)..."
1226 buffer-name remaining)))
1227 (when desktop-lazy-verbose
1228 (message "%s" msg))
1229 (let ((desktop-first-buffer nil)
1230 (desktop-buffer-ok-count 0)
1231 (desktop-buffer-fail-count 0))
1232 (apply 'desktop-create-buffer args)
1233 (run-hooks 'desktop-delay-hook)
1234 (setq desktop-delay-hook nil)
1235 (bury-buffer (get-buffer buffer-name))
1236 (when desktop-lazy-verbose
1237 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1239 (defun desktop-idle-create-buffers ()
1240 "Create buffers until the user does something, then stop.
1241 If there are no buffers left to create, kill the timer."
1242 (let ((repeat 1))
1243 (while (and repeat desktop-buffer-args-list)
1244 (save-window-excursion
1245 (desktop-lazy-create-buffer))
1246 (setq repeat (sit-for 0.2))
1247 (unless desktop-buffer-args-list
1248 (cancel-timer desktop-lazy-timer)
1249 (setq desktop-lazy-timer nil)
1250 (message "Lazy desktop load complete")
1251 (sit-for 3)
1252 (message "")))))
1254 (defun desktop-lazy-complete ()
1255 "Run the desktop load to completion."
1256 (interactive)
1257 (let ((desktop-lazy-verbose t))
1258 (while desktop-buffer-args-list
1259 (save-window-excursion
1260 (desktop-lazy-create-buffer)))
1261 (message "Lazy desktop load complete")))
1263 (defun desktop-lazy-abort ()
1264 "Abort lazy loading of the desktop."
1265 (interactive)
1266 (when desktop-lazy-timer
1267 (cancel-timer desktop-lazy-timer)
1268 (setq desktop-lazy-timer nil))
1269 (when desktop-buffer-args-list
1270 (setq desktop-buffer-args-list nil)
1271 (when (interactive-p)
1272 (message "Lazy desktop load aborted"))))
1274 ;; ----------------------------------------------------------------------------
1275 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1276 ;; command line, we do the rest of what it takes to use desktop, but do it
1277 ;; after finishing loading the init file.
1278 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1279 ;; functions are processed after `after-init-hook'.
1280 (add-hook
1281 'after-init-hook
1282 (lambda ()
1283 (let ((key "--no-desktop"))
1284 (when (member key command-line-args)
1285 (setq command-line-args (delete key command-line-args))
1286 (setq desktop-save-mode nil)))
1287 (when desktop-save-mode (desktop-read))))
1289 (provide 'desktop)
1291 ;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1292 ;;; desktop.el ends here