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
110 'desktop-save-mode "22.1")
112 (defcustom desktop-save 'ask-if-new
113 "*Specifies whether the desktop should be saved when it is killed.
114 A desktop is killed when the user changes desktop or quits Emacs.
118 ask-if-new -- ask if no desktop file exists, otherwise just save.
119 ask-if-exists -- ask if desktop file exists, otherwise don't save.
120 if-exists -- save if desktop file exists, otherwise don't save.
122 The desktop is never saved when `desktop-save-mode' is nil.
123 The variables `desktop-dirname' and `desktop-base-file-name'
124 determine where the desktop is saved."
126 (const :tag "Always save" t)
127 (const :tag "Always ask" ask)
128 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
129 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
130 (const :tag "Save if desktop file exists, else don't" if-exists)
131 (const :tag "Never save" nil))
135 (defcustom desktop-base-file-name
136 (convert-standard-filename ".emacs.desktop")
137 "Name of file for Emacs desktop, excluding the directory part."
140 (define-obsolete-variable-alias 'desktop-basefilename
141 'desktop-base-file-name "22.1")
143 (defcustom desktop-path '("." "~")
144 "List of directories to search for the desktop file.
145 The base name of the file is specified in `desktop-base-file-name'."
146 :type '(repeat directory)
150 (defcustom desktop-missing-file-warning nil
151 "*If non-nil then `desktop-read' asks if a non-existent file should be recreated.
152 Also pause for a moment to display message about errors signaled in
153 `desktop-buffer-mode-handlers'.
155 If nil, just print error messages in the message buffer."
160 (defcustom desktop-no-desktop-file-hook nil
161 "Normal hook run when `desktop-read' can't find a desktop file.
162 May be used to show a dired buffer."
167 (defcustom desktop-after-read-hook nil
168 "Normal hook run after a successful `desktop-read'.
169 May be used to show a buffer list."
174 (defcustom desktop-save-hook nil
175 "Normal hook run before the desktop is saved in a desktop file.
176 This is useful for truncating history lists, for example."
180 (defcustom desktop-globals-to-save
181 '(desktop-missing-file-warning
187 "List of global variables saved by `desktop-save'.
188 An element may be variable name (a symbol) or a cons cell of the form
189 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
190 MAX-SIZE elements (if the value is a list) before saving the value.
191 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
192 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
195 (defcustom desktop-globals-to-clear
197 kill-ring-yank-pointer
199 search-ring-yank-pointer
201 regexp-search-ring-yank-pointer)
202 "List of global variables that `desktop-clear' will clear.
203 An element may be variable name (a symbol) or a cons cell of the form
204 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
205 to the value obtained by evaluating FORM."
206 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
210 (defcustom desktop-clear-preserve-buffers-regexp
211 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*server\\*\\|\\*tramp/.+\\*\\)$"
212 "Regexp identifying buffers that `desktop-clear' should not delete.
213 See also `desktop-clear-preserve-buffers'."
218 (defcustom desktop-clear-preserve-buffers nil
219 "*List of buffer names that `desktop-clear' should not delete.
220 See also `desktop-clear-preserve-buffers-regexp'."
221 :type '(repeat string)
224 (defcustom desktop-locals-to-save
225 '(desktop-locals-to-save ; Itself! Think it over.
231 change-log-default-name
233 buffer-file-coding-system)
234 "List of local variables to save for each buffer.
235 The variables are saved only when they really are local."
236 :type '(repeat symbol)
238 (make-variable-buffer-local 'desktop-locals-to-save)
240 ;; We skip .log files because they are normally temporary.
241 ;; (ftp) files because they require passwords and whatnot.
242 ;; TAGS files to save time (tags-file-name is saved instead).
243 (defcustom desktop-buffers-not-to-save
244 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
245 "Regexp identifying buffers that are to be excluded from saving."
249 ;; Skip tramp and ange-ftp files
250 (defcustom desktop-files-not-to-save
252 "Regexp identifying files whose buffers are to be excluded from saving."
256 (defcustom desktop-modes-not-to-save nil
257 "List of major modes whose buffers should not be saved."
258 :type '(repeat symbol)
261 (defcustom desktop-file-name-format 'absolute
262 "*Format in which desktop file names should be saved.
264 absolute -- Absolute file name.
265 tilde -- Relative to ~.
266 local -- Relative to directory of desktop file."
267 :type '(choice (const absolute) (const tilde) (const local))
271 (defcustom desktop-restore-eager t
272 "Number of buffers to restore immediately.
273 Remaining buffers are restored lazily (when Emacs is idle).
274 If value is t, all buffers are restored immediately."
275 :type '(choice (const t) integer)
279 (defcustom desktop-lazy-verbose t
280 "Verbose reporting of lazily created buffers."
285 (defcustom desktop-lazy-idle-delay 5
286 "Idle delay before starting to create buffers.
287 See `desktop-restore-eager'."
293 (defvar desktop-save-buffer nil
294 "When non-nil, save buffer status in desktop file.
295 This variable becomes buffer local when set.
297 If the value is a function, it is called by `desktop-save' with argument
298 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
299 file along with the state of the buffer for which it was called.
301 When file names are returned, they should be formatted using the call
302 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
304 Later, when `desktop-read' calls a function in `desktop-buffer-mode-handlers'
305 to restore the buffer, the auxiliary information is passed as the argument
306 DESKTOP-BUFFER-MISC.")
307 (make-variable-buffer-local 'desktop-save-buffer)
308 (make-obsolete-variable 'desktop-buffer-modes-to-save
309 'desktop-save-buffer "22.1")
310 (make-obsolete-variable 'desktop-buffer-misc-functions
311 'desktop-save-buffer "22.1")
313 (defcustom desktop-buffer-mode-handlers
314 '((dired-mode . dired-restore-desktop-buffer)
315 (rmail-mode . rmail-restore-desktop-buffer)
316 (mh-folder-mode . mh-restore-desktop-buffer)
317 (Info-mode . Info-restore-desktop-buffer))
318 "Alist of major mode specific functions to restore a desktop buffer.
319 Functions are called by `desktop-read'. List elements must have the form
320 \(MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
322 Buffers with a major mode not specified here, are restored by the default
323 handler `desktop-restore-file-buffer'.
325 Handlers are called with argument list
327 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
329 Furthermore, they may use the following variables:
332 desktop-buffer-major-mode
333 desktop-buffer-minor-modes
336 desktop-buffer-read-only
337 desktop-buffer-locals
339 If a handler returns a buffer, then the saved mode settings
340 and variable values for that buffer are copied into it."
344 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
345 (make-obsolete-variable 'desktop-buffer-handlers
346 'desktop-buffer-mode-handlers "22.1")
348 (defcustom desktop-minor-mode-table
349 '((auto-fill-function auto-fill-mode)
351 "Table mapping minor mode variables to minor mode functions.
352 Each entry has the form (NAME RESTORE-FUNCTION).
353 NAME is the name of the buffer-local variable indicating that the minor
354 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
355 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
356 Only minor modes for which the name of the buffer-local variable
357 and the name of the minor mode function are different have to be added to
362 ;; ----------------------------------------------------------------------------
363 (defvar desktop-dirname nil
364 "The directory in which the desktop file should be saved.")
366 (defconst desktop-header
367 ";; --------------------------------------------------------------------------
368 ;; Desktop File for Emacs
369 ;; --------------------------------------------------------------------------
370 " "*Header to place in Desktop file.")
372 (defvar desktop-delay-hook nil
373 "Hooks run after all buffers are loaded; intended for internal use.")
375 ;; ----------------------------------------------------------------------------
376 (defun desktop-truncate (list n)
377 "Truncate LIST to at most N elements destructively."
378 (let ((here (nthcdr (1- n) list)))
382 ;; ----------------------------------------------------------------------------
383 (defun desktop-clear ()
385 This kills all buffers except for internal ones and those matching
386 `desktop-clear-preserve-buffers-regexp' or listed in
387 `desktop-clear-preserve-buffers'. Furthermore, it clears the
388 variables listed in `desktop-globals-to-clear'."
391 (dolist (var desktop-globals-to-clear)
393 (eval `(setq-default ,var nil))
394 (eval `(setq-default ,(car var) ,(cdr var)))))
395 (let ((buffers (buffer-list)))
397 (let ((bufname (buffer-name (car buffers))))
400 (string-match desktop-clear-preserve-buffers-regexp bufname)
401 (member bufname desktop-clear-preserve-buffers)
402 ;; Don't kill buffers made for internal purposes.
403 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
404 (kill-buffer (car buffers))))
405 (setq buffers (cdr buffers))))
406 (delete-other-windows))
408 ;; ----------------------------------------------------------------------------
409 (add-hook 'kill-emacs-hook 'desktop-kill)
411 (defun desktop-kill ()
412 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
413 If the desktop should be saved and `desktop-dirname'
414 is nil, ask the user where to save the desktop."
418 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
421 (and exists (memq desktop-save '(ask-if-new if-exists)))
424 (memq desktop-save '(ask ask-if-new))
425 (and exists (eq desktop-save 'ask-if-exists)))
426 (y-or-n-p "Save desktop? ")))))
427 (unless desktop-dirname
428 (setq desktop-dirname
429 (file-name-as-directory
432 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
434 (desktop-save desktop-dirname)
436 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
437 (signal (car err) (cdr err)))))))
439 ;; ----------------------------------------------------------------------------
440 (defun desktop-list* (&rest args)
441 (if (null (cdr args))
443 (setq args (nreverse args))
444 (let ((value (cons (nth 1 args) (car args))))
445 (setq args (cdr (cdr args)))
447 (setq value (cons (car args) value))
448 (setq args (cdr args)))
451 ;; ----------------------------------------------------------------------------
452 (defun desktop-internal-v2s (value)
453 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
454 TXT is a string that when read and evaluated yields value.
455 QUOTE may be `may' (value may be quoted),
456 `must' (values must be quoted), or nil (value may not be quoted)."
458 ((or (numberp value) (null value) (eq t value) (keywordp value))
459 (cons 'may (prin1-to-string value)))
461 (let ((copy (copy-sequence value)))
462 (set-text-properties 0 (length copy) nil copy)
463 ;; Get rid of text properties because we cannot read them
464 (cons 'may (prin1-to-string copy))))
466 (cons 'must (prin1-to-string value)))
471 (let ((res (desktop-internal-v2s el)))
477 (cons nil (concat "(vector "
478 (mapconcat (lambda (el)
479 (if (eq (car el) 'must)
480 (concat "'" (cdr el))
485 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
492 (let ((q.txt (desktop-internal-v2s (car p))))
493 (or anynil (setq anynil (null (car q.txt))))
494 (setq newlist (cons q.txt newlist)))
497 (let ((last (desktop-internal-v2s p))
499 (or anynil (setq anynil (null (car last))))
501 (setq newlist (cons '(must . ".") newlist)))
503 (setq newlist (cons last newlist))))
504 (setq newlist (nreverse newlist))
507 (concat (if use-list* "(desktop-list* " "(list ")
508 (mapconcat (lambda (el)
509 (if (eq (car el) 'must)
510 (concat "'" (cdr el))
516 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
518 (cons nil (concat "(symbol-function '"
519 (substring (prin1-to-string value) 7 -1)
522 (let ((pos (prin1-to-string (marker-position value)))
523 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
524 (cons nil (concat "(let ((mk (make-marker)))"
525 " (add-hook 'desktop-delay-hook"
526 " (list 'lambda '() (list 'set-marker mk "
527 pos " (get-buffer " buf ")))) mk)"))))
529 (cons 'may "\"Unprintable entity\""))))
531 ;; ----------------------------------------------------------------------------
532 (defun desktop-value-to-string (value)
533 "Convert VALUE to a string that when read evaluates to the same value.
534 Not all types of values are supported."
535 (let* ((print-escape-newlines t)
536 (float-output-format nil)
537 (quote.txt (desktop-internal-v2s value))
538 (quote (car quote.txt))
539 (txt (cdr quote.txt)))
544 ;; ----------------------------------------------------------------------------
545 (defun desktop-outvar (varspec)
546 "Output a setq statement for variable VAR to the desktop file.
547 The argument VARSPEC may be the variable name VAR (a symbol),
548 or a cons cell of the form (VAR . MAX-SIZE),
549 which means to truncate VAR's value to at most MAX-SIZE elements
550 \(if the value is a list) before saving the value."
553 (setq var (car varspec) size (cdr varspec))
557 (if (and (integerp size)
560 (desktop-truncate (eval var) size))
564 (desktop-value-to-string (symbol-value var))
567 ;; ----------------------------------------------------------------------------
568 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
569 "Return t if buffer should have its state saved in the desktop file.
570 FILENAME is the visited file name, BUFNAME is the buffer name, and
571 MODE is the major mode."
572 (let ((case-fold-search nil))
573 (and (not (string-match desktop-buffers-not-to-save bufname))
574 (not (memq mode desktop-modes-not-to-save))
576 (not (string-match desktop-files-not-to-save filename)))
577 (and (eq mode 'dired-mode)
578 (with-current-buffer bufname
579 (not (string-match desktop-files-not-to-save
580 default-directory))))
582 (with-current-buffer bufname desktop-save-buffer))))))
584 ;; ----------------------------------------------------------------------------
585 (defun desktop-file-name (filename dirname)
586 "Convert FILENAME to format specified in `desktop-file-name-format'.
587 DIRNAME must be the directory in which the desktop file will be saved."
590 ((eq desktop-file-name-format 'tilde)
591 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
593 ((file-name-absolute-p relative-name) relative-name)
594 ((string= "./" relative-name) "~/")
595 ((string= "." relative-name) "~")
596 (t (concat "~/" relative-name)))))
597 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
598 (t (expand-file-name filename))))
600 ;; ----------------------------------------------------------------------------
601 (defun desktop-save (dirname)
602 "Save the desktop in a desktop file.
603 Parameter DIRNAME specifies where to save the desktop file.
604 See also `desktop-base-file-name'."
605 (interactive "DDirectory to save desktop file in: ")
606 (run-hooks 'desktop-save-hook)
607 (setq dirname (file-name-as-directory (expand-file-name dirname)))
609 (let ((filename (expand-file-name desktop-base-file-name dirname))
615 (desktop-file-name (buffer-file-name) dirname)
621 #'(lambda (minor-mode)
624 (symbol-value minor-mode)
625 (let ((special (assq minor-mode desktop-minor-mode-table)))
626 (when (or special (functionp minor-mode))
629 (if special (cadr special) minor-mode)
631 (mapcar #'car minor-mode-alist))
634 (list (mark t) mark-active)
636 ;; Auxiliary information
637 (when (functionp desktop-save-buffer)
638 (funcall desktop-save-buffer dirname))
639 (let ((locals desktop-locals-to-save)
640 (loclist (buffer-local-variables))
643 (let ((here (assq (car locals) loclist)))
645 (setq ll (cons here ll))
646 (when (member (car locals) loclist)
647 (setq ll (cons (car locals) ll)))))
648 (setq locals (cdr locals)))
651 (eager desktop-restore-eager)
652 (buf (get-buffer-create "*desktop*")))
657 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
659 ";; Created " (current-time-string) "\n"
660 ";; Desktop file format version " desktop-file-version "\n"
661 ";; Emacs version " emacs-version "\n\n"
662 ";; Global section:\n")
663 (mapc (function desktop-outvar) desktop-globals-to-save)
664 (if (memq 'kill-ring desktop-globals-to-save)
666 "(setq kill-ring-yank-pointer (nthcdr "
667 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
670 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
672 (when (apply 'desktop-save-buffer-p l)
674 (if (or (not (integerp eager))
675 (unless (zerop eager)
676 (setq eager (1- eager))
678 "desktop-create-buffer"
679 "desktop-append-buffer-args")
681 desktop-file-version)
683 (insert "\n " (desktop-value-to-string e)))
687 (setq default-directory dirname)
688 (when (file-exists-p filename) (delete-file filename))
689 (let ((coding-system-for-write 'emacs-mule))
690 (write-region (point-min) (point-max) filename nil 'nomessage))))
691 (setq desktop-dirname dirname))
693 ;; ----------------------------------------------------------------------------
694 (defun desktop-remove ()
695 "Delete desktop file in `desktop-dirname'.
696 This function also sets `desktop-dirname' to nil."
698 (when desktop-dirname
699 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname)))
700 (setq desktop-dirname nil)
701 (when (file-exists-p filename)
702 (delete-file filename)))))
704 (defvar desktop-buffer-args-list nil
705 "List of args for `desktop-create-buffer'.")
707 (defvar desktop-lazy-timer nil)
709 ;; ----------------------------------------------------------------------------
711 (defun desktop-read (&optional dirname)
712 "Read and process the desktop file in directory DIRNAME.
713 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
714 directories listed in `desktop-path'. If a desktop file is found, it
715 is processed and `desktop-after-read-hook' is run. If no desktop file
716 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
717 This function is a no-op when Emacs is running in batch mode.
718 It returns t if a desktop file was loaded, nil otherwise."
720 (unless noninteractive
721 (setq desktop-dirname
722 (file-name-as-directory
725 ;; If DIRNAME is specified, use it.
726 (and (< 0 (length dirname)) dirname)
727 ;; Otherwise search desktop file in desktop-path.
728 (let ((dirs desktop-path))
733 (file-exists-p (expand-file-name desktop-base-file-name (car dirs)))))
734 (setq dirs (cdr dirs)))
735 (and dirs (car dirs)))
736 ;; If not found and `desktop-path' is non-nil, use its first element.
737 (and desktop-path (car desktop-path))
738 ;; Default: Home directory.
740 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
741 ;; Desktop file found, process it.
742 (let ((desktop-first-buffer nil)
743 (desktop-buffer-ok-count 0)
744 (desktop-buffer-fail-count 0))
745 (setq desktop-lazy-timer nil)
746 ;; Evaluate desktop buffer.
747 (load (expand-file-name desktop-base-file-name desktop-dirname) t t t)
748 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
749 ;; We want buffers existing prior to evaluating the desktop (and not reused)
750 ;; to be placed at the end of the buffer list, so we move them here.
752 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
753 (switch-to-buffer (car (buffer-list)))
754 (run-hooks 'desktop-delay-hook)
755 (setq desktop-delay-hook nil)
756 (run-hooks 'desktop-after-read-hook)
757 (message "Desktop: %d buffer%s restored%s%s."
758 desktop-buffer-ok-count
759 (if (= 1 desktop-buffer-ok-count) "" "s")
760 (if (< 0 desktop-buffer-fail-count)
761 (format ", %d failed to restore" desktop-buffer-fail-count)
763 (if desktop-buffer-args-list
764 (format ", %d to restore lazily"
765 (length desktop-buffer-args-list))
768 ;; No desktop file found.
770 (let ((default-directory desktop-dirname))
771 (run-hooks 'desktop-no-desktop-file-hook))
772 (message "No desktop file.")
775 ;; ----------------------------------------------------------------------------
776 ;; Maintained for backward compatibility
778 (defun desktop-load-default ()
779 "Load the `default' start-up library manually.
780 Also inhibit further loading of it."
781 (unless inhibit-default-init ; safety check
783 (setq inhibit-default-init t)))
784 (make-obsolete 'desktop-load-default
785 'desktop-save-mode "22.1")
787 ;; ----------------------------------------------------------------------------
789 (defun desktop-change-dir (dirname)
790 "Change to desktop saved in DIRNAME.
791 Kill the desktop as specified by variables `desktop-save-mode' and
792 `desktop-save', then clear the desktop and load the desktop file in
794 (interactive "DChange to directory: ")
795 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
798 (desktop-read dirname))
800 ;; ----------------------------------------------------------------------------
802 (defun desktop-save-in-desktop-dir ()
803 "Save the desktop in directory `desktop-dirname'."
806 (desktop-save desktop-dirname)
807 (call-interactively 'desktop-save))
808 (message "Desktop saved in %s" desktop-dirname))
810 ;; ----------------------------------------------------------------------------
812 (defun desktop-revert ()
813 "Revert to the last loaded desktop."
815 (unless desktop-dirname
816 (error "Unknown desktop directory"))
817 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
818 (error "No desktop file found"))
820 (desktop-read desktop-dirname))
822 ;; ----------------------------------------------------------------------------
823 (defun desktop-restore-file-buffer (desktop-buffer-file-name
826 "Restore a file buffer."
827 (eval-when-compile ; Just to silence the byte compiler
828 (defvar desktop-buffer-major-mode)
829 (defvar desktop-buffer-locals))
830 (if desktop-buffer-file-name
831 (if (or (file-exists-p desktop-buffer-file-name)
832 (let ((msg (format "Desktop: File \"%s\" no longer exists."
833 desktop-buffer-file-name)))
834 (if desktop-missing-file-warning
835 (y-or-n-p (concat msg " Re-create? "))
838 (let* ((auto-insert nil) ; Disable auto insertion
839 (coding-system-for-read
840 (or coding-system-for-read
841 (cdr (assq 'buffer-file-coding-system
842 desktop-buffer-locals))))
843 (buf (find-file-noselect desktop-buffer-file-name)))
845 (switch-to-buffer buf)
846 (error (pop-to-buffer buf)))
847 (and (not (eq major-mode desktop-buffer-major-mode))
848 (functionp desktop-buffer-major-mode)
849 (funcall desktop-buffer-major-mode))
853 ;; ----------------------------------------------------------------------------
854 ;; Create a buffer, load its file, set its mode, ...;
855 ;; called from Desktop file only.
857 (eval-when-compile ; Just to silence the byte compiler
858 (defvar desktop-first-buffer) ;; Dynamically bound in `desktop-read'
861 (defun desktop-create-buffer
862 (desktop-file-version
863 desktop-buffer-file-name
865 desktop-buffer-major-mode
866 desktop-buffer-minor-modes
869 desktop-buffer-read-only
872 desktop-buffer-locals)
873 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
875 (defvar desktop-buffer-ok-count)
876 (defvar desktop-buffer-fail-count))
877 ;; To make desktop files with relative file names possible, we cannot
878 ;; allow `default-directory' to change. Therefore we save current buffer.
880 (let ((buffer-list (buffer-list))
883 (funcall (or (cdr (assq desktop-buffer-major-mode
884 desktop-buffer-mode-handlers))
885 'desktop-restore-file-buffer)
886 desktop-buffer-file-name
890 (message "Desktop: Can't load buffer %s: %s"
892 (error-message-string err))
893 (when desktop-missing-file-warning (sit-for 1))
896 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
897 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
899 ;; Restore buffer list order with new buffer at end. Don't change
900 ;; the order for old desktop files (old desktop module behaviour).
901 (unless (< desktop-file-version 206)
902 (mapc 'bury-buffer buffer-list)
903 (when result (bury-buffer result)))
905 (unless (or desktop-first-buffer (< desktop-file-version 206))
906 (setq desktop-first-buffer result))
908 (unless (equal (buffer-name) desktop-buffer-name)
909 (rename-buffer desktop-buffer-name))
911 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
913 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
916 (mapc #'(lambda (minor-mode)
917 (when (functionp minor-mode) (funcall minor-mode 1)))
918 desktop-buffer-minor-modes)))
919 ;; Even though point and mark are non-nil when written by `desktop-save'
920 ;; they may be modified by handlers wanting to set point or mark themselves.
921 (when desktop-buffer-point
924 ;; Evaluate point. Thus point can be something like '(search-forward ...
925 (eval desktop-buffer-point)
926 (error (message "%s" (error-message-string err)) 1))))
927 (when desktop-buffer-mark
928 (if (consp desktop-buffer-mark)
930 (set-mark (car desktop-buffer-mark))
931 (setq mark-active (car (cdr desktop-buffer-mark))))
932 (set-mark desktop-buffer-mark)))
933 ;; Never override file system if the file really is read-only marked.
934 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
935 (while desktop-buffer-locals
936 (let ((this (car desktop-buffer-locals)))
938 ;; an entry of this form `(symbol . value)'
940 (make-local-variable (car this))
941 (set (car this) (cdr this)))
942 ;; an entry of the form `symbol'
943 (make-local-variable this)
945 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
947 ;; ----------------------------------------------------------------------------
948 ;; Backward compatibility -- update parameters to 205 standards.
949 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
950 desktop-buffer-major-mode
951 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
952 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
953 desktop-buffer-major-mode (cdr mim) pt mk ro
955 (list (cons 'truncate-lines tl)
956 (cons 'fill-column fc)
957 (cons 'case-fold-search cfs)
958 (cons 'case-replace cr)
959 (cons 'overwrite-mode (car mim)))))
961 (defun desktop-append-buffer-args (&rest args)
962 "Append ARGS at end of `desktop-buffer-args-list'.
963 ARGS must be an argument list for `desktop-create-buffer'."
964 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
965 (unless desktop-lazy-timer
966 (setq desktop-lazy-timer
967 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
969 (defun desktop-lazy-create-buffer ()
970 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
971 (when desktop-buffer-args-list
972 (let* ((remaining (length desktop-buffer-args-list))
973 (args (pop desktop-buffer-args-list))
974 (buffer-name (nth 2 args))
975 (msg (format "Desktop lazily opening %s (%s remaining)..."
976 buffer-name remaining)))
977 (when desktop-lazy-verbose
979 (let ((desktop-first-buffer nil)
980 (desktop-buffer-ok-count 0)
981 (desktop-buffer-fail-count 0))
982 (apply 'desktop-create-buffer args)
983 (run-hooks 'desktop-delay-hook)
984 (setq desktop-delay-hook nil)
985 (bury-buffer (get-buffer buffer-name))
986 (when desktop-lazy-verbose
987 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
989 (defun desktop-idle-create-buffers ()
990 "Create buffers until the user does something, then stop.
991 If there are no buffers left to create, kill the timer."
993 (while (and repeat desktop-buffer-args-list)
994 (save-window-excursion
995 (desktop-lazy-create-buffer))
996 (setq repeat (sit-for 0.2))
997 (unless desktop-buffer-args-list
998 (cancel-timer desktop-lazy-timer)
999 (setq desktop-lazy-timer nil)
1000 (message "Lazy desktop load complete")
1004 (defun desktop-lazy-complete ()
1005 "Run the desktop load to completion."
1007 (let ((desktop-lazy-verbose t))
1008 (while desktop-buffer-args-list
1009 (save-window-excursion
1010 (desktop-lazy-create-buffer)))
1011 (message "Lazy desktop load complete")))
1013 (defun desktop-lazy-abort ()
1014 "Abort lazy loading of the desktop."
1016 (when desktop-lazy-timer
1017 (cancel-timer desktop-lazy-timer)
1018 (setq desktop-lazy-timer nil))
1019 (when desktop-buffer-args-list
1020 (setq desktop-buffer-args-list nil)
1021 (when (interactive-p)
1022 (message "Lazy desktop load aborted"))))
1024 ;; ----------------------------------------------------------------------------
1025 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1026 ;; command line, we do the rest of what it takes to use desktop, but do it
1027 ;; after finishing loading the init file.
1028 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1029 ;; functions are processed after `after-init-hook'.
1033 (let ((key "--no-desktop"))
1034 (when (member key command-line-args)
1035 (setq command-line-args (delete key command-line-args))
1036 (setq desktop-save-mode nil)))
1037 (when desktop-save-mode (desktop-read))))
1041 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1042 ;;; desktop.el ends here