(wait_reading_process_input): Use getpid when generating SIGIO.
[emacs.git] / lisp / custom.el
blobcb566cf8fe75594a34e5ab654ef888691143fd22
1 ;;; custom.el --- User friendly customization support.
3 ;; Copyright (C) 1995 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 (choice (type . default)
518 ;; See `custom-match'.
519 (query . custom-choice-query)
520 (accept . custom-choice-accept)
521 (extract . custom-choice-extract)
522 (validate . custom-choice-validate)
523 (insert . custom-choice-insert)
524 (none (tag . "Unknown")
525 (default . __uninitialized__)
526 (type . const)))
527 (const (type . default)
528 ;; A `const' only matches a single lisp value.
529 (extract . (lambda (c f) (list (custom-default c))))
530 (validate . (lambda (c f) nil))
531 (valid . custom-const-valid)
532 (update . custom-const-update)
533 (insert . custom-const-insert))
534 (face-doc (type . doc)
535 ;; A variable containing a face.
536 (doc . "\
537 You can customize the look of Emacs by deciding which faces should be
538 used when. If you push one of the face buttons below, you will be
539 given a choice between a number of standard faces. The name of the
540 selected face is shown right after the face button, and it is
541 displayed its own face so you can see how it looks. If you know of
542 another standard face not listed and want to use it, you can select
543 `Other' and write the name in the editing field.
545 If none of the standard faces suits you, you can select `Customize' to
546 create your own face. This will make six fields appear under the face
547 button. The `Fg' and `Bg' fields are the foreground and background
548 colors for the face, respectively. You should type the name of the
549 color in the field. You can use any X11 color name. A list of X11
550 color names may be available in the file `/usr/lib/X11/rgb.txt' on
551 your system. The special color name `default' means that the face
552 will not change the color of the text. The `Stipple' field is weird,
553 so just ignore it. The three remaining fields are toggles, which will
554 make the text `bold', `italic', or `underline' respectively. For some
555 fonts `bold' or `italic' will not make any visible change."))
556 (face (type . choice)
557 (eval . custom-face-eval)
558 (import . custom-face-import)
559 (data ((tag . "None")
560 (default . nil)
561 (type . const))
562 ((tag . "Default")
563 (default . default)
564 (face . custom-const-face)
565 (type . const))
566 ((tag . "Bold")
567 (default . bold)
568 (face . custom-const-face)
569 (type . const))
570 ((tag . "Bold-italic")
571 (default . bold-italic)
572 (face . custom-const-face)
573 (type . const))
574 ((tag . "Italic")
575 (default . italic)
576 (face . custom-const-face)
577 (type . const))
578 ((tag . "Underline")
579 (default . underline)
580 (face . custom-const-face)
581 (type . const))
582 ((tag . "Highlight")
583 (default . highlight)
584 (face . custom-const-face)
585 (type . const))
586 ((tag . "Modeline")
587 (default . modeline)
588 (face . custom-const-face)
589 (type . const))
590 ((tag . "Region")
591 (default . region)
592 (face . custom-const-face)
593 (type . const))
594 ((tag . "Secondary Selection")
595 (default . secondary-selection)
596 (face . custom-const-face)
597 (type . const))
598 ((tag . "Customized")
599 (compact . t)
600 (face-tag . custom-face-hack)
601 (eval . custom-face-eval)
602 (data ((hidden . t)
603 (tag . "")
604 (doc . "\
605 Select the properties you want this face to have.")
606 (default . custom-face-lookup)
607 (type . const))
608 "\n"
609 ((tag . "Fg")
610 (hidden . t)
611 (default . "default")
612 (width . 20)
613 (type . string))
614 ((tag . "Bg")
615 (default . "default")
616 (width . 20)
617 (type . string))
618 ((tag . "Stipple")
619 (default . "default")
620 (width . 20)
621 (type . string))
622 "\n"
623 ((tag . "Bold")
624 (default . nil)
625 (type . toggle))
627 ((tag . "Italic")
628 (default . nil)
629 (type . toggle))
631 ((tag . "Underline")
632 (hidden . t)
633 (default . nil)
634 (type . toggle)))
635 (default . (custom-face-lookup "default" "default" "default"
636 nil nil nil))
637 (type . list))
638 ((prompt . "Other")
639 (face . custom-field-value)
640 (default . __uninitialized__)
641 (type . symbol))))
642 (file (type . string)
643 ;; A string containing a file or directory name.
644 (directory . nil)
645 (default-file . nil)
646 (query . custom-file-query))
647 (sexp (type . default)
648 ;; Any lisp expression.
649 (width . 40)
650 (default . (__uninitialized__ . "Uninitialized"))
651 (read . custom-sexp-read)
652 (write . custom-sexp-write))
653 (symbol (type . sexp)
654 ;; A lisp symbol.
655 (width . 40)
656 (valid . (lambda (c d) (symbolp d))))
657 (integer (type . sexp)
658 ;; A lisp integer.
659 (width . 10)
660 (valid . (lambda (c d) (integerp d))))
661 (string (type . default)
662 ;; A lisp string.
663 (width . 40)
664 (valid . (lambda (c d) (stringp d)))
665 (read . custom-string-read)
666 (write . custom-string-write))
667 (button (type . default)
668 ;; Push me.
669 (accept . ignore)
670 (extract . nil)
671 (validate . ignore)
672 (insert . custom-button-insert))
673 (doc (type . default)
674 ;; A documentation only entry with no value.
675 (header . nil)
676 (reset . ignore)
677 (extract . nil)
678 (validate . ignore)
679 (insert . custom-documentation-insert))
680 (default (width . 20)
681 (valid . (lambda (c v) t))
682 (insert . custom-default-insert)
683 (update . custom-default-update)
684 (query . custom-default-query)
685 (tag . nil)
686 (prompt . nil)
687 (doc . nil)
688 (header . t)
689 (padding . ? )
690 (quote . custom-default-quote)
691 (eval . (lambda (c v) nil))
692 (export . custom-default-export)
693 (import . (lambda (c v) (list v)))
694 (synchronize . ignore)
695 (initialize . custom-default-initialize)
696 (extract . custom-default-extract)
697 (validate . custom-default-validate)
698 (apply . custom-default-apply)
699 (reset . custom-default-reset)
700 (factory-reset . custom-default-factory-reset)
701 (accept . custom-default-accept)
702 (match . custom-default-match)
703 (name . nil)
704 (compact . nil)
705 (hidden . nil)
706 (face . custom-default-face)
707 (data . nil)
708 (calculate . nil)
709 (default . __uninitialized__)))
710 "Alist of default properties for type symbols.
711 The format is `((SYMBOL (PROPERTY . VALUE)... )... )'.")
713 (defconst custom-local-type-properties nil
714 "Local type properties.
715 Entries in this list take precedence over `custom-type-properties'.")
717 (make-variable-buffer-local 'custom-local-type-properties)
719 (defconst custom-nil '__uninitialized__
720 "Special value representing an uninitialized field.")
722 (defconst custom-invalid '__invalid__
723 "Special value representing an invalid field.")
725 (defun custom-property (custom property)
726 "Extract from CUSTOM property PROPERTY."
727 (let ((entry (assq property custom)))
728 (while (null entry)
729 ;; Look in superclass.
730 (let ((type (custom-type custom)))
731 (setq custom (cdr (or (assq type custom-local-type-properties)
732 (assq type custom-type-properties)))
733 entry (assq property custom))
734 (custom-assert 'custom)))
735 (cdr entry)))
737 (defun custom-super (custom property)
738 "Extract from CUSTOM property PROPERTY. Start with CUSTOM's superclass."
739 (let ((entry nil))
740 (while (null entry)
741 ;; Look in superclass.
742 (let ((type (custom-type custom)))
743 (setq custom (cdr (or (assq type custom-local-type-properties)
744 (assq type custom-type-properties)))
745 entry (assq property custom))
746 (custom-assert 'custom)))
747 (cdr entry)))
749 (defun custom-property-set (custom property value)
750 "Set CUSTOM PROPERTY to VALUE by side effect.
751 CUSTOM must have at least one property already."
752 (let ((entry (assq property custom)))
753 (if entry
754 (setcdr entry value)
755 (setcdr custom (cons (cons property value) (cdr custom))))))
757 (defun custom-type (custom)
758 "Extract `type' from CUSTOM."
759 (cdr (assq 'type custom)))
761 (defun custom-name (custom)
762 "Extract `name' from CUSTOM."
763 (custom-property custom 'name))
765 (defun custom-tag (custom)
766 "Extract `tag' from CUSTOM."
767 (custom-property custom 'tag))
769 (defun custom-face-tag (custom)
770 "Extract `face-tag' from CUSTOM."
771 (custom-property custom 'face-tag))
773 (defun custom-prompt (custom)
774 "Extract `prompt' from CUSTOM.
775 If none exist, default to `tag' or, failing that, `type'."
776 (or (custom-property custom 'prompt)
777 (custom-property custom 'tag)
778 (capitalize (symbol-name (custom-type custom)))))
780 (defun custom-default (custom)
781 "Extract `default' from CUSTOM."
782 (let ((value (custom-property custom 'calculate)))
783 (if value
784 (eval value)
785 (custom-property custom 'default))))
787 (defun custom-data (custom)
788 "Extract the `data' from CUSTOM."
789 (custom-property custom 'data))
791 (defun custom-documentation (custom)
792 "Extract `doc' from CUSTOM."
793 (custom-property custom 'doc))
795 (defun custom-width (custom)
796 "Extract `width' from CUSTOM."
797 (custom-property custom 'width))
799 (defun custom-compact (custom)
800 "Extract `compact' from CUSTOM."
801 (custom-property custom 'compact))
803 (defun custom-padding (custom)
804 "Extract `padding' from CUSTOM."
805 (custom-property custom 'padding))
807 (defun custom-valid (custom value)
808 "Non-nil if CUSTOM may validly be set to VALUE."
809 (and (not (and (listp value) (eq custom-invalid (car value))))
810 (funcall (custom-property custom 'valid) custom value)))
812 (defun custom-import (custom value)
813 "Import CUSTOM VALUE from external variable.
815 This function change VALUE into a form that makes it easier to edit
816 internally. What the internal form is exactly depends on CUSTOM.
817 The internal form is returned."
818 (if (eq custom-nil value)
819 (list custom-nil)
820 (funcall (custom-property custom 'import) custom value)))
822 (defun custom-eval (custom value)
823 "Return non-nil if CUSTOM's VALUE needs to be evaluated."
824 (funcall (custom-property custom 'eval) custom value))
826 (defun custom-quote (custom value)
827 "Quote CUSTOM's VALUE if necessary."
828 (funcall (custom-property custom 'quote) custom value))
830 (defun custom-write (custom value)
831 "Convert CUSTOM VALUE to a string."
832 (cond ((eq value custom-nil)
834 ((and (listp value) (eq (car value) custom-invalid))
835 (cdr value))
837 (funcall (custom-property custom 'write) custom value))))
839 (defun custom-read (custom string)
840 "Convert CUSTOM field content STRING into lisp."
841 (condition-case nil
842 (funcall (custom-property custom 'read) custom string)
843 (error (cons custom-invalid string))))
845 (defun custom-match (custom values)
846 "Match CUSTOM with a list of VALUES.
848 Return a cons-cell where the car is the sublist of VALUES matching CUSTOM,
849 and the cdr is the remaining VALUES.
851 A CUSTOM is actually a regular expression over the alphabet of lisp
852 types. Most CUSTOM types are just doing a literal match, e.g. the
853 `symbol' type matches any lisp symbol. The exceptions are:
855 group: which corresponds to a `(' and `)' group in a regular expression.
856 choice: which corresponds to a group of `|' in a regular expression.
857 repeat: which corresponds to a `*' in a regular expression.
858 optional: which corresponds to a `?', and isn't implemented yet."
859 (if (memq values (list custom-nil nil))
860 ;; Nothing matches the uninitialized or empty list.
861 (cons custom-nil nil)
862 (funcall (custom-property custom 'match) custom values)))
864 (defun custom-initialize (custom)
865 "Initialize `doc' and `default' attributes of CUSTOM."
866 (funcall (custom-property custom 'initialize) custom))
868 (defun custom-find (custom tag)
869 "Find child in CUSTOM with `tag' TAG."
870 (funcall (custom-property custom 'find) custom tag))
872 (defun custom-travel-path (custom path)
873 "Find decedent of CUSTOM by looking through PATH."
874 (if (null path)
875 custom
876 (custom-travel-path (custom-find custom (car path)) (cdr path))))
878 (defun custom-field-extract (custom field)
879 "Extract CUSTOM's value in FIELD."
880 (if (stringp custom)
882 (funcall (custom-property (custom-field-custom field) 'extract)
883 custom field)))
885 (defun custom-field-validate (custom field)
886 "Validate CUSTOM's value in FIELD.
887 Return nil if valid, otherwise return a cons-cell where the car is the
888 position of the error, and the cdr is a text describing the error."
889 (if (stringp custom)
891 (funcall (custom-property custom 'validate) custom field)))
893 ;;; Field Functions:
895 ;; This section defines the public functions for manipulating the
896 ;; FIELD datatype. The FIELD instance hold information about a
897 ;; specific editing field in the customization buffer.
899 ;; Each FIELD can be seen as an instantiation of a CUSTOM.
901 (defvar custom-field-last nil)
902 ;; Last field containing point.
903 (make-variable-buffer-local 'custom-field-last)
905 (defvar custom-modified-list nil)
906 ;; List of modified fields.
907 (make-variable-buffer-local 'custom-modified-list)
909 (defun custom-field-create (custom value)
910 "Create a field structure of type CUSTOM containing VALUE.
912 A field structure is an array [ CUSTOM VALUE ORIGINAL START END ], where
913 CUSTOM defines the type of the field,
914 VALUE is the current value of the field,
915 ORIGINAL is the original value when created, and
916 START and END are markers to the start and end of the field."
917 (vector custom value custom-nil nil nil))
919 (defun custom-field-custom (field)
920 "Return the `custom' attribute of FIELD."
921 (aref field 0))
923 (defun custom-field-value (field)
924 "Return the `value' attribute of FIELD."
925 (aref field 1))
927 (defun custom-field-original (field)
928 "Return the `original' attribute of FIELD."
929 (aref field 2))
931 (defun custom-field-start (field)
932 "Return the `start' attribute of FIELD."
933 (aref field 3))
935 (defun custom-field-end (field)
936 "Return the `end' attribute of FIELD."
937 (aref field 4))
939 (defun custom-field-value-set (field value)
940 "Set the `value' attribute of FIELD to VALUE."
941 (aset field 1 value))
943 (defun custom-field-original-set (field original)
944 "Set the `original' attribute of FIELD to ORIGINAL."
945 (aset field 2 original))
947 (defun custom-field-move (field start end)
948 "Set the `start'and `end' attributes of FIELD to START and END."
949 (set-marker (or (aref field 3) (aset field 3 (make-marker))) start)
950 (set-marker (or (aref field 4) (aset field 4 (make-marker))) end))
952 (defun custom-field-query (field)
953 "Query user for content of current field."
954 (funcall (custom-property (custom-field-custom field) 'query) field))
956 (defun custom-field-accept (field value &optional original)
957 "Store a new value into field FIELD, taking it from VALUE.
958 If optional ORIGINAL is non-nil, consider VALUE for the original value."
959 (let ((inhibit-point-motion-hooks t))
960 (funcall (custom-property (custom-field-custom field) 'accept)
961 field value original)))
963 (defun custom-field-face (field)
964 "The face used for highlighting FIELD."
965 (let ((custom (custom-field-custom field)))
966 (if (stringp custom)
968 (let ((face (funcall (custom-property custom 'face) field)))
969 (if (custom-facep face) face nil)))))
971 (defun custom-field-update (field)
972 "Update the screen appearance of FIELD to correspond with the field's value."
973 (let ((custom (custom-field-custom field)))
974 (if (stringp custom)
976 (funcall (custom-property custom 'update) field))))
978 ;;; Types:
980 ;; The following functions defines type specific actions.
982 (defun custom-repeat-eval (custom value)
983 "Non-nil if CUSTOM's VALUE needs to be evaluated."
984 (if (eq value custom-nil)
986 (let ((child (custom-data custom))
987 (found nil))
988 (mapcar (lambda (v) (if (custom-eval child v) (setq found t)))
989 value))))
991 (defun custom-repeat-quote (custom value)
992 "A list of CUSTOM's VALUEs quoted."
993 (let ((child (custom-data custom)))
994 (apply 'append (mapcar (lambda (v) (custom-quote child v))
995 value))))
998 (defun custom-repeat-import (custom value)
999 "Modify CUSTOM's VALUE to match internal expectations."
1000 (let ((child (custom-data custom)))
1001 (apply 'append (mapcar (lambda (v) (custom-import child v))
1002 value))))
1004 (defun custom-repeat-accept (field value &optional original)
1005 "Store a new value into field FIELD, taking it from VALUE."
1006 (let ((values (copy-sequence (custom-field-value field)))
1007 (all (custom-field-value field))
1008 (start (custom-field-start field))
1009 current new)
1010 (if original
1011 (custom-field-original-set field value))
1012 (while (consp value)
1013 (setq new (car value)
1014 value (cdr value))
1015 (if values
1016 ;; Change existing field.
1017 (setq current (car values)
1018 values (cdr values))
1019 ;; Insert new field if series has grown.
1020 (goto-char start)
1021 (setq current (custom-repeat-insert-entry field))
1022 (setq all (custom-insert-before all nil current))
1023 (custom-field-value-set field all))
1024 (custom-field-accept current new original))
1025 (while (consp values)
1026 ;; Delete old field if series has scrunk.
1027 (setq current (car values)
1028 values (cdr values))
1029 (let ((pos (custom-field-start current))
1030 data)
1031 (while (not data)
1032 (setq pos (previous-single-property-change pos 'custom-data))
1033 (custom-assert 'pos)
1034 (setq data (get-text-property pos 'custom-data))
1035 (or (and (arrayp data)
1036 (> (length data) 1)
1037 (eq current (aref data 1)))
1038 (setq data nil)))
1039 (custom-repeat-delete data)))))
1041 (defun custom-repeat-insert (custom level)
1042 "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1043 (let* ((field (custom-field-create custom nil))
1044 (add-tag (custom-property custom 'add-tag))
1045 (start (make-marker))
1046 (data (vector field nil start nil)))
1047 (custom-text-insert "\n")
1048 (let ((pos (point)))
1049 (custom-text-insert (custom-property custom 'prefix))
1050 (custom-tag-insert add-tag 'custom-repeat-add data)
1051 (set-marker start pos))
1052 (custom-field-move field start (point))
1053 (custom-documentation-insert custom)
1054 field))
1056 (defun custom-repeat-insert-entry (repeat)
1057 "Insert entry at point in the REPEAT field."
1058 (let* ((inhibit-point-motion-hooks t)
1059 (inhibit-read-only t)
1060 (before-change-functions nil)
1061 (after-change-functions nil)
1062 (custom (custom-field-custom repeat))
1063 (add-tag (custom-property custom 'add-tag))
1064 (del-tag (custom-property custom 'del-tag))
1065 (start (make-marker))
1066 (end (make-marker))
1067 (data (vector repeat nil start end))
1068 field)
1069 (insert-before-markers "\n")
1070 (backward-char 1)
1071 (set-marker start (point))
1072 (custom-text-insert " ")
1073 (aset data 1 (setq field (custom-insert (custom-data custom) nil)))
1074 (custom-text-insert " ")
1075 (set-marker end (point))
1076 (goto-char start)
1077 (custom-text-insert (custom-property custom 'prefix))
1078 (custom-tag-insert add-tag 'custom-repeat-add data)
1079 (custom-text-insert " ")
1080 (custom-tag-insert del-tag 'custom-repeat-delete data)
1081 (forward-char 1)
1082 field))
1084 (defun custom-repeat-add (data)
1085 "Add list entry."
1086 (let ((parent (aref data 0))
1087 (field (aref data 1))
1088 (at (aref data 2))
1089 new)
1090 (goto-char at)
1091 (setq new (custom-repeat-insert-entry parent))
1092 (custom-field-value-set parent
1093 (custom-insert-before (custom-field-value parent)
1094 field new))))
1096 (defun custom-repeat-delete (data)
1097 "Delete list entry."
1098 (let ((inhibit-point-motion-hooks t)
1099 (inhibit-read-only t)
1100 (before-change-functions nil)
1101 (after-change-functions nil)
1102 (parent (aref data 0))
1103 (field (aref data 1)))
1104 (delete-region (aref data 2) (1+ (aref data 3)))
1105 (custom-field-untouch (aref data 1))
1106 (custom-field-value-set parent
1107 (delq field (custom-field-value parent)))))
1109 (defun custom-repeat-match (custom values)
1110 "Match CUSTOM with VALUES."
1111 (let* ((child (custom-data custom))
1112 (match (custom-match child values))
1113 matches)
1114 (while (not (eq (car match) custom-nil))
1115 (setq matches (cons (car match) matches)
1116 values (cdr match)
1117 match (custom-match child values)))
1118 (cons (nreverse matches) values)))
1120 (defun custom-repeat-extract (custom field)
1121 "Extract list of children's values."
1122 (let ((values (custom-field-value field))
1123 (data (custom-data custom))
1124 result)
1125 (if (eq values custom-nil)
1127 (while values
1128 (setq result (append result (custom-field-extract data (car values)))
1129 values (cdr values))))
1130 result))
1132 (defun custom-repeat-validate (custom field)
1133 "Validate children."
1134 (let ((values (custom-field-value field))
1135 (data (custom-data custom))
1136 result)
1137 (if (eq values custom-nil)
1138 (setq result (cons (custom-field-start field) "Uninitialized list")))
1139 (while (and values (not result))
1140 (setq result (custom-field-validate data (car values))
1141 values (cdr values)))
1142 result))
1144 (defun custom-pair-accept (field value &optional original)
1145 "Store a new value into field FIELD, taking it from VALUE."
1146 (custom-group-accept field (list (car value) (cdr value)) original))
1148 (defun custom-pair-eval (custom value)
1149 "Non-nil if CUSTOM's VALUE needs to be evaluated."
1150 (custom-group-eval custom (list (car value) (cdr value))))
1152 (defun custom-pair-import (custom value)
1153 "Modify CUSTOM's VALUE to match internal expectations."
1154 (let ((result (car (custom-group-import custom
1155 (list (car value) (cdr value))))))
1156 (custom-assert '(eq (length result) 2))
1157 (list (cons (nth 0 result) (nth 1 result)))))
1159 (defun custom-pair-quote (custom value)
1160 "Quote CUSTOM's VALUE if necessary."
1161 (if (custom-eval custom value)
1162 (let ((v (car (custom-group-quote custom
1163 (list (car value) (cdr value))))))
1164 (list (list 'cons (nth 0 v) (nth 1 v))))
1165 (custom-default-quote custom value)))
1167 (defun custom-pair-extract (custom field)
1168 "Extract cons of children's values."
1169 (let ((values (custom-field-value field))
1170 (data (custom-data custom))
1171 result)
1172 (custom-assert '(eq (length values) (length data)))
1173 (while values
1174 (setq result (append result
1175 (custom-field-extract (car data) (car values)))
1176 data (cdr data)
1177 values (cdr values)))
1178 (custom-assert '(null data))
1179 (list (cons (nth 0 result) (nth 1 result)))))
1181 (defun custom-list-quote (custom value)
1182 "Quote CUSTOM's VALUE if necessary."
1183 (if (custom-eval custom value)
1184 (let ((v (car (custom-group-quote custom value))))
1185 (list (cons 'list v)))
1186 (custom-default-quote custom value)))
1188 (defun custom-list-extract (custom field)
1189 "Extract list of children's values."
1190 (let ((values (custom-field-value field))
1191 (data (custom-data custom))
1192 result)
1193 (custom-assert '(eq (length values) (length data)))
1194 (while values
1195 (setq result (append result
1196 (custom-field-extract (car data) (car values)))
1197 data (cdr data)
1198 values (cdr values)))
1199 (custom-assert '(null data))
1200 (list result)))
1202 (defun custom-group-validate (custom field)
1203 "Validate children."
1204 (let ((values (custom-field-value field))
1205 (data (custom-data custom))
1206 result)
1207 (if (eq values custom-nil)
1208 (setq result (cons (custom-field-start field) "Uninitialized list"))
1209 (custom-assert '(eq (length values) (length data))))
1210 (while (and values (not result))
1211 (setq result (custom-field-validate (car data) (car values))
1212 data (cdr data)
1213 values (cdr values)))
1214 result))
1216 (defun custom-group-eval (custom value)
1217 "Non-nil if CUSTOM's VALUE needs to be evaluated."
1218 (let ((found nil))
1219 (mapcar (lambda (c)
1220 (or (stringp c)
1221 (let ((match (custom-match c value)))
1222 (if (custom-eval c (car match))
1223 (setq found t))
1224 (setq value (cdr match)))))
1225 (custom-data custom))
1226 found))
1228 (defun custom-group-quote (custom value)
1229 "A list of CUSTOM's VALUE members, quoted."
1230 (list (apply 'append
1231 (mapcar (lambda (c)
1232 (if (stringp c)
1234 (let ((match (custom-match c value)))
1235 (prog1 (custom-quote c (car match))
1236 (setq value (cdr match))))))
1237 (custom-data custom)))))
1239 (defun custom-group-import (custom value)
1240 "Modify CUSTOM's VALUE to match internal expectations."
1241 (list (apply 'append
1242 (mapcar (lambda (c)
1243 (if (stringp c)
1245 (let ((match (custom-match c value)))
1246 (prog1 (custom-import c (car match))
1247 (setq value (cdr match))))))
1248 (custom-data custom)))))
1250 (defun custom-group-initialize (custom)
1251 "Initialize `doc' and `default' entries in CUSTOM."
1252 (if (custom-name custom)
1253 (custom-default-initialize custom)
1254 (mapcar 'custom-initialize (custom-data custom))))
1256 (defun custom-group-apply (field)
1257 "Reset `value' in FIELD to `original'."
1258 (let ((custom (custom-field-custom field))
1259 (values (custom-field-value field)))
1260 (if (custom-name custom)
1261 (custom-default-apply field)
1262 (mapcar 'custom-field-apply values))))
1264 (defun custom-group-reset (field)
1265 "Reset `value' in FIELD to `original'."
1266 (let ((custom (custom-field-custom field))
1267 (values (custom-field-value field)))
1268 (if (custom-name custom)
1269 (custom-default-reset field)
1270 (mapcar 'custom-field-reset values))))
1272 (defun custom-group-factory-reset (field)
1273 "Reset `value' in FIELD to `default'."
1274 (let ((custom (custom-field-custom field))
1275 (values (custom-field-value field)))
1276 (if (custom-name custom)
1277 (custom-default-factory-reset field)
1278 (mapcar 'custom-field-factory-reset values))))
1280 (defun custom-group-find (custom tag)
1281 "Find child in CUSTOM with `tag' TAG."
1282 (let ((data (custom-data custom))
1283 (result nil))
1284 (while (not result)
1285 (custom-assert 'data)
1286 (if (equal (custom-tag (car data)) tag)
1287 (setq result (car data))
1288 (setq data (cdr data))))))
1290 (defun custom-group-accept (field value &optional original)
1291 "Store a new value into field FIELD, taking it from VALUE."
1292 (let* ((values (custom-field-value field))
1293 (custom (custom-field-custom field))
1294 (from (custom-field-start field))
1295 (face-tag (custom-face-tag custom))
1296 current)
1297 (if face-tag
1298 (put-text-property from (+ from (length (custom-tag custom)))
1299 'face (funcall face-tag field value)))
1300 (if original
1301 (custom-field-original-set field value))
1302 (while values
1303 (setq current (car values)
1304 values (cdr values))
1305 (if current
1306 (let* ((custom (custom-field-custom current))
1307 (match (custom-match custom value)))
1308 (setq value (cdr match))
1309 (custom-field-accept current (car match) original))))))
1311 (defun custom-group-insert (custom level)
1312 "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1313 (let* ((field (custom-field-create custom nil))
1314 fields hidden
1315 (from (point))
1316 (compact (custom-compact custom))
1317 (tag (custom-tag custom))
1318 (face-tag (custom-face-tag custom)))
1319 (cond (face-tag (custom-text-insert tag))
1320 (tag (custom-tag-insert tag field)))
1321 (or compact (custom-documentation-insert custom))
1322 (or compact (custom-text-insert "\n"))
1323 (let ((data (custom-data custom)))
1324 (while data
1325 (setq fields (cons (custom-insert (car data) (if level (1+ level)))
1326 fields))
1327 (setq hidden (or (stringp (car data))
1328 (custom-property (car data) 'hidden)))
1329 (setq data (cdr data))
1330 (if data (custom-text-insert (cond (hidden "")
1331 (compact " ")
1332 (t "\n"))))))
1333 (if compact (custom-documentation-insert custom))
1334 (custom-field-value-set field (nreverse fields))
1335 (custom-field-move field from (point))
1336 field))
1338 (defun custom-choice-insert (custom level)
1339 "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1340 (let* ((field (custom-field-create custom nil))
1341 (from (point)))
1342 (custom-text-insert "lars er en nisse")
1343 (custom-field-move field from (point))
1344 (custom-documentation-insert custom)
1345 (custom-field-reset field)
1346 field))
1348 (defun custom-choice-accept (field value &optional original)
1349 "Store a new value into field FIELD, taking it from VALUE."
1350 (let ((custom (custom-field-custom field))
1351 (start (custom-field-start field))
1352 (end (custom-field-end field))
1353 (inhibit-read-only t)
1354 (before-change-functions nil)
1355 (after-change-functions nil)
1356 from)
1357 (cond (original
1358 (setq custom-modified-list (delq field custom-modified-list))
1359 (custom-field-original-set field value))
1360 ((equal value (custom-field-original field))
1361 (setq custom-modified-list (delq field custom-modified-list)))
1363 (add-to-list 'custom-modified-list field)))
1364 (custom-field-untouch (custom-field-value field))
1365 (delete-region start end)
1366 (goto-char start)
1367 (setq from (point))
1368 (insert-before-markers " ")
1369 (backward-char 1)
1370 (custom-category-set (point) (1+ (point)) 'custom-hidden-properties)
1371 (custom-tag-insert (custom-tag custom) field)
1372 (custom-text-insert ": ")
1373 (let ((data (custom-data custom))
1374 found begin)
1375 (while (and data (not found))
1376 (if (not (custom-valid (car data) value))
1377 (setq data (cdr data))
1378 (setq found (custom-insert (car data) nil))
1379 (setq data nil)))
1380 (if found
1382 (setq begin (point)
1383 found (custom-insert (custom-property custom 'none) nil))
1384 (add-text-properties begin (point)
1385 (list rear-nonsticky t
1386 'face custom-field-uninitialized-face)))
1387 (or original
1388 (custom-field-original-set found (custom-field-original field)))
1389 (custom-field-accept found value original)
1390 (custom-field-value-set field found)
1391 (custom-field-move field from end))))
1393 (defun custom-choice-extract (custom field)
1394 "Extract child's value."
1395 (let ((value (custom-field-value field)))
1396 (custom-field-extract (custom-field-custom value) value)))
1398 (defun custom-choice-validate (custom field)
1399 "Validate child's value."
1400 (let ((value (custom-field-value field))
1401 (custom (custom-field-custom field)))
1402 (if (or (eq value custom-nil)
1403 (eq (custom-field-custom value) (custom-property custom 'none)))
1404 (cons (custom-field-start field) "Make a choice")
1405 (custom-field-validate (custom-field-custom value) value))))
1407 (defun custom-choice-query (field)
1408 "Choose a child."
1409 (let* ((custom (custom-field-custom field))
1410 (old (custom-field-custom (custom-field-value field)))
1411 (default (custom-prompt old))
1412 (tag (custom-prompt custom))
1413 (data (custom-data custom))
1414 current alist)
1415 (if (eq (length data) 2)
1416 (custom-field-accept field (custom-default (if (eq (nth 0 data) old)
1417 (nth 1 data)
1418 (nth 0 data))))
1419 (while data
1420 (setq current (car data)
1421 data (cdr data))
1422 (setq alist (cons (cons (custom-prompt current) current) alist)))
1423 (let ((answer (cond ((and (fboundp 'button-press-event-p)
1424 (fboundp 'popup-menu)
1425 (button-press-event-p last-input-event))
1426 (cdr (assoc (car (custom-x-really-popup-menu
1427 last-input-event tag
1428 (reverse alist)))
1429 alist)))
1430 ((listp last-input-event)
1431 (x-popup-menu last-input-event
1432 (list tag (cons "" (reverse alist)))))
1434 (let ((choice (completing-read (concat tag
1435 " (default "
1436 default
1437 "): ")
1438 alist nil t)))
1439 (if (or (null choice) (string-equal choice ""))
1440 (setq choice default))
1441 (cdr (assoc choice alist)))))))
1442 (if answer
1443 (custom-field-accept field (custom-default answer)))))))
1445 (defun custom-file-query (field)
1446 "Prompt for a file name"
1447 (let* ((value (custom-field-value field))
1448 (custom (custom-field-custom field))
1449 (valid (custom-valid custom value))
1450 (directory (custom-property custom 'directory))
1451 (default (and (not valid)
1452 (custom-property custom 'default-file)))
1453 (tag (custom-tag custom))
1454 (prompt (if default
1455 (concat tag " (" default "): ")
1456 (concat tag ": "))))
1457 (custom-field-accept field
1458 (if (custom-valid custom value)
1459 (read-file-name prompt
1460 (if (file-name-absolute-p value)
1462 directory)
1463 default nil value)
1464 (read-file-name prompt directory default)))))
1466 (defun custom-face-eval (custom value)
1467 "Return non-nil if CUSTOM's VALUE needs to be evaluated."
1468 (not (symbolp value)))
1470 (defun custom-face-import (custom value)
1471 "Modify CUSTOM's VALUE to match internal expectations."
1472 (let ((name (symbol-name value)))
1473 (list (if (string-match "\
1474 custom-face-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)"
1475 name)
1476 (list 'custom-face-lookup
1477 (match-string 1 name)
1478 (match-string 2 name)
1479 (match-string 3 name)
1480 (intern (match-string 4 name))
1481 (intern (match-string 5 name))
1482 (intern (match-string 6 name)))
1483 value))))
1485 (defun custom-face-lookup (fg bg stipple bold italic underline)
1486 "Lookup or create a face with specified attributes.
1487 FG BG STIPPLE BOLD ITALIC UNDERLINE"
1488 (let ((name (intern (format "custom-face-%s-%s-%s-%S-%S-%S"
1489 (or fg "default")
1490 (or bg "default")
1491 (or stipple "default")
1492 bold italic underline))))
1493 (if (and (custom-facep name)
1494 (fboundp 'make-face))
1496 (make-face name)
1497 (modify-face name
1498 (if (string-equal fg "default") nil fg)
1499 (if (string-equal bg "default") nil bg)
1500 (if (string-equal stipple "default") nil stipple)
1501 bold italic underline))
1502 name))
1504 (defun custom-face-hack (field value)
1505 "Face that should be used for highlighting FIELD containing VALUE."
1506 (let* ((custom (custom-field-custom field))
1507 (face (eval (funcall (custom-property custom 'export)
1508 custom value))))
1509 (if (custom-facep face) face nil)))
1511 (defun custom-const-insert (custom level)
1512 "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1513 (let* ((field (custom-field-create custom custom-nil))
1514 (face (custom-field-face field))
1515 (from (point)))
1516 (custom-text-insert (custom-tag custom))
1517 (add-text-properties from (point)
1518 (list 'face face
1519 rear-nonsticky t))
1520 (custom-documentation-insert custom)
1521 (custom-field-move field from (point))
1522 field))
1524 (defun custom-const-update (field)
1525 "Update face of FIELD."
1526 (let ((from (custom-field-start field))
1527 (custom (custom-field-custom field)))
1528 (put-text-property from (+ from (length (custom-tag custom)))
1529 'face (custom-field-face field))))
1531 (defun custom-const-valid (custom value)
1532 "Non-nil if CUSTOM can validly have the value VALUE."
1533 (equal (custom-default custom) value))
1535 (defun custom-const-face (field)
1536 "Face used for a FIELD."
1537 (custom-default (custom-field-custom field)))
1539 (defun custom-sexp-read (custom string)
1540 "Read from CUSTOM an STRING."
1541 (save-match-data
1542 (save-excursion
1543 (set-buffer (get-buffer-create " *Custom Scratch*"))
1544 (erase-buffer)
1545 (insert string)
1546 (goto-char (point-min))
1547 (prog1 (read (current-buffer))
1548 (or (looking-at
1549 (concat (regexp-quote (char-to-string
1550 (custom-padding custom)))
1551 "*\\'"))
1552 (error "Junk at end of expression"))))))
1554 (autoload 'pp-to-string "pp")
1556 (defun custom-sexp-write (custom sexp)
1557 "Write CUSTOM SEXP as string."
1558 (let ((string (prin1-to-string sexp)))
1559 (if (<= (length string) (custom-width custom))
1560 string
1561 (setq string (pp-to-string sexp))
1562 (string-match "[ \t\n]*\\'" string)
1563 (concat "\n" (substring string 0 (match-beginning 0))))))
1565 (defun custom-string-read (custom string)
1566 "Read string by ignoring trailing padding characters."
1567 (let ((last (length string))
1568 (padding (custom-padding custom)))
1569 (while (and (> last 0)
1570 (eq (aref string (1- last)) padding))
1571 (setq last (1- last)))
1572 (substring string 0 last)))
1574 (defun custom-string-write (custom string)
1575 "Write raw string."
1576 string)
1578 (defun custom-button-insert (custom level)
1579 "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1580 (custom-tag-insert (concat "[" (custom-tag custom) "]")
1581 (custom-property custom 'query))
1582 (custom-documentation-insert custom)
1583 nil)
1585 (defun custom-default-export (custom value)
1586 ;; Convert CUSTOM's VALUE to external representation.
1587 ;; See `custom-import'.
1588 (if (custom-eval custom value)
1589 (eval (car (custom-quote custom value)))
1590 value))
1592 (defun custom-default-quote (custom value)
1593 "Quote CUSTOM's VALUE if necessary."
1594 (list (if (and (not (custom-eval custom value))
1595 (or (and (symbolp value)
1596 value
1597 (not (eq t value)))
1598 (and (listp value)
1599 value
1600 (not (memq (car value) '(quote function lambda))))))
1601 (list 'quote value)
1602 value)))
1604 (defun custom-default-initialize (custom)
1605 "Initialize `doc' and `default' entries in CUSTOM."
1606 (let ((name (custom-name custom)))
1607 (if (null name)
1609 (let ((default (custom-default custom))
1610 (doc (custom-documentation custom))
1611 (vdoc (documentation-property name 'variable-documentation t)))
1612 (if doc
1613 (or vdoc (put name 'variable-documentation doc))
1614 (if vdoc (custom-property-set custom 'doc vdoc)))
1615 (if (eq default custom-nil)
1616 (if (boundp name)
1617 (custom-property-set custom 'default (symbol-value name)))
1618 (or (boundp name)
1619 (set name default)))))))
1621 (defun custom-default-insert (custom level)
1622 "Insert field for CUSTOM at nesting LEVEL in customization buffer."
1623 (let ((field (custom-field-create custom custom-nil))
1624 (tag (custom-tag custom)))
1625 (if (null tag)
1627 (custom-tag-insert tag field)
1628 (custom-text-insert ": "))
1629 (custom-field-insert field)
1630 (custom-documentation-insert custom)
1631 field))
1633 (defun custom-default-accept (field value &optional original)
1634 "Store a new value into field FIELD, taking it from VALUE."
1635 (if original
1636 (custom-field-original-set field value))
1637 (custom-field-value-set field value)
1638 (custom-field-update field))
1640 (defun custom-default-apply (field)
1641 "Apply any changes in FIELD since the last apply."
1642 (let* ((custom (custom-field-custom field))
1643 (name (custom-name custom)))
1644 (if (null name)
1645 (error "This field cannot be applied alone"))
1646 (custom-external-set name (custom-name-value name))
1647 (custom-field-reset field)))
1649 (defun custom-default-reset (field)
1650 "Reset content of editing FIELD to `original'."
1651 (custom-field-accept field (custom-field-original field) t))
1653 (defun custom-default-factory-reset (field)
1654 "Reset content of editing FIELD to `default'."
1655 (let* ((custom (custom-field-custom field))
1656 (default (car (custom-import custom (custom-default custom)))))
1657 (or (eq default custom-nil)
1658 (custom-field-accept field default nil))))
1660 (defun custom-default-query (field)
1661 "Prompt for a FIELD"
1662 (let* ((custom (custom-field-custom field))
1663 (value (custom-field-value field))
1664 (initial (custom-write custom value))
1665 (prompt (concat (custom-prompt custom) ": ")))
1666 (custom-field-accept field
1667 (custom-read custom
1668 (if (custom-valid custom value)
1669 (read-string prompt (cons initial 1))
1670 (read-string prompt))))))
1672 (defun custom-default-match (custom values)
1673 "Match CUSTOM with VALUES."
1674 values)
1676 (defun custom-default-extract (custom field)
1677 "Extract CUSTOM's content in FIELD."
1678 (list (custom-field-value field)))
1680 (defun custom-default-validate (custom field)
1681 "Validate FIELD."
1682 (let ((value (custom-field-value field))
1683 (start (custom-field-start field)))
1684 (cond ((eq value custom-nil)
1685 (cons start "Uninitialized field"))
1686 ((and (consp value) (eq (car value) custom-invalid))
1687 (cons start "Unparsable field content"))
1688 ((custom-valid custom value)
1689 nil)
1691 (cons start "Wrong type of field content")))))
1693 (defun custom-default-face (field)
1694 "Face used for a FIELD."
1695 (let ((value (custom-field-value field)))
1696 (cond ((eq value custom-nil)
1697 custom-field-uninitialized-face)
1698 ((not (custom-valid (custom-field-custom field) value))
1699 custom-field-invalid-face)
1700 ((not (equal (custom-field-original field) value))
1701 custom-field-modified-face)
1703 custom-field-face))))
1705 (defun custom-default-update (field)
1706 "Update the content of FIELD."
1707 (let ((inhibit-point-motion-hooks t)
1708 (before-change-functions nil)
1709 (after-change-functions nil)
1710 (start (custom-field-start field))
1711 (end (custom-field-end field))
1712 (pos (point)))
1713 ;; Keep track of how many modified fields we have.
1714 (cond ((equal (custom-field-value field) (custom-field-original field))
1715 (setq custom-modified-list (delq field custom-modified-list)))
1716 ((memq field custom-modified-list))
1718 (setq custom-modified-list (cons field custom-modified-list))))
1719 ;; Update the field.
1720 (goto-char end)
1721 (insert-before-markers " ")
1722 (delete-region start (1- end))
1723 (goto-char start)
1724 (custom-field-insert field)
1725 (goto-char end)
1726 (delete-char 1)
1727 (goto-char pos)
1728 (and (<= start pos)
1729 (<= pos end)
1730 (custom-field-enter field))))
1732 ;;; Create Buffer:
1734 ;; Public functions to create a customization buffer and to insert
1735 ;; various forms of text, fields, and buttons in it.
1737 (defun customize ()
1738 "Customize GNU Emacs.
1739 Create a *Customize* buffer with editable customization information
1740 about GNU Emacs."
1741 (interactive)
1742 (custom-buffer-create "*Customize*")
1743 (custom-reset-all))
1745 (defun custom-buffer-create (name &optional custom types set get save)
1746 "Create a customization buffer named NAME.
1747 If the optional argument CUSTOM is non-nil, use that as the custom declaration.
1748 If the optional argument TYPES is non-nil, use that as the local types.
1749 If the optional argument SET is non-nil, use that to set external data.
1750 If the optional argument GET is non-nil, use that to get external data.
1751 If the optional argument SAVE is non-nil, use that for saving changes."
1752 (switch-to-buffer name)
1753 (buffer-disable-undo (current-buffer))
1754 (custom-mode)
1755 (setq custom-local-type-properties types)
1756 (if (null custom)
1758 (make-local-variable 'custom-data)
1759 (setq custom-data custom))
1760 (if (null set)
1762 (make-local-variable 'custom-external-set)
1763 (setq custom-external-set set))
1764 (if (null get)
1766 (make-local-variable 'custom-external)
1767 (setq custom-external get))
1768 (if (null save)
1770 (make-local-variable 'custom-save)
1771 (setq custom-save save))
1772 (let ((inhibit-point-motion-hooks t)
1773 (before-change-functions nil)
1774 (after-change-functions nil))
1775 (erase-buffer)
1776 (insert "\n")
1777 (goto-char (point-min))
1778 (custom-text-insert "This is a customization buffer.\n")
1779 (custom-help-insert "\n")
1780 (custom-help-button 'custom-forward-field)
1781 (custom-help-button 'custom-backward-field)
1782 (custom-help-button 'custom-enter-value)
1783 (custom-help-button 'custom-field-factory-reset)
1784 (custom-help-button 'custom-field-reset)
1785 (custom-help-button 'custom-field-apply)
1786 (custom-help-button 'custom-save-and-exit)
1787 (custom-help-button 'custom-toggle-documentation)
1788 (custom-help-insert "\nClick mouse-2 on any button to activate it.\n")
1789 (custom-text-insert "\n")
1790 (custom-insert custom-data 0)
1791 (goto-char (point-min))))
1793 (defun custom-insert (custom level)
1794 "Insert custom declaration CUSTOM in current buffer at level LEVEL."
1795 (if (stringp custom)
1796 (progn
1797 (custom-text-insert custom)
1798 nil)
1799 (and level (null (custom-property custom 'header))
1800 (setq level nil))
1801 (and level
1802 (> level 0)
1803 (custom-text-insert (concat "\n" (make-string level ?*) " ")))
1804 (let ((field (funcall (custom-property custom 'insert) custom level)))
1805 (custom-name-enter (custom-name custom) field)
1806 field)))
1808 (defun custom-text-insert (text)
1809 "Insert TEXT in current buffer."
1810 (insert text))
1812 (defun custom-tag-insert (tag field &optional data)
1813 "Insert TAG for FIELD in current buffer."
1814 (let ((from (point)))
1815 (insert tag)
1816 (custom-category-set from (point) 'custom-button-properties)
1817 (put-text-property from (point) 'custom-tag field)
1818 (if data
1819 (add-text-properties from (point) (list 'custom-data data)))))
1821 (defun custom-documentation-insert (custom &rest ignore)
1822 "Insert documentation from CUSTOM in current buffer."
1823 (let ((doc (custom-documentation custom)))
1824 (if (null doc)
1826 (custom-help-insert "\n" doc))))
1828 (defun custom-help-insert (&rest args)
1829 "Insert ARGS as documentation text."
1830 (let ((from (point)))
1831 (apply 'insert args)
1832 (custom-category-set from (point) 'custom-documentation-properties)))
1834 (defun custom-help-button (command)
1835 "Describe how to execute COMMAND."
1836 (let ((from (point)))
1837 (insert "`" (key-description (where-is-internal command nil t)) "'")
1838 (set-text-properties from (point)
1839 (list 'face custom-button-face
1840 mouse-face custom-mouse-face
1841 'custom-jump t ;Make TAB jump over it.
1842 'custom-tag command))
1843 (custom-category-set from (point) 'custom-documentation-properties))
1844 (custom-help-insert ": " (custom-first-line (documentation command)) "\n"))
1846 ;;; Mode:
1848 ;; The Customization major mode and interactive commands.
1850 (defvar custom-mode-map nil
1851 "Keymap for Custom Mode.")
1852 (if custom-mode-map
1854 (setq custom-mode-map (make-sparse-keymap))
1855 (define-key custom-mode-map (if (string-match "XEmacs" emacs-version) [button2] [mouse-2]) 'custom-push-button)
1856 (define-key custom-mode-map "\t" 'custom-forward-field)
1857 (define-key custom-mode-map "\M-\t" 'custom-backward-field)
1858 (define-key custom-mode-map "\r" 'custom-enter-value)
1859 (define-key custom-mode-map "\C-k" 'custom-kill-line)
1860 (define-key custom-mode-map "\C-c\C-r" 'custom-field-reset)
1861 (define-key custom-mode-map "\C-c\M-\C-r" 'custom-reset-all)
1862 (define-key custom-mode-map "\C-c\C-z" 'custom-field-factory-reset)
1863 (define-key custom-mode-map "\C-c\M-\C-z" 'custom-factory-reset-all)
1864 (define-key custom-mode-map "\C-c\C-a" 'custom-field-apply)
1865 (define-key custom-mode-map "\C-c\M-\C-a" 'custom-apply-all)
1866 (define-key custom-mode-map "\C-c\C-c" 'custom-save-and-exit)
1867 (define-key custom-mode-map "\C-c\C-d" 'custom-toggle-documentation))
1869 ;; C-c keymap ideas: C-a field-beginning, C-e field-end, C-f
1870 ;; forward-field, C-b backward-field, C-n next-field, C-p
1871 ;; previous-field, ? describe-field.
1873 (defun custom-mode ()
1874 "Major mode for doing customizations.
1876 \\{custom-mode-map}"
1877 (kill-all-local-variables)
1878 (setq major-mode 'custom-mode
1879 mode-name "Custom")
1880 (use-local-map custom-mode-map)
1881 (make-local-variable 'before-change-functions)
1882 (setq before-change-functions '(custom-before-change))
1883 (make-local-variable 'after-change-functions)
1884 (setq after-change-functions '(custom-after-change))
1885 (if (not (fboundp 'make-local-hook))
1886 ;; Emacs 19.28 and earlier.
1887 (add-hook 'post-command-hook
1888 (lambda ()
1889 (if (eq major-mode 'custom-mode)
1890 (custom-post-command))))
1891 ;; Emacs 19.29.
1892 (make-local-hook 'post-command-hook)
1893 (add-hook 'post-command-hook 'custom-post-command nil t)))
1895 (defun custom-forward-field (arg)
1896 "Move point to the next field or button.
1897 With optional ARG, move across that many fields."
1898 (interactive "p")
1899 (while (> arg 0)
1900 (let ((next (if (get-text-property (point) 'custom-tag)
1901 (next-single-property-change (point) 'custom-tag)
1902 (point))))
1903 (setq next (or (next-single-property-change next 'custom-tag)
1904 (next-single-property-change (point-min) 'custom-tag)))
1905 (if next
1906 (goto-char next)
1907 (error "No customization fields in this buffer.")))
1908 (or (get-text-property (point) 'custom-jump)
1909 (setq arg (1- arg))))
1910 (while (< arg 0)
1911 (let ((previous (if (get-text-property (1- (point)) 'custom-tag)
1912 (previous-single-property-change (point) 'custom-tag)
1913 (point))))
1914 (setq previous
1915 (or (previous-single-property-change previous 'custom-tag)
1916 (previous-single-property-change (point-max) 'custom-tag)))
1917 (if previous
1918 (goto-char previous)
1919 (error "No customization fields in this buffer.")))
1920 (or (get-text-property (1- (point)) 'custom-jump)
1921 (setq arg (1+ arg)))))
1923 (defun custom-backward-field (arg)
1924 "Move point to the previous field or button.
1925 With optional ARG, move across that many fields."
1926 (interactive "p")
1927 (custom-forward-field (- arg)))
1929 (defun custom-toggle-documentation (&optional arg)
1930 "Toggle display of documentation text.
1931 If the optional argument is non-nil, show text iff the argument is positive."
1932 (interactive "P")
1933 (let ((hide (or (and (null arg)
1934 (null (custom-category-get
1935 'custom-documentation-properties 'invisible)))
1936 (<= (prefix-numeric-value arg) 0))))
1937 (custom-category-put 'custom-documentation-properties 'invisible hide)
1938 (custom-category-put 'custom-documentation-properties intangible hide))
1939 (redraw-display))
1941 (defun custom-enter-value (field data)
1942 "Enter value for current customization field or push button."
1943 (interactive (list (get-text-property (point) 'custom-tag)
1944 (get-text-property (point) 'custom-data)))
1945 (cond (data
1946 (funcall field data))
1947 ((eq field 'custom-enter-value)
1948 (error "Don't be silly"))
1949 ((and (symbolp field) (fboundp field))
1950 (call-interactively field))
1951 (field
1952 (custom-field-query field))
1954 (message "Nothing to enter here"))))
1956 (defun custom-kill-line ()
1957 "Kill to end of field or end of line, whichever is first."
1958 (interactive)
1959 (let ((field (get-text-property (point) 'custom-field))
1960 (newline (save-excursion (search-forward "\n")))
1961 (next (next-single-property-change (point) 'custom-field)))
1962 (if (and field (> newline next))
1963 (kill-region (point) next)
1964 (call-interactively 'kill-line))))
1966 (defun custom-push-button (event)
1967 "Activate button below mouse pointer."
1968 (interactive "@e")
1969 (let* ((pos (event-point event))
1970 (field (get-text-property pos 'custom-field))
1971 (tag (get-text-property pos 'custom-tag))
1972 (data (get-text-property pos 'custom-data)))
1973 (cond (data
1974 (funcall tag data))
1975 ((and (symbolp tag) (fboundp tag))
1976 (call-interactively tag))
1977 (field
1978 (call-interactively (lookup-key global-map (this-command-keys))))
1979 (tag
1980 (custom-enter-value tag data))
1982 (error "Nothing to click on here.")))))
1984 (defun custom-reset-all ()
1985 "Undo any changes since the last apply in all fields."
1986 (interactive (and custom-modified-list
1987 (not (y-or-n-p "Discard all changes? "))
1988 (error "Reset aborted")))
1989 (let ((all custom-name-fields)
1990 current field)
1991 (while all
1992 (setq current (car all)
1993 field (cdr current)
1994 all (cdr all))
1995 (custom-field-reset field))))
1997 (defun custom-field-reset (field)
1998 "Undo any changes in FIELD since the last apply."
1999 (interactive (list (or (get-text-property (point) 'custom-field)
2000 (get-text-property (point) 'custom-tag))))
2001 (if (arrayp field)
2002 (let* ((custom (custom-field-custom field))
2003 (name (custom-name custom)))
2004 (save-excursion
2005 (if name
2006 (custom-field-original-set
2007 field (car (custom-import custom (custom-external name)))))
2008 (if (not (custom-valid custom (custom-field-original field)))
2009 (error "This field cannot be reset alone")
2010 (funcall (custom-property custom 'reset) field)
2011 (funcall (custom-property custom 'synchronize) field))))))
2013 (defun custom-factory-reset-all ()
2014 "Reset all field to their default values."
2015 (interactive (and custom-modified-list
2016 (not (y-or-n-p "Discard all changes? "))
2017 (error "Reset aborted")))
2018 (let ((all custom-name-fields)
2019 field)
2020 (while all
2021 (setq field (cdr (car all))
2022 all (cdr all))
2023 (custom-field-factory-reset field))))
2025 (defun custom-field-factory-reset (field)
2026 "Reset FIELD to its default value."
2027 (interactive (list (or (get-text-property (point) 'custom-field)
2028 (get-text-property (point) 'custom-tag))))
2029 (if (arrayp field)
2030 (save-excursion
2031 (funcall (custom-property (custom-field-custom field) 'factory-reset)
2032 field))))
2034 (defun custom-apply-all ()
2035 "Apply any changes since the last reset in all fields."
2036 (interactive (if custom-modified-list
2038 (error "No changes to apply.")))
2039 (custom-field-parse custom-field-last)
2040 (let ((all custom-name-fields)
2041 field)
2042 (while all
2043 (setq field (cdr (car all))
2044 all (cdr all))
2045 (let ((error (custom-field-validate (custom-field-custom field) field)))
2046 (if (null error)
2048 (goto-char (car error))
2049 (error (cdr error))))))
2050 (let ((all custom-name-fields)
2051 field)
2052 (while all
2053 (setq field (cdr (car all))
2054 all (cdr all))
2055 (custom-field-apply field))))
2057 (defun custom-field-apply (field)
2058 "Apply any changes in FIELD since the last apply."
2059 (interactive (list (or (get-text-property (point) 'custom-field)
2060 (get-text-property (point) 'custom-tag))))
2061 (custom-field-parse custom-field-last)
2062 (if (arrayp field)
2063 (let* ((custom (custom-field-custom field))
2064 (error (custom-field-validate custom field)))
2065 (if error
2066 (error (cdr error)))
2067 (funcall (custom-property custom 'apply) field))))
2069 (defun custom-toggle-hide (&rest ignore)
2070 "Hide or show entry."
2071 (interactive)
2072 (error "This button is not yet implemented"))
2074 (defun custom-save-and-exit ()
2075 "Save and exit customization buffer."
2076 (interactive "@")
2077 (save-excursion
2078 (funcall custom-save))
2079 (kill-buffer (current-buffer)))
2081 (defun custom-save ()
2082 "Save customization information."
2083 (interactive)
2084 (custom-apply-all)
2085 (let ((new custom-name-fields))
2086 (set-buffer (find-file-noselect custom-file))
2087 (goto-char (point-min))
2088 (save-excursion
2089 (let ((old (condition-case nil
2090 (read (current-buffer))
2091 (end-of-file (append '(setq custom-dummy
2092 'custom-dummy) ())))))
2093 (or (eq (car old) 'setq)
2094 (error "Invalid customization file: %s" custom-file))
2095 (while new
2096 (let* ((field (cdr (car new)))
2097 (custom (custom-field-custom field))
2098 (value (custom-field-original field))
2099 (default (car (custom-import custom (custom-default custom))))
2100 (name (car (car new))))
2101 (setq new (cdr new))
2102 (custom-assert '(eq name (custom-name custom)))
2103 (if (equal default value)
2104 (setcdr old (custom-plist-delq name (cdr old)))
2105 (setcdr old (plist-put (cdr old) name
2106 (car (custom-quote custom value)))))))
2107 (erase-buffer)
2108 (insert ";; " custom-file "\
2109 --- Automatically generated customization information.
2111 ;; Feel free to edit by hand, but the entire content should consist of
2112 ;; a single setq. Any other lisp expressions will confuse the
2113 ;; automatic configuration engine.
2115 \(setq ")
2116 (setq old (cdr old))
2117 (while old
2118 (prin1 (car old) (current-buffer))
2119 (setq old (cdr old))
2120 (insert " ")
2121 (pp (car old) (current-buffer))
2122 (setq old (cdr old))
2123 (if old (insert "\n ")))
2124 (insert ")\n")
2125 (save-buffer)
2126 (kill-buffer (current-buffer))))))
2128 (defun custom-load ()
2129 "Save customization information."
2130 (interactive (and custom-modified-list
2131 (not (equal (list (custom-name-field 'custom-file))
2132 custom-modified-list))
2133 (not (y-or-n-p "Discard all changes? "))
2134 (error "Load aborted")))
2135 (load-file (custom-name-value 'custom-file))
2136 (custom-reset-all))
2138 ;;; Field Editing:
2140 ;; Various internal functions for implementing the direct editing of
2141 ;; fields in the customization buffer.
2143 (defun custom-field-untouch (field)
2144 ;; Remove FIELD and its children from `custom-modified-list'.
2145 (setq custom-modified-list (delq field custom-modified-list))
2146 (if (arrayp field)
2147 (let ((value (custom-field-value field)))
2148 (cond ((null (custom-data (custom-field-custom field))))
2149 ((arrayp value)
2150 (custom-field-untouch value))
2151 ((listp value)
2152 (mapcar 'custom-field-untouch value))))))
2155 (defun custom-field-insert (field)
2156 ;; Insert editing FIELD in current buffer.
2157 (let ((from (point))
2158 (custom (custom-field-custom field))
2159 (value (custom-field-value field)))
2160 (insert (custom-write custom value))
2161 (insert-char (custom-padding custom)
2162 (- (custom-width custom) (- (point) from)))
2163 (custom-field-move field from (point))
2164 (set-text-properties
2165 from (point)
2166 (list 'custom-field field
2167 'custom-tag field
2168 'face (custom-field-face field)
2169 front-sticky t))))
2171 (defun custom-field-read (field)
2172 ;; Read the screen content of FIELD.
2173 (custom-read (custom-field-custom field)
2174 (buffer-substring-no-properties (custom-field-start field)
2175 (custom-field-end field))))
2177 ;; Fields are shown in a special `active' face when point is inside
2178 ;; it. You activate the field by moving point inside (entering) it
2179 ;; and deactivate the field by moving point outside (leaving) it.
2181 (defun custom-field-leave (field)
2182 ;; Deactivate FIELD.
2183 (let ((before-change-functions nil)
2184 (after-change-functions nil))
2185 (put-text-property (custom-field-start field) (custom-field-end field)
2186 'face (custom-field-face field))))
2188 (defun custom-field-enter (field)
2189 ;; Activate FIELD.
2190 (let* ((start (custom-field-start field))
2191 (end (custom-field-end field))
2192 (custom (custom-field-custom field))
2193 (padding (custom-padding custom))
2194 (before-change-functions nil)
2195 (after-change-functions nil))
2196 (or (eq this-command 'self-insert-command)
2197 (let ((pos end))
2198 (while (and (< start pos)
2199 (eq (char-after (1- pos)) padding))
2200 (setq pos (1- pos)))
2201 (if (< pos (point))
2202 (goto-char pos))))
2203 (put-text-property start end 'face custom-field-active-face)))
2205 (defun custom-field-resize (field)
2206 ;; Resize FIELD after change.
2207 (let* ((custom (custom-field-custom field))
2208 (begin (custom-field-start field))
2209 (end (custom-field-end field))
2210 (pos (point))
2211 (padding (custom-padding custom))
2212 (width (custom-width custom))
2213 (size (- end begin)))
2214 (cond ((< size width)
2215 (goto-char end)
2216 (if (fboundp 'insert-before-markers-and-inherit)
2217 ;; Emacs 19.
2218 (insert-before-markers-and-inherit
2219 (make-string (- width size) padding))
2220 ;; XEmacs: BUG: Doesn't work!
2221 (insert-before-markers (make-string (- width size) padding)))
2222 (goto-char pos))
2223 ((> size width)
2224 (let ((start (if (and (< (+ begin width) pos) (<= pos end))
2226 (+ begin width))))
2227 (goto-char end)
2228 (while (and (< start (point)) (= (preceding-char) padding))
2229 (backward-delete-char 1))
2230 (goto-char pos))))))
2232 (defvar custom-field-changed nil)
2233 ;; List of fields changed on the screen but whose VALUE attribute has
2234 ;; not yet been updated to reflect the new screen content.
2235 (make-variable-buffer-local 'custom-field-changed)
2237 (defun custom-field-parse (field)
2238 ;; Parse FIELD content iff changed.
2239 (if (memq field custom-field-changed)
2240 (progn
2241 (setq custom-field-changed (delq field custom-field-changed))
2242 (custom-field-value-set field (custom-field-read field))
2243 (custom-field-update field))))
2245 (defun custom-post-command ()
2246 ;; Keep track of their active field.
2247 (custom-assert '(eq major-mode 'custom-mode))
2248 (let ((field (custom-field-property (point))))
2249 (if (eq field custom-field-last)
2250 (if (memq field custom-field-changed)
2251 (custom-field-resize field))
2252 (custom-field-parse custom-field-last)
2253 (if custom-field-last
2254 (custom-field-leave custom-field-last))
2255 (if field
2256 (custom-field-enter field))
2257 (setq custom-field-last field))
2258 (set-buffer-modified-p (or custom-modified-list
2259 custom-field-changed))))
2261 (defvar custom-field-was nil)
2262 ;; The custom data before the change.
2263 (make-variable-buffer-local 'custom-field-was)
2265 (defun custom-before-change (begin end)
2266 ;; Check that we the modification is allowed.
2267 (if (not (eq major-mode 'custom-mode))
2268 (message "Aargh! Why is custom-before-change called here?")
2269 (let ((from (custom-field-property begin))
2270 (to (custom-field-property end)))
2271 (cond ((or (null from) (null to))
2272 (error "You can only modify the fields"))
2273 ((not (eq from to))
2274 (error "Changes must be limited to a single field."))
2276 (setq custom-field-was from))))))
2278 (defun custom-after-change (begin end length)
2279 ;; Keep track of field content.
2280 (if (not (eq major-mode 'custom-mode))
2281 (message "Aargh! Why is custom-after-change called here?")
2282 (let ((field custom-field-was))
2283 (custom-assert '(prog1 field (setq custom-field-was nil)))
2284 ;; Prevent mixing fields properties.
2285 (put-text-property begin end 'custom-field field)
2286 ;; Update the field after modification.
2287 (if (eq (custom-field-property begin) field)
2288 (let ((field-end (custom-field-end field)))
2289 (if (> end field-end)
2290 (set-marker field-end end))
2291 (add-to-list 'custom-field-changed field))
2292 ;; We deleted the entire field, reinsert it.
2293 (custom-assert '(eq begin end))
2294 (save-excursion
2295 (goto-char begin)
2296 (custom-field-value-set field
2297 (custom-read (custom-field-custom field) ""))
2298 (custom-field-insert field))))))
2300 (defun custom-field-property (pos)
2301 ;; The `custom-field' text property valid for POS.
2302 (or (get-text-property pos 'custom-field)
2303 (and (not (eq pos (point-min)))
2304 (get-text-property (1- pos) 'custom-field))))
2306 ;;; Generic Utilities:
2308 ;; Some utility functions that are not really specific to custom.
2310 (defun custom-assert (expr)
2311 "Assert that EXPR evaluates to non-nil at this point"
2312 (or (eval expr)
2313 (error "Assertion failed: %S" expr)))
2315 (defun custom-first-line (string)
2316 "Return the part of STRING before the first newline."
2317 (let ((pos 0)
2318 (len (length string)))
2319 (while (and (< pos len) (not (eq (aref string pos) ?\n)))
2320 (setq pos (1+ pos)))
2321 (if (eq pos len)
2322 string
2323 (substring string 0 pos))))
2325 (defun custom-insert-before (list old new)
2326 "In LIST insert before OLD a NEW element."
2327 (cond ((null list)
2328 (list new))
2329 ((null old)
2330 (nconc list (list new)))
2331 ((eq old (car list))
2332 (cons new list))
2334 (let ((list list))
2335 (while (not (eq old (car (cdr list))))
2336 (setq list (cdr list))
2337 (custom-assert '(cdr list)))
2338 (setcdr list (cons new (cdr list))))
2339 list)))
2341 (defun custom-strip-padding (string padding)
2342 "Remove padding from STRING."
2343 (let ((regexp (concat (regexp-quote (char-to-string padding)) "+")))
2344 (while (string-match regexp string)
2345 (setq string (concat (substring string 0 (match-beginning 0))
2346 (substring string (match-end 0))))))
2347 string)
2349 (defun custom-plist-memq (prop plist)
2350 "Return non-nil if PROP is a property of PLIST. Comparison done with EQ."
2351 (let (result)
2352 (while plist
2353 (if (eq (car plist) prop)
2354 (setq result plist
2355 plist nil)
2356 (setq plist (cdr (cdr plist)))))
2357 result))
2359 (defun custom-plist-delq (prop plist)
2360 "Delete property PROP from property list PLIST."
2361 (while (eq (car plist) prop)
2362 (setq plist (cdr (cdr plist))))
2363 (let ((list plist)
2364 (next (cdr (cdr plist))))
2365 (while next
2366 (if (eq (car next) prop)
2367 (progn
2368 (setq next (cdr (cdr next)))
2369 (setcdr (cdr list) next))
2370 (setq list next
2371 next (cdr (cdr next))))))
2372 plist)
2374 ;;; Meta Customization:
2376 (custom-declare '()
2377 '((tag . "Meta Customization")
2378 (doc . "Customization of the customization support.")
2379 (type . group)
2380 (data ((type . face-doc))
2381 ((tag . "Button Face")
2382 (default . bold)
2383 (doc . "Face used for tags in customization buffers.")
2384 (name . custom-button-face)
2385 (synchronize . (lambda (f)
2386 (custom-category-put 'custom-button-properties
2387 'face custom-button-face)))
2388 (type . face))
2389 ((tag . "Mouse Face")
2390 (default . highlight)
2391 (doc . "\
2392 Face used when mouse is above a button in customization buffers.")
2393 (name . custom-mouse-face)
2394 (synchronize . (lambda (f)
2395 (custom-category-put 'custom-button-properties
2396 mouse-face
2397 custom-mouse-face)))
2398 (type . face))
2399 ((tag . "Field Face")
2400 (default . italic)
2401 (doc . "Face used for customization fields.")
2402 (name . custom-field-face)
2403 (type . face))
2404 ((tag . "Uninitialized Face")
2405 (default . modeline)
2406 (doc . "Face used for uninitialized customization fields.")
2407 (name . custom-field-uninitialized-face)
2408 (type . face))
2409 ((tag . "Invalid Face")
2410 (default . highlight)
2411 (doc . "\
2412 Face used for customization fields containing invalid data.")
2413 (name . custom-field-invalid-face)
2414 (type . face))
2415 ((tag . "Modified Face")
2416 (default . bold-italic)
2417 (doc . "Face used for modified customization fields.")
2418 (name . custom-field-modified-face)
2419 (type . face))
2420 ((tag . "Active Face")
2421 (default . underline)
2422 (doc . "\
2423 Face used for customization fields while they are being edited.")
2424 (name . custom-field-active-face)
2425 (type . face)))))
2427 ;; custom.el uses two categories.
2429 (custom-category-create 'custom-documentation-properties)
2430 (custom-category-put 'custom-documentation-properties rear-nonsticky t)
2432 (custom-category-create 'custom-button-properties)
2433 (custom-category-put 'custom-button-properties 'face custom-button-face)
2434 (custom-category-put 'custom-button-properties mouse-face custom-mouse-face)
2435 (custom-category-put 'custom-button-properties rear-nonsticky t)
2437 (custom-category-create 'custom-hidden-properties)
2438 (custom-category-put 'custom-hidden-properties 'invisible
2439 (not (string-match "XEmacs" emacs-version)))
2440 (custom-category-put 'custom-hidden-properties intangible t)
2442 (if (file-readable-p custom-file)
2443 (load-file custom-file))
2445 (provide 'custom)
2447 ;;; custom.el ends here