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