*** empty log message ***
[gnus] / lisp / custom.el
1 ;;; custom.el --- User friendly customization support.
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3 ;;
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: help
6 ;; Version: 0.3
7
8 ;;; Commentary:
9 ;;
10 ;; WARNING: This package is still under construction and not all of
11 ;; the features below are implemented.
12 ;;
13 ;; This package provides a framework for adding user friendly
14 ;; customization support to Emacs.  Having to do customization by
15 ;; editing a text file in some arcane syntax is user hostile in the
16 ;; extreme, and to most users emacs lisp definitely count as arcane.
17 ;;
18 ;; The intension is that authors of emacs lisp packages declare the
19 ;; variables intended for user customization with `custom-declare'.
20 ;; Custom can then automatically generate a customization buffer with
21 ;; `custom-buffer-create' where the user can edit the package
22 ;; variables in a simple and intuitive way, as well as a menu with
23 ;; `custom-menu-create' where he can set the more commonly used
24 ;; variables interactively.
25 ;;
26 ;; It is also possible to use custom for modifying the properties of
27 ;; other objects than the package itself, by specifying extra optional
28 ;; arguments to `custom-buffer-create'.
29 ;;
30 ;; Custom is inspired by OPEN LOOK property windows.
31
32 ;;; Todo:  
33 ;;
34 ;; - Toggle documentation in three states `none', `one-line', `full'.
35 ;; - Function to generate a XEmacs menu from a CUSTOM.
36 ;; - Write TeXinfo documentation.
37 ;; - Make it possible to hide sections by clicking at the level.
38 ;; - Declare AUC TeX variables.
39 ;; - Declare (ding) Gnus variables.
40 ;; - Declare Emacs variables.
41 ;; - Implement remaining types.
42 ;; - XEmacs port.
43 ;; - Allow `URL', `info', and internal hypertext buttons.
44
45 ;;; Code:
46
47 ;;; Compatibility:
48
49 (or (fboundp 'buffer-substring-no-properties)
50     ;; Introduced in Emacs 19.29.
51     (defun buffer-substring-no-properties (beg end)
52       "Return the text from BEG to END, without text properties, as a string."
53       (let ((string (buffer-substring beg end)))
54         (set-text-properties 0 (length string) nil string)
55         string)))
56
57 (or (fboundp 'add-to-list)
58     ;; Introduced in Emacs 19.29.
59     (defun add-to-list (list-var element)
60       "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
61 If you want to use `add-to-list' on a variable that is not defined
62 until a certain package is loaded, you should put the call to `add-to-list'
63 into a hook function that will be run only after loading the package.
64 `eval-after-load' provides one way to do this.  In some cases
65 other hooks, such as major mode hooks, can do the job."
66       (or (member element (symbol-value list-var))
67           (set list-var (cons element (symbol-value list-var))))))
68
69 (or (fboundp 'plist-get)
70     ;; Introduced in Emacs 19.29.
71     (defun plist-get (plist prop)
72       "Extract a value from a property list.
73 PLIST is a property list, which is a list of the form
74 \(PROP1 VALUE1 PROP2 VALUE2...).  This function returns the value
75 corresponding to the given PROP, or nil if PROP is not
76 one of the properties on the list."
77       (let (result)
78         (while plist
79           (if (eq (car plist) prop)
80               (setq result (car (cdr plist))
81                     plist nil)
82             (set plist (cdr (cdr plist)))))
83         result)))
84
85 (or (fboundp 'plist-put)
86     ;; Introduced in Emacs 19.29.
87     (defun plist-put (plist prop val)    
88       "Change value in PLIST of PROP to VAL.
89 PLIST is a property list, which is a list of the form
90 \(PROP1 VALUE1 PROP2 VALUE2 ...).  PROP is a symbol and VAL is any object.
91 If PROP is already a property on the list, its value is set to VAL,
92 otherwise the new PROP VAL pair is added.  The new plist is returned;
93 use `(setq x (plist-put x prop val))' to be sure to use the new value.
94 The PLIST is modified by side effects."
95       (while plist
96         (cond ((eq (car plist) prop)
97                (setcar (cdr plist) val)
98                (setq plist nil))
99               ((null (cdr (cdr plist)))
100                (setcdr (cdr plist) (list prop val))
101                (setq plist nil))
102               (t
103                (setq plist (cdr (cdr plist))))))))
104
105 (or (fboundp 'match-string)
106     ;; Introduced in Emacs 19.29.
107     (defun match-string (num &optional string)
108   "Return string of text matched by last search.
109 NUM specifies which parenthesized expression in the last regexp.
110  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
111 Zero means the entire text matched by the whole regexp or whole string.
112 STRING should be given if the last search was by `string-match' on STRING."
113   (if (match-beginning num)
114       (if string
115           (substring string (match-beginning num) (match-end num))
116         (buffer-substring (match-beginning num) (match-end num))))))
117
118 (or (fboundp 'facep)
119     ;; Introduced in Emacs 19.29.
120     (defun facep (x)
121   "Return t if X is a face name or an internal face vector."
122   (and (or (internal-facep x)
123            (and (symbolp x) (assq x global-face-data)))
124        t)))
125       
126 (or (fboundp 'modify-face)
127     ;; Introduced in Emacs 19.29.
128     (defun modify-face (face foreground background stipple
129                              bold-p italic-p underline-p)
130   "Change the display attributes for face FACE.
131 FOREGROUND and BACKGROUND should be color strings or nil.
132 STIPPLE should be a stipple pattern name or nil.
133 BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
134 in italic, and underlined, respectively.  (Yes if non-nil.)
135 If called interactively, prompts for a face and face attributes."
136   (interactive
137    (let* ((completion-ignore-case t)
138           (face        (symbol-name (read-face-name "Modify face: ")))
139           (colors      (mapcar 'list x-colors))
140           (stipples    (mapcar 'list
141                                (apply 'nconc
142                                       (mapcar 'directory-files
143                                               x-bitmap-file-path))))
144           (foreground  (modify-face-read-string
145                         face (face-foreground (intern face))
146                         "foreground" colors))
147           (background  (modify-face-read-string
148                         face (face-background (intern face))
149                         "background" colors))
150           (stipple     (modify-face-read-string
151                         face (face-stipple (intern face))
152                         "stipple" stipples))
153           (bold-p      (y-or-n-p (concat "Set face " face " bold ")))
154           (italic-p    (y-or-n-p (concat "Set face " face " italic ")))
155           (underline-p (y-or-n-p (concat "Set face " face " underline "))))
156      (message "Face %s: %s" face
157       (mapconcat 'identity
158        (delq nil
159         (list (and foreground (concat (downcase foreground) " foreground"))
160               (and background (concat (downcase background) " background"))
161               (and stipple (concat (downcase stipple) " stipple"))
162               (and bold-p "bold") (and italic-p "italic")
163               (and underline-p "underline"))) ", "))
164      (list (intern face) foreground background stipple
165            bold-p italic-p underline-p)))
166   (condition-case nil (set-face-foreground face foreground) (error nil))
167   (condition-case nil (set-face-background face background) (error nil))
168   (condition-case nil (set-face-stipple face stipple) (error nil))
169   (funcall (if bold-p 'make-face-bold 'make-face-unbold) face nil t)
170   (funcall (if italic-p 'make-face-italic 'make-face-unitalic) face nil t)
171   (set-face-underline-p face underline-p)
172   (and (interactive-p) (redraw-display))))
173
174
175 ;; We can't easily check for a working intangible.
176 (defvar intangible nil
177   "The symbol making text intangible")
178
179 (if (and (boundp 'emacs-minor-version)
180          (or (> emacs-major-version 19)
181              (and (> emacs-major-version 18)
182                   (> emacs-minor-version 28))))
183     (setq intangible 'intangible)
184   (setq intangible 'intangible-if-it-had-been-working))
185
186 ;; Put it in the Help menu, if possible.
187 (condition-case nil
188     ;; This will not work under XEmacs.
189     (global-set-key [ menu-bar help customize ] '("Customize..." . customize))
190   (error nil))
191
192 ;;; External Data:
193 ;; 
194 ;; The following functions and variables defines the interface for
195 ;; connecting a CUSTOM with an external entity, by default an emacs
196 ;; lisp variable.
197
198 (defvar custom-external 'default-value
199   "Function returning the external value of NAME.")
200
201 (defvar custom-external-set 'set-default
202   "Function setting the external value of NAME to VALUE.")
203
204 (defun custom-external (name)
205   "Get the external value associated with NAME."
206   (funcall custom-external name))
207
208 (defun custom-external-set (name value)
209   "Set the external value associated with NAME to VALUE."
210   (funcall custom-external-set name value))
211
212 (defvar custom-name-fields nil
213   "Alist of custom names and their associated editing field.")
214 (make-variable-buffer-local 'custom-name-fields)
215
216 (defun custom-name-enter (name field)
217   "Associate NAME with FIELD."
218   (if (null name)
219       ()
220     (custom-assert 'field)
221     (setq custom-name-fields (cons (cons name field) custom-name-fields))))
222
223 (defun custom-name-field (name)
224   "The editing field associated with NAME."
225   (cdr (assq name custom-name-fields)))
226
227 (defun custom-name-value (name)
228   "The value currently displayed for NAME in the customization buffer."
229   (let* ((field (custom-name-field name))
230          (custom (custom-field-custom field)))
231     (funcall (custom-property custom 'export)
232              (car (custom-field-extract custom field)))))
233
234 ;;; Custom Functions:
235 ;;
236 ;; The following functions are part of the public interface to the
237 ;; CUSTOM datastructure.  Each CUSTOM describes a group of variables,
238 ;; a single variable, or a component of a structured variable.  The
239 ;; CUSTOM instances are part of two hiearachies, the first is the
240 ;; `part-of' hierarchy in which each CUSTOM is a component of another
241 ;; CUSTOM, except for the top level CUSTOM which is contained in
242 ;; `custom-data'.  The second hiearachy is a `is-a' type hierarchy
243 ;; where each CUSTOM is a leaf in the hierarchy defined by the `type'
244 ;; property and `custom-type-properties'.
245
246 (defvar custom-file "~/.custom.el"
247   "Name of file with customization information.")
248
249 (defconst custom-data
250   '((tag . "Emacs")
251     (doc . "The extensible self-documenting text editor.")
252     (type . group)
253     (data "\n"
254           ((header . nil)
255            (compact . t)
256            (type . group)
257            (doc . "\
258 Press [Save] to save any changes permanently after you are done editing.  
259 You can load customization information from other files by editing the
260 `File' field and pressing the [Load] button.  When you press [Save] the
261 customization information of all files you have loaded, plus any
262 changes you might have made manually, will be stored in the file 
263 specified by the `File' field.")
264            (data ((tag . "Load")
265                   (type . button)
266                   (query . custom-load))
267                  ((tag . "Save")
268                   (type . button)
269                   (query . custom-save))
270                  ((name . custom-file)
271                   (default . "~/.custom.el")
272                   (doc . "Name of file with customization information.\n")
273                   (tag . "File")
274                   (type . file))))))
275   "The global customization information.  
276 A custom association list.")
277
278 (defun custom-declare (path custom)
279   "Declare variables for customization.  
280 PATH is a list of tags leading to the place in the customization
281 hierarchy the new entry should be added.  CUSTOM is the entry to add."
282   (custom-initialize custom)
283   (let ((current (custom-travel-path custom-data path)))
284     (or (member custom (custom-data current))
285         (nconc (custom-data current) (list custom)))))
286
287 (put 'custom-declare 'lisp-indent-hook 1)
288
289 (defconst custom-type-properties
290   '((repeat (type . default)
291             (accept . custom-repeat-accept)
292             (extract . custom-repeat-extract)
293             (validate . custom-repeat-validate)
294             (insert . custom-repeat-insert)
295             (match . custom-repeat-match)
296             (query . custom-repeat-query)
297             (prefix . "")
298             (del-tag . "[DEL]")
299             (add-tag . "[INS]"))
300     (pair (type . group)
301           (valid . (lambda (c d) (consp d)))
302           (extract . custom-pair-extract))
303     (list (type . group)
304           (valid . (lambda (c d) (listp d)))
305           (quote . custom-list-quote)
306           (extract . custom-list-extract))
307     (group (type . default)
308            (face-tag . nil)
309            (initialize . custom-group-initialize)
310            (apply . custom-group-apply)
311            (reset . custom-group-reset)
312            (factory-reset . custom-group-factory-reset)
313            (extract . nil)
314            (validate . custom-group-validate)
315            (query . custom-toggle-hide)
316            (accept . custom-group-accept)
317            (insert . custom-group-insert)
318            (find . custom-group-find))
319     (toggle (type . choice)
320             (data ((type . const)
321                    (tag . "On ")
322                    (default . t))
323                   ((type . const)
324                    (tag . "Off")
325                    (default . nil))))
326     (choice (type . default)
327             (query . custom-choice-query)
328             (accept . custom-choice-accept)
329             (extract . custom-choice-extract)
330             (validate . custom-choice-validate)
331             (insert . custom-choice-insert)
332             (none (tag . "Unknown")
333                   (default . __uninitialized__)
334                   (type . const)))
335     (const (type . default)
336            (extract . (lambda (c f) (list (custom-default c))))
337            (validate . (lambda (c f) nil))
338            (valid . custom-const-valid)
339            (update . custom-const-update)
340            (insert . custom-const-insert))
341     (face-doc (type . doc)
342               (doc . "\
343 You can customize the look of Emacs by deciding which faces should be
344 used when.  If you push one of the face buttons below, you will be
345 given a choice between a number of standard faces.  The name of the
346 selected face is shown right after the face button, and it is
347 displayed its own face so you can see how it looks.  If you know of
348 another standard face not listed and want to use it, you can select
349 `Other' and write the name in the editing field.
350
351 If none of the standard faces suits you, you can select `Customize' to
352 create your own face.  This will make six fields appear under the face
353 button.  The `Fg' and `Bg' fields are the foreground and background
354 colors for the face, respectively.  You should type the name of the
355 color in the field.  You can use any X11 color name.  A list of X11
356 color names may be available in the file `/usr/lib/X11/rgb.txt' on
357 your system.  The special color name `default' means that the face
358 will not change the color of the text.  The `Stipple' field is weird,
359 so just ignore it.  The three remaining fields are toggles, which will
360 make the text `bold', `italic', or `underline' respectively.  For some
361 fonts `bold' or `italic' will not make any visible change."))
362     (face (type . choice)
363           (quote . custom-face-quote)
364           (export . custom-face-export)
365           (import . custom-face-import)
366           (data ((tag . "None")
367                  (default . nil)
368                  (type . const))
369                 ((tag . "Default")
370                  (default . default)
371                  (face . custom-const-face)
372                  (type . const))
373                 ((tag . "Bold")
374                  (default . bold)
375                  (face . custom-const-face)
376                  (type . const))
377                 ((tag . "Bold-italic")
378                  (default . bold-italic)
379                  (face . custom-const-face)
380                  (type . const))
381                 ((tag . "Italic")
382                  (default . italic)
383                  (face . custom-const-face)
384                  (type . const))
385                 ((tag . "Underline")
386                  (default . underline)
387                  (face . custom-const-face)
388                  (type . const))
389                 ((tag . "Highlight")
390                  (default . highlight)
391                  (face . custom-const-face)
392                  (type . const))
393                 ((tag . "Modeline")
394                  (default . modeline)
395                  (face . custom-const-face)
396                  (type . const))
397                 ((tag . "Region")
398                  (default . region)
399                  (face . custom-const-face)
400                  (type . const))
401                 ((tag . "Secondary Selection")
402                  (default . secondary-selection)
403                  (face . custom-const-face)
404                  (type . const))
405                 ((tag . "Customized")
406                  (compact . t)
407                  (face-tag . custom-face-hack)
408                  (export . custom-face-export)
409                  (data ((hidden . t)
410                         (tag . "")
411                         (doc . "\
412 Select the properties you want this face to have.")
413                         (default . custom-face-lookup)
414                         (type . const))
415                        "\n"
416                        ((tag . "Fg")
417                         (hidden . t)
418                         (default . "default")
419                         (width . 20)
420                         (type . string))
421                        ((tag . "Bg")
422                         (default . "default")
423                         (width . 20)
424                         (type . string))
425                        ((tag . "Stipple")
426                         (default . "default")
427                         (width . 20)
428                         (type . string))
429                        "\n"
430                        ((tag . "Bold")
431                         (default . nil)
432                         (type . toggle))
433                        "              "
434                        ((tag . "Italic")
435                         (default . nil)
436                         (type . toggle))
437                        "             "
438                        ((tag . "Underline")
439                         (hidden . t)
440                         (default . nil)
441                         (type . toggle)))
442                  (default . (custom-face-lookup "default" "default" "default"
443                                                 nil nil nil))
444                  (type . list))
445                 ((prompt . "Other")
446                  (face . custom-field-value)
447                  (type . symbol))))
448     (file (type . string)
449           (directory . nil)
450           (default-file . nil)
451           (query . custom-file-query))
452     (sexp (type . default)
453           (width . 40)
454           (default . (__uninitialized__ . "Uninitialized"))
455           (valid . custom-sexp-valid)
456           (quote . custom-sexp-quote)
457           (read . custom-sexp-read)
458           (write . custom-sexp-write))
459     (symbol (type . default)
460             (width . 40)
461             (valid . (lambda (c d) (symbolp d)))
462             (quote . custom-symbol-quote)
463             (read . custom-symbol-read)
464             (write . custom-symbol-write))
465     (integer (type . default)
466              (width . 10)
467              (valid . (lambda (c d) (integerp d)))
468              (allow-padding . nil)
469              (read . custom-integer-read)
470              (write . custom-integer-write))
471     (string (type . default)
472             (width . 40) 
473             (valid . (lambda (c d) (stringp d)))
474             (read . custom-string-read)
475             (write . custom-string-write))
476     (button (type . default)
477             (accept . ignore)
478             (extract . nil)
479             (validate . ignore)
480             (insert . custom-button-insert))
481     (doc (type . default)
482          (header . nil)
483          (reset . ignore)
484          (extract . nil)
485          (validate . ignore)
486          (insert . custom-documentation-insert))
487     (default (width . 20)
488              (valid . (lambda (c v) t))
489              (insert . custom-default-insert)
490              (update . custom-default-update)
491              (query . custom-default-query)
492              (tag . nil)
493              (prompt . nil)
494              (doc . nil)
495              (header . t)
496              (padding . ? )
497              (allow-padding . t)
498              (quote . identity)
499              (export . identity)
500              (import . identity)
501              (synchronize . ignore)
502              (initialize . custom-default-initialize)
503              (extract . custom-default-extract)
504              (validate . custom-default-validate)
505              (apply . custom-default-apply)
506              (reset . custom-default-reset)
507              (factory-reset . custom-default-factory-reset)
508              (accept . custom-default-accept)
509              (match . custom-default-match)
510              (name . nil)
511              (compact . nil)
512              (hidden . nil)
513              (face . custom-default-face)
514              (data . nil)
515              (default . __uninitialized__)))
516   "Alist of default properties for type symbols.
517 The format is `((SYMBOL (PROPERTY . VALUE)... )... )'.")
518
519 (defconst custom-local-type-properties nil
520   "Local type properties.")
521 (make-variable-buffer-local 'custom-local-type-properties)
522
523 (defconst custom-nil '__uninitialized__
524   "Special value representing an uninitialized field.")
525
526 (defun custom-property (custom property)
527   "Extract from CUSTOM property PROPERTY."
528   (let ((entry (assq property custom)))
529     (while (null entry)
530       ;; Look in superclass.
531       (let ((type (custom-type custom)))
532         (setq custom (cdr (or (assq type custom-local-type-properties)
533                               (assq type custom-type-properties)))
534               entry (assq property custom))
535         (custom-assert 'custom)))
536     (cdr entry)))
537
538 (defun custom-property-set (custom property value)
539   "Set CUSTOM PROPERY to VALUE by side effect.
540 CUSTOM must have at least one property already."
541   (let ((entry (assq property custom)))
542     (if entry
543         (setcdr entry value)
544       (setcdr custom (cons (cons property value) (cdr custom))))))
545
546 (defun custom-type (custom)
547   "Extract `type' from CUSTOM."
548   (cdr (assq 'type custom)))
549
550 (defun custom-name (custom)
551   "Extract `name' from CUSTOM."
552   (custom-property custom 'name))
553
554 (defun custom-tag (custom)
555   "Extract `tag' from CUSTOM."
556   (custom-property custom 'tag))
557
558 (defun custom-prompt (custom)
559   "Extract `prompt' from CUSTOM.  
560 If none exist, default to `tag' or, failing that, `type'."
561   (or (custom-property custom 'prompt)
562       (custom-property custom 'tag)
563       (capitalize (symbol-name (custom-type custom)))))
564
565 (defun custom-default (custom)
566   "Extract `default' from CUSTOM."
567   (custom-property custom 'default))
568
569 (defun custom-data (custom)
570   "Extract the `data' from CUSTOM."
571   (custom-property custom 'data))
572
573 (defun custom-documentation (custom)
574   "Extract `doc' from CUSTOM."
575   (custom-property custom 'doc))
576
577 (defun custom-width (custom)
578   "Extract `width' from CUSTOM."
579   (custom-property custom 'width))
580
581 (defun custom-compact (custom)
582   "Extract `compact' from CUSTOM."
583   (custom-property custom 'compact))
584
585 (defun custom-padding (custom)
586   "Extract `padding' from CUSTOM."
587   (custom-property custom 'padding))
588
589 (defun custom-allow-padding (custom)
590   "Extract `allow-padding' from CUSTOM."
591   (custom-property custom 'allow-padding))
592
593 (defun custom-valid (custom value)
594   "Non-nil if CUSTOM may legally be set to VALUE."
595   (funcall (custom-property custom 'valid) custom value))
596
597 (defun custom-import (custom value)
598   "Import CUSTOM VALUE from external variable."
599   (funcall (custom-property custom 'import) value))
600
601 (defun custom-quote (custom value)
602   "Quote CUSTOM's VALUE if necessary."
603   (funcall (custom-property custom 'quote) value))
604
605 (defun custom-write (custom value)
606   "Convert CUSTOM VALUE to a string."
607   (if (eq value custom-nil) 
608       ""
609     (funcall (custom-property custom 'write) custom value)))
610
611 (defun custom-read (custom string)
612   "Convert CUSTOM field content STRING into external form."
613   (funcall (custom-property custom 'read) custom string))
614
615 (defun custom-match (custom values)
616   "Match CUSTOM with a list of VALUES.
617 Return a cons-cell where the car is the sublist of VALUES matching CUSTOM,
618 and the cdr is the remaining VALUES."
619   (if (memq values (list custom-nil nil))
620       (cons custom-nil nil)
621     (funcall (custom-property custom 'match) custom values)))
622
623 (defun custom-initialize (custom)
624   "Initialize `doc' and `default' attributes of CUSTOM."
625   (funcall (custom-property custom 'initialize) custom))
626
627 (defun custom-find (custom tag)
628   "Find child in CUSTOM with `tag' TAG."
629   (funcall (custom-property custom 'find) custom tag))
630
631 (defun custom-travel-path (custom path)
632   "Find decedent of CUSTOM by looking through PATH."
633   (if (null path)
634       custom
635     (custom-travel-path (custom-find custom (car path)) (cdr path))))
636
637 (defun custom-field-extract (custom field)
638   "Extract CUSTOM's value in FIELD."
639   (if (stringp custom)
640       nil
641     (funcall (custom-property (custom-field-custom field) 'extract)
642              custom field)))
643
644 (defun custom-field-validate (custom field)
645   "Validate CUSTOM's value in FIELD.
646 Return nil if valid, otherwise return a cons-cell where the car is the
647 position of the error, and the cdr is a text describing the error."
648   (if (stringp custom)
649       nil
650     (funcall (custom-property custom 'validate) custom field)))
651
652 ;;; Field Functions:
653 ;;
654 ;; This section defines the public functions for manipulating the
655 ;; FIELD datatype.  The FIELD instance hold information about a
656 ;; specific editing field in the customization buffer.
657 ;;
658 ;; Each FIELD can be seen as an instanciation of a CUSTOM.
659
660 (defun custom-field-create (custom value)
661   "Create a field structure of type CUSTOM containing VALUE.
662
663 A field structure is an array [ CUSTOM VALUE ORIGINAL START END ], where
664 CUSTOM defines the type of the field, 
665 VALUE is the current value of the field,
666 ORIGINAL is the original value when created, and
667 START and END are markers to the start and end of the field."
668   (vector custom value custom-nil nil nil))
669
670 (defun custom-field-custom (field)
671   "Return the `custom' attribute of FIELD."
672   (aref field 0))
673   
674 (defun custom-field-value (field)
675   "Return the `value' attribute of FIELD."
676   (aref field 1))
677
678 (defun custom-field-original (field)
679   "Return the `original' attribute of FIELD."
680   (aref field 2))
681
682 (defun custom-field-start (field)
683   "Return the `start' attribute of FIELD."
684   (aref field 3))
685
686 (defun custom-field-end (field)
687   "Return the `end' attribute of FIELD."
688   (aref field 4))
689   
690 (defun custom-field-value-set (field value)
691   "Set the `value' attribute of FIELD to VALUE."
692   (aset field 1 value))
693
694 (defun custom-field-original-set (field original)
695   "Set the `original' attribute of FIELD to ORIGINAL."
696   (aset field 2 original))
697
698 (defun custom-field-move (field start end)
699   "Set the `start'and `end' attributes of FIELD to START and END."
700   (set-marker (or (aref field 3) (aset field 3 (make-marker))) start)
701   (set-marker (or (aref field 4) (aset field 4 (make-marker))) end))
702
703 (defun custom-field-query (field)
704   "Query user for content of current field."
705   (funcall (custom-property (custom-field-custom field) 'query) field))
706
707 (defun custom-field-accept (field value &optional original)
708   "Accept FIELD VALUE.  
709 If optional ORIGINAL is non-nil, concider VALUE for the original value."
710   (funcall (custom-property (custom-field-custom field) 'accept) 
711            field value original))
712
713 (defun custom-field-face (field)
714   "The face used for highlighting FIELD."
715   (let ((custom (custom-field-custom field)))
716     (if (stringp custom)
717         nil
718       (funcall (custom-property custom 'face) field))))
719
720 (defun custom-field-update (field)
721   "Update content of FIELD."
722   (let ((custom (custom-field-custom field)))
723     (if (stringp custom)
724         nil
725       (funcall (custom-property custom 'update) field))))
726
727 ;;; Types:
728 ;;
729 ;; The following functions defines type specific actions.
730
731 (defun custom-repeat-accept (field value &optional original)
732   "Enter content of editing FIELD."
733   (let ((values (copy-sequence (custom-field-value field)))
734         (all (custom-field-value field))
735         (start (custom-field-start field))
736         current new)
737     (if original 
738         (custom-field-original-set field value))
739     (while (consp value)
740       (setq new (car value)
741             value (cdr value))
742       (if values
743           ;; Change existing field.
744           (setq current (car values)
745                 values (cdr values))
746         ;; Insert new field if series has grown.
747         (goto-char start)
748         (setq current (custom-repeat-insert-entry field))
749         (setq all (custom-insert-before all nil current))
750         (custom-field-value-set field all))
751       (custom-field-accept current new original))
752     (while (consp values)
753       ;; Delete old field if series has scrunk.
754       (setq current (car values)
755             values (cdr values))
756       (let ((pos (custom-field-start current))
757             data)
758         (while (not data)
759           (setq pos (previous-single-property-change pos 'custom-data))
760           (custom-assert 'pos)
761           (setq data (get-text-property pos 'custom-data))
762           (or (and (arrayp data)
763                    (> (length data) 1)
764                    (eq current (aref data 1)))
765               (setq data nil)))
766         (custom-repeat-delete data)))))
767
768 (defun custom-repeat-insert (custom level)
769   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
770   (let* ((field (custom-field-create custom nil))
771          (add-tag (custom-property custom 'add-tag))
772          (del-tag (custom-property custom 'del-tag))
773          (start (make-marker))
774          (data (vector field nil start nil)))
775     (custom-text-insert "\n")
776     (let ((pos (point)))
777       (custom-text-insert (custom-property custom 'prefix))
778       (custom-tag-insert add-tag 'custom-repeat-add data)
779       (set-marker start pos))
780     (custom-field-move field start (point))
781     (custom-documentation-insert custom)
782     field))
783
784 (defun custom-repeat-insert-entry (repeat)
785   "Insert entry at point in the REPEAT field."
786   (let* ((inhibit-point-motion-hooks t)
787          (inhibit-read-only t)
788          (before-change-functions nil)
789          (after-change-functions nil)
790          (custom (custom-field-custom repeat))
791          (add-tag (custom-property custom 'add-tag))
792          (del-tag (custom-property custom 'del-tag))
793          (start (make-marker))
794          (end (make-marker))
795          (data (vector repeat nil start end))
796          field)
797     (insert-before-markers "\n")
798     (backward-char 1)
799     (set-marker start (point))
800     (custom-text-insert " ")
801     (aset data 1 (setq field (custom-insert (custom-data custom) nil)))
802     (custom-text-insert " ")
803     (set-marker end (point))
804     (goto-char start)
805     (custom-text-insert (custom-property custom 'prefix))
806     (custom-tag-insert add-tag 'custom-repeat-add data)
807     (custom-text-insert " ")
808     (custom-tag-insert del-tag 'custom-repeat-delete data)
809     (forward-char 1)
810     field))
811
812 (defun custom-repeat-add (data)
813   "Add list entry."
814   (let ((parent (aref data 0))
815         (field (aref data 1))
816         (at (aref data 2))
817         new)
818     (goto-char at)
819     (setq new (custom-repeat-insert-entry parent))
820     (custom-field-value-set parent
821                             (custom-insert-before (custom-field-value parent)
822                                                   field new))))
823
824 (defun custom-repeat-delete (data)
825   "Delete list entry."
826   (let ((inhibit-point-motion-hooks t)
827         (inhibit-read-only t)
828         (before-change-functions nil)
829         (after-change-functions nil)
830         (parent (aref data 0))
831         (field (aref data 1)))
832     (delete-region (aref data 2) (1+ (aref data 3)))
833     (custom-field-untouch (aref data 1))
834     (custom-field-value-set parent 
835                             (delq field (custom-field-value parent)))))
836
837 (defun custom-repeat-match (custom values)
838   "Match CUSTOM with VALUES."
839   (let* ((child (custom-data custom))
840          (match (custom-match child values))
841          matches)
842     (while (not (eq (car match) custom-nil))
843       (setq matches (cons (car match) matches)
844             values (cdr match)
845             match (custom-match child values)))
846     (cons (nreverse matches) values)))
847
848 (defun custom-repeat-extract (custom field)
849   "Extract list of childrens values."
850   (let ((values (custom-field-value field))
851         (data (custom-data custom))
852         result)
853     (if (eq values custom-nil)
854         ()
855       (while values
856         (setq result (append result (custom-field-extract data (car values)))
857               values (cdr values))))
858     result))
859
860 (defun custom-repeat-validate (custom field)
861   "Validate children."
862   (let ((values (custom-field-value field))
863         (data (custom-data custom))
864         result)
865     (if (eq values custom-nil)
866         (setq result (cons (custom-field-start field) "Uninitialized list")))
867     (while (and values (not result))
868       (setq result (custom-field-validate data (car values))
869             values (cdr values)))
870     result))
871
872 (defun custom-pair-extract (custom field)
873   "Extract cons of childrens values."
874   (let ((values (custom-field-value field))
875         (data (custom-data custom))
876         result)
877     (custom-assert '(eq (length values) (length data)))
878     (custom-assert '(eq (length values) 2))
879     (while values
880       (setq result (append result
881                            (custom-field-extract (car data) (car values)))
882             data (cdr data)
883             values (cdr values)))
884     (custom-assert '(null data))
885     (list (cons (nth 0 result) (nth 1 result)))))
886
887 (defun custom-list-quote (value)
888   "Quote VALUE if necessary."
889   (and value
890        (list 'quote value)))
891
892 (defun custom-list-extract (custom field)
893   "Extract list of childrens values."
894   (let ((values (custom-field-value field))
895         (data (custom-data custom))
896         result)
897     (custom-assert '(eq (length values) (length data)))
898     (while values
899       (setq result (append result
900                            (custom-field-extract (car data) (car values)))
901             data (cdr data)
902             values (cdr values)))
903     (custom-assert '(null data))
904     (list result)))
905
906 (defun custom-group-validate (custom field)
907   "Validate children."
908   (let ((values (custom-field-value field))
909         (data (custom-data custom))
910         result)
911     (if (eq values custom-nil)
912         (setq result (cons (custom-field-start field) "Uninitialized list"))
913       (custom-assert '(eq (length values) (length data))))
914     (while (and values (not result))
915       (setq result (custom-field-validate (car data) (car values))
916             data (cdr data)
917             values (cdr values)))
918     result))
919
920 (defun custom-group-initialize (custom)
921   "Initialize `doc' and `default' entries in CUSTOM."
922   (if (custom-name custom)
923       (custom-default-initialize custom)
924     (mapcar 'custom-initialize (custom-data custom))))
925
926 (defun custom-group-apply (field)
927   "Reset `value' in FIELD to `original'."
928   (let ((custom (custom-field-custom field))
929         (values (custom-field-value field)))
930     (if (custom-name custom)
931         (custom-default-apply field)
932       (mapcar 'custom-field-apply values))))
933
934 (defun custom-group-reset (field)
935   "Reset `value' in FIELD to `original'."
936   (let ((custom (custom-field-custom field))
937         (values (custom-field-value field)))
938     (if (custom-name custom)
939         (custom-default-reset field)
940       (mapcar 'custom-field-reset values))))
941
942 (defun custom-group-factory-reset (field)
943   "Reset `value' in FIELD to `default'."
944   (let ((custom (custom-field-custom field))
945         (values (custom-field-value field)))
946     (if (custom-name custom)
947         (custom-default-factory-reset field)
948       (mapcar 'custom-field-factory-reset values))))
949
950 (defun custom-group-find (custom tag)
951   "Find child in CUSTOM with `tag' TAG."
952   (let ((data (custom-data custom))
953         (result nil))
954     (while (not result)
955       (custom-assert 'data)
956       (if (equal (custom-tag (car data)) tag)
957           (setq result (car data))
958         (setq data (cdr data))))))
959
960 (defun custom-group-accept (field value &optional original)
961   "Enter content of editing FIELD with VALUE."
962   (let* ((values (custom-field-value field))
963          (custom (custom-field-custom field))
964          (from (custom-field-start field))
965          (face-tag (custom-property custom 'face-tag))
966          current)
967     (if face-tag 
968         (put-text-property from (+ from (length (custom-tag custom)))
969                            'face (funcall face-tag field value)))
970     (if original 
971         (custom-field-original-set field value))
972     (while values
973       (setq current (car values)
974             values (cdr values))
975       (if current
976           (let* ((custom (custom-field-custom current))
977                  (match (custom-match custom value)))
978             (setq value (cdr match))
979             (custom-field-accept current (car match) original))))))
980
981 (defun custom-group-insert (custom level)
982   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
983   (let* ((field (custom-field-create custom nil))
984          fields hidden
985          (from (point))
986          (compact (custom-compact custom))
987          (tag (custom-tag custom))
988          (face-tag (custom-property custom 'face-tag)))
989     (cond (face-tag (custom-text-insert tag))
990           (tag (custom-tag-insert tag field)))
991     (or compact (custom-documentation-insert custom))
992     (or compact (custom-text-insert "\n"))
993     (let ((data (custom-data custom)))
994       (while data
995         (setq fields (cons (custom-insert (car data) (if level (1+ level)))
996                            fields))
997         (setq hidden (or (stringp (car data))
998                          (custom-property (car data) 'hidden)))
999         (setq data (cdr data))
1000         (if data (custom-text-insert (cond (hidden "")
1001                                            (compact " ")
1002                                            (t "\n"))))))
1003     (if compact (custom-documentation-insert custom))
1004     (custom-field-value-set field (nreverse fields))
1005     (custom-field-move field from (point))
1006     field))
1007
1008 (defun custom-choice-insert (custom level)
1009   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1010   (let* ((field (custom-field-create custom nil))
1011          (from (point))
1012          (tag (custom-tag custom)))
1013     (custom-text-insert "lars er en nisse")
1014     (custom-field-move field from (point))
1015     (custom-documentation-insert custom)
1016     (custom-field-reset field)
1017     field))
1018
1019 (defun custom-choice-accept (field value &optional original)
1020   "Reset content of editing FIELD."
1021   (let ((custom (custom-field-custom field))
1022         (start (custom-field-start field))
1023         (end (custom-field-end field))
1024         (inhibit-read-only t)
1025         (before-change-functions nil)
1026         (after-change-functions nil)
1027         from)
1028     (cond (original 
1029            (setq custom-modified-list (delq field custom-modified-list))
1030            (custom-field-original-set field value))
1031           ((equal value (custom-field-original field))
1032            (setq custom-modified-list (delq field custom-modified-list)))
1033           (t
1034            (add-to-list 'custom-modified-list field)))
1035     (custom-field-untouch (custom-field-value field))
1036     (delete-region start end)
1037     (goto-char start)
1038     (setq from (point))
1039     (insert-before-markers " ")
1040     (backward-char 1)
1041     (set-text-properties (point) (1+ (point)) 
1042                          (list 'invisible t 
1043                                intangible t))
1044     (custom-tag-insert (custom-tag custom) field)
1045     (custom-text-insert ": ")
1046     (let ((data (custom-data custom))
1047           found begin)
1048       (while (and data (not found))
1049         (if (not (custom-valid (car data) value))
1050             (setq data (cdr data))
1051           (setq found (custom-insert (car data) nil))
1052           (setq data nil)))
1053       (if found 
1054           ()
1055         (setq begin (point)
1056               found (custom-insert (custom-property custom 'none) nil))
1057         (add-text-properties begin (point)
1058                              (list 'rear-nonsticky t
1059                                    'face custom-field-uninitialized-face)))
1060       (or original
1061           (custom-field-original-set found (custom-field-original field)))
1062       (custom-field-accept found value original)
1063       (custom-field-value-set field found)
1064       (custom-field-move field from end))))
1065
1066 (defun custom-choice-extract (custom field)
1067   "Extract childs value."
1068   (let ((value (custom-field-value field)))
1069     (custom-field-extract (custom-field-custom value) value)))
1070
1071 (defun custom-choice-validate (custom field)
1072   "Validate childs value."
1073   (let ((value (custom-field-value field))
1074         (custom (custom-field-custom field)))
1075     (if (or (eq value custom-nil)
1076             (eq (custom-field-custom value) (custom-property custom 'none)))
1077         (cons (custom-field-start field) "Make a choice")
1078       (custom-field-validate (custom-field-custom value) value))))
1079
1080 (defun custom-choice-query (field)
1081   "Choose a child."
1082   (let* ((custom (custom-field-custom field))
1083          (old (custom-field-custom (custom-field-value field)))
1084          (default (custom-prompt old))
1085          (tag (custom-prompt custom))
1086          (data (custom-data custom))
1087          current alist)
1088     (if (eq (length data) 2)
1089         (custom-field-accept field (custom-default (if (eq (nth 0 data) old)
1090                                                        (nth 1 data)
1091                                                      (nth 0 data))))
1092       (while data
1093         (setq current (car data)
1094               data (cdr data))
1095         (setq alist (cons (cons (custom-prompt current) current) alist)))
1096       (let ((answer (if (listp last-input-event)
1097                         (x-popup-menu last-input-event
1098                                       (list tag (cons "" (reverse alist))))
1099                       (let ((choice (completing-read (concat tag " (default "
1100                                                              default "): ") 
1101                                                      alist nil t)))
1102                         (if (or (null choice) (string-equal choice ""))
1103                             (setq choice default))
1104                         (cdr (assoc choice alist))))))
1105         (if answer
1106             (custom-field-accept field (custom-default answer)))))))
1107
1108 (defun custom-file-query (field)
1109   "Prompt for a file name"
1110   (let* ((value (custom-field-value field))
1111          (custom (custom-field-custom field))
1112          (valid (custom-valid custom value))
1113          (directory (custom-property custom 'directory))
1114          (default (and (not valid)
1115                        (custom-property custom 'default-file)))
1116          (tag (custom-tag custom))
1117          (prompt (if default
1118                      (concat tag " (" default "): ")
1119                    (concat tag ": "))))
1120     (custom-field-accept field 
1121                          (if (custom-valid custom value)
1122                              (read-file-name prompt 
1123                                              (if (file-name-absolute-p value)
1124                                                  ""
1125                                                directory)
1126                                              default nil value)
1127                            (read-file-name prompt directory default)))))
1128
1129 (defun custom-face-quote (value)
1130   "Quote VALUE if necessary."
1131   (if (symbolp value)
1132       (custom-symbol-quote value)
1133     value))
1134
1135 (defun custom-face-export (value)
1136   "Modify VALUE to match external expectations."
1137   (if (symbolp value)
1138       value
1139     (eval value)))
1140
1141 (defun custom-face-import (value)
1142   "Modify VALUE to match internal expectations."
1143   (let ((name (symbol-name value)))
1144     (if (string-match "\
1145 custom-face-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)"
1146                       name)
1147         (list 'custom-face-lookup 
1148               (match-string 1 name)
1149               (match-string 2 name)
1150               (match-string 3 name)
1151               (intern (match-string 4 name))
1152               (intern (match-string 5 name))
1153               (intern (match-string 6 name)))
1154       value)))
1155
1156 (defun custom-face-lookup (fg bg stipple bold italic underline)
1157   "Lookup or create a face with specified attributes.
1158 FG BG STIPPLE BOLD ITALIC UNDERLINE"
1159   (let ((name (intern (format "custom-face-%s-%s-%s-%S-%S-%S"
1160                               (or fg "default")
1161                               (or bg "default")
1162                               (or stipple "default")
1163                               bold italic underline))))
1164     (if (facep name)
1165         ()
1166       (make-face name)
1167       (modify-face name
1168                    (if (string-equal fg "default") nil fg)
1169                    (if (string-equal bg "default") nil bg)
1170                    (if (string-equal stipple "default") nil stipple)
1171                    bold italic underline))
1172     name))
1173
1174 (defun custom-face-hack (field value)
1175   "Face that should be used for highlighting FIELD containing VALUE."
1176   (funcall (custom-property (custom-field-custom field) 'export) value))
1177
1178 (defun custom-const-insert (custom level)
1179   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1180   (let* ((field (custom-field-create custom custom-nil))
1181          (face (custom-field-face field))
1182          (from (point)))
1183     (custom-text-insert (custom-tag custom))
1184     (add-text-properties from (point) 
1185                          (list 'face face
1186                                'rear-nonsticky t))
1187     (custom-documentation-insert custom)
1188     (custom-field-move field from (point))
1189     field))
1190
1191 (defun custom-const-update (field)
1192   "Update face of FIELD."
1193   (let ((from (custom-field-start field))
1194         (custom (custom-field-custom field)))
1195     (put-text-property from (+ from (length (custom-tag custom)))
1196                        'face (custom-field-face field))))
1197
1198 (defun custom-const-valid (custom value)
1199   "Non-nil if CUSTOM can legally have the value VALUE."
1200   (equal (custom-default custom) value))
1201
1202 (defun custom-const-face (field)
1203   "Face used for a FIELD."
1204   (custom-default (custom-field-custom field)))
1205
1206 (defun custom-sexp-valid (custom value)
1207   "Non-nil if CUSTOM can legally have the value VALUE."
1208   (not (and (listp value) (eq custom-nil (car value)))))
1209
1210 (defun custom-sexp-quote (value)
1211   "Quote VALUE if necessary."
1212   (if (or (and (symbolp value)
1213                value 
1214                (not (eq t value)))
1215           (and (listp value)
1216                value
1217                (not (memq (car value) '(quote function lambda)))))
1218       (list 'quote value)
1219     value))
1220
1221 (defun custom-sexp-read (custom string)
1222   "Read from CUSTOM an STRING."
1223   (save-match-data
1224     (save-excursion
1225       (set-buffer (get-buffer-create " *Custom Scratch*"))
1226       (erase-buffer)
1227       (insert string)
1228       (goto-char (point-min))
1229       (condition-case signal
1230           (prog1 (read (current-buffer))
1231             (or (looking-at
1232                  (concat (regexp-quote (char-to-string
1233                                         (custom-padding custom)))
1234                          "*\\'"))
1235                 (error "Junk at end of expression")))
1236         (error (cons custom-nil string))))))
1237
1238 (defun custom-sexp-write (custom sexp)
1239   "Write CUSTOM SEXP as string."
1240   (if (and (listp sexp) (eq (car sexp) custom-nil))
1241       (cdr sexp)
1242     (prin1-to-string sexp)))
1243
1244 (defun custom-symbol-quote (value)
1245   "Quote VALUE if necessary."
1246   (if (or (null value) (eq t value))
1247       value
1248     (list 'quote value)))
1249
1250 (defun custom-symbol-read (custom symbol)
1251   "Read from CUSTOM an SYMBOL."
1252   (intern (save-match-data
1253             (custom-strip-padding symbol (custom-padding custom)))))
1254
1255 (defun custom-symbol-write (custom symbol)
1256   "Write CUSTOM SYMBOL as string."
1257   (symbol-name symbol))
1258
1259 (defun custom-integer-read (custom integer)
1260   "Read from CUSTOM an INTEGER."
1261   (string-to-int (save-match-data
1262                    (custom-strip-padding integer (custom-padding custom)))))
1263
1264 (defun custom-integer-write (custom integer)
1265   "Write CUSTOM INTEGER as string."
1266   (int-to-string integer))
1267
1268 (defun custom-string-read (custom string)
1269   "Read string by ignoring trailing padding characters."
1270   (let ((last (length string))
1271         (padding (custom-padding custom)))
1272     (while (and (> last 0)
1273                 (eq (aref string (1- last)) padding))
1274       (setq last (1- last)))
1275     (substring string 0 last)))
1276
1277 (defun custom-string-write (custom string)
1278   "Write raw string."
1279   string)
1280
1281 (defun custom-button-insert (custom level)
1282   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1283   (custom-tag-insert (concat "[" (custom-tag custom) "]") 
1284                      (custom-property custom 'query))
1285   (custom-documentation-insert custom)
1286   nil)
1287
1288 (defun custom-default-initialize (custom)
1289   "Initialize `doc' and `default' entries in CUSTOM."
1290   (let ((name (custom-name custom)))
1291     (if (null name)
1292         ()
1293       (let ((default (custom-default custom))
1294             (doc (custom-documentation custom))
1295             (vdoc (documentation-property name 'variable-documentation t)))
1296         (if doc
1297             (or vdoc (put name 'variable-documentation doc))
1298           (if vdoc (custom-property-set custom 'doc vdoc)))
1299         (if (eq default custom-nil)
1300             (if (boundp name)
1301                 (custom-property-set custom 'default (symbol-value name)))
1302           (or (boundp name)
1303               (set name default)))))))
1304
1305 (defun custom-default-insert (custom level)
1306   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1307   (let ((field (custom-field-create custom custom-nil))
1308         (tag (custom-tag custom)))
1309     (if (null tag)
1310         ()
1311       (custom-tag-insert tag field)
1312       (custom-text-insert ": "))
1313     (custom-field-insert field)
1314     (custom-documentation-insert custom)
1315     field))
1316
1317 (defun custom-default-accept (field value &optional original)
1318   "Enter into FIELD the value VALUE."
1319   (if original 
1320       (custom-field-original-set field value))
1321   (custom-field-value-set field value)
1322   (custom-field-update field))
1323   
1324 (defun custom-default-apply (field)
1325   "Apply any changes in FIELD since the last apply."
1326   (let* ((custom (custom-field-custom field))
1327          (name (custom-name custom)))
1328     (if (null name)
1329         (error "This field cannot be applied alone"))
1330     (custom-external-set name (custom-name-value name))
1331     (custom-field-reset field)))
1332
1333 (defun custom-default-reset (field)
1334   "Reset content of editing FIELD to `original'."
1335   (custom-field-accept field (custom-field-original field) t))
1336
1337 (defun custom-default-factory-reset (field)
1338   "Reset content of editing FIELD to `default'."
1339   (let ((default (custom-default (custom-field-custom field))))
1340     (or (eq default custom-nil)
1341         (custom-field-accept field default nil))))
1342
1343 (defun custom-default-query (field)
1344   "Prompt for a FIELD"
1345   (let* ((custom (custom-field-custom field))
1346          (value (custom-field-value field))
1347          (initial (custom-write custom value))
1348          (prompt (concat (custom-prompt custom) ": ")))
1349     (custom-field-accept field 
1350                          (custom-read custom 
1351                                       (if (custom-valid custom value)
1352                                           (read-string prompt (cons initial 1))
1353                                         (read-string prompt))))))
1354
1355 (defun custom-default-match (custom values)
1356   "Match CUSTOM with VALUES."
1357   values)
1358
1359 (defun custom-default-extract (custom field)
1360   "Extract CUSTOM's content in FIELD."
1361   (list (custom-field-value field)))
1362
1363 (defun custom-default-validate (custom field)
1364   "Validate FIELD."
1365   (let ((value (custom-field-value field))
1366         (start (custom-field-start field)))
1367     (cond ((eq value custom-nil)
1368            (cons (custom-field-start field) "Uninitialized field"))
1369           ((custom-valid custom value)
1370            nil)
1371           (t
1372            (cons start "Wrong type")))))
1373
1374 (defun custom-default-face (field)
1375   "Face used for a FIELD."
1376   (let ((value (custom-field-value field)))
1377     (cond ((eq value custom-nil)
1378            custom-field-uninitialized-face)
1379           ((not (custom-valid (custom-field-custom field) value))
1380            custom-field-invalid-face)
1381           ((not (equal (custom-field-original field) value))
1382            custom-field-modified-face)
1383           (t
1384            custom-field-face))))
1385
1386 (defun custom-default-update (field)
1387   "Update the content of FIELD."
1388   (let ((inhibit-point-motion-hooks t)
1389         (before-change-functions nil)
1390         (after-change-functions nil)
1391         (start (custom-field-start field))
1392         (end (custom-field-end field)) 
1393         (pos (point)))
1394     ;; Keep track of how many modified fields we have.
1395     (cond ((equal (custom-field-value field) (custom-field-original field))
1396            (setq custom-modified-list (delq field custom-modified-list)))
1397           ((memq field custom-modified-list))
1398           (t
1399            (setq custom-modified-list (cons field custom-modified-list))))
1400     ;; Update the field.
1401     (goto-char end)
1402     (insert-before-markers " ")
1403     (delete-region start (1- end))
1404     (goto-char start)
1405     (custom-field-insert field)
1406     (goto-char end)
1407     (delete-char 1)
1408     (goto-char pos)
1409     (and (<= start pos) 
1410          (<= pos end)
1411          (custom-field-enter field))))
1412
1413 ;;; Create Buffer:
1414 ;;
1415 ;; Public functions to create a customization buffer and to insert
1416 ;; various forms of text, fields, and buttons in it.
1417
1418 (defun customize ()
1419   "Customize GNU Emacs.
1420 Create a *Customize* buffer with editable customization information
1421 about GNU Emacs." 
1422   (interactive)
1423   (custom-buffer-create "*Customize*")
1424   (custom-reset-all))
1425
1426 (defun custom-buffer-create (name &optional custom types set get)
1427   "Create a customization buffer named NAME.
1428 If the optional argument CUSTOM is non-nil, use that as the custom declaration.
1429 If the optional argument TYPES is non-nil, use that as the local types.
1430 If the optional argument SET is non-nil, use that to set external data.
1431 If the optional argument GET is non-nil, use that to get external data."
1432   (switch-to-buffer name)
1433   (buffer-disable-undo (current-buffer))
1434   (custom-mode)
1435   (setq custom-local-type-properties types)
1436   (if (null custom)
1437       ()
1438     (make-local-variable 'custom-data)
1439     (setq custom-data custom))
1440   (if (null set)
1441       ()
1442     (make-local-variable 'custom-external-set)
1443     (setq custom-external-set set))
1444   (if (null get)
1445       ()
1446     (make-local-variable 'custom-external)
1447     (setq custom-external get))
1448   (let ((inhibit-point-motion-hooks t)
1449         (before-change-functions nil)
1450         (after-change-functions nil))
1451     (erase-buffer)
1452     (insert "\n")
1453     (goto-char (point-min))
1454     (custom-text-insert "This is a customization buffer.\n")
1455     (custom-help-insert "\n")
1456     (custom-help-button 'custom-forward-field)
1457     (custom-help-button 'custom-enter-value)
1458     (custom-help-button 'custom-field-factory-reset)
1459     (custom-help-button 'custom-field-reset)
1460     (custom-help-button 'custom-field-apply)
1461     (custom-help-button 'custom-toggle-documentation)
1462     (custom-help-insert "\nClick mouse-2 on any button to activate it.\n")
1463     (custom-text-insert "\n")
1464     (custom-insert custom-data 0)
1465     (goto-char (point-min))))
1466
1467 (defun custom-insert (custom level)
1468   "Insert custom declaration CUSTOM in current buffer at level LEVEL."
1469   (if (stringp custom)
1470       (progn 
1471         (custom-text-insert custom)
1472         nil)
1473     (and level (null (custom-property custom 'header))
1474          (setq level nil))
1475     (and level 
1476          (> level 0)
1477          (custom-text-insert (concat "\n" (make-string level ?*) " ")))
1478     (let ((field (funcall (custom-property custom 'insert) custom level)))
1479       (custom-name-enter (custom-name custom) field)
1480       field)))
1481
1482 (defun custom-text-insert (text)
1483   "Insert TEXT in current buffer." 
1484   (insert text))
1485
1486 (defun custom-tag-insert (tag field &optional data)
1487   "Insert TAG for FIELD in current buffer."
1488   (let ((from (point)))
1489     (insert tag)
1490     (set-text-properties from (point) 
1491                          (list 'category custom-button-properties
1492                                'custom-tag field))
1493     (if data
1494         (add-text-properties from (point) (list 'custom-data data)))))
1495
1496 (defun custom-documentation-insert (custom &rest ignore)
1497   "Insert documentation from CUSTOM in current buffer."
1498   (let ((doc (custom-documentation custom)))
1499     (if (null doc)
1500         ()
1501       (custom-help-insert "\n" doc))))
1502
1503 (defun custom-help-insert (&rest args)
1504   "Insert ARGS as documentation text."
1505   (let ((from (point)))
1506     (apply 'insert args)
1507     (set-text-properties from (point) 
1508                          (list 'category custom-documentation-properties))))
1509
1510 (defun custom-help-button (command)
1511   "Describe how to execute COMMAND."
1512   (let ((from (point)))
1513     (insert "`" (key-description (where-is-internal command nil t)) "'")
1514     (set-text-properties from (point)
1515                          (list 'category custom-documentation-properties
1516                                'face custom-button-face
1517                                'mouse-face custom-mouse-face
1518                                'custom-tag command)))
1519   (custom-help-insert ": " (custom-first-line (documentation command)) "\n"))
1520
1521 ;;; Mode:
1522 ;;
1523 ;; The Customization major mode and interactive commands. 
1524
1525 (defvar custom-mode-map nil
1526   "Keymap for Custum Mode.")
1527 (if custom-mode-map
1528     nil
1529   (setq custom-mode-map (make-sparse-keymap))
1530   (define-key custom-mode-map [ mouse-2 ] 'custom-push-button)
1531   (define-key custom-mode-map "\t" 'custom-forward-field)
1532   (define-key custom-mode-map "\r" 'custom-enter-value)
1533   (define-key custom-mode-map "\C-k" 'custom-kill-line)
1534   (define-key custom-mode-map "\C-c\C-r" 'custom-field-reset)
1535   (define-key custom-mode-map "\C-c\M-\C-r" 'custom-reset-all)
1536   (define-key custom-mode-map "\C-c\C-z" 'custom-field-factory-reset)
1537   (define-key custom-mode-map "\C-c\M-\C-z" 'custom-factory-reset-all)
1538   (define-key custom-mode-map "\C-c\C-a" 'custom-field-apply)
1539   (define-key custom-mode-map "\C-c\M-\C-a" 'custom-apply-all)
1540   (define-key custom-mode-map "\C-c\C-d" 'custom-toggle-documentation))
1541
1542 ;; C-c keymap ideas: C-a field-beginning, C-e field-end, C-f
1543 ;; forward-field, C-b backward-field, C-n next-field, C-p
1544 ;; previous-field, ? describe-field.
1545
1546 (defun custom-mode ()
1547   "Major mode for doing customizations.
1548
1549 \\{custom-mode-map}"
1550   (kill-all-local-variables)
1551   (setq major-mode 'custom-mode
1552         mode-name "Custom")
1553   (use-local-map custom-mode-map)
1554   (make-local-variable 'before-change-functions)
1555   (setq before-change-functions '(custom-before-change))
1556   (make-local-variable 'after-change-functions)
1557   (setq after-change-functions '(custom-after-change))
1558   (if (not (fboundp 'make-local-hook))
1559       ;; Emacs 19.28 and earlier.
1560       (add-hook 'post-command-hook 'custom-post-command nil)      
1561     ;; Emacs 19.29.
1562     (make-local-hook 'post-command-hook)
1563     (add-hook 'post-command-hook 'custom-post-command nil t)))
1564
1565 (defun custom-forward-field (arg)
1566   "Move point to the next field or button.
1567 With optional ARG, move across that many fields."
1568   (interactive "p")
1569   (while (> arg 0)
1570     (setq arg (1- arg))
1571     (let ((next (if (get-text-property (point) 'custom-tag)
1572                     (next-single-property-change (point) 'custom-tag)
1573                   (point))))
1574       (setq next (or (next-single-property-change next 'custom-tag)
1575                      (next-single-property-change (point-min) 'custom-tag)))
1576       (if next
1577           (goto-char next)
1578         (error "No customization fields in this buffer.")))))
1579
1580 (defun custom-toggle-documentation (&optional arg)
1581   "Toggle display of documentation text.
1582 If the optional argument is non-nil, show text iff the argument is positive."
1583   (interactive "P")
1584   (let ((hide (or (and (null arg) 
1585                        (null (get custom-documentation-properties 'invisible)))
1586                   (<= (prefix-numeric-value arg) 0))))
1587     (put custom-documentation-properties 'invisible hide)
1588     (put custom-documentation-properties intangible hide))
1589   (redraw-display))
1590
1591 (defun custom-enter-value (field data)
1592   "Enter value for current customization field or push button."
1593   (interactive (list (get-text-property (point) 'custom-tag)
1594                      (get-text-property (point) 'custom-data)))
1595   (cond (data
1596          (funcall field data))
1597         ((eq field 'custom-enter-value)
1598          (error "Don't be silly"))
1599         ((and (symbolp field) (fboundp field))
1600          (call-interactively field))
1601         (field
1602          (custom-field-query field))
1603         (t
1604          (message "Nothing to enter here"))))
1605
1606 (defun custom-kill-line ()
1607   "Kill to end of field or end of line, whichever is first."
1608   (interactive)
1609   (let ((field (get-text-property (point) 'custom-field))
1610         (newline (save-excursion (search-forward "\n")))
1611         (next (next-single-property-change (point) 'custom-field)))
1612     (if (and field (> newline next))
1613         (kill-region (point) next)
1614       (call-interactively 'kill-line))))
1615
1616 (defun custom-push-button (event)
1617   "Activate button below mouse pointer."
1618   (interactive "e")
1619   (set-buffer (window-buffer (posn-window (event-start event))))
1620   (let* ((pos (posn-point (event-start event)))
1621          (field (get-text-property pos 'custom-field))
1622          (tag (get-text-property pos 'custom-tag))
1623          (data (get-text-property pos 'custom-data)))
1624     (cond (data
1625             (funcall tag data))
1626           ((and (symbolp tag) (fboundp tag))
1627            (call-interactively tag))
1628           (field
1629            (call-interactively (lookup-key global-map (this-command-keys))))
1630           (tag
1631            (custom-enter-value tag data))
1632           (t 
1633            (error "Nothing to click on here.")))))
1634
1635 (defun custom-reset-all ()
1636   "Undo any changes since the last apply in all fields."
1637   (interactive (and custom-modified-list
1638                     (not (y-or-n-p "Discard all changes? "))
1639                     (error "Reset aborted")))
1640   (let ((all custom-name-fields)
1641         current name field)
1642     (while all
1643       (setq current (car all)
1644             name (car current)
1645             field (cdr current)
1646             all (cdr all))
1647       (custom-field-reset field))))
1648
1649 (defun custom-field-reset (field)
1650   "Undo any changes in FIELD since the last apply."
1651   (interactive (list (or (get-text-property (point) 'custom-field)
1652                          (get-text-property (point) 'custom-tag))))
1653   (if (arrayp field)
1654       (let* ((custom (custom-field-custom field))
1655              (name (custom-name custom)))
1656         (save-excursion
1657           (if name
1658               (custom-field-original-set 
1659                field (custom-import custom (custom-external name))))
1660           (if (not (custom-valid custom (custom-field-original field)))
1661               (error "This field cannot be reset alone")
1662             (funcall (custom-property custom 'reset) field)
1663             (funcall (custom-property custom 'synchronize) field))))))
1664
1665 (defun custom-factory-reset-all ()
1666   "Reset all field to their default values."
1667   (interactive (and custom-modified-list
1668                     (not (y-or-n-p "Discard all changes? "))
1669                     (error "Reset aborted")))
1670   (let ((all custom-name-fields)
1671         name field custom default)
1672     (while all
1673       (setq field (cdr (car all))
1674             custom (custom-field-custom field)
1675             default (custom-default custom)
1676             all (cdr all))
1677       (custom-field-factory-reset field))))
1678
1679 (defun custom-field-factory-reset (field)
1680   "Reset FIELD to its default value."
1681   (interactive (list (or (get-text-property (point) 'custom-field)
1682                          (get-text-property (point) 'custom-tag))))
1683   (if (arrayp field)
1684       (let* ((custom (custom-field-custom field))
1685              (default (custom-default custom)))
1686         (save-excursion
1687           (funcall (custom-property custom 'factory-reset) field)))))
1688
1689 (defun custom-apply-all ()
1690   "Apply any changes since the last reset in all fields."
1691   (interactive (if custom-modified-list
1692                    nil
1693                  (error "No changes to apply.")))
1694   (let ((all custom-name-fields)
1695         name field)
1696     (while all
1697       (setq field (cdr (car all))
1698             all (cdr all))
1699       (let ((error (custom-field-validate (custom-field-custom field) field)))
1700         (if (null error)
1701             ()
1702           (goto-char (car error))
1703           (error (cdr error))))))
1704   (let ((all custom-name-fields)
1705         current name field)
1706     (while all
1707       (setq field (cdr (car all))
1708             all (cdr all))
1709       (custom-field-apply field))))
1710
1711 (defun custom-field-apply (field)
1712   "Apply any changes in FIELD since the last apply."
1713   (interactive (list (or (get-text-property (point) 'custom-field)
1714                          (get-text-property (point) 'custom-tag))))
1715   (if (arrayp field)
1716       (let* ((custom (custom-field-custom field))
1717              (error (custom-field-validate custom field)))
1718         (if error
1719             (error (cdr error)))
1720         (funcall (custom-property custom 'apply) field))))
1721
1722 (defun custom-toggle-hide (&rest ignore)
1723   "Hide or show entry."
1724   (interactive)
1725   (error "This button is not yet implemented"))
1726
1727 (defun custom-save ()
1728   "Save customization information."
1729   (interactive)
1730   (custom-apply-all)
1731   (let ((new custom-name-fields))
1732     (set-buffer (find-file-noselect custom-file))
1733     (goto-char (point-min))
1734     (save-excursion
1735       (let ((old (condition-case nil
1736                      (read (current-buffer))
1737                    (end-of-file (append '(setq custom-dummy
1738                                                'custom-dummy) ())))))
1739         (or (eq (car old) 'setq)
1740             (error "Invalid customization file: %s" custom-file))
1741         (while new
1742           (let* ((field (cdr (car new)))
1743                  (custom (custom-field-custom field))
1744                  (value (custom-field-original field))
1745                  (default (custom-default custom))
1746                  (name (car (car new))))
1747             (setq new (cdr new))
1748             (custom-assert '(eq name (custom-name custom)))
1749             (if (equal default value)
1750                 (setcdr old (custom-plist-delq name (cdr old)))
1751               (setcdr old (plist-put (cdr old) name 
1752                                      (custom-quote custom value))))))
1753         (erase-buffer)
1754         (insert ";; " custom-file "\
1755  --- Automatically generated customization information.
1756 ;; 
1757 ;; Feel free to edit by hand, but the entire content should consist of
1758 ;; a single setq.  Any other lisp expressions will confuse the
1759 ;; automatic configuration engine.
1760
1761 \(setq ")
1762         (setq old (cdr old))
1763         (while old
1764           (prin1 (car old) (current-buffer))
1765           (setq old (cdr old))
1766           (insert " ")
1767           (pp (car old) (current-buffer))
1768           (setq old (cdr old))
1769           (if old (insert "\n      ")))
1770         (insert ")\n")
1771         (save-buffer)
1772         (kill-buffer (current-buffer))))))
1773
1774 (defun custom-load ()
1775   "Save customization information."
1776   (interactive (and custom-modified-list
1777                     (not (equal (list (custom-name-field 'custom-file))
1778                                 custom-modified-list))
1779                     (not (y-or-n-p "Discard all changes? "))
1780                     (error "Load aborted")))
1781   (load-file (custom-name-value 'custom-file))
1782   (custom-reset-all))
1783
1784 ;;; Field Editing:
1785 ;;
1786 ;; Various internal functions for implementing the direct editing of
1787 ;; fields in the customization buffer.
1788
1789 (defvar custom-modified-list nil)
1790 ;; List of modified fields.
1791 (make-variable-buffer-local 'custom-modified-list)
1792
1793 (defun custom-field-untouch (field)
1794   ;; Remove FIELD and its children from `custom-modified-list'.
1795   (setq custom-modified-list (delq field custom-modified-list))
1796   (if (arrayp field)
1797       (let ((value (custom-field-value field)))
1798         (cond ((null (custom-data (custom-field-custom field))))
1799               ((arrayp value)
1800                (custom-field-untouch value))
1801               ((listp value)
1802                (mapcar 'custom-field-untouch value))))))
1803
1804
1805 (defun custom-field-insert (field)
1806   ;; Insert editing FIELD in current buffer.
1807   (let ((from (point))
1808         (custom (custom-field-custom field))
1809         (value (custom-field-value field)))
1810     (insert (custom-write custom value))
1811     (insert-char (custom-padding custom)
1812                  (- (custom-width custom) (- (point) from)))
1813     (custom-field-move field from (point))
1814     (set-text-properties 
1815      from (point)
1816      (list 'custom-field field
1817            'custom-tag field
1818            'face (custom-field-face field)
1819            'front-sticky t))))
1820
1821 (defun custom-field-read (field)
1822   ;; Read the screen content of FIELD.
1823   (custom-read (custom-field-custom field)
1824                (buffer-substring-no-properties (custom-field-start field)
1825                                                (custom-field-end field))))
1826
1827 (defun custom-field-leave (field)
1828   ;; Deactivate FIELD.
1829   (let ((before-change-functions nil)
1830         (after-change-functions nil))
1831     (put-text-property (custom-field-start field) (custom-field-end field)
1832                        'face (custom-field-face field))))
1833
1834 (defun custom-field-enter (field)
1835   ;; Activate FIELD.
1836   (let* ((start (custom-field-start field)) 
1837          (end (custom-field-end field))
1838          (custom (custom-field-custom field))
1839          (padding (custom-padding custom))
1840          (allow (custom-allow-padding custom))
1841          (before-change-functions nil)
1842          (after-change-functions nil))
1843     (or (and (eq this-command 'self-insert-command)
1844              allow)
1845         (let ((pos end))
1846           (while (and (< start pos)
1847                       (eq (char-after (1- pos)) padding))
1848             (setq pos (1- pos)))
1849           (if (< pos (point))
1850               (goto-char pos))))
1851     (put-text-property start end 'face custom-field-active-face)))
1852
1853 (defvar custom-field-last nil)
1854 ;; Last field containing point.
1855 (make-variable-buffer-local 'custom-field-last)
1856
1857 (defun custom-post-command ()
1858   ;; Keep track of their active field.
1859   (if (not (eq major-mode 'custom-mode))
1860       ;; BUG: Should have been local!
1861       ()
1862     (let ((field (custom-field-property (point))))
1863       (if (eq field custom-field-last)
1864           ()
1865         (if custom-field-last
1866             (custom-field-leave custom-field-last))
1867         (if field
1868             (custom-field-enter field))
1869         (setq custom-field-last field)))
1870     (set-buffer-modified-p custom-modified-list)))
1871
1872 (defvar custom-field-was nil)
1873 ;; The custom data before the change.
1874 (make-variable-buffer-local 'custom-field-was)
1875
1876 (defun custom-before-change (begin end)
1877   ;; Check that we the modification is allowed.
1878   (if (not (eq major-mode 'custom-mode))
1879       (message "Aargh! Why is custom-before-change called here?")
1880     (let ((from (custom-field-property begin))
1881           (to (custom-field-property end)))
1882       (cond ((or (null from) (null to))
1883              (error "You can only modify the fields"))
1884             ((not (eq from to))
1885              (error "Changes must be limited to a single field."))
1886             (t
1887              (setq custom-field-was from))))))
1888
1889 (defun custom-after-change (begin end length)
1890   ;; Keep track of field content.
1891   (if (not (eq major-mode 'custom-mode))
1892       (message "Aargh! Why is custom-after-change called here?")
1893     (let ((field custom-field-was))
1894       (custom-assert '(prog1 field (setq custom-field-was nil)))
1895       ;; Prevent mixing fields properties.
1896       (put-text-property begin end 'custom-field field)
1897       ;; Update the field after modification.
1898       (if (eq (custom-field-property begin) field)
1899           (let ((field-end (custom-field-end field)))
1900             (if (> end field-end)
1901                 (set-marker field-end end))
1902             (custom-field-value-set field (custom-field-read field))
1903             (custom-field-update field))
1904         ;; We deleted the entire field, reinsert it.
1905         (custom-assert '(eq begin end))
1906         (save-excursion
1907           (goto-char begin)
1908           (custom-field-value-set field
1909                                   (custom-read (custom-field-custom field) ""))
1910           (custom-field-insert field))))))
1911
1912 (defun custom-field-property (pos)
1913   ;; The `custom-field' text property valid for POS.
1914   (or (get-text-property pos 'custom-field)
1915       (and (not (eq pos (point-min)))
1916            (get-text-property (1- pos) 'custom-field))))
1917
1918 ;;; Generic Utilities:
1919 ;;
1920 ;; Some utility functions that are not really specific to custom.
1921
1922 (defun custom-assert (expr)
1923   "Assert that EXPR evaluates to non-nil at this point"
1924   (or (eval expr)
1925       (error "Assertion failed: %S" expr)))
1926
1927 (defun custom-first-line (string)
1928   "Return the part of STRING before the first newline."
1929   (let ((pos 0)
1930         (len (length string)))
1931     (while (and (< pos len) (not (eq (aref string pos) ?\n)))
1932       (setq pos (1+ pos)))
1933     (if (eq pos len)
1934         string
1935     (substring string 0 pos))))
1936
1937 (defun custom-insert-before (list old new)
1938   "In LIST insert before OLD a NEW element."
1939   (cond ((null list)
1940          (list new))
1941         ((null old)
1942          (nconc list (list new)))
1943         ((eq old (car list))
1944          (cons new list))
1945         (t
1946          (let ((list list))
1947            (while (not (eq old (car (cdr list))))
1948              (setq list (cdr list))
1949              (custom-assert '(cdr list)))
1950            (setcdr list (cons new (cdr list))))
1951          list)))
1952
1953 (defun custom-strip-padding (string padding)
1954   "Remove padding from STRING."
1955   (let ((regexp (concat (regexp-quote (char-to-string padding)) "+")))
1956     (while (string-match regexp string)
1957       (setq string (concat (substring string 0 (match-beginning 0))
1958                            (substring string (match-end 0))))))
1959   string)
1960
1961 (defun custom-plist-memq (prop plist)
1962   "Return non-nil if PROP is a property of PLIST.  Comparison done with EQ."
1963   (let (result)
1964     (while plist
1965       (if (eq (car plist) prop)
1966           (setq result plist
1967                 plist nil)
1968         (setq plist (cdr (cdr plist)))))
1969     result))
1970
1971 (defun custom-plist-delq (prop plist)
1972   "Delete property PROP from property list PLIST."
1973   (while (eq (car plist) prop)
1974     (setq plist (cdr (cdr plist))))
1975   (let ((list plist)
1976         (next (cdr (cdr plist))))
1977     (while next
1978       (if (eq (car next) prop)
1979           (progn 
1980             (setq next (cdr (cdr next)))
1981             (setcdr (cdr list) next))
1982         (setq list next
1983               next (cdr (cdr next))))))
1984   plist)
1985
1986 ;;; Meta Customization:
1987
1988 (custom-declare '()
1989   '((tag . "Meta Customization")
1990     (doc . "Customization of the customization support.")
1991     (type . group)
1992     (data ((type . face-doc))
1993           ((tag . "Button Face")
1994            (default . bold)
1995            (doc . "Face used for tags in customization buffers.")
1996            (name . custom-button-face)
1997            (synchronize . (lambda (f)
1998                             (put custom-button-properties 
1999                                  'face custom-button-face)))
2000            (type . face))
2001           ((tag . "Mouse Face")
2002            (default . highlight)
2003            (doc . "\
2004 Face used when mouse is above a button in customization buffers.")
2005            (name . custom-mouse-face)
2006            (synchronize . (lambda (f)
2007                             (put custom-button-properties 
2008                                  'mouse-face custom-mouse-face)))
2009            (type . face))
2010           ((tag . "Field Face")
2011            (default . italic)
2012            (doc . "Face used for customization fields.")
2013            (name . custom-field-face)
2014            (type . face))
2015           ((tag . "Uninitialized Face")
2016            (default . modeline)
2017            (doc . "Face used for uninitialized customization fields.")
2018            (name . custom-field-uninitialized-face)
2019            (type . face))
2020           ((tag . "Invalid Face")
2021            (default . highlight)
2022            (doc . "\
2023 Face used for customization fields containing invalid data.")
2024            (name . custom-field-invalid-face)
2025            (type . face))
2026           ((tag . "Modified Face")
2027            (default . bold-italic)
2028            (doc . "Face used for modified customization fields.")
2029            (name . custom-field-modified-face)
2030            (type . face))
2031           ((tag . "Active Face")
2032            (default . underline)
2033            (doc . "\
2034 Face used for customization fields while they are being edited.")
2035            (name . custom-field-active-face)
2036            (type . face)))))
2037
2038 (if (file-readable-p custom-file)
2039     (load-file custom-file))
2040
2041 (defvar custom-documentation-properties 'custom-documentation-properties
2042   "The properties of this symbol will be in effect for all documentation.")
2043 (put custom-documentation-properties 'rear-nonsticky t)
2044
2045 (defvar custom-button-properties 'custom-button-properties 
2046   "The properties of this symbol will be in effect for all buttons.")
2047 (put custom-button-properties 'face custom-button-face)
2048 (put custom-button-properties 'mouse-face custom-mouse-face)
2049 (put custom-button-properties 'rear-nonsticky t)
2050
2051 (provide 'custom)
2052
2053 ;;; custom.el ends here