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