(dired-do-print): Put spaces between lpr switches.
[bpt/emacs.git] / lisp / desktop.el
CommitLineData
6343ed61
RS
1;;; desktop.el --- save partial status of Emacs when killed
2
de9e2828 3;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
6343ed61
RS
4
5;; Author: Morten Welinder <terra@diku.dk>
fa379636 6;; Version: 2.09
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;;
de9e2828 53;; To avoid saving excessive amounts of data you may also with 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.
87;; treese@lcs.mit.edu (Win Treese) for ange-ftp ftps.
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
de9e2828 107 (if (eq system-type 'ms-dos)
6343ed61
RS
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.")
111
0b9bd504 112(defvar desktop-missing-file-warning t
6343ed61
RS
113 "*If non-nil then issue warning if a file no longer exists.
114Otherwise simply ignore the file.")
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
RS
138 )
139 "List of local variables to save for each buffer. The variables are saved
140only 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)
0b9bd504
RS
161 "*List of functions to call in order to create a buffer. The functions are
162called without explicit parameters but may access the the major mode as `mam',
163the 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.
6343ed61 165If the function returns t then the buffer is considered created.")
ec4c6f22
RS
166
167(defvar desktop-create-buffer-form "(desktop-create-buffer 205"
168 "Opening of form for creation of new buffers.")
fa379636
KH
169
170(defvar desktop-save-hook nil
171 "Hook run before saving the desktop to allow you to cut history lists and
172the like shorter.")
0b9bd504
RS
173;; ----------------------------------------------------------------------------
174(defvar desktop-dirname nil
6343ed61
RS
175 "The directory in which the current desktop file resides.")
176
177(defconst desktop-header
0b9bd504
RS
178";; --------------------------------------------------------------------------
179;; Desktop File for Emacs
180;; --------------------------------------------------------------------------
6343ed61 181" "*Header to place in Desktop file.")
de9e2828
RS
182
183(defvar desktop-delay-hook nil
184 "Hooks run after all buffers are loaded; intended for internal use.")
0b9bd504 185;; ----------------------------------------------------------------------------
ec4c6f22
RS
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)
de9e2828 190 (setcdr here nil))))
ec4c6f22 191;; ----------------------------------------------------------------------------
6343ed61
RS
192(defun desktop-clear () "Empty the Desktop."
193 (interactive)
fa379636
KH
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)
b6105c76
RS
200 (mapcar (function kill-buffer) (buffer-list))
201 (delete-other-windows))
0b9bd504 202;; ----------------------------------------------------------------------------
de9e2828 203(add-hook 'kill-emacs-hook 'desktop-kill)
ec4c6f22 204
6343ed61 205(defun desktop-kill ()
0b9bd504 206 (if desktop-dirname
fa379636
KH
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? ")
211 nil
212 (signal (car err) (cdr err)))))))
0b9bd504 213;; ----------------------------------------------------------------------------
de9e2828
RS
214(defun desktop-internal-v2s (val)
215 "Convert VALUE to a pair (quote . txt) where txt is a string that when read
fbe58183 216and evaluated yields value. quote may be 'may (value may be quoted),
de9e2828
RS
217'must (values must be quoted), or nil (value may not be quoted)."
218 (cond
219 ((or (numberp val) (stringp val) (null val) (eq t val))
220 (cons 'may (prin1-to-string val)))
221 ((symbolp val)
222 (cons 'must (prin1-to-string val)))
223 ((vectorp val)
224 (let* ((special nil)
225 (pass1 (mapcar
226 (lambda (el)
227 (let ((res (desktop-internal-v2s el)))
228 (if (null (car res))
229 (setq special t))
230 res))
231 val)))
232 (if special
233 (cons nil (concat "(vector "
234 (mapconcat (lambda (el)
235 (if (eq (car el) 'must)
236 (concat "'" (cdr el))
237 (cdr el)))
238 pass1
239 " ")
240 ")"))
241 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
242 ((consp val)
b4929f75
RS
243 (let ((p val)
244 newlist
245 anynil)
246 (while (consp p)
247 (let ((q.txt (desktop-internal-v2s (car p))))
248 (or anynil (setq anynil (null (car q.txt))))
249 (setq newlist (cons q.txt newlist)))
250 (setq p (cdr p)))
251 (if p
252 (let ((last (desktop-internal-v2s p))
253 (el (car newlist)))
254 (setcar newlist
255 (if (or anynil (setq anynil (null (car last))))
256 (cons nil
257 (concat "(cons "
258 (if (eq (car el) 'must) "'" "")
259 (cdr el)
260 " "
261 (if (eq (car last) 'must) "'" "")
262 (cdr last)
263 ")"))
264 (cons 'must
265 (concat (cdr el) " . " (cdr last)))))))
266 (setq newlist (nreverse newlist))
267 (if anynil
268 (cons nil
269 (concat "(list "
270 (mapconcat (lambda (el)
271 (if (eq (car el) 'must)
272 (concat "'" (cdr el))
273 (cdr el)))
274 newlist
275 " ")
276 ")"))
277 (cons 'must
278 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
de9e2828
RS
279 ((subrp val)
280 (cons nil (concat "(symbol-function '"
281 (substring (prin1-to-string val) 7 -1)
282 ")")))
283 ((markerp val)
284 (let ((pos (prin1-to-string (marker-position val)))
285 (buf (prin1-to-string (buffer-name (marker-buffer val)))))
286 (cons nil (concat "(let ((mk (make-marker)))"
287 " (add-hook 'desktop-delay-hook"
288 " (list 'lambda '() (list 'set-marker mk "
289 pos " (get-buffer " buf ")))) mk)"))))
290 (t ; save as text
fa379636 291 (cons 'may "\"Unprintable entity\""))))
de9e2828 292
ec4c6f22 293(defun desktop-value-to-string (val)
de9e2828
RS
294 "Convert VALUE to a string that when read evaluates to the same value. Not
295all types of values are supported."
296 (let* ((print-escape-newlines t)
297 (float-output-format nil)
298 (quote.txt (desktop-internal-v2s val))
299 (quote (car quote.txt))
300 (txt (cdr quote.txt)))
301 (if (eq quote 'must)
302 (concat "'" txt)
303 txt)))
ec4c6f22 304;; ----------------------------------------------------------------------------
b6105c76 305(defun desktop-outvar (var)
6343ed61 306 "Output a setq statement for VAR to the desktop file."
b6105c76 307 (if (boundp var)
ec4c6f22
RS
308 (insert "(setq "
309 (symbol-name var)
310 " "
311 (desktop-value-to-string (symbol-value var))
312 ")\n")))
0b9bd504 313;; ----------------------------------------------------------------------------
ec4c6f22 314(defun desktop-save-buffer-p (filename bufname mode &rest dummy)
b6105c76 315 "Return t if the desktop should record a particular buffer for next startup.
0b9bd504 316FILENAME is the visited file name, BUFNAME is the buffer name, and
6343ed61 317MODE is the major mode."
fa379636
KH
318 (let ((case-fold-search nil))
319 (or (and filename
320 (not (string-match desktop-buffers-not-to-save bufname))
321 (not (string-match desktop-files-not-to-save filename)))
322 (and (eq mode 'dired-mode)
323 (save-excursion
324 (set-buffer (get-buffer bufname))
325 (not (string-match desktop-files-not-to-save
326 default-directory))))
327 (and (null filename)
328 (memq mode '(Info-mode rmail-mode))))))
0b9bd504 329;; ----------------------------------------------------------------------------
6343ed61
RS
330(defun desktop-save (dirname)
331 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
332 (interactive "DDirectory to save desktop file in: ")
fa379636 333 (run-hooks 'desktop-save-hook)
6343ed61 334 (save-excursion
0b9bd504 335 (let ((filename (expand-file-name
6343ed61 336 (concat dirname desktop-basefilename)))
0b9bd504
RS
337 (info (nreverse
338 (mapcar
6343ed61
RS
339 (function (lambda (b)
340 (set-buffer b)
0b9bd504 341 (list
6343ed61
RS
342 (buffer-file-name)
343 (buffer-name)
ec4c6f22
RS
344 major-mode
345 (list ; list explaining minor modes
fa379636 346 (not (null auto-fill-function)))
6343ed61 347 (point)
de9e2828 348 (list (mark t) mark-active)
6343ed61 349 buffer-read-only
ec4c6f22
RS
350 (cond ((eq major-mode 'Info-mode)
351 (list Info-current-file
352 Info-current-node))
353 ((eq major-mode 'dired-mode)
de9e2828
RS
354 (nreverse
355 (mapcar
356 (function car)
357 dired-subdir-alist)))
ec4c6f22
RS
358 )
359 (let ((locals desktop-locals-to-save)
360 (loclist (buffer-local-variables))
361 (ll))
362 (while locals
363 (let ((here (assq (car locals) loclist)))
364 (if here
365 (setq ll (cons here ll))
366 (if (member (car locals) loclist)
367 (setq ll (cons (car locals) ll)))))
368 (setq locals (cdr locals)))
369 ll)
6343ed61
RS
370 )))
371 (buffer-list))))
372 (buf (get-buffer-create "*desktop*")))
373 (set-buffer buf)
374 (erase-buffer)
fa379636 375
0b9bd504
RS
376 (insert desktop-header
377 ";; Created " (current-time-string) "\n"
fa379636
KH
378 ";; Emacs version " emacs-version "\n\n"
379 ";; Global section:\n")
6343ed61
RS
380 (mapcar (function desktop-outvar) desktop-globals-to-save)
381 (if (memq 'kill-ring desktop-globals-to-save)
0b9bd504
RS
382 (insert "(setq kill-ring-yank-pointer (nthcdr "
383 (int-to-string
6343ed61
RS
384 (- (length kill-ring) (length kill-ring-yank-pointer)))
385 " kill-ring))\n"))
386
0b9bd504 387 (insert "\n;; Buffer section:\n")
de9e2828
RS
388 (mapcar
389 (function (lambda (l)
390 (if (apply 'desktop-save-buffer-p l)
391 (progn
392 (insert desktop-create-buffer-form)
393 (mapcar
394 (function (lambda (e)
395 (insert "\n "
396 (desktop-value-to-string e))))
397 l)
398 (insert ")\n\n")))))
399 info)
6343ed61
RS
400 (setq default-directory dirname)
401 (if (file-exists-p filename) (delete-file filename))
402 (write-region (point-min) (point-max) filename nil 'nomessage)))
403 (setq desktop-dirname dirname))
0b9bd504 404;; ----------------------------------------------------------------------------
6343ed61
RS
405(defun desktop-remove ()
406 "Delete the Desktop file and inactivate the desktop system."
407 (interactive)
408 (if desktop-dirname
409 (let ((filename (concat desktop-dirname desktop-basefilename)))
fa379636
KH
410 (setq desktop-dirname nil)
411 (if (file-exists-p filename)
412 (delete-file filename)))))
0b9bd504 413;; ----------------------------------------------------------------------------
6343ed61
RS
414(defun desktop-read ()
415 "Read the Desktop file and the files it specifies."
416 (interactive)
417 (let ((filename))
418 (if (file-exists-p (concat "./" desktop-basefilename))
419 (setq desktop-dirname (expand-file-name "./"))
420 (if (file-exists-p (concat "~/" desktop-basefilename))
421 (setq desktop-dirname (expand-file-name "~/"))
422 (setq desktop-dirname nil)))
423 (if desktop-dirname
424 (progn
425 (load (concat desktop-dirname desktop-basefilename) t t t)
de9e2828 426 (run-hooks 'desktop-delay-hook)
6343ed61
RS
427 (message "Desktop loaded."))
428 (desktop-clear))))
0b9bd504 429;; ----------------------------------------------------------------------------
6343ed61 430(defun desktop-load-default ()
0b9bd504 431 "Load the `default' start-up library manually. Also inhibit further loading
ec4c6f22 432of it. Call this from your `.emacs' file to provide correct modes for
0b9bd504
RS
433autoloaded files."
434 (if (not inhibit-default-init) ; safety check
6343ed61
RS
435 (progn
436 (load "default" t t)
437 (setq inhibit-default-init t))))
0b9bd504
RS
438;; ----------------------------------------------------------------------------
439;; Note: the following functions use the dynamic variable binding in Lisp.
0b9bd504 440;;
6343ed61 441(defun desktop-buffer-info () "Load an info file."
ec4c6f22 442 (if (eq 'Info-mode mam)
6343ed61
RS
443 (progn
444 (require 'info)
445 (Info-find-node (nth 0 misc) (nth 1 misc))
446 t)))
0b9bd504 447;; ----------------------------------------------------------------------------
b6105c76
RS
448(defun desktop-buffer-rmail () "Load an RMAIL file."
449 (if (eq 'rmail-mode mam)
e36d00d4
RS
450 (condition-case error
451 (progn (rmail-input fn) t)
452 (file-locked
453 (kill-buffer (current-buffer))
454 'ignored))))
0b9bd504 455;; ----------------------------------------------------------------------------
ec4c6f22
RS
456(defun desktop-buffer-mh () "Load a folder in the mh system."
457 (if (eq 'mh-folder-mode mam)
458 (progn
459 (require 'mh-e)
460 (mh-find-path)
461 (mh-visit-folder bn)
462 t)))
463;; ----------------------------------------------------------------------------
6343ed61 464(defun desktop-buffer-dired () "Load a directory using dired."
b6105c76 465 (if (eq 'dired-mode mam)
fa379636
KH
466 (if (file-directory-p (directory-file-name (car misc)))
467 (progn
468 (dired (car misc))
469 (mapcar (function dired-maybe-insert-subdir) (cdr misc))
470 t)
471 (message "Directory %s no longer exists." (car misc))
472 (sit-for 1)
473 'ignored)))
0b9bd504 474;; ----------------------------------------------------------------------------
6343ed61
RS
475(defun desktop-buffer-file () "Load a file."
476 (if fn
477 (if (or (file-exists-p fn)
478 (and desktop-missing-file-warning
0b9bd504
RS
479 (y-or-n-p (format
480 "File \"%s\" no longer exists. Re-create? "
6343ed61
RS
481 fn))))
482 (progn (find-file fn) t)
483 'ignored)))
0b9bd504
RS
484;; ----------------------------------------------------------------------------
485;; Create a buffer, load its file, set is mode, ...; called from Desktop file
6343ed61 486;; only.
ec4c6f22 487(defun desktop-create-buffer (ver fn bn mam mim pt mk ro misc &optional locals)
6343ed61
RS
488 (let ((hlist desktop-buffer-handlers)
489 (result)
490 (handler))
491 (while (and (not result) hlist)
492 (setq handler (car hlist))
493 (setq result (funcall handler))
494 (setq hlist (cdr hlist)))
b6105c76 495 (if (eq result t)
6343ed61
RS
496 (progn
497 (if (not (equal (buffer-name) bn))
498 (rename-buffer bn))
ec4c6f22 499 (auto-fill-mode (if (nth 0 mim) 1 0))
6343ed61 500 (goto-char pt)
0b9bd504
RS
501 (if (consp mk)
502 (progn
503 (set-mark (car mk))
504 (setq mark-active (car (cdr mk))))
505 (set-mark mk))
506 ;; Never override file system if the file really is read-only marked.
507 (if ro (setq buffer-read-only ro))
ec4c6f22
RS
508 (while locals
509 (let ((this (car locals)))
510 (if (consp this)
511 ;; an entry of this form `(symbol . value)'
512 (progn
513 (make-local-variable (car this))
514 (set (car this) (cdr this)))
515 ;; an entry of the form `symbol'
516 (make-local-variable this)
517 (makunbound this)))
518 (setq locals (cdr locals)))
6343ed61 519 ))))
ec4c6f22
RS
520
521;; Backward compatibility -- update parameters to 205 standards.
522(defun desktop-buffer (fn bn mam mim pt mk ro tl fc cfs cr misc)
523 (desktop-create-buffer 205 fn bn mam (cdr mim) pt mk ro misc
524 (list (cons 'truncate-lines tl)
525 (cons 'fill-column fc)
526 (cons 'case-fold-search cfs)
527 (cons 'case-replace cr)
528 (cons 'overwrite-mode (car mim)))))
0b9bd504 529;; ----------------------------------------------------------------------------
ab1d55ea 530(provide 'desktop)
6343ed61
RS
531
532;; desktop.el ends here.