1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Maintainter: Lars Hansen <larsh@soem.dk>
8 ;; Keywords: convenience
9 ;; Favourite-brand-of-beer: None, I hate beer.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Save the Desktop, i.e.,
31 ;; - some global variables
32 ;; - the list of buffers with associated files. For each buffer also
34 ;; - the default directory
36 ;; - the mark & mark-active
38 ;; - some local variables
40 ;; To use this, use customize to turn on desktop-save-mode or add the
41 ;; following line somewhere in your .emacs file:
43 ;; (desktop-save-mode 1)
45 ;; For further usage information, look at the section
46 ;; "Saving Emacs Sessions" in the GNU Emacs Manual.
48 ;; When the desktop module is loaded, the function `desktop-kill' is
49 ;; added to the `kill-emacs-hook'. This function is responsible for
50 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
51 ;; function is added to the `after-init-hook'. This function is
52 ;; responsible for loading the desktop when Emacs is started.
54 ;; Some words on minor modes: Most minor modes are controlled by
55 ;; buffer-local variables, which have a standard save / restore
56 ;; mechanism. To handle all minor modes, we take the following
57 ;; approach: (1) check whether the variable name from
58 ;; `minor-mode-alist' is also a function; and (2) use translation
59 ;; table `desktop-minor-mode-table' in the case where the two names
62 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
63 ;; in your home directory is used for that. Saving global default values
64 ;; for buffers is an example of misuse.
66 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
67 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
68 ;; things you did not mean to keep. Use M-x desktop-clear RET.
70 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
71 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
72 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
73 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
74 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
75 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
76 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
77 ;; ---------------------------------------------------------------------------
80 ;; Save window configuration.
81 ;; Recognize more minor modes.
86 (defvar desktop-file-version "206"
87 "Version number of desktop file format.
88 Written into the desktop file and used at desktop read to provide
89 backward compatibility.")
91 ;; ----------------------------------------------------------------------------
92 ;; USER OPTIONS -- settings you might want to play with.
93 ;; ----------------------------------------------------------------------------
96 "Save status of Emacs when you exit."
100 (define-minor-mode desktop-save-mode
101 "Toggle desktop saving mode.
102 With numeric ARG, turn desktop saving on if ARG is positive, off
103 otherwise. See variable `desktop-save' for a description of when the
108 ;; Maintained for backward compatibility
109 (define-obsolete-variable-alias 'desktop-enable 'desktop-save-mode "22.1")
111 (defcustom desktop-save 'ask-if-new
112 "*Specifies whether the desktop should be saved when it is killed.
113 A desktop is killed when the user changes desktop or quits Emacs.
117 ask-if-new -- ask if no desktop file exists, otherwise just save.
118 ask-if-exists -- ask if desktop file exists, otherwise don't save.
119 if-exists -- save if desktop file exists, otherwise don't save.
121 The desktop is never saved when `desktop-save-mode' is nil.
122 The variables `desktop-dirname' and `desktop-base-file-name'
123 determine where the desktop is saved."
125 (const :tag "Always save" t)
126 (const :tag "Always ask" ask)
127 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
128 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
129 (const :tag "Save if desktop file exists, else don't" if-exists)
130 (const :tag "Never save" nil))
134 (defcustom desktop-base-file-name
135 (convert-standard-filename ".emacs.desktop")
136 "Name of file for Emacs desktop, excluding the directory part."
139 (define-obsolete-variable-alias 'desktop-basefilename 'desktop-base-file-name "22.1")
141 (defcustom desktop-path '("." "~")
142 "List of directories to search for the desktop file.
143 The base name of the file is specified in `desktop-base-file-name'."
144 :type '(repeat directory)
148 (defcustom desktop-missing-file-warning nil
149 "*If non-nil then `desktop-read' asks if a non-existent file should be recreated.
150 Also pause for a moment to display message about errors signaled in
151 `desktop-buffer-mode-handlers'.
153 If nil, just print error messages in the message buffer."
158 (defcustom desktop-no-desktop-file-hook nil
159 "Normal hook run when `desktop-read' can't find a desktop file.
160 May be used to show a dired buffer."
165 (defcustom desktop-after-read-hook nil
166 "Normal hook run after a successful `desktop-read'.
167 May be used to show a buffer list."
172 (defcustom desktop-save-hook nil
173 "Normal hook run before the desktop is saved in a desktop file.
174 This is useful for truncating history lists, for example."
178 (defcustom desktop-globals-to-save
179 '(desktop-missing-file-warning
185 "List of global variables saved by `desktop-save'.
186 An element may be variable name (a symbol) or a cons cell of the form
187 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
188 MAX-SIZE elements (if the value is a list) before saving the value.
189 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
190 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
193 (defcustom desktop-globals-to-clear
195 kill-ring-yank-pointer
197 search-ring-yank-pointer
199 regexp-search-ring-yank-pointer)
200 "List of global variables that `desktop-clear' will clear.
201 An element may be variable name (a symbol) or a cons cell of the form
202 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
203 to the value obtained by evaluating FORM."
204 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
208 (defcustom desktop-clear-preserve-buffers-regexp
209 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*tramp/.+\\*\\)$"
210 "Regexp identifying buffers that `desktop-clear' should not delete.
211 See also `desktop-clear-preserve-buffers'."
216 (defcustom desktop-clear-preserve-buffers nil
217 "*List of buffer names that `desktop-clear' should not delete.
218 See also `desktop-clear-preserve-buffers-regexp'."
219 :type '(repeat string)
222 (defcustom desktop-locals-to-save
223 '(desktop-locals-to-save ; Itself! Think it over.
229 change-log-default-name
231 buffer-file-coding-system)
232 "List of local variables to save for each buffer.
233 The variables are saved only when they really are local."
234 :type '(repeat symbol)
236 (make-variable-buffer-local 'desktop-locals-to-save)
238 ;; We skip .log files because they are normally temporary.
239 ;; (ftp) files because they require passwords and whatnot.
240 ;; TAGS files to save time (tags-file-name is saved instead).
241 (defcustom desktop-buffers-not-to-save
242 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
243 "Regexp identifying buffers that are to be excluded from saving."
247 ;; Skip tramp and ange-ftp files
248 (defcustom desktop-files-not-to-save
250 "Regexp identifying files whose buffers are to be excluded from saving."
254 (defcustom desktop-modes-not-to-save nil
255 "List of major modes whose buffers should not be saved."
256 :type '(repeat symbol)
259 (defcustom desktop-file-name-format 'absolute
260 "*Format in which desktop file names should be saved.
262 absolute -- Absolute file name.
263 tilde -- Relative to ~.
264 local -- Relative to directory of desktop file."
265 :type '(choice (const absolute) (const tilde) (const local))
269 (defcustom desktop-restore-eager t
270 "Number of buffers to restore immediately.
271 Remaining buffers are restored lazily (when Emacs is idle).
272 If value is t, all buffers are restored immediately."
273 :type '(choice (const t) integer)
277 (defcustom desktop-lazy-verbose t
278 "Verbose reporting of lazily created buffers."
283 (defcustom desktop-lazy-idle-delay 5
284 "Idle delay before starting to create buffers.
285 See `desktop-restore-eager'."
291 (defvar desktop-save-buffer nil
292 "When non-nil, save buffer status in desktop file.
293 This variable becomes buffer local when set.
295 If the value is a function, it called by `desktop-save' with argument
296 DESKTOP-DIRNAME to obtain auxiliary information to saved in the desktop
297 file along with the state of the buffer for which it was called.
299 When file names are returned, they should be formatted using the call
300 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
302 Later, when `desktop-read' calls a function in `desktop-buffer-mode-handlers'
303 to restore the buffer, the auxiliary information is passed as the argument
304 DESKTOP-BUFFER-MISC.")
305 (make-variable-buffer-local 'desktop-save-buffer)
306 (make-obsolete-variable 'desktop-buffer-modes-to-save
307 'desktop-save-buffer "22.1")
308 (make-obsolete-variable 'desktop-buffer-misc-functions
309 'desktop-save-buffer "22.1")
311 (defcustom desktop-buffer-mode-handlers
312 '((dired-mode . dired-restore-desktop-buffer)
313 (rmail-mode . rmail-restore-desktop-buffer)
314 (mh-folder-mode . mh-restore-desktop-buffer)
315 (Info-mode . Info-restore-desktop-buffer))
316 "Alist of major mode specific functions to restore a desktop buffer.
317 Functions are called by `desktop-read'. List elements must have the form
318 \(MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
320 Buffers with a major mode not specified here, are restored by the default
321 handler `desktop-restore-file-buffer'.
323 Handlers are called with argument list
325 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
327 Furthermore, they may use the following variables:
330 desktop-buffer-major-mode
331 desktop-buffer-minor-modes
334 desktop-buffer-read-only
335 desktop-buffer-locals
337 If a handler returns a buffer, then the saved mode settings
338 and variable values for that buffer are copied into it."
342 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
343 (make-obsolete-variable 'desktop-buffer-handlers
344 'desktop-buffer-mode-handlers "22.1")
346 (defcustom desktop-minor-mode-table
347 '((auto-fill-function auto-fill-mode)
349 "Table mapping minor mode variables to minor mode functions.
350 Each entry has the form (NAME RESTORE-FUNCTION).
351 NAME is the name of the buffer-local variable indicating that the minor
352 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
353 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
354 Only minor modes for which the name of the buffer-local variable
355 and the name of the minor mode function are different have to be added to
360 ;; ----------------------------------------------------------------------------
361 (defvar desktop-dirname nil
362 "The directory in which the desktop file should be saved.")
364 (defconst desktop-header
365 ";; --------------------------------------------------------------------------
366 ;; Desktop File for Emacs
367 ;; --------------------------------------------------------------------------
368 " "*Header to place in Desktop file.")
370 (defvar desktop-delay-hook nil
371 "Hooks run after all buffers are loaded; intended for internal use.")
373 ;; ----------------------------------------------------------------------------
374 (defun desktop-truncate (list n)
375 "Truncate LIST to at most N elements destructively."
376 (let ((here (nthcdr (1- n) list)))
380 ;; ----------------------------------------------------------------------------
381 (defun desktop-clear ()
383 This kills all buffers except for internal ones and those matching
384 `desktop-clear-preserve-buffers-regexp' or listed in
385 `desktop-clear-preserve-buffers'. Furthermore, it clears the
386 variables listed in `desktop-globals-to-clear'."
389 (dolist (var desktop-globals-to-clear)
391 (eval `(setq-default ,var nil))
392 (eval `(setq-default ,(car var) ,(cdr var)))))
393 (let ((buffers (buffer-list)))
395 (let ((bufname (buffer-name (car buffers))))
398 (string-match desktop-clear-preserve-buffers-regexp bufname)
399 (member bufname desktop-clear-preserve-buffers)
400 ;; Don't kill buffers made for internal purposes.
401 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
402 (kill-buffer (car buffers))))
403 (setq buffers (cdr buffers))))
404 (delete-other-windows))
406 ;; ----------------------------------------------------------------------------
407 (add-hook 'kill-emacs-hook 'desktop-kill)
409 (defun desktop-kill ()
410 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
411 If the desktop should be saved and `desktop-dirname'
412 is nil, ask the user where to save the desktop."
416 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
419 (and exists (memq desktop-save '(ask-if-new if-exists)))
422 (memq desktop-save '(ask ask-if-new))
423 (and exists (eq desktop-save 'ask-if-exists)))
424 (y-or-n-p "Save desktop? ")))))
425 (unless desktop-dirname
426 (setq desktop-dirname
427 (file-name-as-directory
430 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
432 (desktop-save desktop-dirname)
434 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
435 (signal (car err) (cdr err)))))))
437 ;; ----------------------------------------------------------------------------
438 (defun desktop-list* (&rest args)
439 (if (null (cdr args))
441 (setq args (nreverse args))
442 (let ((value (cons (nth 1 args) (car args))))
443 (setq args (cdr (cdr args)))
445 (setq value (cons (car args) value))
446 (setq args (cdr args)))
449 ;; ----------------------------------------------------------------------------
450 (defun desktop-internal-v2s (value)
451 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
452 TXT is a string that when read and evaluated yields value.
453 QUOTE may be `may' (value may be quoted),
454 `must' (values must be quoted), or nil (value may not be quoted)."
456 ((or (numberp value) (null value) (eq t value) (keywordp value))
457 (cons 'may (prin1-to-string value)))
459 (let ((copy (copy-sequence value)))
460 (set-text-properties 0 (length copy) nil copy)
461 ;; Get rid of text properties because we cannot read them
462 (cons 'may (prin1-to-string copy))))
464 (cons 'must (prin1-to-string value)))
469 (let ((res (desktop-internal-v2s el)))
475 (cons nil (concat "(vector "
476 (mapconcat (lambda (el)
477 (if (eq (car el) 'must)
478 (concat "'" (cdr el))
483 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
490 (let ((q.txt (desktop-internal-v2s (car p))))
491 (or anynil (setq anynil (null (car q.txt))))
492 (setq newlist (cons q.txt newlist)))
495 (let ((last (desktop-internal-v2s p))
497 (or anynil (setq anynil (null (car last))))
499 (setq newlist (cons '(must . ".") newlist)))
501 (setq newlist (cons last newlist))))
502 (setq newlist (nreverse newlist))
505 (concat (if use-list* "(desktop-list* " "(list ")
506 (mapconcat (lambda (el)
507 (if (eq (car el) 'must)
508 (concat "'" (cdr el))
514 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
516 (cons nil (concat "(symbol-function '"
517 (substring (prin1-to-string value) 7 -1)
520 (let ((pos (prin1-to-string (marker-position value)))
521 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
522 (cons nil (concat "(let ((mk (make-marker)))"
523 " (add-hook 'desktop-delay-hook"
524 " (list 'lambda '() (list 'set-marker mk "
525 pos " (get-buffer " buf ")))) mk)"))))
527 (cons 'may "\"Unprintable entity\""))))
529 ;; ----------------------------------------------------------------------------
530 (defun desktop-value-to-string (value)
531 "Convert VALUE to a string that when read evaluates to the same value.
532 Not all types of values are supported."
533 (let* ((print-escape-newlines t)
534 (float-output-format nil)
535 (quote.txt (desktop-internal-v2s value))
536 (quote (car quote.txt))
537 (txt (cdr quote.txt)))
542 ;; ----------------------------------------------------------------------------
543 (defun desktop-outvar (varspec)
544 "Output a setq statement for variable VAR to the desktop file.
545 The argument VARSPEC may be the variable name VAR (a symbol),
546 or a cons cell of the form (VAR . MAX-SIZE),
547 which means to truncate VAR's value to at most MAX-SIZE elements
548 \(if the value is a list) before saving the value."
551 (setq var (car varspec) size (cdr varspec))
555 (if (and (integerp size)
558 (desktop-truncate (eval var) size))
562 (desktop-value-to-string (symbol-value var))
565 ;; ----------------------------------------------------------------------------
566 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
567 "Return t if buffer should have its state saved in the desktop file.
568 FILENAME is the visited file name, BUFNAME is the buffer name, and
569 MODE is the major mode."
570 (let ((case-fold-search nil))
571 (and (not (string-match desktop-buffers-not-to-save bufname))
572 (not (memq mode desktop-modes-not-to-save))
574 (not (string-match desktop-files-not-to-save filename)))
575 (and (eq mode 'dired-mode)
576 (with-current-buffer bufname
577 (not (string-match desktop-files-not-to-save
578 default-directory))))
580 (with-current-buffer bufname desktop-save-buffer))))))
582 ;; ----------------------------------------------------------------------------
583 (defun desktop-file-name (filename dirname)
584 "Convert FILENAME to format specified in `desktop-file-name-format'.
585 DIRNAME must be the directory in which the desktop file will be saved."
588 ((eq desktop-file-name-format 'tilde)
589 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
591 ((file-name-absolute-p relative-name) relative-name)
592 ((string= "./" relative-name) "~/")
593 ((string= "." relative-name) "~")
594 (t (concat "~/" relative-name)))))
595 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
596 (t (expand-file-name filename))))
598 ;; ----------------------------------------------------------------------------
599 (defun desktop-save (dirname)
600 "Save the desktop in a desktop file.
601 Parameter DIRNAME specifies where to save the desktop file.
602 See also `desktop-base-file-name'."
603 (interactive "DDirectory to save desktop file in: ")
604 (run-hooks 'desktop-save-hook)
605 (setq dirname (file-name-as-directory (expand-file-name dirname)))
607 (let ((filename (expand-file-name desktop-base-file-name dirname))
613 (desktop-file-name (buffer-file-name) dirname)
619 #'(lambda (minor-mode)
622 (symbol-value minor-mode)
623 (let ((special (assq minor-mode desktop-minor-mode-table)))
624 (when (or special (functionp minor-mode))
627 (if special (cadr special) minor-mode)
629 (mapcar #'car minor-mode-alist))
632 (list (mark t) mark-active)
634 ;; Auxiliary information
635 (when (functionp desktop-save-buffer)
636 (funcall desktop-save-buffer dirname))
637 (let ((locals desktop-locals-to-save)
638 (loclist (buffer-local-variables))
641 (let ((here (assq (car locals) loclist)))
643 (setq ll (cons here ll))
644 (when (member (car locals) loclist)
645 (setq ll (cons (car locals) ll)))))
646 (setq locals (cdr locals)))
649 (eager desktop-restore-eager)
650 (buf (get-buffer-create "*desktop*")))
655 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
657 ";; Created " (current-time-string) "\n"
658 ";; Desktop file format version " desktop-file-version "\n"
659 ";; Emacs version " emacs-version "\n\n"
660 ";; Global section:\n")
661 (mapc (function desktop-outvar) desktop-globals-to-save)
662 (if (memq 'kill-ring desktop-globals-to-save)
664 "(setq kill-ring-yank-pointer (nthcdr "
665 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
668 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
670 (when (apply 'desktop-save-buffer-p l)
672 (if (or (not (integerp eager))
673 (unless (zerop eager)
674 (setq eager (1- eager))
676 "desktop-create-buffer"
677 "desktop-append-buffer-args")
679 desktop-file-version)
681 (insert "\n " (desktop-value-to-string e)))
685 (setq default-directory dirname)
686 (when (file-exists-p filename) (delete-file filename))
687 (let ((coding-system-for-write 'emacs-mule))
688 (write-region (point-min) (point-max) filename nil 'nomessage))))
689 (setq desktop-dirname dirname))
691 ;; ----------------------------------------------------------------------------
692 (defun desktop-remove ()
693 "Delete desktop file in `desktop-dirname'.
694 This function also sets `desktop-dirname' to nil."
696 (when desktop-dirname
697 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname)))
698 (setq desktop-dirname nil)
699 (when (file-exists-p filename)
700 (delete-file filename)))))
702 (defvar desktop-buffer-args-list nil
703 "List of args for `desktop-create-buffer'.")
705 (defvar desktop-lazy-timer nil)
707 ;; ----------------------------------------------------------------------------
709 (defun desktop-read (&optional dirname)
710 "Read and process the desktop file in directory DIRNAME.
711 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
712 directories listed in `desktop-path'. If a desktop file is found, it
713 is processed and `desktop-after-read-hook' is run. If no desktop file
714 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
715 This function is a no-op when Emacs is running in batch mode.
716 It returns t if a desktop file was loaded, nil otherwise."
718 (unless noninteractive
719 (setq desktop-dirname
720 (file-name-as-directory
723 ;; If DIRNAME is specified, use it.
724 (and (< 0 (length dirname)) dirname)
725 ;; Otherwise search desktop file in desktop-path.
726 (let ((dirs desktop-path))
731 (file-exists-p (expand-file-name desktop-base-file-name (car dirs)))))
732 (setq dirs (cdr dirs)))
733 (and dirs (car dirs)))
734 ;; If not found and `desktop-path' is non-nil, use its first element.
735 (and desktop-path (car desktop-path))
736 ;; Default: Home directory.
738 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
739 ;; Desktop file found, process it.
740 (let ((desktop-first-buffer nil)
741 (desktop-buffer-ok-count 0)
742 (desktop-buffer-fail-count 0))
743 (setq desktop-lazy-timer nil)
744 ;; Evaluate desktop buffer.
745 (load (expand-file-name desktop-base-file-name desktop-dirname) t t t)
746 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
747 ;; We want buffers existing prior to evaluating the desktop (and not reused)
748 ;; to be placed at the end of the buffer list, so we move them here.
750 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
751 (switch-to-buffer (car (buffer-list)))
752 (run-hooks 'desktop-delay-hook)
753 (setq desktop-delay-hook nil)
754 (run-hooks 'desktop-after-read-hook)
755 (message "Desktop: %d buffer%s restored%s%s."
756 desktop-buffer-ok-count
757 (if (= 1 desktop-buffer-ok-count) "" "s")
758 (if (< 0 desktop-buffer-fail-count)
759 (format ", %d failed to restore" desktop-buffer-fail-count)
761 (if desktop-buffer-args-list
762 (format ", %d to restore lazily"
763 (length desktop-buffer-args-list))
766 ;; No desktop file found.
768 (let ((default-directory desktop-dirname))
769 (run-hooks 'desktop-no-desktop-file-hook))
770 (message "No desktop file.")
773 ;; ----------------------------------------------------------------------------
774 ;; Maintained for backward compatibility
776 (defun desktop-load-default ()
777 "Load the `default' start-up library manually.
778 Also inhibit further loading of it."
779 (if (not inhibit-default-init) ; safety check
782 (setq inhibit-default-init t))))
783 (make-obsolete 'desktop-load-default 'desktop-save-mode "22.1")
785 ;; ----------------------------------------------------------------------------
787 (defun desktop-change-dir (dirname)
788 "Change to desktop saved in DIRNAME.
789 Kill the desktop as specified by variables `desktop-save-mode' and
790 `desktop-save', then clear the desktop and load the desktop file in
792 (interactive "DChange to directory: ")
793 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
796 (desktop-read dirname))
798 ;; ----------------------------------------------------------------------------
800 (defun desktop-save-in-desktop-dir ()
801 "Save the desktop in directory `desktop-dirname'."
804 (desktop-save desktop-dirname)
805 (call-interactively 'desktop-save))
806 (message "Desktop saved in %s" desktop-dirname))
808 ;; ----------------------------------------------------------------------------
810 (defun desktop-revert ()
811 "Revert to the last loaded desktop."
813 (unless desktop-dirname
814 (error "Unknown desktop directory"))
815 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
816 (error "No desktop file found"))
818 (desktop-read desktop-dirname))
820 ;; ----------------------------------------------------------------------------
821 (defun desktop-restore-file-buffer (desktop-buffer-file-name
824 "Restore a file buffer."
825 (eval-when-compile ; Just to silence the byte compiler
826 (defvar desktop-buffer-major-mode)
827 (defvar desktop-buffer-locals))
828 (if desktop-buffer-file-name
829 (if (or (file-exists-p desktop-buffer-file-name)
830 (let ((msg (format "Desktop: File \"%s\" no longer exists."
831 desktop-buffer-file-name)))
832 (if desktop-missing-file-warning
833 (y-or-n-p (concat msg " Re-create? "))
836 (let* ((auto-insert nil) ; Disable auto insertion
837 (coding-system-for-read
838 (or coding-system-for-read
839 (cdr (assq 'buffer-file-coding-system
840 desktop-buffer-locals))))
841 (buf (find-file-noselect desktop-buffer-file-name)))
843 (switch-to-buffer buf)
844 (error (pop-to-buffer buf)))
845 (and (not (eq major-mode desktop-buffer-major-mode))
846 (functionp desktop-buffer-major-mode)
847 (funcall desktop-buffer-major-mode))
851 ;; ----------------------------------------------------------------------------
852 ;; Create a buffer, load its file, set its mode, ...;
853 ;; called from Desktop file only.
855 (eval-when-compile ; Just to silence the byte compiler
856 (defvar desktop-first-buffer) ;; Dynamically bound in `desktop-read'
859 (defun desktop-create-buffer
860 (desktop-file-version
861 desktop-buffer-file-name
863 desktop-buffer-major-mode
864 desktop-buffer-minor-modes
867 desktop-buffer-read-only
870 desktop-buffer-locals)
871 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
873 (defvar desktop-buffer-ok-count)
874 (defvar desktop-buffer-fail-count))
875 ;; To make desktop files with relative file names possible, we cannot
876 ;; allow `default-directory' to change. Therefore we save current buffer.
878 (let ((buffer-list (buffer-list))
881 (funcall (or (cdr (assq desktop-buffer-major-mode
882 desktop-buffer-mode-handlers))
883 'desktop-restore-file-buffer)
884 desktop-buffer-file-name
888 (message "Desktop: Can't load buffer %s: %s"
890 (error-message-string err))
891 (when desktop-missing-file-warning (sit-for 1))
894 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
895 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
897 ;; Restore buffer list order with new buffer at end. Don't change
898 ;; the order for old desktop files (old desktop module behaviour).
899 (unless (< desktop-file-version 206)
900 (mapc 'bury-buffer buffer-list)
901 (when result (bury-buffer result)))
903 (unless (or desktop-first-buffer (< desktop-file-version 206))
904 (setq desktop-first-buffer result))
906 (unless (equal (buffer-name) desktop-buffer-name)
907 (rename-buffer desktop-buffer-name))
909 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
911 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
914 (mapc #'(lambda (minor-mode)
915 (when (functionp minor-mode) (funcall minor-mode 1)))
916 desktop-buffer-minor-modes)))
917 ;; Even though point and mark are non-nil when written by `desktop-save'
918 ;; they may be modified by handlers wanting to set point or mark themselves.
919 (when desktop-buffer-point
922 ;; Evaluate point. Thus point can be something like '(search-forward ...
923 (eval desktop-buffer-point)
924 (error (message "%s" (error-message-string err)) 1))))
925 (when desktop-buffer-mark
926 (if (consp desktop-buffer-mark)
928 (set-mark (car desktop-buffer-mark))
929 (setq mark-active (car (cdr desktop-buffer-mark))))
930 (set-mark desktop-buffer-mark)))
931 ;; Never override file system if the file really is read-only marked.
932 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
933 (while desktop-buffer-locals
934 (let ((this (car desktop-buffer-locals)))
936 ;; an entry of this form `(symbol . value)'
938 (make-local-variable (car this))
939 (set (car this) (cdr this)))
940 ;; an entry of the form `symbol'
941 (make-local-variable this)
943 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
945 ;; ----------------------------------------------------------------------------
946 ;; Backward compatibility -- update parameters to 205 standards.
947 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
948 desktop-buffer-major-mode
949 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
950 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
951 desktop-buffer-major-mode (cdr mim) pt mk ro
953 (list (cons 'truncate-lines tl)
954 (cons 'fill-column fc)
955 (cons 'case-fold-search cfs)
956 (cons 'case-replace cr)
957 (cons 'overwrite-mode (car mim)))))
959 (defun desktop-append-buffer-args (&rest args)
960 "Append ARGS at end of `desktop-buffer-args-list'.
961 ARGS must be an argument list for `desktop-create-buffer'."
962 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
963 (unless desktop-lazy-timer
964 (setq desktop-lazy-timer
965 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
967 (defun desktop-lazy-create-buffer ()
968 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
969 (when desktop-buffer-args-list
970 (let* ((remaining (length desktop-buffer-args-list))
971 (args (pop desktop-buffer-args-list))
972 (buffer-name (nth 2 args))
973 (msg (format "Desktop lazily opening %s (%s remaining)..."
974 buffer-name remaining)))
975 (when desktop-lazy-verbose
977 (let ((desktop-first-buffer nil)
978 (desktop-buffer-ok-count 0)
979 (desktop-buffer-fail-count 0))
980 (apply 'desktop-create-buffer args)
981 (run-hooks 'desktop-delay-hook)
982 (setq desktop-delay-hook nil)
983 (bury-buffer (get-buffer buffer-name))
984 (when desktop-lazy-verbose
985 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
987 (defun desktop-idle-create-buffers ()
988 "Create buffers until the user does something, then stop.
989 If there are no buffers left to create, kill the timer."
991 (while (and repeat desktop-buffer-args-list)
992 (save-window-excursion
993 (desktop-lazy-create-buffer))
994 (setq repeat (sit-for 0.2))
995 (unless desktop-buffer-args-list
996 (cancel-timer desktop-lazy-timer)
997 (setq desktop-lazy-timer nil)
998 (message "Lazy desktop load complete")
1002 (defun desktop-lazy-complete ()
1003 "Run the desktop load to completion."
1005 (let ((desktop-lazy-verbose t))
1006 (while desktop-buffer-args-list
1007 (save-window-excursion
1008 (desktop-lazy-create-buffer)))
1009 (message "Lazy desktop load complete")))
1011 (defun desktop-lazy-abort ()
1012 "Abort lazy loading of the desktop."
1014 (when desktop-lazy-timer
1015 (cancel-timer desktop-lazy-timer)
1016 (setq desktop-lazy-timer nil))
1017 (when desktop-buffer-args-list
1018 (setq desktop-buffer-args-list nil)
1019 (when (interactive-p)
1020 (message "Lazy desktop load aborted"))))
1022 ;; ----------------------------------------------------------------------------
1023 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1024 ;; command line, we do the rest of what it takes to use desktop, but do it
1025 ;; after finishing loading the init file.
1026 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1027 ;; functions are processed after `after-init-hook'.
1031 (let ((key "--no-desktop"))
1032 (when (member key command-line-args)
1033 (setq command-line-args (delete key command-line-args))
1034 (setq desktop-save-mode nil)))
1035 (when desktop-save-mode (desktop-read))))
1039 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1040 ;;; desktop.el ends here