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