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