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