Remove defining user variables via * in docstring.
[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
b8d4c8d0
GM
465@node Customization Types
466@section Customization Types
467
468@cindex customization types
469 When you define a user option with @code{defcustom}, you must specify
470its @dfn{customization type}. That is a Lisp object which describes (1)
471which values are legitimate and (2) how to display the value in the
472customization buffer for editing.
473
474@kindex type@r{, @code{defcustom} keyword}
475 You specify the customization type in @code{defcustom} with the
476@code{:type} keyword. The argument of @code{:type} is evaluated, but
477only once when the @code{defcustom} is executed, so it isn't useful
478for the value to vary. Normally we use a quoted constant. For
479example:
480
481@example
482(defcustom diff-command "diff"
483 "The command to use to run diff."
484 :type '(string)
485 :group 'diff)
486@end example
487
488 In general, a customization type is a list whose first element is a
489symbol, one of the customization type names defined in the following
490sections. After this symbol come a number of arguments, depending on
491the symbol. Between the type symbol and its arguments, you can
492optionally write keyword-value pairs (@pxref{Type Keywords}).
493
0f631634 494 Some type symbols do not use any arguments; those are called
b8d4c8d0
GM
495@dfn{simple types}. For a simple type, if you do not use any
496keyword-value pairs, you can omit the parentheses around the type
497symbol. For example just @code{string} as a customization type is
498equivalent to @code{(string)}.
499
0f631634
CY
500 All customization types are implemented as widgets; see @ref{Top, ,
501Introduction, widget, The Emacs Widget Library}, for details.
502
b8d4c8d0 503@menu
3deead93 504* Simple Types:: Simple customization types: sexp, integer, etc.
51d9979c
GM
505* Composite Types:: Build new types from other types or data.
506* Splicing into Lists:: Splice elements into list with @code{:inline}.
507* Type Keywords:: Keyword-argument pairs in a customization type.
508* Defining New Types:: Give your type a name.
b8d4c8d0
GM
509@end menu
510
b8d4c8d0
GM
511@node Simple Types
512@subsection Simple Types
513
e1161b06
CY
514 This section describes all the simple customization types. For
515several of these customization types, the customization widget
516provides inline completion with @kbd{C-M-i} or @kbd{M-@key{TAB}}.
b8d4c8d0
GM
517
518@table @code
519@item sexp
e1161b06
CY
520The value may be any Lisp object that can be printed and read back.
521You can use @code{sexp} as a fall-back for any option, if you don't
522want to take the time to work out a more specific type to use.
b8d4c8d0
GM
523
524@item integer
e1161b06 525The value must be an integer.
b8d4c8d0
GM
526
527@item number
e1161b06 528The value must be a number (floating point or integer).
b8d4c8d0
GM
529
530@item float
e1161b06 531The value must be a floating point number.
b8d4c8d0
GM
532
533@item string
e1161b06
CY
534The value must be a string. The customization buffer shows the string
535without delimiting @samp{"} characters or @samp{\} quotes.
b8d4c8d0
GM
536
537@item regexp
538Like @code{string} except that the string must be a valid regular
539expression.
540
541@item character
542The value must be a character code. A character code is actually an
543integer, but this type shows the value by inserting the character in the
544buffer, rather than by showing the number.
545
546@item file
e1161b06 547The value must be a file name. The widget provides completion.
b8d4c8d0
GM
548
549@item (file :must-match t)
e1161b06
CY
550The value must be a file name for an existing file. The widget
551provides completion.
b8d4c8d0
GM
552
553@item directory
e1161b06 554The value must be a directory name. The widget provides completion.
b8d4c8d0
GM
555
556@item hook
e1161b06
CY
557The value must be a list of functions. This customization type is
558used for hook variables. You can use the @code{:options} keyword in a
559hook variable's @code{defcustom} to specify a list of functions
560recommended for use in the hook; @xref{Variable Definitions}.
b8d4c8d0 561
3deead93
CY
562@item symbol
563The value must be a symbol. It appears in the customization buffer as
e1161b06 564the symbol name. The widget provides completion.
b8d4c8d0 565
3deead93 566@item function
e1161b06
CY
567The value must be either a lambda expression or a function name. The
568widget provides completion for function names.
b8d4c8d0 569
3deead93 570@item variable
e1161b06 571The value must be a variable name. The widget provides completion.
3deead93
CY
572
573@item face
e1161b06
CY
574The value must be a symbol which is a face name. The widget provides
575completion.
3deead93
CY
576
577@item boolean
578The value is boolean---either @code{nil} or @code{t}. Note that by
579using @code{choice} and @code{const} together (see the next section),
580you can specify that the value must be @code{nil} or @code{t}, but also
581specify the text to describe each value in a way that fits the specific
582meaning of the alternative.
583
584@item coding-system
585The value must be a coding-system name, and you can do completion with
586@kbd{M-@key{TAB}}.
587
588@item color
e1161b06
CY
589The value must be a valid color name. The widget provides completion
590for color names, as well as a sample and a button for selecting a
591color name from a list of color names shown in a @samp{*Colors*}
592buffer.
3deead93
CY
593@end table
594
595@node Composite Types
596@subsection Composite Types
597@cindex composite types (customization)
598
599 When none of the simple types is appropriate, you can use composite
600types, which build new types from other types or from specified data.
601The specified types or data are called the @dfn{arguments} of the
602composite type. The composite type normally looks like this:
603
604@example
605(@var{constructor} @var{arguments}@dots{})
606@end example
b8d4c8d0
GM
607
608@noindent
3deead93
CY
609but you can also add keyword-value pairs before the arguments, like
610this:
611
612@example
613(@var{constructor} @r{@{}@var{keyword} @var{value}@r{@}}@dots{} @var{arguments}@dots{})
614@end example
615
616 Here is a table of constructors and how to use them to write
617composite types:
618
619@table @code
620@item (cons @var{car-type} @var{cdr-type})
621The value must be a cons cell, its @sc{car} must fit @var{car-type}, and
622its @sc{cdr} must fit @var{cdr-type}. For example, @code{(cons string
623symbol)} is a customization type which matches values such as
624@code{("foo" . foo)}.
625
e1161b06
CY
626In the customization buffer, the @sc{car} and @sc{cdr} are displayed
627and edited separately, each according to their specified type.
3deead93
CY
628
629@item (list @var{element-types}@dots{})
630The value must be a list with exactly as many elements as the
631@var{element-types} given; and each element must fit the
632corresponding @var{element-type}.
633
634For example, @code{(list integer string function)} describes a list of
635three elements; the first element must be an integer, the second a
636string, and the third a function.
637
638In the customization buffer, each element is displayed and edited
639separately, according to the type specified for it.
640
641@item (group @var{element-types}@dots{})
642This works like @code{list} except for the formatting
643of text in the Custom buffer. @code{list} labels each
644element value with its tag; @code{group} does not.
645
646@item (vector @var{element-types}@dots{})
647Like @code{list} except that the value must be a vector instead of a
648list. The elements work the same as in @code{list}.
649
650@item (alist :key-type @var{key-type} :value-type @var{value-type})
651The value must be a list of cons-cells, the @sc{car} of each cell
652representing a key of customization type @var{key-type}, and the
653@sc{cdr} of the same cell representing a value of customization type
654@var{value-type}. The user can add and delete key/value pairs, and
655edit both the key and the value of each pair.
656
657If omitted, @var{key-type} and @var{value-type} default to
658@code{sexp}.
b8d4c8d0
GM
659
660The user can add any key matching the specified key type, but you can
661give some keys a preferential treatment by specifying them with the
662@code{:options} (see @ref{Variable Definitions}). The specified keys
663will always be shown in the customize buffer (together with a suitable
664value), with a checkbox to include or exclude or disable the key/value
665pair from the alist. The user will not be able to edit the keys
666specified by the @code{:options} keyword argument.
667
668The argument to the @code{:options} keywords should be a list of
669specifications for reasonable keys in the alist. Ordinarily, they are
e1161b06 670simply atoms, which stand for themselves. For example:
b8d4c8d0
GM
671
672@smallexample
673:options '("foo" "bar" "baz")
674@end smallexample
675
676@noindent
677specifies that there are three ``known'' keys, namely @code{"foo"},
678@code{"bar"} and @code{"baz"}, which will always be shown first.
679
680You may want to restrict the value type for specific keys, for
681example, the value associated with the @code{"bar"} key can only be an
682integer. You can specify this by using a list instead of an atom in
683the list. The first element will specify the key, like before, while
684the second element will specify the value type. For example:
685
686@smallexample
687:options '("foo" ("bar" integer) "baz")
688@end smallexample
689
690Finally, you may want to change how the key is presented. By default,
691the key is simply shown as a @code{const}, since the user cannot change
692the special keys specified with the @code{:options} keyword. However,
693you may want to use a more specialized type for presenting the key, like
694@code{function-item} if you know it is a symbol with a function binding.
695This is done by using a customization type specification instead of a
696symbol for the key.
697
698@smallexample
699:options '("foo" ((function-item some-function) integer)
700 "baz")
701@end smallexample
702
703Many alists use lists with two elements, instead of cons cells. For
704example,
705
706@smallexample
707(defcustom list-alist '(("foo" 1) ("bar" 2) ("baz" 3))
708 "Each element is a list of the form (KEY VALUE).")
709@end smallexample
710
711@noindent
712instead of
713
714@smallexample
715(defcustom cons-alist '(("foo" . 1) ("bar" . 2) ("baz" . 3))
716 "Each element is a cons-cell (KEY . VALUE).")
717@end smallexample
718
719Because of the way lists are implemented on top of cons cells, you can
720treat @code{list-alist} in the example above as a cons cell alist, where
721the value type is a list with a single element containing the real
722value.
723
724@smallexample
725(defcustom list-alist '(("foo" 1) ("bar" 2) ("baz" 3))
726 "Each element is a list of the form (KEY VALUE)."
727 :type '(alist :value-type (group integer)))
728@end smallexample
729
730The @code{group} widget is used here instead of @code{list} only because
731the formatting is better suited for the purpose.
732
733Similarly, you can have alists with more values associated with each
734key, using variations of this trick:
735
736@smallexample
737(defcustom person-data '(("brian" 50 t)
738 ("dorith" 55 nil)
739 ("ken" 52 t))
740 "Alist of basic info about people.
741Each element has the form (NAME AGE MALE-FLAG)."
742 :type '(alist :value-type (group integer boolean)))
b8d4c8d0
GM
743@end smallexample
744
3deead93
CY
745@item (plist :key-type @var{key-type} :value-type @var{value-type})
746This customization type is similar to @code{alist} (see above), except
747that (i) the information is stored as a property list,
748(@pxref{Property Lists}), and (ii) @var{key-type}, if omitted,
749defaults to @code{symbol} rather than @code{sexp}.
b8d4c8d0
GM
750
751@item (choice @var{alternative-types}@dots{})
e1161b06
CY
752The value must fit one of @var{alternative-types}. For example,
753@code{(choice integer string)} allows either an integer or a string.
b8d4c8d0
GM
754
755In the customization buffer, the user selects an alternative
756using a menu, and can then edit the value in the usual way for that
757alternative.
758
759Normally the strings in this menu are determined automatically from the
760choices; however, you can specify different strings for the menu by
761including the @code{:tag} keyword in the alternatives. For example, if
762an integer stands for a number of spaces, while a string is text to use
763verbatim, you might write the customization type this way,
764
765@example
766(choice (integer :tag "Number of spaces")
767 (string :tag "Literal text"))
768@end example
769
770@noindent
771so that the menu offers @samp{Number of spaces} and @samp{Literal text}.
772
773In any alternative for which @code{nil} is not a valid value, other than
774a @code{const}, you should specify a valid default for that alternative
775using the @code{:value} keyword. @xref{Type Keywords}.
776
777If some values are covered by more than one of the alternatives,
778customize will choose the first alternative that the value fits. This
779means you should always list the most specific types first, and the
780most general last. Here's an example of proper usage:
781
782@example
783(choice (const :tag "Off" nil)
784 symbol (sexp :tag "Other"))
785@end example
786
787@noindent
788This way, the special value @code{nil} is not treated like other
789symbols, and symbols are not treated like other Lisp expressions.
790
791@item (radio @var{element-types}@dots{})
792This is similar to @code{choice}, except that the choices are displayed
793using `radio buttons' rather than a menu. This has the advantage of
794displaying documentation for the choices when applicable and so is often
795a good choice for a choice between constant functions
796(@code{function-item} customization types).
797
798@item (const @var{value})
799The value must be @var{value}---nothing else is allowed.
800
801The main use of @code{const} is inside of @code{choice}. For example,
802@code{(choice integer (const nil))} allows either an integer or
803@code{nil}.
804
805@code{:tag} is often used with @code{const}, inside of @code{choice}.
806For example,
807
808@example
809(choice (const :tag "Yes" t)
810 (const :tag "No" nil)
811 (const :tag "Ask" foo))
812@end example
813
814@noindent
815describes a variable for which @code{t} means yes, @code{nil} means no,
816and @code{foo} means ``ask.''
817
818@item (other @var{value})
819This alternative can match any Lisp value, but if the user chooses this
820alternative, that selects the value @var{value}.
821
822The main use of @code{other} is as the last element of @code{choice}.
823For example,
824
825@example
826(choice (const :tag "Yes" t)
827 (const :tag "No" nil)
828 (other :tag "Ask" foo))
829@end example
830
831@noindent
832describes a variable for which @code{t} means yes, @code{nil} means no,
833and anything else means ``ask.'' If the user chooses @samp{Ask} from
834the menu of alternatives, that specifies the value @code{foo}; but any
835other value (not @code{t}, @code{nil} or @code{foo}) displays as
836@samp{Ask}, just like @code{foo}.
837
838@item (function-item @var{function})
839Like @code{const}, but used for values which are functions. This
840displays the documentation string as well as the function name.
841The documentation string is either the one you specify with
842@code{:doc}, or @var{function}'s own documentation string.
843
844@item (variable-item @var{variable})
845Like @code{const}, but used for values which are variable names. This
846displays the documentation string as well as the variable name. The
847documentation string is either the one you specify with @code{:doc}, or
848@var{variable}'s own documentation string.
849
850@item (set @var{types}@dots{})
851The value must be a list, and each element of the list must match one of
852the @var{types} specified.
853
854This appears in the customization buffer as a checklist, so that each of
855@var{types} may have either one corresponding element or none. It is
856not possible to specify two different elements that match the same one
857of @var{types}. For example, @code{(set integer symbol)} allows one
858integer and/or one symbol in the list; it does not allow multiple
859integers or multiple symbols. As a result, it is rare to use
860nonspecific types such as @code{integer} in a @code{set}.
861
862Most often, the @var{types} in a @code{set} are @code{const} types, as
863shown here:
864
865@example
866(set (const :bold) (const :italic))
867@end example
868
869Sometimes they describe possible elements in an alist:
870
871@example
872(set (cons :tag "Height" (const height) integer)
873 (cons :tag "Width" (const width) integer))
874@end example
875
876@noindent
877That lets the user specify a height value optionally
878and a width value optionally.
879
880@item (repeat @var{element-type})
881The value must be a list and each element of the list must fit the type
882@var{element-type}. This appears in the customization buffer as a
883list of elements, with @samp{[INS]} and @samp{[DEL]} buttons for adding
884more elements or removing elements.
885
886@item (restricted-sexp :match-alternatives @var{criteria})
887This is the most general composite type construct. The value may be
888any Lisp object that satisfies one of @var{criteria}. @var{criteria}
889should be a list, and each element should be one of these
890possibilities:
891
892@itemize @bullet
893@item
894A predicate---that is, a function of one argument that has no side
895effects, and returns either @code{nil} or non-@code{nil} according to
896the argument. Using a predicate in the list says that objects for which
897the predicate returns non-@code{nil} are acceptable.
898
899@item
900A quoted constant---that is, @code{'@var{object}}. This sort of element
901in the list says that @var{object} itself is an acceptable value.
902@end itemize
903
904For example,
905
906@example
907(restricted-sexp :match-alternatives
908 (integerp 't 'nil))
909@end example
910
911@noindent
912allows integers, @code{t} and @code{nil} as legitimate values.
913
914The customization buffer shows all legitimate values using their read
915syntax, and the user edits them textually.
916@end table
917
918 Here is a table of the keywords you can use in keyword-value pairs
919in a composite type:
920
921@table @code
922@item :tag @var{tag}
923Use @var{tag} as the name of this alternative, for user communication
924purposes. This is useful for a type that appears inside of a
925@code{choice}.
926
927@item :match-alternatives @var{criteria}
928@kindex match-alternatives@r{, customization keyword}
929Use @var{criteria} to match possible values. This is used only in
930@code{restricted-sexp}.
931
932@item :args @var{argument-list}
933@kindex args@r{, customization keyword}
934Use the elements of @var{argument-list} as the arguments of the type
935construct. For instance, @code{(const :args (foo))} is equivalent to
936@code{(const foo)}. You rarely need to write @code{:args} explicitly,
937because normally the arguments are recognized automatically as
938whatever follows the last keyword-value pair.
939@end table
940
941@node Splicing into Lists
942@subsection Splicing into Lists
943
944 The @code{:inline} feature lets you splice a variable number of
e1161b06
CY
945elements into the middle of a @code{list} or @code{vector}
946customization type. You use it by adding @code{:inline t} to a type
947specification which is contained in a @code{list} or @code{vector}
948specification.
949
950 Normally, each entry in a @code{list} or @code{vector} type
951specification describes a single element type. But when an entry
952contains @code{:inline t}, the value it matches is merged directly
953into the containing sequence. For example, if the entry matches a
954list with three elements, those become three elements of the overall
955sequence. This is analogous to @samp{,@@} in a backquote construct
956(@pxref{Backquote}).
b8d4c8d0
GM
957
958 For example, to specify a list whose first element must be @code{baz}
959and whose remaining arguments should be zero or more of @code{foo} and
960@code{bar}, use this customization type:
961
962@example
963(list (const baz) (set :inline t (const foo) (const bar)))
964@end example
965
966@noindent
967This matches values such as @code{(baz)}, @code{(baz foo)}, @code{(baz bar)}
968and @code{(baz foo bar)}.
969
970 When the element-type is a @code{choice}, you use @code{:inline} not
971in the @code{choice} itself, but in (some of) the alternatives of the
972@code{choice}. For example, to match a list which must start with a
973file name, followed either by the symbol @code{t} or two strings, use
974this customization type:
975
976@example
977(list file
978 (choice (const t)
979 (list :inline t string string)))
980@end example
981
982@noindent
983If the user chooses the first alternative in the choice, then the
984overall list has two elements and the second element is @code{t}. If
985the user chooses the second alternative, then the overall list has three
986elements and the second and third must be strings.
987
988@node Type Keywords
989@subsection Type Keywords
990
991You can specify keyword-argument pairs in a customization type after the
992type name symbol. Here are the keywords you can use, and their
993meanings:
994
995@table @code
996@item :value @var{default}
0ec389b9
LMI
997Provide a default value.
998
999If @code{nil} is not a valid value for the alternative, then it is
1000essential to specify a valid default with @code{:value}.
1001
1002If you use this for a type that appears as an alternative inside of
b8d4c8d0
GM
1003@code{choice}; it specifies the default value to use, at first, if and
1004when the user selects this alternative with the menu in the
1005customization buffer.
1006
1007Of course, if the actual value of the option fits this alternative, it
1008will appear showing the actual value, not @var{default}.
1009
b8d4c8d0
GM
1010@item :format @var{format-string}
1011@kindex format@r{, customization keyword}
1012This string will be inserted in the buffer to represent the value
1013corresponding to the type. The following @samp{%} escapes are available
1014for use in @var{format-string}:
1015
1016@table @samp
1017@item %[@var{button}%]
1018Display the text @var{button} marked as a button. The @code{:action}
1019attribute specifies what the button will do if the user invokes it;
1020its value is a function which takes two arguments---the widget which
1021the button appears in, and the event.
1022
1023There is no way to specify two different buttons with different
1024actions.
1025
1026@item %@{@var{sample}%@}
1027Show @var{sample} in a special face specified by @code{:sample-face}.
1028
1029@item %v
1030Substitute the item's value. How the value is represented depends on
1031the kind of item, and (for variables) on the customization type.
1032
1033@item %d
1034Substitute the item's documentation string.
1035
1036@item %h
1037Like @samp{%d}, but if the documentation string is more than one line,
1038add an active field to control whether to show all of it or just the
1039first line.
1040
1041@item %t
1042Substitute the tag here. You specify the tag with the @code{:tag}
1043keyword.
1044
1045@item %%
1046Display a literal @samp{%}.
1047@end table
1048
1049@item :action @var{action}
1050@kindex action@r{, customization keyword}
1051Perform @var{action} if the user clicks on a button.
1052
1053@item :button-face @var{face}
1054@kindex button-face@r{, customization keyword}
1055Use the face @var{face} (a face name or a list of face names) for button
1056text displayed with @samp{%[@dots{}%]}.
1057
1058@item :button-prefix @var{prefix}
1059@itemx :button-suffix @var{suffix}
1060@kindex button-prefix@r{, customization keyword}
1061@kindex button-suffix@r{, customization keyword}
1062These specify the text to display before and after a button.
1063Each can be:
1064
1065@table @asis
1066@item @code{nil}
1067No text is inserted.
1068
1069@item a string
1070The string is inserted literally.
1071
1072@item a symbol
1073The symbol's value is used.
1074@end table
1075
1076@item :tag @var{tag}
1077Use @var{tag} (a string) as the tag for the value (or part of the value)
1078that corresponds to this type.
1079
1080@item :doc @var{doc}
1081@kindex doc@r{, customization keyword}
1082Use @var{doc} as the documentation string for this value (or part of the
1083value) that corresponds to this type. In order for this to work, you
1084must specify a value for @code{:format}, and use @samp{%d} or @samp{%h}
1085in that value.
1086
1087The usual reason to specify a documentation string for a type is to
1088provide more information about the meanings of alternatives inside a
1089@code{:choice} type or the parts of some other composite type.
1090
1091@item :help-echo @var{motion-doc}
1092@kindex help-echo@r{, customization keyword}
1093When you move to this item with @code{widget-forward} or
1094@code{widget-backward}, it will display the string @var{motion-doc} in
1095the echo area. In addition, @var{motion-doc} is used as the mouse
1096@code{help-echo} string and may actually be a function or form evaluated
1097to yield a help string. If it is a function, it is called with one
1098argument, the widget.
1099
1100@item :match @var{function}
1101@kindex match@r{, customization keyword}
1102Specify how to decide whether a value matches the type. The
1103corresponding value, @var{function}, should be a function that accepts
1104two arguments, a widget and a value; it should return non-@code{nil} if
1105the value is acceptable.
1106
72b7e664
RS
1107@item :validate @var{function}
1108Specify a validation function for input. @var{function} takes a
1109widget as an argument, and should return @code{nil} if the widget's
1110current value is valid for the widget. Otherwise, it should return
1111the widget containing the invalid data, and set that widget's
1112@code{:error} property to a string explaining the error.
1113
b8d4c8d0
GM
1114@ignore
1115@item :indent @var{columns}
1116Indent this item by @var{columns} columns. The indentation is used for
1117@samp{%n}, and automatically for group names, for checklists and radio
1118buttons, and for editable lists. It affects the whole of the
1119item except for the first line.
1120
72b7e664
RS
1121@item :offset @var{extra}
1122Indent the subitems of this item @var{extra} columns more than this
1123item itself. By default, subitems are indented the same as their
1124parent.
b8d4c8d0 1125
72b7e664
RS
1126@item :extra-offset @var{n}
1127Add @var{n} extra spaces to this item's indentation, compared to its
1128parent's indentation.
b8d4c8d0 1129
72b7e664
RS
1130@item :notify @var{function}
1131Call @var{function} each time the item or a subitem is changed. The
1132function gets two or three arguments. The first argument is the item
1133itself, the second argument is the item that was changed, and the
1134third argument is the event leading to the change, if any.
b8d4c8d0 1135
72b7e664
RS
1136@item :menu-tag @var{tag-string}
1137Use @var{tag-string} in the menu when the widget is used as an option
1138in a @code{menu-choice} widget.
b8d4c8d0
GM
1139
1140@item :menu-tag-get
1141A function used for finding the tag when the widget is used as an option
1142in a @code{menu-choice} widget. By default, the tag used will be either the
1143@code{:menu-tag} or @code{:tag} property if present, or the @code{princ}
1144representation of the @code{:value} property if not.
1145
b8d4c8d0
GM
1146@item :tab-order
1147Specify the order in which widgets are traversed with
1148@code{widget-forward} or @code{widget-backward}. This is only partially
1149implemented.
1150
1151@enumerate a
1152@item
1153Widgets with tabbing order @code{-1} are ignored.
1154
1155@item
1156(Unimplemented) When on a widget with tabbing order @var{n}, go to the
1157next widget in the buffer with tabbing order @var{n+1} or @code{nil},
1158whichever comes first.
1159
1160@item
1161When on a widget with no tabbing order specified, go to the next widget
1162in the buffer with a positive tabbing order, or @code{nil}
1163@end enumerate
1164
1165@item :parent
1166The parent of a nested widget (e.g., a @code{menu-choice} item or an
1167element of a @code{editable-list} widget).
1168
1169@item :sibling-args
1170This keyword is only used for members of a @code{radio-button-choice} or
1171@code{checklist}. The value should be a list of extra keyword
1172arguments, which will be used when creating the @code{radio-button} or
1173@code{checkbox} associated with this item.
1174@end ignore
1175@end table
1176
1177@node Defining New Types
1178@subsection Defining New Types
1179
1180In the previous sections we have described how to construct elaborate
1181type specifications for @code{defcustom}. In some cases you may want
1182to give such a type specification a name. The obvious case is when
1183you are using the same type for many user options: rather than repeat
1184the specification for each option, you can give the type specification
1185a name, and use that name each @code{defcustom}. The other case is
1186when a user option's value is a recursive data structure. To make it
1187possible for a datatype to refer to itself, it needs to have a name.
1188
1189Since custom types are implemented as widgets, the way to define a new
1190customize type is to define a new widget. We are not going to describe
1191the widget interface here in details, see @ref{Top, , Introduction,
1192widget, The Emacs Widget Library}, for that. Instead we are going to
1193demonstrate the minimal functionality needed for defining new customize
1194types by a simple example.
1195
1196@example
1197(define-widget 'binary-tree-of-string 'lazy
1198 "A binary tree made of cons-cells and strings."
1199 :offset 4
1200 :tag "Node"
1201 :type '(choice (string :tag "Leaf" :value "")
1202 (cons :tag "Interior"
1203 :value ("" . "")
1204 binary-tree-of-string
1205 binary-tree-of-string)))
1206
1207(defcustom foo-bar ""
1208 "Sample variable holding a binary tree of strings."
1209 :type 'binary-tree-of-string)
1210@end example
1211
1212The function to define a new widget is called @code{define-widget}. The
1213first argument is the symbol we want to make a new widget type. The
1214second argument is a symbol representing an existing widget, the new
1215widget is going to be defined in terms of difference from the existing
1216widget. For the purpose of defining new customization types, the
1217@code{lazy} widget is perfect, because it accepts a @code{:type} keyword
1218argument with the same syntax as the keyword argument to
1219@code{defcustom} with the same name. The third argument is a
1220documentation string for the new widget. You will be able to see that
1221string with the @kbd{M-x widget-browse @key{RET} binary-tree-of-string
1222@key{RET}} command.
1223
1224After these mandatory arguments follow the keyword arguments. The most
1225important is @code{:type}, which describes the data type we want to match
1226with this widget. Here a @code{binary-tree-of-string} is described as
1227being either a string, or a cons-cell whose car and cdr are themselves
1228both @code{binary-tree-of-string}. Note the reference to the widget
1229type we are currently in the process of defining. The @code{:tag}
1230attribute is a string to name the widget in the user interface, and the
1231@code{:offset} argument is there to ensure that child nodes are
1232indented four spaces relative to the parent node, making the tree
1233structure apparent in the customization buffer.
1234
1235The @code{defcustom} shows how the new widget can be used as an ordinary
1236customization type.
1237
1238The reason for the name @code{lazy} is that the other composite
1239widgets convert their inferior widgets to internal form when the
1240widget is instantiated in a buffer. This conversion is recursive, so
1241the inferior widgets will convert @emph{their} inferior widgets. If
1242the data structure is itself recursive, this conversion is an infinite
1243recursion. The @code{lazy} widget prevents the recursion: it convert
1244its @code{:type} argument only when needed.