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-modes-not-to-save nil
186 "List of major modes whose buffers should not be saved."
187 :type
'(repeat symbol
)
190 (defcustom desktop-buffer-major-mode nil
191 "When desktop creates a buffer, this holds the desired Major mode."
195 (defcustom desktop-buffer-file-name nil
196 "When desktop creates a buffer, this holds the file name to visit."
197 :type
'(choice file
(const nil
))
200 (defcustom desktop-buffer-name nil
201 "When desktop creates a buffer, this holds the desired buffer name."
202 :type
'(choice string
(const nil
))
205 (defvar desktop-buffer-misc nil
206 "When desktop creates a buffer, this holds a list of misc info.
207 It is used by the `desktop-buffer-handlers' functions.")
209 (defcustom desktop-buffer-handlers
210 '(desktop-buffer-dired
215 "*List of functions to call in order to create a buffer.
216 The functions are called without explicit parameters but can use the
217 variables `desktop-buffer-major-mode', `desktop-buffer-file-name',
218 `desktop-buffer-name'.
219 If one function returns non-nil, no further functions are called.
220 If the function returns t then the buffer is considered created."
221 :type
'(repeat function
)
224 (defvar desktop-create-buffer-form
"(desktop-create-buffer 205"
225 "Opening of form for creation of new buffers.")
227 (defcustom desktop-save-hook nil
228 "Hook run before desktop saves the state of Emacs.
229 This is useful for truncating history lists, for example."
233 (defcustom desktop-minor-mode-table
234 '((auto-fill-function auto-fill-mode
)
236 "Table mapping minor mode variables to minor mode functions.
237 Each entry has the form (NAME RESTORE-FUNCTION).
238 NAME is the name of the buffer-local variable indicating that the minor
239 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
240 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
241 Only minor modes for which the name of the buffer-local variable
242 and the name of the minor mode function are different have to added to
247 ;; ----------------------------------------------------------------------------
248 (defvar desktop-dirname nil
249 "The directory in which the current desktop file resides.")
251 (defconst desktop-header
252 ";; --------------------------------------------------------------------------
253 ;; Desktop File for Emacs
254 ;; --------------------------------------------------------------------------
255 " "*Header to place in Desktop file.")
257 (defvar desktop-delay-hook nil
258 "Hooks run after all buffers are loaded; intended for internal use.")
260 ;; ----------------------------------------------------------------------------
261 (defun desktop-truncate (l n
)
262 "Truncate LIST to at most N elements destructively."
263 (let ((here (nthcdr (1- n
) l
)))
266 ;; ----------------------------------------------------------------------------
267 (defcustom desktop-clear-preserve-buffers
268 '("*scratch*" "*Messages*")
269 "*Buffer names that `desktop-clear' should not delete."
270 :type
'(repeat string
)
273 (defun desktop-clear ()
275 This kills all buffers except for internal ones
276 and those listed in `desktop-clear-preserve-buffers'."
279 kill-ring-yank-pointer nil
281 search-ring-yank-pointer nil
282 regexp-search-ring nil
283 regexp-search-ring-yank-pointer nil
)
284 (let ((buffers (buffer-list)))
286 (or (member (buffer-name (car buffers
)) desktop-clear-preserve-buffers
)
287 (null (buffer-name (car buffers
)))
288 ;; Don't kill buffers made for internal purposes.
289 (and (not (equal (buffer-name (car buffers
)) ""))
290 (eq (aref (buffer-name (car buffers
)) 0) ?\
))
291 (kill-buffer (car buffers
)))
292 (setq buffers
(cdr buffers
))))
293 (delete-other-windows))
294 ;; ----------------------------------------------------------------------------
295 (add-hook 'kill-emacs-hook
'desktop-kill
)
297 (defun desktop-kill ()
300 (desktop-save desktop-dirname
)
302 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
304 (signal (car err
) (cdr err
)))))))
305 ;; ----------------------------------------------------------------------------
306 (defun desktop-list* (&rest args
)
307 (if (null (cdr args
))
309 (setq args
(nreverse args
))
310 (let ((value (cons (nth 1 args
) (car args
))))
311 (setq args
(cdr (cdr args
)))
313 (setq value
(cons (car args
) value
))
314 (setq args
(cdr args
)))
317 (defun desktop-internal-v2s (val)
318 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
319 TXT is a string that when read and evaluated yields value.
320 QUOTE may be `may' (value may be quoted),
321 `must' (values must be quoted), or nil (value may not be quoted)."
323 ((or (numberp val
) (null val
) (eq t val
))
324 (cons 'may
(prin1-to-string val
)))
326 (let ((copy (copy-sequence val
)))
327 (set-text-properties 0 (length copy
) nil copy
)
328 ;; Get rid of text properties because we cannot read them
329 (cons 'may
(prin1-to-string copy
))))
331 (cons 'must
(prin1-to-string val
)))
336 (let ((res (desktop-internal-v2s el
)))
342 (cons nil
(concat "(vector "
343 (mapconcat (lambda (el)
344 (if (eq (car el
) 'must
)
345 (concat "'" (cdr el
))
350 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
357 (let ((q.txt
(desktop-internal-v2s (car p
))))
358 (or anynil
(setq anynil
(null (car q.txt
))))
359 (setq newlist
(cons q.txt newlist
)))
362 (let ((last (desktop-internal-v2s p
))
364 (or anynil
(setq anynil
(null (car last
))))
366 (setq newlist
(cons '(must .
".") newlist
)))
368 (setq newlist
(cons last newlist
))))
369 (setq newlist
(nreverse newlist
))
372 (concat (if use-list
* "(desktop-list* " "(list ")
373 (mapconcat (lambda (el)
374 (if (eq (car el
) 'must
)
375 (concat "'" (cdr el
))
381 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
383 (cons nil
(concat "(symbol-function '"
384 (substring (prin1-to-string val
) 7 -
1)
387 (let ((pos (prin1-to-string (marker-position val
)))
388 (buf (prin1-to-string (buffer-name (marker-buffer val
)))))
389 (cons nil
(concat "(let ((mk (make-marker)))"
390 " (add-hook 'desktop-delay-hook"
391 " (list 'lambda '() (list 'set-marker mk "
392 pos
" (get-buffer " buf
")))) mk)"))))
394 (cons 'may
"\"Unprintable entity\""))))
396 (defun desktop-value-to-string (val)
397 "Convert VALUE to a string that when read evaluates to the same value.
398 Not all types of values are supported."
399 (let* ((print-escape-newlines t
)
400 (float-output-format nil
)
401 (quote.txt
(desktop-internal-v2s val
))
402 (quote (car quote.txt
))
403 (txt (cdr quote.txt
)))
407 ;; ----------------------------------------------------------------------------
408 (defun desktop-outvar (varspec)
409 "Output a setq statement for variable VAR to the desktop file.
410 The argument VARSPEC may be the variable name VAR (a symbol),
411 or a cons cell of the form (VAR . MAX-SIZE),
412 which means to truncate VAR's value to at most MAX-SIZE elements
413 \(if the value is a list) before saving the value."
416 (setq var
(car varspec
) size
(cdr varspec
))
420 (if (and (integerp size
)
423 (desktop-truncate (eval var
) size
))
427 (desktop-value-to-string (symbol-value var
))
429 ;; ----------------------------------------------------------------------------
430 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
431 "Return t if the desktop should record a particular buffer for next startup.
432 FILENAME is the visited file name, BUFNAME is the buffer name, and
433 MODE is the major mode."
434 (let ((case-fold-search nil
))
435 (and (not (string-match desktop-buffers-not-to-save bufname
))
436 (not (memq mode desktop-modes-not-to-save
))
438 (not (string-match desktop-files-not-to-save filename
)))
439 (and (eq mode
'dired-mode
)
441 (set-buffer (get-buffer bufname
))
442 (not (string-match desktop-files-not-to-save
443 default-directory
))))
445 (memq mode
'(Info-mode rmail-mode
)))))))
446 ;; ----------------------------------------------------------------------------
447 (defun desktop-save (dirname)
448 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
449 (interactive "DDirectory to save desktop file in: ")
450 (run-hooks 'desktop-save-hook
)
452 (let ((filename (expand-file-name
453 (concat dirname desktop-basefilename
)))
470 (cons (let ((special (assq mim desktop-minor-mode-table
)))
475 (mapcar #'car minor-mode-alist
))
478 (list (mark t
) mark-active
)
480 (cond ((eq major-mode
'Info-mode
)
481 (list Info-current-file
483 ((eq major-mode
'dired-mode
)
485 (expand-file-name dired-directory
)
490 dired-subdir-alist
))))))
491 (let ((locals desktop-locals-to-save
)
492 (loclist (buffer-local-variables))
495 (let ((here (assq (car locals
) loclist
)))
497 (setq ll
(cons here ll
))
498 (if (member (car locals
) loclist
)
499 (setq ll
(cons (car locals
) ll
)))))
500 (setq locals
(cdr locals
)))
504 (buf (get-buffer-create "*desktop*")))
508 (insert desktop-header
509 ";; Created " (current-time-string) "\n"
510 ";; Emacs version " emacs-version
"\n\n"
511 ";; Global section:\n")
512 (mapcar (function desktop-outvar
) desktop-globals-to-save
)
513 (if (memq 'kill-ring desktop-globals-to-save
)
514 (insert "(setq kill-ring-yank-pointer (nthcdr "
516 (- (length kill-ring
) (length kill-ring-yank-pointer
)))
519 (insert "\n;; Buffer section:\n")
521 (function (lambda (l)
522 (if (apply 'desktop-save-buffer-p l
)
524 (insert desktop-create-buffer-form
)
526 (function (lambda (e)
528 (desktop-value-to-string e
))))
532 (setq default-directory dirname
)
533 (if (file-exists-p filename
) (delete-file filename
))
534 (write-region (point-min) (point-max) filename nil
'nomessage
)))
535 (setq desktop-dirname dirname
))
536 ;; ----------------------------------------------------------------------------
537 (defun desktop-remove ()
538 "Delete the Desktop file and inactivate the desktop system."
541 (let ((filename (concat desktop-dirname desktop-basefilename
)))
542 (setq desktop-dirname nil
)
543 (if (file-exists-p filename
)
544 (delete-file filename
)))))
545 ;; ----------------------------------------------------------------------------
547 (defun desktop-read ()
548 "Read the Desktop file and the files it specifies.
549 This is a no-op when Emacs is running in batch mode."
553 (let ((dirs '("./" "~/")))
555 (not (file-exists-p (expand-file-name
558 (setq dirs
(cdr dirs
)))
559 (setq desktop-dirname
(and dirs
(expand-file-name (car dirs
))))
562 (load (expand-file-name desktop-basefilename desktop-dirname
)
564 (run-hooks 'desktop-delay-hook
)
565 (setq desktop-delay-hook nil
)
566 (message "Desktop loaded."))
568 ;; ----------------------------------------------------------------------------
570 (defun desktop-load-default ()
571 "Load the `default' start-up library manually.
572 Also inhibit further loading of it. Call this from your `.emacs' file
573 to provide correct modes for autoloaded files."
574 (if (not inhibit-default-init
) ; safety check
577 (setq inhibit-default-init t
))))
578 ;; ----------------------------------------------------------------------------
579 ;; Note: the following functions use the dynamic variable binding in Lisp.
581 (defun desktop-buffer-info () "Load an info file."
582 (if (eq 'Info-mode desktop-buffer-major-mode
)
585 (Info-find-node (nth 0 desktop-buffer-misc
) (nth 1 desktop-buffer-misc
))
587 ;; ----------------------------------------------------------------------------
588 (defun desktop-buffer-rmail () "Load an RMAIL file."
589 (if (eq 'rmail-mode desktop-buffer-major-mode
)
590 (condition-case error
591 (progn (rmail-input desktop-buffer-file-name
)
592 (if (eq major-mode
'rmail-mode
)
596 (kill-buffer (current-buffer))
598 ;; ----------------------------------------------------------------------------
599 (defun desktop-buffer-mh () "Load a folder in the mh system."
600 (if (eq 'mh-folder-mode desktop-buffer-major-mode
)
604 (mh-visit-folder desktop-buffer-name
)
606 ;; ----------------------------------------------------------------------------
607 (defun desktop-buffer-dired () "Load a directory using dired."
608 (if (eq 'dired-mode desktop-buffer-major-mode
)
609 (if (file-directory-p (file-name-directory (car desktop-buffer-misc
)))
611 (dired (car desktop-buffer-misc
))
612 (mapcar 'dired-maybe-insert-subdir
(cdr desktop-buffer-misc
))
614 (message "Directory %s no longer exists." (car desktop-buffer-misc
))
617 ;; ----------------------------------------------------------------------------
618 (defun desktop-buffer-file () "Load a file."
619 (if desktop-buffer-file-name
620 (if (or (file-exists-p desktop-buffer-file-name
)
621 (and desktop-missing-file-warning
623 "File \"%s\" no longer exists. Re-create? "
624 desktop-buffer-file-name
))))
625 (let ((buf (find-file-noselect desktop-buffer-file-name
)))
627 (switch-to-buffer buf
)
628 (error (pop-to-buffer buf
))))
630 ;; ----------------------------------------------------------------------------
631 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
633 (defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name
634 desktop-buffer-major-mode
635 mim pt mk ro desktop-buffer-misc
637 (let ((hlist desktop-buffer-handlers
)
640 (while (and (not result
) hlist
)
641 (setq handler
(car hlist
))
642 (setq result
(funcall handler
))
643 (setq hlist
(cdr hlist
)))
644 (when (bufferp result
)
646 (if (not (equal (buffer-name) desktop-buffer-name
))
647 (rename-buffer desktop-buffer-name
))
649 (cond ((equal '(t) mim
) (auto-fill-mode 1)) ; backwards compatible
650 ((equal '(nil) mim
) (auto-fill-mode 0))
651 (t (mapcar #'(lambda (minor-mode)
652 (when minor-mode
(funcall minor-mode
1)))
658 (setq mark-active
(car (cdr mk
))))
660 ;; Never override file system if the file really is read-only marked.
661 (if ro
(setq buffer-read-only ro
))
663 (let ((this (car locals
)))
665 ;; an entry of this form `(symbol . value)'
667 (make-local-variable (car this
))
668 (set (car this
) (cdr this
)))
669 ;; an entry of the form `symbol'
670 (make-local-variable this
)
672 (setq locals
(cdr locals
))))))
674 ;; Backward compatibility -- update parameters to 205 standards.
675 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
676 desktop-buffer-major-mode
677 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
678 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
679 desktop-buffer-major-mode
(cdr mim
) pt mk ro
681 (list (cons 'truncate-lines tl
)
682 (cons 'fill-column fc
)
683 (cons 'case-fold-search cfs
)
684 (cons 'case-replace cr
)
685 (cons 'overwrite-mode
(car mim
)))))
686 ;; ----------------------------------------------------------------------------
688 ;; If the user set desktop-enable to t with Custom,
689 ;; do the rest of what it takes to use desktop,
690 ;; but do it after finishing loading the init file.
691 (add-hook 'after-init-hook
694 (desktop-load-default)
699 ;;; desktop.el ends here