(Man-fontify-manpage, Man-cleanup-manpage):
[emacs.git] / lisp / desktop.el
blobaa881af5d031493673b08f3e22d00638b477bc55
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)
14 ;; any later version.
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.
25 ;;; Commentary:
27 ;; Save the Desktop, i.e.,
28 ;; - some global variables
29 ;; - the list of buffers with associated files. For each buffer also
30 ;; - the major mode
31 ;; - the default directory
32 ;; - the point
33 ;; - the mark & mark-active
34 ;; - buffer-read-only
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):
40 ;; (load "desktop")
41 ;; (desktop-load-default)
42 ;; (desktop-read)
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
48 ;; the line
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
56 ;; '(lambda ()
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 ;; ---------------------------------------------------------------------------
88 ;; TODO:
90 ;; Save window configuration.
91 ;; Recognize more minor modes.
92 ;; Save mark rings.
93 ;; Start-up with buffer-menu???
95 ;;; Code:
97 ;; Make the compilation more silent
98 (eval-when-compile
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
108 ".emacs.desktop")
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
118 ;; 'kill-ring
119 'tags-file-name
120 'tags-table-list
121 'search-ring
122 'regexp-search-ring
123 'register-alist
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.
134 'truncate-lines
135 'case-fold-search
136 'case-replace
137 'fill-column
138 'overwrite-mode
139 'change-log-default-name
140 'line-number-mode
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
155 "^/[^/:]*:"
156 "Regexp identifying files whose buffers are to be excluded from saving.")
158 (defvar desktop-buffer-handlers
159 '(desktop-buffer-dired
160 desktop-buffer-rmail
161 desktop-buffer-mh
162 desktop-buffer-info
163 desktop-buffer-file)
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
176 the like shorter.")
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)))
193 (if (consp here)
194 (setcdr here nil))))
195 ;; ----------------------------------------------------------------------------
196 (defun desktop-clear () "Empty the Desktop."
197 (interactive)
198 (setq kill-ring nil
199 kill-ring-yank-pointer nil
200 search-ring 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 ()
210 (if desktop-dirname
211 (condition-case err
212 (desktop-save desktop-dirname)
213 (file-error
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)."
223 (cond
224 ((or (numberp val) (null val) (eq t val))
225 (cons 'may (prin1-to-string val)))
226 ((stringp 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))))
231 ((symbolp val)
232 (cons 'must (prin1-to-string val)))
233 ((vectorp val)
234 (let* ((special nil)
235 (pass1 (mapcar
236 (lambda (el)
237 (let ((res (desktop-internal-v2s el)))
238 (if (null (car res))
239 (setq special t))
240 res))
241 val)))
242 (if special
243 (cons nil (concat "(vector "
244 (mapconcat (lambda (el)
245 (if (eq (car el) 'must)
246 (concat "'" (cdr el))
247 (cdr el)))
248 pass1
249 " ")
250 ")"))
251 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
252 ((consp val)
253 (let ((p val)
254 newlist
255 anynil)
256 (while (consp p)
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)))
260 (setq p (cdr p)))
261 (if p
262 (let ((last (desktop-internal-v2s p))
263 (el (car newlist)))
264 (setcar newlist
265 (if (or anynil (setq anynil (null (car last))))
266 (cons nil
267 (concat "(cons "
268 (if (eq (car el) 'must) "'" "")
269 (cdr el)
271 (if (eq (car last) 'must) "'" "")
272 (cdr last)
273 ")"))
274 (cons 'must
275 (concat (cdr el) " . " (cdr last)))))))
276 (setq newlist (nreverse newlist))
277 (if anynil
278 (cons nil
279 (concat "(list "
280 (mapconcat (lambda (el)
281 (if (eq (car el) 'must)
282 (concat "'" (cdr el))
283 (cdr el)))
284 newlist
285 " ")
286 ")"))
287 (cons 'must
288 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
289 ((subrp val)
290 (cons nil (concat "(symbol-function '"
291 (substring (prin1-to-string val) 7 -1)
292 ")")))
293 ((markerp val)
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)"))))
300 (t ; save as text
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)))
311 (if (eq quote 'must)
312 (concat "'" txt)
313 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."
321 (let (var size)
322 (if (consp varspec)
323 (setq var (car varspec) size (cdr varspec))
324 (setq var varspec))
325 (if (boundp var)
326 (progn
327 (if (and (integerp size)
328 (> size 0)
329 (listp (eval var)))
330 (desktop-truncate (eval var) size))
331 (insert "(setq "
332 (symbol-name var)
334 (desktop-value-to-string (symbol-value var))
335 ")\n")))))
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))
342 (or (and filename
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)
346 (save-excursion
347 (set-buffer (get-buffer bufname))
348 (not (string-match desktop-files-not-to-save
349 default-directory))))
350 (and (null filename)
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)
357 (save-excursion
358 (let ((filename (expand-file-name
359 (concat dirname desktop-basefilename)))
360 (info (nreverse
361 (mapcar
362 (function (lambda (b)
363 (set-buffer b)
364 (list
365 (buffer-file-name)
366 (buffer-name)
367 major-mode
368 (list ; list explaining minor modes
369 (not (null auto-fill-function)))
370 (point)
371 (list (mark t) mark-active)
372 buffer-read-only
373 (cond ((eq major-mode 'Info-mode)
374 (list Info-current-file
375 Info-current-node))
376 ((eq major-mode 'dired-mode)
377 (cons
378 (expand-file-name dired-directory)
379 (cdr
380 (nreverse
381 (mapcar
382 (function car)
383 dired-subdir-alist))))))
384 (let ((locals desktop-locals-to-save)
385 (loclist (buffer-local-variables))
386 (ll))
387 (while locals
388 (let ((here (assq (car locals) loclist)))
389 (if here
390 (setq ll (cons here ll))
391 (if (member (car locals) loclist)
392 (setq ll (cons (car locals) ll)))))
393 (setq locals (cdr locals)))
396 (buffer-list))))
397 (buf (get-buffer-create "*desktop*")))
398 (set-buffer buf)
399 (erase-buffer)
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 "
408 (int-to-string
409 (- (length kill-ring) (length kill-ring-yank-pointer)))
410 " kill-ring))\n"))
412 (insert "\n;; Buffer section:\n")
413 (mapcar
414 (function (lambda (l)
415 (if (apply 'desktop-save-buffer-p l)
416 (progn
417 (insert desktop-create-buffer-form)
418 (mapcar
419 (function (lambda (e)
420 (insert "\n "
421 (desktop-value-to-string e))))
423 (insert ")\n\n")))))
424 info)
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."
432 (interactive)
433 (if desktop-dirname
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."
441 (interactive)
442 (let ((filename))
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)))
448 (if desktop-dirname
449 (progn
450 (load (concat desktop-dirname desktop-basefilename) t t t)
451 (run-hooks 'desktop-delay-hook)
452 (message "Desktop loaded."))
453 (desktop-clear))))
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
460 (progn
461 (load "default" t t)
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)
468 (progn
469 (require 'info)
470 (Info-find-node (nth 0 misc) (nth 1 misc))
471 t)))
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)
477 (file-locked
478 (kill-buffer (current-buffer))
479 'ignored))))
480 ;; ----------------------------------------------------------------------------
481 (defun desktop-buffer-mh () "Load a folder in the mh system."
482 (if (eq 'mh-folder-mode mam)
483 (progn
484 (require 'mh-e)
485 (mh-find-path)
486 (mh-visit-folder bn)
487 t)))
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)))
492 (progn
493 (dired (car misc))
494 (mapcar 'dired-insert-subdir (cdr misc))
496 (message "Directory %s no longer exists." (car misc))
497 (sit-for 1)
498 'ignored)))
499 ;; ----------------------------------------------------------------------------
500 (defun desktop-buffer-file () "Load a file."
501 (if fn
502 (if (or (file-exists-p fn)
503 (and desktop-missing-file-warning
504 (y-or-n-p (format
505 "File \"%s\" no longer exists. Re-create? "
506 fn))))
507 (progn (find-file fn) t)
508 'ignored)))
509 ;; ----------------------------------------------------------------------------
510 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
511 ;; only.
512 (defun desktop-create-buffer (ver fn bn mam mim pt mk ro misc &optional locals)
513 (let ((hlist desktop-buffer-handlers)
514 (result)
515 (handler))
516 (while (and (not result) hlist)
517 (setq handler (car hlist))
518 (setq result (funcall handler))
519 (setq hlist (cdr hlist)))
520 (if (eq result t)
521 (progn
522 (if (not (equal (buffer-name) bn))
523 (rename-buffer bn))
524 (auto-fill-mode (if (nth 0 mim) 1 0))
525 (goto-char pt)
526 (if (consp mk)
527 (progn
528 (set-mark (car mk))
529 (setq mark-active (car (cdr mk))))
530 (set-mark mk))
531 ;; Never override file system if the file really is read-only marked.
532 (if ro (setq buffer-read-only ro))
533 (while locals
534 (let ((this (car locals)))
535 (if (consp this)
536 ;; an entry of this form `(symbol . value)'
537 (progn
538 (make-local-variable (car this))
539 (set (car this) (cdr this)))
540 ;; an entry of the form `symbol'
541 (make-local-variable this)
542 (makunbound this)))
543 (setq locals (cdr locals)))
544 ))))
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 ;; ----------------------------------------------------------------------------
555 (provide 'desktop)
557 ;; desktop.el ends here.