1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997 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-major-mode nil
186 "When desktop creates a buffer, this holds the desired Major mode."
190 (defcustom desktop-buffer-file-name nil
191 "When desktop creates a buffer, this holds the file name to visit."
192 :type
'(choice file
(const nil
))
195 (defcustom desktop-buffer-name nil
196 "When desktop creates a buffer, this holds the desired buffer name."
197 :type
'(choice string
(const nil
))
200 (defvar desktop-buffer-misc nil
201 "When desktop creates a buffer, this holds a list of misc info.
202 It is used by the `desktop-buffer-handlers' functions.")
204 (defcustom desktop-buffer-handlers
205 '(desktop-buffer-dired
210 "*List of functions to call in order to create a buffer.
211 The functions are called without explicit parameters but can use the
212 variables `desktop-buffer-major-mode', `desktop-buffer-file-name',
213 `desktop-buffer-name'.
214 If one function returns non-nil, no further functions are called.
215 If the function returns t then the buffer is considered created."
216 :type
'(repeat function
)
219 (defvar desktop-create-buffer-form
"(desktop-create-buffer 205"
220 "Opening of form for creation of new buffers.")
222 (defcustom desktop-save-hook nil
223 "Hook run before desktop saves the state of Emacs.
224 This is useful for truncating history lists, for example."
228 (defcustom desktop-minor-mode-table
229 '((auto-fill-function auto-fill-mode
)
231 "Table mapping minor mode variables to minor mode functions.
232 Each entry has the form (NAME RESTORE-FUNCTION).
233 NAME is the name of the buffer-local variable indicating that the minor
234 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
235 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
236 Only minor modes for which the name of the buffer-local variable
237 and the name of the minor mode function are different have to added to
242 ;; ----------------------------------------------------------------------------
243 (defvar desktop-dirname nil
244 "The directory in which the current desktop file resides.")
246 (defconst desktop-header
247 ";; --------------------------------------------------------------------------
248 ;; Desktop File for Emacs
249 ;; --------------------------------------------------------------------------
250 " "*Header to place in Desktop file.")
252 (defvar desktop-delay-hook nil
253 "Hooks run after all buffers are loaded; intended for internal use.")
255 ;; ----------------------------------------------------------------------------
256 (defun desktop-truncate (l n
)
257 "Truncate LIST to at most N elements destructively."
258 (let ((here (nthcdr (1- n
) l
)))
261 ;; ----------------------------------------------------------------------------
262 (defcustom desktop-clear-preserve-buffers
263 '("*scratch*" "*Messages*")
264 "*Buffer names that `desktop-clear' should not delete."
265 :type
'(repeat string
)
268 (defun desktop-clear ()
270 This kills all buffers except for internal ones
271 and those listed in `desktop-clear-preserve-buffers'."
274 kill-ring-yank-pointer nil
276 search-ring-yank-pointer nil
277 regexp-search-ring nil
278 regexp-search-ring-yank-pointer nil
)
279 (let ((buffers (buffer-list)))
281 (or (member (buffer-name (car buffers
)) desktop-clear-preserve-buffers
)
282 (null (buffer-name (car buffers
)))
283 ;; Don't kill buffers made for internal purposes.
284 (and (not (equal (buffer-name (car buffers
)) ""))
285 (eq (aref (buffer-name (car buffers
)) 0) ?\
))
286 (kill-buffer (car buffers
)))
287 (setq buffers
(cdr buffers
))))
288 (delete-other-windows))
289 ;; ----------------------------------------------------------------------------
290 (add-hook 'kill-emacs-hook
'desktop-kill
)
292 (defun desktop-kill ()
295 (desktop-save desktop-dirname
)
297 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
299 (signal (car err
) (cdr err
)))))))
300 ;; ----------------------------------------------------------------------------
301 (defun desktop-list* (&rest args
)
302 (if (null (cdr args
))
304 (setq args
(nreverse args
))
305 (let ((value (cons (nth 1 args
) (car args
))))
306 (setq args
(cdr (cdr args
)))
308 (setq value
(cons (car args
) value
))
309 (setq args
(cdr args
)))
312 (defun desktop-internal-v2s (val)
313 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
314 TXT is a string that when read and evaluated yields value.
315 QUOTE may be `may' (value may be quoted),
316 `must' (values must be quoted), or nil (value may not be quoted)."
318 ((or (numberp val
) (null val
) (eq t val
))
319 (cons 'may
(prin1-to-string val
)))
321 (let ((copy (copy-sequence val
)))
322 (set-text-properties 0 (length copy
) nil copy
)
323 ;; Get rid of text properties because we cannot read them
324 (cons 'may
(prin1-to-string copy
))))
326 (cons 'must
(prin1-to-string val
)))
331 (let ((res (desktop-internal-v2s el
)))
337 (cons nil
(concat "(vector "
338 (mapconcat (lambda (el)
339 (if (eq (car el
) 'must
)
340 (concat "'" (cdr el
))
345 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
352 (let ((q.txt
(desktop-internal-v2s (car p
))))
353 (or anynil
(setq anynil
(null (car q.txt
))))
354 (setq newlist
(cons q.txt newlist
)))
357 (let ((last (desktop-internal-v2s p
))
359 (or anynil
(setq anynil
(null (car last
))))
361 (setq newlist
(cons '(must .
".") newlist
)))
363 (setq newlist
(cons last newlist
))))
364 (setq newlist
(nreverse newlist
))
367 (concat (if use-list
* "(desktop-list* " "(list ")
368 (mapconcat (lambda (el)
369 (if (eq (car el
) 'must
)
370 (concat "'" (cdr el
))
376 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
378 (cons nil
(concat "(symbol-function '"
379 (substring (prin1-to-string val
) 7 -
1)
382 (let ((pos (prin1-to-string (marker-position val
)))
383 (buf (prin1-to-string (buffer-name (marker-buffer val
)))))
384 (cons nil
(concat "(let ((mk (make-marker)))"
385 " (add-hook 'desktop-delay-hook"
386 " (list 'lambda '() (list 'set-marker mk "
387 pos
" (get-buffer " buf
")))) mk)"))))
389 (cons 'may
"\"Unprintable entity\""))))
391 (defun desktop-value-to-string (val)
392 "Convert VALUE to a string that when read evaluates to the same value.
393 Not all types of values are supported."
394 (let* ((print-escape-newlines t
)
395 (float-output-format nil
)
396 (quote.txt
(desktop-internal-v2s val
))
397 (quote (car quote.txt
))
398 (txt (cdr quote.txt
)))
402 ;; ----------------------------------------------------------------------------
403 (defun desktop-outvar (varspec)
404 "Output a setq statement for variable VAR to the desktop file.
405 The argument VARSPEC may be the variable name VAR (a symbol),
406 or a cons cell of the form (VAR . MAX-SIZE),
407 which means to truncate VAR's value to at most MAX-SIZE elements
408 \(if the value is a list) before saving the value."
411 (setq var
(car varspec
) size
(cdr varspec
))
415 (if (and (integerp size
)
418 (desktop-truncate (eval var
) size
))
422 (desktop-value-to-string (symbol-value var
))
424 ;; ----------------------------------------------------------------------------
425 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
426 "Return t if the desktop should record a particular buffer for next startup.
427 FILENAME is the visited file name, BUFNAME is the buffer name, and
428 MODE is the major mode."
429 (let ((case-fold-search nil
))
431 (not (string-match desktop-buffers-not-to-save bufname
))
432 (not (string-match desktop-files-not-to-save filename
)))
433 (and (eq mode
'dired-mode
)
435 (set-buffer (get-buffer bufname
))
436 (not (string-match desktop-files-not-to-save
437 default-directory
))))
439 (memq mode
'(Info-mode rmail-mode
))))))
440 ;; ----------------------------------------------------------------------------
441 (defun desktop-save (dirname)
442 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
443 (interactive "DDirectory to save desktop file in: ")
444 (run-hooks 'desktop-save-hook
)
446 (let ((filename (expand-file-name
447 (concat dirname desktop-basefilename
)))
464 (cons (let ((special (assq mim desktop-minor-mode-table
)))
469 (mapcar #'car minor-mode-alist
))
472 (list (mark t
) mark-active
)
474 (cond ((eq major-mode
'Info-mode
)
475 (list Info-current-file
477 ((eq major-mode
'dired-mode
)
479 (expand-file-name dired-directory
)
484 dired-subdir-alist
))))))
485 (let ((locals desktop-locals-to-save
)
486 (loclist (buffer-local-variables))
489 (let ((here (assq (car locals
) loclist
)))
491 (setq ll
(cons here ll
))
492 (if (member (car locals
) loclist
)
493 (setq ll
(cons (car locals
) ll
)))))
494 (setq locals
(cdr locals
)))
498 (buf (get-buffer-create "*desktop*")))
502 (insert desktop-header
503 ";; Created " (current-time-string) "\n"
504 ";; Emacs version " emacs-version
"\n\n"
505 ";; Global section:\n")
506 (mapcar (function desktop-outvar
) desktop-globals-to-save
)
507 (if (memq 'kill-ring desktop-globals-to-save
)
508 (insert "(setq kill-ring-yank-pointer (nthcdr "
510 (- (length kill-ring
) (length kill-ring-yank-pointer
)))
513 (insert "\n;; Buffer section:\n")
515 (function (lambda (l)
516 (if (apply 'desktop-save-buffer-p l
)
518 (insert desktop-create-buffer-form
)
520 (function (lambda (e)
522 (desktop-value-to-string e
))))
526 (setq default-directory dirname
)
527 (if (file-exists-p filename
) (delete-file filename
))
528 (write-region (point-min) (point-max) filename nil
'nomessage
)))
529 (setq desktop-dirname dirname
))
530 ;; ----------------------------------------------------------------------------
531 (defun desktop-remove ()
532 "Delete the Desktop file and inactivate the desktop system."
535 (let ((filename (concat desktop-dirname desktop-basefilename
)))
536 (setq desktop-dirname nil
)
537 (if (file-exists-p filename
)
538 (delete-file filename
)))))
539 ;; ----------------------------------------------------------------------------
541 (defun desktop-read ()
542 "Read the Desktop file and the files it specifies.
543 This is a no-op when Emacs is running in batch mode."
547 (let ((dirs '("./" "~/")))
549 (not (file-exists-p (expand-file-name
552 (setq dirs
(cdr dirs
)))
553 (setq desktop-dirname
(and dirs
(expand-file-name (car dirs
))))
556 (load (expand-file-name desktop-basefilename desktop-dirname
)
558 (run-hooks 'desktop-delay-hook
)
559 (setq desktop-delay-hook nil
)
560 (message "Desktop loaded."))
562 ;; ----------------------------------------------------------------------------
564 (defun desktop-load-default ()
565 "Load the `default' start-up library manually.
566 Also inhibit further loading of it. Call this from your `.emacs' file
567 to provide correct modes for autoloaded files."
568 (if (not inhibit-default-init
) ; safety check
571 (setq inhibit-default-init t
))))
572 ;; ----------------------------------------------------------------------------
573 ;; Note: the following functions use the dynamic variable binding in Lisp.
575 (defun desktop-buffer-info () "Load an info file."
576 (if (eq 'Info-mode desktop-buffer-major-mode
)
579 (Info-find-node (nth 0 desktop-buffer-misc
) (nth 1 desktop-buffer-misc
))
581 ;; ----------------------------------------------------------------------------
582 (defun desktop-buffer-rmail () "Load an RMAIL file."
583 (if (eq 'rmail-mode desktop-buffer-major-mode
)
584 (condition-case error
585 (progn (rmail-input desktop-buffer-file-name
)
586 (if (eq major-mode
'rmail-mode
)
590 (kill-buffer (current-buffer))
592 ;; ----------------------------------------------------------------------------
593 (defun desktop-buffer-mh () "Load a folder in the mh system."
594 (if (eq 'mh-folder-mode desktop-buffer-major-mode
)
598 (mh-visit-folder desktop-buffer-name
)
600 ;; ----------------------------------------------------------------------------
601 (defun desktop-buffer-dired () "Load a directory using dired."
602 (if (eq 'dired-mode desktop-buffer-major-mode
)
603 (if (file-directory-p (file-name-directory (car desktop-buffer-misc
)))
605 (dired (car desktop-buffer-misc
))
606 (mapcar 'dired-maybe-insert-subdir
(cdr desktop-buffer-misc
))
608 (message "Directory %s no longer exists." (car desktop-buffer-misc
))
611 ;; ----------------------------------------------------------------------------
612 (defun desktop-buffer-file () "Load a file."
613 (if desktop-buffer-file-name
614 (if (or (file-exists-p desktop-buffer-file-name
)
615 (and desktop-missing-file-warning
617 "File \"%s\" no longer exists. Re-create? "
618 desktop-buffer-file-name
))))
619 (progn (find-file desktop-buffer-file-name
) (current-buffer))
621 ;; ----------------------------------------------------------------------------
622 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
624 (defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name
625 desktop-buffer-major-mode
626 mim pt mk ro desktop-buffer-misc
628 (let ((hlist desktop-buffer-handlers
)
631 (while (and (not result
) hlist
)
632 (setq handler
(car hlist
))
633 (setq result
(funcall handler
))
634 (setq hlist
(cdr hlist
)))
635 (when (bufferp result
)
637 (if (not (equal (buffer-name) desktop-buffer-name
))
638 (rename-buffer desktop-buffer-name
))
640 (cond ((equal '(t) mim
) (auto-fill-mode 1)) ; backwards compatible
641 ((equal '(nil) mim
) (auto-fill-mode 0))
642 (t (mapcar #'(lambda (minor-mode)
643 (when minor-mode
(funcall minor-mode
1)))
649 (setq mark-active
(car (cdr mk
))))
651 ;; Never override file system if the file really is read-only marked.
652 (if ro
(setq buffer-read-only ro
))
654 (let ((this (car locals
)))
656 ;; an entry of this form `(symbol . value)'
658 (make-local-variable (car this
))
659 (set (car this
) (cdr this
)))
660 ;; an entry of the form `symbol'
661 (make-local-variable this
)
663 (setq locals
(cdr locals
))))))
665 ;; Backward compatibility -- update parameters to 205 standards.
666 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
667 desktop-buffer-major-mode
668 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
669 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
670 desktop-buffer-major-mode
(cdr mim
) pt mk ro
672 (list (cons 'truncate-lines tl
)
673 (cons 'fill-column fc
)
674 (cons 'case-fold-search cfs
)
675 (cons 'case-replace cr
)
676 (cons 'overwrite-mode
(car mim
)))))
677 ;; ----------------------------------------------------------------------------
679 ;; If the user set desktop-enable to t with Custom,
680 ;; do the rest of what it takes to use desktop,
681 ;; but do it after finishing loading the init file.
682 (add-hook 'after-init-hook
685 (desktop-load-default)
690 ;; desktop.el ends here.