1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000 Free Software Foundation, Inc.
5 ;; Author: Morten Welinder <terra@diku.dk>
6 ;; Keywords: convenience
7 ;; Favourite-brand-of-beer: None, I hate beer.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Save the Desktop, i.e.,
29 ;; - some global variables
30 ;; - the list of buffers with associated files. For each buffer also
32 ;; - the default directory
34 ;; - the mark & mark-active
36 ;; - some local variables
38 ;; To use this, first put these two lines in the bottom of your .emacs
39 ;; file (the later the better):
41 ;; (desktop-load-default)
44 ;; Between these two lines you may wish to add something that updates the
45 ;; variables `desktop-globals-to-save' and/or `desktop-locals-to-save'. If
46 ;; for instance you want to save the local variable `foobar' for every buffer
47 ;; in which it is local, you could add the line
49 ;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
51 ;; To avoid saving excessive amounts of data you may also wish to add
52 ;; something like the following
54 ;; (add-hook 'kill-emacs-hook
56 ;; (desktop-truncate search-ring 3)
57 ;; (desktop-truncate regexp-search-ring 3)))
59 ;; which will make sure that no more than three search items are saved. You
60 ;; must place this line *after* the `(desktop-load-default)' line. See also
61 ;; the variable `desktop-save-hook'.
63 ;; Start Emacs in the root directory of your "project". The desktop saver
64 ;; is inactive by default. You activate it by M-x desktop-save RET. When
65 ;; you exit the next time the above data will be saved. This ensures that
66 ;; all the files you were editing will be reloaded the next time you start
67 ;; Emacs from the same directory and that points will be set where you
68 ;; left them. If you save a desktop file in your home directory it will
69 ;; act as a default desktop when you start Emacs from a directory that
70 ;; doesn't have its own. I never do this, but you may want to.
72 ;; Some words on minor modes: Most minor modes are controlled by
73 ;; buffer-local variables, which have a standard save / restore
74 ;; mechanism. To handle all minor modes, we take the following
75 ;; approach: (1) check whether the variable name from
76 ;; `minor-mode-alist' is also a function; and (2) use translation
77 ;; table `desktop-minor-mode-table' in the case where the two names
80 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
81 ;; in your home directory is used for that. Saving global default values
82 ;; for buffers is an example of misuse.
84 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
85 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
86 ;; things you did not mean to keep. Use M-x desktop-clear RET.
88 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
89 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
90 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
91 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
92 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
93 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
94 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
95 ;; ---------------------------------------------------------------------------
98 ;; Save window configuration.
99 ;; Recognize more minor modes.
101 ;; Start-up with buffer-menu???
105 ;; Make the compilation more silent
107 ;; We use functions from these modules
108 ;; We can't (require 'mh-e) since that wants to load something.
109 (mapcar 'require
'(info dired reporter
)))
110 ;; ----------------------------------------------------------------------------
111 ;; USER OPTIONS -- settings you might want to play with.
112 ;; ----------------------------------------------------------------------------
114 (defgroup desktop nil
115 "Save status of Emacs when you exit."
118 (defcustom desktop-enable nil
119 "*Non-nil enable Desktop to save the state of Emacs when you exit."
123 :initialize
'custom-initialize-default
126 (defcustom desktop-basefilename
127 (convert-standard-filename ".emacs.desktop")
128 "File for Emacs desktop, not including the directory name."
132 (defcustom desktop-missing-file-warning nil
133 "*If non-nil then desktop warns when a file no longer exists.
134 Otherwise it simply ignores that file."
138 (defvar desktop-globals-to-save
139 (list 'desktop-missing-file-warning
140 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
147 ;; 'desktop-globals-to-save ; Itself!
149 "List of global variables to save when killing Emacs.
150 An element may be variable name (a symbol)
151 or a cons cell of the form (VAR . MAX-SIZE),
152 which means to truncate VAR's value to at most MAX-SIZE elements
153 \(if the value is a list) before saving the value.")
155 (defvar desktop-locals-to-save
156 (list 'desktop-locals-to-save
; Itself! Think it over.
162 'change-log-default-name
165 "List of local variables to save for each buffer.
166 The variables are saved only when they really are local.")
167 (make-variable-buffer-local 'desktop-locals-to-save
)
169 ;; We skip .log files because they are normally temporary.
170 ;; (ftp) files because they require passwords and whatnot.
171 ;; TAGS files to save time (tags-file-name is saved instead).
172 (defcustom desktop-buffers-not-to-save
173 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
174 "Regexp identifying buffers that are to be excluded from saving."
178 ;; Skip ange-ftp files
179 (defcustom desktop-files-not-to-save
181 "Regexp identifying files whose buffers are to be excluded from saving."
185 (defcustom desktop-buffer-modes-to-save
186 '(Info-mode rmail-mode
)
187 "If a buffer is of one of these major modes, save the buffer name.
188 It is up to the functions in `desktop-buffer-handlers' to decide
189 whether the buffer should be recreated or not, and how."
190 :type
'(repeat symbol
)
193 (defcustom desktop-modes-not-to-save nil
194 "List of major modes whose buffers should not be saved."
195 :type
'(repeat symbol
)
198 (defcustom desktop-buffer-major-mode nil
199 "When desktop creates a buffer, this holds the desired Major mode."
203 (defcustom desktop-buffer-file-name nil
204 "When desktop creates a buffer, this holds the file name to visit."
205 :type
'(choice file
(const nil
))
208 (defcustom desktop-buffer-name nil
209 "When desktop creates a buffer, this holds the desired buffer name."
210 :type
'(choice string
(const nil
))
213 (defvar desktop-buffer-misc nil
214 "When desktop creates a buffer, this holds a list of misc info.
215 It is used by the `desktop-buffer-handlers' functions.")
217 (defcustom desktop-buffer-misc-functions
218 '(desktop-buffer-info-misc-data
219 desktop-buffer-dired-misc-data
)
220 "*Functions used to determine auxiliary information for a buffer.
221 These functions are called in order, with no arguments. If a function
222 returns non-nil, its value is saved along with the desktop buffer for
223 which it was called; no further functions will be called.
225 Later, when desktop.el restores the buffers it has saved, each of the
226 `desktop-buffer-handlers' functions will have access to a buffer local
227 variable, named `desktop-buffer-misc', whose value is what the
228 \"misc\" function returned previously."
229 :type
'(repeat function
)
232 (defcustom desktop-buffer-handlers
233 '(desktop-buffer-dired
238 "*List of functions to call in order to create a buffer.
239 The functions are called without explicit parameters but can use the
240 variables `desktop-buffer-major-mode', `desktop-buffer-file-name',
241 `desktop-buffer-name'.
242 If one function returns non-nil, no further functions are called.
243 If the function returns t then the buffer is considered created."
244 :type
'(repeat function
)
247 (put 'desktop-buffer-handlers
'risky-local-variable t
)
249 (defvar desktop-create-buffer-form
"(desktop-create-buffer 205"
250 "Opening of form for creation of new buffers.")
252 (defcustom desktop-save-hook nil
253 "Hook run before desktop saves the state of Emacs.
254 This is useful for truncating history lists, for example."
258 (defcustom desktop-minor-mode-table
259 '((auto-fill-function auto-fill-mode
)
261 "Table mapping minor mode variables to minor mode functions.
262 Each entry has the form (NAME RESTORE-FUNCTION).
263 NAME is the name of the buffer-local variable indicating that the minor
264 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
265 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
266 Only minor modes for which the name of the buffer-local variable
267 and the name of the minor mode function are different have to added to
272 ;; ----------------------------------------------------------------------------
273 (defvar desktop-dirname nil
274 "The directory in which the current desktop file resides.")
276 (defconst desktop-header
277 ";; --------------------------------------------------------------------------
278 ;; Desktop File for Emacs
279 ;; --------------------------------------------------------------------------
280 " "*Header to place in Desktop file.")
282 (defvar desktop-delay-hook nil
283 "Hooks run after all buffers are loaded; intended for internal use.")
285 ;; ----------------------------------------------------------------------------
286 (defun desktop-truncate (l n
)
287 "Truncate LIST to at most N elements destructively."
288 (let ((here (nthcdr (1- n
) l
)))
291 ;; ----------------------------------------------------------------------------
292 (defcustom desktop-clear-preserve-buffers
293 '("*scratch*" "*Messages*")
294 "*Buffer names that `desktop-clear' should not delete."
295 :type
'(repeat string
)
298 (defun desktop-clear ()
300 This kills all buffers except for internal ones
301 and those listed in `desktop-clear-preserve-buffers'."
304 kill-ring-yank-pointer nil
306 search-ring-yank-pointer nil
307 regexp-search-ring nil
308 regexp-search-ring-yank-pointer nil
)
309 (let ((buffers (buffer-list)))
311 (or (member (buffer-name (car buffers
)) desktop-clear-preserve-buffers
)
312 (null (buffer-name (car buffers
)))
313 ;; Don't kill buffers made for internal purposes.
314 (and (not (equal (buffer-name (car buffers
)) ""))
315 (eq (aref (buffer-name (car buffers
)) 0) ?\
))
316 (kill-buffer (car buffers
)))
317 (setq buffers
(cdr buffers
))))
318 (delete-other-windows))
319 ;; ----------------------------------------------------------------------------
320 (add-hook 'kill-emacs-hook
'desktop-kill
)
322 (defun desktop-kill ()
325 (desktop-save desktop-dirname
)
327 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
329 (signal (car err
) (cdr err
)))))))
330 ;; ----------------------------------------------------------------------------
331 (defun desktop-list* (&rest args
)
332 (if (null (cdr args
))
334 (setq args
(nreverse args
))
335 (let ((value (cons (nth 1 args
) (car args
))))
336 (setq args
(cdr (cdr args
)))
338 (setq value
(cons (car args
) value
))
339 (setq args
(cdr args
)))
342 (defun desktop-internal-v2s (val)
343 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
344 TXT is a string that when read and evaluated yields value.
345 QUOTE may be `may' (value may be quoted),
346 `must' (values must be quoted), or nil (value may not be quoted)."
348 ((or (numberp val
) (null val
) (eq t val
))
349 (cons 'may
(prin1-to-string val
)))
351 (let ((copy (copy-sequence val
)))
352 (set-text-properties 0 (length copy
) nil copy
)
353 ;; Get rid of text properties because we cannot read them
354 (cons 'may
(prin1-to-string copy
))))
356 (cons 'must
(prin1-to-string val
)))
361 (let ((res (desktop-internal-v2s el
)))
367 (cons nil
(concat "(vector "
368 (mapconcat (lambda (el)
369 (if (eq (car el
) 'must
)
370 (concat "'" (cdr el
))
375 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
382 (let ((q.txt
(desktop-internal-v2s (car p
))))
383 (or anynil
(setq anynil
(null (car q.txt
))))
384 (setq newlist
(cons q.txt newlist
)))
387 (let ((last (desktop-internal-v2s p
))
389 (or anynil
(setq anynil
(null (car last
))))
391 (setq newlist
(cons '(must .
".") newlist
)))
393 (setq newlist
(cons last newlist
))))
394 (setq newlist
(nreverse newlist
))
397 (concat (if use-list
* "(desktop-list* " "(list ")
398 (mapconcat (lambda (el)
399 (if (eq (car el
) 'must
)
400 (concat "'" (cdr el
))
406 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
408 (cons nil
(concat "(symbol-function '"
409 (substring (prin1-to-string val
) 7 -
1)
412 (let ((pos (prin1-to-string (marker-position val
)))
413 (buf (prin1-to-string (buffer-name (marker-buffer val
)))))
414 (cons nil
(concat "(let ((mk (make-marker)))"
415 " (add-hook 'desktop-delay-hook"
416 " (list 'lambda '() (list 'set-marker mk "
417 pos
" (get-buffer " buf
")))) mk)"))))
419 (cons 'may
"\"Unprintable entity\""))))
421 (defun desktop-value-to-string (val)
422 "Convert VALUE to a string that when read evaluates to the same value.
423 Not all types of values are supported."
424 (let* ((print-escape-newlines t
)
425 (float-output-format nil
)
426 (quote.txt
(desktop-internal-v2s val
))
427 (quote (car quote.txt
))
428 (txt (cdr quote.txt
)))
432 ;; ----------------------------------------------------------------------------
433 (defun desktop-outvar (varspec)
434 "Output a setq statement for variable VAR to the desktop file.
435 The argument VARSPEC may be the variable name VAR (a symbol),
436 or a cons cell of the form (VAR . MAX-SIZE),
437 which means to truncate VAR's value to at most MAX-SIZE elements
438 \(if the value is a list) before saving the value."
441 (setq var
(car varspec
) size
(cdr varspec
))
445 (if (and (integerp size
)
448 (desktop-truncate (eval var
) size
))
452 (desktop-value-to-string (symbol-value var
))
454 ;; ----------------------------------------------------------------------------
455 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
456 "Return t if the desktop should record a particular buffer for next startup.
457 FILENAME is the visited file name, BUFNAME is the buffer name, and
458 MODE is the major mode."
459 (let ((case-fold-search nil
))
460 (and (not (string-match desktop-buffers-not-to-save bufname
))
461 (not (memq mode desktop-modes-not-to-save
))
463 (not (string-match desktop-files-not-to-save filename
)))
464 (and (eq mode
'dired-mode
)
466 (set-buffer (get-buffer bufname
))
467 (not (string-match desktop-files-not-to-save
468 default-directory
))))
470 (memq mode desktop-buffer-modes-to-save
))))))
471 ;; ----------------------------------------------------------------------------
472 (defun desktop-save (dirname)
473 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
474 (interactive "DDirectory to save desktop file in: ")
475 (run-hooks 'desktop-save-hook
)
477 (let ((filename (expand-file-name
478 (concat dirname desktop-basefilename
)))
495 (cons (let ((special (assq mim desktop-minor-mode-table
)))
500 (mapcar #'car minor-mode-alist
))
503 (list (mark t
) mark-active
)
505 (run-hook-with-args-until-success
506 'desktop-buffer-misc-functions
)
507 (let ((locals desktop-locals-to-save
)
508 (loclist (buffer-local-variables))
511 (let ((here (assq (car locals
) loclist
)))
513 (setq ll
(cons here ll
))
514 (if (member (car locals
) loclist
)
515 (setq ll
(cons (car locals
) ll
)))))
516 (setq locals
(cdr locals
)))
520 (buf (get-buffer-create "*desktop*")))
524 (insert desktop-header
525 ";; Created " (current-time-string) "\n"
526 ";; Emacs version " emacs-version
"\n\n"
527 ";; Global section:\n")
528 (mapcar (function desktop-outvar
) desktop-globals-to-save
)
529 (if (memq 'kill-ring desktop-globals-to-save
)
530 (insert "(setq kill-ring-yank-pointer (nthcdr "
532 (- (length kill-ring
) (length kill-ring-yank-pointer
)))
535 (insert "\n;; Buffer section:\n")
537 (function (lambda (l)
538 (if (apply 'desktop-save-buffer-p l
)
540 (insert desktop-create-buffer-form
)
542 (function (lambda (e)
544 (desktop-value-to-string e
))))
548 (setq default-directory dirname
)
549 (if (file-exists-p filename
) (delete-file filename
))
550 (write-region (point-min) (point-max) filename nil
'nomessage
)))
551 (setq desktop-dirname dirname
))
552 ;; ----------------------------------------------------------------------------
553 (defun desktop-remove ()
554 "Delete the Desktop file and inactivate the desktop system."
557 (let ((filename (concat desktop-dirname desktop-basefilename
)))
558 (setq desktop-dirname nil
)
559 (if (file-exists-p filename
)
560 (delete-file filename
)))))
561 ;; ----------------------------------------------------------------------------
563 (defun desktop-read ()
564 "Read the Desktop file and the files it specifies.
565 This is a no-op when Emacs is running in batch mode."
569 (let ((dirs '("./" "~/")))
571 (not (file-exists-p (expand-file-name
574 (setq dirs
(cdr dirs
)))
575 (setq desktop-dirname
(and dirs
(expand-file-name (car dirs
))))
578 (load (expand-file-name desktop-basefilename desktop-dirname
)
580 (run-hooks 'desktop-delay-hook
)
581 (setq desktop-delay-hook nil
)
582 (message "Desktop loaded."))
584 ;; ----------------------------------------------------------------------------
586 (defun desktop-load-default ()
587 "Load the `default' start-up library manually.
588 Also inhibit further loading of it. Call this from your `.emacs' file
589 to provide correct modes for autoloaded files."
590 (if (not inhibit-default-init
) ; safety check
593 (setq inhibit-default-init t
))))
594 ;; ----------------------------------------------------------------------------
595 ;; Note: the following functions use the dynamic variable binding in Lisp.
597 (defun desktop-buffer-info-misc-data ()
598 (if (eq major-mode
'Info-mode
)
599 (list Info-current-file
602 (defun desktop-buffer-dired-misc-data ()
603 (if (eq major-mode
'dired-mode
)
605 (expand-file-name dired-directory
)
610 dired-subdir-alist
))))))
612 (defun desktop-buffer-info () "Load an info file."
613 (if (eq 'Info-mode desktop-buffer-major-mode
)
615 (let ((first (nth 0 desktop-buffer-misc
))
616 (second (nth 1 desktop-buffer-misc
)))
617 (when (and first second
)
619 (Info-find-node first second
)
620 (current-buffer))))))
621 ;; ----------------------------------------------------------------------------
622 (defun desktop-buffer-rmail () "Load an RMAIL file."
623 (if (eq 'rmail-mode desktop-buffer-major-mode
)
624 (condition-case error
625 (progn (rmail-input desktop-buffer-file-name
)
626 (if (eq major-mode
'rmail-mode
)
630 (kill-buffer (current-buffer))
632 ;; ----------------------------------------------------------------------------
633 (defun desktop-buffer-mh () "Load a folder in the mh system."
634 (if (eq 'mh-folder-mode desktop-buffer-major-mode
)
638 (mh-visit-folder desktop-buffer-name
)
640 ;; ----------------------------------------------------------------------------
641 (defun desktop-buffer-dired () "Load a directory using dired."
642 (if (eq 'dired-mode desktop-buffer-major-mode
)
643 (if (file-directory-p (file-name-directory (car desktop-buffer-misc
)))
645 (dired (car desktop-buffer-misc
))
646 (mapcar 'dired-maybe-insert-subdir
(cdr desktop-buffer-misc
))
648 (message "Directory %s no longer exists." (car desktop-buffer-misc
))
651 ;; ----------------------------------------------------------------------------
652 (defun desktop-buffer-file () "Load a file."
653 (if desktop-buffer-file-name
654 (if (or (file-exists-p desktop-buffer-file-name
)
655 (and desktop-missing-file-warning
657 "File \"%s\" no longer exists. Re-create? "
658 desktop-buffer-file-name
))))
659 (let ((buf (find-file-noselect desktop-buffer-file-name
)))
661 (switch-to-buffer buf
)
662 (error (pop-to-buffer buf
))))
664 ;; ----------------------------------------------------------------------------
665 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
667 (defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name
668 desktop-buffer-major-mode
669 mim pt mk ro desktop-buffer-misc
671 (let ((hlist desktop-buffer-handlers
)
674 (while (and (not result
) hlist
)
675 (setq handler
(car hlist
))
676 (setq result
(funcall handler
))
677 (setq hlist
(cdr hlist
)))
678 (when (bufferp result
)
680 (if (not (equal (buffer-name) desktop-buffer-name
))
681 (rename-buffer desktop-buffer-name
))
683 (cond ((equal '(t) mim
) (auto-fill-mode 1)) ; backwards compatible
684 ((equal '(nil) mim
) (auto-fill-mode 0))
685 (t (mapcar #'(lambda (minor-mode)
686 (unless (or (eq minor-mode t
) (eq minor-mode nil
))
687 (if (and minor-mode
(fboundp minor-mode
))
688 (funcall minor-mode
1))))
694 (setq mark-active
(car (cdr mk
))))
696 ;; Never override file system if the file really is read-only marked.
697 (if ro
(setq buffer-read-only ro
))
699 (let ((this (car locals
)))
701 ;; an entry of this form `(symbol . value)'
703 (make-local-variable (car this
))
704 (set (car this
) (cdr this
)))
705 ;; an entry of the form `symbol'
706 (make-local-variable this
)
708 (setq locals
(cdr locals
))))))
710 ;; Backward compatibility -- update parameters to 205 standards.
711 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
712 desktop-buffer-major-mode
713 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
714 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
715 desktop-buffer-major-mode
(cdr mim
) pt mk ro
717 (list (cons 'truncate-lines tl
)
718 (cons 'fill-column fc
)
719 (cons 'case-fold-search cfs
)
720 (cons 'case-replace cr
)
721 (cons 'overwrite-mode
(car mim
)))))
722 ;; ----------------------------------------------------------------------------
724 ;; If the user set desktop-enable to t with Custom,
725 ;; do the rest of what it takes to use desktop,
726 ;; but do it after finishing loading the init file.
727 (add-hook 'after-init-hook
730 (desktop-load-default)
735 ;;; desktop.el ends here