]> code.delx.au - gnu-emacs/blob - lisp/desktop.el
(find-file-noselect): If after-find-file switches buffers,
[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-internal-v2s (val)
221 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
222 TXT is a string that when read and evaluated yields value.
223 QUOTE may be `may' (value may be quoted),
224 `must' (values must be quoted), or nil (value may not be quoted)."
225 (cond
226 ((or (numberp val) (null val) (eq t val))
227 (cons 'may (prin1-to-string val)))
228 ((stringp val)
229 (let ((copy (copy-sequence val)))
230 (set-text-properties 0 (length copy) nil copy)
231 ;; Get rid of text properties because we cannot read them
232 (cons 'may (prin1-to-string copy))))
233 ((symbolp val)
234 (cons 'must (prin1-to-string val)))
235 ((vectorp val)
236 (let* ((special nil)
237 (pass1 (mapcar
238 (lambda (el)
239 (let ((res (desktop-internal-v2s el)))
240 (if (null (car res))
241 (setq special t))
242 res))
243 val)))
244 (if special
245 (cons nil (concat "(vector "
246 (mapconcat (lambda (el)
247 (if (eq (car el) 'must)
248 (concat "'" (cdr el))
249 (cdr el)))
250 pass1
251 " ")
252 ")"))
253 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
254 ((consp val)
255 (let ((p val)
256 newlist
257 anynil)
258 (while (consp p)
259 (let ((q.txt (desktop-internal-v2s (car p))))
260 (or anynil (setq anynil (null (car q.txt))))
261 (setq newlist (cons q.txt newlist)))
262 (setq p (cdr p)))
263 (if p
264 (let ((last (desktop-internal-v2s p))
265 (el (car newlist)))
266 (setcar newlist
267 (if (or anynil (setq anynil (null (car last))))
268 (cons nil
269 (concat "(cons "
270 (if (eq (car el) 'must) "'" "")
271 (cdr el)
272 " "
273 (if (eq (car last) 'must) "'" "")
274 (cdr last)
275 ")"))
276 (cons 'must
277 (concat (cdr el) " . " (cdr last)))))))
278 (setq newlist (nreverse newlist))
279 (if anynil
280 (cons nil
281 (concat "(list "
282 (mapconcat (lambda (el)
283 (if (eq (car el) 'must)
284 (concat "'" (cdr el))
285 (cdr el)))
286 newlist
287 " ")
288 ")"))
289 (cons 'must
290 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
291 ((subrp val)
292 (cons nil (concat "(symbol-function '"
293 (substring (prin1-to-string val) 7 -1)
294 ")")))
295 ((markerp val)
296 (let ((pos (prin1-to-string (marker-position val)))
297 (buf (prin1-to-string (buffer-name (marker-buffer val)))))
298 (cons nil (concat "(let ((mk (make-marker)))"
299 " (add-hook 'desktop-delay-hook"
300 " (list 'lambda '() (list 'set-marker mk "
301 pos " (get-buffer " buf ")))) mk)"))))
302 (t ; save as text
303 (cons 'may "\"Unprintable entity\""))))
304
305 (defun desktop-value-to-string (val)
306 "Convert VALUE to a string that when read evaluates to the same value.
307 Not all types of values are supported."
308 (let* ((print-escape-newlines t)
309 (float-output-format nil)
310 (quote.txt (desktop-internal-v2s val))
311 (quote (car quote.txt))
312 (txt (cdr quote.txt)))
313 (if (eq quote 'must)
314 (concat "'" txt)
315 txt)))
316 ;; ----------------------------------------------------------------------------
317 (defun desktop-outvar (varspec)
318 "Output a setq statement for variable VAR to the desktop file.
319 The argument VARSPEC may be the variable name VAR (a symbol),
320 or a cons cell of the form (VAR . MAX-SIZE),
321 which means to truncate VAR's value to at most MAX-SIZE elements
322 \(if the value is a list) before saving the value."
323 (let (var size)
324 (if (consp varspec)
325 (setq var (car varspec) size (cdr varspec))
326 (setq var varspec))
327 (if (boundp var)
328 (progn
329 (if (and (integerp size)
330 (> size 0)
331 (listp (eval var)))
332 (desktop-truncate (eval var) size))
333 (insert "(setq "
334 (symbol-name var)
335 " "
336 (desktop-value-to-string (symbol-value var))
337 ")\n")))))
338 ;; ----------------------------------------------------------------------------
339 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
340 "Return t if the desktop should record a particular buffer for next startup.
341 FILENAME is the visited file name, BUFNAME is the buffer name, and
342 MODE is the major mode."
343 (let ((case-fold-search nil))
344 (or (and filename
345 (not (string-match desktop-buffers-not-to-save bufname))
346 (not (string-match desktop-files-not-to-save filename)))
347 (and (eq mode 'dired-mode)
348 (save-excursion
349 (set-buffer (get-buffer bufname))
350 (not (string-match desktop-files-not-to-save
351 default-directory))))
352 (and (null filename)
353 (memq mode '(Info-mode rmail-mode))))))
354 ;; ----------------------------------------------------------------------------
355 (defun desktop-save (dirname)
356 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
357 (interactive "DDirectory to save desktop file in: ")
358 (run-hooks 'desktop-save-hook)
359 (save-excursion
360 (let ((filename (expand-file-name
361 (concat dirname desktop-basefilename)))
362 (info (nreverse
363 (mapcar
364 (function (lambda (b)
365 (set-buffer b)
366 (list
367 (buffer-file-name)
368 (buffer-name)
369 major-mode
370 (list ; list explaining minor modes
371 (not (null auto-fill-function)))
372 (point)
373 (list (mark t) mark-active)
374 buffer-read-only
375 (cond ((eq major-mode 'Info-mode)
376 (list Info-current-file
377 Info-current-node))
378 ((eq major-mode 'dired-mode)
379 (cons
380 (expand-file-name dired-directory)
381 (cdr
382 (nreverse
383 (mapcar
384 (function car)
385 dired-subdir-alist))))))
386 (let ((locals desktop-locals-to-save)
387 (loclist (buffer-local-variables))
388 (ll))
389 (while locals
390 (let ((here (assq (car locals) loclist)))
391 (if here
392 (setq ll (cons here ll))
393 (if (member (car locals) loclist)
394 (setq ll (cons (car locals) ll)))))
395 (setq locals (cdr locals)))
396 ll)
397 )))
398 (buffer-list))))
399 (buf (get-buffer-create "*desktop*")))
400 (set-buffer buf)
401 (erase-buffer)
402
403 (insert desktop-header
404 ";; Created " (current-time-string) "\n"
405 ";; Emacs version " emacs-version "\n\n"
406 ";; Global section:\n")
407 (mapcar (function desktop-outvar) desktop-globals-to-save)
408 (if (memq 'kill-ring desktop-globals-to-save)
409 (insert "(setq kill-ring-yank-pointer (nthcdr "
410 (int-to-string
411 (- (length kill-ring) (length kill-ring-yank-pointer)))
412 " kill-ring))\n"))
413
414 (insert "\n;; Buffer section:\n")
415 (mapcar
416 (function (lambda (l)
417 (if (apply 'desktop-save-buffer-p l)
418 (progn
419 (insert desktop-create-buffer-form)
420 (mapcar
421 (function (lambda (e)
422 (insert "\n "
423 (desktop-value-to-string e))))
424 l)
425 (insert ")\n\n")))))
426 info)
427 (setq default-directory dirname)
428 (if (file-exists-p filename) (delete-file filename))
429 (write-region (point-min) (point-max) filename nil 'nomessage)))
430 (setq desktop-dirname dirname))
431 ;; ----------------------------------------------------------------------------
432 (defun desktop-remove ()
433 "Delete the Desktop file and inactivate the desktop system."
434 (interactive)
435 (if desktop-dirname
436 (let ((filename (concat desktop-dirname desktop-basefilename)))
437 (setq desktop-dirname nil)
438 (if (file-exists-p filename)
439 (delete-file filename)))))
440 ;; ----------------------------------------------------------------------------
441 (defun desktop-read ()
442 "Read the Desktop file and the files it specifies.
443 This is a no-op when Emacs is running in batch mode."
444 (interactive)
445 (if noninteractive
446 nil
447 (let ((dirs '("./" "~/")))
448 (while (and dirs
449 (not (file-exists-p (expand-file-name
450 desktop-basefilename
451 (car dirs)))))
452 (setq dirs (cdr dirs)))
453 (setq desktop-dirname (and dirs (expand-file-name (car dirs))))
454 (if desktop-dirname
455 (progn
456 (load (expand-file-name desktop-basefilename desktop-dirname)
457 t t t)
458 (run-hooks 'desktop-delay-hook)
459 (setq desktop-delay-hook nil)
460 (message "Desktop loaded."))
461 (desktop-clear)))))
462 ;; ----------------------------------------------------------------------------
463 (defun desktop-load-default ()
464 "Load the `default' start-up library manually.
465 Also inhibit further loading of it. Call this from your `.emacs' file
466 to provide correct modes for autoloaded files."
467 (if (not inhibit-default-init) ; safety check
468 (progn
469 (load "default" t t)
470 (setq inhibit-default-init t))))
471 ;; ----------------------------------------------------------------------------
472 ;; Note: the following functions use the dynamic variable binding in Lisp.
473 ;;
474 (defun desktop-buffer-info () "Load an info file."
475 (if (eq 'Info-mode mam)
476 (progn
477 (require 'info)
478 (Info-find-node (nth 0 misc) (nth 1 misc))
479 t)))
480 ;; ----------------------------------------------------------------------------
481 (defun desktop-buffer-rmail () "Load an RMAIL file."
482 (if (eq 'rmail-mode mam)
483 (condition-case error
484 (progn (rmail-input fn) t)
485 (file-locked
486 (kill-buffer (current-buffer))
487 'ignored))))
488 ;; ----------------------------------------------------------------------------
489 (defun desktop-buffer-mh () "Load a folder in the mh system."
490 (if (eq 'mh-folder-mode mam)
491 (progn
492 (require 'mh-e)
493 (mh-find-path)
494 (mh-visit-folder bn)
495 t)))
496 ;; ----------------------------------------------------------------------------
497 (defun desktop-buffer-dired () "Load a directory using dired."
498 (if (eq 'dired-mode mam)
499 (if (file-directory-p (file-name-directory (car misc)))
500 (progn
501 (dired (car misc))
502 (mapcar 'dired-insert-subdir (cdr misc))
503 t)
504 (message "Directory %s no longer exists." (car misc))
505 (sit-for 1)
506 'ignored)))
507 ;; ----------------------------------------------------------------------------
508 (defun desktop-buffer-file () "Load a file."
509 (if fn
510 (if (or (file-exists-p fn)
511 (and desktop-missing-file-warning
512 (y-or-n-p (format
513 "File \"%s\" no longer exists. Re-create? "
514 fn))))
515 (progn (find-file fn) t)
516 'ignored)))
517 ;; ----------------------------------------------------------------------------
518 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
519 ;; only.
520 (defun desktop-create-buffer (ver fn bn mam mim pt mk ro misc &optional locals)
521 (let ((hlist desktop-buffer-handlers)
522 (result)
523 (handler))
524 (while (and (not result) hlist)
525 (setq handler (car hlist))
526 (setq result (funcall handler))
527 (setq hlist (cdr hlist)))
528 (if (eq result t)
529 (progn
530 (if (not (equal (buffer-name) bn))
531 (rename-buffer bn))
532 (auto-fill-mode (if (nth 0 mim) 1 0))
533 (goto-char pt)
534 (if (consp mk)
535 (progn
536 (set-mark (car mk))
537 (setq mark-active (car (cdr mk))))
538 (set-mark mk))
539 ;; Never override file system if the file really is read-only marked.
540 (if ro (setq buffer-read-only ro))
541 (while locals
542 (let ((this (car locals)))
543 (if (consp this)
544 ;; an entry of this form `(symbol . value)'
545 (progn
546 (make-local-variable (car this))
547 (set (car this) (cdr this)))
548 ;; an entry of the form `symbol'
549 (make-local-variable this)
550 (makunbound this)))
551 (setq locals (cdr locals)))
552 ))))
553
554 ;; Backward compatibility -- update parameters to 205 standards.
555 (defun desktop-buffer (fn bn mam mim pt mk ro tl fc cfs cr misc)
556 (desktop-create-buffer 205 fn bn mam (cdr mim) pt mk ro misc
557 (list (cons 'truncate-lines tl)
558 (cons 'fill-column fc)
559 (cons 'case-fold-search cfs)
560 (cons 'case-replace cr)
561 (cons 'overwrite-mode (car mim)))))
562 ;; ----------------------------------------------------------------------------
563 (provide 'desktop)
564
565 ;; desktop.el ends here.