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