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