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