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