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