1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
5 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Keywords: customization
8 ;; Favourite-brand-of-beer: None, I hate beer.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 with 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 ftps.
88 ;; ---------------------------------------------------------------------------
91 ;; Save window configuration.
92 ;; Recognize more minor modes.
94 ;; Start-up with buffer-menu???
98 ;; Make the compilation more silent
100 ;; We use functions from these modules
101 ;; We can't (require 'mh-e) since that wants to load something.
102 (mapcar 'require
'(info dired reporter
)))
103 ;; ----------------------------------------------------------------------------
104 ;; USER OPTIONS -- settings you might want to play with.
105 ;; ----------------------------------------------------------------------------
106 (defconst desktop-basefilename
107 (if (or (eq system-type
'ms-dos
) (eq system-type
'windows-nt
))
108 "emacs.dsk" ; Ms-Dos does not support multiple dots in file name
110 "File for Emacs desktop, not including the directory name.")
112 (defvar desktop-missing-file-warning t
113 "*If non-nil then desktop warns when a file no longer exists.
114 Otherwise it simply ignores that file.")
116 (defvar desktop-globals-to-save
117 (list 'desktop-missing-file-warning
118 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
125 ;; 'desktop-globals-to-save ; Itself!
127 "List of global variables to save when killing Emacs.")
129 (defvar desktop-locals-to-save
130 (list 'desktop-locals-to-save
; Itself! Think it over.
136 'change-log-default-name
139 "List of local variables to save for each buffer.
140 The variables are saved only when they really are local.")
141 (make-variable-buffer-local 'desktop-locals-to-save
)
143 ;; We skip .log files because they are normally temporary.
144 ;; (ftp) files because they require passwords and whatsnot.
145 ;; TAGS files to save time (tags-file-name is saved instead).
146 (defvar desktop-buffers-not-to-save
147 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
148 "Regexp identifying buffers that are to be excluded from saving.")
150 ;; Skip ange-ftp files
151 (defvar desktop-files-not-to-save
153 "Regexp identifying files whose buffers are to be excluded from saving.")
155 (defvar desktop-buffer-handlers
156 '(desktop-buffer-dired
161 "*List of functions to call in order to create a buffer.
162 The functions are called without explicit parameters but may access
163 the the major mode as `mam', the file name as `fn', the buffer name as
164 `bn', the default directory as `dd'. If some function returns non-nil
165 no further functions are called. If the function returns t then the
166 buffer is considered created.")
168 (defvar desktop-create-buffer-form
"(desktop-create-buffer 205"
169 "Opening of form for creation of new buffers.")
171 (defvar desktop-save-hook nil
172 "Hook run before saving the desktop to allow you to cut history lists and
174 ;; ----------------------------------------------------------------------------
175 (defvar desktop-dirname nil
176 "The directory in which the current desktop file resides.")
178 (defconst desktop-header
179 ";; --------------------------------------------------------------------------
180 ;; Desktop File for Emacs
181 ;; --------------------------------------------------------------------------
182 " "*Header to place in Desktop file.")
184 (defvar desktop-delay-hook nil
185 "Hooks run after all buffers are loaded; intended for internal use.")
186 ;; ----------------------------------------------------------------------------
187 (defun desktop-truncate (l n
)
188 "Truncate LIST to at most N elements destructively."
189 (let ((here (nthcdr (1- n
) l
)))
192 ;; ----------------------------------------------------------------------------
193 (defun desktop-clear () "Empty the Desktop."
196 kill-ring-yank-pointer nil
198 search-ring-yank-pointer nil
199 regexp-search-ring nil
200 regexp-search-ring-yank-pointer nil
)
201 (mapcar (function kill-buffer
) (buffer-list))
202 (delete-other-windows))
203 ;; ----------------------------------------------------------------------------
204 (add-hook 'kill-emacs-hook
'desktop-kill
)
206 (defun desktop-kill ()
209 (desktop-save desktop-dirname
)
211 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
213 (signal (car err
) (cdr err
)))))))
214 ;; ----------------------------------------------------------------------------
215 (defun desktop-internal-v2s (val)
216 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
217 TXT is a string that when read and evaluated yields value.
218 QUOTE may be `may' (value may be quoted),
219 `must' (values must be quoted), or nil (value may not be quoted)."
221 ((or (numberp val
) (null val
) (eq t val
))
222 (cons 'may
(prin1-to-string val
)))
224 ;; Get rid of text properties because we cannot read them
225 (cons 'may
(prin1-to-string (format "%s" val
))))
227 (cons 'must
(prin1-to-string val
)))
232 (let ((res (desktop-internal-v2s el
)))
238 (cons nil
(concat "(vector "
239 (mapconcat (lambda (el)
240 (if (eq (car el
) 'must
)
241 (concat "'" (cdr el
))
246 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
252 (let ((q.txt
(desktop-internal-v2s (car p
))))
253 (or anynil
(setq anynil
(null (car q.txt
))))
254 (setq newlist
(cons q.txt newlist
)))
257 (let ((last (desktop-internal-v2s p
))
260 (if (or anynil
(setq anynil
(null (car last
))))
263 (if (eq (car el
) 'must
) "'" "")
266 (if (eq (car last
) 'must
) "'" "")
270 (concat (cdr el
) " . " (cdr last
)))))))
271 (setq newlist
(nreverse newlist
))
275 (mapconcat (lambda (el)
276 (if (eq (car el
) 'must
)
277 (concat "'" (cdr el
))
283 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
285 (cons nil
(concat "(symbol-function '"
286 (substring (prin1-to-string val
) 7 -
1)
289 (let ((pos (prin1-to-string (marker-position val
)))
290 (buf (prin1-to-string (buffer-name (marker-buffer val
)))))
291 (cons nil
(concat "(let ((mk (make-marker)))"
292 " (add-hook 'desktop-delay-hook"
293 " (list 'lambda '() (list 'set-marker mk "
294 pos
" (get-buffer " buf
")))) mk)"))))
296 (cons 'may
"\"Unprintable entity\""))))
298 (defun desktop-value-to-string (val)
299 "Convert VALUE to a string that when read evaluates to the same value.
300 Not all types of values are supported."
301 (let* ((print-escape-newlines t
)
302 (float-output-format nil
)
303 (quote.txt
(desktop-internal-v2s val
))
304 (quote (car quote.txt
))
305 (txt (cdr quote.txt
)))
309 ;; ----------------------------------------------------------------------------
310 (defun desktop-outvar (var)
311 "Output a setq statement for VAR to the desktop file."
316 (desktop-value-to-string (symbol-value var
))
318 ;; ----------------------------------------------------------------------------
319 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
320 "Return t if the desktop should record a particular buffer for next startup.
321 FILENAME is the visited file name, BUFNAME is the buffer name, and
322 MODE is the major mode."
323 (let ((case-fold-search nil
))
325 (not (string-match desktop-buffers-not-to-save bufname
))
326 (not (string-match desktop-files-not-to-save filename
)))
327 (and (eq mode
'dired-mode
)
329 (set-buffer (get-buffer bufname
))
330 (not (string-match desktop-files-not-to-save
331 default-directory
))))
333 (memq mode
'(Info-mode rmail-mode
))))))
334 ;; ----------------------------------------------------------------------------
335 (defun desktop-save (dirname)
336 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
337 (interactive "DDirectory to save desktop file in: ")
338 (run-hooks 'desktop-save-hook
)
340 (let ((filename (expand-file-name
341 (concat dirname desktop-basefilename
)))
344 (function (lambda (b)
350 (list ; list explaining minor modes
351 (not (null auto-fill-function
)))
353 (list (mark t
) mark-active
)
355 (cond ((eq major-mode
'Info-mode
)
356 (list Info-current-file
358 ((eq major-mode
'dired-mode
)
362 dired-subdir-alist
)))
364 (let ((locals desktop-locals-to-save
)
365 (loclist (buffer-local-variables))
368 (let ((here (assq (car locals
) loclist
)))
370 (setq ll
(cons here ll
))
371 (if (member (car locals
) loclist
)
372 (setq ll
(cons (car locals
) ll
)))))
373 (setq locals
(cdr locals
)))
377 (buf (get-buffer-create "*desktop*")))
381 (insert desktop-header
382 ";; Created " (current-time-string) "\n"
383 ";; Emacs version " emacs-version
"\n\n"
384 ";; Global section:\n")
385 (mapcar (function desktop-outvar
) desktop-globals-to-save
)
386 (if (memq 'kill-ring desktop-globals-to-save
)
387 (insert "(setq kill-ring-yank-pointer (nthcdr "
389 (- (length kill-ring
) (length kill-ring-yank-pointer
)))
392 (insert "\n;; Buffer section:\n")
394 (function (lambda (l)
395 (if (apply 'desktop-save-buffer-p l
)
397 (insert desktop-create-buffer-form
)
399 (function (lambda (e)
401 (desktop-value-to-string e
))))
405 (setq default-directory dirname
)
406 (if (file-exists-p filename
) (delete-file filename
))
407 (write-region (point-min) (point-max) filename nil
'nomessage
)))
408 (setq desktop-dirname dirname
))
409 ;; ----------------------------------------------------------------------------
410 (defun desktop-remove ()
411 "Delete the Desktop file and inactivate the desktop system."
414 (let ((filename (concat desktop-dirname desktop-basefilename
)))
415 (setq desktop-dirname nil
)
416 (if (file-exists-p filename
)
417 (delete-file filename
)))))
418 ;; ----------------------------------------------------------------------------
419 (defun desktop-read ()
420 "Read the Desktop file and the files it specifies."
423 (if (file-exists-p (concat "./" desktop-basefilename
))
424 (setq desktop-dirname
(expand-file-name "./"))
425 (if (file-exists-p (concat "~/" desktop-basefilename
))
426 (setq desktop-dirname
(expand-file-name "~/"))
427 (setq desktop-dirname nil
)))
430 (load (concat desktop-dirname desktop-basefilename
) t t t
)
431 (run-hooks 'desktop-delay-hook
)
432 (message "Desktop loaded."))
434 ;; ----------------------------------------------------------------------------
435 (defun desktop-load-default ()
436 "Load the `default' start-up library manually.
437 Also inhibit further loading of it. Call this from your `.emacs' file
438 to provide correct modes for autoloaded files."
439 (if (not inhibit-default-init
) ; safety check
442 (setq inhibit-default-init t
))))
443 ;; ----------------------------------------------------------------------------
444 ;; Note: the following functions use the dynamic variable binding in Lisp.
446 (defun desktop-buffer-info () "Load an info file."
447 (if (eq 'Info-mode mam
)
450 (Info-find-node (nth 0 misc
) (nth 1 misc
))
452 ;; ----------------------------------------------------------------------------
453 (defun desktop-buffer-rmail () "Load an RMAIL file."
454 (if (eq 'rmail-mode mam
)
455 (condition-case error
456 (progn (rmail-input fn
) t
)
458 (kill-buffer (current-buffer))
460 ;; ----------------------------------------------------------------------------
461 (defun desktop-buffer-mh () "Load a folder in the mh system."
462 (if (eq 'mh-folder-mode mam
)
468 ;; ----------------------------------------------------------------------------
469 (defun desktop-buffer-dired () "Load a directory using dired."
470 (if (eq 'dired-mode mam
)
471 (if (file-directory-p (directory-file-name (car misc
)))
474 (mapcar (function dired-maybe-insert-subdir
) (cdr misc
))
476 (message "Directory %s no longer exists." (car misc
))
479 ;; ----------------------------------------------------------------------------
480 (defun desktop-buffer-file () "Load a file."
482 (if (or (file-exists-p fn
)
483 (and desktop-missing-file-warning
485 "File \"%s\" no longer exists. Re-create? "
487 (progn (find-file fn
) t
)
489 ;; ----------------------------------------------------------------------------
490 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
492 (defun desktop-create-buffer (ver fn bn mam mim pt mk ro misc
&optional locals
)
493 (let ((hlist desktop-buffer-handlers
)
496 (while (and (not result
) hlist
)
497 (setq handler
(car hlist
))
498 (setq result
(funcall handler
))
499 (setq hlist
(cdr hlist
)))
502 (if (not (equal (buffer-name) bn
))
504 (auto-fill-mode (if (nth 0 mim
) 1 0))
509 (setq mark-active
(car (cdr mk
))))
511 ;; Never override file system if the file really is read-only marked.
512 (if ro
(setq buffer-read-only ro
))
514 (let ((this (car locals
)))
516 ;; an entry of this form `(symbol . value)'
518 (make-local-variable (car this
))
519 (set (car this
) (cdr this
)))
520 ;; an entry of the form `symbol'
521 (make-local-variable this
)
523 (setq locals
(cdr locals
)))
526 ;; Backward compatibility -- update parameters to 205 standards.
527 (defun desktop-buffer (fn bn mam mim pt mk ro tl fc cfs cr misc
)
528 (desktop-create-buffer 205 fn bn mam
(cdr mim
) pt mk ro misc
529 (list (cons 'truncate-lines tl
)
530 (cons 'fill-column fc
)
531 (cons 'case-fold-search cfs
)
532 (cons 'case-replace cr
)
533 (cons 'overwrite-mode
(car mim
)))))
534 ;; ----------------------------------------------------------------------------
537 ;; desktop.el ends here.