]> code.delx.au - gnu-emacs/blob - lispref/customize.texi
*** empty log message ***
[gnu-emacs] / lispref / customize.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004,
4 @c 2005, 2006 Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/customize
7 @node Customization, Loading, Macros, Top
8 @chapter Writing Customization Definitions
9
10 This chapter describes how to declare user options for customization,
11 and also customization groups for classifying them. We use the term
12 @dfn{customization item} to include both kinds of customization
13 definitions---as well as face definitions (@pxref{Defining Faces}).
14
15 @menu
16 * Common Keywords:: Common keyword arguments for all kinds of
17 customization declarations.
18 * Group Definitions:: Writing customization group definitions.
19 * Variable Definitions:: Declaring user options.
20 * Customization Types:: Specifying the type of a user option.
21 @end menu
22
23 @node Common Keywords
24 @section Common Item Keywords
25
26 All kinds of customization declarations (for variables and groups, and
27 for faces) accept keyword arguments for specifying various information.
28 This section describes some keywords that apply to all kinds.
29
30 All of these keywords, except @code{:tag}, can be used more than once
31 in a given item. Each use of the keyword has an independent effect.
32 The keyword @code{:tag} is an exception because any given item can only
33 display one name.
34
35 @table @code
36 @item :tag @var{label}
37 Use @var{label}, a string, instead of the item's name, to label the
38 item in customization menus and buffers. @strong{Don't use a tag
39 which is substantially different from the item's real name; that would
40 cause confusion.} One legitimate case for use of @code{:tag} is to
41 specify a dash where normally a hyphen would be converted to a space:
42
43 @example
44 (defcustom cursor-in-non-selected-windows @dots{}
45 :tag "Cursor In Non-selected Windows"
46 @end example
47
48 @item :group @var{group}
49 Put this customization item in group @var{group}. When you use
50 @code{:group} in a @code{defgroup}, it makes the new group a subgroup of
51 @var{group}.
52
53 If you use this keyword more than once, you can put a single item into
54 more than one group. Displaying any of those groups will show this
55 item. Please don't overdo this, since the result would be annoying.
56
57 @item :link @var{link-data}
58 Include an external link after the documentation string for this item.
59 This is a sentence containing an active field which references some
60 other documentation.
61
62 There are several alternatives you can use for @var{link-data}:
63
64 @table @code
65 @item (custom-manual @var{info-node})
66 Link to an Info node; @var{info-node} is a string which specifies the
67 node name, as in @code{"(emacs)Top"}. The link appears as
68 @samp{[Manual]} in the customization buffer and enters the built-in
69 Info reader on @var{info-node}.
70
71 @item (info-link @var{info-node})
72 Like @code{custom-manual} except that the link appears
73 in the customization buffer with the Info node name.
74
75 @item (url-link @var{url})
76 Link to a web page; @var{url} is a string which specifies the
77 @acronym{URL}. The link appears in the customization buffer as
78 @var{url} and invokes the WWW browser specified by
79 @code{browse-url-browser-function}.
80
81 @item (emacs-commentary-link @var{library})
82 Link to the commentary section of a library; @var{library} is a string
83 which specifies the library name.
84
85 @item (emacs-library-link @var{library})
86 Link to an Emacs Lisp library file; @var{library} is a string which
87 specifies the library name.
88
89 @item (file-link @var{file})
90 Link to a file; @var{file} is a string which specifies the name of the
91 file to visit with @code{find-file} when the user invokes this link.
92
93 @item (function-link @var{function})
94 Link to the documentation of a function; @var{function} is a string
95 which specifies the name of the function to describe with
96 @code{describe-function} when the user invokes this link.
97
98 @item (variable-link @var{variable})
99 Link to the documentation of a variable; @var{variable} is a string
100 which specifies the name of the variable to describe with
101 @code{describe-variable} when the user invokes this link.
102
103 @item (custom-group-link @var{group})
104 Link to another customization group. Invoking it creates a new
105 customization buffer for @var{group}.
106 @end table
107
108 You can specify the text to use in the customization buffer by adding
109 @code{:tag @var{name}} after the first element of the @var{link-data};
110 for example, @code{(info-link :tag "foo" "(emacs)Top")} makes a link to
111 the Emacs manual which appears in the buffer as @samp{foo}.
112
113 An item can have more than one external link; however, most items have
114 none at all.
115
116 @item :load @var{file}
117 Load file @var{file} (a string) before displaying this customization
118 item. Loading is done with @code{load-library}, and only if the file is
119 not already loaded.
120
121 @item :require @var{feature}
122 Execute @code{(require '@var{feature})} when your saved customizations
123 set the value of this item. @var{feature} should be a symbol.
124
125 The most common reason to use @code{:require} is when a variable enables
126 a feature such as a minor mode, and just setting the variable won't have
127 any effect unless the code which implements the mode is loaded.
128
129 @item :version @var{version}
130 This option specifies that the item was first introduced in Emacs
131 version @var{version}, or that its default value was changed in that
132 version. The value @var{version} must be a string.
133
134 @item :package-version '(@var{package} . @var{version})
135 This option specifies that the item was first introduced in
136 @var{package} version @var{version}, or that its default value was
137 changed in that version. This keyword takes priority over :version.
138 The value of @var{package} is a symbol and @var{version} is a string.
139 The @var{package} and @var{version} must appear in the alist
140 @code{customize-package-emacs-version-alist}. Since @var{package} must
141 be unique and the user might see it in an error message, a good choice
142 is the official name of the package, such as MH-E or Gnus.
143
144 @end table
145
146 Packages that use the @code{:package-version} keyword must also update
147 the @code{customize-package-emacs-version-alist} variable.
148
149 @defvar customize-package-emacs-version-alist
150 This alist provides a mapping for the versions of Emacs that are
151 associated with versions of a package listed in the
152 @code{:package-version} keyword. Its elements look like this:
153
154 @example
155 (@var{package} (@var{pversion} . @var{eversion})@dots{})
156 @end example
157
158 For each @var{package}, which is a symbol, there are one or more
159 elements that contain a package version @var{pversion} with an
160 associated Emacs version @var{eversion}. These versions are strings.
161 For example, the MH-E package updates this alist with the following:
162
163 @smallexample
164 (add-to-list 'customize-package-emacs-version-alist
165 '(MH-E ("6.0" . "22.1") ("6.1" . "22.1") ("7.0" . "22.1")
166 ("7.1" . "22.1") ("7.2" . "22.1") ("7.3" . "22.1")
167 ("7.4" . "22.1") ("8.0" . "22.1")))
168 @end smallexample
169
170 The value of @var{package} needs to be unique and it needs to match
171 the @var{package} value appearing in the @code{:package-version}
172 keyword. Since the user might see the value in a error message, a good
173 choice is the official name of the package, such as MH-E or Gnus.
174 @end defvar
175
176 @node Group Definitions
177 @section Defining Custom Groups
178
179 Each Emacs Lisp package should have one main customization group which
180 contains all the options, faces and other groups in the package. If the
181 package has a small number of options and faces, use just one group and
182 put everything in it. When there are more than twelve or so options and
183 faces, then you should structure them into subgroups, and put the
184 subgroups under the package's main customization group. It is OK to
185 put some of the options and faces in the package's main group alongside
186 the subgroups.
187
188 The package's main or only group should be a member of one or more of
189 the standard customization groups. (To display the full list of them,
190 use @kbd{M-x customize}.) Choose one or more of them (but not too
191 many), and add your group to each of them using the @code{:group}
192 keyword.
193
194 The way to declare new customization groups is with @code{defgroup}.
195
196 @defmac defgroup group members doc [keyword value]@dots{}
197 Declare @var{group} as a customization group containing @var{members}.
198 Do not quote the symbol @var{group}. The argument @var{doc} specifies
199 the documentation string for the group.
200
201 The argument @var{members} is a list specifying an initial set of
202 customization items to be members of the group. However, most often
203 @var{members} is @code{nil}, and you specify the group's members by
204 using the @code{:group} keyword when defining those members.
205
206 If you want to specify group members through @var{members}, each element
207 should have the form @code{(@var{name} @var{widget})}. Here @var{name}
208 is a symbol, and @var{widget} is a widget type for editing that symbol.
209 Useful widgets are @code{custom-variable} for a variable,
210 @code{custom-face} for a face, and @code{custom-group} for a group.
211
212 When you introduce a new group into Emacs, use the @code{:version}
213 keyword in the @code{defgroup}; then you need not use it for
214 the individual members of the group.
215
216 In addition to the common keywords (@pxref{Common Keywords}), you can
217 also use this keyword in @code{defgroup}:
218
219 @table @code
220 @item :prefix @var{prefix}
221 If the name of an item in the group starts with @var{prefix}, then the
222 tag for that item is constructed (by default) by omitting @var{prefix}.
223
224 One group can have any number of prefixes.
225 @end table
226 @end defmac
227
228 The prefix-discarding feature is currently turned off, which means
229 that @code{:prefix} currently has no effect. We did this because we
230 found that discarding the specified prefixes often led to confusing
231 names for options. This happened because the people who wrote the
232 @code{defgroup} definitions for various groups added @code{:prefix}
233 keywords whenever they make logical sense---that is, whenever the
234 variables in the library have a common prefix.
235
236 In order to obtain good results with @code{:prefix}, it would be
237 necessary to check the specific effects of discarding a particular
238 prefix, given the specific items in a group and their names and
239 documentation. If the resulting text is not clear, then @code{:prefix}
240 should not be used in that case.
241
242 It should be possible to recheck all the customization groups, delete
243 the @code{:prefix} specifications which give unclear results, and then
244 turn this feature back on, if someone would like to do the work.
245
246 @node Variable Definitions
247 @section Defining Customization Variables
248
249 Use @code{defcustom} to declare user-editable variables.
250
251 @defmac defcustom option default doc [keyword value]@dots{}
252 Declare @var{option} as a customizable user option variable. Do not
253 quote @var{option}. The argument @var{doc} specifies the documentation
254 string for the variable. There is no need to start it with a @samp{*}
255 because @code{defcustom} automatically marks @var{option} as a
256 @dfn{user option} (@pxref{Defining Variables}).
257
258 If @var{option} is void, @code{defcustom} initializes it to
259 @var{default}. @var{default} should be an expression to compute the
260 value; be careful in writing it, because it can be evaluated on more
261 than one occasion. You should normally avoid using backquotes in
262 @var{default} because they are not expanded when editing the value,
263 causing list values to appear to have the wrong structure.
264
265 If you specify the @code{:set} option, to make the variable take other
266 special actions when set through the customization buffer, the
267 variable's documentation string should tell the user specifically how
268 to do the same job in hand-written Lisp code.
269
270 When you evaluate a @code{defcustom} form with @kbd{C-M-x} in Emacs Lisp
271 mode (@code{eval-defun}), a special feature of @code{eval-defun}
272 arranges to set the variable unconditionally, without testing whether
273 its value is void. (The same feature applies to @code{defvar}.)
274 @xref{Defining Variables}.
275 @end defmac
276
277 @code{defcustom} accepts the following additional keywords:
278
279 @table @code
280 @item :type @var{type}
281 Use @var{type} as the data type for this option. It specifies which
282 values are legitimate, and how to display the value.
283 @xref{Customization Types}, for more information.
284
285 @item :options @var{list}
286 Specify @var{list} as the list of reasonable values for use in this
287 option. The user is not restricted to using only these values, but they
288 are offered as convenient alternatives.
289
290 This is meaningful only for certain types, currently including
291 @code{hook}, @code{plist} and @code{alist}. See the definition of the
292 individual types for a description of how to use @code{:options}.
293
294 @item :set @var{setfunction}
295 Specify @var{setfunction} as the way to change the value of this
296 option. The function @var{setfunction} should take two arguments, a
297 symbol (the option name) and the new value, and should do whatever is
298 necessary to update the value properly for this option (which may not
299 mean simply setting the option as a Lisp variable). The default for
300 @var{setfunction} is @code{set-default}.
301
302 @item :get @var{getfunction}
303 Specify @var{getfunction} as the way to extract the value of this
304 option. The function @var{getfunction} should take one argument, a
305 symbol, and should return whatever customize should use as the
306 ``current value'' for that symbol (which need not be the symbol's Lisp
307 value). The default is @code{default-value}.
308
309 You have to really understand the workings of Custom to use
310 @code{:get} correctly. It is meant for values that are treated in
311 Custom as variables but are not actually stored in Lisp variables. It
312 is almost surely a mistake to specify @code{getfunction} for a value
313 that really is stored in a Lisp variable.
314
315 @item :initialize @var{function}
316 @var{function} should be a function used to initialize the variable
317 when the @code{defcustom} is evaluated. It should take two arguments,
318 the option name (a symbol) and the value. Here are some predefined
319 functions meant for use in this way:
320
321 @table @code
322 @item custom-initialize-set
323 Use the variable's @code{:set} function to initialize the variable, but
324 do not reinitialize it if it is already non-void.
325
326 @item custom-initialize-default
327 Like @code{custom-initialize-set}, but use the function
328 @code{set-default} to set the variable, instead of the variable's
329 @code{:set} function. This is the usual choice for a variable whose
330 @code{:set} function enables or disables a minor mode; with this choice,
331 defining the variable will not call the minor mode function, but
332 customizing the variable will do so.
333
334 @item custom-initialize-reset
335 Always use the @code{:set} function to initialize the variable. If
336 the variable is already non-void, reset it by calling the @code{:set}
337 function using the current value (returned by the @code{:get} method).
338 This is the default @code{:initialize} function.
339
340 @item custom-initialize-changed
341 Use the @code{:set} function to initialize the variable, if it is
342 already set or has been customized; otherwise, just use
343 @code{set-default}.
344
345 @item custom-initialize-safe-set
346 @itemx custom-initialize-safe-default
347 These functions behave like @code{custom-initialize-set}
348 (@code{custom-initialize-default}, respectively), but catch errors.
349 If an error occurs during initialization, they set the variable to
350 @code{nil} using @code{set-default}, and throw no error.
351
352 These two functions are only meant for options defined in pre-loaded
353 files, where some variables or functions used to compute the option's
354 value may not yet be defined. The option normally gets updated in
355 @file{startup.el}, ignoring the previously computed value. Because of
356 this typical usage, the value which these two functions compute
357 normally only matters when, after startup, one unsets the option's
358 value and then reevaluates the defcustom. By that time, the necessary
359 variables and functions will be defined, so there will not be an error.
360 @end table
361
362 @item :set-after @var{variables}
363 When setting variables according to saved customizations, make sure to
364 set the variables @var{variables} before this one; in other words, delay
365 setting this variable until after those others have been handled. Use
366 @code{:set-after} if setting this variable won't work properly unless
367 those other variables already have their intended values.
368 @end table
369
370 The @code{:require} option is useful for an option that turns on the
371 operation of a certain feature. Assuming that the package is coded to
372 check the value of the option, you still need to arrange for the package
373 to be loaded. You can do that with @code{:require}. @xref{Common
374 Keywords}. Here is an example, from the library @file{saveplace.el}:
375
376 @example
377 (defcustom save-place nil
378 "*Non-nil means automatically save place in each file..."
379 :type 'boolean
380 :require 'saveplace
381 :group 'save-place)
382 @end example
383
384 If a customization item has a type such as @code{hook} or @code{alist},
385 which supports @code{:options}, you can add additional options to the
386 item, outside the @code{defcustom} declaration, by calling
387 @code{custom-add-option}. For example, if you define a function
388 @code{my-lisp-mode-initialization} intended to be called from
389 @code{emacs-lisp-mode-hook}, you might want to add that to the list of
390 options for @code{emacs-lisp-mode-hook}, but not by editing its
391 definition. You can do it thus:
392
393 @example
394 (custom-add-option 'emacs-lisp-mode-hook
395 'my-lisp-mode-initialization)
396 @end example
397
398 @defun custom-add-option symbol option
399 To the customization @var{symbol}, add @var{option}.
400
401 The precise effect of adding @var{option} depends on the customization
402 type of @var{symbol}.
403 @end defun
404
405 Internally, @code{defcustom} uses the symbol property
406 @code{standard-value} to record the expression for the default value,
407 and @code{saved-value} to record the value saved by the user with the
408 customization buffer. Both properties are actually lists whose car is
409 an expression which evaluates to the value.
410
411 @node Customization Types
412 @section Customization Types
413
414 When you define a user option with @code{defcustom}, you must specify
415 its @dfn{customization type}. That is a Lisp object which describes (1)
416 which values are legitimate and (2) how to display the value in the
417 customization buffer for editing.
418
419 You specify the customization type in @code{defcustom} with the
420 @code{:type} keyword. The argument of @code{:type} is evaluated, but
421 only once when the @code{defcustom} is executed, so it isn't useful
422 for the value to vary. Normally we use a quoted constant. For
423 example:
424
425 @example
426 (defcustom diff-command "diff"
427 "*The command to use to run diff."
428 :type '(string)
429 :group 'diff)
430 @end example
431
432 In general, a customization type is a list whose first element is a
433 symbol, one of the customization type names defined in the following
434 sections. After this symbol come a number of arguments, depending on
435 the symbol. Between the type symbol and its arguments, you can
436 optionally write keyword-value pairs (@pxref{Type Keywords}).
437
438 Some of the type symbols do not use any arguments; those are called
439 @dfn{simple types}. For a simple type, if you do not use any
440 keyword-value pairs, you can omit the parentheses around the type
441 symbol. For example just @code{string} as a customization type is
442 equivalent to @code{(string)}.
443
444 @menu
445 * Simple Types::
446 * Composite Types::
447 * Splicing into Lists::
448 * Type Keywords::
449 * Defining New Types::
450 @end menu
451
452 All customization types are implemented as widgets; see @ref{Top, ,
453 Introduction, widget, The Emacs Widget Library}, for details.
454
455 @node Simple Types
456 @subsection Simple Types
457
458 This section describes all the simple customization types.
459
460 @table @code
461 @item sexp
462 The value may be any Lisp object that can be printed and read back. You
463 can use @code{sexp} as a fall-back for any option, if you don't want to
464 take the time to work out a more specific type to use.
465
466 @item integer
467 The value must be an integer, and is represented textually
468 in the customization buffer.
469
470 @item number
471 The value must be a number (floating point or integer), and is
472 represented textually in the customization buffer.
473
474 @item float
475 The value must be a floating point number, and is represented
476 textually in the customization buffer.
477
478 @item string
479 The value must be a string, and the customization buffer shows just the
480 contents, with no delimiting @samp{"} characters and no quoting with
481 @samp{\}.
482
483 @item regexp
484 Like @code{string} except that the string must be a valid regular
485 expression.
486
487 @item character
488 The value must be a character code. A character code is actually an
489 integer, but this type shows the value by inserting the character in the
490 buffer, rather than by showing the number.
491
492 @item file
493 The value must be a file name, and you can do completion with
494 @kbd{M-@key{TAB}}.
495
496 @item (file :must-match t)
497 The value must be a file name for an existing file, and you can do
498 completion with @kbd{M-@key{TAB}}.
499
500 @item directory
501 The value must be a directory name, and you can do completion with
502 @kbd{M-@key{TAB}}.
503
504 @item hook
505 The value must be a list of functions (or a single function, but that is
506 obsolete usage). This customization type is used for hook variables.
507 You can use the @code{:options} keyword in a hook variable's
508 @code{defcustom} to specify a list of functions recommended for use in
509 the hook; see @ref{Variable Definitions}.
510
511 @item alist
512 The value must be a list of cons-cells, the @sc{car} of each cell
513 representing a key, and the @sc{cdr} of the same cell representing an
514 associated value. The user can add and delete key/value pairs, and
515 edit both the key and the value of each pair.
516
517 You can specify the key and value types like this:
518
519 @smallexample
520 (alist :key-type @var{key-type} :value-type @var{value-type})
521 @end smallexample
522
523 @noindent
524 where @var{key-type} and @var{value-type} are customization type
525 specifications. The default key type is @code{sexp}, and the default
526 value type is @code{sexp}.
527
528 The user can add any key matching the specified key type, but you can
529 give some keys a preferential treatment by specifying them with the
530 @code{:options} (see @ref{Variable Definitions}). The specified keys
531 will always be shown in the customize buffer (together with a suitable
532 value), with a checkbox to include or exclude or disable the key/value
533 pair from the alist. The user will not be able to edit the keys
534 specified by the @code{:options} keyword argument.
535
536 The argument to the @code{:options} keywords should be a list of option
537 specifications. Ordinarily, the options are simply atoms, which are the
538 specified keys. For example:
539
540 @smallexample
541 :options '("foo" "bar" "baz")
542 @end smallexample
543
544 @noindent
545 specifies that there are three ``known'' keys, namely @code{"foo"},
546 @code{"bar"} and @code{"baz"}, which will always be shown first.
547
548 You may want to restrict the value type for specific keys, for example,
549 the value associated with the @code{"bar"} key can only be an integer.
550 You can specify this by using a list instead of an atom in the option
551 specification. The first element will specify the key, like before,
552 while the second element will specify the value type.
553
554 @smallexample
555 :options '("foo" ("bar" integer) "baz")
556 @end smallexample
557
558 Finally, you may want to change how the key is presented. By default,
559 the key is simply shown as a @code{const}, since the user cannot change
560 the special keys specified with the @code{:options} keyword. However,
561 you may want to use a more specialized type for presenting the key, like
562 @code{function-item} if you know it is a symbol with a function binding.
563 This is done by using a customization type specification instead of a
564 symbol for the key.
565
566 @smallexample
567 :options '("foo" ((function-item some-function) integer) "baz")
568 @end smallexample
569
570 Many alists use lists with two elements, instead of cons cells. For
571 example,
572
573 @smallexample
574 (defcustom list-alist '(("foo" 1) ("bar" 2) ("baz" 3))
575 "Each element is a list of the form (KEY VALUE).")
576 @end smallexample
577
578 @noindent
579 instead of
580
581 @smallexample
582 (defcustom cons-alist '(("foo" . 1) ("bar" . 2) ("baz" . 3))
583 "Each element is a cons-cell (KEY . VALUE).")
584 @end smallexample
585
586 Because of the way lists are implemented on top of cons cells, you can
587 treat @code{list-alist} in the example above as a cons cell alist, where
588 the value type is a list with a single element containing the real
589 value.
590
591 @smallexample
592 (defcustom list-alist '(("foo" 1) ("bar" 2) ("baz" 3))
593 "Each element is a list of the form (KEY VALUE)."
594 :type '(alist :value-type (group integer)))
595 @end smallexample
596
597 The @code{group} widget is used here instead of @code{list} only because
598 the formatting is better suited for the purpose.
599
600 Similarly, you can have alists with more values associated with each
601 key, using variations of this trick:
602
603 @smallexample
604 (defcustom person-data '(("brian" 50 t)
605 ("dorith" 55 nil)
606 ("ken" 52 t))
607 "Alist of basic info about people.
608 Each element has the form (NAME AGE MALE-FLAG)."
609 :type '(alist :value-type (group integer boolean)))
610
611 (defcustom pets '(("brian")
612 ("dorith" "dog" "guppy")
613 ("ken" "cat"))
614 "Alist of people's pets.
615 In an element (KEY . VALUE), KEY is the person's name,
616 and the VALUE is a list of that person's pets."
617 :type '(alist :value-type (repeat string)))
618 @end smallexample
619
620 @item plist
621 The @code{plist} custom type is similar to the @code{alist} (see above),
622 except that the information is stored as a property list, i.e. a list of
623 this form:
624
625 @smallexample
626 (@var{key} @var{value} @var{key} @var{value} @var{key} @var{value} @dots{})
627 @end smallexample
628
629 The default @code{:key-type} for @code{plist} is @code{symbol},
630 rather than @code{sexp}.
631
632 @item symbol
633 The value must be a symbol. It appears in the customization buffer as
634 the name of the symbol.
635
636 @item function
637 The value must be either a lambda expression or a function name. When
638 it is a function name, you can do completion with @kbd{M-@key{TAB}}.
639
640 @item variable
641 The value must be a variable name, and you can do completion with
642 @kbd{M-@key{TAB}}.
643
644 @item face
645 The value must be a symbol which is a face name, and you can do
646 completion with @kbd{M-@key{TAB}}.
647
648 @item boolean
649 The value is boolean---either @code{nil} or @code{t}. Note that by
650 using @code{choice} and @code{const} together (see the next section),
651 you can specify that the value must be @code{nil} or @code{t}, but also
652 specify the text to describe each value in a way that fits the specific
653 meaning of the alternative.
654
655 @item coding-system
656 The value must be a coding-system name, and you can do completion with
657 @kbd{M-@key{TAB}}.
658
659 @item color
660 The value must be a valid color name, and you can do completion with
661 @kbd{M-@key{TAB}}. A sample is provided.
662 @end table
663
664 @node Composite Types
665 @subsection Composite Types
666 @cindex arguments (of composite type)
667
668 When none of the simple types is appropriate, you can use composite
669 types, which build new types from other types or from specified data.
670 The specified types or data are called the @dfn{arguments} of the
671 composite type. The composite type normally looks like this:
672
673 @example
674 (@var{constructor} @var{arguments}@dots{})
675 @end example
676
677 @noindent
678 but you can also add keyword-value pairs before the arguments, like
679 this:
680
681 @example
682 (@var{constructor} @r{@{}@var{keyword} @var{value}@r{@}}@dots{} @var{arguments}@dots{})
683 @end example
684
685 Here is a table of constructors and how to use them to write
686 composite types:
687
688 @table @code
689 @item (cons @var{car-type} @var{cdr-type})
690 The value must be a cons cell, its @sc{car} must fit @var{car-type}, and
691 its @sc{cdr} must fit @var{cdr-type}. For example, @code{(cons string
692 symbol)} is a customization type which matches values such as
693 @code{("foo" . foo)}.
694
695 In the customization buffer, the @sc{car} and the @sc{cdr} are
696 displayed and edited separately, each according to the type
697 that you specify for it.
698
699 @item (list @var{element-types}@dots{})
700 The value must be a list with exactly as many elements as the
701 @var{element-types} you have specified; and each element must fit the
702 corresponding @var{element-type}.
703
704 For example, @code{(list integer string function)} describes a list of
705 three elements; the first element must be an integer, the second a
706 string, and the third a function.
707
708 In the customization buffer, each element is displayed and edited
709 separately, according to the type specified for it.
710
711 @item (vector @var{element-types}@dots{})
712 Like @code{list} except that the value must be a vector instead of a
713 list. The elements work the same as in @code{list}.
714
715 @item (choice @var{alternative-types}@dots{})
716 The value must fit at least one of @var{alternative-types}.
717 For example, @code{(choice integer string)} allows either an
718 integer or a string.
719
720 In the customization buffer, the user selects one of the alternatives
721 using a menu, and can then edit the value in the usual way for that
722 alternative.
723
724 Normally the strings in this menu are determined automatically from the
725 choices; however, you can specify different strings for the menu by
726 including the @code{:tag} keyword in the alternatives. For example, if
727 an integer stands for a number of spaces, while a string is text to use
728 verbatim, you might write the customization type this way,
729
730 @example
731 (choice (integer :tag "Number of spaces")
732 (string :tag "Literal text"))
733 @end example
734
735 @noindent
736 so that the menu offers @samp{Number of spaces} and @samp{Literal text}.
737
738 In any alternative for which @code{nil} is not a valid value, other than
739 a @code{const}, you should specify a valid default for that alternative
740 using the @code{:value} keyword. @xref{Type Keywords}.
741
742 If some values are covered by more than one of the alternatives,
743 customize will choose the first alternative that the value fits. This
744 means you should always list the most specific types first, and the
745 most general last. Here's an example of proper usage:
746
747 @example
748 (choice (const :tag "Off" nil)
749 symbol (sexp :tag "Other"))
750 @end example
751
752 @noindent
753 This way, the special value @code{nil} is not treated like other
754 symbols, and symbols are not treated like other Lisp expressions.
755
756 @item (radio @var{element-types}@dots{})
757 This is similar to @code{choice}, except that the choices are displayed
758 using `radio buttons' rather than a menu. This has the advantage of
759 displaying documentation for the choices when applicable and so is often
760 a good choice for a choice between constant functions
761 (@code{function-item} customization types).
762
763 @item (const @var{value})
764 The value must be @var{value}---nothing else is allowed.
765
766 The main use of @code{const} is inside of @code{choice}. For example,
767 @code{(choice integer (const nil))} allows either an integer or
768 @code{nil}.
769
770 @code{:tag} is often used with @code{const}, inside of @code{choice}.
771 For example,
772
773 @example
774 (choice (const :tag "Yes" t)
775 (const :tag "No" nil)
776 (const :tag "Ask" foo))
777 @end example
778
779 @noindent
780 describes a variable for which @code{t} means yes, @code{nil} means no,
781 and @code{foo} means ``ask.''
782
783 @item (other @var{value})
784 This alternative can match any Lisp value, but if the user chooses this
785 alternative, that selects the value @var{value}.
786
787 The main use of @code{other} is as the last element of @code{choice}.
788 For example,
789
790 @example
791 (choice (const :tag "Yes" t)
792 (const :tag "No" nil)
793 (other :tag "Ask" foo))
794 @end example
795
796 @noindent
797 describes a variable for which @code{t} means yes, @code{nil} means no,
798 and anything else means ``ask.'' If the user chooses @samp{Ask} from
799 the menu of alternatives, that specifies the value @code{foo}; but any
800 other value (not @code{t}, @code{nil} or @code{foo}) displays as
801 @samp{Ask}, just like @code{foo}.
802
803 @item (function-item @var{function})
804 Like @code{const}, but used for values which are functions. This
805 displays the documentation string as well as the function name.
806 The documentation string is either the one you specify with
807 @code{:doc}, or @var{function}'s own documentation string.
808
809 @item (variable-item @var{variable})
810 Like @code{const}, but used for values which are variable names. This
811 displays the documentation string as well as the variable name. The
812 documentation string is either the one you specify with @code{:doc}, or
813 @var{variable}'s own documentation string.
814
815 @item (set @var{types}@dots{})
816 The value must be a list, and each element of the list must match one of
817 the @var{types} specified.
818
819 This appears in the customization buffer as a checklist, so that each of
820 @var{types} may have either one corresponding element or none. It is
821 not possible to specify two different elements that match the same one
822 of @var{types}. For example, @code{(set integer symbol)} allows one
823 integer and/or one symbol in the list; it does not allow multiple
824 integers or multiple symbols. As a result, it is rare to use
825 nonspecific types such as @code{integer} in a @code{set}.
826
827 Most often, the @var{types} in a @code{set} are @code{const} types, as
828 shown here:
829
830 @example
831 (set (const :bold) (const :italic))
832 @end example
833
834 Sometimes they describe possible elements in an alist:
835
836 @example
837 (set (cons :tag "Height" (const height) integer)
838 (cons :tag "Width" (const width) integer))
839 @end example
840
841 @noindent
842 That lets the user specify a height value optionally
843 and a width value optionally.
844
845 @item (repeat @var{element-type})
846 The value must be a list and each element of the list must fit the type
847 @var{element-type}. This appears in the customization buffer as a
848 list of elements, with @samp{[INS]} and @samp{[DEL]} buttons for adding
849 more elements or removing elements.
850
851 @item (restricted-sexp :match-alternatives @var{criteria})
852 This is the most general composite type construct. The value may be
853 any Lisp object that satisfies one of @var{criteria}. @var{criteria}
854 should be a list, and each element should be one of these
855 possibilities:
856
857 @itemize @bullet
858 @item
859 A predicate---that is, a function of one argument that has no side
860 effects, and returns either @code{nil} or non-@code{nil} according to
861 the argument. Using a predicate in the list says that objects for which
862 the predicate returns non-@code{nil} are acceptable.
863
864 @item
865 A quoted constant---that is, @code{'@var{object}}. This sort of element
866 in the list says that @var{object} itself is an acceptable value.
867 @end itemize
868
869 For example,
870
871 @example
872 (restricted-sexp :match-alternatives
873 (integerp 't 'nil))
874 @end example
875
876 @noindent
877 allows integers, @code{t} and @code{nil} as legitimate values.
878
879 The customization buffer shows all legitimate values using their read
880 syntax, and the user edits them textually.
881 @end table
882
883 Here is a table of the keywords you can use in keyword-value pairs
884 in a composite type:
885
886 @table @code
887 @item :tag @var{tag}
888 Use @var{tag} as the name of this alternative, for user communication
889 purposes. This is useful for a type that appears inside of a
890 @code{choice}.
891
892 @item :match-alternatives @var{criteria}
893 Use @var{criteria} to match possible values. This is used only in
894 @code{restricted-sexp}.
895
896 @item :args @var{argument-list}
897 Use the elements of @var{argument-list} as the arguments of the type
898 construct. For instance, @code{(const :args (foo))} is equivalent to
899 @code{(const foo)}. You rarely need to write @code{:args} explicitly,
900 because normally the arguments are recognized automatically as
901 whatever follows the last keyword-value pair.
902 @end table
903
904 @node Splicing into Lists
905 @subsection Splicing into Lists
906
907 The @code{:inline} feature lets you splice a variable number of
908 elements into the middle of a list or vector. You use it in a
909 @code{set}, @code{choice} or @code{repeat} type which appears among the
910 element-types of a @code{list} or @code{vector}.
911
912 Normally, each of the element-types in a @code{list} or @code{vector}
913 describes one and only one element of the list or vector. Thus, if an
914 element-type is a @code{repeat}, that specifies a list of unspecified
915 length which appears as one element.
916
917 But when the element-type uses @code{:inline}, the value it matches is
918 merged directly into the containing sequence. For example, if it
919 matches a list with three elements, those become three elements of the
920 overall sequence. This is analogous to using @samp{,@@} in the backquote
921 construct.
922
923 For example, to specify a list whose first element must be @code{baz}
924 and whose remaining arguments should be zero or more of @code{foo} and
925 @code{bar}, use this customization type:
926
927 @example
928 (list (const baz) (set :inline t (const foo) (const bar)))
929 @end example
930
931 @noindent
932 This matches values such as @code{(baz)}, @code{(baz foo)}, @code{(baz bar)}
933 and @code{(baz foo bar)}.
934
935 When the element-type is a @code{choice}, you use @code{:inline} not
936 in the @code{choice} itself, but in (some of) the alternatives of the
937 @code{choice}. For example, to match a list which must start with a
938 file name, followed either by the symbol @code{t} or two strings, use
939 this customization type:
940
941 @example
942 (list file
943 (choice (const t)
944 (list :inline t string string)))
945 @end example
946
947 @noindent
948 If the user chooses the first alternative in the choice, then the
949 overall list has two elements and the second element is @code{t}. If
950 the user chooses the second alternative, then the overall list has three
951 elements and the second and third must be strings.
952
953 @node Type Keywords
954 @subsection Type Keywords
955
956 You can specify keyword-argument pairs in a customization type after the
957 type name symbol. Here are the keywords you can use, and their
958 meanings:
959
960 @table @code
961 @item :value @var{default}
962 This is used for a type that appears as an alternative inside of
963 @code{choice}; it specifies the default value to use, at first, if and
964 when the user selects this alternative with the menu in the
965 customization buffer.
966
967 Of course, if the actual value of the option fits this alternative, it
968 will appear showing the actual value, not @var{default}.
969
970 If @code{nil} is not a valid value for the alternative, then it is
971 essential to specify a valid default with @code{:value}.
972
973 @item :format @var{format-string}
974 This string will be inserted in the buffer to represent the value
975 corresponding to the type. The following @samp{%} escapes are available
976 for use in @var{format-string}:
977
978 @table @samp
979 @item %[@var{button}%]
980 Display the text @var{button} marked as a button. The @code{:action}
981 attribute specifies what the button will do if the user invokes it;
982 its value is a function which takes two arguments---the widget which
983 the button appears in, and the event.
984
985 There is no way to specify two different buttons with different
986 actions.
987
988 @item %@{@var{sample}%@}
989 Show @var{sample} in a special face specified by @code{:sample-face}.
990
991 @item %v
992 Substitute the item's value. How the value is represented depends on
993 the kind of item, and (for variables) on the customization type.
994
995 @item %d
996 Substitute the item's documentation string.
997
998 @item %h
999 Like @samp{%d}, but if the documentation string is more than one line,
1000 add an active field to control whether to show all of it or just the
1001 first line.
1002
1003 @item %t
1004 Substitute the tag here. You specify the tag with the @code{:tag}
1005 keyword.
1006
1007 @item %%
1008 Display a literal @samp{%}.
1009 @end table
1010
1011 @item :action @var{action}
1012 Perform @var{action} if the user clicks on a button.
1013
1014 @item :button-face @var{face}
1015 Use the face @var{face} (a face name or a list of face names) for button
1016 text displayed with @samp{%[@dots{}%]}.
1017
1018 @item :button-prefix @var{prefix}
1019 @itemx :button-suffix @var{suffix}
1020 These specify the text to display before and after a button.
1021 Each can be:
1022
1023 @table @asis
1024 @item @code{nil}
1025 No text is inserted.
1026
1027 @item a string
1028 The string is inserted literally.
1029
1030 @item a symbol
1031 The symbol's value is used.
1032 @end table
1033
1034 @item :tag @var{tag}
1035 Use @var{tag} (a string) as the tag for the value (or part of the value)
1036 that corresponds to this type.
1037
1038 @item :doc @var{doc}
1039 Use @var{doc} as the documentation string for this value (or part of the
1040 value) that corresponds to this type. In order for this to work, you
1041 must specify a value for @code{:format}, and use @samp{%d} or @samp{%h}
1042 in that value.
1043
1044 The usual reason to specify a documentation string for a type is to
1045 provide more information about the meanings of alternatives inside a
1046 @code{:choice} type or the parts of some other composite type.
1047
1048 @item :help-echo @var{motion-doc}
1049 When you move to this item with @code{widget-forward} or
1050 @code{widget-backward}, it will display the string @var{motion-doc} in
1051 the echo area. In addition, @var{motion-doc} is used as the mouse
1052 @code{help-echo} string and may actually be a function or form evaluated
1053 to yield a help string. If it is a function, it is called with one
1054 argument, the widget.
1055
1056 @item :match @var{function}
1057 Specify how to decide whether a value matches the type. The
1058 corresponding value, @var{function}, should be a function that accepts
1059 two arguments, a widget and a value; it should return non-@code{nil} if
1060 the value is acceptable.
1061
1062 @ignore
1063 @item :indent @var{columns}
1064 Indent this item by @var{columns} columns. The indentation is used for
1065 @samp{%n}, and automatically for group names, for checklists and radio
1066 buttons, and for editable lists. It affects the whole of the
1067 item except for the first line.
1068
1069 @item :offset @var{columns}
1070 An integer indicating how many extra spaces to indent the subitems of
1071 this item. By default, subitems are indented the same as their parent.
1072
1073 @item :extra-offset
1074 An integer indicating how many extra spaces to add to this item's
1075 indentation, compared to its parent.
1076
1077 @item :notify
1078 A function called each time the item or a subitem is changed. The
1079 function is called with two or three arguments. The first argument is
1080 the item itself, the second argument is the item that was changed, and
1081 the third argument is the event leading to the change, if any.
1082
1083 @item :menu-tag
1084 A tag used in the menu when the widget is used as an option in a
1085 @code{menu-choice} widget.
1086
1087 @item :menu-tag-get
1088 A function used for finding the tag when the widget is used as an option
1089 in a @code{menu-choice} widget. By default, the tag used will be either the
1090 @code{:menu-tag} or @code{:tag} property if present, or the @code{princ}
1091 representation of the @code{:value} property if not.
1092
1093 @item :validate
1094 A function which takes a widget as an argument, and return @code{nil}
1095 if the widget's current value is valid for the widget. Otherwise, it
1096 should return the widget containing the invalid data, and set that
1097 widget's @code{:error} property to a string explaining the error.
1098
1099 You can use the function @code{widget-children-validate} for this job;
1100 it tests that all children of @var{widget} are valid.
1101
1102 @item :tab-order
1103 Specify the order in which widgets are traversed with
1104 @code{widget-forward} or @code{widget-backward}. This is only partially
1105 implemented.
1106
1107 @enumerate a
1108 @item
1109 Widgets with tabbing order @code{-1} are ignored.
1110
1111 @item
1112 (Unimplemented) When on a widget with tabbing order @var{n}, go to the
1113 next widget in the buffer with tabbing order @var{n+1} or @code{nil},
1114 whichever comes first.
1115
1116 @item
1117 When on a widget with no tabbing order specified, go to the next widget
1118 in the buffer with a positive tabbing order, or @code{nil}
1119 @end enumerate
1120
1121 @item :parent
1122 The parent of a nested widget (e.g., a @code{menu-choice} item or an
1123 element of a @code{editable-list} widget).
1124
1125 @item :sibling-args
1126 This keyword is only used for members of a @code{radio-button-choice} or
1127 @code{checklist}. The value should be a list of extra keyword
1128 arguments, which will be used when creating the @code{radio-button} or
1129 @code{checkbox} associated with this item.
1130 @end ignore
1131 @end table
1132
1133 @node Defining New Types
1134 @subsection Defining New Types
1135
1136 In the previous sections we have described how to construct elaborate
1137 type specifications for @code{defcustom}. In some cases you may want
1138 to give such a type specification a name. The obvious case is when
1139 you are using the same type for many user options: rather than repeat
1140 the specification for each option, you can give the type specification
1141 a name, and use that name each @code{defcustom}. The other case is
1142 when a user option's value is a recursive data structure. To make it
1143 possible for a datatype to refer to itself, it needs to have a name.
1144
1145 Since custom types are implemented as widgets, the way to define a new
1146 customize type is to define a new widget. We are not going to describe
1147 the widget interface here in details, see @ref{Top, , Introduction,
1148 widget, The Emacs Widget Library}, for that. Instead we are going to
1149 demonstrate the minimal functionality needed for defining new customize
1150 types by a simple example.
1151
1152 @example
1153 (define-widget 'binary-tree-of-string 'lazy
1154 "A binary tree made of cons-cells and strings."
1155 :offset 4
1156 :tag "Node"
1157 :type '(choice (string :tag "Leaf" :value "")
1158 (cons :tag "Interior"
1159 :value ("" . "")
1160 binary-tree-of-string
1161 binary-tree-of-string)))
1162
1163 (defcustom foo-bar ""
1164 "Sample variable holding a binary tree of strings."
1165 :type 'binary-tree-of-string)
1166 @end example
1167
1168 The function to define a new widget is called @code{define-widget}. The
1169 first argument is the symbol we want to make a new widget type. The
1170 second argument is a symbol representing an existing widget, the new
1171 widget is going to be defined in terms of difference from the existing
1172 widget. For the purpose of defining new customization types, the
1173 @code{lazy} widget is perfect, because it accepts a @code{:type} keyword
1174 argument with the same syntax as the keyword argument to
1175 @code{defcustom} with the same name. The third argument is a
1176 documentation string for the new widget. You will be able to see that
1177 string with the @kbd{M-x widget-browse @key{RET} binary-tree-of-string
1178 @key{RET}} command.
1179
1180 After these mandatory arguments follow the keyword arguments. The most
1181 important is @code{:type}, which describes the data type we want to match
1182 with this widget. Here a @code{binary-tree-of-string} is described as
1183 being either a string, or a cons-cell whose car and cdr are themselves
1184 both @code{binary-tree-of-string}. Note the reference to the widget
1185 type we are currently in the process of defining. The @code{:tag}
1186 attribute is a string to name the widget in the user interface, and the
1187 @code{:offset} argument is there to ensure that child nodes are
1188 indented four spaces relative to the parent node, making the tree
1189 structure apparent in the customization buffer.
1190
1191 The @code{defcustom} shows how the new widget can be used as an ordinary
1192 customization type.
1193
1194 The reason for the name @code{lazy} is that the other composite
1195 widgets convert their inferior widgets to internal form when the
1196 widget is instantiated in a buffer. This conversion is recursive, so
1197 the inferior widgets will convert @emph{their} inferior widgets. If
1198 the data structure is itself recursive, this conversion is an infinite
1199 recursion. The @code{lazy} widget prevents the recursion: it convert
1200 its @code{:type} argument only when needed.
1201
1202 @ignore
1203 arch-tag: d1b8fad3-f48c-4ce4-a402-f73b5ef19bd2
1204 @end ignore