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