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