*** empty log message ***
[bpt/emacs.git] / lisp / desktop.el
CommitLineData
6343ed61
RS
1;;; desktop.el --- save partial status of Emacs when killed
2
b6b70cda 3;; Copyright (C) 1993, 1994, 1995, 1997, 2000 Free Software Foundation, Inc.
6343ed61
RS
4
5;; Author: Morten Welinder <terra@diku.dk>
3ea96cac 6;; Keywords: convenience
b6105c76 7;; Favourite-brand-of-beer: None, I hate beer.
6343ed61
RS
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
b578f267
EN
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.
6343ed61
RS
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
478653c9 38;; To use this, first put these two lines in the bottom of your .emacs
0b9bd504
RS
39;; file (the later the better):
40;;
0b9bd504
RS
41;; (desktop-load-default)
42;; (desktop-read)
43;;
47640244
GM
44;; Between these two lines you may wish to add something that updates the
45;; variables `desktop-globals-to-save' and/or `desktop-locals-to-save'. If
46;; for instance you want to save the local variable `foobar' for every buffer
47;; in which it is local, you could add the line
ec4c6f22
RS
48;;
49;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
50;;
f4c73d07 51;; To avoid saving excessive amounts of data you may also wish to add
ec4c6f22
RS
52;; something like the following
53;;
54;; (add-hook 'kill-emacs-hook
de9e2828 55;; '(lambda ()
ec4c6f22
RS
56;; (desktop-truncate search-ring 3)
57;; (desktop-truncate regexp-search-ring 3)))
58;;
59;; which will make sure that no more than three search items are saved. You
47640244
GM
60;; must place this line *after* the `(desktop-load-default)' line. See also
61;; the variable `desktop-save-hook'.
6343ed61 62
0b9bd504
RS
63;; Start Emacs in the root directory of your "project". The desktop saver
64;; is inactive by default. You activate it by M-x desktop-save RET. When
65;; you exit the next time the above data will be saved. This ensures that
66;; all the files you were editing will be reloaded the next time you start
67;; Emacs from the same directory and that points will be set where you
ec4c6f22 68;; left them. If you save a desktop file in your home directory it will
de9e2828 69;; act as a default desktop when you start Emacs from a directory that
ec4c6f22
RS
70;; doesn't have its own. I never do this, but you may want to.
71
47640244
GM
72;; Some words on minor modes: Most minor modes are controlled by
73;; buffer-local variables, which have a standard save / restore
74;; mechanism. To handle all minor modes, we take the following
75;; approach: (1) check whether the variable name from
76;; `minor-mode-alist' is also a function; and (2) use translation
77;; table `desktop-minor-mode-table' in the case where the two names
78;; are not the same.
79
ec4c6f22
RS
80;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
81;; in your home directory is used for that. Saving global default values
82;; for buffers is an example of misuse.
83
0b9bd504
RS
84;; PLEASE NOTE: The kill ring can be saved as specified by the variable
85;; `desktop-globals-to-save' (by default it isn't). This may result in saving
86;; things you did not mean to keep. Use M-x desktop-clear RET.
ec4c6f22 87
fa379636
KH
88;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
89;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
90;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
91;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
92;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
f4c73d07 93;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
253406b9 94;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
0b9bd504
RS
95;; ---------------------------------------------------------------------------
96;; TODO:
97;;
98;; Save window configuration.
99;; Recognize more minor modes.
100;; Save mark rings.
101;; Start-up with buffer-menu???
6343ed61
RS
102
103;;; Code:
104
ec4c6f22
RS
105;; Make the compilation more silent
106(eval-when-compile
107 ;; We use functions from these modules
fa379636
KH
108 ;; We can't (require 'mh-e) since that wants to load something.
109 (mapcar 'require '(info dired reporter)))
ec4c6f22 110;; ----------------------------------------------------------------------------
0b9bd504
RS
111;; USER OPTIONS -- settings you might want to play with.
112;; ----------------------------------------------------------------------------
bbf5eb28
RS
113
114(defgroup desktop nil
115 "Save status of Emacs when you exit."
116 :group 'frames)
117
813dbb2d
RS
118(defcustom desktop-enable nil
119 "*Non-nil enable Desktop to save the state of Emacs when you exit."
120 :group 'desktop
121 :type 'boolean
122 :require 'desktop
cd32a7ba
DN
123 :initialize 'custom-initialize-default
124 :version "20.3")
813dbb2d
RS
125
126(defcustom desktop-basefilename
fc715501 127 (convert-standard-filename ".emacs.desktop")
813dbb2d
RS
128 "File for Emacs desktop, not including the directory name."
129 :type 'file
130 :group 'desktop)
6343ed61 131
bbf5eb28 132(defcustom desktop-missing-file-warning nil
577ed2b2 133 "*If non-nil then desktop warns when a file no longer exists.
bbf5eb28
RS
134Otherwise it simply ignores that file."
135 :type 'boolean
136 :group 'desktop)
6343ed61 137
0b9bd504 138(defvar desktop-globals-to-save
6343ed61 139 (list 'desktop-missing-file-warning
0b9bd504 140 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
ec4c6f22 141 ;; 'kill-ring
0b9bd504
RS
142 'tags-file-name
143 'tags-table-list
ec4c6f22
RS
144 'search-ring
145 'regexp-search-ring
de9e2828 146 'register-alist
0b9bd504 147 ;; 'desktop-globals-to-save ; Itself!
b6105c76 148 )
0e7c8611
RS
149 "List of global variables to save when killing Emacs.
150An element may be variable name (a symbol)
151or a cons cell of the form (VAR . MAX-SIZE),
152which means to truncate VAR's value to at most MAX-SIZE elements
153\(if the value is a list) before saving the value.")
6343ed61 154
ec4c6f22
RS
155(defvar desktop-locals-to-save
156 (list 'desktop-locals-to-save ; Itself! Think it over.
157 'truncate-lines
158 'case-fold-search
159 'case-replace
160 'fill-column
161 'overwrite-mode
162 'change-log-default-name
fa379636 163 'line-number-mode
ec4c6f22 164 )
577ed2b2
RS
165 "List of local variables to save for each buffer.
166The variables are saved only when they really are local.")
de9e2828 167(make-variable-buffer-local 'desktop-locals-to-save)
ec4c6f22 168
0b9bd504 169;; We skip .log files because they are normally temporary.
a7acbbe4 170;; (ftp) files because they require passwords and whatnot.
0b9bd504 171;; TAGS files to save time (tags-file-name is saved instead).
bbf5eb28 172(defcustom desktop-buffers-not-to-save
de9e2828 173 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
bbf5eb28
RS
174 "Regexp identifying buffers that are to be excluded from saving."
175 :type 'regexp
176 :group 'desktop)
6343ed61 177
fa379636 178;; Skip ange-ftp files
bbf5eb28 179(defcustom desktop-files-not-to-save
fa379636 180 "^/[^/:]*:"
bbf5eb28
RS
181 "Regexp identifying files whose buffers are to be excluded from saving."
182 :type 'regexp
183 :group 'desktop)
fa379636 184
b6b70cda
JW
185(defcustom desktop-buffer-modes-to-save
186 '(Info-mode rmail-mode)
187 "If a buffer is of one of these major modes, save the buffer name.
188It is up to the functions in `desktop-buffer-handlers' to decide
189whether the buffer should be recreated or not, and how."
190 :type '(repeat symbol)
191 :group 'desktop)
192
80f9f3db
SM
193(defcustom desktop-modes-not-to-save nil
194 "List of major modes whose buffers should not be saved."
195 :type '(repeat symbol)
196 :group 'desktop)
197
bbf5eb28
RS
198(defcustom desktop-buffer-major-mode nil
199 "When desktop creates a buffer, this holds the desired Major mode."
200 :type 'symbol
201 :group 'desktop)
569c754e 202
bbf5eb28
RS
203(defcustom desktop-buffer-file-name nil
204 "When desktop creates a buffer, this holds the file name to visit."
205 :type '(choice file (const nil))
206 :group 'desktop)
569c754e 207
bbf5eb28
RS
208(defcustom desktop-buffer-name nil
209 "When desktop creates a buffer, this holds the desired buffer name."
210 :type '(choice string (const nil))
211 :group 'desktop)
569c754e 212
2699e23e
RS
213(defvar desktop-buffer-misc nil
214 "When desktop creates a buffer, this holds a list of misc info.
215It is used by the `desktop-buffer-handlers' functions.")
216
b6b70cda
JW
217(defcustom desktop-buffer-misc-functions
218 '(desktop-buffer-info-misc-data
219 desktop-buffer-dired-misc-data)
220 "*Functions used to determine auxiliary information for a buffer.
221These functions are called in order, with no arguments. If a function
222returns non-nil, its value is saved along with the desktop buffer for
223which it was called; no further functions will be called.
224
225Later, when desktop.el restores the buffers it has saved, each of the
226`desktop-buffer-handlers' functions will have access to a buffer local
227variable, named `desktop-buffer-misc', whose value is what the
228\"misc\" function returned previously."
229 :type '(repeat function)
230 :group 'desktop)
231
bbf5eb28 232(defcustom desktop-buffer-handlers
0b9bd504 233 '(desktop-buffer-dired
6343ed61 234 desktop-buffer-rmail
ec4c6f22 235 desktop-buffer-mh
6343ed61
RS
236 desktop-buffer-info
237 desktop-buffer-file)
577ed2b2 238 "*List of functions to call in order to create a buffer.
569c754e
RS
239The functions are called without explicit parameters but can use the
240variables `desktop-buffer-major-mode', `desktop-buffer-file-name',
241`desktop-buffer-name'.
242If one function returns non-nil, no further functions are called.
bbf5eb28
RS
243If the function returns t then the buffer is considered created."
244 :type '(repeat function)
245 :group 'desktop)
ec4c6f22 246
b6b70cda
JW
247(put 'desktop-buffer-handlers 'risky-local-variable t)
248
ec4c6f22
RS
249(defvar desktop-create-buffer-form "(desktop-create-buffer 205"
250 "Opening of form for creation of new buffers.")
fa379636 251
bbf5eb28 252(defcustom desktop-save-hook nil
813dbb2d
RS
253 "Hook run before desktop saves the state of Emacs.
254This is useful for truncating history lists, for example."
bbf5eb28
RS
255 :type 'hook
256 :group 'desktop)
813dbb2d 257
47640244
GM
258(defcustom desktop-minor-mode-table
259 '((auto-fill-function auto-fill-mode)
260 (vc-mode nil))
261 "Table mapping minor mode variables to minor mode functions.
262Each entry has the form (NAME RESTORE-FUNCTION).
263NAME is the name of the buffer-local variable indicating that the minor
264mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
265called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
266Only minor modes for which the name of the buffer-local variable
267and the name of the minor mode function are different have to added to
268this table."
269 :type 'sexp
270 :group 'desktop)
271
0b9bd504
RS
272;; ----------------------------------------------------------------------------
273(defvar desktop-dirname nil
6343ed61
RS
274 "The directory in which the current desktop file resides.")
275
276(defconst desktop-header
0b9bd504
RS
277";; --------------------------------------------------------------------------
278;; Desktop File for Emacs
279;; --------------------------------------------------------------------------
6343ed61 280" "*Header to place in Desktop file.")
de9e2828
RS
281
282(defvar desktop-delay-hook nil
283 "Hooks run after all buffers are loaded; intended for internal use.")
813dbb2d 284
0b9bd504 285;; ----------------------------------------------------------------------------
ec4c6f22
RS
286(defun desktop-truncate (l n)
287 "Truncate LIST to at most N elements destructively."
288 (let ((here (nthcdr (1- n) l)))
289 (if (consp here)
de9e2828 290 (setcdr here nil))))
ec4c6f22 291;; ----------------------------------------------------------------------------
f9be4574
RS
292(defcustom desktop-clear-preserve-buffers
293 '("*scratch*" "*Messages*")
294 "*Buffer names that `desktop-clear' should not delete."
295 :type '(repeat string)
296 :group 'desktop)
297
298(defun desktop-clear ()
299 "Empty the Desktop.
300This kills all buffers except for internal ones
301and those listed in `desktop-clear-preserve-buffers'."
6343ed61 302 (interactive)
fa379636
KH
303 (setq kill-ring nil
304 kill-ring-yank-pointer nil
305 search-ring nil
306 search-ring-yank-pointer nil
307 regexp-search-ring nil
308 regexp-search-ring-yank-pointer nil)
f9be4574
RS
309 (let ((buffers (buffer-list)))
310 (while buffers
311 (or (member (buffer-name (car buffers)) desktop-clear-preserve-buffers)
2f348ca3 312 (null (buffer-name (car buffers)))
f9be4574
RS
313 ;; Don't kill buffers made for internal purposes.
314 (and (not (equal (buffer-name (car buffers)) ""))
315 (eq (aref (buffer-name (car buffers)) 0) ?\ ))
316 (kill-buffer (car buffers)))
317 (setq buffers (cdr buffers))))
b6105c76 318 (delete-other-windows))
0b9bd504 319;; ----------------------------------------------------------------------------
de9e2828 320(add-hook 'kill-emacs-hook 'desktop-kill)
ec4c6f22 321
6343ed61 322(defun desktop-kill ()
0b9bd504 323 (if desktop-dirname
fa379636
KH
324 (condition-case err
325 (desktop-save desktop-dirname)
326 (file-error
327 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
328 nil
329 (signal (car err) (cdr err)))))))
0b9bd504 330;; ----------------------------------------------------------------------------
ed2f7fc8
RS
331(defun desktop-list* (&rest args)
332 (if (null (cdr args))
333 (car args)
334 (setq args (nreverse args))
335 (let ((value (cons (nth 1 args) (car args))))
336 (setq args (cdr (cdr args)))
337 (while args
338 (setq value (cons (car args) value))
339 (setq args (cdr args)))
340 value)))
341
de9e2828 342(defun desktop-internal-v2s (val)
577ed2b2
RS
343 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
344TXT is a string that when read and evaluated yields value.
345QUOTE may be `may' (value may be quoted),
346`must' (values must be quoted), or nil (value may not be quoted)."
de9e2828 347 (cond
e2247420 348 ((or (numberp val) (null val) (eq t val))
de9e2828 349 (cons 'may (prin1-to-string val)))
e2247420 350 ((stringp val)
95bf6435
RS
351 (let ((copy (copy-sequence val)))
352 (set-text-properties 0 (length copy) nil copy)
353 ;; Get rid of text properties because we cannot read them
354 (cons 'may (prin1-to-string copy))))
de9e2828
RS
355 ((symbolp val)
356 (cons 'must (prin1-to-string val)))
357 ((vectorp val)
358 (let* ((special nil)
359 (pass1 (mapcar
360 (lambda (el)
361 (let ((res (desktop-internal-v2s el)))
362 (if (null (car res))
363 (setq special t))
364 res))
365 val)))
366 (if special
367 (cons nil (concat "(vector "
368 (mapconcat (lambda (el)
369 (if (eq (car el) 'must)
370 (concat "'" (cdr el))
371 (cdr el)))
372 pass1
373 " ")
374 ")"))
375 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
376 ((consp val)
b4929f75
RS
377 (let ((p val)
378 newlist
ed2f7fc8 379 use-list*
b4929f75
RS
380 anynil)
381 (while (consp p)
382 (let ((q.txt (desktop-internal-v2s (car p))))
383 (or anynil (setq anynil (null (car q.txt))))
384 (setq newlist (cons q.txt newlist)))
385 (setq p (cdr p)))
386 (if p
387 (let ((last (desktop-internal-v2s p))
388 (el (car newlist)))
ed2f7fc8
RS
389 (or anynil (setq anynil (null (car last))))
390 (or anynil
391 (setq newlist (cons '(must . ".") newlist)))
392 (setq use-list* t)
393 (setq newlist (cons last newlist))))
b4929f75
RS
394 (setq newlist (nreverse newlist))
395 (if anynil
396 (cons nil
ed2f7fc8 397 (concat (if use-list* "(desktop-list* " "(list ")
b4929f75
RS
398 (mapconcat (lambda (el)
399 (if (eq (car el) 'must)
400 (concat "'" (cdr el))
401 (cdr el)))
402 newlist
403 " ")
404 ")"))
405 (cons 'must
406 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
de9e2828
RS
407 ((subrp val)
408 (cons nil (concat "(symbol-function '"
409 (substring (prin1-to-string val) 7 -1)
410 ")")))
411 ((markerp val)
412 (let ((pos (prin1-to-string (marker-position val)))
413 (buf (prin1-to-string (buffer-name (marker-buffer val)))))
414 (cons nil (concat "(let ((mk (make-marker)))"
415 " (add-hook 'desktop-delay-hook"
416 " (list 'lambda '() (list 'set-marker mk "
417 pos " (get-buffer " buf ")))) mk)"))))
418 (t ; save as text
fa379636 419 (cons 'may "\"Unprintable entity\""))))
de9e2828 420
ec4c6f22 421(defun desktop-value-to-string (val)
577ed2b2
RS
422 "Convert VALUE to a string that when read evaluates to the same value.
423Not all types of values are supported."
de9e2828
RS
424 (let* ((print-escape-newlines t)
425 (float-output-format nil)
426 (quote.txt (desktop-internal-v2s val))
427 (quote (car quote.txt))
428 (txt (cdr quote.txt)))
429 (if (eq quote 'must)
430 (concat "'" txt)
431 txt)))
ec4c6f22 432;; ----------------------------------------------------------------------------
0e7c8611
RS
433(defun desktop-outvar (varspec)
434 "Output a setq statement for variable VAR to the desktop file.
435The argument VARSPEC may be the variable name VAR (a symbol),
436or a cons cell of the form (VAR . MAX-SIZE),
437which means to truncate VAR's value to at most MAX-SIZE elements
438\(if the value is a list) before saving the value."
439 (let (var size)
440 (if (consp varspec)
441 (setq var (car varspec) size (cdr varspec))
442 (setq var varspec))
443 (if (boundp var)
444 (progn
445 (if (and (integerp size)
446 (> size 0)
447 (listp (eval var)))
47640244 448 (desktop-truncate (eval var) size))
0e7c8611
RS
449 (insert "(setq "
450 (symbol-name var)
451 " "
452 (desktop-value-to-string (symbol-value var))
453 ")\n")))))
0b9bd504 454;; ----------------------------------------------------------------------------
ec4c6f22 455(defun desktop-save-buffer-p (filename bufname mode &rest dummy)
b6105c76 456 "Return t if the desktop should record a particular buffer for next startup.
0b9bd504 457FILENAME is the visited file name, BUFNAME is the buffer name, and
6343ed61 458MODE is the major mode."
fa379636 459 (let ((case-fold-search nil))
80f9f3db
SM
460 (and (not (string-match desktop-buffers-not-to-save bufname))
461 (not (memq mode desktop-modes-not-to-save))
462 (or (and filename
463 (not (string-match desktop-files-not-to-save filename)))
464 (and (eq mode 'dired-mode)
465 (save-excursion
466 (set-buffer (get-buffer bufname))
467 (not (string-match desktop-files-not-to-save
468 default-directory))))
469 (and (null filename)
b6b70cda 470 (memq mode desktop-buffer-modes-to-save))))))
0b9bd504 471;; ----------------------------------------------------------------------------
6343ed61
RS
472(defun desktop-save (dirname)
473 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
474 (interactive "DDirectory to save desktop file in: ")
fa379636 475 (run-hooks 'desktop-save-hook)
6343ed61 476 (save-excursion
0b9bd504 477 (let ((filename (expand-file-name
6343ed61 478 (concat dirname desktop-basefilename)))
0b9bd504
RS
479 (info (nreverse
480 (mapcar
47640244
GM
481 (function
482 (lambda (b)
6343ed61 483 (set-buffer b)
0b9bd504 484 (list
6343ed61
RS
485 (buffer-file-name)
486 (buffer-name)
ec4c6f22 487 major-mode
47640244
GM
488 ;; minor modes
489 (let (ret)
490 (mapcar
491 #'(lambda (mim)
69db7ee7
EZ
492 (and (boundp mim)
493 (symbol-value mim)
47640244
GM
494 (setq ret
495 (cons (let ((special (assq mim desktop-minor-mode-table)))
496 (if special
497 (cadr special)
498 mim))
499 ret))))
500 (mapcar #'car minor-mode-alist))
501 ret)
6343ed61 502 (point)
de9e2828 503 (list (mark t) mark-active)
6343ed61 504 buffer-read-only
b6b70cda
JW
505 (run-hook-with-args-until-success
506 'desktop-buffer-misc-functions)
ec4c6f22
RS
507 (let ((locals desktop-locals-to-save)
508 (loclist (buffer-local-variables))
509 (ll))
510 (while locals
511 (let ((here (assq (car locals) loclist)))
512 (if here
513 (setq ll (cons here ll))
514 (if (member (car locals) loclist)
515 (setq ll (cons (car locals) ll)))))
516 (setq locals (cdr locals)))
517 ll)
6343ed61
RS
518 )))
519 (buffer-list))))
520 (buf (get-buffer-create "*desktop*")))
521 (set-buffer buf)
522 (erase-buffer)
fa379636 523
0b9bd504
RS
524 (insert desktop-header
525 ";; Created " (current-time-string) "\n"
fa379636
KH
526 ";; Emacs version " emacs-version "\n\n"
527 ";; Global section:\n")
6343ed61
RS
528 (mapcar (function desktop-outvar) desktop-globals-to-save)
529 (if (memq 'kill-ring desktop-globals-to-save)
0b9bd504
RS
530 (insert "(setq kill-ring-yank-pointer (nthcdr "
531 (int-to-string
6343ed61
RS
532 (- (length kill-ring) (length kill-ring-yank-pointer)))
533 " kill-ring))\n"))
534
0b9bd504 535 (insert "\n;; Buffer section:\n")
de9e2828
RS
536 (mapcar
537 (function (lambda (l)
538 (if (apply 'desktop-save-buffer-p l)
539 (progn
540 (insert desktop-create-buffer-form)
541 (mapcar
542 (function (lambda (e)
543 (insert "\n "
544 (desktop-value-to-string e))))
545 l)
546 (insert ")\n\n")))))
547 info)
6343ed61
RS
548 (setq default-directory dirname)
549 (if (file-exists-p filename) (delete-file filename))
550 (write-region (point-min) (point-max) filename nil 'nomessage)))
551 (setq desktop-dirname dirname))
0b9bd504 552;; ----------------------------------------------------------------------------
6343ed61
RS
553(defun desktop-remove ()
554 "Delete the Desktop file and inactivate the desktop system."
555 (interactive)
556 (if desktop-dirname
557 (let ((filename (concat desktop-dirname desktop-basefilename)))
fa379636
KH
558 (setq desktop-dirname nil)
559 (if (file-exists-p filename)
560 (delete-file filename)))))
0b9bd504 561;; ----------------------------------------------------------------------------
478653c9 562;;;###autoload
6343ed61 563(defun desktop-read ()
253406b9
RS
564 "Read the Desktop file and the files it specifies.
565This is a no-op when Emacs is running in batch mode."
6343ed61 566 (interactive)
253406b9
RS
567 (if noninteractive
568 nil
569 (let ((dirs '("./" "~/")))
570 (while (and dirs
571 (not (file-exists-p (expand-file-name
572 desktop-basefilename
573 (car dirs)))))
574 (setq dirs (cdr dirs)))
575 (setq desktop-dirname (and dirs (expand-file-name (car dirs))))
576 (if desktop-dirname
577 (progn
578 (load (expand-file-name desktop-basefilename desktop-dirname)
579 t t t)
580 (run-hooks 'desktop-delay-hook)
581 (setq desktop-delay-hook nil)
582 (message "Desktop loaded."))
583 (desktop-clear)))))
0b9bd504 584;; ----------------------------------------------------------------------------
478653c9 585;;;###autoload
6343ed61 586(defun desktop-load-default ()
577ed2b2
RS
587 "Load the `default' start-up library manually.
588Also inhibit further loading of it. Call this from your `.emacs' file
589to provide correct modes for autoloaded files."
0b9bd504 590 (if (not inhibit-default-init) ; safety check
6343ed61
RS
591 (progn
592 (load "default" t t)
593 (setq inhibit-default-init t))))
0b9bd504
RS
594;; ----------------------------------------------------------------------------
595;; Note: the following functions use the dynamic variable binding in Lisp.
0b9bd504 596;;
b6b70cda
JW
597(defun desktop-buffer-info-misc-data ()
598 (if (eq major-mode 'Info-mode)
599 (list Info-current-file
600 Info-current-node)))
601
602(defun desktop-buffer-dired-misc-data ()
603 (if (eq major-mode 'dired-mode)
604 (cons
605 (expand-file-name dired-directory)
606 (cdr
607 (nreverse
608 (mapcar
609 (function car)
610 dired-subdir-alist))))))
611
6343ed61 612(defun desktop-buffer-info () "Load an info file."
569c754e 613 (if (eq 'Info-mode desktop-buffer-major-mode)
6343ed61 614 (progn
b6b70cda
JW
615 (let ((first (nth 0 desktop-buffer-misc))
616 (second (nth 1 desktop-buffer-misc)))
617 (when (and first second)
618 (require 'info)
619 (Info-find-node first second)
620 (current-buffer))))))
0b9bd504 621;; ----------------------------------------------------------------------------
b6105c76 622(defun desktop-buffer-rmail () "Load an RMAIL file."
569c754e 623 (if (eq 'rmail-mode desktop-buffer-major-mode)
e36d00d4 624 (condition-case error
608b9ed2
RS
625 (progn (rmail-input desktop-buffer-file-name)
626 (if (eq major-mode 'rmail-mode)
627 (current-buffer)
628 rmail-buffer))
e2247420
RS
629 (file-locked
630 (kill-buffer (current-buffer))
631 'ignored))))
0b9bd504 632;; ----------------------------------------------------------------------------
ec4c6f22 633(defun desktop-buffer-mh () "Load a folder in the mh system."
569c754e 634 (if (eq 'mh-folder-mode desktop-buffer-major-mode)
ec4c6f22
RS
635 (progn
636 (require 'mh-e)
637 (mh-find-path)
608b9ed2
RS
638 (mh-visit-folder desktop-buffer-name)
639 (current-buffer))))
ec4c6f22 640;; ----------------------------------------------------------------------------
6343ed61 641(defun desktop-buffer-dired () "Load a directory using dired."
569c754e 642 (if (eq 'dired-mode desktop-buffer-major-mode)
2699e23e 643 (if (file-directory-p (file-name-directory (car desktop-buffer-misc)))
fa379636 644 (progn
608b9ed2 645 (dired (car desktop-buffer-misc))
581bba23 646 (mapcar 'dired-maybe-insert-subdir (cdr desktop-buffer-misc))
608b9ed2 647 (current-buffer))
2699e23e 648 (message "Directory %s no longer exists." (car desktop-buffer-misc))
fa379636
KH
649 (sit-for 1)
650 'ignored)))
0b9bd504 651;; ----------------------------------------------------------------------------
6343ed61 652(defun desktop-buffer-file () "Load a file."
569c754e
RS
653 (if desktop-buffer-file-name
654 (if (or (file-exists-p desktop-buffer-file-name)
6343ed61 655 (and desktop-missing-file-warning
0b9bd504
RS
656 (y-or-n-p (format
657 "File \"%s\" no longer exists. Re-create? "
569c754e 658 desktop-buffer-file-name))))
80f9f3db
SM
659 (let ((buf (find-file-noselect desktop-buffer-file-name)))
660 (condition-case nil
661 (switch-to-buffer buf)
662 (error (pop-to-buffer buf))))
6343ed61 663 'ignored)))
0b9bd504
RS
664;; ----------------------------------------------------------------------------
665;; Create a buffer, load its file, set is mode, ...; called from Desktop file
6343ed61 666;; only.
569c754e
RS
667(defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name
668 desktop-buffer-major-mode
47640244
GM
669 mim pt mk ro desktop-buffer-misc
670 &optional locals)
6343ed61
RS
671 (let ((hlist desktop-buffer-handlers)
672 (result)
673 (handler))
674 (while (and (not result) hlist)
675 (setq handler (car hlist))
676 (setq result (funcall handler))
677 (setq hlist (cdr hlist)))
608b9ed2
RS
678 (when (bufferp result)
679 (set-buffer result)
680 (if (not (equal (buffer-name) desktop-buffer-name))
681 (rename-buffer desktop-buffer-name))
47640244
GM
682 ;; minor modes
683 (cond ((equal '(t) mim) (auto-fill-mode 1)) ; backwards compatible
684 ((equal '(nil) mim) (auto-fill-mode 0))
685 (t (mapcar #'(lambda (minor-mode)
b6b70cda
JW
686 (unless (or (eq minor-mode t) (eq minor-mode nil))
687 (if (and minor-mode (fboundp minor-mode))
688 (funcall minor-mode 1))))
47640244 689 mim)))
7c3d2af2 690 (goto-char pt)
608b9ed2
RS
691 (if (consp mk)
692 (progn
693 (set-mark (car mk))
694 (setq mark-active (car (cdr mk))))
695 (set-mark mk))
696 ;; Never override file system if the file really is read-only marked.
697 (if ro (setq buffer-read-only ro))
698 (while locals
699 (let ((this (car locals)))
700 (if (consp this)
701 ;; an entry of this form `(symbol . value)'
0b9bd504 702 (progn
608b9ed2
RS
703 (make-local-variable (car this))
704 (set (car this) (cdr this)))
705 ;; an entry of the form `symbol'
706 (make-local-variable this)
707 (makunbound this)))
708 (setq locals (cdr locals))))))
ec4c6f22
RS
709
710;; Backward compatibility -- update parameters to 205 standards.
569c754e
RS
711(defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
712 desktop-buffer-major-mode
2699e23e 713 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
569c754e 714 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
2699e23e
RS
715 desktop-buffer-major-mode (cdr mim) pt mk ro
716 desktop-buffer-misc
ec4c6f22
RS
717 (list (cons 'truncate-lines tl)
718 (cons 'fill-column fc)
719 (cons 'case-fold-search cfs)
720 (cons 'case-replace cr)
721 (cons 'overwrite-mode (car mim)))))
0b9bd504 722;; ----------------------------------------------------------------------------
813dbb2d
RS
723
724;; If the user set desktop-enable to t with Custom,
725;; do the rest of what it takes to use desktop,
726;; but do it after finishing loading the init file.
727(add-hook 'after-init-hook
728 '(lambda ()
729 (when desktop-enable
730 (desktop-load-default)
731 (desktop-read))))
732
ab1d55ea 733(provide 'desktop)
6343ed61 734
80f9f3db 735;;; desktop.el ends here