(M-40132'): Unify GT-53970.
[chise/xemacs-chise.git-] / info / lispref.info-11
1 This is ../info/lispref.info, produced by makeinfo version 4.0b from
2 lispref/lispref.texi.
3
4 INFO-DIR-SECTION XEmacs Editor
5 START-INFO-DIR-ENTRY
6 * Lispref: (lispref).           XEmacs Lisp Reference Manual.
7 END-INFO-DIR-ENTRY
8
9    Edition History:
10
11    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
12 Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
13 Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
14 XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
15 GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
16 Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
17 Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
18 Reference Manual (for 19.15 and 20.1, 20.2, 20.3) v3.2, April, May,
19 November 1997 XEmacs Lisp Reference Manual (for 21.0) v3.3, April 1998
20
21    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
22 Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
23 Copyright (C) 1995, 1996 Ben Wing.
24
25    Permission is granted to make and distribute verbatim copies of this
26 manual provided the copyright notice and this permission notice are
27 preserved on all copies.
28
29    Permission is granted to copy and distribute modified versions of
30 this manual under the conditions for verbatim copying, provided that the
31 entire resulting derived work is distributed under the terms of a
32 permission notice identical to this one.
33
34    Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that this permission notice may be stated in a
37 translation approved by the Foundation.
38
39    Permission is granted to copy and distribute modified versions of
40 this manual under the conditions for verbatim copying, provided also
41 that the section entitled "GNU General Public License" is included
42 exactly as in the original, and provided that the entire resulting
43 derived work is distributed under the terms of a permission notice
44 identical to this one.
45
46    Permission is granted to copy and distribute translations of this
47 manual into another language, under the above conditions for modified
48 versions, except that the section entitled "GNU General Public License"
49 may be included in a translation approved by the Free Software
50 Foundation instead of in the original English.
51
52 \1f
53 File: lispref.info,  Node: Inline Functions,  Next: Related Topics,  Prev: Function Cells,  Up: Functions
54
55 Inline Functions
56 ================
57
58    You can define an "inline function" by using `defsubst' instead of
59 `defun'.  An inline function works just like an ordinary function
60 except for one thing: when you compile a call to the function, the
61 function's definition is open-coded into the caller.
62
63    Making a function inline makes explicit calls run faster.  But it
64 also has disadvantages.  For one thing, it reduces flexibility; if you
65 change the definition of the function, calls already inlined still use
66 the old definition until you recompile them.  Since the flexibility of
67 redefining functions is an important feature of XEmacs, you should not
68 make a function inline unless its speed is really crucial.
69
70    Another disadvantage is that making a large function inline can
71 increase the size of compiled code both in files and in memory.  Since
72 the speed advantage of inline functions is greatest for small
73 functions, you generally should not make large functions inline.
74
75    It's possible to define a macro to expand into the same code that an
76 inline function would execute.  But the macro would have a limitation:
77 you can use it only explicitly--a macro cannot be called with `apply',
78 `mapcar' and so on.  Also, it takes some work to convert an ordinary
79 function into a macro.  (*Note Macros::.)  To convert it into an inline
80 function is very easy; simply replace `defun' with `defsubst'.  Since
81 each argument of an inline function is evaluated exactly once, you
82 needn't worry about how many times the body uses the arguments, as you
83 do for macros.  (*Note Argument Evaluation::.)
84
85    Inline functions can be used and open-coded later on in the same
86 file, following the definition, just like macros.
87
88 \1f
89 File: lispref.info,  Node: Related Topics,  Prev: Inline Functions,  Up: Functions
90
91 Other Topics Related to Functions
92 =================================
93
94    Here is a table of several functions that do things related to
95 function calling and function definitions.  They are documented
96 elsewhere, but we provide cross references here.
97
98 `apply'
99      See *Note Calling Functions::.
100
101 `autoload'
102      See *Note Autoload::.
103
104 `call-interactively'
105      See *Note Interactive Call::.
106
107 `commandp'
108      See *Note Interactive Call::.
109
110 `documentation'
111      See *Note Accessing Documentation::.
112
113 `eval'
114      See *Note Eval::.
115
116 `funcall'
117      See *Note Calling Functions::.
118
119 `ignore'
120      See *Note Calling Functions::.
121
122 `indirect-function'
123      See *Note Function Indirection::.
124
125 `interactive'
126      See *Note Using Interactive::.
127
128 `interactive-p'
129      See *Note Interactive Call::.
130
131 `mapatoms'
132      See *Note Creating Symbols::.
133
134 `mapcar'
135      See *Note Mapping Functions::.
136
137 `mapconcat'
138      See *Note Mapping Functions::.
139
140 `undefined'
141      See *Note Key Lookup::.
142
143 \1f
144 File: lispref.info,  Node: Macros,  Next: Loading,  Prev: Functions,  Up: Top
145
146 Macros
147 ******
148
149    "Macros" enable you to define new control constructs and other
150 language features.  A macro is defined much like a function, but instead
151 of telling how to compute a value, it tells how to compute another Lisp
152 expression which will in turn compute the value.  We call this
153 expression the "expansion" of the macro.
154
155    Macros can do this because they operate on the unevaluated
156 expressions for the arguments, not on the argument values as functions
157 do.  They can therefore construct an expansion containing these
158 argument expressions or parts of them.
159
160    If you are using a macro to do something an ordinary function could
161 do, just for the sake of speed, consider using an inline function
162 instead.  *Note Inline Functions::.
163
164 * Menu:
165
166 * Simple Macro::            A basic example.
167 * Expansion::               How, when and why macros are expanded.
168 * Compiling Macros::        How macros are expanded by the compiler.
169 * Defining Macros::         How to write a macro definition.
170 * Backquote::               Easier construction of list structure.
171 * Problems with Macros::    Don't evaluate the macro arguments too many times.
172                               Don't hide the user's variables.
173
174 \1f
175 File: lispref.info,  Node: Simple Macro,  Next: Expansion,  Up: Macros
176
177 A Simple Example of a Macro
178 ===========================
179
180    Suppose we would like to define a Lisp construct to increment a
181 variable value, much like the `++' operator in C.  We would like to
182 write `(inc x)' and have the effect of `(setq x (1+ x))'.  Here's a
183 macro definition that does the job:
184
185      (defmacro inc (var)
186         (list 'setq var (list '1+ var)))
187
188    When this is called with `(inc x)', the argument `var' has the value
189 `x'--_not_ the _value_ of `x'.  The body of the macro uses this to
190 construct the expansion, which is `(setq x (1+ x))'.  Once the macro
191 definition returns this expansion, Lisp proceeds to evaluate it, thus
192 incrementing `x'.
193
194 \1f
195 File: lispref.info,  Node: Expansion,  Next: Compiling Macros,  Prev: Simple Macro,  Up: Macros
196
197 Expansion of a Macro Call
198 =========================
199
200    A macro call looks just like a function call in that it is a list
201 which starts with the name of the macro.  The rest of the elements of
202 the list are the arguments of the macro.
203
204    Evaluation of the macro call begins like evaluation of a function
205 call except for one crucial difference: the macro arguments are the
206 actual expressions appearing in the macro call.  They are not evaluated
207 before they are given to the macro definition.  By contrast, the
208 arguments of a function are results of evaluating the elements of the
209 function call list.
210
211    Having obtained the arguments, Lisp invokes the macro definition just
212 as a function is invoked.  The argument variables of the macro are bound
213 to the argument values from the macro call, or to a list of them in the
214 case of a `&rest' argument.  And the macro body executes and returns
215 its value just as a function body does.
216
217    The second crucial difference between macros and functions is that
218 the value returned by the macro body is not the value of the macro call.
219 Instead, it is an alternate expression for computing that value, also
220 known as the "expansion" of the macro.  The Lisp interpreter proceeds
221 to evaluate the expansion as soon as it comes back from the macro.
222
223    Since the expansion is evaluated in the normal manner, it may contain
224 calls to other macros.  It may even be a call to the same macro, though
225 this is unusual.
226
227    You can see the expansion of a given macro call by calling
228 `macroexpand'.
229
230  - Function: macroexpand form &optional environment
231      This function expands FORM, if it is a macro call.  If the result
232      is another macro call, it is expanded in turn, until something
233      which is not a macro call results.  That is the value returned by
234      `macroexpand'.  If FORM is not a macro call to begin with, it is
235      returned as given.
236
237      Note that `macroexpand' does not look at the subexpressions of
238      FORM (although some macro definitions may do so).  Even if they
239      are macro calls themselves, `macroexpand' does not expand them.
240
241      The function `macroexpand' does not expand calls to inline
242      functions.  Normally there is no need for that, since a call to an
243      inline function is no harder to understand than a call to an
244      ordinary function.
245
246      If ENVIRONMENT is provided, it specifies an alist of macro
247      definitions that shadow the currently defined macros.  Byte
248      compilation uses this feature.
249
250           (defmacro inc (var)
251               (list 'setq var (list '1+ var)))
252                => inc
253           
254           (macroexpand '(inc r))
255                => (setq r (1+ r))
256           
257           (defmacro inc2 (var1 var2)
258               (list 'progn (list 'inc var1) (list 'inc var2)))
259                => inc2
260           
261           (macroexpand '(inc2 r s))
262                => (progn (inc r) (inc s))  ; `inc' not expanded here.
263
264 \1f
265 File: lispref.info,  Node: Compiling Macros,  Next: Defining Macros,  Prev: Expansion,  Up: Macros
266
267 Macros and Byte Compilation
268 ===========================
269
270    You might ask why we take the trouble to compute an expansion for a
271 macro and then evaluate the expansion.  Why not have the macro body
272 produce the desired results directly?  The reason has to do with
273 compilation.
274
275    When a macro call appears in a Lisp program being compiled, the Lisp
276 compiler calls the macro definition just as the interpreter would, and
277 receives an expansion.  But instead of evaluating this expansion, it
278 compiles the expansion as if it had appeared directly in the program.
279 As a result, the compiled code produces the value and side effects
280 intended for the macro, but executes at full compiled speed.  This would
281 not work if the macro body computed the value and side effects
282 itself--they would be computed at compile time, which is not useful.
283
284    In order for compilation of macro calls to work, the macros must be
285 defined in Lisp when the calls to them are compiled.  The compiler has a
286 special feature to help you do this: if a file being compiled contains a
287 `defmacro' form, the macro is defined temporarily for the rest of the
288 compilation of that file.  To use this feature, you must define the
289 macro in the same file where it is used and before its first use.
290
291    Byte-compiling a file executes any `require' calls at top-level in
292 the file.  This is in case the file needs the required packages for
293 proper compilation.  One way to ensure that necessary macro definitions
294 are available during compilation is to require the files that define
295 them (*note Named Features::).  To avoid loading the macro definition
296 files when someone _runs_ the compiled program, write
297 `eval-when-compile' around the `require' calls (*note Eval During
298 Compile::).
299
300 \1f
301 File: lispref.info,  Node: Defining Macros,  Next: Backquote,  Prev: Compiling Macros,  Up: Macros
302
303 Defining Macros
304 ===============
305
306    A Lisp macro is a list whose CAR is `macro'.  Its CDR should be a
307 function; expansion of the macro works by applying the function (with
308 `apply') to the list of unevaluated argument-expressions from the macro
309 call.
310
311    It is possible to use an anonymous Lisp macro just like an anonymous
312 function, but this is never done, because it does not make sense to pass
313 an anonymous macro to functionals such as `mapcar'.  In practice, all
314 Lisp macros have names, and they are usually defined with the special
315 form `defmacro'.
316
317  - Special Form: defmacro name argument-list body-forms...
318      `defmacro' defines the symbol NAME as a macro that looks like this:
319
320           (macro lambda ARGUMENT-LIST . BODY-FORMS)
321
322      This macro object is stored in the function cell of NAME.  The
323      value returned by evaluating the `defmacro' form is NAME, but
324      usually we ignore this value.
325
326      The shape and meaning of ARGUMENT-LIST is the same as in a
327      function, and the keywords `&rest' and `&optional' may be used
328      (*note Argument List::).  Macros may have a documentation string,
329      but any `interactive' declaration is ignored since macros cannot be
330      called interactively.
331
332 \1f
333 File: lispref.info,  Node: Backquote,  Next: Problems with Macros,  Prev: Defining Macros,  Up: Macros
334
335 Backquote
336 =========
337
338    Macros often need to construct large list structures from a mixture
339 of constants and nonconstant parts.  To make this easier, use the macro
340 ``' (often called "backquote").
341
342    Backquote allows you to quote a list, but selectively evaluate
343 elements of that list.  In the simplest case, it is identical to the
344 special form `quote' (*note Quoting::).  For example, these two forms
345 yield identical results:
346
347      `(a list of (+ 2 3) elements)
348           => (a list of (+ 2 3) elements)
349      '(a list of (+ 2 3) elements)
350           => (a list of (+ 2 3) elements)
351
352    The special marker `,' inside of the argument to backquote indicates
353 a value that isn't constant.  Backquote evaluates the argument of `,'
354 and puts the value in the list structure:
355
356      (list 'a 'list 'of (+ 2 3) 'elements)
357           => (a list of 5 elements)
358      `(a list of ,(+ 2 3) elements)
359           => (a list of 5 elements)
360
361    You can also "splice" an evaluated value into the resulting list,
362 using the special marker `,@'.  The elements of the spliced list become
363 elements at the same level as the other elements of the resulting list.
364 The equivalent code without using ``' is often unreadable.  Here are
365 some examples:
366
367      (setq some-list '(2 3))
368           => (2 3)
369      (cons 1 (append some-list '(4) some-list))
370           => (1 2 3 4 2 3)
371      `(1 ,@some-list 4 ,@some-list)
372           => (1 2 3 4 2 3)
373      
374      (setq list '(hack foo bar))
375           => (hack foo bar)
376      (cons 'use
377        (cons 'the
378          (cons 'words (append (cdr list) '(as elements)))))
379           => (use the words foo bar as elements)
380      `(use the words ,@(cdr list) as elements)
381           => (use the words foo bar as elements)
382
383      In older versions of Emacs (before XEmacs 19.12 or FSF Emacs
384      version 19.29), ``' used a different syntax which required an
385      extra level of parentheses around the entire backquote construct.
386      Likewise, each `,' or `,@' substitution required an extra level of
387      parentheses surrounding both the `,' or `,@' and the following
388      expression.  The old syntax required whitespace between the ``',
389      `,' or `,@' and the following expression.
390
391      This syntax is still accepted, but no longer recommended except for
392      compatibility with old Emacs versions.
393
394 \1f
395 File: lispref.info,  Node: Problems with Macros,  Prev: Backquote,  Up: Macros
396
397 Common Problems Using Macros
398 ============================
399
400    The basic facts of macro expansion have counterintuitive
401 consequences.  This section describes some important consequences that
402 can lead to trouble, and rules to follow to avoid trouble.
403
404 * Menu:
405
406 * Argument Evaluation::    The expansion should evaluate each macro arg once.
407 * Surprising Local Vars::  Local variable bindings in the expansion
408                               require special care.
409 * Eval During Expansion::  Don't evaluate them; put them in the expansion.
410 * Repeated Expansion::     Avoid depending on how many times expansion is done.
411
412 \1f
413 File: lispref.info,  Node: Argument Evaluation,  Next: Surprising Local Vars,  Up: Problems with Macros
414
415 Evaluating Macro Arguments Repeatedly
416 -------------------------------------
417
418    When defining a macro you must pay attention to the number of times
419 the arguments will be evaluated when the expansion is executed.  The
420 following macro (used to facilitate iteration) illustrates the problem.
421 This macro allows us to write a simple "for" loop such as one might
422 find in Pascal.
423
424      (defmacro for (var from init to final do &rest body)
425        "Execute a simple \"for\" loop.
426      For example, (for i from 1 to 10 do (print i))."
427        (list 'let (list (list var init))
428              (cons 'while (cons (list '<= var final)
429                                 (append body (list (list 'inc var)))))))
430      => for
431      
432      (for i from 1 to 3 do
433         (setq square (* i i))
434         (princ (format "\n%d %d" i square)))
435      ==>
436      (let ((i 1))
437        (while (<= i 3)
438          (setq square (* i i))
439          (princ (format "%d      %d" i square))
440          (inc i)))
441      
442           -|1       1
443           -|2       4
444           -|3       9
445      => nil
446
447 (The arguments `from', `to', and `do' in this macro are "syntactic
448 sugar"; they are entirely ignored.  The idea is that you will write
449 noise words (such as `from', `to', and `do') in those positions in the
450 macro call.)
451
452    Here's an equivalent definition simplified through use of backquote:
453
454      (defmacro for (var from init to final do &rest body)
455        "Execute a simple \"for\" loop.
456      For example, (for i from 1 to 10 do (print i))."
457        `(let ((,var ,init))
458           (while (<= ,var ,final)
459             ,@body
460             (inc ,var))))
461
462    Both forms of this definition (with backquote and without) suffer
463 from the defect that FINAL is evaluated on every iteration.  If FINAL
464 is a constant, this is not a problem.  If it is a more complex form,
465 say `(long-complex-calculation x)', this can slow down the execution
466 significantly.  If FINAL has side effects, executing it more than once
467 is probably incorrect.
468
469    A well-designed macro definition takes steps to avoid this problem by
470 producing an expansion that evaluates the argument expressions exactly
471 once unless repeated evaluation is part of the intended purpose of the
472 macro.  Here is a correct expansion for the `for' macro:
473
474      (let ((i 1)
475            (max 3))
476        (while (<= i max)
477          (setq square (* i i))
478          (princ (format "%d      %d" i square))
479          (inc i)))
480
481    Here is a macro definition that creates this expansion:
482
483      (defmacro for (var from init to final do &rest body)
484        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
485        `(let ((,var ,init)
486               (max ,final))
487           (while (<= ,var max)
488             ,@body
489             (inc ,var))))
490
491    Unfortunately, this introduces another problem.  Proceed to the
492 following node.
493
494 \1f
495 File: lispref.info,  Node: Surprising Local Vars,  Next: Eval During Expansion,  Prev: Argument Evaluation,  Up: Problems with Macros
496
497 Local Variables in Macro Expansions
498 -----------------------------------
499
500    In the previous section, the definition of `for' was fixed as
501 follows to make the expansion evaluate the macro arguments the proper
502 number of times:
503
504      (defmacro for (var from init to final do &rest body)
505        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
506        `(let ((,var ,init)
507               (max ,final))
508           (while (<= ,var max)
509             ,@body
510             (inc ,var))))
511
512    The new definition of `for' has a new problem: it introduces a local
513 variable named `max' which the user does not expect.  This causes
514 trouble in examples such as the following:
515
516      (let ((max 0))
517        (for x from 0 to 10 do
518          (let ((this (frob x)))
519            (if (< max this)
520                (setq max this)))))
521
522 The references to `max' inside the body of the `for', which are
523 supposed to refer to the user's binding of `max', really access the
524 binding made by `for'.
525
526    The way to correct this is to use an uninterned symbol instead of
527 `max' (*note Creating Symbols::).  The uninterned symbol can be bound
528 and referred to just like any other symbol, but since it is created by
529 `for', we know that it cannot already appear in the user's program.
530 Since it is not interned, there is no way the user can put it into the
531 program later.  It will never appear anywhere except where put by
532 `for'.  Here is a definition of `for' that works this way:
533
534      (defmacro for (var from init to final do &rest body)
535        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
536        (let ((tempvar (make-symbol "max")))
537          `(let ((,var ,init)
538                 (,tempvar ,final))
539             (while (<= ,var ,tempvar)
540               ,@body
541               (inc ,var)))))
542
543 This creates an uninterned symbol named `max' and puts it in the
544 expansion instead of the usual interned symbol `max' that appears in
545 expressions ordinarily.
546
547 \1f
548 File: lispref.info,  Node: Eval During Expansion,  Next: Repeated Expansion,  Prev: Surprising Local Vars,  Up: Problems with Macros
549
550 Evaluating Macro Arguments in Expansion
551 ---------------------------------------
552
553    Another problem can happen if you evaluate any of the macro argument
554 expressions during the computation of the expansion, such as by calling
555 `eval' (*note Eval::).  If the argument is supposed to refer to the
556 user's variables, you may have trouble if the user happens to use a
557 variable with the same name as one of the macro arguments.  Inside the
558 macro body, the macro argument binding is the most local binding of this
559 variable, so any references inside the form being evaluated do refer to
560 it.  Here is an example:
561
562      (defmacro foo (a)
563        (list 'setq (eval a) t))
564           => foo
565      (setq x 'b)
566      (foo x) ==> (setq b t)
567           => t                  ; and `b' has been set.
568      ;; but
569      (setq a 'c)
570      (foo a) ==> (setq a t)
571           => t                  ; but this set `a', not `c'.
572
573    It makes a difference whether the user's variable is named `a' or
574 `x', because `a' conflicts with the macro argument variable `a'.
575
576    Another reason not to call `eval' in a macro definition is that it
577 probably won't do what you intend in a compiled program.  The
578 byte-compiler runs macro definitions while compiling the program, when
579 the program's own computations (which you might have wished to access
580 with `eval') don't occur and its local variable bindings don't exist.
581
582    The safe way to work with the run-time value of an expression is to
583 put the expression into the macro expansion, so that its value is
584 computed as part of executing the expansion.
585
586 \1f
587 File: lispref.info,  Node: Repeated Expansion,  Prev: Eval During Expansion,  Up: Problems with Macros
588
589 How Many Times is the Macro Expanded?
590 -------------------------------------
591
592    Occasionally problems result from the fact that a macro call is
593 expanded each time it is evaluated in an interpreted function, but is
594 expanded only once (during compilation) for a compiled function.  If the
595 macro definition has side effects, they will work differently depending
596 on how many times the macro is expanded.
597
598    In particular, constructing objects is a kind of side effect.  If the
599 macro is called once, then the objects are constructed only once.  In
600 other words, the same structure of objects is used each time the macro
601 call is executed.  In interpreted operation, the macro is reexpanded
602 each time, producing a fresh collection of objects each time.  Usually
603 this does not matter--the objects have the same contents whether they
604 are shared or not.  But if the surrounding program does side effects on
605 the objects, it makes a difference whether they are shared.  Here is an
606 example:
607
608      (defmacro empty-object ()
609        (list 'quote (cons nil nil)))
610      
611      (defun initialize (condition)
612        (let ((object (empty-object)))
613          (if condition
614              (setcar object condition))
615          object))
616
617 If `initialize' is interpreted, a new list `(nil)' is constructed each
618 time `initialize' is called.  Thus, no side effect survives between
619 calls.  If `initialize' is compiled, then the macro `empty-object' is
620 expanded during compilation, producing a single "constant" `(nil)' that
621 is reused and altered each time `initialize' is called.
622
623    One way to avoid pathological cases like this is to think of
624 `empty-object' as a funny kind of constant, not as a memory allocation
625 construct.  You wouldn't use `setcar' on a constant such as `'(nil)',
626 so naturally you won't use it on `(empty-object)' either.
627
628 \1f
629 File: lispref.info,  Node: Customization,  Up: Top
630
631 Writing Customization Definitions
632 *********************************
633
634    This chapter describes how to declare user options for customization,
635 and also customization groups for classifying them.  We use the term
636 "customization item" to include both kinds of customization
637 definitions--as well as face definitions.
638
639 * Menu:
640
641 * Common Keywords::
642 * Group Definitions::
643 * Variable Definitions::
644 * Customization Types::
645
646 \1f
647 File: lispref.info,  Node: Common Keywords,  Next: Group Definitions,  Up: Customization
648
649 Common Keywords for All Kinds of Items
650 ======================================
651
652    All kinds of customization declarations (for variables and groups,
653 and for faces) accept keyword arguments for specifying various
654 information.  This section describes some keywords that apply to all
655 kinds.
656
657    All of these keywords, except `:tag', can be used more than once in
658 a given item.  Each use of the keyword has an independent effect.  The
659 keyword `:tag' is an exception because any given item can only display
660 one name.
661
662 `:tag NAME'
663      Use NAME, a string, instead of the item's name, to label the item
664      in customization menus and buffers.
665
666 `:group GROUP'
667      Put this customization item in group GROUP.  When you use `:group'
668      in a `defgroup', it makes the new group a subgroup of GROUP.
669
670      If you use this keyword more than once, you can put a single item
671      into more than one group.  Displaying any of those groups will
672      show this item.  Be careful not to overdo this!
673
674 `:link LINK-DATA'
675      Include an external link after the documentation string for this
676      item.  This is a sentence containing an active field which
677      references some other documentation.
678
679      There are three alternatives you can use for LINK-DATA:
680
681     `(custom-manual INFO-NODE)'
682           Link to an Info node; INFO-NODE is a string which specifies
683           the node name, as in `"(emacs)Top"'.  The link appears as
684           `[manual]' in the customization buffer.
685
686     `(info-link INFO-NODE)'
687           Like `custom-manual' except that the link appears in the
688           customization buffer with the Info node name.
689
690     `(url-link URL)'
691           Link to a web page; URL is a string which specifies the URL.
692           The link appears in the customization buffer as URL.
693
694      You can specify the text to use in the customization buffer by
695      adding `:tag NAME' after the first element of the LINK-DATA; for
696      example, `(info-link :tag "foo" "(emacs)Top")' makes a link to the
697      Emacs manual which appears in the buffer as `foo'.
698
699      An item can have more than one external link; however, most items
700      have none at all.
701
702 `:load FILE'
703      Load file FILE (a string) before displaying this customization
704      item.  Loading is done with `load-library', and only if the file is
705      not already loaded.
706
707 `:require FEATURE'
708      Require feature FEATURE (a symbol) when installing a value for
709      this item (an option or a face) that was saved using the
710      customization feature.  This is done by calling `require'.
711
712      The most common reason to use `:require' is when a variable enables
713      a feature such as a minor mode, and just setting the variable
714      won't have any effect unless the code which implements the mode is
715      loaded.
716
717 \1f
718 File: lispref.info,  Node: Group Definitions,  Next: Variable Definitions,  Prev: Common Keywords,  Up: Customization
719
720 Defining Custom Groups
721 ======================
722
723    Each Emacs Lisp package should have one main customization group
724 which contains all the options, faces and other groups in the package.
725 If the package has a small number of options and faces, use just one
726 group and put everything in it.  When there are more than twelve or so
727 options and faces, then you should structure them into subgroups, and
728 put the subgroups under the package's main customization group.  It is
729 OK to put some of the options and faces in the package's main group
730 alongside the subgroups.
731
732    The package's main or only group should be a member of one or more of
733 the standard customization groups.  (To display the full list of them,
734 use `M-x customize'.)  Choose one or more of them (but not too many),
735 and add your group to each of them using the `:group' keyword.
736
737    The way to declare new customization groups is with `defgroup'.
738
739  - Macro: defgroup group members doc [keyword value]...
740      Declare GROUP as a customization group containing MEMBERS.  Do not
741      quote the symbol GROUP.  The argument DOC specifies the
742      documentation string for the group.
743
744      The argument MEMBERS is a list specifying an initial set of
745      customization items to be members of the group.  However, most
746      often MEMBERS is `nil', and you specify the group's members by
747      using the `:group' keyword when defining those members.
748
749      If you want to specify group members through MEMBERS, each element
750      should have the form `(NAME WIDGET)'.  Here NAME is a symbol, and
751      WIDGET is a widget type for editing that symbol.  Useful widgets
752      are `custom-variable' for a variable, `custom-face' for a face,
753      and `custom-group' for a group.
754
755      In addition to the common keywords (*note Common Keywords::), you
756      can use this keyword in `defgroup':
757
758     `:prefix PREFIX'
759           If the name of an item in the group starts with PREFIX, then
760           the tag for that item is constructed (by default) by omitting
761           PREFIX.
762
763           One group can have any number of prefixes.
764
765 \1f
766 File: lispref.info,  Node: Variable Definitions,  Next: Customization Types,  Prev: Group Definitions,  Up: Customization
767
768 Defining Customization Variables
769 ================================
770
771    Use `defcustom' to declare user-editable variables.
772
773  - Macro: defcustom option default doc [keyword value]...
774      Declare OPTION as a customizable user option variable.  Do not
775      quote OPTION.  The argument DOC specifies the documentation string
776      for the variable.
777
778      If OPTION is void, `defcustom' initializes it to DEFAULT.  DEFAULT
779      should be an expression to compute the value; be careful in
780      writing it, because it can be evaluated on more than one occasion.
781
782      The following additional keywords are defined:
783
784     `:type TYPE'
785           Use TYPE as the data type for this option.  It specifies which
786           values are legitimate, and how to display the value.  *Note
787           Customization Types::, for more information.
788
789     `:options LIST'
790           Specify LIST as the list of reasonable values for use in this
791           option.
792
793           Currently this is meaningful only when the type is `hook'.
794           In that case, the elements of LIST should be functions that
795           are useful as elements of the hook value.  The user is not
796           restricted to using only these functions, but they are
797           offered as convenient alternatives.
798
799     `:version VERSION'
800           This option specifies that the variable was first introduced,
801           or its default value was changed, in Emacs version VERSION.
802           The value VERSION must be a string.  For example,
803
804                (defcustom foo-max 34
805                  "*Maximum number of foo's allowed."
806                  :type 'integer
807                  :group 'foo
808                  :version "20.3")
809
810     `:set SETFUNCTION'
811           Specify SETFUNCTION as the way to change the value of this
812           option.  The function SETFUNCTION should take two arguments,
813           a symbol and the new value, and should do whatever is
814           necessary to update the value properly for this option (which
815           may not mean simply setting the option as a Lisp variable).
816           The default for SETFUNCTION is `set-default'.
817
818     `:get GETFUNCTION'
819           Specify GETFUNCTION as the way to extract the value of this
820           option.  The function GETFUNCTION should take one argument, a
821           symbol, and should return the "current value" for that symbol
822           (which need not be the symbol's Lisp value).  The default is
823           `default-value'.
824
825     `:initialize FUNCTION'
826           FUNCTION should be a function used to initialize the variable
827           when the `defcustom' is evaluated.  It should take two
828           arguments, the symbol and value.  Here are some predefined
829           functions meant for use in this way:
830
831          `custom-initialize-set'
832                Use the variable's `:set' function to initialize the
833                variable, but do not reinitialize it if it is already
834                non-void.  This is the default `:initialize' function.
835
836          `custom-initialize-default'
837                Like `custom-initialize-set', but use the function
838                `set-default' to set the variable, instead of the
839                variable's `:set' function.  This is the usual choice
840                for a variable whose `:set' function enables or disables
841                a minor mode; with this choice, defining the variable
842                will not call the minor mode function, but customizing
843                the variable will do so.
844
845          `custom-initialize-reset'
846                Always use the `:set' function to initialize the
847                variable.  If the variable is already non-void, reset it
848                by calling the `:set' function using the current value
849                (returned by the `:get' method).
850
851          `custom-initialize-changed'
852                Use the `:set' function to initialize the variable, if
853                it is already set or has been customized; otherwise,
854                just use `set-default'.
855
856    The `:require' option is useful for an option that turns on the
857 operation of a certain feature.  Assuming that the package is coded to
858 check the value of the option, you still need to arrange for the package
859 to be loaded.  You can do that with `:require'.  *Note Common
860 Keywords::.  Here is an example, from the library `paren.el':
861
862      (defcustom show-paren-mode nil
863        "Toggle Show Paren mode...."
864        :set (lambda (symbol value)
865               (show-paren-mode (or value 0)))
866        :initialize 'custom-initialize-default
867        :type 'boolean
868        :group 'paren-showing
869        :require 'paren)
870
871    Internally, `defcustom' uses the symbol property `standard-value' to
872 record the expression for the default value, and `saved-value' to
873 record the value saved by the user with the customization buffer.  The
874 `saved-value' property is actually a list whose car is an expression
875 which evaluates to the value.
876
877 \1f
878 File: lispref.info,  Node: Customization Types,  Prev: Variable Definitions,  Up: Customization
879
880 Customization Types
881 ===================
882
883    When you define a user option with `defcustom', you must specify its
884 "customization type".  That is a Lisp object which describes (1) which
885 values are legitimate and (2) how to display the value in the
886 customization buffer for editing.
887
888    You specify the customization type in `defcustom' with the `:type'
889 keyword.  The argument of `:type' is evaluated; since types that vary
890 at run time are rarely useful, normally you use a quoted constant.  For
891 example:
892
893      (defcustom diff-command "diff"
894        "*The command to use to run diff."
895        :type '(string)
896        :group 'diff)
897
898    In general, a customization type is a list whose first element is a
899 symbol, one of the customization type names defined in the following
900 sections.  After this symbol come a number of arguments, depending on
901 the symbol.  Between the type symbol and its arguments, you can
902 optionally write keyword-value pairs (*note Type Keywords::).
903
904    Some of the type symbols do not use any arguments; those are called
905 "simple types".  For a simple type, if you do not use any keyword-value
906 pairs, you can omit the parentheses around the type symbol.  For
907 example just `string' as a customization type is equivalent to
908 `(string)'.
909
910 * Menu:
911
912 * Simple Types::
913 * Composite Types::
914 * Splicing into Lists::
915 * Type Keywords::
916
917 \1f
918 File: lispref.info,  Node: Simple Types,  Next: Composite Types,  Up: Customization Types
919
920 Simple Types
921 ------------
922
923    This section describes all the simple customization types.
924
925 `sexp'
926      The value may be any Lisp object that can be printed and read
927      back.  You can use `sexp' as a fall-back for any option, if you
928      don't want to take the time to work out a more specific type to
929      use.
930
931 `integer'
932      The value must be an integer, and is represented textually in the
933      customization buffer.
934
935 `number'
936      The value must be a number, and is represented textually in the
937      customization buffer.
938
939 `string'
940      The value must be a string, and the customization buffer shows
941      just the contents, with no delimiting `"' characters and no
942      quoting with `\'.
943
944 `regexp'
945      Like `string' except that the string must be a valid regular
946      expression.
947
948 `character'
949      The value must be a character code.  A character code is actually
950      an integer, but this type shows the value by inserting the
951      character in the buffer, rather than by showing the number.
952
953 `file'
954      The value must be a file name, and you can do completion with
955      `M-<TAB>'.
956
957 `(file :must-match t)'
958      The value must be a file name for an existing file, and you can do
959      completion with `M-<TAB>'.
960
961 `directory'
962      The value must be a directory name, and you can do completion with
963      `M-<TAB>'.
964
965 `symbol'
966      The value must be a symbol.  It appears in the customization
967      buffer as the name of the symbol.
968
969 `function'
970      The value must be either a lambda expression or a function name.
971      When it is a function name, you can do completion with `M-<TAB>'.
972
973 `variable'
974      The value must be a variable name, and you can do completion with
975      `M-<TAB>'.
976
977 `face'
978      The value must be a symbol which is a face name.
979
980 `boolean'
981      The value is boolean--either `nil' or `t'.  Note that by using
982      `choice' and `const' together (see the next section), you can
983      specify that the value must be `nil' or `t', but also specify the
984      text to describe each value in a way that fits the specific
985      meaning of the alternative.
986
987 \1f
988 File: lispref.info,  Node: Composite Types,  Next: Splicing into Lists,  Prev: Simple Types,  Up: Customization Types
989
990 Composite Types
991 ---------------
992
993    When none of the simple types is appropriate, you can use composite
994 types, which build new types from other types.  Here are several ways of
995 doing that:
996
997 `(restricted-sexp :match-alternatives CRITERIA)'
998      The value may be any Lisp object that satisfies one of CRITERIA.
999      CRITERIA should be a list, and each elements should be one of
1000      these possibilities:
1001
1002         * A predicate--that is, a function of one argument that returns
1003           non-`nil' if the argument fits a certain type.  This means
1004           that objects of that type are acceptable.
1005
1006         * A quoted constant--that is, `'OBJECT'.  This means that
1007           OBJECT itself is an acceptable value.
1008
1009      For example,
1010
1011           (restricted-sexp :match-alternatives (integerp 't 'nil))
1012
1013      allows integers, `t' and `nil' as legitimate values.
1014
1015      The customization buffer shows all legitimate values using their
1016      read syntax, and the user edits them textually.
1017
1018 `(cons CAR-TYPE CDR-TYPE)'
1019      The value must be a cons cell, its CAR must fit CAR-TYPE, and its
1020      CDR must fit CDR-TYPE.  For example, `(cons string symbol)' is a
1021      customization type which matches values such as `("foo" . foo)'.
1022
1023      In the customization buffer, the CAR and the CDR are displayed and
1024      edited separately, each according to the type that you specify for
1025      it.
1026
1027 `(list ELEMENT-TYPES...)'
1028      The value must be a list with exactly as many elements as the
1029      ELEMENT-TYPES you have specified; and each element must fit the
1030      corresponding ELEMENT-TYPE.
1031
1032      For example, `(list integer string function)' describes a list of
1033      three elements; the first element must be an integer, the second a
1034      string, and the third a function.
1035
1036      In the customization buffer, the each element is displayed and
1037      edited separately, according to the type specified for it.
1038
1039 `(vector ELEMENT-TYPES...)'
1040      Like `list' except that the value must be a vector instead of a
1041      list.  The elements work the same as in `list'.
1042
1043 `(choice ALTERNATIVE-TYPES...)'
1044      The value must fit at least one of ALTERNATIVE-TYPES.  For
1045      example, `(choice integer string)' allows either an integer or a
1046      string.
1047
1048      In the customization buffer, the user selects one of the
1049      alternatives using a menu, and can then edit the value in the
1050      usual way for that alternative.
1051
1052      Normally the strings in this menu are determined automatically
1053      from the choices; however, you can specify different strings for
1054      the menu by including the `:tag' keyword in the alternatives.  For
1055      example, if an integer stands for a number of spaces, while a
1056      string is text to use verbatim, you might write the customization
1057      type this way,
1058
1059           (choice (integer :tag "Number of spaces")
1060                   (string :tag "Literal text"))
1061
1062      so that the menu offers `Number of spaces' and `Literal Text'.
1063
1064      In any alternative for which `nil' is not a valid value, other than
1065      a `const', you should specify a valid default for that alternative
1066      using the `:value' keyword.  *Note Type Keywords::.
1067
1068 `(const VALUE)'
1069      The value must be VALUE--nothing else is allowed.
1070
1071      The main use of `const' is inside of `choice'.  For example,
1072      `(choice integer (const nil))' allows either an integer or `nil'.
1073
1074      `:tag' is often used with `const', inside of `choice'.  For
1075      example,
1076
1077           (choice (const :tag "Yes" t)
1078                   (const :tag "No" nil)
1079                   (const :tag "Ask" foo))
1080
1081 `(function-item FUNCTION)'
1082      Like `const', but used for values which are functions.  This
1083      displays the documentation string as well as the function name.
1084      The documentation string is either the one you specify with
1085      `:doc', or FUNCTION's own documentation string.
1086
1087 `(variable-item VARIABLE)'
1088      Like `const', but used for values which are variable names.  This
1089      displays the documentation string as well as the variable name.
1090      The documentation string is either the one you specify with
1091      `:doc', or VARIABLE's own documentation string.
1092
1093 `(set ELEMENTS...)'
1094      The value must be a list and each element of the list must be one
1095      of the ELEMENTS specified.  This appears in the customization
1096      buffer as a checklist.
1097
1098 `(repeat ELEMENT-TYPE)'
1099      The value must be a list and each element of the list must fit the
1100      type ELEMENT-TYPE.  This appears in the customization buffer as a
1101      list of elements, with `[INS]' and `[DEL]' buttons for adding more
1102      elements or removing elements.
1103
1104 \1f
1105 File: lispref.info,  Node: Splicing into Lists,  Next: Type Keywords,  Prev: Composite Types,  Up: Customization Types
1106
1107 Splicing into Lists
1108 -------------------
1109
1110    The `:inline' feature lets you splice a variable number of elements
1111 into the middle of a list or vector.  You use it in a `set', `choice'
1112 or `repeat' type which appears among the element-types of a `list' or
1113 `vector'.
1114
1115    Normally, each of the element-types in a `list' or `vector'
1116 describes one and only one element of the list or vector.  Thus, if an
1117 element-type is a `repeat', that specifies a list of unspecified length
1118 which appears as one element.
1119
1120    But when the element-type uses `:inline', the value it matches is
1121 merged directly into the containing sequence.  For example, if it
1122 matches a list with three elements, those become three elements of the
1123 overall sequence.  This is analogous to using `,@' in the backquote
1124 construct.
1125
1126    For example, to specify a list whose first element must be `t' and
1127 whose remaining arguments should be zero or more of `foo' and `bar',
1128 use this customization type:
1129
1130      (list (const t) (set :inline t foo bar))
1131
1132 This matches values such as `(t)', `(t foo)', `(t bar)' and `(t foo
1133 bar)'.
1134
1135    When the element-type is a `choice', you use `:inline' not in the
1136 `choice' itself, but in (some of) the alternatives of the `choice'.
1137 For example, to match a list which must start with a file name,
1138 followed either by the symbol `t' or two strings, use this
1139 customization type:
1140
1141      (list file
1142            (choice (const t)
1143                    (list :inline t string string)))
1144
1145 If the user chooses the first alternative in the choice, then the
1146 overall list has two elements and the second element is `t'.  If the
1147 user chooses the second alternative, then the overall list has three
1148 elements and the second and third must be strings.
1149
1150 \1f
1151 File: lispref.info,  Node: Type Keywords,  Prev: Splicing into Lists,  Up: Customization Types
1152
1153 Type Keywords
1154 -------------
1155
1156    You can specify keyword-argument pairs in a customization type after
1157 the type name symbol.  Here are the keywords you can use, and their
1158 meanings:
1159
1160 `:value DEFAULT'
1161      This is used for a type that appears as an alternative inside of
1162      `choice'; it specifies the default value to use, at first, if and
1163      when the user selects this alternative with the menu in the
1164      customization buffer.
1165
1166      Of course, if the actual value of the option fits this
1167      alternative, it will appear showing the actual value, not DEFAULT.
1168
1169      If `nil' is not a valid value for the alternative, then it is
1170      essential to specify a valid default with `:value'.
1171
1172 `:format FORMAT-STRING'
1173      This string will be inserted in the buffer to represent the value
1174      corresponding to the type.  The following `%' escapes are available
1175      for use in FORMAT-STRING:
1176
1177     `%[BUTTON%]'
1178           Display the text BUTTON marked as a button.  The `:action'
1179           attribute specifies what the button will do if the user
1180           invokes it; its value is a function which takes two
1181           arguments--the widget which the button appears in, and the
1182           event.
1183
1184           There is no way to specify two different buttons with
1185           different actions.
1186
1187     `%{SAMPLE%}'
1188           Show SAMPLE in a special face specified by `:sample-face'.
1189
1190     `%v'
1191           Substitute the item's value.  How the value is represented
1192           depends on the kind of item, and (for variables) on the
1193           customization type.
1194
1195     `%d'
1196           Substitute the item's documentation string.
1197
1198     `%h'
1199           Like `%d', but if the documentation string is more than one
1200           line, add an active field to control whether to show all of
1201           it or just the first line.
1202
1203     `%t'
1204           Substitute the tag here.  You specify the tag with the `:tag'
1205           keyword.
1206
1207     `%%'
1208           Display a literal `%'.
1209
1210 `:action ACTION'
1211      Perform ACTION if the user clicks on a button.
1212
1213 `:button-face FACE'
1214      Use the face FACE (a face name or a list of face names) for button
1215      text displayed with `%[...%]'.
1216
1217 `:button-prefix PREFIX'
1218 `:button-suffix SUFFIX'
1219      These specify the text to display before and after a button.  Each
1220      can be:
1221
1222     `nil'
1223           No text is inserted.
1224
1225     a string
1226           The string is inserted literally.
1227
1228     a symbol
1229           The symbol's value is used.
1230
1231 `:tag TAG'
1232      Use TAG (a string) as the tag for the value (or part of the value)
1233      that corresponds to this type.
1234
1235 `:doc DOC'
1236      Use DOC as the documentation string for this value (or part of the
1237      value) that corresponds to this type.  In order for this to work,
1238      you must specify a value for `:format', and use `%d' or `%h' in
1239      that value.
1240
1241      The usual reason to specify a documentation string for a type is to
1242      provide more information about the meanings of alternatives inside
1243      a `:choice' type or the parts of some other composite type.
1244
1245 `:help-echo MOTION-DOC'
1246      When you move to this item with `widget-forward' or
1247      `widget-backward', it will display the string MOTION-DOC in the
1248      echo area.
1249
1250 `:match FUNCTION'
1251      Specify how to decide whether a value matches the type.  The
1252      corresponding value, FUNCTION, should be a function that accepts
1253      two arguments, a widget and a value; it should return non-`nil' if
1254      the value is acceptable.
1255
1256 \1f
1257 File: lispref.info,  Node: Loading,  Next: Byte Compilation,  Prev: Macros,  Up: Top
1258
1259 Loading
1260 *******
1261
1262    Loading a file of Lisp code means bringing its contents into the Lisp
1263 environment in the form of Lisp objects.  XEmacs finds and opens the
1264 file, reads the text, evaluates each form, and then closes the file.
1265
1266    The load functions evaluate all the expressions in a file just as
1267 the `eval-current-buffer' function evaluates all the expressions in a
1268 buffer.  The difference is that the load functions read and evaluate
1269 the text in the file as found on disk, not the text in an Emacs buffer.
1270
1271    The loaded file must contain Lisp expressions, either as source code
1272 or as byte-compiled code.  Each form in the file is called a "top-level
1273 form".  There is no special format for the forms in a loadable file;
1274 any form in a file may equally well be typed directly into a buffer and
1275 evaluated there.  (Indeed, most code is tested this way.)  Most often,
1276 the forms are function definitions and variable definitions.
1277
1278    A file containing Lisp code is often called a "library".  Thus, the
1279 "Rmail library" is a file containing code for Rmail mode.  Similarly, a
1280 "Lisp library directory" is a directory of files containing Lisp code.
1281
1282 * Menu:
1283
1284 * How Programs Do Loading::     The `load' function and others.
1285 * Autoload::                    Setting up a function to autoload.
1286 * Repeated Loading::            Precautions about loading a file twice.
1287 * Named Features::              Loading a library if it isn't already loaded.
1288 * Unloading::                   How to ``unload'' a library that was loaded.
1289 * Hooks for Loading::           Providing code to be run when
1290                                   particular libraries are loaded.
1291