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