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