New directory
[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>
16906a65 7;; Maintainter: Lars Hansen <larsh@math.ku.dk>
3ea96cac 8;; Keywords: convenience
b6105c76 9;; Favourite-brand-of-beer: None, I hate beer.
6343ed61
RS
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b578f267
EN
24;; along with GNU Emacs; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
6343ed61
RS
27
28;;; Commentary:
29
0b9bd504
RS
30;; Save the Desktop, i.e.,
31;; - some global variables
32;; - the list of buffers with associated files. For each buffer also
33;; - the major mode
34;; - the default directory
35;; - the point
36;; - the mark & mark-active
37;; - buffer-read-only
ec4c6f22 38;; - some local variables
6343ed61 39
463f5630
KH
40;; To use this, first put these two lines in the bottom of your .emacs
41;; file (the later the better):
0b9bd504 42;;
463f5630
KH
43;; (desktop-load-default)
44;; (desktop-read)
0b9bd504 45;;
463f5630 46;; Between these two lines you may wish to add something that updates the
47640244
GM
47;; variables `desktop-globals-to-save' and/or `desktop-locals-to-save'. If
48;; for instance you want to save the local variable `foobar' for every buffer
49;; in which it is local, you could add the line
ec4c6f22 50;;
463f5630 51;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
ec4c6f22 52;;
f4c73d07 53;; To avoid saving excessive amounts of data you may also wish to add
ec4c6f22
RS
54;; something like the following
55;;
56;; (add-hook 'kill-emacs-hook
de9e2828 57;; '(lambda ()
ec4c6f22
RS
58;; (desktop-truncate search-ring 3)
59;; (desktop-truncate regexp-search-ring 3)))
60;;
61;; which will make sure that no more than three search items are saved. You
47640244
GM
62;; must place this line *after* the `(desktop-load-default)' line. See also
63;; the 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
47640244
GM
74;; Some words on minor modes: Most minor modes are controlled by
75;; buffer-local variables, which have a standard save / restore
76;; mechanism. To handle all minor modes, we take the following
77;; approach: (1) check whether the variable name from
78;; `minor-mode-alist' is also a function; and (2) use translation
79;; table `desktop-minor-mode-table' in the case where the two names
80;; are not the same.
81
ec4c6f22
RS
82;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
83;; in your home directory is used for that. Saving global default values
84;; for buffers is an example of misuse.
85
0b9bd504
RS
86;; PLEASE NOTE: The kill ring can be saved as specified by the variable
87;; `desktop-globals-to-save' (by default it isn't). This may result in saving
88;; things you did not mean to keep. Use M-x desktop-clear RET.
ec4c6f22 89
fa379636
KH
90;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
91;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
92;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
93;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
94;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
f4c73d07 95;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
253406b9 96;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
0b9bd504
RS
97;; ---------------------------------------------------------------------------
98;; TODO:
99;;
100;; Save window configuration.
101;; Recognize more minor modes.
102;; Save mark rings.
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)))
4a2fce7a
RS
111
112(defvar desktop-file-version "206"
113 "Verion number of desktop file format.
114Written into the desktop file and used at desktop read to provide
115backward compatibility.")
116
ec4c6f22 117;; ----------------------------------------------------------------------------
0b9bd504
RS
118;; USER OPTIONS -- settings you might want to play with.
119;; ----------------------------------------------------------------------------
bbf5eb28
RS
120
121(defgroup desktop nil
122 "Save status of Emacs when you exit."
123 :group 'frames)
124
813dbb2d
RS
125(defcustom desktop-enable nil
126 "*Non-nil enable Desktop to save the state of Emacs when you exit."
127 :group 'desktop
128 :type 'boolean
129 :require 'desktop
cd32a7ba
DN
130 :initialize 'custom-initialize-default
131 :version "20.3")
813dbb2d 132
4a2fce7a
RS
133(defcustom desktop-save 'ask-if-new
134 "*When the user changes desktop or quits emacs, should the desktop be saved?
135\(in the current desktop directory)
136 t -- always save.
137 ask -- always ask.
138 ask-if-new -- ask if no desktop file exists, otherwise just save.
139 ask-if-exists -- ask if desktop file exists, otherwise don't save.
140 if-exists -- save if desktop file exists, otherwise don't save.
141 nil -- never save.
142The desktop is never saved when `desktop-enable' is nil."
143 :type '(choice
144 (const :tag "Always save" t)
145 (const :tag "Always ask" ask)
146 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
147 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
148 (const :tag "Save if desktop file exists, else don't" if-exists)
149 (const :tag "Never save" nil))
150 :group 'desktop)
151
152(defcustom desktop-base-file-name
fc715501 153 (convert-standard-filename ".emacs.desktop")
813dbb2d
RS
154 "File for Emacs desktop, not including the directory name."
155 :type 'file
156 :group 'desktop)
7065d42f 157(defvaralias 'desktop-basefilename 'desktop-base-file-name)
6343ed61 158
4a2fce7a
RS
159(defcustom desktop-path '("." "~")
160 "List of directories to search for the desktop file.
161The base name of the file is specified in `desktop-base-file-name'."
162 :type '(repeat directory)
163 :group 'desktop)
164
bbf5eb28 165(defcustom desktop-missing-file-warning nil
577ed2b2 166 "*If non-nil then desktop warns when a file no longer exists.
bbf5eb28
RS
167Otherwise it simply ignores that file."
168 :type 'boolean
169 :group 'desktop)
6343ed61 170
4a2fce7a
RS
171(defcustom desktop-no-desktop-file-hook nil
172 "Normal hook run after fail of `desktop-read' due to missing desktop file.
173May e.g. be used to show a dired buffer."
174 :type 'hook
175 :group 'desktop)
176
177(defcustom desktop-after-read-hook nil
178 "Normal hook run after a sucessful `desktop-read'.
179May e.g. be used to show a buffer list."
180 :type 'hook
181 :group 'desktop)
182
183(defcustom desktop-save-hook nil
184 "Hook run before desktop saves the state of Emacs.
185This is useful for truncating history lists, for example."
186 :type 'hook
187 :group 'desktop)
188
189(defcustom desktop-globals-to-save '(
190 desktop-missing-file-warning
191 tags-file-name
192 tags-table-list
193 search-ring
194 regexp-search-ring
195 register-alist)
0e7c8611
RS
196 "List of global variables to save when killing Emacs.
197An element may be variable name (a symbol)
198or a cons cell of the form (VAR . MAX-SIZE),
199which means to truncate VAR's value to at most MAX-SIZE elements
4a2fce7a
RS
200\(if the value is a list) before saving the value.
201Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
202 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
203 :group 'desktop)
204
205(defcustom desktop-globals-to-clear '(
206 kill-ring
207 kill-ring-yank-pointer
208 search-ring
209 search-ring-yank-pointer
210 regexp-search-ring
211 regexp-search-ring-yank-pointer)
212 "List of global variables set to clear by `desktop-clear'.
213An element may be variable name (a symbol) or a cons cell of the form
214\(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
215to the value obtained by evaluateing FORM."
216 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
217 :group 'desktop)
218
219(defcustom desktop-clear-preserve-buffers-regexp
220 "^\\*tramp/.+\\*$"
221 "Regexp identifying buffers that `desktop-clear' should not delete."
222 :type 'regexp
223 :group 'desktop)
224
225;; Maintained for backward compatibility
226(defcustom desktop-clear-preserve-buffers
227 '("*scratch*" "*Messages*")
228 "*List of buffer names that `desktop-clear' should not delete."
229 :type '(repeat string)
230 :group 'desktop)
231
232(defvar desktop-locals-to-save '(
233 desktop-locals-to-save ; Itself! Think it over.
234 truncate-lines
235 case-fold-search
236 case-replace
237 fill-column
238 overwrite-mode
239 change-log-default-name
240 line-number-mode)
577ed2b2
RS
241 "List of local variables to save for each buffer.
242The variables are saved only when they really are local.")
de9e2828 243(make-variable-buffer-local 'desktop-locals-to-save)
ec4c6f22 244
0b9bd504 245;; We skip .log files because they are normally temporary.
a7acbbe4 246;; (ftp) files because they require passwords and whatnot.
0b9bd504 247;; TAGS files to save time (tags-file-name is saved instead).
bbf5eb28 248(defcustom desktop-buffers-not-to-save
4a2fce7a
RS
249 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
250 "Regexp identifying buffers that are to be excluded from saving."
251 :type 'regexp
252 :group 'desktop)
6343ed61 253
fa379636 254;; Skip ange-ftp files
bbf5eb28 255(defcustom desktop-files-not-to-save
fa379636 256 "^/[^/:]*:"
bbf5eb28
RS
257 "Regexp identifying files whose buffers are to be excluded from saving."
258 :type 'regexp
259 :group 'desktop)
fa379636 260
b6b70cda
JW
261(defcustom desktop-buffer-modes-to-save
262 '(Info-mode rmail-mode)
263 "If a buffer is of one of these major modes, save the buffer name.
264It is up to the functions in `desktop-buffer-handlers' to decide
265whether the buffer should be recreated or not, and how."
266 :type '(repeat symbol)
267 :group 'desktop)
268
80f9f3db
SM
269(defcustom desktop-modes-not-to-save nil
270 "List of major modes whose buffers should not be saved."
271 :type '(repeat symbol)
272 :group 'desktop)
273
4a2fce7a
RS
274(defcustom desktop-file-name-format 'absolute
275 "*Format in which desktop file names should be saved.
276Possible values are:
277 absolute -- Absolute file name.
278 tilde -- Relative to ~.
279 local -- Relative to directory of desktop file."
280 :type '(choice (const absolute) (const tilde) (const local))
bbf5eb28 281 :group 'desktop)
569c754e 282
b6b70cda
JW
283(defcustom desktop-buffer-misc-functions
284 '(desktop-buffer-info-misc-data
285 desktop-buffer-dired-misc-data)
286 "*Functions used to determine auxiliary information for a buffer.
287These functions are called in order, with no arguments. If a function
288returns non-nil, its value is saved along with the desktop buffer for
289which it was called; no further functions will be called.
290
4a2fce7a
RS
291File names should formatted using the call
292\"(desktop-file-name FILE-NAME dirname)\".
293
b6b70cda
JW
294Later, when desktop.el restores the buffers it has saved, each of the
295`desktop-buffer-handlers' functions will have access to a buffer local
296variable, named `desktop-buffer-misc', whose value is what the
297\"misc\" function returned previously."
298 :type '(repeat function)
299 :group 'desktop)
300
bbf5eb28 301(defcustom desktop-buffer-handlers
0b9bd504 302 '(desktop-buffer-dired
6343ed61 303 desktop-buffer-rmail
ec4c6f22 304 desktop-buffer-mh
6343ed61
RS
305 desktop-buffer-info
306 desktop-buffer-file)
577ed2b2 307 "*List of functions to call in order to create a buffer.
569c754e 308The functions are called without explicit parameters but can use the
4a2fce7a
RS
309following variables:
310
311 desktop-file-version
312 desktop-buffer-file-name
313 desktop-buffer-name
314 desktop-buffer-major-mode
315 desktop-buffer-minor-modes
316 desktop-buffer-point
317 desktop-buffer-mark
318 desktop-buffer-read-only
319 desktop-buffer-misc
320 desktop-buffer-locals
321
569c754e 322If one function returns non-nil, no further functions are called.
0a8c8225
RS
323If the function returns a buffer, then the saved mode settings
324and variable values for that buffer are copied into it."
bbf5eb28
RS
325 :type '(repeat function)
326 :group 'desktop)
ec4c6f22 327
b6b70cda
JW
328(put 'desktop-buffer-handlers 'risky-local-variable t)
329
47640244
GM
330(defcustom desktop-minor-mode-table
331 '((auto-fill-function auto-fill-mode)
332 (vc-mode nil))
333 "Table mapping minor mode variables to minor mode functions.
334Each entry has the form (NAME RESTORE-FUNCTION).
335NAME is the name of the buffer-local variable indicating that the minor
336mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
337called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
338Only minor modes for which the name of the buffer-local variable
339and the name of the minor mode function are different have to added to
340this table."
341 :type 'sexp
342 :group 'desktop)
343
0b9bd504
RS
344;; ----------------------------------------------------------------------------
345(defvar desktop-dirname nil
6343ed61
RS
346 "The directory in which the current desktop file resides.")
347
348(defconst desktop-header
0b9bd504
RS
349";; --------------------------------------------------------------------------
350;; Desktop File for Emacs
351;; --------------------------------------------------------------------------
6343ed61 352" "*Header to place in Desktop file.")
de9e2828
RS
353
354(defvar desktop-delay-hook nil
355 "Hooks run after all buffers are loaded; intended for internal use.")
813dbb2d 356
0b9bd504 357;; ----------------------------------------------------------------------------
ec4c6f22
RS
358(defun desktop-truncate (l n)
359 "Truncate LIST to at most N elements destructively."
360 (let ((here (nthcdr (1- n) l)))
361 (if (consp here)
de9e2828 362 (setcdr here nil))))
f9be4574 363
4a2fce7a 364;; ----------------------------------------------------------------------------
f9be4574
RS
365(defun desktop-clear ()
366 "Empty the Desktop.
4a2fce7a
RS
367This kills all buffers except for internal ones and those listed
368in `desktop-clear-preserve-buffers'. Furthermore, it clears the
369variables listed in `desktop-globals-to-clear'."
6343ed61 370 (interactive)
4a2fce7a
RS
371 (dolist (var desktop-globals-to-clear)
372 (if (symbolp var)
373 (eval `(setq-default ,var nil))
374 (eval `(setq-default ,(car var) ,(cdr var)))))
f9be4574
RS
375 (let ((buffers (buffer-list)))
376 (while buffers
4a2fce7a
RS
377 (let ((bufname (buffer-name (car buffers))))
378 (or
379 (null bufname)
380 (string-match desktop-clear-preserve-buffers-regexp bufname)
381 (member bufname desktop-clear-preserve-buffers)
382 ;; Don't kill buffers made for internal purposes.
383 (and (not (equal bufname "")) (eq (aref bufname 0) ?\ ))
384 (kill-buffer (car buffers))))
f9be4574 385 (setq buffers (cdr buffers))))
b6105c76 386 (delete-other-windows))
4a2fce7a 387
0b9bd504 388;; ----------------------------------------------------------------------------
de9e2828 389(add-hook 'kill-emacs-hook 'desktop-kill)
ec4c6f22 390
6343ed61 391(defun desktop-kill ()
4a2fce7a
RS
392 "If `desktop-enable' is non-nil, do what `desktop-save' says to do.
393If the desktop should be saved and `desktop-dirname'
394is nil, ask the user where to save the desktop."
395 (when
396 (and
397 desktop-enable
73b0b745 398 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
4a2fce7a 399 (or
463f5630 400 (eq desktop-save 't)
4a2fce7a
RS
401 (and exists (memq desktop-save '(ask-if-new if-exists)))
402 (and
403 (or
404 (memq desktop-save '(ask ask-if-new))
405 (and exists (eq desktop-save 'ask-if-exists)))
406 (y-or-n-p "Save desktop? ")))))
407 (unless desktop-dirname
408 (setq desktop-dirname
73b0b745
JB
409 (file-name-as-directory
410 (expand-file-name
411 (call-interactively
412 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
4a2fce7a
RS
413 (condition-case err
414 (desktop-save desktop-dirname)
415 (file-error
416 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
417 (signal (car err) (cdr err)))))))
418
0b9bd504 419;; ----------------------------------------------------------------------------
ed2f7fc8
RS
420(defun desktop-list* (&rest args)
421 (if (null (cdr args))
422 (car args)
423 (setq args (nreverse args))
424 (let ((value (cons (nth 1 args) (car args))))
425 (setq args (cdr (cdr args)))
426 (while args
427 (setq value (cons (car args) value))
428 (setq args (cdr args)))
429 value)))
430
4a2fce7a 431;; ----------------------------------------------------------------------------
de9e2828 432(defun desktop-internal-v2s (val)
577ed2b2
RS
433 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
434TXT is a string that when read and evaluated yields value.
435QUOTE may be `may' (value may be quoted),
436`must' (values must be quoted), or nil (value may not be quoted)."
de9e2828 437 (cond
e2247420 438 ((or (numberp val) (null val) (eq t val))
de9e2828 439 (cons 'may (prin1-to-string val)))
e2247420 440 ((stringp val)
95bf6435
RS
441 (let ((copy (copy-sequence val)))
442 (set-text-properties 0 (length copy) nil copy)
443 ;; Get rid of text properties because we cannot read them
444 (cons 'may (prin1-to-string copy))))
de9e2828
RS
445 ((symbolp val)
446 (cons 'must (prin1-to-string val)))
447 ((vectorp val)
448 (let* ((special nil)
449 (pass1 (mapcar
450 (lambda (el)
451 (let ((res (desktop-internal-v2s el)))
452 (if (null (car res))
453 (setq special t))
454 res))
455 val)))
456 (if special
457 (cons nil (concat "(vector "
458 (mapconcat (lambda (el)
459 (if (eq (car el) 'must)
460 (concat "'" (cdr el))
461 (cdr el)))
462 pass1
463 " ")
464 ")"))
465 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
466 ((consp val)
b4929f75
RS
467 (let ((p val)
468 newlist
ed2f7fc8 469 use-list*
b4929f75
RS
470 anynil)
471 (while (consp p)
472 (let ((q.txt (desktop-internal-v2s (car p))))
473 (or anynil (setq anynil (null (car q.txt))))
474 (setq newlist (cons q.txt newlist)))
475 (setq p (cdr p)))
476 (if p
477 (let ((last (desktop-internal-v2s p))
478 (el (car newlist)))
ed2f7fc8
RS
479 (or anynil (setq anynil (null (car last))))
480 (or anynil
481 (setq newlist (cons '(must . ".") newlist)))
482 (setq use-list* t)
483 (setq newlist (cons last newlist))))
b4929f75
RS
484 (setq newlist (nreverse newlist))
485 (if anynil
486 (cons nil
ed2f7fc8 487 (concat (if use-list* "(desktop-list* " "(list ")
b4929f75
RS
488 (mapconcat (lambda (el)
489 (if (eq (car el) 'must)
490 (concat "'" (cdr el))
491 (cdr el)))
492 newlist
493 " ")
494 ")"))
495 (cons 'must
496 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
de9e2828
RS
497 ((subrp val)
498 (cons nil (concat "(symbol-function '"
499 (substring (prin1-to-string val) 7 -1)
500 ")")))
501 ((markerp val)
502 (let ((pos (prin1-to-string (marker-position val)))
503 (buf (prin1-to-string (buffer-name (marker-buffer val)))))
504 (cons nil (concat "(let ((mk (make-marker)))"
505 " (add-hook 'desktop-delay-hook"
506 " (list 'lambda '() (list 'set-marker mk "
507 pos " (get-buffer " buf ")))) mk)"))))
508 (t ; save as text
fa379636 509 (cons 'may "\"Unprintable entity\""))))
de9e2828 510
4a2fce7a 511;; ----------------------------------------------------------------------------
ec4c6f22 512(defun desktop-value-to-string (val)
577ed2b2
RS
513 "Convert VALUE to a string that when read evaluates to the same value.
514Not all types of values are supported."
de9e2828
RS
515 (let* ((print-escape-newlines t)
516 (float-output-format nil)
517 (quote.txt (desktop-internal-v2s val))
518 (quote (car quote.txt))
519 (txt (cdr quote.txt)))
520 (if (eq quote 'must)
521 (concat "'" txt)
522 txt)))
4a2fce7a 523
ec4c6f22 524;; ----------------------------------------------------------------------------
0e7c8611
RS
525(defun desktop-outvar (varspec)
526 "Output a setq statement for variable VAR to the desktop file.
527The argument VARSPEC may be the variable name VAR (a symbol),
528or a cons cell of the form (VAR . MAX-SIZE),
529which means to truncate VAR's value to at most MAX-SIZE elements
530\(if the value is a list) before saving the value."
531 (let (var size)
532 (if (consp varspec)
533 (setq var (car varspec) size (cdr varspec))
534 (setq var varspec))
535 (if (boundp var)
536 (progn
537 (if (and (integerp size)
538 (> size 0)
539 (listp (eval var)))
47640244 540 (desktop-truncate (eval var) size))
0e7c8611
RS
541 (insert "(setq "
542 (symbol-name var)
543 " "
544 (desktop-value-to-string (symbol-value var))
545 ")\n")))))
4a2fce7a 546
0b9bd504 547;; ----------------------------------------------------------------------------
ec4c6f22 548(defun desktop-save-buffer-p (filename bufname mode &rest dummy)
b6105c76 549 "Return t if the desktop should record a particular buffer for next startup.
0b9bd504 550FILENAME is the visited file name, BUFNAME is the buffer name, and
6343ed61 551MODE is the major mode."
fa379636 552 (let ((case-fold-search nil))
80f9f3db
SM
553 (and (not (string-match desktop-buffers-not-to-save bufname))
554 (not (memq mode desktop-modes-not-to-save))
555 (or (and filename
556 (not (string-match desktop-files-not-to-save filename)))
557 (and (eq mode 'dired-mode)
558 (save-excursion
559 (set-buffer (get-buffer bufname))
560 (not (string-match desktop-files-not-to-save
561 default-directory))))
562 (and (null filename)
b6b70cda 563 (memq mode desktop-buffer-modes-to-save))))))
4a2fce7a 564
0b9bd504 565;; ----------------------------------------------------------------------------
4a2fce7a
RS
566(defun desktop-file-name (filename dirname)
567 "Convert FILENAME to format specified in `desktop-file-name-format'.
568DIRNAME must be the directory in which the desktop file will be saved."
569 (cond
570 ((not filename) nil)
571 ((eq desktop-file-name-format 'tilde)
572 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
573 (cond
574 ((file-name-absolute-p relative-name) relative-name)
575 ((string= "./" relative-name) "~/")
576 ((string= "." relative-name) "~")
577 (t (concat "~/" relative-name)))))
578 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
579 (t (expand-file-name filename))))
e5714620 580
4a2fce7a 581;; ----------------------------------------------------------------------------
6343ed61 582(defun desktop-save (dirname)
4a2fce7a 583 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
6343ed61 584 (interactive "DDirectory to save desktop file in: ")
fa379636 585 (run-hooks 'desktop-save-hook)
7bcbf3c2 586 (setq dirname (file-name-as-directory (expand-file-name dirname)))
6343ed61 587 (save-excursion
73b0b745 588 (let ((filename (expand-file-name desktop-base-file-name dirname))
7bcbf3c2
JB
589 (info
590 (mapcar
591 (function
592 (lambda (b)
593 (set-buffer b)
594 (list
595 (desktop-file-name (buffer-file-name) dirname)
596 (buffer-name)
597 major-mode
598 ;; minor modes
599 (let (ret)
600 (mapcar
601 #'(lambda (mim)
602 (and
603 (boundp mim)
604 (symbol-value mim)
605 (setq ret
606 (cons
607 (let ((special (assq mim desktop-minor-mode-table)))
608 (if special (cadr special) mim))
609 ret))))
610 (mapcar #'car minor-mode-alist))
611 ret)
612 (point)
613 (list (mark t) mark-active)
614 buffer-read-only
615 (run-hook-with-args-until-success 'desktop-buffer-misc-functions)
616 (let ((locals desktop-locals-to-save)
617 (loclist (buffer-local-variables))
618 (ll))
619 (while locals
620 (let ((here (assq (car locals) loclist)))
621 (if here
622 (setq ll (cons here ll))
623 (when (member (car locals) loclist)
624 (setq ll (cons (car locals) ll)))))
625 (setq locals (cdr locals)))
626 ll))))
627 (buffer-list)))
628 (buf (get-buffer-create "*desktop*")))
6343ed61
RS
629 (set-buffer buf)
630 (erase-buffer)
fa379636 631
4a2fce7a
RS
632 (insert
633 ";; -*- coding: emacs-mule; -*-\n"
634 desktop-header
635 ";; Created " (current-time-string) "\n"
636 ";; Desktop file format version " desktop-file-version "\n"
637 ";; Emacs version " emacs-version "\n\n"
638 ";; Global section:\n")
6343ed61
RS
639 (mapcar (function desktop-outvar) desktop-globals-to-save)
640 (if (memq 'kill-ring desktop-globals-to-save)
4a2fce7a
RS
641 (insert
642 "(setq kill-ring-yank-pointer (nthcdr "
643 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
644 " kill-ring))\n"))
6343ed61 645
4a2fce7a 646 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
de9e2828 647 (mapcar
4a2fce7a
RS
648 (function
649 (lambda (l)
650 (if (apply 'desktop-save-buffer-p l)
651 (progn
652 (insert "(desktop-create-buffer " desktop-file-version)
653 (mapcar
654 (function
655 (lambda (e)
656 (insert "\n " (desktop-value-to-string e))))
657 l)
658 (insert ")\n\n")))))
659 info)
6343ed61 660 (setq default-directory dirname)
4a2fce7a 661 (when (file-exists-p filename) (delete-file filename))
7642acca 662 (let ((coding-system-for-write 'emacs-mule))
4a2fce7a 663 (write-region (point-min) (point-max) filename nil 'nomessage))))
6343ed61 664 (setq desktop-dirname dirname))
4a2fce7a 665
0b9bd504 666;; ----------------------------------------------------------------------------
6343ed61
RS
667(defun desktop-remove ()
668 "Delete the Desktop file and inactivate the desktop system."
669 (interactive)
670 (if desktop-dirname
73b0b745
JB
671 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname)))
672 (setq desktop-dirname nil)
673 (if (file-exists-p filename)
674 (delete-file filename)))))
0b9bd504 675;; ----------------------------------------------------------------------------
478653c9 676;;;###autoload
6343ed61 677(defun desktop-read ()
253406b9 678 "Read the Desktop file and the files it specifies.
4a2fce7a
RS
679This is a no-op when Emacs is running in batch mode.
680Look for the desktop file according to the variables `desktop-base-file-name'
681and `desktop-path'. If no desktop file is found, clear the desktop.
682Returns t if it has read a desktop file, nil otherwise."
6343ed61 683 (interactive)
4a2fce7a
RS
684 (unless noninteractive
685 (let ((dirs desktop-path))
686 (while
687 (and
688 dirs
689 (not
690 (file-exists-p (expand-file-name desktop-base-file-name (car dirs)))))
691 (setq dirs (cdr dirs)))
73b0b745 692 (setq desktop-dirname (and dirs (file-name-as-directory (expand-file-name (car dirs)))))
253406b9 693 (if desktop-dirname
4a2fce7a 694 (let ((desktop-first-buffer nil))
7bcbf3c2 695 ;; Evaluate desktop buffer.
4a2fce7a 696 (load (expand-file-name desktop-base-file-name desktop-dirname) t t t)
7bcbf3c2
JB
697 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
698 ;; We want buffers existing prior to evaluating the desktop (and not reused)
699 ;; to be placed at the end of the buffer list, so we move them here.
700 (mapcar 'bury-buffer
701 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
702 (switch-to-buffer (car (buffer-list)))
4a2fce7a
RS
703 (run-hooks 'desktop-delay-hook)
704 (setq desktop-delay-hook nil)
705 (run-hooks 'desktop-after-read-hook)
706 (message "Desktop loaded.")
707 t)
708 (desktop-clear)
709 (run-hooks 'desktop-no-desktop-file-hook)
710 (message "No desktop file.")
711 nil))))
712
0b9bd504 713;; ----------------------------------------------------------------------------
478653c9 714;;;###autoload
6343ed61 715(defun desktop-load-default ()
577ed2b2
RS
716 "Load the `default' start-up library manually.
717Also inhibit further loading of it. Call this from your `.emacs' file
718to provide correct modes for autoloaded files."
0b9bd504 719 (if (not inhibit-default-init) ; safety check
6343ed61
RS
720 (progn
721 (load "default" t t)
722 (setq inhibit-default-init t))))
4a2fce7a
RS
723
724;; ----------------------------------------------------------------------------
725;;;###autoload
726(defun desktop-change-dir (dir)
727 "Save and clear the desktop, then load the desktop from directory DIR.
728However, if `desktop-enable' was nil at call, don't save the old desktop.
729This function always sets `desktop-enable' to t."
730 (interactive "DNew directory: ")
73b0b745 731 (setq dir (file-name-as-directory (expand-file-name dir desktop-dirname)))
4a2fce7a
RS
732 (desktop-kill)
733 (desktop-clear)
4a2fce7a 734 (setq desktop-enable t)
73b0b745
JB
735 (let ((desktop-path (list dir))
736 (default-directory dir))
737 (desktop-read))
738 ;; Set `desktop-dirname' even in no desktop file was found
739 (setq desktop-dirname dir))
740 ;; ----------------------------------------------------------------------------
4a2fce7a
RS
741;;;###autoload
742(defun desktop-save-in-load-dir ()
743 "Save desktop in directory from which it was loaded."
744 (interactive)
745 (if desktop-dirname
746 (desktop-save desktop-dirname)
747 (call-interactively 'desktop-save))
748 (message "Desktop saved in %s" desktop-dirname))
749
750;; ----------------------------------------------------------------------------
751;;;###autoload
752(defun desktop-revert ()
753 "Revert to the last loaded desktop."
754 (interactive)
755 (unless desktop-dirname (error "No desktop has been loaded"))
756 (setq desktop-enable nil)
757 (desktop-change-dir desktop-dirname))
758
0b9bd504
RS
759;; ----------------------------------------------------------------------------
760;; Note: the following functions use the dynamic variable binding in Lisp.
0b9bd504 761;;
4a2fce7a
RS
762
763(eval-when-compile ; Just to silence the byte compiler
764 (defvar desktop-file-version)
765 (defvar desktop-buffer-file-name)
766 (defvar desktop-buffer-name)
767 (defvar desktop-buffer-major-mode)
768 (defvar desktop-buffer-minor-modes)
769 (defvar desktop-buffer-point)
770 (defvar desktop-buffer-mark)
771 (defvar desktop-buffer-read-only)
772 (defvar desktop-buffer-misc)
773 (defvar desktop-buffer-locals)
774)
775
b6b70cda
JW
776(defun desktop-buffer-info-misc-data ()
777 (if (eq major-mode 'Info-mode)
778 (list Info-current-file
779 Info-current-node)))
780
4a2fce7a 781;; ----------------------------------------------------------------------------
b6b70cda 782(defun desktop-buffer-dired-misc-data ()
4a2fce7a
RS
783 (when (eq major-mode 'dired-mode)
784 (eval-when-compile (defvar dirname))
785 (cons
df7eb2bd
JB
786 ;; Value of `dired-directory'.
787 (if (consp dired-directory)
788 ;; Directory name followed by list of files.
789 (cons (desktop-file-name (car dired-directory) dirname) (cdr dired-directory))
790 ;; Directory name, optionally with with shell wildcard.
791 (desktop-file-name dired-directory dirname))
792 ;; Subdirectories in `dired-subdir-alist'.
793 (cdr
794 (nreverse
795 (mapcar
796 (function (lambda (f) (desktop-file-name (car f) dirname)))
797 dired-subdir-alist))))))
b6b70cda 798
4a2fce7a 799;; ----------------------------------------------------------------------------
6343ed61 800(defun desktop-buffer-info () "Load an info file."
569c754e 801 (if (eq 'Info-mode desktop-buffer-major-mode)
6343ed61 802 (progn
b6b70cda
JW
803 (let ((first (nth 0 desktop-buffer-misc))
804 (second (nth 1 desktop-buffer-misc)))
805 (when (and first second)
806 (require 'info)
6df8016c
RS
807 (with-no-warnings
808 (Info-find-node first second))
b6b70cda 809 (current-buffer))))))
4a2fce7a 810
0b9bd504 811;; ----------------------------------------------------------------------------
4a2fce7a 812(eval-when-compile (defvar rmail-buffer)) ; Just to silence the byte compiler.
b6105c76 813(defun desktop-buffer-rmail () "Load an RMAIL file."
569c754e 814 (if (eq 'rmail-mode desktop-buffer-major-mode)
e36d00d4 815 (condition-case error
608b9ed2
RS
816 (progn (rmail-input desktop-buffer-file-name)
817 (if (eq major-mode 'rmail-mode)
818 (current-buffer)
819 rmail-buffer))
e2247420
RS
820 (file-locked
821 (kill-buffer (current-buffer))
822 'ignored))))
4a2fce7a 823
0b9bd504 824;; ----------------------------------------------------------------------------
ec4c6f22 825(defun desktop-buffer-mh () "Load a folder in the mh system."
569c754e 826 (if (eq 'mh-folder-mode desktop-buffer-major-mode)
6df8016c 827 (with-no-warnings
ec4c6f22 828 (mh-find-path)
608b9ed2
RS
829 (mh-visit-folder desktop-buffer-name)
830 (current-buffer))))
4a2fce7a 831
ec4c6f22 832;; ----------------------------------------------------------------------------
6343ed61 833(defun desktop-buffer-dired () "Load a directory using dired."
569c754e 834 (if (eq 'dired-mode desktop-buffer-major-mode)
df7eb2bd
JB
835 ;; First element of `desktop-buffer-misc' is the value of `dired-directory'.
836 ;; This value is a directory name, optionally with with shell wildcard or
837 ;; a directory name followed by list of files.
838 (let* ((dired-directory (car desktop-buffer-misc))
839 (dir (if (consp dired-directory) (car dired-directory) dired-directory)))
840 (if (file-directory-p (file-name-directory dir))
841 (progn
842 (dired dired-directory)
843 (mapcar 'dired-maybe-insert-subdir (cdr desktop-buffer-misc))
844 (current-buffer))
845 (message "Directory %s no longer exists." dir)
846 (sit-for 1)
847 'ignored))))
4a2fce7a 848
0b9bd504 849;; ----------------------------------------------------------------------------
84b538ec
JB
850(defun desktop-buffer-file ()
851 "Load a file."
569c754e
RS
852 (if desktop-buffer-file-name
853 (if (or (file-exists-p desktop-buffer-file-name)
6343ed61 854 (and desktop-missing-file-warning
0b9bd504
RS
855 (y-or-n-p (format
856 "File \"%s\" no longer exists. Re-create? "
569c754e 857 desktop-buffer-file-name))))
80f9f3db
SM
858 (let ((buf (find-file-noselect desktop-buffer-file-name)))
859 (condition-case nil
860 (switch-to-buffer buf)
0a8c8225 861 (error (pop-to-buffer buf)))
84b538ec
JB
862 (and (not (eq major-mode desktop-buffer-major-mode))
863 (functionp desktop-buffer-major-mode)
864 (funcall desktop-buffer-major-mode))
0a8c8225 865 buf)
6343ed61 866 'ignored)))
4a2fce7a 867
0b9bd504
RS
868;; ----------------------------------------------------------------------------
869;; Create a buffer, load its file, set is mode, ...; called from Desktop file
6343ed61 870;; only.
1d500ca6 871
4a2fce7a
RS
872(eval-when-compile ; Just to silence the byte compiler
873 (defvar desktop-first-buffer) ;; Dynamically bound in `desktop-read'
874)
875
876(defun desktop-create-buffer (
877 desktop-file-version
878 desktop-buffer-file-name
879 desktop-buffer-name
880 desktop-buffer-major-mode
881 desktop-buffer-minor-modes
882 desktop-buffer-point
883 desktop-buffer-mark
884 desktop-buffer-read-only
885 desktop-buffer-misc
886 &optional
887 desktop-buffer-locals)
888 ;; To make desktop files with relative file names possible, we cannot
889 ;; allow `default-directory' to change. Therefore we save current buffer.
890 (save-current-buffer
891 (let (
892 (buffer-list (buffer-list))
893 (hlist desktop-buffer-handlers)
894 (result)
895 (handler)
896 )
897 ;; Call desktop-buffer-handlers to create buffer.
898 (while (and (not result) hlist)
899 (setq handler (car hlist))
900 (setq result (funcall handler))
901 (setq hlist (cdr hlist)))
902 (unless (bufferp result) (setq result nil))
7bcbf3c2
JB
903 ;; Restore buffer list order with new buffer at end. Don't change
904 ;; the order for old desktop files (old desktop module behaviour).
4a2fce7a 905 (unless (< desktop-file-version 206)
7bcbf3c2
JB
906 (mapcar 'bury-buffer buffer-list)
907 (when result (bury-buffer result)))
4a2fce7a 908 (when result
7bcbf3c2
JB
909 (unless (or desktop-first-buffer (< desktop-file-version 206))
910 (setq desktop-first-buffer result))
4a2fce7a
RS
911 (set-buffer result)
912 (unless (equal (buffer-name) desktop-buffer-name)
913 (rename-buffer desktop-buffer-name))
914 ;; minor modes
915 (cond (
916 ;; backwards compatible
917 (equal '(t) desktop-buffer-minor-modes)
918 (auto-fill-mode 1))(
919 (equal '(nil) desktop-buffer-minor-modes)
920 (auto-fill-mode 0))(
921 t
922 (mapcar
923 #'(lambda (minor-mode)
924 (when (functionp minor-mode) (funcall minor-mode 1)))
925 desktop-buffer-minor-modes)))
926 ;; Even though point and mark are non-nil when written by `desktop-save'
73b0b745 927 ;; they may be modified by handlers wanting to set point or mark themselves.
4a2fce7a
RS
928 (when desktop-buffer-point (goto-char desktop-buffer-point))
929 (when desktop-buffer-mark
930 (if (consp desktop-buffer-mark)
931 (progn
932 (set-mark (car desktop-buffer-mark))
933 (setq mark-active (car (cdr desktop-buffer-mark))))
934 (set-mark desktop-buffer-mark)))
935 ;; Never override file system if the file really is read-only marked.
936 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
937 (while desktop-buffer-locals
938 (let ((this (car desktop-buffer-locals)))
939 (if (consp this)
940 ;; an entry of this form `(symbol . value)'
941 (progn
942 (make-local-variable (car this))
943 (set (car this) (cdr this)))
944 ;; an entry of the form `symbol'
945 (make-local-variable this)
946 (makunbound this)))
947 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
ec4c6f22 948
4a2fce7a 949;; ----------------------------------------------------------------------------
ec4c6f22 950;; Backward compatibility -- update parameters to 205 standards.
569c754e
RS
951(defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
952 desktop-buffer-major-mode
2699e23e 953 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
569c754e 954 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
2699e23e
RS
955 desktop-buffer-major-mode (cdr mim) pt mk ro
956 desktop-buffer-misc
ec4c6f22
RS
957 (list (cons 'truncate-lines tl)
958 (cons 'fill-column fc)
959 (cons 'case-fold-search cfs)
960 (cons 'case-replace cr)
961 (cons 'overwrite-mode (car mim)))))
84b538ec 962
0b9bd504 963;; ----------------------------------------------------------------------------
4a2fce7a
RS
964;; When `desktop-enable' is non-nil and "--no-desktop" is not specified on the
965;; command line, we do the rest of what it takes to use desktop, but do it
966;; after finishing loading the init file.
967;; We cannot use `command-switch-alist' to process "--no-desktop" because these
968;; functions are processed after `after-init-hook'.
969(add-hook
970 'after-init-hook
971 '(lambda ()
972 (let ((key "--no-desktop"))
973 (if (member key command-line-args)
974 (delete key command-line-args)
975 (when desktop-enable
976 (desktop-load-default)
977 (desktop-read))))))
813dbb2d 978
ab1d55ea 979(provide 'desktop)
6343ed61 980
80f9f3db 981;;; desktop.el ends here