]> code.delx.au - gnu-emacs/blob - lisp/desktop.el
(desktop-list*): New function.
[gnu-emacs] / lisp / desktop.el
1 ;;; desktop.el --- save partial status of Emacs when killed
2
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Morten Welinder <terra@diku.dk>
6 ;; Keywords: customization
7 ;; Favourite-brand-of-beer: None, I hate beer.
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Save the Desktop, i.e.,
29 ;; - some global variables
30 ;; - the list of buffers with associated files. For each buffer also
31 ;; - the major mode
32 ;; - the default directory
33 ;; - the point
34 ;; - the mark & mark-active
35 ;; - buffer-read-only
36 ;; - some local variables
37
38 ;; To use this, first put these three lines in the bottom of your .emacs
39 ;; file (the later the better):
40 ;;
41 ;; (load "desktop")
42 ;; (desktop-load-default)
43 ;; (desktop-read)
44 ;;
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
49 ;; the line
50 ;;
51 ;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
52 ;;
53 ;; To avoid saving excessive amounts of data you may also wish to add
54 ;; something like the following
55 ;;
56 ;; (add-hook 'kill-emacs-hook
57 ;; '(lambda ()
58 ;; (desktop-truncate search-ring 3)
59 ;; (desktop-truncate regexp-search-ring 3)))
60 ;;
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.
64
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.
73
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.
77
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.
81
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 ;; ---------------------------------------------------------------------------
90 ;; TODO:
91 ;;
92 ;; Save window configuration.
93 ;; Recognize more minor modes.
94 ;; Save mark rings.
95 ;; Start-up with buffer-menu???
96
97 ;;; Code:
98
99 ;; Make the compilation more silent
100 (eval-when-compile
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 ;; ----------------------------------------------------------------------------
107 (defconst desktop-basefilename
108 (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt))
109 "emacs.dsk" ; Ms-Dos does not support multiple dots in file name
110 ".emacs.desktop")
111 "File for Emacs desktop, not including the directory name.")
112
113 (defvar desktop-missing-file-warning t
114 "*If non-nil then desktop warns when a file no longer exists.
115 Otherwise it simply ignores that file.")
116
117 (defvar desktop-globals-to-save
118 (list 'desktop-missing-file-warning
119 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
120 ;; 'kill-ring
121 'tags-file-name
122 'tags-table-list
123 'search-ring
124 'regexp-search-ring
125 'register-alist
126 ;; 'desktop-globals-to-save ; Itself!
127 )
128 "List of global variables to save when killing Emacs.
129 An element may be variable name (a symbol)
130 or a cons cell of the form (VAR . MAX-SIZE),
131 which means to truncate VAR's value to at most MAX-SIZE elements
132 \(if the value is a list) before saving the value.")
133
134 (defvar desktop-locals-to-save
135 (list 'desktop-locals-to-save ; Itself! Think it over.
136 'truncate-lines
137 'case-fold-search
138 'case-replace
139 'fill-column
140 'overwrite-mode
141 'change-log-default-name
142 'line-number-mode
143 )
144 "List of local variables to save for each buffer.
145 The variables are saved only when they really are local.")
146 (make-variable-buffer-local 'desktop-locals-to-save)
147
148 ;; We skip .log files because they are normally temporary.
149 ;; (ftp) files because they require passwords and whatnot.
150 ;; TAGS files to save time (tags-file-name is saved instead).
151 (defvar desktop-buffers-not-to-save
152 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
153 "Regexp identifying buffers that are to be excluded from saving.")
154
155 ;; Skip ange-ftp files
156 (defvar desktop-files-not-to-save
157 "^/[^/:]*:"
158 "Regexp identifying files whose buffers are to be excluded from saving.")
159
160 (defvar desktop-buffer-handlers
161 '(desktop-buffer-dired
162 desktop-buffer-rmail
163 desktop-buffer-mh
164 desktop-buffer-info
165 desktop-buffer-file)
166 "*List of functions to call in order to create a buffer.
167 The functions are called without explicit parameters but may access
168 the the major mode as `mam', the file name as `fn', the buffer name as
169 `bn', the default directory as `dd'. If some function returns non-nil
170 no further functions are called. If the function returns t then the
171 buffer is considered created.")
172
173 (defvar desktop-create-buffer-form "(desktop-create-buffer 205"
174 "Opening of form for creation of new buffers.")
175
176 (defvar desktop-save-hook nil
177 "Hook run before saving the desktop to allow you to cut history lists and
178 the like shorter.")
179 ;; ----------------------------------------------------------------------------
180 (defvar desktop-dirname nil
181 "The directory in which the current desktop file resides.")
182
183 (defconst desktop-header
184 ";; --------------------------------------------------------------------------
185 ;; Desktop File for Emacs
186 ;; --------------------------------------------------------------------------
187 " "*Header to place in Desktop file.")
188
189 (defvar desktop-delay-hook nil
190 "Hooks run after all buffers are loaded; intended for internal use.")
191 ;; ----------------------------------------------------------------------------
192 (defun desktop-truncate (l n)
193 "Truncate LIST to at most N elements destructively."
194 (let ((here (nthcdr (1- n) l)))
195 (if (consp here)
196 (setcdr here nil))))
197 ;; ----------------------------------------------------------------------------
198 (defun desktop-clear () "Empty the Desktop."
199 (interactive)
200 (setq kill-ring nil
201 kill-ring-yank-pointer nil
202 search-ring nil
203 search-ring-yank-pointer nil
204 regexp-search-ring nil
205 regexp-search-ring-yank-pointer nil)
206 (mapcar (function kill-buffer) (buffer-list))
207 (delete-other-windows))
208 ;; ----------------------------------------------------------------------------
209 (add-hook 'kill-emacs-hook 'desktop-kill)
210
211 (defun desktop-kill ()
212 (if desktop-dirname
213 (condition-case err
214 (desktop-save desktop-dirname)
215 (file-error
216 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
217 nil
218 (signal (car err) (cdr err)))))))
219 ;; ----------------------------------------------------------------------------
220 (defun desktop-list* (&rest args)
221 (if (null (cdr args))
222 (car args)
223 (setq args (nreverse args))
224 (let ((value (cons (nth 1 args) (car args))))
225 (setq args (cdr (cdr args)))
226 (while args
227 (setq value (cons (car args) value))
228 (setq args (cdr args)))
229 value)))
230
231 (defun desktop-internal-v2s (val)
232 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
233 TXT is a string that when read and evaluated yields value.
234 QUOTE may be `may' (value may be quoted),
235 `must' (values must be quoted), or nil (value may not be quoted)."
236 (cond
237 ((or (numberp val) (null val) (eq t val))
238 (cons 'may (prin1-to-string val)))
239 ((stringp val)
240 (let ((copy (copy-sequence val)))
241 (set-text-properties 0 (length copy) nil copy)
242 ;; Get rid of text properties because we cannot read them
243 (cons 'may (prin1-to-string copy))))
244 ((symbolp val)
245 (cons 'must (prin1-to-string val)))
246 ((vectorp val)
247 (let* ((special nil)
248 (pass1 (mapcar
249 (lambda (el)
250 (let ((res (desktop-internal-v2s el)))
251 (if (null (car res))
252 (setq special t))
253 res))
254 val)))
255 (if special
256 (cons nil (concat "(vector "
257 (mapconcat (lambda (el)
258 (if (eq (car el) 'must)
259 (concat "'" (cdr el))
260 (cdr el)))
261 pass1
262 " ")
263 ")"))
264 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
265 ((consp val)
266 (let ((p val)
267 newlist
268 use-list*
269 anynil)
270 (while (consp p)
271 (let ((q.txt (desktop-internal-v2s (car p))))
272 (or anynil (setq anynil (null (car q.txt))))
273 (setq newlist (cons q.txt newlist)))
274 (setq p (cdr p)))
275 (if p
276 (let ((last (desktop-internal-v2s p))
277 (el (car newlist)))
278 (or anynil (setq anynil (null (car last))))
279 (or anynil
280 (setq newlist (cons '(must . ".") newlist)))
281 (setq use-list* t)
282 (setq newlist (cons last newlist))))
283 (setq newlist (nreverse newlist))
284 (if anynil
285 (cons nil
286 (concat (if use-list* "(desktop-list* " "(list ")
287 (mapconcat (lambda (el)
288 (if (eq (car el) 'must)
289 (concat "'" (cdr el))
290 (cdr el)))
291 newlist
292 " ")
293 ")"))
294 (cons 'must
295 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
296 ((subrp val)
297 (cons nil (concat "(symbol-function '"
298 (substring (prin1-to-string val) 7 -1)
299 ")")))
300 ((markerp val)
301 (let ((pos (prin1-to-string (marker-position val)))
302 (buf (prin1-to-string (buffer-name (marker-buffer val)))))
303 (cons nil (concat "(let ((mk (make-marker)))"
304 " (add-hook 'desktop-delay-hook"
305 " (list 'lambda '() (list 'set-marker mk "
306 pos " (get-buffer " buf ")))) mk)"))))
307 (t ; save as text
308 (cons 'may "\"Unprintable entity\""))))
309
310 (defun desktop-value-to-string (val)
311 "Convert VALUE to a string that when read evaluates to the same value.
312 Not all types of values are supported."
313 (let* ((print-escape-newlines t)
314 (float-output-format nil)
315 (quote.txt (desktop-internal-v2s val))
316 (quote (car quote.txt))
317 (txt (cdr quote.txt)))
318 (if (eq quote 'must)
319 (concat "'" txt)
320 txt)))
321 ;; ----------------------------------------------------------------------------
322 (defun desktop-outvar (varspec)
323 "Output a setq statement for variable VAR to the desktop file.
324 The argument VARSPEC may be the variable name VAR (a symbol),
325 or a cons cell of the form (VAR . MAX-SIZE),
326 which means to truncate VAR's value to at most MAX-SIZE elements
327 \(if the value is a list) before saving the value."
328 (let (var size)
329 (if (consp varspec)
330 (setq var (car varspec) size (cdr varspec))
331 (setq var varspec))
332 (if (boundp var)
333 (progn
334 (if (and (integerp size)
335 (> size 0)
336 (listp (eval var)))
337 (desktop-truncate (eval var) size))
338 (insert "(setq "
339 (symbol-name var)
340 " "
341 (desktop-value-to-string (symbol-value var))
342 ")\n")))))
343 ;; ----------------------------------------------------------------------------
344 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
345 "Return t if the desktop should record a particular buffer for next startup.
346 FILENAME is the visited file name, BUFNAME is the buffer name, and
347 MODE is the major mode."
348 (let ((case-fold-search nil))
349 (or (and filename
350 (not (string-match desktop-buffers-not-to-save bufname))
351 (not (string-match desktop-files-not-to-save filename)))
352 (and (eq mode 'dired-mode)
353 (save-excursion
354 (set-buffer (get-buffer bufname))
355 (not (string-match desktop-files-not-to-save
356 default-directory))))
357 (and (null filename)
358 (memq mode '(Info-mode rmail-mode))))))
359 ;; ----------------------------------------------------------------------------
360 (defun desktop-save (dirname)
361 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
362 (interactive "DDirectory to save desktop file in: ")
363 (run-hooks 'desktop-save-hook)
364 (save-excursion
365 (let ((filename (expand-file-name
366 (concat dirname desktop-basefilename)))
367 (info (nreverse
368 (mapcar
369 (function (lambda (b)
370 (set-buffer b)
371 (list
372 (buffer-file-name)
373 (buffer-name)
374 major-mode
375 (list ; list explaining minor modes
376 (not (null auto-fill-function)))
377 (point)
378 (list (mark t) mark-active)
379 buffer-read-only
380 (cond ((eq major-mode 'Info-mode)
381 (list Info-current-file
382 Info-current-node))
383 ((eq major-mode 'dired-mode)
384 (cons
385 (expand-file-name dired-directory)
386 (cdr
387 (nreverse
388 (mapcar
389 (function car)
390 dired-subdir-alist))))))
391 (let ((locals desktop-locals-to-save)
392 (loclist (buffer-local-variables))
393 (ll))
394 (while locals
395 (let ((here (assq (car locals) loclist)))
396 (if here
397 (setq ll (cons here ll))
398 (if (member (car locals) loclist)
399 (setq ll (cons (car locals) ll)))))
400 (setq locals (cdr locals)))
401 ll)
402 )))
403 (buffer-list))))
404 (buf (get-buffer-create "*desktop*")))
405 (set-buffer buf)
406 (erase-buffer)
407
408 (insert desktop-header
409 ";; Created " (current-time-string) "\n"
410 ";; Emacs version " emacs-version "\n\n"
411 ";; Global section:\n")
412 (mapcar (function desktop-outvar) desktop-globals-to-save)
413 (if (memq 'kill-ring desktop-globals-to-save)
414 (insert "(setq kill-ring-yank-pointer (nthcdr "
415 (int-to-string
416 (- (length kill-ring) (length kill-ring-yank-pointer)))
417 " kill-ring))\n"))
418
419 (insert "\n;; Buffer section:\n")
420 (mapcar
421 (function (lambda (l)
422 (if (apply 'desktop-save-buffer-p l)
423 (progn
424 (insert desktop-create-buffer-form)
425 (mapcar
426 (function (lambda (e)
427 (insert "\n "
428 (desktop-value-to-string e))))
429 l)
430 (insert ")\n\n")))))
431 info)
432 (setq default-directory dirname)
433 (if (file-exists-p filename) (delete-file filename))
434 (write-region (point-min) (point-max) filename nil 'nomessage)))
435 (setq desktop-dirname dirname))
436 ;; ----------------------------------------------------------------------------
437 (defun desktop-remove ()
438 "Delete the Desktop file and inactivate the desktop system."
439 (interactive)
440 (if desktop-dirname
441 (let ((filename (concat desktop-dirname desktop-basefilename)))
442 (setq desktop-dirname nil)
443 (if (file-exists-p filename)
444 (delete-file filename)))))
445 ;; ----------------------------------------------------------------------------
446 (defun desktop-read ()
447 "Read the Desktop file and the files it specifies.
448 This is a no-op when Emacs is running in batch mode."
449 (interactive)
450 (if noninteractive
451 nil
452 (let ((dirs '("./" "~/")))
453 (while (and dirs
454 (not (file-exists-p (expand-file-name
455 desktop-basefilename
456 (car dirs)))))
457 (setq dirs (cdr dirs)))
458 (setq desktop-dirname (and dirs (expand-file-name (car dirs))))
459 (if desktop-dirname
460 (progn
461 (load (expand-file-name desktop-basefilename desktop-dirname)
462 t t t)
463 (run-hooks 'desktop-delay-hook)
464 (setq desktop-delay-hook nil)
465 (message "Desktop loaded."))
466 (desktop-clear)))))
467 ;; ----------------------------------------------------------------------------
468 (defun desktop-load-default ()
469 "Load the `default' start-up library manually.
470 Also inhibit further loading of it. Call this from your `.emacs' file
471 to provide correct modes for autoloaded files."
472 (if (not inhibit-default-init) ; safety check
473 (progn
474 (load "default" t t)
475 (setq inhibit-default-init t))))
476 ;; ----------------------------------------------------------------------------
477 ;; Note: the following functions use the dynamic variable binding in Lisp.
478 ;;
479 (defun desktop-buffer-info () "Load an info file."
480 (if (eq 'Info-mode mam)
481 (progn
482 (require 'info)
483 (Info-find-node (nth 0 misc) (nth 1 misc))
484 t)))
485 ;; ----------------------------------------------------------------------------
486 (defun desktop-buffer-rmail () "Load an RMAIL file."
487 (if (eq 'rmail-mode mam)
488 (condition-case error
489 (progn (rmail-input fn) t)
490 (file-locked
491 (kill-buffer (current-buffer))
492 'ignored))))
493 ;; ----------------------------------------------------------------------------
494 (defun desktop-buffer-mh () "Load a folder in the mh system."
495 (if (eq 'mh-folder-mode mam)
496 (progn
497 (require 'mh-e)
498 (mh-find-path)
499 (mh-visit-folder bn)
500 t)))
501 ;; ----------------------------------------------------------------------------
502 (defun desktop-buffer-dired () "Load a directory using dired."
503 (if (eq 'dired-mode mam)
504 (if (file-directory-p (file-name-directory (car misc)))
505 (progn
506 (dired (car misc))
507 (mapcar 'dired-insert-subdir (cdr misc))
508 t)
509 (message "Directory %s no longer exists." (car misc))
510 (sit-for 1)
511 'ignored)))
512 ;; ----------------------------------------------------------------------------
513 (defun desktop-buffer-file () "Load a file."
514 (if fn
515 (if (or (file-exists-p fn)
516 (and desktop-missing-file-warning
517 (y-or-n-p (format
518 "File \"%s\" no longer exists. Re-create? "
519 fn))))
520 (progn (find-file fn) t)
521 'ignored)))
522 ;; ----------------------------------------------------------------------------
523 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
524 ;; only.
525 (defun desktop-create-buffer (ver fn bn mam mim pt mk ro misc &optional locals)
526 (let ((hlist desktop-buffer-handlers)
527 (result)
528 (handler))
529 (while (and (not result) hlist)
530 (setq handler (car hlist))
531 (setq result (funcall handler))
532 (setq hlist (cdr hlist)))
533 (if (eq result t)
534 (progn
535 (if (not (equal (buffer-name) bn))
536 (rename-buffer bn))
537 (auto-fill-mode (if (nth 0 mim) 1 0))
538 (goto-char pt)
539 (if (consp mk)
540 (progn
541 (set-mark (car mk))
542 (setq mark-active (car (cdr mk))))
543 (set-mark mk))
544 ;; Never override file system if the file really is read-only marked.
545 (if ro (setq buffer-read-only ro))
546 (while locals
547 (let ((this (car locals)))
548 (if (consp this)
549 ;; an entry of this form `(symbol . value)'
550 (progn
551 (make-local-variable (car this))
552 (set (car this) (cdr this)))
553 ;; an entry of the form `symbol'
554 (make-local-variable this)
555 (makunbound this)))
556 (setq locals (cdr locals)))
557 ))))
558
559 ;; Backward compatibility -- update parameters to 205 standards.
560 (defun desktop-buffer (fn bn mam mim pt mk ro tl fc cfs cr misc)
561 (desktop-create-buffer 205 fn bn mam (cdr mim) pt mk ro misc
562 (list (cons 'truncate-lines tl)
563 (cons 'fill-column fc)
564 (cons 'case-fold-search cfs)
565 (cons 'case-replace cr)
566 (cons 'overwrite-mode (car mim)))))
567 ;; ----------------------------------------------------------------------------
568 (provide 'desktop)
569
570 ;; desktop.el ends here.