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