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