* progmodes/compile.el (compilation-error-regexp-alist-alist):
[emacs.git] / lisp / desktop.el
blobc60745a6b16012ca4d27aef2b03ee7565f075da2
1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993-1995, 1997, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Morten Welinder <terra@diku.dk>
6 ;; Keywords: convenience
7 ;; Favourite-brand-of-beer: None, I hate beer.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Save the Desktop, i.e.,
27 ;; - some global variables
28 ;; - the list of buffers with associated files. For each buffer also
29 ;; - the major mode
30 ;; - the default directory
31 ;; - the point
32 ;; - the mark & mark-active
33 ;; - buffer-read-only
34 ;; - some local variables
36 ;; To use this, use customize to turn on desktop-save-mode or add the
37 ;; following line somewhere in your .emacs file:
39 ;; (desktop-save-mode 1)
41 ;; For further usage information, look at the section
42 ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
44 ;; When the desktop module is loaded, the function `desktop-kill' is
45 ;; added to the `kill-emacs-hook'. This function is responsible for
46 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
47 ;; function is added to the `after-init-hook'. This function is
48 ;; responsible for loading the desktop when Emacs is started.
50 ;; Special handling.
51 ;; -----------------
52 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
53 ;; are supplied to handle special major and minor modes respectively.
54 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
55 ;; to restore a desktop buffer. Elements must have the form
57 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
59 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
60 ;; evaluates the desktop file. Buffers with a major mode not specified here,
61 ;; are restored by the default handler `desktop-restore-file-buffer'.
62 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
63 ;; non-standard minor modes. Elements must have the form
65 ;; (MINOR-MODE . RESTORE-FUNCTION).
67 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
68 ;; Minor modes not specified here, are restored by the standard minor mode
69 ;; function. If you write a module that defines a major or minor mode that
70 ;; needs a special handler, then place code like
72 ;; (defun foo-restore-desktop-buffer
73 ;; ...
74 ;; (add-to-list 'desktop-buffer-mode-handlers
75 ;; '(foo-mode . foo-restore-desktop-buffer))
77 ;; or
79 ;; (defun bar-desktop-restore
80 ;; ...
81 ;; (add-to-list 'desktop-minor-mode-handlers
82 ;; '(bar-mode . bar-desktop-restore))
84 ;; in the module itself, and make sure that the mode function is
85 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
86 ;; `desktop-minor-mode-handlers' for more info.
88 ;; Minor modes.
89 ;; ------------
90 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
91 ;; manual) are handled in the following way:
92 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
93 ;; saves as `desktop-minor-modes' the list of names of those variables in
94 ;; `minor-mode-alist' that have a non-nil value.
95 ;; When `desktop-create' restores the buffer, each of the symbols in
96 ;; `desktop-minor-modes' is called as function with parameter 1.
97 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
98 ;; are used to handle non-conventional minor modes. `desktop-save' uses
99 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
100 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
101 ;; variable name that is different form its function name, an entry
103 ;; (NAME RESTORE-FUNCTION)
105 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
106 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
107 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
108 ;; function different from the usual minor mode function.
109 ;; ---------------------------------------------------------------------------
111 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
112 ;; in your home directory is used for that. Saving global default values
113 ;; for buffers is an example of misuse.
115 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
116 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
117 ;; things you did not mean to keep. Use M-x desktop-clear RET.
119 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
120 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
121 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
122 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
123 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
124 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
125 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
126 ;; ---------------------------------------------------------------------------
127 ;; TODO:
129 ;; Save window configuration.
130 ;; Recognize more minor modes.
131 ;; Save mark rings.
133 ;;; Code:
135 (defvar desktop-file-version "206"
136 "Version number of desktop file format.
137 Written into the desktop file and used at desktop read to provide
138 backward compatibility.")
140 ;; ----------------------------------------------------------------------------
141 ;; USER OPTIONS -- settings you might want to play with.
142 ;; ----------------------------------------------------------------------------
144 (defgroup desktop nil
145 "Save status of Emacs when you exit."
146 :group 'frames)
148 ;;;###autoload
149 (define-minor-mode desktop-save-mode
150 "Toggle desktop saving mode.
151 With numeric ARG, turn desktop saving on if ARG is positive, off
152 otherwise. If desktop saving is turned on, the state of Emacs is
153 saved from one session to another. See variable `desktop-save'
154 and function `desktop-read' for details."
155 :global t
156 :group 'desktop)
158 ;; Maintained for backward compatibility
159 (define-obsolete-variable-alias 'desktop-enable
160 'desktop-save-mode "22.1")
162 (defun desktop-save-mode-off ()
163 "Disable `desktop-save-mode'. Provided for use in hooks."
164 (desktop-save-mode 0))
166 (defcustom desktop-save 'ask-if-new
167 "Specifies whether the desktop should be saved when it is killed.
168 A desktop is killed when the user changes desktop or quits Emacs.
169 Possible values are:
170 t -- always save.
171 ask -- always ask.
172 ask-if-new -- ask if no desktop file exists, otherwise just save.
173 ask-if-exists -- ask if desktop file exists, otherwise don't save.
174 if-exists -- save if desktop file exists, otherwise don't save.
175 nil -- never save.
176 The desktop is never saved when `desktop-save-mode' is nil.
177 The variables `desktop-dirname' and `desktop-base-file-name'
178 determine where the desktop is saved."
179 :type
180 '(choice
181 (const :tag "Always save" t)
182 (const :tag "Always ask" ask)
183 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
184 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
185 (const :tag "Save if desktop file exists, else don't" if-exists)
186 (const :tag "Never save" nil))
187 :group 'desktop
188 :version "22.1")
190 (defcustom desktop-load-locked-desktop 'ask
191 "Specifies whether the desktop should be loaded if locked.
192 Possible values are:
193 t -- load anyway.
194 nil -- don't load.
195 ask -- ask the user.
196 If the value is nil, or `ask' and the user chooses not to load the desktop,
197 the normal hook `desktop-not-loaded-hook' is run."
198 :type
199 '(choice
200 (const :tag "Load anyway" t)
201 (const :tag "Don't load" nil)
202 (const :tag "Ask the user" ask))
203 :group 'desktop
204 :version "22.2")
206 (define-obsolete-variable-alias 'desktop-basefilename
207 'desktop-base-file-name "22.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)
215 (defcustom desktop-base-lock-name
216 (convert-standard-filename ".emacs.desktop.lock")
217 "Name of lock file for Emacs desktop, excluding the directory part."
218 :type 'file
219 :group 'desktop
220 :version "22.2")
222 (defcustom desktop-path (list "." user-emacs-directory "~")
223 "List of directories to search for the desktop file.
224 The base name of the file is specified in `desktop-base-file-name'."
225 :type '(repeat directory)
226 :group 'desktop
227 :version "23.2") ; user-emacs-directory added
229 (defcustom desktop-missing-file-warning nil
230 "If non-nil, offer to recreate the buffer of a deleted file.
231 Also pause for a moment to display message about errors signaled in
232 `desktop-buffer-mode-handlers'.
234 If nil, just print error messages in the message buffer."
235 :type 'boolean
236 :group 'desktop
237 :version "22.1")
239 (defcustom desktop-no-desktop-file-hook nil
240 "Normal hook run when `desktop-read' can't find a desktop file.
241 Run in the directory in which the desktop file was sought.
242 May be used to show a dired buffer."
243 :type 'hook
244 :group 'desktop
245 :version "22.1")
247 (defcustom desktop-not-loaded-hook nil
248 "Normal hook run when the user declines to re-use a desktop file.
249 Run in the directory in which the desktop file was found.
250 May be used to deal with accidental multiple Emacs jobs."
251 :type 'hook
252 :group 'desktop
253 :options '(desktop-save-mode-off save-buffers-kill-emacs)
254 :version "22.2")
256 (defcustom desktop-after-read-hook nil
257 "Normal hook run after a successful `desktop-read'.
258 May be used to show a buffer list."
259 :type 'hook
260 :group 'desktop
261 :options '(list-buffers)
262 :version "22.1")
264 (defcustom desktop-save-hook nil
265 "Normal hook run before the desktop is saved in a desktop file.
266 Run with the desktop buffer current with only the header present.
267 May be used to add to the desktop code or to truncate history lists,
268 for example."
269 :type 'hook
270 :group 'desktop)
272 (defcustom desktop-globals-to-save
273 '(desktop-missing-file-warning
274 tags-file-name
275 tags-table-list
276 search-ring
277 regexp-search-ring
278 register-alist
279 file-name-history)
280 "List of global variables saved by `desktop-save'.
281 An element may be variable name (a symbol) or a cons cell of the form
282 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
283 MAX-SIZE elements (if the value is a list) before saving the value.
284 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
285 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
286 :group 'desktop)
288 (defcustom desktop-globals-to-clear
289 '(kill-ring
290 kill-ring-yank-pointer
291 search-ring
292 search-ring-yank-pointer
293 regexp-search-ring
294 regexp-search-ring-yank-pointer)
295 "List of global variables that `desktop-clear' will clear.
296 An element may be variable name (a symbol) or a cons cell of the form
297 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
298 to the value obtained by evaluating FORM."
299 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
300 :group 'desktop
301 :version "22.1")
303 (defcustom desktop-clear-preserve-buffers
304 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
305 "\\*Warnings\\*")
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 :version "23.3" ; added Warnings - bug#6336
310 :type '(repeat string)
311 :group 'desktop)
313 ;;;###autoload
314 (defcustom desktop-locals-to-save
315 '(desktop-locals-to-save ; Itself! Think it over.
316 truncate-lines
317 case-fold-search
318 case-replace
319 fill-column
320 overwrite-mode
321 change-log-default-name
322 line-number-mode
323 column-number-mode
324 size-indication-mode
325 buffer-file-coding-system
326 indent-tabs-mode
327 tab-width
328 indicate-buffer-boundaries
329 indicate-empty-lines
330 show-trailing-whitespace)
331 "List of local variables to save for each buffer.
332 The variables are saved only when they really are local. Conventional minor
333 modes are restored automatically; they should not be listed here."
334 :type '(repeat symbol)
335 :group 'desktop)
337 (defcustom desktop-buffers-not-to-save nil
338 "Regexp identifying buffers that are to be excluded from saving."
339 :type '(choice (const :tag "None" nil)
340 regexp)
341 :version "23.2" ; set to nil
342 :group 'desktop)
344 ;; Skip tramp and ange-ftp files
345 (defcustom desktop-files-not-to-save
346 "\\(^/[^/:]*:\\|(ftp)$\\)"
347 "Regexp identifying files whose buffers are to be excluded from saving."
348 :type '(choice (const :tag "None" nil)
349 regexp)
350 :group 'desktop)
352 ;; We skip TAGS files to save time (tags-file-name is saved instead).
353 (defcustom desktop-modes-not-to-save
354 '(tags-table-mode)
355 "List of major modes whose buffers should not be saved."
356 :type '(repeat symbol)
357 :group 'desktop)
359 (defcustom desktop-file-name-format 'absolute
360 "Format in which desktop file names should be saved.
361 Possible values are:
362 absolute -- Absolute file name.
363 tilde -- Relative to ~.
364 local -- Relative to directory of desktop file."
365 :type '(choice (const absolute) (const tilde) (const local))
366 :group 'desktop
367 :version "22.1")
369 (defcustom desktop-restore-eager t
370 "Number of buffers to restore immediately.
371 Remaining buffers are restored lazily (when Emacs is idle).
372 If value is t, all buffers are restored immediately."
373 :type '(choice (const t) integer)
374 :group 'desktop
375 :version "22.1")
377 (defcustom desktop-lazy-verbose t
378 "Verbose reporting of lazily created buffers."
379 :type 'boolean
380 :group 'desktop
381 :version "22.1")
383 (defcustom desktop-lazy-idle-delay 5
384 "Idle delay before starting to create buffers.
385 See `desktop-restore-eager'."
386 :type 'integer
387 :group 'desktop
388 :version "22.1")
390 ;;;###autoload
391 (defvar desktop-save-buffer nil
392 "When non-nil, save buffer status in desktop file.
393 This variable becomes buffer local when set.
395 If the value is a function, it is called by `desktop-save' with argument
396 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
397 file along with the state of the buffer for which it was called.
399 When file names are returned, they should be formatted using the call
400 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
402 Later, when `desktop-read' evaluates the desktop file, auxiliary information
403 is passed as the argument DESKTOP-BUFFER-MISC to functions in
404 `desktop-buffer-mode-handlers'.")
405 (make-variable-buffer-local 'desktop-save-buffer)
406 (make-obsolete-variable 'desktop-buffer-modes-to-save
407 'desktop-save-buffer "22.1")
408 (make-obsolete-variable 'desktop-buffer-misc-functions
409 'desktop-save-buffer "22.1")
411 ;;;###autoload
412 (defvar desktop-buffer-mode-handlers
414 "Alist of major mode specific functions to restore a desktop buffer.
415 Functions listed are called by `desktop-create-buffer' when `desktop-read'
416 evaluates the desktop file. List elements must have the form
418 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
420 Buffers with a major mode not specified here, are restored by the default
421 handler `desktop-restore-file-buffer'.
423 Handlers are called with argument list
425 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
427 Furthermore, they may use the following variables:
429 desktop-file-version
430 desktop-buffer-major-mode
431 desktop-buffer-minor-modes
432 desktop-buffer-point
433 desktop-buffer-mark
434 desktop-buffer-read-only
435 desktop-buffer-locals
437 If a handler returns a buffer, then the saved mode settings
438 and variable values for that buffer are copied into it.
440 Modules that define a major mode that needs a special handler should contain
441 code like
443 (defun foo-restore-desktop-buffer
445 (add-to-list 'desktop-buffer-mode-handlers
446 '(foo-mode . foo-restore-desktop-buffer))
448 Furthermore the major mode function must be autoloaded.")
450 ;;;###autoload
451 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
452 (make-obsolete-variable 'desktop-buffer-handlers
453 'desktop-buffer-mode-handlers "22.1")
455 (defcustom desktop-minor-mode-table
456 '((auto-fill-function auto-fill-mode)
457 (vc-mode nil)
458 (vc-dired-mode nil)
459 (erc-track-minor-mode nil)
460 (savehist-mode nil))
461 "Table mapping minor mode variables to minor mode functions.
462 Each entry has the form (NAME RESTORE-FUNCTION).
463 NAME is the name of the buffer-local variable indicating that the minor
464 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
465 RESTORE-FUNCTION nil means don't try to restore the minor mode.
466 Only minor modes for which the name of the buffer-local variable
467 and the name of the minor mode function are different have to be added to
468 this table. See also `desktop-minor-mode-handlers'."
469 :type 'sexp
470 :group 'desktop)
472 ;;;###autoload
473 (defvar desktop-minor-mode-handlers
475 "Alist of functions to restore non-standard minor modes.
476 Functions are called by `desktop-create-buffer' to restore minor modes.
477 List elements must have the form
479 (MINOR-MODE . RESTORE-FUNCTION).
481 Minor modes not specified here, are restored by the standard minor mode
482 function.
484 Handlers are called with argument list
486 (DESKTOP-BUFFER-LOCALS)
488 Furthermore, they may use the following variables:
490 desktop-file-version
491 desktop-buffer-file-name
492 desktop-buffer-name
493 desktop-buffer-major-mode
494 desktop-buffer-minor-modes
495 desktop-buffer-point
496 desktop-buffer-mark
497 desktop-buffer-read-only
498 desktop-buffer-misc
500 When a handler is called, the buffer has been created and the major mode has
501 been set, but local variables listed in desktop-buffer-locals has not yet been
502 created and set.
504 Modules that define a minor mode that needs a special handler should contain
505 code like
507 (defun foo-desktop-restore
509 (add-to-list 'desktop-minor-mode-handlers
510 '(foo-mode . foo-desktop-restore))
512 Furthermore the minor mode function must be autoloaded.
514 See also `desktop-minor-mode-table'.")
516 ;;;###autoload
517 (put 'desktop-minor-mode-handlers 'risky-local-variable t)
519 ;; ----------------------------------------------------------------------------
520 (defvar desktop-dirname nil
521 "The directory in which the desktop file should be saved.")
523 (defun desktop-full-file-name (&optional dirname)
524 "Return the full name of the desktop file in DIRNAME.
525 DIRNAME omitted or nil means use `desktop-dirname'."
526 (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
528 (defun desktop-full-lock-name (&optional dirname)
529 "Return the full name of the desktop lock file in DIRNAME.
530 DIRNAME omitted or nil means use `desktop-dirname'."
531 (expand-file-name desktop-base-lock-name (or dirname desktop-dirname)))
533 (defconst desktop-header
534 ";; --------------------------------------------------------------------------
535 ;; Desktop File for Emacs
536 ;; --------------------------------------------------------------------------
537 " "*Header to place in Desktop file.")
539 (defvar desktop-delay-hook nil
540 "Hooks run after all buffers are loaded; intended for internal use.")
542 ;; ----------------------------------------------------------------------------
543 ;; Desktop file conflict detection
544 (defvar desktop-file-modtime nil
545 "When the desktop file was last modified to the knowledge of this Emacs.
546 Used to detect desktop file conflicts.")
548 (defun desktop-owner (&optional dirname)
549 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
550 Return nil if no desktop file found or no Emacs process is using it.
551 DIRNAME omitted or nil means use `desktop-dirname'."
552 (let (owner)
553 (and (file-exists-p (desktop-full-lock-name dirname))
554 (condition-case nil
555 (with-temp-buffer
556 (insert-file-contents-literally (desktop-full-lock-name dirname))
557 (goto-char (point-min))
558 (setq owner (read (current-buffer)))
559 (integerp owner))
560 (error nil))
561 owner)))
563 (defun desktop-claim-lock (&optional dirname)
564 "Record this Emacs process as the owner of the desktop file in DIRNAME.
565 DIRNAME omitted or nil means use `desktop-dirname'."
566 (write-region (number-to-string (emacs-pid)) nil
567 (desktop-full-lock-name dirname)))
569 (defun desktop-release-lock (&optional dirname)
570 "Remove the lock file for the desktop in DIRNAME.
571 DIRNAME omitted or nil means use `desktop-dirname'."
572 (let ((file (desktop-full-lock-name dirname)))
573 (when (file-exists-p file) (delete-file file))))
575 ;; ----------------------------------------------------------------------------
576 (defun desktop-truncate (list n)
577 "Truncate LIST to at most N elements destructively."
578 (let ((here (nthcdr (1- n) list)))
579 (when (consp here)
580 (setcdr here nil))))
582 ;; ----------------------------------------------------------------------------
583 ;;;###autoload
584 (defun desktop-clear ()
585 "Empty the Desktop.
586 This kills all buffers except for internal ones and those with names matched by
587 a regular expression in the list `desktop-clear-preserve-buffers'.
588 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
589 (interactive)
590 (desktop-lazy-abort)
591 (dolist (var desktop-globals-to-clear)
592 (if (symbolp var)
593 (eval `(setq-default ,var nil))
594 (eval `(setq-default ,(car var) ,(cdr var)))))
595 (let ((buffers (buffer-list))
596 (preserve-regexp (concat "^\\("
597 (mapconcat (lambda (regexp)
598 (concat "\\(" regexp "\\)"))
599 desktop-clear-preserve-buffers
600 "\\|")
601 "\\)$")))
602 (while buffers
603 (let ((bufname (buffer-name (car buffers))))
605 (null bufname)
606 (string-match preserve-regexp bufname)
607 ;; Don't kill buffers made for internal purposes.
608 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
609 (kill-buffer (car buffers))))
610 (setq buffers (cdr buffers))))
611 (delete-other-windows))
613 ;; ----------------------------------------------------------------------------
614 (unless noninteractive
615 (add-hook 'kill-emacs-hook 'desktop-kill))
617 (defun desktop-kill ()
618 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
619 If the desktop should be saved and `desktop-dirname'
620 is nil, ask the user where to save the desktop."
621 (when (and desktop-save-mode
622 (let ((exists (file-exists-p (desktop-full-file-name))))
623 (or (eq desktop-save t)
624 (and exists (eq desktop-save 'if-exists))
625 ;; If it exists, but we aren't using it, we are going
626 ;; to ask for a new directory below.
627 (and exists desktop-dirname (eq desktop-save 'ask-if-new))
628 (and
629 (or (memq desktop-save '(ask ask-if-new))
630 (and exists (eq desktop-save 'ask-if-exists)))
631 (y-or-n-p "Save desktop? ")))))
632 (unless desktop-dirname
633 (setq desktop-dirname
634 (file-name-as-directory
635 (expand-file-name
636 (read-directory-name "Directory for desktop file: " nil nil t)))))
637 (condition-case err
638 (desktop-save desktop-dirname t)
639 (file-error
640 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
641 (signal (car err) (cdr err))))))
642 ;; If we own it, we don't anymore.
643 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
645 ;; ----------------------------------------------------------------------------
646 (defun desktop-list* (&rest args)
647 (if (null (cdr args))
648 (car args)
649 (setq args (nreverse args))
650 (let ((value (cons (nth 1 args) (car args))))
651 (setq args (cdr (cdr args)))
652 (while args
653 (setq value (cons (car args) value))
654 (setq args (cdr args)))
655 value)))
657 ;; ----------------------------------------------------------------------------
658 (defun desktop-buffer-info (buffer)
659 (set-buffer buffer)
660 (list
661 ;; base name of the buffer; replaces the buffer name if managed by uniquify
662 (and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name))
663 ;; basic information
664 (desktop-file-name (buffer-file-name) desktop-dirname)
665 (buffer-name)
666 major-mode
667 ;; minor modes
668 (let (ret)
669 (mapc
670 #'(lambda (minor-mode)
671 (and (boundp minor-mode)
672 (symbol-value minor-mode)
673 (let* ((special (assq minor-mode desktop-minor-mode-table))
674 (value (cond (special (cadr special))
675 ((functionp minor-mode) minor-mode))))
676 (when value (add-to-list 'ret value)))))
677 (mapcar #'car minor-mode-alist))
678 ret)
679 ;; point and mark, and read-only status
680 (point)
681 (list (mark t) mark-active)
682 buffer-read-only
683 ;; auxiliary information
684 (when (functionp desktop-save-buffer)
685 (funcall desktop-save-buffer desktop-dirname))
686 ;; local variables
687 (let ((locals desktop-locals-to-save)
688 (loclist (buffer-local-variables))
689 (ll))
690 (while locals
691 (let ((here (assq (car locals) loclist)))
692 (if here
693 (setq ll (cons here ll))
694 (when (member (car locals) loclist)
695 (setq ll (cons (car locals) ll)))))
696 (setq locals (cdr locals)))
697 ll)))
699 ;; ----------------------------------------------------------------------------
700 (defun desktop-internal-v2s (value)
701 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
702 TXT is a string that when read and evaluated yields value.
703 QUOTE may be `may' (value may be quoted),
704 `must' (values must be quoted), or nil (value may not be quoted)."
705 (cond
706 ((or (numberp value) (null value) (eq t value) (keywordp value))
707 (cons 'may (prin1-to-string value)))
708 ((stringp value)
709 (let ((copy (copy-sequence value)))
710 (set-text-properties 0 (length copy) nil copy)
711 ;; Get rid of text properties because we cannot read them
712 (cons 'may (prin1-to-string copy))))
713 ((symbolp value)
714 (cons 'must (prin1-to-string value)))
715 ((vectorp value)
716 (let* ((special nil)
717 (pass1 (mapcar
718 (lambda (el)
719 (let ((res (desktop-internal-v2s el)))
720 (if (null (car res))
721 (setq special t))
722 res))
723 value)))
724 (if special
725 (cons nil (concat "(vector "
726 (mapconcat (lambda (el)
727 (if (eq (car el) 'must)
728 (concat "'" (cdr el))
729 (cdr el)))
730 pass1
731 " ")
732 ")"))
733 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
734 ((consp value)
735 (let ((p value)
736 newlist
737 use-list*
738 anynil)
739 (while (consp p)
740 (let ((q.txt (desktop-internal-v2s (car p))))
741 (or anynil (setq anynil (null (car q.txt))))
742 (setq newlist (cons q.txt newlist)))
743 (setq p (cdr p)))
744 (if p
745 (let ((last (desktop-internal-v2s p)))
746 (or anynil (setq anynil (null (car last))))
747 (or anynil
748 (setq newlist (cons '(must . ".") newlist)))
749 (setq use-list* t)
750 (setq newlist (cons last newlist))))
751 (setq newlist (nreverse newlist))
752 (if anynil
753 (cons nil
754 (concat (if use-list* "(desktop-list* " "(list ")
755 (mapconcat (lambda (el)
756 (if (eq (car el) 'must)
757 (concat "'" (cdr el))
758 (cdr el)))
759 newlist
760 " ")
761 ")"))
762 (cons 'must
763 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
764 ((subrp value)
765 (cons nil (concat "(symbol-function '"
766 (substring (prin1-to-string value) 7 -1)
767 ")")))
768 ((markerp value)
769 (let ((pos (prin1-to-string (marker-position value)))
770 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
771 (cons nil (concat "(let ((mk (make-marker)))"
772 " (add-hook 'desktop-delay-hook"
773 " (list 'lambda '() (list 'set-marker mk "
774 pos " (get-buffer " buf ")))) mk)"))))
775 (t ; save as text
776 (cons 'may "\"Unprintable entity\""))))
778 ;; ----------------------------------------------------------------------------
779 (defun desktop-value-to-string (value)
780 "Convert VALUE to a string that when read evaluates to the same value.
781 Not all types of values are supported."
782 (let* ((print-escape-newlines t)
783 (float-output-format nil)
784 (quote.txt (desktop-internal-v2s value))
785 (quote (car quote.txt))
786 (txt (cdr quote.txt)))
787 (if (eq quote 'must)
788 (concat "'" txt)
789 txt)))
791 ;; ----------------------------------------------------------------------------
792 (defun desktop-outvar (varspec)
793 "Output a setq statement for variable VAR to the desktop file.
794 The argument VARSPEC may be the variable name VAR (a symbol),
795 or a cons cell of the form (VAR . MAX-SIZE),
796 which means to truncate VAR's value to at most MAX-SIZE elements
797 \(if the value is a list) before saving the value."
798 (let (var size)
799 (if (consp varspec)
800 (setq var (car varspec) size (cdr varspec))
801 (setq var varspec))
802 (when (boundp var)
803 (when (and (integerp size)
804 (> size 0)
805 (listp (eval var)))
806 (desktop-truncate (eval var) size))
807 (insert "(setq "
808 (symbol-name var)
810 (desktop-value-to-string (symbol-value var))
811 ")\n"))))
813 ;; ----------------------------------------------------------------------------
814 (defun desktop-save-buffer-p (filename bufname mode &rest _dummy)
815 "Return t if buffer should have its state saved in the desktop file.
816 FILENAME is the visited file name, BUFNAME is the buffer name, and
817 MODE is the major mode.
818 \n\(fn FILENAME BUFNAME MODE)"
819 (let ((case-fold-search nil)
820 dired-skip)
821 (and (not (and (stringp desktop-buffers-not-to-save)
822 (not filename)
823 (string-match desktop-buffers-not-to-save bufname)))
824 (not (memq mode desktop-modes-not-to-save))
825 ;; FIXME this is broken if desktop-files-not-to-save is nil.
826 (or (and filename
827 (stringp desktop-files-not-to-save)
828 (not (string-match desktop-files-not-to-save filename)))
829 (and (eq mode 'dired-mode)
830 (with-current-buffer bufname
831 (not (setq dired-skip
832 (string-match desktop-files-not-to-save
833 default-directory)))))
834 (and (null filename)
835 (null dired-skip) ; bug#5755
836 (with-current-buffer bufname desktop-save-buffer))))))
838 ;; ----------------------------------------------------------------------------
839 (defun desktop-file-name (filename dirname)
840 "Convert FILENAME to format specified in `desktop-file-name-format'.
841 DIRNAME must be the directory in which the desktop file will be saved."
842 (cond
843 ((not filename) nil)
844 ((eq desktop-file-name-format 'tilde)
845 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
846 (cond
847 ((file-name-absolute-p relative-name) relative-name)
848 ((string= "./" relative-name) "~/")
849 ((string= "." relative-name) "~")
850 (t (concat "~/" relative-name)))))
851 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
852 (t (expand-file-name filename))))
855 ;; ----------------------------------------------------------------------------
856 ;;;###autoload
857 (defun desktop-save (dirname &optional release)
858 "Save the desktop in a desktop file.
859 Parameter DIRNAME specifies where to save the desktop file.
860 Optional parameter RELEASE says whether we're done with this desktop.
861 See also `desktop-base-file-name'."
862 (interactive "DDirectory to save desktop file in: ")
863 (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
864 (save-excursion
865 (let ((eager desktop-restore-eager)
866 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
867 (when
868 (or (not new-modtime) ; nothing to overwrite
869 (equal desktop-file-modtime new-modtime)
870 (yes-or-no-p (if desktop-file-modtime
871 (if (> (float-time new-modtime) (float-time desktop-file-modtime))
872 "Desktop file is more recent than the one loaded. Save anyway? "
873 "Desktop file isn't the one loaded. Overwrite it? ")
874 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
875 (unless release (error "Desktop file conflict")))
877 ;; If we're done with it, release the lock.
878 ;; Otherwise, claim it if it's unclaimed or if we created it.
879 (if release
880 (desktop-release-lock)
881 (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
883 (with-temp-buffer
884 (insert
885 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
886 desktop-header
887 ";; Created " (current-time-string) "\n"
888 ";; Desktop file format version " desktop-file-version "\n"
889 ";; Emacs version " emacs-version "\n")
890 (save-excursion (run-hooks 'desktop-save-hook))
891 (goto-char (point-max))
892 (insert "\n;; Global section:\n")
893 (mapc (function desktop-outvar) desktop-globals-to-save)
894 (when (memq 'kill-ring desktop-globals-to-save)
895 (insert
896 "(setq kill-ring-yank-pointer (nthcdr "
897 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
898 " kill-ring))\n"))
900 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
901 (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
902 (let ((base (pop l)))
903 (when (apply 'desktop-save-buffer-p l)
904 (insert "("
905 (if (or (not (integerp eager))
906 (if (zerop eager)
908 (setq eager (1- eager))))
909 "desktop-create-buffer"
910 "desktop-append-buffer-args")
912 desktop-file-version)
913 ;; If there's a non-empty base name, we save it instead of the buffer name
914 (when (and base (not (string= base "")))
915 (setcar (nthcdr 1 l) base))
916 (dolist (e l)
917 (insert "\n " (desktop-value-to-string e)))
918 (insert ")\n\n"))))
920 (setq default-directory desktop-dirname)
921 (let ((coding-system-for-write 'emacs-mule))
922 (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
923 ;; We remember when it was modified (which is presumably just now).
924 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))
926 ;; ----------------------------------------------------------------------------
927 ;;;###autoload
928 (defun desktop-remove ()
929 "Delete desktop file in `desktop-dirname'.
930 This function also sets `desktop-dirname' to nil."
931 (interactive)
932 (when desktop-dirname
933 (let ((filename (desktop-full-file-name)))
934 (setq desktop-dirname nil)
935 (when (file-exists-p filename)
936 (delete-file filename)))))
938 (defvar desktop-buffer-args-list nil
939 "List of args for `desktop-create-buffer'.")
941 (defvar desktop-lazy-timer nil)
943 ;; ----------------------------------------------------------------------------
944 ;;;###autoload
945 (defun desktop-read (&optional dirname)
946 "Read and process the desktop file in directory DIRNAME.
947 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
948 directories listed in `desktop-path'. If a desktop file is found, it
949 is processed and `desktop-after-read-hook' is run. If no desktop file
950 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
951 This function is a no-op when Emacs is running in batch mode.
952 It returns t if a desktop file was loaded, nil otherwise."
953 (interactive)
954 (unless noninteractive
955 (setq desktop-dirname
956 (file-name-as-directory
957 (expand-file-name
959 ;; If DIRNAME is specified, use it.
960 (and (< 0 (length dirname)) dirname)
961 ;; Otherwise search desktop file in desktop-path.
962 (let ((dirs desktop-path))
963 (while (and dirs
964 (not (file-exists-p
965 (desktop-full-file-name (car dirs)))))
966 (setq dirs (cdr dirs)))
967 (and dirs (car dirs)))
968 ;; If not found and `desktop-path' is non-nil, use its first element.
969 (and desktop-path (car desktop-path))
970 ;; Default: Home directory.
971 "~"))))
972 (if (file-exists-p (desktop-full-file-name))
973 ;; Desktop file found, but is it already in use?
974 (let ((desktop-first-buffer nil)
975 (desktop-buffer-ok-count 0)
976 (desktop-buffer-fail-count 0)
977 (owner (desktop-owner))
978 ;; Avoid desktop saving during evaluation of desktop buffer.
979 (desktop-save nil))
980 (if (and owner
981 (memq desktop-load-locked-desktop '(nil ask))
982 (or (null desktop-load-locked-desktop)
983 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
984 Using it may cause conflicts. Use it anyway? " owner)))))
985 (let ((default-directory desktop-dirname))
986 (setq desktop-dirname nil)
987 (run-hooks 'desktop-not-loaded-hook)
988 (unless desktop-dirname
989 (message "Desktop file in use; not loaded.")))
990 (desktop-lazy-abort)
991 ;; Evaluate desktop buffer and remember when it was modified.
992 (load (desktop-full-file-name) t t t)
993 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
994 ;; If it wasn't already, mark it as in-use, to bother other
995 ;; desktop instances.
996 (unless owner
997 (condition-case nil
998 (desktop-claim-lock)
999 (file-error (message "Couldn't record use of desktop file")
1000 (sit-for 1))))
1002 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1003 ;; We want buffers existing prior to evaluating the desktop (and
1004 ;; not reused) to be placed at the end of the buffer list, so we
1005 ;; move them here.
1006 (mapc 'bury-buffer
1007 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
1008 (switch-to-buffer (car (buffer-list)))
1009 (run-hooks 'desktop-delay-hook)
1010 (setq desktop-delay-hook nil)
1011 (run-hooks 'desktop-after-read-hook)
1012 (message "Desktop: %d buffer%s restored%s%s."
1013 desktop-buffer-ok-count
1014 (if (= 1 desktop-buffer-ok-count) "" "s")
1015 (if (< 0 desktop-buffer-fail-count)
1016 (format ", %d failed to restore" desktop-buffer-fail-count)
1018 (if desktop-buffer-args-list
1019 (format ", %d to restore lazily"
1020 (length desktop-buffer-args-list))
1021 ""))
1023 ;; No desktop file found.
1024 (desktop-clear)
1025 (let ((default-directory desktop-dirname))
1026 (run-hooks 'desktop-no-desktop-file-hook))
1027 (message "No desktop file.")
1028 nil)))
1030 ;; ----------------------------------------------------------------------------
1031 ;; Maintained for backward compatibility
1032 ;;;###autoload
1033 (defun desktop-load-default ()
1034 "Load the `default' start-up library manually.
1035 Also inhibit further loading of it."
1036 (unless inhibit-default-init ; safety check
1037 (load "default" t t)
1038 (setq inhibit-default-init t)))
1039 (make-obsolete 'desktop-load-default
1040 'desktop-save-mode "22.1")
1042 ;; ----------------------------------------------------------------------------
1043 ;;;###autoload
1044 (defun desktop-change-dir (dirname)
1045 "Change to desktop saved in DIRNAME.
1046 Kill the desktop as specified by variables `desktop-save-mode' and
1047 `desktop-save', then clear the desktop and load the desktop file in
1048 directory DIRNAME."
1049 (interactive "DChange to directory: ")
1050 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
1051 (desktop-kill)
1052 (desktop-clear)
1053 (desktop-read dirname))
1055 ;; ----------------------------------------------------------------------------
1056 ;;;###autoload
1057 (defun desktop-save-in-desktop-dir ()
1058 "Save the desktop in directory `desktop-dirname'."
1059 (interactive)
1060 (if desktop-dirname
1061 (desktop-save desktop-dirname)
1062 (call-interactively 'desktop-save))
1063 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname)))
1065 ;; ----------------------------------------------------------------------------
1066 ;;;###autoload
1067 (defun desktop-revert ()
1068 "Revert to the last loaded desktop."
1069 (interactive)
1070 (unless desktop-dirname
1071 (error "Unknown desktop directory"))
1072 (unless (file-exists-p (desktop-full-file-name))
1073 (error "No desktop file found"))
1074 (desktop-clear)
1075 (desktop-read desktop-dirname))
1077 (defvar desktop-buffer-major-mode)
1078 (defvar desktop-buffer-locals)
1079 (defvar auto-insert) ; from autoinsert.el
1080 ;; ----------------------------------------------------------------------------
1081 (defun desktop-restore-file-buffer (buffer-filename
1082 _buffer-name
1083 _buffer-misc)
1084 "Restore a file buffer."
1085 (when buffer-filename
1086 (if (or (file-exists-p buffer-filename)
1087 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1088 buffer-filename)))
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 buffer-filename)))
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 (file-version
1130 buffer-filename
1131 buffer-name
1132 buffer-majormode
1133 buffer-minormodes
1134 buffer-point
1135 buffer-mark
1136 buffer-readonly
1137 buffer-misc
1138 &optional
1139 buffer-locals)
1141 (let ((desktop-file-version file-version)
1142 (desktop-buffer-file-name buffer-filename)
1143 (desktop-buffer-name buffer-name)
1144 (desktop-buffer-major-mode buffer-majormode)
1145 (desktop-buffer-minor-modes buffer-minormodes)
1146 (desktop-buffer-point buffer-point)
1147 (desktop-buffer-mark buffer-mark)
1148 (desktop-buffer-read-only buffer-readonly)
1149 (desktop-buffer-misc buffer-misc)
1150 (desktop-buffer-locals buffer-locals))
1151 ;; To make desktop files with relative file names possible, we cannot
1152 ;; allow `default-directory' to change. Therefore we save current buffer.
1153 (save-current-buffer
1154 ;; Give major mode module a chance to add a handler.
1155 (desktop-load-file desktop-buffer-major-mode)
1156 (let ((buffer-list (buffer-list))
1157 (result
1158 (condition-case-no-debug err
1159 (funcall (or (cdr (assq desktop-buffer-major-mode
1160 desktop-buffer-mode-handlers))
1161 'desktop-restore-file-buffer)
1162 desktop-buffer-file-name
1163 desktop-buffer-name
1164 desktop-buffer-misc)
1165 (error
1166 (message "Desktop: Can't load buffer %s: %s"
1167 desktop-buffer-name
1168 (error-message-string err))
1169 (when desktop-missing-file-warning (sit-for 1))
1170 nil))))
1171 (if (bufferp result)
1172 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1173 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1174 (setq result nil))
1175 ;; Restore buffer list order with new buffer at end. Don't change
1176 ;; the order for old desktop files (old desktop module behaviour).
1177 (unless (< desktop-file-version 206)
1178 (mapc 'bury-buffer buffer-list)
1179 (when result (bury-buffer result)))
1180 (when result
1181 (unless (or desktop-first-buffer (< desktop-file-version 206))
1182 (setq desktop-first-buffer result))
1183 (set-buffer result)
1184 (unless (equal (buffer-name) desktop-buffer-name)
1185 (rename-buffer desktop-buffer-name t))
1186 ;; minor modes
1187 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1188 (auto-fill-mode 1))
1189 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1190 (auto-fill-mode 0))
1192 (dolist (minor-mode desktop-buffer-minor-modes)
1193 ;; Give minor mode module a chance to add a handler.
1194 (desktop-load-file minor-mode)
1195 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1196 (if handler
1197 (funcall handler desktop-buffer-locals)
1198 (when (functionp minor-mode)
1199 (funcall minor-mode 1)))))))
1200 ;; Even though point and mark are non-nil when written by
1201 ;; `desktop-save', they may be modified by handlers wanting to set
1202 ;; point or mark themselves.
1203 (when desktop-buffer-point
1204 (goto-char
1205 (condition-case err
1206 ;; Evaluate point. Thus point can be something like
1207 ;; '(search-forward ...
1208 (eval desktop-buffer-point)
1209 (error (message "%s" (error-message-string err)) 1))))
1210 (when desktop-buffer-mark
1211 (if (consp desktop-buffer-mark)
1212 (progn
1213 (set-mark (car desktop-buffer-mark))
1214 (setq mark-active (car (cdr desktop-buffer-mark))))
1215 (set-mark desktop-buffer-mark)))
1216 ;; Never override file system if the file really is read-only marked.
1217 (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1218 (while desktop-buffer-locals
1219 (let ((this (car desktop-buffer-locals)))
1220 (if (consp this)
1221 ;; an entry of this form `(symbol . value)'
1222 (progn
1223 (make-local-variable (car this))
1224 (set (car this) (cdr this)))
1225 ;; an entry of the form `symbol'
1226 (make-local-variable this)
1227 (makunbound this)))
1228 (setq desktop-buffer-locals (cdr desktop-buffer-locals))))))))
1230 ;; ----------------------------------------------------------------------------
1231 ;; Backward compatibility -- update parameters to 205 standards.
1232 (defun desktop-buffer (buffer-filename buffer-name buffer-majormode
1233 mim pt mk ro tl fc cfs cr buffer-misc)
1234 (desktop-create-buffer 205 buffer-filename buffer-name
1235 buffer-majormode (cdr mim) pt mk ro
1236 buffer-misc
1237 (list (cons 'truncate-lines tl)
1238 (cons 'fill-column fc)
1239 (cons 'case-fold-search cfs)
1240 (cons 'case-replace cr)
1241 (cons 'overwrite-mode (car mim)))))
1243 (defun desktop-append-buffer-args (&rest args)
1244 "Append ARGS at end of `desktop-buffer-args-list'.
1245 ARGS must be an argument list for `desktop-create-buffer'."
1246 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1247 (unless desktop-lazy-timer
1248 (setq desktop-lazy-timer
1249 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1251 (defun desktop-lazy-create-buffer ()
1252 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1253 (when desktop-buffer-args-list
1254 (let* ((remaining (length desktop-buffer-args-list))
1255 (args (pop desktop-buffer-args-list))
1256 (buffer-name (nth 2 args))
1257 (msg (format "Desktop lazily opening %s (%s remaining)..."
1258 buffer-name remaining)))
1259 (when desktop-lazy-verbose
1260 (message "%s" msg))
1261 (let ((desktop-first-buffer nil)
1262 (desktop-buffer-ok-count 0)
1263 (desktop-buffer-fail-count 0))
1264 (apply 'desktop-create-buffer args)
1265 (run-hooks 'desktop-delay-hook)
1266 (setq desktop-delay-hook nil)
1267 (bury-buffer (get-buffer buffer-name))
1268 (when desktop-lazy-verbose
1269 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1271 (defun desktop-idle-create-buffers ()
1272 "Create buffers until the user does something, then stop.
1273 If there are no buffers left to create, kill the timer."
1274 (let ((repeat 1))
1275 (while (and repeat desktop-buffer-args-list)
1276 (save-window-excursion
1277 (desktop-lazy-create-buffer))
1278 (setq repeat (sit-for 0.2))
1279 (unless desktop-buffer-args-list
1280 (cancel-timer desktop-lazy-timer)
1281 (setq desktop-lazy-timer nil)
1282 (message "Lazy desktop load complete")
1283 (sit-for 3)
1284 (message "")))))
1286 (defun desktop-lazy-complete ()
1287 "Run the desktop load to completion."
1288 (interactive)
1289 (let ((desktop-lazy-verbose t))
1290 (while desktop-buffer-args-list
1291 (save-window-excursion
1292 (desktop-lazy-create-buffer)))
1293 (message "Lazy desktop load complete")))
1295 (defun desktop-lazy-abort ()
1296 "Abort lazy loading of the desktop."
1297 (interactive)
1298 (when desktop-lazy-timer
1299 (cancel-timer desktop-lazy-timer)
1300 (setq desktop-lazy-timer nil))
1301 (when desktop-buffer-args-list
1302 (setq desktop-buffer-args-list nil)
1303 (when (called-interactively-p 'interactive)
1304 (message "Lazy desktop load aborted"))))
1306 ;; ----------------------------------------------------------------------------
1307 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1308 ;; command line, we do the rest of what it takes to use desktop, but do it
1309 ;; after finishing loading the init file.
1310 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1311 ;; functions are processed after `after-init-hook'.
1312 (add-hook
1313 'after-init-hook
1314 (lambda ()
1315 (let ((key "--no-desktop"))
1316 (when (member key command-line-args)
1317 (setq command-line-args (delete key command-line-args))
1318 (setq desktop-save-mode nil)))
1319 (when desktop-save-mode
1320 (desktop-read)
1321 (setq inhibit-startup-screen t))))
1323 (provide 'desktop)
1325 ;;; desktop.el ends here