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