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