1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 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.
56 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
57 ;; are supplied to handle special major and minor modes respectively.
58 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
59 ;; to restore a desktop buffer. Elements must have the form
61 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
63 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
64 ;; evaluates the desktop file. Buffers with a major mode not specified here,
65 ;; are restored by the default handler `desktop-restore-file-buffer'.
66 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
67 ;; non-standard minor modes. Elements must have the form
69 ;; (MINOR-MODE . RESTORE-FUNCTION).
71 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
72 ;; Minor modes not specified here, are restored by the standard minor mode
73 ;; function. If you write a module that defines a major or minor mode that
74 ;; needs a special handler, then place code like
76 ;; (defun foo-restore-desktop-buffer
78 ;; (add-to-list 'desktop-buffer-mode-handlers
79 ;; '(foo-mode . foo-restore-desktop-buffer))
83 ;; (defun bar-desktop-restore
85 ;; (add-to-list 'desktop-minor-mode-handlers
86 ;; '(bar-mode . bar-desktop-restore))
88 ;; in the module itself, and make shure that the mode function is
89 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
90 ;; `desktop-minor-mode-handlers' for more info.
94 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
95 ;; manual) are handled in the following way:
96 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
97 ;; saves as `desktop-minor-modes' the list of names of those variables in
98 ;; `minor-mode-alist' that have a non-nil value.
99 ;; When `desktop-create' restores the buffer, each of the symbols in
100 ;; `desktop-minor-modes' is called as function with parameter 1.
101 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
102 ;; are used to handle non-conventional minor modes. `desktop-save' uses
103 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
104 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
105 ;; variable name that is different form its function name, an entry
107 ;; (NAME RESTORE-FUNCTION)
109 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
110 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
111 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
112 ;; function different from the usual minor mode function.
113 ;; ---------------------------------------------------------------------------
115 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
116 ;; in your home directory is used for that. Saving global default values
117 ;; for buffers is an example of misuse.
119 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
120 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
121 ;; things you did not mean to keep. Use M-x desktop-clear RET.
123 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
124 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
125 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
126 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
127 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
128 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
129 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
130 ;; ---------------------------------------------------------------------------
133 ;; Save window configuration.
134 ;; Recognize more minor modes.
139 (defvar desktop-file-version
"206"
140 "Version number of desktop file format.
141 Written into the desktop file and used at desktop read to provide
142 backward compatibility.")
144 ;; ----------------------------------------------------------------------------
145 ;; USER OPTIONS -- settings you might want to play with.
146 ;; ----------------------------------------------------------------------------
148 (defgroup desktop nil
149 "Save status of Emacs when you exit."
153 (define-minor-mode desktop-save-mode
154 "Toggle desktop saving mode.
155 With numeric ARG, turn desktop saving on if ARG is positive, off
156 otherwise. See variable `desktop-save' for a description of when the
161 ;; Maintained for backward compatibility
162 (define-obsolete-variable-alias 'desktop-enable
163 'desktop-save-mode
"22.1")
165 (defcustom desktop-save
'ask-if-new
166 "*Specifies whether the desktop should be saved when it is killed.
167 A desktop is killed when the user changes desktop or quits Emacs.
171 ask-if-new -- ask if no desktop file exists, otherwise just save.
172 ask-if-exists -- ask if desktop file exists, otherwise don't save.
173 if-exists -- save if desktop file exists, otherwise don't save.
175 The desktop is never saved when `desktop-save-mode' is nil.
176 The variables `desktop-dirname' and `desktop-base-file-name'
177 determine where the desktop is saved."
179 (const :tag
"Always save" t
)
180 (const :tag
"Always ask" ask
)
181 (const :tag
"Ask if desktop file is new, else do save" ask-if-new
)
182 (const :tag
"Ask if desktop file exists, else don't save" ask-if-exists
)
183 (const :tag
"Save if desktop file exists, else don't" if-exists
)
184 (const :tag
"Never save" nil
))
188 (defcustom desktop-base-file-name
189 (convert-standard-filename ".emacs.desktop")
190 "Name of file for Emacs desktop, excluding the directory part."
193 (define-obsolete-variable-alias 'desktop-basefilename
194 'desktop-base-file-name
"22.1")
196 (defcustom desktop-path
'("." "~")
197 "List of directories to search for the desktop file.
198 The base name of the file is specified in `desktop-base-file-name'."
199 :type
'(repeat directory
)
203 (defcustom desktop-missing-file-warning nil
204 "If non-nil, offer to recreate the buffer of a deleted file.
205 Also pause for a moment to display message about errors signaled in
206 `desktop-buffer-mode-handlers'.
208 If nil, just print error messages in the message buffer."
213 (defcustom desktop-no-desktop-file-hook nil
214 "Normal hook run when `desktop-read' can't find a desktop file.
215 May be used to show a dired buffer."
220 (defcustom desktop-after-read-hook nil
221 "Normal hook run after a successful `desktop-read'.
222 May be used to show a buffer list."
227 (defcustom desktop-save-hook nil
228 "Normal hook run before the desktop is saved in a desktop file.
229 This is useful for truncating history lists, for example."
233 (defcustom desktop-globals-to-save
234 '(desktop-missing-file-warning
240 "List of global variables saved by `desktop-save'.
241 An element may be variable name (a symbol) or a cons cell of the form
242 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
243 MAX-SIZE elements (if the value is a list) before saving the value.
244 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
245 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
248 (defcustom desktop-globals-to-clear
250 kill-ring-yank-pointer
252 search-ring-yank-pointer
254 regexp-search-ring-yank-pointer
)
255 "List of global variables that `desktop-clear' will clear.
256 An element may be variable name (a symbol) or a cons cell of the form
257 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
258 to the value obtained by evaluating FORM."
259 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
263 (defcustom desktop-clear-preserve-buffers
264 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*")
265 "*List of buffers that `desktop-clear' should not delete.
266 Each element is a regular expression. Buffers with a name matched by any of
267 these won't be deleted."
268 :type
'(repeat string
)
272 (defcustom desktop-locals-to-save
273 '(desktop-locals-to-save ; Itself! Think it over.
279 change-log-default-name
283 buffer-file-coding-system
285 indicate-buffer-boundaries
287 show-trailing-whitespace
)
288 "List of local variables to save for each buffer.
289 The variables are saved only when they really are local. Conventional minor
290 modes are restored automatically; they should not be listed here."
291 :type
'(repeat symbol
)
294 ;; We skip .log files because they are normally temporary.
295 ;; (ftp) files because they require passwords and whatnot.
296 (defcustom desktop-buffers-not-to-save
297 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\)$"
298 "Regexp identifying buffers that are to be excluded from saving."
302 ;; Skip tramp and ange-ftp files
303 (defcustom desktop-files-not-to-save
305 "Regexp identifying files whose buffers are to be excluded from saving."
309 ;; We skip TAGS files to save time (tags-file-name is saved instead).
310 (defcustom desktop-modes-not-to-save
312 "List of major modes whose buffers should not be saved."
313 :type
'(repeat symbol
)
316 (defcustom desktop-file-name-format
'absolute
317 "*Format in which desktop file names should be saved.
319 absolute -- Absolute file name.
320 tilde -- Relative to ~.
321 local -- Relative to directory of desktop file."
322 :type
'(choice (const absolute
) (const tilde
) (const local
))
326 (defcustom desktop-restore-eager t
327 "Number of buffers to restore immediately.
328 Remaining buffers are restored lazily (when Emacs is idle).
329 If value is t, all buffers are restored immediately."
330 :type
'(choice (const t
) integer
)
334 (defcustom desktop-lazy-verbose t
335 "Verbose reporting of lazily created buffers."
340 (defcustom desktop-lazy-idle-delay
5
341 "Idle delay before starting to create buffers.
342 See `desktop-restore-eager'."
348 (defvar desktop-save-buffer nil
349 "When non-nil, save buffer status in desktop file.
350 This variable becomes buffer local when set.
352 If the value is a function, it is called by `desktop-save' with argument
353 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
354 file along with the state of the buffer for which it was called.
356 When file names are returned, they should be formatted using the call
357 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
359 Later, when `desktop-read' evaluates the desktop file, auxiliary information
360 is passed as the argument DESKTOP-BUFFER-MISC to functions in
361 `desktop-buffer-mode-handlers'.")
362 (make-variable-buffer-local 'desktop-save-buffer
)
363 (make-obsolete-variable 'desktop-buffer-modes-to-save
364 'desktop-save-buffer
"22.1")
365 (make-obsolete-variable 'desktop-buffer-misc-functions
366 'desktop-save-buffer
"22.1")
369 (defvar desktop-buffer-mode-handlers
371 "Alist of major mode specific functions to restore a desktop buffer.
372 Functions listed are called by `desktop-create-buffer' when `desktop-read'
373 evaluates the desktop file. List elements must have the form
375 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
377 Buffers with a major mode not specified here, are restored by the default
378 handler `desktop-restore-file-buffer'.
380 Handlers are called with argument list
382 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
384 Furthermore, they may use the following variables:
387 desktop-buffer-major-mode
388 desktop-buffer-minor-modes
391 desktop-buffer-read-only
392 desktop-buffer-locals
394 If a handler returns a buffer, then the saved mode settings
395 and variable values for that buffer are copied into it.
397 Modules that define a major mode that needs a special handler should contain
400 (defun foo-restore-desktop-buffer
402 (add-to-list 'desktop-buffer-mode-handlers
403 '(foo-mode . foo-restore-desktop-buffer))
405 Furthermore the major mode function must be autoloaded.")
408 (put 'desktop-buffer-mode-handlers
'risky-local-variable t
)
409 (make-obsolete-variable 'desktop-buffer-handlers
410 'desktop-buffer-mode-handlers
"22.1")
412 (defcustom desktop-minor-mode-table
413 '((auto-fill-function auto-fill-mode
)
416 "Table mapping minor mode variables to minor mode functions.
417 Each entry has the form (NAME RESTORE-FUNCTION).
418 NAME is the name of the buffer-local variable indicating that the minor
419 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
420 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
421 Only minor modes for which the name of the buffer-local variable
422 and the name of the minor mode function are different have to be added to
423 this table. See also `desktop-minor-mode-handlers'."
428 (defvar desktop-minor-mode-handlers
430 "Alist of functions to restore non-standard minor modes.
431 Functions are called by `desktop-create-buffer' to restore minor modes.
432 List elements must have the form
434 (MINOR-MODE . RESTORE-FUNCTION).
436 Minor modes not specified here, are restored by the standard minor mode
439 Handlers are called with argument list
441 (DESKTOP-BUFFER-LOCALS)
443 Furthermore, they may use the following variables:
446 desktop-buffer-file-name
448 desktop-buffer-major-mode
449 desktop-buffer-minor-modes
452 desktop-buffer-read-only
455 When a handler is called, the buffer has been created and the major mode has
456 been set, but local variables listed in desktop-buffer-locals has not yet been
459 Modules that define a minor mode that needs a special handler should contain
462 (defun foo-desktop-restore
464 (add-to-list 'desktop-minor-mode-handlers
465 '(foo-mode . foo-desktop-restore))
467 Furthermore the minor mode function must be autoloaded.
469 See also `desktop-minor-mode-table'.")
472 (put 'desktop-minor-mode-handlers
'risky-local-variable t
)
474 ;; ----------------------------------------------------------------------------
475 (defvar desktop-dirname nil
476 "The directory in which the desktop file should be saved.")
478 (defconst desktop-header
479 ";; --------------------------------------------------------------------------
480 ;; Desktop File for Emacs
481 ;; --------------------------------------------------------------------------
482 " "*Header to place in Desktop file.")
484 (defvar desktop-delay-hook nil
485 "Hooks run after all buffers are loaded; intended for internal use.")
487 ;; ----------------------------------------------------------------------------
488 (defun desktop-truncate (list n
)
489 "Truncate LIST to at most N elements destructively."
490 (let ((here (nthcdr (1- n
) list
)))
494 ;; ----------------------------------------------------------------------------
495 (defun desktop-clear ()
497 This kills all buffers except for internal ones and those with names matched by
498 a regular expression in the list `desktop-clear-preserve-buffers'.
499 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
502 (dolist (var desktop-globals-to-clear
)
504 (eval `(setq-default ,var nil
))
505 (eval `(setq-default ,(car var
) ,(cdr var
)))))
506 (let ((buffers (buffer-list))
507 (preserve-regexp (concat "^\\("
508 (mapconcat (lambda (regexp)
509 (concat "\\(" regexp
"\\)"))
510 desktop-clear-preserve-buffers
514 (let ((bufname (buffer-name (car buffers
))))
517 (string-match preserve-regexp bufname
)
518 ;; Don't kill buffers made for internal purposes.
519 (and (not (equal bufname
"")) (eq (aref bufname
0) ?\s
))
520 (kill-buffer (car buffers
))))
521 (setq buffers
(cdr buffers
))))
522 (delete-other-windows))
524 ;; ----------------------------------------------------------------------------
525 (add-hook 'kill-emacs-hook
'desktop-kill
)
527 (defun desktop-kill ()
528 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
529 If the desktop should be saved and `desktop-dirname'
530 is nil, ask the user where to save the desktop."
534 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))))
537 (and exists
(memq desktop-save
'(ask-if-new if-exists
)))
540 (memq desktop-save
'(ask ask-if-new
))
541 (and exists
(eq desktop-save
'ask-if-exists
)))
542 (y-or-n-p "Save desktop? ")))))
543 (unless desktop-dirname
544 (setq desktop-dirname
545 (file-name-as-directory
548 (lambda (dir) (interactive "DDirectory for desktop file: ") dir
))))))
550 (desktop-save desktop-dirname
)
552 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
553 (signal (car err
) (cdr err
)))))))
555 ;; ----------------------------------------------------------------------------
556 (defun desktop-list* (&rest args
)
557 (if (null (cdr args
))
559 (setq args
(nreverse args
))
560 (let ((value (cons (nth 1 args
) (car args
))))
561 (setq args
(cdr (cdr args
)))
563 (setq value
(cons (car args
) value
))
564 (setq args
(cdr args
)))
567 ;; ----------------------------------------------------------------------------
568 (defun desktop-internal-v2s (value)
569 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
570 TXT is a string that when read and evaluated yields value.
571 QUOTE may be `may' (value may be quoted),
572 `must' (values must be quoted), or nil (value may not be quoted)."
574 ((or (numberp value
) (null value
) (eq t value
) (keywordp value
))
575 (cons 'may
(prin1-to-string value
)))
577 (let ((copy (copy-sequence value
)))
578 (set-text-properties 0 (length copy
) nil copy
)
579 ;; Get rid of text properties because we cannot read them
580 (cons 'may
(prin1-to-string copy
))))
582 (cons 'must
(prin1-to-string value
)))
587 (let ((res (desktop-internal-v2s el
)))
593 (cons nil
(concat "(vector "
594 (mapconcat (lambda (el)
595 (if (eq (car el
) 'must
)
596 (concat "'" (cdr el
))
601 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
608 (let ((q.txt
(desktop-internal-v2s (car p
))))
609 (or anynil
(setq anynil
(null (car q.txt
))))
610 (setq newlist
(cons q.txt newlist
)))
613 (let ((last (desktop-internal-v2s p
))
615 (or anynil
(setq anynil
(null (car last
))))
617 (setq newlist
(cons '(must .
".") newlist
)))
619 (setq newlist
(cons last newlist
))))
620 (setq newlist
(nreverse newlist
))
623 (concat (if use-list
* "(desktop-list* " "(list ")
624 (mapconcat (lambda (el)
625 (if (eq (car el
) 'must
)
626 (concat "'" (cdr el
))
632 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
634 (cons nil
(concat "(symbol-function '"
635 (substring (prin1-to-string value
) 7 -
1)
638 (let ((pos (prin1-to-string (marker-position value
)))
639 (buf (prin1-to-string (buffer-name (marker-buffer value
)))))
640 (cons nil
(concat "(let ((mk (make-marker)))"
641 " (add-hook 'desktop-delay-hook"
642 " (list 'lambda '() (list 'set-marker mk "
643 pos
" (get-buffer " buf
")))) mk)"))))
645 (cons 'may
"\"Unprintable entity\""))))
647 ;; ----------------------------------------------------------------------------
648 (defun desktop-value-to-string (value)
649 "Convert VALUE to a string that when read evaluates to the same value.
650 Not all types of values are supported."
651 (let* ((print-escape-newlines t
)
652 (float-output-format nil
)
653 (quote.txt
(desktop-internal-v2s value
))
654 (quote (car quote.txt
))
655 (txt (cdr quote.txt
)))
660 ;; ----------------------------------------------------------------------------
661 (defun desktop-outvar (varspec)
662 "Output a setq statement for variable VAR to the desktop file.
663 The argument VARSPEC may be the variable name VAR (a symbol),
664 or a cons cell of the form (VAR . MAX-SIZE),
665 which means to truncate VAR's value to at most MAX-SIZE elements
666 \(if the value is a list) before saving the value."
669 (setq var
(car varspec
) size
(cdr varspec
))
673 (if (and (integerp size
)
676 (desktop-truncate (eval var
) size
))
680 (desktop-value-to-string (symbol-value var
))
683 ;; ----------------------------------------------------------------------------
684 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
685 "Return t if buffer should have its state saved in the desktop file.
686 FILENAME is the visited file name, BUFNAME is the buffer name, and
687 MODE is the major mode.
688 \n\(fn FILENAME BUFNAME MODE)"
689 (let ((case-fold-search nil
))
690 (and (not (string-match desktop-buffers-not-to-save bufname
))
691 (not (memq mode desktop-modes-not-to-save
))
693 (not (string-match desktop-files-not-to-save filename
)))
694 (and (eq mode
'dired-mode
)
695 (with-current-buffer bufname
696 (not (string-match desktop-files-not-to-save
697 default-directory
))))
699 (with-current-buffer bufname desktop-save-buffer
))))))
701 ;; ----------------------------------------------------------------------------
702 (defun desktop-file-name (filename dirname
)
703 "Convert FILENAME to format specified in `desktop-file-name-format'.
704 DIRNAME must be the directory in which the desktop file will be saved."
707 ((eq desktop-file-name-format
'tilde
)
708 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
710 ((file-name-absolute-p relative-name
) relative-name
)
711 ((string= "./" relative-name
) "~/")
712 ((string= "." relative-name
) "~")
713 (t (concat "~/" relative-name
)))))
714 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
715 (t (expand-file-name filename
))))
717 ;; ----------------------------------------------------------------------------
718 (defun desktop-save (dirname)
719 "Save the desktop in a desktop file.
720 Parameter DIRNAME specifies where to save the desktop file.
721 See also `desktop-base-file-name'."
722 (interactive "DDirectory to save desktop file in: ")
723 (run-hooks 'desktop-save-hook
)
724 (setq dirname
(file-name-as-directory (expand-file-name dirname
)))
726 (let ((filename (expand-file-name desktop-base-file-name dirname
))
732 (desktop-file-name (buffer-file-name) dirname
)
738 #'(lambda (minor-mode)
741 (symbol-value minor-mode
)
742 (let* ((special (assq minor-mode desktop-minor-mode-table
))
743 (value (cond (special (cadr special
))
744 ((functionp minor-mode
) minor-mode
))))
745 (when value
(add-to-list 'ret value
)))))
746 (mapcar #'car minor-mode-alist
))
749 (list (mark t
) mark-active
)
751 ;; Auxiliary information
752 (when (functionp desktop-save-buffer
)
753 (funcall desktop-save-buffer dirname
))
754 (let ((locals desktop-locals-to-save
)
755 (loclist (buffer-local-variables))
758 (let ((here (assq (car locals
) loclist
)))
760 (setq ll
(cons here ll
))
761 (when (member (car locals
) loclist
)
762 (setq ll
(cons (car locals
) ll
)))))
763 (setq locals
(cdr locals
)))
766 (eager desktop-restore-eager
))
769 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
771 ";; Created " (current-time-string) "\n"
772 ";; Desktop file format version " desktop-file-version
"\n"
773 ";; Emacs version " emacs-version
"\n\n"
774 ";; Global section:\n")
775 (mapc (function desktop-outvar
) desktop-globals-to-save
)
776 (if (memq 'kill-ring desktop-globals-to-save
)
778 "(setq kill-ring-yank-pointer (nthcdr "
779 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
782 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
784 (when (apply 'desktop-save-buffer-p l
)
786 (if (or (not (integerp eager
))
787 (unless (zerop eager
)
788 (setq eager
(1- eager
))
790 "desktop-create-buffer"
791 "desktop-append-buffer-args")
793 desktop-file-version
)
795 (insert "\n " (desktop-value-to-string e
)))
799 (setq default-directory dirname
)
800 (let ((coding-system-for-write 'emacs-mule
))
801 (write-region (point-min) (point-max) filename nil
'nomessage
)))))
802 (setq desktop-dirname dirname
))
804 ;; ----------------------------------------------------------------------------
805 (defun desktop-remove ()
806 "Delete desktop file in `desktop-dirname'.
807 This function also sets `desktop-dirname' to nil."
809 (when desktop-dirname
810 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname
)))
811 (setq desktop-dirname nil
)
812 (when (file-exists-p filename
)
813 (delete-file filename
)))))
815 (defvar desktop-buffer-args-list nil
816 "List of args for `desktop-create-buffer'.")
818 (defvar desktop-lazy-timer nil
)
820 ;; ----------------------------------------------------------------------------
822 (defun desktop-read (&optional dirname
)
823 "Read and process the desktop file in directory DIRNAME.
824 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
825 directories listed in `desktop-path'. If a desktop file is found, it
826 is processed and `desktop-after-read-hook' is run. If no desktop file
827 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
828 This function is a no-op when Emacs is running in batch mode.
829 It returns t if a desktop file was loaded, nil otherwise."
831 (unless noninteractive
832 (setq desktop-dirname
833 (file-name-as-directory
836 ;; If DIRNAME is specified, use it.
837 (and (< 0 (length dirname
)) dirname
)
838 ;; Otherwise search desktop file in desktop-path.
839 (let ((dirs desktop-path
))
844 (file-exists-p (expand-file-name desktop-base-file-name
(car dirs
)))))
845 (setq dirs
(cdr dirs
)))
846 (and dirs
(car dirs
)))
847 ;; If not found and `desktop-path' is non-nil, use its first element.
848 (and desktop-path
(car desktop-path
))
849 ;; Default: Home directory.
851 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))
852 ;; Desktop file found, process it.
853 (let ((desktop-first-buffer nil
)
854 (desktop-buffer-ok-count 0)
855 (desktop-buffer-fail-count 0))
856 (setq desktop-lazy-timer nil
)
857 ;; Evaluate desktop buffer.
858 (load (expand-file-name desktop-base-file-name desktop-dirname
) t t t
)
859 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
860 ;; We want buffers existing prior to evaluating the desktop (and not reused)
861 ;; to be placed at the end of the buffer list, so we move them here.
863 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
864 (switch-to-buffer (car (buffer-list)))
865 (run-hooks 'desktop-delay-hook
)
866 (setq desktop-delay-hook nil
)
867 (run-hooks 'desktop-after-read-hook
)
868 (message "Desktop: %d buffer%s restored%s%s."
869 desktop-buffer-ok-count
870 (if (= 1 desktop-buffer-ok-count
) "" "s")
871 (if (< 0 desktop-buffer-fail-count
)
872 (format ", %d failed to restore" desktop-buffer-fail-count
)
874 (if desktop-buffer-args-list
875 (format ", %d to restore lazily"
876 (length desktop-buffer-args-list
))
879 ;; No desktop file found.
881 (let ((default-directory desktop-dirname
))
882 (run-hooks 'desktop-no-desktop-file-hook
))
883 (message "No desktop file.")
886 ;; ----------------------------------------------------------------------------
887 ;; Maintained for backward compatibility
889 (defun desktop-load-default ()
890 "Load the `default' start-up library manually.
891 Also inhibit further loading of it."
892 (unless inhibit-default-init
; safety check
894 (setq inhibit-default-init t
)))
895 (make-obsolete 'desktop-load-default
896 'desktop-save-mode
"22.1")
898 ;; ----------------------------------------------------------------------------
900 (defun desktop-change-dir (dirname)
901 "Change to desktop saved in DIRNAME.
902 Kill the desktop as specified by variables `desktop-save-mode' and
903 `desktop-save', then clear the desktop and load the desktop file in
905 (interactive "DChange to directory: ")
906 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
909 (desktop-read dirname
))
911 ;; ----------------------------------------------------------------------------
913 (defun desktop-save-in-desktop-dir ()
914 "Save the desktop in directory `desktop-dirname'."
917 (desktop-save desktop-dirname
)
918 (call-interactively 'desktop-save
))
919 (message "Desktop saved in %s" desktop-dirname
))
921 ;; ----------------------------------------------------------------------------
923 (defun desktop-revert ()
924 "Revert to the last loaded desktop."
926 (unless desktop-dirname
927 (error "Unknown desktop directory"))
928 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))
929 (error "No desktop file found"))
931 (desktop-read desktop-dirname
))
933 ;; ----------------------------------------------------------------------------
934 (defun desktop-restore-file-buffer (desktop-buffer-file-name
937 "Restore a file buffer."
938 (eval-when-compile ; Just to silence the byte compiler
939 (defvar desktop-buffer-major-mode
)
940 (defvar desktop-buffer-locals
))
941 (if desktop-buffer-file-name
942 (if (or (file-exists-p desktop-buffer-file-name
)
943 (let ((msg (format "Desktop: File \"%s\" no longer exists."
944 desktop-buffer-file-name
)))
945 (if desktop-missing-file-warning
946 (y-or-n-p (concat msg
" Re-create buffer? "))
949 (let* ((auto-insert nil
) ; Disable auto insertion
950 (coding-system-for-read
951 (or coding-system-for-read
952 (cdr (assq 'buffer-file-coding-system
953 desktop-buffer-locals
))))
954 (buf (find-file-noselect desktop-buffer-file-name
)))
956 (switch-to-buffer buf
)
957 (error (pop-to-buffer buf
)))
958 (and (not (eq major-mode desktop-buffer-major-mode
))
959 (functionp desktop-buffer-major-mode
)
960 (funcall desktop-buffer-major-mode
))
964 (defun desktop-load-file (function)
965 "Load the file where auto loaded FUNCTION is defined."
967 (let ((fcell (and (fboundp function
) (symbol-function function
))))
968 (when (and (listp fcell
)
969 (eq 'autoload
(car fcell
)))
970 (load (cadr fcell
))))))
972 ;; ----------------------------------------------------------------------------
973 ;; Create a buffer, load its file, set its mode, ...;
974 ;; called from Desktop file only.
976 ;; Just to silence the byte compiler.
978 (defvar desktop-first-buffer
)) ; Dynamically bound in `desktop-read'
980 (defun desktop-create-buffer
981 (desktop-file-version
982 desktop-buffer-file-name
984 desktop-buffer-major-mode
985 desktop-buffer-minor-modes
988 desktop-buffer-read-only
991 desktop-buffer-locals
)
992 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
994 (defvar desktop-buffer-ok-count
)
995 (defvar desktop-buffer-fail-count
))
996 ;; To make desktop files with relative file names possible, we cannot
997 ;; allow `default-directory' to change. Therefore we save current buffer.
999 ;; Give major mode module a chance to add a handler.
1000 (desktop-load-file desktop-buffer-major-mode
)
1001 (let ((buffer-list (buffer-list))
1004 (funcall (or (cdr (assq desktop-buffer-major-mode
1005 desktop-buffer-mode-handlers
))
1006 'desktop-restore-file-buffer
)
1007 desktop-buffer-file-name
1009 desktop-buffer-misc
)
1011 (message "Desktop: Can't load buffer %s: %s"
1013 (error-message-string err
))
1014 (when desktop-missing-file-warning
(sit-for 1))
1016 (if (bufferp result
)
1017 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
1018 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
1020 ;; Restore buffer list order with new buffer at end. Don't change
1021 ;; the order for old desktop files (old desktop module behaviour).
1022 (unless (< desktop-file-version
206)
1023 (mapc 'bury-buffer buffer-list
)
1024 (when result
(bury-buffer result
)))
1026 (unless (or desktop-first-buffer
(< desktop-file-version
206))
1027 (setq desktop-first-buffer result
))
1029 (unless (equal (buffer-name) desktop-buffer-name
)
1030 (rename-buffer desktop-buffer-name
))
1032 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
1034 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
1037 (mapc #'(lambda (minor-mode)
1038 ;; Give minor mode module a chance to add a handler.
1039 (desktop-load-file minor-mode
)
1040 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers
))))
1042 (funcall handler desktop-buffer-locals
)
1043 (when (functionp minor-mode
)
1044 (funcall minor-mode
1)))))
1045 desktop-buffer-minor-modes
)))
1046 ;; Even though point and mark are non-nil when written by `desktop-save',
1047 ;; they may be modified by handlers wanting to set point or mark themselves.
1048 (when desktop-buffer-point
1051 ;; Evaluate point. Thus point can be something like '(search-forward ...
1052 (eval desktop-buffer-point
)
1053 (error (message "%s" (error-message-string err
)) 1))))
1054 (when desktop-buffer-mark
1055 (if (consp desktop-buffer-mark
)
1057 (set-mark (car desktop-buffer-mark
))
1058 (setq mark-active
(car (cdr desktop-buffer-mark
))))
1059 (set-mark desktop-buffer-mark
)))
1060 ;; Never override file system if the file really is read-only marked.
1061 (if desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
1062 (while desktop-buffer-locals
1063 (let ((this (car desktop-buffer-locals
)))
1065 ;; an entry of this form `(symbol . value)'
1067 (make-local-variable (car this
))
1068 (set (car this
) (cdr this
)))
1069 ;; an entry of the form `symbol'
1070 (make-local-variable this
)
1072 (setq desktop-buffer-locals
(cdr desktop-buffer-locals
)))))))
1074 ;; ----------------------------------------------------------------------------
1075 ;; Backward compatibility -- update parameters to 205 standards.
1076 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1077 desktop-buffer-major-mode
1078 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
1079 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1080 desktop-buffer-major-mode
(cdr mim
) pt mk ro
1082 (list (cons 'truncate-lines tl
)
1083 (cons 'fill-column fc
)
1084 (cons 'case-fold-search cfs
)
1085 (cons 'case-replace cr
)
1086 (cons 'overwrite-mode
(car mim
)))))
1088 (defun desktop-append-buffer-args (&rest args
)
1089 "Append ARGS at end of `desktop-buffer-args-list'.
1090 ARGS must be an argument list for `desktop-create-buffer'."
1091 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
1092 (unless desktop-lazy-timer
1093 (setq desktop-lazy-timer
1094 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
1096 (defun desktop-lazy-create-buffer ()
1097 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1098 (when desktop-buffer-args-list
1099 (let* ((remaining (length desktop-buffer-args-list
))
1100 (args (pop desktop-buffer-args-list
))
1101 (buffer-name (nth 2 args
))
1102 (msg (format "Desktop lazily opening %s (%s remaining)..."
1103 buffer-name remaining
)))
1104 (when desktop-lazy-verbose
1106 (let ((desktop-first-buffer nil
)
1107 (desktop-buffer-ok-count 0)
1108 (desktop-buffer-fail-count 0))
1109 (apply 'desktop-create-buffer args
)
1110 (run-hooks 'desktop-delay-hook
)
1111 (setq desktop-delay-hook nil
)
1112 (bury-buffer (get-buffer buffer-name
))
1113 (when desktop-lazy-verbose
1114 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
1116 (defun desktop-idle-create-buffers ()
1117 "Create buffers until the user does something, then stop.
1118 If there are no buffers left to create, kill the timer."
1120 (while (and repeat desktop-buffer-args-list
)
1121 (save-window-excursion
1122 (desktop-lazy-create-buffer))
1123 (setq repeat
(sit-for 0.2))
1124 (unless desktop-buffer-args-list
1125 (cancel-timer desktop-lazy-timer
)
1126 (setq desktop-lazy-timer nil
)
1127 (message "Lazy desktop load complete")
1131 (defun desktop-lazy-complete ()
1132 "Run the desktop load to completion."
1134 (let ((desktop-lazy-verbose t
))
1135 (while desktop-buffer-args-list
1136 (save-window-excursion
1137 (desktop-lazy-create-buffer)))
1138 (message "Lazy desktop load complete")))
1140 (defun desktop-lazy-abort ()
1141 "Abort lazy loading of the desktop."
1143 (when desktop-lazy-timer
1144 (cancel-timer desktop-lazy-timer
)
1145 (setq desktop-lazy-timer nil
))
1146 (when desktop-buffer-args-list
1147 (setq desktop-buffer-args-list nil
)
1148 (when (interactive-p)
1149 (message "Lazy desktop load aborted"))))
1151 ;; ----------------------------------------------------------------------------
1152 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1153 ;; command line, we do the rest of what it takes to use desktop, but do it
1154 ;; after finishing loading the init file.
1155 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1156 ;; functions are processed after `after-init-hook'.
1160 (let ((key "--no-desktop"))
1161 (when (member key command-line-args
)
1162 (setq command-line-args
(delete key command-line-args
))
1163 (setq desktop-save-mode nil
)))
1164 (when desktop-save-mode
(desktop-read))))
1168 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1169 ;;; desktop.el ends here