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