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