1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995 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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; Save the Desktop, i.e.,
28 ;; - some global variables
29 ;; - the list of buffers with associated files. For each buffer also
31 ;; - the default directory
33 ;; - the mark & mark-active
35 ;; - some local variables
37 ;; To use this, first put these three lines in the bottom of your .emacs
38 ;; file (the later the better):
41 ;; (desktop-load-default)
44 ;; Between the second and the third line you may wish to add something that
45 ;; updates the variables `desktop-globals-to-save' and/or
46 ;; `desktop-locals-to-save'. If for instance you want to save the local
47 ;; variable `foobar' for every buffer in which it is local, you could add
50 ;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
52 ;; To avoid saving excessive amounts of data you may also wish to add
53 ;; something like the following
55 ;; (add-hook 'kill-emacs-hook
57 ;; (desktop-truncate search-ring 3)
58 ;; (desktop-truncate regexp-search-ring 3)))
60 ;; which will make sure that no more than three search items are saved. You
61 ;; must place this line *after* the (load "desktop") line. See also the
62 ;; variable desktop-save-hook.
64 ;; Start Emacs in the root directory of your "project". The desktop saver
65 ;; is inactive by default. You activate it by M-x desktop-save RET. When
66 ;; you exit the next time the above data will be saved. This ensures that
67 ;; all the files you were editing will be reloaded the next time you start
68 ;; Emacs from the same directory and that points will be set where you
69 ;; left them. If you save a desktop file in your home directory it will
70 ;; act as a default desktop when you start Emacs from a directory that
71 ;; doesn't have its own. I never do this, but you may want to.
73 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
74 ;; in your home directory is used for that. Saving global default values
75 ;; for buffers is an example of misuse.
77 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
78 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
79 ;; things you did not mean to keep. Use M-x desktop-clear RET.
81 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
82 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
83 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
84 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
85 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
86 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
87 ;; ---------------------------------------------------------------------------
90 ;; Save window configuration.
91 ;; Recognize more minor modes.
93 ;; Start-up with buffer-menu???
97 ;; Make the compilation more silent
99 ;; We use functions from these modules
100 ;; We can't (require 'mh-e) since that wants to load something.
101 (mapcar 'require
'(info dired reporter
)))
102 ;; ----------------------------------------------------------------------------
103 ;; USER OPTIONS -- settings you might want to play with.
104 ;; ----------------------------------------------------------------------------
105 (defconst desktop-basefilename
106 (if (or (eq system-type
'ms-dos
) (eq system-type
'windows-nt
))
107 "emacs.dsk" ; Ms-Dos does not support multiple dots in file name
109 "File for Emacs desktop, not including the directory name.")
111 (defvar desktop-missing-file-warning t
112 "*If non-nil then desktop warns when a file no longer exists.
113 Otherwise it simply ignores that file.")
115 (defvar desktop-globals-to-save
116 (list 'desktop-missing-file-warning
117 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
124 ;; 'desktop-globals-to-save ; Itself!
126 "List of global variables to save when killing Emacs.
127 An element may be variable name (a symbol)
128 or a cons cell of the form (VAR . MAX-SIZE),
129 which means to truncate VAR's value to at most MAX-SIZE elements
130 \(if the value is a list) before saving the value.")
132 (defvar desktop-locals-to-save
133 (list 'desktop-locals-to-save
; Itself! Think it over.
139 'change-log-default-name
142 "List of local variables to save for each buffer.
143 The variables are saved only when they really are local.")
144 (make-variable-buffer-local 'desktop-locals-to-save
)
146 ;; We skip .log files because they are normally temporary.
147 ;; (ftp) files because they require passwords and whatsnot.
148 ;; TAGS files to save time (tags-file-name is saved instead).
149 (defvar desktop-buffers-not-to-save
150 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
151 "Regexp identifying buffers that are to be excluded from saving.")
153 ;; Skip ange-ftp files
154 (defvar desktop-files-not-to-save
156 "Regexp identifying files whose buffers are to be excluded from saving.")
158 (defvar desktop-buffer-handlers
159 '(desktop-buffer-dired
164 "*List of functions to call in order to create a buffer.
165 The functions are called without explicit parameters but may access
166 the the major mode as `mam', the file name as `fn', the buffer name as
167 `bn', the default directory as `dd'. If some function returns non-nil
168 no further functions are called. If the function returns t then the
169 buffer is considered created.")
171 (defvar desktop-create-buffer-form
"(desktop-create-buffer 205"
172 "Opening of form for creation of new buffers.")
174 (defvar desktop-save-hook nil
175 "Hook run before saving the desktop to allow you to cut history lists and
177 ;; ----------------------------------------------------------------------------
178 (defvar desktop-dirname nil
179 "The directory in which the current desktop file resides.")
181 (defconst desktop-header
182 ";; --------------------------------------------------------------------------
183 ;; Desktop File for Emacs
184 ;; --------------------------------------------------------------------------
185 " "*Header to place in Desktop file.")
187 (defvar desktop-delay-hook nil
188 "Hooks run after all buffers are loaded; intended for internal use.")
189 ;; ----------------------------------------------------------------------------
190 (defun desktop-truncate (l n
)
191 "Truncate LIST to at most N elements destructively."
192 (let ((here (nthcdr (1- n
) l
)))
195 ;; ----------------------------------------------------------------------------
196 (defun desktop-clear () "Empty the Desktop."
199 kill-ring-yank-pointer nil
201 search-ring-yank-pointer nil
202 regexp-search-ring nil
203 regexp-search-ring-yank-pointer nil
)
204 (mapcar (function kill-buffer
) (buffer-list))
205 (delete-other-windows))
206 ;; ----------------------------------------------------------------------------
207 (add-hook 'kill-emacs-hook
'desktop-kill
)
209 (defun desktop-kill ()
212 (desktop-save desktop-dirname
)
214 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
216 (signal (car err
) (cdr err
)))))))
217 ;; ----------------------------------------------------------------------------
218 (defun desktop-internal-v2s (val)
219 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
220 TXT is a string that when read and evaluated yields value.
221 QUOTE may be `may' (value may be quoted),
222 `must' (values must be quoted), or nil (value may not be quoted)."
224 ((or (numberp val
) (null val
) (eq t val
))
225 (cons 'may
(prin1-to-string val
)))
227 (let ((copy (copy-sequence val
)))
228 (set-text-properties 0 (length copy
) nil copy
)
229 ;; Get rid of text properties because we cannot read them
230 (cons 'may
(prin1-to-string copy
))))
232 (cons 'must
(prin1-to-string val
)))
237 (let ((res (desktop-internal-v2s el
)))
243 (cons nil
(concat "(vector "
244 (mapconcat (lambda (el)
245 (if (eq (car el
) 'must
)
246 (concat "'" (cdr el
))
251 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
257 (let ((q.txt
(desktop-internal-v2s (car p
))))
258 (or anynil
(setq anynil
(null (car q.txt
))))
259 (setq newlist
(cons q.txt newlist
)))
262 (let ((last (desktop-internal-v2s p
))
265 (if (or anynil
(setq anynil
(null (car last
))))
268 (if (eq (car el
) 'must
) "'" "")
271 (if (eq (car last
) 'must
) "'" "")
275 (concat (cdr el
) " . " (cdr last
)))))))
276 (setq newlist
(nreverse newlist
))
280 (mapconcat (lambda (el)
281 (if (eq (car el
) 'must
)
282 (concat "'" (cdr el
))
288 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
290 (cons nil
(concat "(symbol-function '"
291 (substring (prin1-to-string val
) 7 -
1)
294 (let ((pos (prin1-to-string (marker-position val
)))
295 (buf (prin1-to-string (buffer-name (marker-buffer val
)))))
296 (cons nil
(concat "(let ((mk (make-marker)))"
297 " (add-hook 'desktop-delay-hook"
298 " (list 'lambda '() (list 'set-marker mk "
299 pos
" (get-buffer " buf
")))) mk)"))))
301 (cons 'may
"\"Unprintable entity\""))))
303 (defun desktop-value-to-string (val)
304 "Convert VALUE to a string that when read evaluates to the same value.
305 Not all types of values are supported."
306 (let* ((print-escape-newlines t
)
307 (float-output-format nil
)
308 (quote.txt
(desktop-internal-v2s val
))
309 (quote (car quote.txt
))
310 (txt (cdr quote.txt
)))
314 ;; ----------------------------------------------------------------------------
315 (defun desktop-outvar (varspec)
316 "Output a setq statement for variable VAR to the desktop file.
317 The argument VARSPEC may be the variable name VAR (a symbol),
318 or a cons cell of the form (VAR . MAX-SIZE),
319 which means to truncate VAR's value to at most MAX-SIZE elements
320 \(if the value is a list) before saving the value."
323 (setq var
(car varspec
) size
(cdr varspec
))
327 (if (and (integerp size
)
330 (desktop-truncate (eval var
) size
))
334 (desktop-value-to-string (symbol-value var
))
336 ;; ----------------------------------------------------------------------------
337 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
338 "Return t if the desktop should record a particular buffer for next startup.
339 FILENAME is the visited file name, BUFNAME is the buffer name, and
340 MODE is the major mode."
341 (let ((case-fold-search nil
))
343 (not (string-match desktop-buffers-not-to-save bufname
))
344 (not (string-match desktop-files-not-to-save filename
)))
345 (and (eq mode
'dired-mode
)
347 (set-buffer (get-buffer bufname
))
348 (not (string-match desktop-files-not-to-save
349 default-directory
))))
351 (memq mode
'(Info-mode rmail-mode
))))))
352 ;; ----------------------------------------------------------------------------
353 (defun desktop-save (dirname)
354 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
355 (interactive "DDirectory to save desktop file in: ")
356 (run-hooks 'desktop-save-hook
)
358 (let ((filename (expand-file-name
359 (concat dirname desktop-basefilename
)))
362 (function (lambda (b)
368 (list ; list explaining minor modes
369 (not (null auto-fill-function
)))
371 (list (mark t
) mark-active
)
373 (cond ((eq major-mode
'Info-mode
)
374 (list Info-current-file
376 ((eq major-mode
'dired-mode
)
378 (expand-file-name dired-directory
)
383 dired-subdir-alist
))))))
384 (let ((locals desktop-locals-to-save
)
385 (loclist (buffer-local-variables))
388 (let ((here (assq (car locals
) loclist
)))
390 (setq ll
(cons here ll
))
391 (if (member (car locals
) loclist
)
392 (setq ll
(cons (car locals
) ll
)))))
393 (setq locals
(cdr locals
)))
397 (buf (get-buffer-create "*desktop*")))
401 (insert desktop-header
402 ";; Created " (current-time-string) "\n"
403 ";; Emacs version " emacs-version
"\n\n"
404 ";; Global section:\n")
405 (mapcar (function desktop-outvar
) desktop-globals-to-save
)
406 (if (memq 'kill-ring desktop-globals-to-save
)
407 (insert "(setq kill-ring-yank-pointer (nthcdr "
409 (- (length kill-ring
) (length kill-ring-yank-pointer
)))
412 (insert "\n;; Buffer section:\n")
414 (function (lambda (l)
415 (if (apply 'desktop-save-buffer-p l
)
417 (insert desktop-create-buffer-form
)
419 (function (lambda (e)
421 (desktop-value-to-string e
))))
425 (setq default-directory dirname
)
426 (if (file-exists-p filename
) (delete-file filename
))
427 (write-region (point-min) (point-max) filename nil
'nomessage
)))
428 (setq desktop-dirname dirname
))
429 ;; ----------------------------------------------------------------------------
430 (defun desktop-remove ()
431 "Delete the Desktop file and inactivate the desktop system."
434 (let ((filename (concat desktop-dirname desktop-basefilename
)))
435 (setq desktop-dirname nil
)
436 (if (file-exists-p filename
)
437 (delete-file filename
)))))
438 ;; ----------------------------------------------------------------------------
439 (defun desktop-read ()
440 "Read the Desktop file and the files it specifies."
443 (if (file-exists-p (concat "./" desktop-basefilename
))
444 (setq desktop-dirname
(expand-file-name "./"))
445 (if (file-exists-p (concat "~/" desktop-basefilename
))
446 (setq desktop-dirname
(expand-file-name "~/"))
447 (setq desktop-dirname nil
)))
450 (load (concat desktop-dirname desktop-basefilename
) t t t
)
451 (run-hooks 'desktop-delay-hook
)
452 (message "Desktop loaded."))
454 ;; ----------------------------------------------------------------------------
455 (defun desktop-load-default ()
456 "Load the `default' start-up library manually.
457 Also inhibit further loading of it. Call this from your `.emacs' file
458 to provide correct modes for autoloaded files."
459 (if (not inhibit-default-init
) ; safety check
462 (setq inhibit-default-init t
))))
463 ;; ----------------------------------------------------------------------------
464 ;; Note: the following functions use the dynamic variable binding in Lisp.
466 (defun desktop-buffer-info () "Load an info file."
467 (if (eq 'Info-mode mam
)
470 (Info-find-node (nth 0 misc
) (nth 1 misc
))
472 ;; ----------------------------------------------------------------------------
473 (defun desktop-buffer-rmail () "Load an RMAIL file."
474 (if (eq 'rmail-mode mam
)
475 (condition-case error
476 (progn (rmail-input fn
) t
)
478 (kill-buffer (current-buffer))
480 ;; ----------------------------------------------------------------------------
481 (defun desktop-buffer-mh () "Load a folder in the mh system."
482 (if (eq 'mh-folder-mode mam
)
488 ;; ----------------------------------------------------------------------------
489 (defun desktop-buffer-dired () "Load a directory using dired."
490 (if (eq 'dired-mode mam
)
491 (if (file-directory-p (file-name-directory (car misc
)))
494 (mapcar 'dired-insert-subdir
(cdr misc
))
496 (message "Directory %s no longer exists." (car misc
))
499 ;; ----------------------------------------------------------------------------
500 (defun desktop-buffer-file () "Load a file."
502 (if (or (file-exists-p fn
)
503 (and desktop-missing-file-warning
505 "File \"%s\" no longer exists. Re-create? "
507 (progn (find-file fn
) t
)
509 ;; ----------------------------------------------------------------------------
510 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
512 (defun desktop-create-buffer (ver fn bn mam mim pt mk ro misc
&optional locals
)
513 (let ((hlist desktop-buffer-handlers
)
516 (while (and (not result
) hlist
)
517 (setq handler
(car hlist
))
518 (setq result
(funcall handler
))
519 (setq hlist
(cdr hlist
)))
522 (if (not (equal (buffer-name) bn
))
524 (auto-fill-mode (if (nth 0 mim
) 1 0))
529 (setq mark-active
(car (cdr mk
))))
531 ;; Never override file system if the file really is read-only marked.
532 (if ro
(setq buffer-read-only ro
))
534 (let ((this (car locals
)))
536 ;; an entry of this form `(symbol . value)'
538 (make-local-variable (car this
))
539 (set (car this
) (cdr this
)))
540 ;; an entry of the form `symbol'
541 (make-local-variable this
)
543 (setq locals
(cdr locals
)))
546 ;; Backward compatibility -- update parameters to 205 standards.
547 (defun desktop-buffer (fn bn mam mim pt mk ro tl fc cfs cr misc
)
548 (desktop-create-buffer 205 fn bn mam
(cdr mim
) pt mk ro misc
549 (list (cons 'truncate-lines tl
)
550 (cons 'fill-column fc
)
551 (cons 'case-fold-search cfs
)
552 (cons 'case-replace cr
)
553 (cons 'overwrite-mode
(car mim
)))))
554 ;; ----------------------------------------------------------------------------
557 ;; desktop.el ends here.