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