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: customization
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 three lines in the bottom of your .emacs
39 ;; file (the later the better):
42 ;; (desktop-load-default)
45 ;; Between the second and the third line you may wish to add something that
46 ;; updates the variables `desktop-globals-to-save' and/or
47 ;; `desktop-locals-to-save'. If for instance you want to save the local
48 ;; variable `foobar' for every buffer in which it is local, you could add
51 ;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
53 ;; To avoid saving excessive amounts of data you may also wish to add
54 ;; something like the following
56 ;; (add-hook 'kill-emacs-hook
58 ;; (desktop-truncate search-ring 3)
59 ;; (desktop-truncate regexp-search-ring 3)))
61 ;; which will make sure that no more than three search items are saved. You
62 ;; must place this line *after* the (load "desktop") line. See also the
63 ;; variable desktop-save-hook.
65 ;; Start Emacs in the root directory of your "project". The desktop saver
66 ;; is inactive by default. You activate it by M-x desktop-save RET. When
67 ;; you exit the next time the above data will be saved. This ensures that
68 ;; all the files you were editing will be reloaded the next time you start
69 ;; Emacs from the same directory and that points will be set where you
70 ;; left them. If you save a desktop file in your home directory it will
71 ;; act as a default desktop when you start Emacs from a directory that
72 ;; doesn't have its own. I never do this, but you may want to.
74 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
75 ;; in your home directory is used for that. Saving global default values
76 ;; for buffers is an example of misuse.
78 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
79 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
80 ;; things you did not mean to keep. Use M-x desktop-clear RET.
82 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
83 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
84 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
85 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
86 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
87 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
88 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
89 ;; ---------------------------------------------------------------------------
92 ;; Save window configuration.
93 ;; Recognize more minor modes.
95 ;; Start-up with buffer-menu???
99 ;; Make the compilation more silent
101 ;; We use functions from these modules
102 ;; We can't (require 'mh-e) since that wants to load something.
103 (mapcar 'require
'(info dired reporter
)))
104 ;; ----------------------------------------------------------------------------
105 ;; USER OPTIONS -- settings you might want to play with.
106 ;; ----------------------------------------------------------------------------
108 (defgroup desktop nil
109 "Save status of Emacs when you exit."
112 (defcustom desktop-enable nil
113 "*Non-nil enable Desktop to save the state of Emacs when you exit."
117 :initialize
'custom-initialize-default
)
119 (defcustom desktop-basefilename
120 (convert-standard-filename ".emacs.desktop")
121 "File for Emacs desktop, not including the directory name."
125 (defcustom desktop-missing-file-warning nil
126 "*If non-nil then desktop warns when a file no longer exists.
127 Otherwise it simply ignores that file."
131 (defvar desktop-globals-to-save
132 (list 'desktop-missing-file-warning
133 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
140 ;; 'desktop-globals-to-save ; Itself!
142 "List of global variables to save when killing Emacs.
143 An element may be variable name (a symbol)
144 or a cons cell of the form (VAR . MAX-SIZE),
145 which means to truncate VAR's value to at most MAX-SIZE elements
146 \(if the value is a list) before saving the value.")
148 (defvar desktop-locals-to-save
149 (list 'desktop-locals-to-save
; Itself! Think it over.
155 'change-log-default-name
158 "List of local variables to save for each buffer.
159 The variables are saved only when they really are local.")
160 (make-variable-buffer-local 'desktop-locals-to-save
)
162 ;; We skip .log files because they are normally temporary.
163 ;; (ftp) files because they require passwords and whatnot.
164 ;; TAGS files to save time (tags-file-name is saved instead).
165 (defcustom desktop-buffers-not-to-save
166 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
167 "Regexp identifying buffers that are to be excluded from saving."
171 ;; Skip ange-ftp files
172 (defcustom desktop-files-not-to-save
174 "Regexp identifying files whose buffers are to be excluded from saving."
178 (defcustom desktop-buffer-major-mode nil
179 "When desktop creates a buffer, this holds the desired Major mode."
183 (defcustom desktop-buffer-file-name nil
184 "When desktop creates a buffer, this holds the file name to visit."
185 :type
'(choice file
(const nil
))
188 (defcustom desktop-buffer-name nil
189 "When desktop creates a buffer, this holds the desired buffer name."
190 :type
'(choice string
(const nil
))
193 (defvar desktop-buffer-misc nil
194 "When desktop creates a buffer, this holds a list of misc info.
195 It is used by the `desktop-buffer-handlers' functions.")
197 (defcustom desktop-buffer-handlers
198 '(desktop-buffer-dired
203 "*List of functions to call in order to create a buffer.
204 The functions are called without explicit parameters but can use the
205 variables `desktop-buffer-major-mode', `desktop-buffer-file-name',
206 `desktop-buffer-name'.
207 If one function returns non-nil, no further functions are called.
208 If the function returns t then the buffer is considered created."
209 :type
'(repeat function
)
212 (defvar desktop-create-buffer-form
"(desktop-create-buffer 205"
213 "Opening of form for creation of new buffers.")
215 (defcustom desktop-save-hook nil
216 "Hook run before desktop saves the state of Emacs.
217 This is useful for truncating history lists, for example."
221 ;; ----------------------------------------------------------------------------
222 (defvar desktop-dirname nil
223 "The directory in which the current desktop file resides.")
225 (defconst desktop-header
226 ";; --------------------------------------------------------------------------
227 ;; Desktop File for Emacs
228 ;; --------------------------------------------------------------------------
229 " "*Header to place in Desktop file.")
231 (defvar desktop-delay-hook nil
232 "Hooks run after all buffers are loaded; intended for internal use.")
234 ;; ----------------------------------------------------------------------------
235 (defun desktop-truncate (l n
)
236 "Truncate LIST to at most N elements destructively."
237 (let ((here (nthcdr (1- n
) l
)))
240 ;; ----------------------------------------------------------------------------
241 (defcustom desktop-clear-preserve-buffers
242 '("*scratch*" "*Messages*")
243 "*Buffer names that `desktop-clear' should not delete."
244 :type
'(repeat string
)
247 (defun desktop-clear ()
249 This kills all buffers except for internal ones
250 and those listed in `desktop-clear-preserve-buffers'."
253 kill-ring-yank-pointer nil
255 search-ring-yank-pointer nil
256 regexp-search-ring nil
257 regexp-search-ring-yank-pointer nil
)
258 (let ((buffers (buffer-list)))
260 (or (member (buffer-name (car buffers
)) desktop-clear-preserve-buffers
)
261 (null (buffer-name (car buffers
)))
262 ;; Don't kill buffers made for internal purposes.
263 (and (not (equal (buffer-name (car buffers
)) ""))
264 (eq (aref (buffer-name (car buffers
)) 0) ?\
))
265 (kill-buffer (car buffers
)))
266 (setq buffers
(cdr buffers
))))
267 (delete-other-windows))
268 ;; ----------------------------------------------------------------------------
269 (add-hook 'kill-emacs-hook
'desktop-kill
)
271 (defun desktop-kill ()
274 (desktop-save desktop-dirname
)
276 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
278 (signal (car err
) (cdr err
)))))))
279 ;; ----------------------------------------------------------------------------
280 (defun desktop-list* (&rest args
)
281 (if (null (cdr args
))
283 (setq args
(nreverse args
))
284 (let ((value (cons (nth 1 args
) (car args
))))
285 (setq args
(cdr (cdr args
)))
287 (setq value
(cons (car args
) value
))
288 (setq args
(cdr args
)))
291 (defun desktop-internal-v2s (val)
292 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
293 TXT is a string that when read and evaluated yields value.
294 QUOTE may be `may' (value may be quoted),
295 `must' (values must be quoted), or nil (value may not be quoted)."
297 ((or (numberp val
) (null val
) (eq t val
))
298 (cons 'may
(prin1-to-string val
)))
300 (let ((copy (copy-sequence val
)))
301 (set-text-properties 0 (length copy
) nil copy
)
302 ;; Get rid of text properties because we cannot read them
303 (cons 'may
(prin1-to-string copy
))))
305 (cons 'must
(prin1-to-string val
)))
310 (let ((res (desktop-internal-v2s el
)))
316 (cons nil
(concat "(vector "
317 (mapconcat (lambda (el)
318 (if (eq (car el
) 'must
)
319 (concat "'" (cdr el
))
324 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
331 (let ((q.txt
(desktop-internal-v2s (car p
))))
332 (or anynil
(setq anynil
(null (car q.txt
))))
333 (setq newlist
(cons q.txt newlist
)))
336 (let ((last (desktop-internal-v2s p
))
338 (or anynil
(setq anynil
(null (car last
))))
340 (setq newlist
(cons '(must .
".") newlist
)))
342 (setq newlist
(cons last newlist
))))
343 (setq newlist
(nreverse newlist
))
346 (concat (if use-list
* "(desktop-list* " "(list ")
347 (mapconcat (lambda (el)
348 (if (eq (car el
) 'must
)
349 (concat "'" (cdr el
))
355 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
357 (cons nil
(concat "(symbol-function '"
358 (substring (prin1-to-string val
) 7 -
1)
361 (let ((pos (prin1-to-string (marker-position val
)))
362 (buf (prin1-to-string (buffer-name (marker-buffer val
)))))
363 (cons nil
(concat "(let ((mk (make-marker)))"
364 " (add-hook 'desktop-delay-hook"
365 " (list 'lambda '() (list 'set-marker mk "
366 pos
" (get-buffer " buf
")))) mk)"))))
368 (cons 'may
"\"Unprintable entity\""))))
370 (defun desktop-value-to-string (val)
371 "Convert VALUE to a string that when read evaluates to the same value.
372 Not all types of values are supported."
373 (let* ((print-escape-newlines t
)
374 (float-output-format nil
)
375 (quote.txt
(desktop-internal-v2s val
))
376 (quote (car quote.txt
))
377 (txt (cdr quote.txt
)))
381 ;; ----------------------------------------------------------------------------
382 (defun desktop-outvar (varspec)
383 "Output a setq statement for variable VAR to the desktop file.
384 The argument VARSPEC may be the variable name VAR (a symbol),
385 or a cons cell of the form (VAR . MAX-SIZE),
386 which means to truncate VAR's value to at most MAX-SIZE elements
387 \(if the value is a list) before saving the value."
390 (setq var
(car varspec
) size
(cdr varspec
))
394 (if (and (integerp size
)
397 (desktop-truncate (eval var
) size
))
401 (desktop-value-to-string (symbol-value var
))
403 ;; ----------------------------------------------------------------------------
404 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
405 "Return t if the desktop should record a particular buffer for next startup.
406 FILENAME is the visited file name, BUFNAME is the buffer name, and
407 MODE is the major mode."
408 (let ((case-fold-search nil
))
410 (not (string-match desktop-buffers-not-to-save bufname
))
411 (not (string-match desktop-files-not-to-save filename
)))
412 (and (eq mode
'dired-mode
)
414 (set-buffer (get-buffer bufname
))
415 (not (string-match desktop-files-not-to-save
416 default-directory
))))
418 (memq mode
'(Info-mode rmail-mode
))))))
419 ;; ----------------------------------------------------------------------------
420 (defun desktop-save (dirname)
421 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
422 (interactive "DDirectory to save desktop file in: ")
423 (run-hooks 'desktop-save-hook
)
425 (let ((filename (expand-file-name
426 (concat dirname desktop-basefilename
)))
429 (function (lambda (b)
435 (list ; list explaining minor modes
436 (not (null auto-fill-function
)))
438 (list (mark t
) mark-active
)
440 (cond ((eq major-mode
'Info-mode
)
441 (list Info-current-file
443 ((eq major-mode
'dired-mode
)
445 (expand-file-name dired-directory
)
450 dired-subdir-alist
))))))
451 (let ((locals desktop-locals-to-save
)
452 (loclist (buffer-local-variables))
455 (let ((here (assq (car locals
) loclist
)))
457 (setq ll
(cons here ll
))
458 (if (member (car locals
) loclist
)
459 (setq ll
(cons (car locals
) ll
)))))
460 (setq locals
(cdr locals
)))
464 (buf (get-buffer-create "*desktop*")))
468 (insert desktop-header
469 ";; Created " (current-time-string) "\n"
470 ";; Emacs version " emacs-version
"\n\n"
471 ";; Global section:\n")
472 (mapcar (function desktop-outvar
) desktop-globals-to-save
)
473 (if (memq 'kill-ring desktop-globals-to-save
)
474 (insert "(setq kill-ring-yank-pointer (nthcdr "
476 (- (length kill-ring
) (length kill-ring-yank-pointer
)))
479 (insert "\n;; Buffer section:\n")
481 (function (lambda (l)
482 (if (apply 'desktop-save-buffer-p l
)
484 (insert desktop-create-buffer-form
)
486 (function (lambda (e)
488 (desktop-value-to-string e
))))
492 (setq default-directory dirname
)
493 (if (file-exists-p filename
) (delete-file filename
))
494 (write-region (point-min) (point-max) filename nil
'nomessage
)))
495 (setq desktop-dirname dirname
))
496 ;; ----------------------------------------------------------------------------
497 (defun desktop-remove ()
498 "Delete the Desktop file and inactivate the desktop system."
501 (let ((filename (concat desktop-dirname desktop-basefilename
)))
502 (setq desktop-dirname nil
)
503 (if (file-exists-p filename
)
504 (delete-file filename
)))))
505 ;; ----------------------------------------------------------------------------
506 (defun desktop-read ()
507 "Read the Desktop file and the files it specifies.
508 This is a no-op when Emacs is running in batch mode."
512 (let ((dirs '("./" "~/")))
514 (not (file-exists-p (expand-file-name
517 (setq dirs
(cdr dirs
)))
518 (setq desktop-dirname
(and dirs
(expand-file-name (car dirs
))))
521 (load (expand-file-name desktop-basefilename desktop-dirname
)
523 (run-hooks 'desktop-delay-hook
)
524 (setq desktop-delay-hook nil
)
525 (message "Desktop loaded."))
527 ;; ----------------------------------------------------------------------------
528 (defun desktop-load-default ()
529 "Load the `default' start-up library manually.
530 Also inhibit further loading of it. Call this from your `.emacs' file
531 to provide correct modes for autoloaded files."
532 (if (not inhibit-default-init
) ; safety check
535 (setq inhibit-default-init t
))))
536 ;; ----------------------------------------------------------------------------
537 ;; Note: the following functions use the dynamic variable binding in Lisp.
539 (defun desktop-buffer-info () "Load an info file."
540 (if (eq 'Info-mode desktop-buffer-major-mode
)
543 (Info-find-node (nth 0 desktop-buffer-misc
) (nth 1 desktop-buffer-misc
))
545 ;; ----------------------------------------------------------------------------
546 (defun desktop-buffer-rmail () "Load an RMAIL file."
547 (if (eq 'rmail-mode desktop-buffer-major-mode
)
548 (condition-case error
549 (progn (rmail-input desktop-buffer-file-name
) t
)
551 (kill-buffer (current-buffer))
553 ;; ----------------------------------------------------------------------------
554 (defun desktop-buffer-mh () "Load a folder in the mh system."
555 (if (eq 'mh-folder-mode desktop-buffer-major-mode
)
559 (mh-visit-folder desktop-buffer-name
)
561 ;; ----------------------------------------------------------------------------
562 (defun desktop-buffer-dired () "Load a directory using dired."
563 (if (eq 'dired-mode desktop-buffer-major-mode
)
564 (if (file-directory-p (file-name-directory (car desktop-buffer-misc
)))
566 (dired (car desktop-buffer-misc
))
567 (mapcar 'dired-insert-subdir
(cdr desktop-buffer-misc
))
569 (message "Directory %s no longer exists." (car desktop-buffer-misc
))
572 ;; ----------------------------------------------------------------------------
573 (defun desktop-buffer-file () "Load a file."
574 (if desktop-buffer-file-name
575 (if (or (file-exists-p desktop-buffer-file-name
)
576 (and desktop-missing-file-warning
578 "File \"%s\" no longer exists. Re-create? "
579 desktop-buffer-file-name
))))
580 (progn (find-file desktop-buffer-file-name
) t
)
582 ;; ----------------------------------------------------------------------------
583 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
585 (defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name
586 desktop-buffer-major-mode
587 mim pt mk ro desktop-buffer-misc
&optional locals
)
588 (let ((hlist desktop-buffer-handlers
)
591 (while (and (not result
) hlist
)
592 (setq handler
(car hlist
))
593 (setq result
(funcall handler
))
594 (setq hlist
(cdr hlist
)))
597 (if (not (equal (buffer-name) desktop-buffer-name
))
598 (rename-buffer desktop-buffer-name
))
599 (auto-fill-mode (if (nth 0 mim
) 1 0))
604 (setq mark-active
(car (cdr mk
))))
606 ;; Never override file system if the file really is read-only marked.
607 (if ro
(setq buffer-read-only ro
))
609 (let ((this (car locals
)))
611 ;; an entry of this form `(symbol . value)'
613 (make-local-variable (car this
))
614 (set (car this
) (cdr this
)))
615 ;; an entry of the form `symbol'
616 (make-local-variable this
)
618 (setq locals
(cdr locals
)))
621 ;; Backward compatibility -- update parameters to 205 standards.
622 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
623 desktop-buffer-major-mode
624 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
625 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
626 desktop-buffer-major-mode
(cdr mim
) pt mk ro
628 (list (cons 'truncate-lines tl
)
629 (cons 'fill-column fc
)
630 (cons 'case-fold-search cfs
)
631 (cons 'case-replace cr
)
632 (cons 'overwrite-mode
(car mim
)))))
633 ;; ----------------------------------------------------------------------------
635 ;; If the user set desktop-enable to t with Custom,
636 ;; do the rest of what it takes to use desktop,
637 ;; but do it after finishing loading the init file.
638 (add-hook 'after-init-hook
641 (desktop-load-default)
646 ;; desktop.el ends here.