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