1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Maintainter: Lars Hansen <larsh@math.ku.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., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, 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 (defvaralias 'desktop-enable
'desktop-save-mode
)
110 (make-obsolete-variable 'desktop-enable
'desktop-save-mode
)
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-directory' 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 (defvaralias 'desktop-basefilename
'desktop-base-file-name
)
142 (defcustom desktop-path
'("." "~")
143 "List of directories to search for the desktop file.
144 The base name of the file is specified in `desktop-base-file-name'."
145 :type
'(repeat directory
)
149 (defcustom desktop-missing-file-warning nil
150 "*If non-nil then `desktop-read' asks if a non-existent file should be recreated.
151 Also pause for a moment to display message about errors signaled in
152 `desktop-buffer-mode-handlers'.
154 If nil, just print error messages in the message buffer."
159 (defcustom desktop-no-desktop-file-hook nil
160 "Normal hook run when `desktop-read' can't find a desktop file.
161 May e.g. be used to show a dired buffer."
166 (defcustom desktop-after-read-hook nil
167 "Normal hook run after a successful `desktop-read'.
168 May e.g. be used to show a buffer list."
173 (defcustom desktop-save-hook nil
174 "Normal hook run before the desktop is saved in a desktop file.
175 This is useful for truncating history lists, for example."
179 (defcustom desktop-globals-to-save
180 '(desktop-missing-file-warning
186 "List of global variables saved by `desktop-save'.
187 An element may be variable name (a symbol) or a cons cell of the form
188 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
189 MAX-SIZE elements (if the value is a list) before saving the value.
190 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
191 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
194 (defcustom desktop-globals-to-clear
196 kill-ring-yank-pointer
198 search-ring-yank-pointer
200 regexp-search-ring-yank-pointer
)
201 "List of global variables that `desktop-clear' will clear.
202 An element may be variable name (a symbol) or a cons cell of the form
203 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
204 to the value obtained by evaluateing FORM."
205 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
209 (defcustom desktop-clear-preserve-buffers-regexp
210 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*tramp/.+\\*\\)$"
211 "Regexp identifying buffers that `desktop-clear' should not delete.
212 See also `desktop-clear-preserve-buffers'."
217 (defcustom desktop-clear-preserve-buffers nil
218 "*List of buffer names that `desktop-clear' should not delete.
219 See also `desktop-clear-preserve-buffers-regexp'."
220 :type
'(repeat string
)
223 (defcustom desktop-locals-to-save
224 '(desktop-locals-to-save ; Itself! Think it over.
230 change-log-default-name
232 buffer-file-coding-system
)
233 "List of local variables to save for each buffer.
234 The variables are saved only when they really are local."
235 :type
'(repeat symbol
)
237 (make-variable-buffer-local 'desktop-locals-to-save
)
239 ;; We skip .log files because they are normally temporary.
240 ;; (ftp) files because they require passwords and whatnot.
241 ;; TAGS files to save time (tags-file-name is saved instead).
242 (defcustom desktop-buffers-not-to-save
243 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
244 "Regexp identifying buffers that are to be excluded from saving."
248 ;; Skip tramp and ange-ftp files
249 (defcustom desktop-files-not-to-save
251 "Regexp identifying files whose buffers are to be excluded from saving."
255 (defcustom desktop-modes-not-to-save nil
256 "List of major modes whose buffers should not be saved."
257 :type
'(repeat symbol
)
260 (defcustom desktop-file-name-format
'absolute
261 "*Format in which desktop file names should be saved.
263 absolute -- Absolute file name.
264 tilde -- Relative to ~.
265 local -- Relative to directory of desktop file."
266 :type
'(choice (const absolute
) (const tilde
) (const local
))
270 (defcustom desktop-restore-eager t
271 "Number of buffers to restore immediately.
272 Remaining buffers are restored lazily (when Emacs is idle).
273 If value is t, all buffers are restored immediately."
274 :type
'(choice (const t
) integer
)
278 (defcustom desktop-lazy-verbose t
279 "Verbose reporting of lazily created buffers."
284 (defcustom desktop-lazy-idle-delay
5
285 "Idle delay before starting to create buffers.
286 See `desktop-restore-eager'."
292 (defvar desktop-save-buffer nil
293 "When non-nil, save buffer status in desktop file.
294 This variable becomes buffer local when set.
296 If the value is a function, it called by `desktop-save' with argument
297 DESKTOP-DIRNAME to obtain auxiliary information to saved in the desktop
298 file along with the state of the buffer for which it was called.
300 When file names are returned, they should be formatted using the call
301 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
303 Later, when `desktop-read' calls a function in `desktop-buffer-mode-handlers'
304 to restore the buffer, the auxiliary information is passed as the argument
305 DESKTOP-BUFFER-MISC.")
306 (make-variable-buffer-local 'desktop-save-buffer
)
307 (make-obsolete-variable 'desktop-buffer-modes-to-save
308 'desktop-save-buffer
)
309 (make-obsolete-variable 'desktop-buffer-misc-functions
310 'desktop-save-buffer
)
312 (defcustom desktop-buffer-mode-handlers
313 '((dired-mode . dired-restore-desktop-buffer
)
314 (rmail-mode . rmail-restore-desktop-buffer
)
315 (mh-folder-mode . mh-restore-desktop-buffer
)
316 (Info-mode . Info-restore-desktop-buffer
))
317 "Alist of major mode specific functions to restore a desktop buffer.
318 Functions are called by `desktop-read'. List elements must have the form
319 \(MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
321 Buffers with a major mode not specified here, are restored by the default
322 handler `desktop-restore-file-buffer'.
324 Handlers are called with argument list
326 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
328 Furthermore, they may use the following variables:
331 desktop-buffer-major-mode
332 desktop-buffer-minor-modes
335 desktop-buffer-read-only
336 desktop-buffer-locals
338 If a handler returns a buffer, then the saved mode settings
339 and variable values for that buffer are copied into it."
343 (put 'desktop-buffer-mode-handlers
'risky-local-variable t
)
344 (make-obsolete-variable 'desktop-buffer-handlers
345 'desktop-buffer-mode-handlers
)
347 (defcustom desktop-minor-mode-table
348 '((auto-fill-function auto-fill-mode
)
350 "Table mapping minor mode variables to minor mode functions.
351 Each entry has the form (NAME RESTORE-FUNCTION).
352 NAME is the name of the buffer-local variable indicating that the minor
353 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
354 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
355 Only minor modes for which the name of the buffer-local variable
356 and the name of the minor mode function are different have to be added to
361 ;; ----------------------------------------------------------------------------
362 (defvar desktop-dirname nil
363 "The directory in which the desktop file should be saved.")
365 (defconst desktop-header
366 ";; --------------------------------------------------------------------------
367 ;; Desktop File for Emacs
368 ;; --------------------------------------------------------------------------
369 " "*Header to place in Desktop file.")
371 (defvar desktop-delay-hook nil
372 "Hooks run after all buffers are loaded; intended for internal use.")
374 ;; ----------------------------------------------------------------------------
375 (defun desktop-truncate (list n
)
376 "Truncate LIST to at most N elements destructively."
377 (let ((here (nthcdr (1- n
) list
)))
381 ;; ----------------------------------------------------------------------------
382 (defun desktop-clear ()
384 This kills all buffers except for internal ones and those matching
385 `desktop-clear-preserve-buffers-regexp' or listed in
386 `desktop-clear-preserve-buffers'. Furthermore, it clears the
387 variables listed in `desktop-globals-to-clear'."
390 (dolist (var desktop-globals-to-clear
)
392 (eval `(setq-default ,var nil
))
393 (eval `(setq-default ,(car var
) ,(cdr var
)))))
394 (let ((buffers (buffer-list)))
396 (let ((bufname (buffer-name (car buffers
))))
399 (string-match desktop-clear-preserve-buffers-regexp bufname
)
400 (member bufname desktop-clear-preserve-buffers
)
401 ;; Don't kill buffers made for internal purposes.
402 (and (not (equal bufname
"")) (eq (aref bufname
0) ?\
))
403 (kill-buffer (car buffers
))))
404 (setq buffers
(cdr buffers
))))
405 (delete-other-windows))
407 ;; ----------------------------------------------------------------------------
408 (add-hook 'kill-emacs-hook
'desktop-kill
)
410 (defun desktop-kill ()
411 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
412 If the desktop should be saved and `desktop-dirname'
413 is nil, ask the user where to save the desktop."
417 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))))
420 (and exists
(memq desktop-save
'(ask-if-new if-exists
)))
423 (memq desktop-save
'(ask ask-if-new
))
424 (and exists
(eq desktop-save
'ask-if-exists
)))
425 (y-or-n-p "Save desktop? ")))))
426 (unless desktop-dirname
427 (setq desktop-dirname
428 (file-name-as-directory
431 (lambda (dir) (interactive "DDirectory for desktop file: ") dir
))))))
433 (desktop-save desktop-dirname
)
435 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
436 (signal (car err
) (cdr err
)))))))
438 ;; ----------------------------------------------------------------------------
439 (defun desktop-list* (&rest args
)
440 (if (null (cdr args
))
442 (setq args
(nreverse args
))
443 (let ((value (cons (nth 1 args
) (car args
))))
444 (setq args
(cdr (cdr args
)))
446 (setq value
(cons (car args
) value
))
447 (setq args
(cdr args
)))
450 ;; ----------------------------------------------------------------------------
451 (defun desktop-internal-v2s (value)
452 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
453 TXT is a string that when read and evaluated yields value.
454 QUOTE may be `may' (value may be quoted),
455 `must' (values must be quoted), or nil (value may not be quoted)."
457 ((or (numberp value
) (null value
) (eq t value
))
458 (cons 'may
(prin1-to-string value
)))
460 (let ((copy (copy-sequence value
)))
461 (set-text-properties 0 (length copy
) nil copy
)
462 ;; Get rid of text properties because we cannot read them
463 (cons 'may
(prin1-to-string copy
))))
465 (cons 'must
(prin1-to-string value
)))
470 (let ((res (desktop-internal-v2s el
)))
476 (cons nil
(concat "(vector "
477 (mapconcat (lambda (el)
478 (if (eq (car el
) 'must
)
479 (concat "'" (cdr el
))
484 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
491 (let ((q.txt
(desktop-internal-v2s (car p
))))
492 (or anynil
(setq anynil
(null (car q.txt
))))
493 (setq newlist
(cons q.txt newlist
)))
496 (let ((last (desktop-internal-v2s p
))
498 (or anynil
(setq anynil
(null (car last
))))
500 (setq newlist
(cons '(must .
".") newlist
)))
502 (setq newlist
(cons last newlist
))))
503 (setq newlist
(nreverse newlist
))
506 (concat (if use-list
* "(desktop-list* " "(list ")
507 (mapconcat (lambda (el)
508 (if (eq (car el
) 'must
)
509 (concat "'" (cdr el
))
515 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
517 (cons nil
(concat "(symbol-function '"
518 (substring (prin1-to-string value
) 7 -
1)
521 (let ((pos (prin1-to-string (marker-position value
)))
522 (buf (prin1-to-string (buffer-name (marker-buffer value
)))))
523 (cons nil
(concat "(let ((mk (make-marker)))"
524 " (add-hook 'desktop-delay-hook"
525 " (list 'lambda '() (list 'set-marker mk "
526 pos
" (get-buffer " buf
")))) mk)"))))
528 (cons 'may
"\"Unprintable entity\""))))
530 ;; ----------------------------------------------------------------------------
531 (defun desktop-value-to-string (value)
532 "Convert VALUE to a string that when read evaluates to the same value.
533 Not all types of values are supported."
534 (let* ((print-escape-newlines t
)
535 (float-output-format nil
)
536 (quote.txt
(desktop-internal-v2s value
))
537 (quote (car quote.txt
))
538 (txt (cdr quote.txt
)))
543 ;; ----------------------------------------------------------------------------
544 (defun desktop-outvar (varspec)
545 "Output a setq statement for variable VAR to the desktop file.
546 The argument VARSPEC may be the variable name VAR (a symbol),
547 or a cons cell of the form (VAR . MAX-SIZE),
548 which means to truncate VAR's value to at most MAX-SIZE elements
549 \(if the value is a list) before saving the value."
552 (setq var
(car varspec
) size
(cdr varspec
))
556 (if (and (integerp size
)
559 (desktop-truncate (eval var
) size
))
563 (desktop-value-to-string (symbol-value var
))
566 ;; ----------------------------------------------------------------------------
567 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
568 "Return t if buffer should have its state saved in the desktop file.
569 FILENAME is the visited file name, BUFNAME is the buffer name, and
570 MODE is the major mode."
571 (let ((case-fold-search nil
))
572 (and (not (string-match desktop-buffers-not-to-save bufname
))
573 (not (memq mode desktop-modes-not-to-save
))
575 (not (string-match desktop-files-not-to-save filename
)))
576 (and (eq mode
'dired-mode
)
577 (with-current-buffer bufname
578 (not (string-match desktop-files-not-to-save
579 default-directory
))))
581 (with-current-buffer bufname desktop-save-buffer
))))))
583 ;; ----------------------------------------------------------------------------
584 (defun desktop-file-name (filename dirname
)
585 "Convert FILENAME to format specified in `desktop-file-name-format'.
586 DIRNAME must be the directory in which the desktop file will be saved."
589 ((eq desktop-file-name-format
'tilde
)
590 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
592 ((file-name-absolute-p relative-name
) relative-name
)
593 ((string= "./" relative-name
) "~/")
594 ((string= "." relative-name
) "~")
595 (t (concat "~/" relative-name
)))))
596 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
597 (t (expand-file-name filename
))))
599 ;; ----------------------------------------------------------------------------
600 (defun desktop-save (dirname)
601 "Save the desktop in a desktop file.
602 Parameter DIRNAME specifies where to save the desktop file.
603 See also `desktop-base-file-name'."
604 (interactive "DDirectory to save desktop file in: ")
605 (run-hooks 'desktop-save-hook
)
606 (setq dirname
(file-name-as-directory (expand-file-name dirname
)))
608 (let ((filename (expand-file-name desktop-base-file-name dirname
))
614 (desktop-file-name (buffer-file-name) dirname
)
620 #'(lambda (minor-mode)
623 (symbol-value minor-mode
)
624 (let ((special (assq minor-mode desktop-minor-mode-table
)))
625 (when (or special
(functionp minor-mode
))
628 (if special
(cadr special
) minor-mode
)
630 (mapcar #'car minor-mode-alist
))
633 (list (mark t
) mark-active
)
635 ;; Auxiliary information
636 (when (functionp desktop-save-buffer
)
637 (funcall desktop-save-buffer dirname
))
638 (let ((locals desktop-locals-to-save
)
639 (loclist (buffer-local-variables))
642 (let ((here (assq (car locals
) loclist
)))
644 (setq ll
(cons here ll
))
645 (when (member (car locals
) loclist
)
646 (setq ll
(cons (car locals
) ll
)))))
647 (setq locals
(cdr locals
)))
650 (eager desktop-restore-eager
)
651 (buf (get-buffer-create "*desktop*")))
656 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
658 ";; Created " (current-time-string) "\n"
659 ";; Desktop file format version " desktop-file-version
"\n"
660 ";; Emacs version " emacs-version
"\n\n"
661 ";; Global section:\n")
662 (mapc (function desktop-outvar
) desktop-globals-to-save
)
663 (if (memq 'kill-ring desktop-globals-to-save
)
665 "(setq kill-ring-yank-pointer (nthcdr "
666 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
669 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
671 (when (apply 'desktop-save-buffer-p l
)
673 (if (or (not (integerp eager
))
674 (unless (zerop eager
)
675 (setq eager
(1- eager
))
677 "desktop-create-buffer"
678 "desktop-append-buffer-args")
680 desktop-file-version
)
682 (insert "\n " (desktop-value-to-string e
)))
686 (setq default-directory dirname
)
687 (when (file-exists-p filename
) (delete-file filename
))
688 (let ((coding-system-for-write 'emacs-mule
))
689 (write-region (point-min) (point-max) filename nil
'nomessage
))))
690 (setq desktop-dirname dirname
))
692 ;; ----------------------------------------------------------------------------
693 (defun desktop-remove ()
694 "Delete desktop file in `desktop-dirname'.
695 This function also sets `desktop-dirname' to nil."
697 (when desktop-dirname
698 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname
)))
699 (setq desktop-dirname nil
)
700 (when (file-exists-p filename
)
701 (delete-file filename
)))))
703 (defvar desktop-buffer-args-list nil
704 "List of args for `desktop-create-buffer'.")
706 (defvar desktop-lazy-timer nil
)
708 ;; ----------------------------------------------------------------------------
710 (defun desktop-read (&optional dirname
)
711 "Read and process the desktop file in directory DIRNAME.
712 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
713 directories listed in `desktop-path'. If a desktop file is found, it
714 is processed and `desktop-after-read-hook' is run. If no desktop file
715 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
716 This function is a no-op when Emacs is running in batch mode.
717 It returns t if a desktop file was loaded, nil otherwise."
719 (unless noninteractive
720 (setq desktop-dirname
721 (file-name-as-directory
724 ;; If DIRNAME is specified, use it.
725 (and (< 0 (length dirname
)) dirname
)
726 ;; Otherwise search desktop file in desktop-path.
727 (let ((dirs desktop-path
))
732 (file-exists-p (expand-file-name desktop-base-file-name
(car dirs
)))))
733 (setq dirs
(cdr dirs
)))
734 (and dirs
(car dirs
)))
735 ;; If not found and `desktop-path' is non-nil, use its first element.
736 (and desktop-path
(car desktop-path
))
737 ;; Default: Home directory.
739 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))
740 ;; Desktop file found, process it.
741 (let ((desktop-first-buffer nil
)
742 (desktop-buffer-ok-count 0)
743 (desktop-buffer-fail-count 0))
744 (setq desktop-lazy-timer nil
)
745 ;; Evaluate desktop buffer.
746 (load (expand-file-name desktop-base-file-name desktop-dirname
) t t t
)
747 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
748 ;; We want buffers existing prior to evaluating the desktop (and not reused)
749 ;; to be placed at the end of the buffer list, so we move them here.
751 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
752 (switch-to-buffer (car (buffer-list)))
753 (run-hooks 'desktop-delay-hook
)
754 (setq desktop-delay-hook nil
)
755 (run-hooks 'desktop-after-read-hook
)
756 (message "Desktop: %d buffer%s restored%s%s."
757 desktop-buffer-ok-count
758 (if (= 1 desktop-buffer-ok-count
) "" "s")
759 (if (< 0 desktop-buffer-fail-count
)
760 (format ", %d failed to restore" desktop-buffer-fail-count
)
762 (if desktop-buffer-args-list
763 (format ", %d to restore lazily"
764 (length desktop-buffer-args-list
))
767 ;; No desktop file found.
769 (let ((default-directory desktop-dirname
))
770 (run-hooks 'desktop-no-desktop-file-hook
))
771 (message "No desktop file.")
774 ;; ----------------------------------------------------------------------------
775 ;; Maintained for backward compatibility
777 (defun desktop-load-default ()
778 "Load the `default' start-up library manually.
779 Also inhibit further loading of it."
780 (if (not inhibit-default-init
) ; safety check
783 (setq inhibit-default-init t
))))
784 (make-obsolete 'desktop-load-default
'desktop-save-mode
)
786 ;; ----------------------------------------------------------------------------
788 (defun desktop-change-dir (dirname)
789 "Change to desktop saved in DIRNAME.
790 Kill the desktop as specified by variables `desktop-save-mode' and
791 `desktop-save', then clear the desktop and load the desktop file in
793 (interactive "DChange to directory: ")
794 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
797 (desktop-read dirname
))
799 ;; ----------------------------------------------------------------------------
801 (defun desktop-save-in-desktop-dir ()
802 "Save the desktop in directory `desktop-dirname'."
805 (desktop-save desktop-dirname
)
806 (call-interactively 'desktop-save
))
807 (message "Desktop saved in %s" desktop-dirname
))
809 ;; ----------------------------------------------------------------------------
811 (defun desktop-revert ()
812 "Revert to the last loaded desktop."
814 (unless desktop-dirname
815 (error "Unknown desktop directory"))
816 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))
817 (error "No desktop file found"))
819 (desktop-read desktop-dirname
))
821 ;; ----------------------------------------------------------------------------
822 (defun desktop-restore-file-buffer (desktop-buffer-file-name
825 "Restore a file buffer."
826 (eval-when-compile ; Just to silence the byte compiler
827 (defvar desktop-buffer-major-mode
)
828 (defvar desktop-buffer-locals
))
829 (if desktop-buffer-file-name
830 (if (or (file-exists-p desktop-buffer-file-name
)
831 (let ((msg (format "Desktop: File \"%s\" no longer exists."
832 desktop-buffer-file-name
)))
833 (if desktop-missing-file-warning
834 (y-or-n-p (concat msg
" Re-create? "))
837 (let* ((auto-insert nil
) ; Disable auto insertion
838 (coding-system-for-read
839 (or coding-system-for-read
840 (cdr (assq 'buffer-file-coding-system
841 desktop-buffer-locals
))))
842 (buf (find-file-noselect desktop-buffer-file-name
)))
844 (switch-to-buffer buf
)
845 (error (pop-to-buffer buf
)))
846 (and (not (eq major-mode desktop-buffer-major-mode
))
847 (functionp desktop-buffer-major-mode
)
848 (funcall desktop-buffer-major-mode
))
852 ;; ----------------------------------------------------------------------------
853 ;; Create a buffer, load its file, set its mode, ...;
854 ;; called from Desktop file only.
856 (eval-when-compile ; Just to silence the byte compiler
857 (defvar desktop-first-buffer
) ;; Dynamically bound in `desktop-read'
860 (defun desktop-create-buffer
861 (desktop-file-version
862 desktop-buffer-file-name
864 desktop-buffer-major-mode
865 desktop-buffer-minor-modes
868 desktop-buffer-read-only
871 desktop-buffer-locals
)
872 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
874 (defvar desktop-buffer-ok-count
)
875 (defvar desktop-buffer-fail-count
))
876 ;; To make desktop files with relative file names possible, we cannot
877 ;; allow `default-directory' to change. Therefore we save current buffer.
879 (let ((buffer-list (buffer-list))
882 (funcall (or (cdr (assq desktop-buffer-major-mode
883 desktop-buffer-mode-handlers
))
884 'desktop-restore-file-buffer
)
885 desktop-buffer-file-name
889 (message "Desktop: Can't load buffer %s: %s"
891 (error-message-string err
))
892 (when desktop-missing-file-warning
(sit-for 1))
895 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
896 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
898 ;; Restore buffer list order with new buffer at end. Don't change
899 ;; the order for old desktop files (old desktop module behaviour).
900 (unless (< desktop-file-version
206)
901 (mapc 'bury-buffer buffer-list
)
902 (when result
(bury-buffer result
)))
904 (unless (or desktop-first-buffer
(< desktop-file-version
206))
905 (setq desktop-first-buffer result
))
907 (unless (equal (buffer-name) desktop-buffer-name
)
908 (rename-buffer desktop-buffer-name
))
910 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
912 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
915 (mapc #'(lambda (minor-mode)
916 (when (functionp minor-mode
) (funcall minor-mode
1)))
917 desktop-buffer-minor-modes
)))
918 ;; Even though point and mark are non-nil when written by `desktop-save'
919 ;; they may be modified by handlers wanting to set point or mark themselves.
920 (when desktop-buffer-point
923 ;; Evaluate point. Thus point can be something like '(search-forward ...
924 (eval desktop-buffer-point
)
925 (error (message "%s" (error-message-string err
)) 1))))
926 (when desktop-buffer-mark
927 (if (consp desktop-buffer-mark
)
929 (set-mark (car desktop-buffer-mark
))
930 (setq mark-active
(car (cdr desktop-buffer-mark
))))
931 (set-mark desktop-buffer-mark
)))
932 ;; Never override file system if the file really is read-only marked.
933 (if desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
934 (while desktop-buffer-locals
935 (let ((this (car desktop-buffer-locals
)))
937 ;; an entry of this form `(symbol . value)'
939 (make-local-variable (car this
))
940 (set (car this
) (cdr this
)))
941 ;; an entry of the form `symbol'
942 (make-local-variable this
)
944 (setq desktop-buffer-locals
(cdr desktop-buffer-locals
)))))))
946 ;; ----------------------------------------------------------------------------
947 ;; Backward compatibility -- update parameters to 205 standards.
948 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
949 desktop-buffer-major-mode
950 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
951 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
952 desktop-buffer-major-mode
(cdr mim
) pt mk ro
954 (list (cons 'truncate-lines tl
)
955 (cons 'fill-column fc
)
956 (cons 'case-fold-search cfs
)
957 (cons 'case-replace cr
)
958 (cons 'overwrite-mode
(car mim
)))))
960 (defun desktop-append-buffer-args (&rest args
)
961 "Append ARGS at end of `desktop-buffer-args-list'
962 ARGS must be an argument list for `desktop-create-buffer'."
963 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
964 (unless desktop-lazy-timer
965 (setq desktop-lazy-timer
966 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
968 (defun desktop-lazy-create-buffer ()
969 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
970 (when desktop-buffer-args-list
971 (let* ((remaining (length desktop-buffer-args-list
))
972 (args (pop desktop-buffer-args-list
))
973 (buffer-name (nth 2 args
))
974 (msg (format "Desktop lazily opening %s (%s remaining)..."
975 buffer-name remaining
)))
976 (when desktop-lazy-verbose
978 (let ((desktop-first-buffer nil
)
979 (desktop-buffer-ok-count 0)
980 (desktop-buffer-fail-count 0))
981 (apply 'desktop-create-buffer args
)
982 (run-hooks 'desktop-delay-hook
)
983 (setq desktop-delay-hook nil
)
984 (bury-buffer (get-buffer buffer-name
))
985 (when desktop-lazy-verbose
986 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
988 (defun desktop-idle-create-buffers ()
989 "Create buffers until the user does something, then stop.
990 If there are no buffers left to create, kill the timer."
992 (while (and repeat desktop-buffer-args-list
)
993 (save-window-excursion
994 (desktop-lazy-create-buffer))
995 (setq repeat
(sit-for 0.2))
996 (unless desktop-buffer-args-list
997 (cancel-timer desktop-lazy-timer
)
998 (setq desktop-lazy-timer nil
)
999 (message "Lazy desktop load complete")
1003 (defun desktop-lazy-complete ()
1004 "Run the desktop load to completion."
1006 (let ((desktop-lazy-verbose t
))
1007 (while desktop-buffer-args-list
1008 (save-window-excursion
1009 (desktop-lazy-create-buffer)))
1010 (message "Lazy desktop load complete")))
1012 (defun desktop-lazy-abort ()
1013 "Abort lazy loading of the desktop."
1015 (when desktop-lazy-timer
1016 (cancel-timer desktop-lazy-timer
)
1017 (setq desktop-lazy-timer nil
))
1018 (when desktop-buffer-args-list
1019 (setq desktop-buffer-args-list nil
)
1020 (when (interactive-p)
1021 (message "Lazy desktop load aborted"))))
1023 ;; ----------------------------------------------------------------------------
1024 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1025 ;; command line, we do the rest of what it takes to use desktop, but do it
1026 ;; after finishing loading the init file.
1027 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1028 ;; functions are processed after `after-init-hook'.
1032 (let ((key "--no-desktop"))
1033 (if (member key command-line-args
)
1034 (delete key command-line-args
)
1035 (when desktop-save-mode
(desktop-read))))))
1039 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1040 ;;; desktop.el ends here