]> code.delx.au - gnu-emacs/blob - doc/lispref/variables.texi
5a2cae0f6781fd6ce8733df37607b483b1a04dcc
[gnu-emacs] / doc / lispref / variables.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-2015 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @node Variables
6 @chapter Variables
7 @cindex variable
8
9 A @dfn{variable} is a name used in a program to stand for a value.
10 In Lisp, each variable is represented by a Lisp symbol
11 (@pxref{Symbols}). The variable name is simply the symbol's name, and
12 the variable's value is stored in the symbol's value cell@footnote{To
13 be precise, under the default @dfn{dynamic scoping} rule, the value
14 cell always holds the variable's current value, but this is not the
15 case under the @dfn{lexical scoping} rule. @xref{Variable Scoping},
16 for details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a
17 symbol as a variable is independent of its use as a function name.
18
19 As previously noted in this manual, a Lisp program is represented
20 primarily by Lisp objects, and only secondarily as text. The textual
21 form of a Lisp program is given by the read syntax of the Lisp objects
22 that constitute the program. Hence, the textual form of a variable in
23 a Lisp program is written using the read syntax for the symbol
24 representing the variable.
25
26 @menu
27 * Global Variables:: Variable values that exist permanently, everywhere.
28 * Constant Variables:: Variables that never change.
29 * Local Variables:: Variable values that exist only temporarily.
30 * Void Variables:: Symbols that lack values.
31 * Defining Variables:: A definition says a symbol is used as a variable.
32 * Tips for Defining:: Things you should think about when you
33 define a variable.
34 * Accessing Variables:: Examining values of variables whose names
35 are known only at run time.
36 * Setting Variables:: Storing new values in variables.
37 * Variable Scoping:: How Lisp chooses among local and global values.
38 * Buffer-Local Variables:: Variable values in effect only in one buffer.
39 * File Local Variables:: Handling local variable lists in files.
40 * Directory Local Variables:: Local variables common to all files in a directory.
41 * Variable Aliases:: Variables that are aliases for other variables.
42 * Variables with Restricted Values:: Non-constant variables whose value can
43 @emph{not} be an arbitrary Lisp object.
44 * Generalized Variables:: Extending the concept of variables.
45 @end menu
46
47 @node Global Variables
48 @section Global Variables
49 @cindex global variable
50
51 The simplest way to use a variable is @dfn{globally}. This means that
52 the variable has just one value at a time, and this value is in effect
53 (at least for the moment) throughout the Lisp system. The value remains
54 in effect until you specify a new one. When a new value replaces the
55 old one, no trace of the old value remains in the variable.
56
57 You specify a value for a symbol with @code{setq}. For example,
58
59 @example
60 (setq x '(a b))
61 @end example
62
63 @noindent
64 gives the variable @code{x} the value @code{(a b)}. Note that
65 @code{setq} is a special form (@pxref{Special Forms}); it does not
66 evaluate its first argument, the name of the variable, but it does
67 evaluate the second argument, the new value.
68
69 Once the variable has a value, you can refer to it by using the
70 symbol itself as an expression. Thus,
71
72 @example
73 @group
74 x @result{} (a b)
75 @end group
76 @end example
77
78 @noindent
79 assuming the @code{setq} form shown above has already been executed.
80
81 If you do set the same variable again, the new value replaces the old
82 one:
83
84 @example
85 @group
86 x
87 @result{} (a b)
88 @end group
89 @group
90 (setq x 4)
91 @result{} 4
92 @end group
93 @group
94 x
95 @result{} 4
96 @end group
97 @end example
98
99 @node Constant Variables
100 @section Variables that Never Change
101 @cindex @code{setting-constant} error
102 @cindex keyword symbol
103 @cindex variable with constant value
104 @cindex constant variables
105 @cindex symbol that evaluates to itself
106 @cindex symbol with constant value
107
108 In Emacs Lisp, certain symbols normally evaluate to themselves. These
109 include @code{nil} and @code{t}, as well as any symbol whose name starts
110 with @samp{:} (these are called @dfn{keywords}). These symbols cannot
111 be rebound, nor can their values be changed. Any attempt to set or bind
112 @code{nil} or @code{t} signals a @code{setting-constant} error. The
113 same is true for a keyword (a symbol whose name starts with @samp{:}),
114 if it is interned in the standard obarray, except that setting such a
115 symbol to itself is not an error.
116
117 @example
118 @group
119 nil @equiv{} 'nil
120 @result{} nil
121 @end group
122 @group
123 (setq nil 500)
124 @error{} Attempt to set constant symbol: nil
125 @end group
126 @end example
127
128 @defun keywordp object
129 function returns @code{t} if @var{object} is a symbol whose name
130 starts with @samp{:}, interned in the standard obarray, and returns
131 @code{nil} otherwise.
132 @end defun
133
134 These constants are fundamentally different from the constants
135 defined using the @code{defconst} special form (@pxref{Defining
136 Variables}). A @code{defconst} form serves to inform human readers
137 that you do not intend to change the value of a variable, but Emacs
138 does not raise an error if you actually change it.
139
140 @node Local Variables
141 @section Local Variables
142 @cindex binding local variables
143 @cindex local variables
144 @cindex local binding
145 @cindex global binding
146
147 Global variables have values that last until explicitly superseded
148 with new values. Sometimes it is useful to give a variable a
149 @dfn{local value}---a value that takes effect only within a certain
150 part of a Lisp program. When a variable has a local value, we say
151 that it is @dfn{locally bound} to that value, and that it is a
152 @dfn{local variable}.
153
154 For example, when a function is called, its argument variables
155 receive local values, which are the actual arguments supplied to the
156 function call; these local bindings take effect within the body of the
157 function. To take another example, the @code{let} special form
158 explicitly establishes local bindings for specific variables, which
159 take effect within the body of the @code{let} form.
160
161 We also speak of the @dfn{global binding}, which is where
162 (conceptually) the global value is kept.
163
164 @cindex shadowing of variables
165 Establishing a local binding saves away the variable's previous
166 value (or lack of one). We say that the previous value is
167 @dfn{shadowed}. Both global and local values may be shadowed. If a
168 local binding is in effect, using @code{setq} on the local variable
169 stores the specified value in the local binding. When that local
170 binding is no longer in effect, the previously shadowed value (or lack
171 of one) comes back.
172
173 @cindex current binding
174 A variable can have more than one local binding at a time (e.g., if
175 there are nested @code{let} forms that bind the variable). The
176 @dfn{current binding} is the local binding that is actually in effect.
177 It determines the value returned by evaluating the variable symbol,
178 and it is the binding acted on by @code{setq}.
179
180 For most purposes, you can think of the current binding as the
181 innermost local binding, or the global binding if there is no
182 local binding. To be more precise, a rule called the @dfn{scoping
183 rule} determines where in a program a local binding takes effect. The
184 default scoping rule in Emacs Lisp is called @dfn{dynamic scoping},
185 which simply states that the current binding at any given point in the
186 execution of a program is the most recently-created binding for that
187 variable that still exists. For details about dynamic scoping, and an
188 alternative scoping rule called @dfn{lexical scoping}, @xref{Variable
189 Scoping}.
190
191 The special forms @code{let} and @code{let*} exist to create local
192 bindings:
193
194 @defspec let (bindings@dots{}) forms@dots{}
195 This special form sets up local bindings for a certain set of
196 variables, as specified by @var{bindings}, and then evaluates all of
197 the @var{forms} in textual order. Its return value is the value of
198 the last form in @var{forms}.
199
200 Each of the @var{bindings} is either @w{(i) a} symbol, in which case
201 that symbol is locally bound to @code{nil}; or @w{(ii) a} list of the
202 form @code{(@var{symbol} @var{value-form})}, in which case
203 @var{symbol} is locally bound to the result of evaluating
204 @var{value-form}. If @var{value-form} is omitted, @code{nil} is used.
205
206 All of the @var{value-form}s in @var{bindings} are evaluated in the
207 order they appear and @emph{before} binding any of the symbols to them.
208 Here is an example of this: @code{z} is bound to the old value of
209 @code{y}, which is 2, not the new value of @code{y}, which is 1.
210
211 @example
212 @group
213 (setq y 2)
214 @result{} 2
215 @end group
216
217 @group
218 (let ((y 1)
219 (z y))
220 (list y z))
221 @result{} (1 2)
222 @end group
223 @end example
224 @end defspec
225
226 @defspec let* (bindings@dots{}) forms@dots{}
227 This special form is like @code{let}, but it binds each variable right
228 after computing its local value, before computing the local value for
229 the next variable. Therefore, an expression in @var{bindings} can
230 refer to the preceding symbols bound in this @code{let*} form.
231 Compare the following example with the example above for @code{let}.
232
233 @example
234 @group
235 (setq y 2)
236 @result{} 2
237 @end group
238
239 @group
240 (let* ((y 1)
241 (z y)) ; @r{Use the just-established value of @code{y}.}
242 (list y z))
243 @result{} (1 1)
244 @end group
245 @end example
246 @end defspec
247
248 Here is a complete list of the other facilities that create local
249 bindings:
250
251 @itemize @bullet
252 @item
253 Function calls (@pxref{Functions}).
254
255 @item
256 Macro calls (@pxref{Macros}).
257
258 @item
259 @code{condition-case} (@pxref{Errors}).
260 @end itemize
261
262 Variables can also have buffer-local bindings (@pxref{Buffer-Local
263 Variables}); a few variables have terminal-local bindings
264 (@pxref{Multiple Terminals}). These kinds of bindings work somewhat
265 like ordinary local bindings, but they are localized depending on
266 where you are in Emacs.
267
268 @defopt max-specpdl-size
269 @anchor{Definition of max-specpdl-size}
270 @cindex variable limit error
271 @cindex evaluation error
272 @cindex infinite recursion
273 This variable defines the limit on the total number of local variable
274 bindings and @code{unwind-protect} cleanups (see @ref{Cleanups,,
275 Cleaning Up from Nonlocal Exits}) that are allowed before Emacs
276 signals an error (with data @code{"Variable binding depth exceeds
277 max-specpdl-size"}).
278
279 This limit, with the associated error when it is exceeded, is one way
280 that Lisp avoids infinite recursion on an ill-defined function.
281 @code{max-lisp-eval-depth} provides another limit on depth of nesting.
282 @xref{Definition of max-lisp-eval-depth,, Eval}.
283
284 The default value is 1300. Entry to the Lisp debugger increases the
285 value, if there is little room left, to make sure the debugger itself
286 has room to execute.
287 @end defopt
288
289 @node Void Variables
290 @section When a Variable is Void
291 @cindex @code{void-variable} error
292 @cindex void variable
293
294 We say that a variable is void if its symbol has an unassigned value
295 cell (@pxref{Symbol Components}).
296
297 Under Emacs Lisp's default dynamic scoping rule (@pxref{Variable
298 Scoping}), the value cell stores the variable's current (local or
299 global) value. Note that an unassigned value cell is @emph{not} the
300 same as having @code{nil} in the value cell. The symbol @code{nil} is
301 a Lisp object and can be the value of a variable, just as any other
302 object can be; but it is still a value. If a variable is void, trying
303 to evaluate the variable signals a @code{void-variable} error, instead
304 of returning a value.
305
306 Under the optional lexical scoping rule, the value cell only holds
307 the variable's global value---the value outside of any lexical binding
308 construct. When a variable is lexically bound, the local value is
309 determined by the lexical environment; hence, variables can have local
310 values even if their symbols' value cells are unassigned.
311
312 @defun makunbound symbol
313 This function empties out the value cell of @var{symbol}, making the
314 variable void. It returns @var{symbol}.
315
316 If @var{symbol} has a dynamic local binding, @code{makunbound} voids
317 the current binding, and this voidness lasts only as long as the local
318 binding is in effect. Afterwards, the previously shadowed local or
319 global binding is reexposed; then the variable will no longer be void,
320 unless the reexposed binding is void too.
321
322 Here are some examples (assuming dynamic binding is in effect):
323
324 @smallexample
325 @group
326 (setq x 1) ; @r{Put a value in the global binding.}
327 @result{} 1
328 (let ((x 2)) ; @r{Locally bind it.}
329 (makunbound 'x) ; @r{Void the local binding.}
330 x)
331 @error{} Symbol's value as variable is void: x
332 @end group
333 @group
334 x ; @r{The global binding is unchanged.}
335 @result{} 1
336
337 (let ((x 2)) ; @r{Locally bind it.}
338 (let ((x 3)) ; @r{And again.}
339 (makunbound 'x) ; @r{Void the innermost-local binding.}
340 x)) ; @r{And refer: it's void.}
341 @error{} Symbol's value as variable is void: x
342 @end group
343
344 @group
345 (let ((x 2))
346 (let ((x 3))
347 (makunbound 'x)) ; @r{Void inner binding, then remove it.}
348 x) ; @r{Now outer @code{let} binding is visible.}
349 @result{} 2
350 @end group
351 @end smallexample
352 @end defun
353
354 @defun boundp variable
355 This function returns @code{t} if @var{variable} (a symbol) is not
356 void, and @code{nil} if it is void.
357
358 Here are some examples (assuming dynamic binding is in effect):
359
360 @smallexample
361 @group
362 (boundp 'abracadabra) ; @r{Starts out void.}
363 @result{} nil
364 @end group
365 @group
366 (let ((abracadabra 5)) ; @r{Locally bind it.}
367 (boundp 'abracadabra))
368 @result{} t
369 @end group
370 @group
371 (boundp 'abracadabra) ; @r{Still globally void.}
372 @result{} nil
373 @end group
374 @group
375 (setq abracadabra 5) ; @r{Make it globally nonvoid.}
376 @result{} 5
377 @end group
378 @group
379 (boundp 'abracadabra)
380 @result{} t
381 @end group
382 @end smallexample
383 @end defun
384
385 @node Defining Variables
386 @section Defining Global Variables
387 @cindex variable definition
388
389 A @dfn{variable definition} is a construct that announces your
390 intention to use a symbol as a global variable. It uses the special
391 forms @code{defvar} or @code{defconst}, which are documented below.
392
393 A variable definition serves three purposes. First, it informs
394 people who read the code that the symbol is @emph{intended} to be used
395 a certain way (as a variable). Second, it informs the Lisp system of
396 this, optionally supplying an initial value and a documentation
397 string. Third, it provides information to programming tools such as
398 @command{etags}, allowing them to find where the variable was defined.
399
400 The difference between @code{defconst} and @code{defvar} is mainly a
401 matter of intent, serving to inform human readers of whether the value
402 should ever change. Emacs Lisp does not actually prevent you from
403 changing the value of a variable defined with @code{defconst}. One
404 notable difference between the two forms is that @code{defconst}
405 unconditionally initializes the variable, whereas @code{defvar}
406 initializes it only if it is originally void.
407
408 To define a customizable variable, you should use @code{defcustom}
409 (which calls @code{defvar} as a subroutine). @xref{Variable
410 Definitions}.
411
412 @defspec defvar symbol [value [doc-string]]
413 This special form defines @var{symbol} as a variable. Note that
414 @var{symbol} is not evaluated; the symbol to be defined should appear
415 explicitly in the @code{defvar} form. The variable is marked as
416 @dfn{special}, meaning that it should always be dynamically bound
417 (@pxref{Variable Scoping}).
418
419 If @var{value} is specified, and @var{symbol} is void (i.e., it has no
420 dynamically bound value; @pxref{Void Variables}), then @var{value} is
421 evaluated and @var{symbol} is set to the result. But if @var{symbol}
422 is not void, @var{value} is not evaluated, and @var{symbol}'s value is
423 left unchanged. If @var{value} is omitted, the value of @var{symbol}
424 is not changed in any case.
425
426 If @var{symbol} has a buffer-local binding in the current buffer,
427 @code{defvar} acts on the default value, which is buffer-independent,
428 rather than the buffer-local binding. It sets the default value if
429 the default value is void. @xref{Buffer-Local Variables}.
430
431 If @var{symbol} is already lexically bound (e.g., if the @code{defvar}
432 form occurs in a @code{let} form with lexical binding enabled), then
433 @code{defvar} sets the dynamic value. The lexical binding remains in
434 effect until its binding construct exits. @xref{Variable Scoping}.
435
436 When you evaluate a top-level @code{defvar} form with @kbd{C-M-x} in
437 Emacs Lisp mode (@code{eval-defun}), a special feature of
438 @code{eval-defun} arranges to set the variable unconditionally, without
439 testing whether its value is void.
440
441 If the @var{doc-string} argument is supplied, it specifies the
442 documentation string for the variable (stored in the symbol's
443 @code{variable-documentation} property). @xref{Documentation}.
444
445 Here are some examples. This form defines @code{foo} but does not
446 initialize it:
447
448 @example
449 @group
450 (defvar foo)
451 @result{} foo
452 @end group
453 @end example
454
455 This example initializes the value of @code{bar} to @code{23}, and gives
456 it a documentation string:
457
458 @example
459 @group
460 (defvar bar 23
461 "The normal weight of a bar.")
462 @result{} bar
463 @end group
464 @end example
465
466 The @code{defvar} form returns @var{symbol}, but it is normally used
467 at top level in a file where its value does not matter.
468 @end defspec
469
470 @cindex constant variables
471 @defspec defconst symbol value [doc-string]
472 This special form defines @var{symbol} as a value and initializes it.
473 It informs a person reading your code that @var{symbol} has a standard
474 global value, established here, that should not be changed by the user
475 or by other programs. Note that @var{symbol} is not evaluated; the
476 symbol to be defined must appear explicitly in the @code{defconst}.
477
478 The @code{defconst} form, like @code{defvar}, marks the variable as
479 @dfn{special}, meaning that it should always be dynamically bound
480 (@pxref{Variable Scoping}). In addition, it marks the variable as
481 risky (@pxref{File Local Variables}).
482
483 @code{defconst} always evaluates @var{value}, and sets the value of
484 @var{symbol} to the result. If @var{symbol} does have a buffer-local
485 binding in the current buffer, @code{defconst} sets the default value,
486 not the buffer-local value. (But you should not be making
487 buffer-local bindings for a symbol that is defined with
488 @code{defconst}.)
489
490 An example of the use of @code{defconst} is Emacs's definition of
491 @code{float-pi}---the mathematical constant @math{pi}, which ought not
492 to be changed by anyone (attempts by the Indiana State Legislature
493 notwithstanding). As the second form illustrates, however,
494 @code{defconst} is only advisory.
495
496 @example
497 @group
498 (defconst float-pi 3.141592653589793 "The value of Pi.")
499 @result{} float-pi
500 @end group
501 @group
502 (setq float-pi 3)
503 @result{} float-pi
504 @end group
505 @group
506 float-pi
507 @result{} 3
508 @end group
509 @end example
510 @end defspec
511
512 @strong{Warning:} If you use a @code{defconst} or @code{defvar}
513 special form while the variable has a local binding (made with
514 @code{let}, or a function argument), it sets the local binding rather
515 than the global binding. This is not what you usually want. To
516 prevent this, use these special forms at top level in a file, where
517 normally no local binding is in effect, and make sure to load the file
518 before making a local binding for the variable.
519
520 @node Tips for Defining
521 @section Tips for Defining Variables Robustly
522
523 When you define a variable whose value is a function, or a list of
524 functions, use a name that ends in @samp{-function} or
525 @samp{-functions}, respectively.
526
527 There are several other variable name conventions;
528 here is a complete list:
529
530 @table @samp
531 @item @dots{}-hook
532 The variable is a normal hook (@pxref{Hooks}).
533
534 @item @dots{}-function
535 The value is a function.
536
537 @item @dots{}-functions
538 The value is a list of functions.
539
540 @item @dots{}-form
541 The value is a form (an expression).
542
543 @item @dots{}-forms
544 The value is a list of forms (expressions).
545
546 @item @dots{}-predicate
547 The value is a predicate---a function of one argument that returns
548 non-@code{nil} for success and @code{nil} for failure.
549
550 @item @dots{}-flag
551 The value is significant only as to whether it is @code{nil} or not.
552 Since such variables often end up acquiring more values over time,
553 this convention is not strongly recommended.
554
555 @item @dots{}-program
556 The value is a program name.
557
558 @item @dots{}-command
559 The value is a whole shell command.
560
561 @item @dots{}-switches
562 The value specifies options for a command.
563 @end table
564
565 When you define a variable, always consider whether you should mark
566 it as safe or risky; see @ref{File Local Variables}.
567
568 When defining and initializing a variable that holds a complicated
569 value (such as a keymap with bindings in it), it's best to put the
570 entire computation of the value into the @code{defvar}, like this:
571
572 @example
573 (defvar my-mode-map
574 (let ((map (make-sparse-keymap)))
575 (define-key map "\C-c\C-a" 'my-command)
576 @dots{}
577 map)
578 @var{docstring})
579 @end example
580
581 @noindent
582 This method has several benefits. First, if the user quits while
583 loading the file, the variable is either still uninitialized or
584 initialized properly, never in-between. If it is still uninitialized,
585 reloading the file will initialize it properly. Second, reloading the
586 file once the variable is initialized will not alter it; that is
587 important if the user has run hooks to alter part of the contents
588 (such as, to rebind keys). Third, evaluating the @code{defvar} form
589 with @kbd{C-M-x} will reinitialize the map completely.
590
591 Putting so much code in the @code{defvar} form has one disadvantage:
592 it puts the documentation string far away from the line which names the
593 variable. Here's a safe way to avoid that:
594
595 @example
596 (defvar my-mode-map nil
597 @var{docstring})
598 (unless my-mode-map
599 (let ((map (make-sparse-keymap)))
600 (define-key map "\C-c\C-a" 'my-command)
601 @dots{}
602 (setq my-mode-map map)))
603 @end example
604
605 @noindent
606 This has all the same advantages as putting the initialization inside
607 the @code{defvar}, except that you must type @kbd{C-M-x} twice, once on
608 each form, if you do want to reinitialize the variable.
609
610 @node Accessing Variables
611 @section Accessing Variable Values
612
613 The usual way to reference a variable is to write the symbol which
614 names it. @xref{Symbol Forms}.
615
616 Occasionally, you may want to reference a variable which is only
617 determined at run time. In that case, you cannot specify the variable
618 name in the text of the program. You can use the @code{symbol-value}
619 function to extract the value.
620
621 @defun symbol-value symbol
622 This function returns the value stored in @var{symbol}'s value cell.
623 This is where the variable's current (dynamic) value is stored. If
624 the variable has no local binding, this is simply its global value.
625 If the variable is void, a @code{void-variable} error is signaled.
626
627 If the variable is lexically bound, the value reported by
628 @code{symbol-value} is not necessarily the same as the variable's
629 lexical value, which is determined by the lexical environment rather
630 than the symbol's value cell. @xref{Variable Scoping}.
631
632 @example
633 @group
634 (setq abracadabra 5)
635 @result{} 5
636 @end group
637 @group
638 (setq foo 9)
639 @result{} 9
640 @end group
641
642 @group
643 ;; @r{Here the symbol @code{abracadabra}}
644 ;; @r{is the symbol whose value is examined.}
645 (let ((abracadabra 'foo))
646 (symbol-value 'abracadabra))
647 @result{} foo
648 @end group
649
650 @group
651 ;; @r{Here, the value of @code{abracadabra},}
652 ;; @r{which is @code{foo},}
653 ;; @r{is the symbol whose value is examined.}
654 (let ((abracadabra 'foo))
655 (symbol-value abracadabra))
656 @result{} 9
657 @end group
658
659 @group
660 (symbol-value 'abracadabra)
661 @result{} 5
662 @end group
663 @end example
664 @end defun
665
666 @node Setting Variables
667 @section Setting Variable Values
668
669 The usual way to change the value of a variable is with the special
670 form @code{setq}. When you need to compute the choice of variable at
671 run time, use the function @code{set}.
672
673 @defspec setq [symbol form]@dots{}
674 This special form is the most common method of changing a variable's
675 value. Each @var{symbol} is given a new value, which is the result of
676 evaluating the corresponding @var{form}. The current binding of the
677 symbol is changed.
678
679 @code{setq} does not evaluate @var{symbol}; it sets the symbol that you
680 write. We say that this argument is @dfn{automatically quoted}. The
681 @samp{q} in @code{setq} stands for ``quoted''.
682
683 The value of the @code{setq} form is the value of the last @var{form}.
684
685 @example
686 @group
687 (setq x (1+ 2))
688 @result{} 3
689 @end group
690 x ; @r{@code{x} now has a global value.}
691 @result{} 3
692 @group
693 (let ((x 5))
694 (setq x 6) ; @r{The local binding of @code{x} is set.}
695 x)
696 @result{} 6
697 @end group
698 x ; @r{The global value is unchanged.}
699 @result{} 3
700 @end example
701
702 Note that the first @var{form} is evaluated, then the first
703 @var{symbol} is set, then the second @var{form} is evaluated, then the
704 second @var{symbol} is set, and so on:
705
706 @example
707 @group
708 (setq x 10 ; @r{Notice that @code{x} is set before}
709 y (1+ x)) ; @r{the value of @code{y} is computed.}
710 @result{} 11
711 @end group
712 @end example
713 @end defspec
714
715 @defun set symbol value
716 This function puts @var{value} in the value cell of @var{symbol}.
717 Since it is a function rather than a special form, the expression
718 written for @var{symbol} is evaluated to obtain the symbol to set.
719 The return value is @var{value}.
720
721 When dynamic variable binding is in effect (the default), @code{set}
722 has the same effect as @code{setq}, apart from the fact that
723 @code{set} evaluates its @var{symbol} argument whereas @code{setq}
724 does not. But when a variable is lexically bound, @code{set} affects
725 its @emph{dynamic} value, whereas @code{setq} affects its current
726 (lexical) value. @xref{Variable Scoping}.
727
728 @example
729 @group
730 (set one 1)
731 @error{} Symbol's value as variable is void: one
732 @end group
733 @group
734 (set 'one 1)
735 @result{} 1
736 @end group
737 @group
738 (set 'two 'one)
739 @result{} one
740 @end group
741 @group
742 (set two 2) ; @r{@code{two} evaluates to symbol @code{one}.}
743 @result{} 2
744 @end group
745 @group
746 one ; @r{So it is @code{one} that was set.}
747 @result{} 2
748 (let ((one 1)) ; @r{This binding of @code{one} is set,}
749 (set 'one 3) ; @r{not the global value.}
750 one)
751 @result{} 3
752 @end group
753 @group
754 one
755 @result{} 2
756 @end group
757 @end example
758
759 If @var{symbol} is not actually a symbol, a @code{wrong-type-argument}
760 error is signaled.
761
762 @example
763 (set '(x y) 'z)
764 @error{} Wrong type argument: symbolp, (x y)
765 @end example
766 @end defun
767
768 @node Variable Scoping
769 @section Scoping Rules for Variable Bindings
770 @cindex scoping rule
771
772 When you create a local binding for a variable, that binding takes
773 effect only within a limited portion of the program (@pxref{Local
774 Variables}). This section describes exactly what this means.
775
776 @cindex scope
777 @cindex extent
778 Each local binding has a certain @dfn{scope} and @dfn{extent}.
779 @dfn{Scope} refers to @emph{where} in the textual source code the
780 binding can be accessed. @dfn{Extent} refers to @emph{when}, as the
781 program is executing, the binding exists.
782
783 @cindex dynamic binding
784 @cindex dynamic scope
785 @cindex dynamic extent
786 By default, the local bindings that Emacs creates are @dfn{dynamic
787 bindings}. Such a binding has @dfn{dynamic scope}, meaning that any
788 part of the program can potentially access the variable binding. It
789 also has @dfn{dynamic extent}, meaning that the binding lasts only
790 while the binding construct (such as the body of a @code{let} form) is
791 being executed.
792
793 @cindex lexical binding
794 @cindex lexical scope
795 @cindex indefinite extent
796 Emacs can optionally create @dfn{lexical bindings}. A lexical
797 binding has @dfn{lexical scope}, meaning that any reference to the
798 variable must be located textually within the binding
799 construct@footnote{With some exceptions; for instance, a lexical
800 binding can also be accessed from the Lisp debugger.}. It also has
801 @dfn{indefinite extent}, meaning that under some circumstances the
802 binding can live on even after the binding construct has finished
803 executing, by means of special objects called @dfn{closures}.
804
805 The following subsections describe dynamic binding and lexical
806 binding in greater detail, and how to enable lexical binding in Emacs
807 Lisp programs.
808
809 @menu
810 * Dynamic Binding:: The default for binding local variables in Emacs.
811 * Dynamic Binding Tips:: Avoiding problems with dynamic binding.
812 * Lexical Binding:: A different type of local variable binding.
813 * Using Lexical Binding:: How to enable lexical binding.
814 @end menu
815
816 @node Dynamic Binding
817 @subsection Dynamic Binding
818
819 By default, the local variable bindings made by Emacs are dynamic
820 bindings. When a variable is dynamically bound, its current binding
821 at any point in the execution of the Lisp program is simply the most
822 recently-created dynamic local binding for that symbol, or the global
823 binding if there is no such local binding.
824
825 Dynamic bindings have dynamic scope and extent, as shown by the
826 following example:
827
828 @example
829 @group
830 (defvar x -99) ; @r{@code{x} receives an initial value of @minus{}99.}
831
832 (defun getx ()
833 x) ; @r{@code{x} is used free in this function.}
834
835 (let ((x 1)) ; @r{@code{x} is dynamically bound.}
836 (getx))
837 @result{} 1
838
839 ;; @r{After the @code{let} form finishes, @code{x} reverts to its}
840 ;; @r{previous value, which is @minus{}99.}
841
842 (getx)
843 @result{} -99
844 @end group
845 @end example
846
847 @noindent
848 The function @code{getx} refers to @code{x}. This is a @dfn{free}
849 reference, in the sense that there is no binding for @code{x} within
850 that @code{defun} construct itself. When we call @code{getx} from
851 within a @code{let} form in which @code{x} is (dynamically) bound, it
852 retrieves the local value (i.e., 1). But when we call @code{getx}
853 outside the @code{let} form, it retrieves the global value (i.e.,
854 @minus{}99).
855
856 Here is another example, which illustrates setting a dynamically
857 bound variable using @code{setq}:
858
859 @example
860 @group
861 (defvar x -99) ; @r{@code{x} receives an initial value of @minus{}99.}
862
863 (defun addx ()
864 (setq x (1+ x))) ; @r{Add 1 to @code{x} and return its new value.}
865
866 (let ((x 1))
867 (addx)
868 (addx))
869 @result{} 3 ; @r{The two @code{addx} calls add to @code{x} twice.}
870
871 ;; @r{After the @code{let} form finishes, @code{x} reverts to its}
872 ;; @r{previous value, which is @minus{}99.}
873
874 (addx)
875 @result{} -98
876 @end group
877 @end example
878
879 Dynamic binding is implemented in Emacs Lisp in a simple way. Each
880 symbol has a value cell, which specifies its current dynamic value (or
881 absence of value). @xref{Symbol Components}. When a symbol is given
882 a dynamic local binding, Emacs records the contents of the value cell
883 (or absence thereof) in a stack, and stores the new local value in the
884 value cell. When the binding construct finishes executing, Emacs pops
885 the old value off the stack, and puts it in the value cell.
886
887 @node Dynamic Binding Tips
888 @subsection Proper Use of Dynamic Binding
889
890 Dynamic binding is a powerful feature, as it allows programs to
891 refer to variables that are not defined within their local textual
892 scope. However, if used without restraint, this can also make
893 programs hard to understand. There are two clean ways to use this
894 technique:
895
896 @itemize @bullet
897 @item
898 If a variable has no global definition, use it as a local variable
899 only within a binding construct, such as the body of the @code{let}
900 form where the variable was bound. If this convention is followed
901 consistently throughout a program, the value of the variable will not
902 affect, nor be affected by, any uses of the same variable symbol
903 elsewhere in the program.
904
905 @item
906 Otherwise, define the variable with @code{defvar}, @code{defconst}, or
907 @code{defcustom}. @xref{Defining Variables}. Usually, the definition
908 should be at top-level in an Emacs Lisp file. As far as possible, it
909 should include a documentation string which explains the meaning and
910 purpose of the variable. You should also choose the variable's name
911 to avoid name conflicts (@pxref{Coding Conventions}).
912
913 Then you can bind the variable anywhere in a program, knowing reliably
914 what the effect will be. Wherever you encounter the variable, it will
915 be easy to refer back to the definition, e.g., via the @kbd{C-h v}
916 command (provided the variable definition has been loaded into Emacs).
917 @xref{Name Help,,, emacs, The GNU Emacs Manual}.
918
919 For example, it is common to use local bindings for customizable
920 variables like @code{case-fold-search}:
921
922 @example
923 @group
924 (defun search-for-abc ()
925 "Search for the string \"abc\", ignoring case differences."
926 (let ((case-fold-search nil))
927 (re-search-forward "abc")))
928 @end group
929 @end example
930 @end itemize
931
932 @node Lexical Binding
933 @subsection Lexical Binding
934
935 Lexical binding was introduced to Emacs, as an optional feature, in
936 version 24.1. We expect its importance to increase in the future.
937 Lexical binding opens up many more opportunities for optimization, so
938 programs using it are likely to run faster in future Emacs versions.
939 Lexical binding is also more compatible with concurrency, which we
940 want to add to Emacs in the future.
941
942 A lexically-bound variable has @dfn{lexical scope}, meaning that any
943 reference to the variable must be located textually within the binding
944 construct. Here is an example
945 @iftex
946 (see the next subsection, for how to actually enable lexical binding):
947 @end iftex
948 @ifnottex
949 (@pxref{Using Lexical Binding}, for how to actually enable lexical binding):
950 @end ifnottex
951
952 @example
953 @group
954 (let ((x 1)) ; @r{@code{x} is lexically bound.}
955 (+ x 3))
956 @result{} 4
957
958 (defun getx ()
959 x) ; @r{@code{x} is used free in this function.}
960
961 (let ((x 1)) ; @r{@code{x} is lexically bound.}
962 (getx))
963 @error{} Symbol's value as variable is void: x
964 @end group
965 @end example
966
967 @noindent
968 Here, the variable @code{x} has no global value. When it is lexically
969 bound within a @code{let} form, it can be used in the textual confines
970 of that @code{let} form. But it can @emph{not} be used from within a
971 @code{getx} function called from the @code{let} form, since the
972 function definition of @code{getx} occurs outside the @code{let} form
973 itself.
974
975 @cindex lexical environment
976 Here is how lexical binding works. Each binding construct defines a
977 @dfn{lexical environment}, specifying the symbols that are bound
978 within the construct and their local values. When the Lisp evaluator
979 wants the current value of a variable, it looks first in the lexical
980 environment; if the variable is not specified in there, it looks in
981 the symbol's value cell, where the dynamic value is stored.
982
983 (Internally, the lexical environment is an alist of symbol-value
984 pairs, with the final element in the alist being the symbol @code{t}
985 rather than a cons cell. Such an alist can be passed as the second
986 argument to the @code{eval} function, in order to specify a lexical
987 environment in which to evaluate a form. @xref{Eval}. Most Emacs
988 Lisp programs, however, should not interact directly with lexical
989 environments in this way; only specialized programs like debuggers.)
990
991 @cindex closures, example of using
992 Lexical bindings have indefinite extent. Even after a binding
993 construct has finished executing, its lexical environment can be
994 ``kept around'' in Lisp objects called @dfn{closures}. A closure is
995 created when you define a named or anonymous function with lexical
996 binding enabled. @xref{Closures}, for details.
997
998 When a closure is called as a function, any lexical variable
999 references within its definition use the retained lexical environment.
1000 Here is an example:
1001
1002 @example
1003 (defvar my-ticker nil) ; @r{We will use this dynamically bound}
1004 ; @r{variable to store a closure.}
1005
1006 (let ((x 0)) ; @r{@code{x} is lexically bound.}
1007 (setq my-ticker (lambda ()
1008 (setq x (1+ x)))))
1009 @result{} (closure ((x . 0) t) ()
1010 (setq x (1+ x)))
1011
1012 (funcall my-ticker)
1013 @result{} 1
1014
1015 (funcall my-ticker)
1016 @result{} 2
1017
1018 (funcall my-ticker)
1019 @result{} 3
1020
1021 x ; @r{Note that @code{x} has no global value.}
1022 @error{} Symbol's value as variable is void: x
1023 @end example
1024
1025 @noindent
1026 The @code{let} binding defines a lexical environment in which the
1027 variable @code{x} is locally bound to 0. Within this binding
1028 construct, we define a lambda expression which increments @code{x} by
1029 one and returns the incremented value. This lambda expression is
1030 automatically turned into a closure, in which the lexical environment
1031 lives on even after the @code{let} binding construct has exited. Each
1032 time we evaluate the closure, it increments @code{x}, using the
1033 binding of @code{x} in that lexical environment.
1034
1035 Note that functions like @code{symbol-value}, @code{boundp}, and
1036 @code{set} only retrieve or modify a variable's dynamic binding
1037 (i.e., the contents of its symbol's value cell). Also, the code in
1038 the body of a @code{defun} or @code{defmacro} cannot refer to
1039 surrounding lexical variables.
1040
1041 @node Using Lexical Binding
1042 @subsection Using Lexical Binding
1043
1044 When loading an Emacs Lisp file or evaluating a Lisp buffer, lexical
1045 binding is enabled if the buffer-local variable @code{lexical-binding}
1046 is non-@code{nil}:
1047
1048 @defvar lexical-binding
1049 If this buffer-local variable is non-@code{nil}, Emacs Lisp files and
1050 buffers are evaluated using lexical binding instead of dynamic
1051 binding. (However, special variables are still dynamically bound; see
1052 below.) If @code{nil}, dynamic binding is used for all local
1053 variables. This variable is typically set for a whole Emacs Lisp
1054 file, as a file local variable (@pxref{File Local Variables}).
1055 Note that unlike other such variables, this one must be set in the
1056 first line of a file.
1057 @end defvar
1058
1059 @noindent
1060 When evaluating Emacs Lisp code directly using an @code{eval} call,
1061 lexical binding is enabled if the @var{lexical} argument to
1062 @code{eval} is non-@code{nil}. @xref{Eval}.
1063
1064 @cindex special variables
1065 Even when lexical binding is enabled, certain variables will
1066 continue to be dynamically bound. These are called @dfn{special
1067 variables}. Every variable that has been defined with @code{defvar},
1068 @code{defcustom} or @code{defconst} is a special variable
1069 (@pxref{Defining Variables}). All other variables are subject to
1070 lexical binding.
1071
1072 @defun special-variable-p symbol
1073 This function returns non-@code{nil} if @var{symbol} is a special
1074 variable (i.e., it has a @code{defvar}, @code{defcustom}, or
1075 @code{defconst} variable definition). Otherwise, the return value is
1076 @code{nil}.
1077 @end defun
1078
1079 The use of a special variable as a formal argument in a function is
1080 discouraged. Doing so gives rise to unspecified behavior when lexical
1081 binding mode is enabled (it may use lexical binding sometimes, and
1082 dynamic binding other times).
1083
1084 Converting an Emacs Lisp program to lexical binding is easy. First,
1085 add a file-local variable setting of @code{lexical-binding} to
1086 @code{t} in the header line of the Emacs Lisp source file (@pxref{File
1087 Local Variables}). Second, check that every variable in the program
1088 which needs to be dynamically bound has a variable definition, so that
1089 it is not inadvertently bound lexically.
1090
1091 @cindex free variable
1092 @cindex unused lexical variable
1093 A simple way to find out which variables need a variable definition
1094 is to byte-compile the source file. @xref{Byte Compilation}. If a
1095 non-special variable is used outside of a @code{let} form, the
1096 byte-compiler will warn about reference or assignment to a free
1097 variable. If a non-special variable is bound but not used within a
1098 @code{let} form, the byte-compiler will warn about an unused lexical
1099 variable. The byte-compiler will also issue a warning if you use a
1100 special variable as a function argument.
1101
1102 (To silence byte-compiler warnings about unused variables, just use
1103 a variable name that start with an underscore. The byte-compiler
1104 interprets this as an indication that this is a variable known not to
1105 be used.)
1106
1107 @node Buffer-Local Variables
1108 @section Buffer-Local Variables
1109 @cindex variable, buffer-local
1110 @cindex buffer-local variables
1111
1112 Global and local variable bindings are found in most programming
1113 languages in one form or another. Emacs, however, also supports
1114 additional, unusual kinds of variable binding, such as
1115 @dfn{buffer-local} bindings, which apply only in one buffer. Having
1116 different values for a variable in different buffers is an important
1117 customization method. (Variables can also have bindings that are
1118 local to each terminal. @xref{Multiple Terminals}.)
1119
1120 @menu
1121 * Intro to Buffer-Local:: Introduction and concepts.
1122 * Creating Buffer-Local:: Creating and destroying buffer-local bindings.
1123 * Default Value:: The default value is seen in buffers
1124 that don't have their own buffer-local values.
1125 @end menu
1126
1127 @node Intro to Buffer-Local
1128 @subsection Introduction to Buffer-Local Variables
1129
1130 A buffer-local variable has a buffer-local binding associated with a
1131 particular buffer. The binding is in effect when that buffer is
1132 current; otherwise, it is not in effect. If you set the variable while
1133 a buffer-local binding is in effect, the new value goes in that binding,
1134 so its other bindings are unchanged. This means that the change is
1135 visible only in the buffer where you made it.
1136
1137 The variable's ordinary binding, which is not associated with any
1138 specific buffer, is called the @dfn{default binding}. In most cases,
1139 this is the global binding.
1140
1141 A variable can have buffer-local bindings in some buffers but not in
1142 other buffers. The default binding is shared by all the buffers that
1143 don't have their own bindings for the variable. (This includes all
1144 newly-created buffers.) If you set the variable in a buffer that does
1145 not have a buffer-local binding for it, this sets the default binding,
1146 so the new value is visible in all the buffers that see the default
1147 binding.
1148
1149 The most common use of buffer-local bindings is for major modes to change
1150 variables that control the behavior of commands. For example, C mode and
1151 Lisp mode both set the variable @code{paragraph-start} to specify that only
1152 blank lines separate paragraphs. They do this by making the variable
1153 buffer-local in the buffer that is being put into C mode or Lisp mode, and
1154 then setting it to the new value for that mode. @xref{Major Modes}.
1155
1156 The usual way to make a buffer-local binding is with
1157 @code{make-local-variable}, which is what major mode commands typically
1158 use. This affects just the current buffer; all other buffers (including
1159 those yet to be created) will continue to share the default value unless
1160 they are explicitly given their own buffer-local bindings.
1161
1162 @cindex automatically buffer-local
1163 A more powerful operation is to mark the variable as
1164 @dfn{automatically buffer-local} by calling
1165 @code{make-variable-buffer-local}. You can think of this as making the
1166 variable local in all buffers, even those yet to be created. More
1167 precisely, the effect is that setting the variable automatically makes
1168 the variable local to the current buffer if it is not already so. All
1169 buffers start out by sharing the default value of the variable as usual,
1170 but setting the variable creates a buffer-local binding for the current
1171 buffer. The new value is stored in the buffer-local binding, leaving
1172 the default binding untouched. This means that the default value cannot
1173 be changed with @code{setq} in any buffer; the only way to change it is
1174 with @code{setq-default}.
1175
1176 @strong{Warning:} When a variable has buffer-local
1177 bindings in one or more buffers, @code{let} rebinds the binding that's
1178 currently in effect. For instance, if the current buffer has a
1179 buffer-local value, @code{let} temporarily rebinds that. If no
1180 buffer-local bindings are in effect, @code{let} rebinds
1181 the default value. If inside the @code{let} you then change to a
1182 different current buffer in which a different binding is in effect,
1183 you won't see the @code{let} binding any more. And if you exit the
1184 @code{let} while still in the other buffer, you won't see the
1185 unbinding occur (though it will occur properly). Here is an example
1186 to illustrate:
1187
1188 @example
1189 @group
1190 (setq foo 'g)
1191 (set-buffer "a")
1192 (make-local-variable 'foo)
1193 @end group
1194 (setq foo 'a)
1195 (let ((foo 'temp))
1196 ;; foo @result{} 'temp ; @r{let binding in buffer @samp{a}}
1197 (set-buffer "b")
1198 ;; foo @result{} 'g ; @r{the global value since foo is not local in @samp{b}}
1199 @var{body}@dots{})
1200 @group
1201 foo @result{} 'g ; @r{exiting restored the local value in buffer @samp{a},}
1202 ; @r{but we don't see that in buffer @samp{b}}
1203 @end group
1204 @group
1205 (set-buffer "a") ; @r{verify the local value was restored}
1206 foo @result{} 'a
1207 @end group
1208 @end example
1209
1210 @noindent
1211 Note that references to @code{foo} in @var{body} access the
1212 buffer-local binding of buffer @samp{b}.
1213
1214 When a file specifies local variable values, these become buffer-local
1215 values when you visit the file. @xref{File Variables,,, emacs, The
1216 GNU Emacs Manual}.
1217
1218 A buffer-local variable cannot be made terminal-local
1219 (@pxref{Multiple Terminals}).
1220
1221 @node Creating Buffer-Local
1222 @subsection Creating and Deleting Buffer-Local Bindings
1223
1224 @deffn Command make-local-variable variable
1225 This function creates a buffer-local binding in the current buffer for
1226 @var{variable} (a symbol). Other buffers are not affected. The value
1227 returned is @var{variable}.
1228
1229 The buffer-local value of @var{variable} starts out as the same value
1230 @var{variable} previously had. If @var{variable} was void, it remains
1231 void.
1232
1233 @example
1234 @group
1235 ;; @r{In buffer @samp{b1}:}
1236 (setq foo 5) ; @r{Affects all buffers.}
1237 @result{} 5
1238 @end group
1239 @group
1240 (make-local-variable 'foo) ; @r{Now it is local in @samp{b1}.}
1241 @result{} foo
1242 @end group
1243 @group
1244 foo ; @r{That did not change}
1245 @result{} 5 ; @r{the value.}
1246 @end group
1247 @group
1248 (setq foo 6) ; @r{Change the value}
1249 @result{} 6 ; @r{in @samp{b1}.}
1250 @end group
1251 @group
1252 foo
1253 @result{} 6
1254 @end group
1255
1256 @group
1257 ;; @r{In buffer @samp{b2}, the value hasn't changed.}
1258 (with-current-buffer "b2"
1259 foo)
1260 @result{} 5
1261 @end group
1262 @end example
1263
1264 Making a variable buffer-local within a @code{let}-binding for that
1265 variable does not work reliably, unless the buffer in which you do this
1266 is not current either on entry to or exit from the @code{let}. This is
1267 because @code{let} does not distinguish between different kinds of
1268 bindings; it knows only which variable the binding was made for.
1269
1270 If the variable is terminal-local (@pxref{Multiple Terminals}), this
1271 function signals an error. Such variables cannot have buffer-local
1272 bindings as well.
1273
1274 @strong{Warning:} do not use @code{make-local-variable} for a hook
1275 variable. The hook variables are automatically made buffer-local as
1276 needed if you use the @var{local} argument to @code{add-hook} or
1277 @code{remove-hook}.
1278 @end deffn
1279
1280 @defmac setq-local variable value
1281 This macro creates a buffer-local binding in the current buffer for
1282 @var{variable}, and gives it the buffer-local value @var{value}. It
1283 is equivalent to calling @code{make-local-variable} followed by
1284 @code{setq}. @var{variable} should be an unquoted symbol.
1285 @end defmac
1286
1287 @deffn Command make-variable-buffer-local variable
1288 This function marks @var{variable} (a symbol) automatically
1289 buffer-local, so that any subsequent attempt to set it will make it
1290 local to the current buffer at the time. Unlike
1291 @code{make-local-variable}, with which it is often confused, this
1292 cannot be undone, and affects the behavior of the variable in all
1293 buffers.
1294
1295 A peculiar wrinkle of this feature is that binding the variable (with
1296 @code{let} or other binding constructs) does not create a buffer-local
1297 binding for it. Only setting the variable (with @code{set} or
1298 @code{setq}), while the variable does not have a @code{let}-style
1299 binding that was made in the current buffer, does so.
1300
1301 If @var{variable} does not have a default value, then calling this
1302 command will give it a default value of @code{nil}. If @var{variable}
1303 already has a default value, that value remains unchanged.
1304 Subsequently calling @code{makunbound} on @var{variable} will result
1305 in a void buffer-local value and leave the default value unaffected.
1306
1307 The value returned is @var{variable}.
1308
1309 @strong{Warning:} Don't assume that you should use
1310 @code{make-variable-buffer-local} for user-option variables, simply
1311 because users @emph{might} want to customize them differently in
1312 different buffers. Users can make any variable local, when they wish
1313 to. It is better to leave the choice to them.
1314
1315 The time to use @code{make-variable-buffer-local} is when it is crucial
1316 that no two buffers ever share the same binding. For example, when a
1317 variable is used for internal purposes in a Lisp program which depends
1318 on having separate values in separate buffers, then using
1319 @code{make-variable-buffer-local} can be the best solution.
1320 @end deffn
1321
1322 @defmac defvar-local variable value &optional docstring
1323 This macro defines @var{variable} as a variable with initial value
1324 @var{value} and @var{docstring}, and marks it as automatically
1325 buffer-local. It is equivalent to calling @code{defvar} followed by
1326 @code{make-variable-buffer-local}. @var{variable} should be an
1327 unquoted symbol.
1328 @end defmac
1329
1330 @defun local-variable-p variable &optional buffer
1331 This returns @code{t} if @var{variable} is buffer-local in buffer
1332 @var{buffer} (which defaults to the current buffer); otherwise,
1333 @code{nil}.
1334 @end defun
1335
1336 @defun local-variable-if-set-p variable &optional buffer
1337 This returns @code{t} if @var{variable} either has a buffer-local
1338 value in buffer @var{buffer}, or is automatically buffer-local.
1339 Otherwise, it returns @code{nil}. If omitted or @code{nil},
1340 @var{buffer} defaults to the current buffer.
1341 @end defun
1342
1343 @defun buffer-local-value variable buffer
1344 This function returns the buffer-local binding of @var{variable} (a
1345 symbol) in buffer @var{buffer}. If @var{variable} does not have a
1346 buffer-local binding in buffer @var{buffer}, it returns the default
1347 value (@pxref{Default Value}) of @var{variable} instead.
1348 @end defun
1349
1350 @defun buffer-local-variables &optional buffer
1351 This function returns a list describing the buffer-local variables in
1352 buffer @var{buffer}. (If @var{buffer} is omitted, the current buffer
1353 is used.) Normally, each list element has the form
1354 @w{@code{(@var{sym} . @var{val})}}, where @var{sym} is a buffer-local
1355 variable (a symbol) and @var{val} is its buffer-local value. But when
1356 a variable's buffer-local binding in @var{buffer} is void, its list
1357 element is just @var{sym}.
1358
1359 @example
1360 @group
1361 (make-local-variable 'foobar)
1362 (makunbound 'foobar)
1363 (make-local-variable 'bind-me)
1364 (setq bind-me 69)
1365 @end group
1366 (setq lcl (buffer-local-variables))
1367 ;; @r{First, built-in variables local in all buffers:}
1368 @result{} ((mark-active . nil)
1369 (buffer-undo-list . nil)
1370 (mode-name . "Fundamental")
1371 @dots{}
1372 @group
1373 ;; @r{Next, non-built-in buffer-local variables.}
1374 ;; @r{This one is buffer-local and void:}
1375 foobar
1376 ;; @r{This one is buffer-local and nonvoid:}
1377 (bind-me . 69))
1378 @end group
1379 @end example
1380
1381 Note that storing new values into the @sc{cdr}s of cons cells in this
1382 list does @emph{not} change the buffer-local values of the variables.
1383 @end defun
1384
1385 @deffn Command kill-local-variable variable
1386 This function deletes the buffer-local binding (if any) for
1387 @var{variable} (a symbol) in the current buffer. As a result, the
1388 default binding of @var{variable} becomes visible in this buffer. This
1389 typically results in a change in the value of @var{variable}, since the
1390 default value is usually different from the buffer-local value just
1391 eliminated.
1392
1393 If you kill the buffer-local binding of a variable that automatically
1394 becomes buffer-local when set, this makes the default value visible in
1395 the current buffer. However, if you set the variable again, that will
1396 once again create a buffer-local binding for it.
1397
1398 @code{kill-local-variable} returns @var{variable}.
1399
1400 This function is a command because it is sometimes useful to kill one
1401 buffer-local variable interactively, just as it is useful to create
1402 buffer-local variables interactively.
1403 @end deffn
1404
1405 @cindex local variables, killed by major mode
1406 @defun kill-all-local-variables
1407 This function eliminates all the buffer-local variable bindings of the
1408 current buffer except for variables marked as permanent and local
1409 hook functions that have a non-@code{nil} @code{permanent-local-hook}
1410 property (@pxref{Setting Hooks}). As a result, the buffer will see
1411 the default values of most variables.
1412
1413 This function also resets certain other information pertaining to the
1414 buffer: it sets the local keymap to @code{nil}, the syntax table to the
1415 value of @code{(standard-syntax-table)}, the case table to
1416 @code{(standard-case-table)}, and the abbrev table to the value of
1417 @code{fundamental-mode-abbrev-table}.
1418
1419 The very first thing this function does is run the normal hook
1420 @code{change-major-mode-hook} (see below).
1421
1422 Every major mode command begins by calling this function, which has the
1423 effect of switching to Fundamental mode and erasing most of the effects
1424 of the previous major mode. To ensure that this does its job, the
1425 variables that major modes set should not be marked permanent.
1426
1427 @code{kill-all-local-variables} returns @code{nil}.
1428 @end defun
1429
1430 @defvar change-major-mode-hook
1431 The function @code{kill-all-local-variables} runs this normal hook
1432 before it does anything else. This gives major modes a way to arrange
1433 for something special to be done if the user switches to a different
1434 major mode. It is also useful for buffer-specific minor modes
1435 that should be forgotten if the user changes the major mode.
1436
1437 For best results, make this variable buffer-local, so that it will
1438 disappear after doing its job and will not interfere with the
1439 subsequent major mode. @xref{Hooks}.
1440 @end defvar
1441
1442 @cindex permanent local variable
1443 A buffer-local variable is @dfn{permanent} if the variable name (a
1444 symbol) has a @code{permanent-local} property that is non-@code{nil}.
1445 Such variables are unaffected by @code{kill-all-local-variables}, and
1446 their local bindings are therefore not cleared by changing major modes.
1447 Permanent locals are appropriate for data pertaining to where the file
1448 came from or how to save it, rather than with how to edit the contents.
1449
1450 @node Default Value
1451 @subsection The Default Value of a Buffer-Local Variable
1452 @cindex default value
1453
1454 The global value of a variable with buffer-local bindings is also
1455 called the @dfn{default} value, because it is the value that is in
1456 effect whenever neither the current buffer nor the selected frame has
1457 its own binding for the variable.
1458
1459 The functions @code{default-value} and @code{setq-default} access and
1460 change a variable's default value regardless of whether the current
1461 buffer has a buffer-local binding. For example, you could use
1462 @code{setq-default} to change the default setting of
1463 @code{paragraph-start} for most buffers; and this would work even when
1464 you are in a C or Lisp mode buffer that has a buffer-local value for
1465 this variable.
1466
1467 @c Emacs 19 feature
1468 The special forms @code{defvar} and @code{defconst} also set the
1469 default value (if they set the variable at all), rather than any
1470 buffer-local value.
1471
1472 @defun default-value symbol
1473 This function returns @var{symbol}'s default value. This is the value
1474 that is seen in buffers and frames that do not have their own values for
1475 this variable. If @var{symbol} is not buffer-local, this is equivalent
1476 to @code{symbol-value} (@pxref{Accessing Variables}).
1477 @end defun
1478
1479 @c Emacs 19 feature
1480 @defun default-boundp symbol
1481 The function @code{default-boundp} tells you whether @var{symbol}'s
1482 default value is nonvoid. If @code{(default-boundp 'foo)} returns
1483 @code{nil}, then @code{(default-value 'foo)} would get an error.
1484
1485 @code{default-boundp} is to @code{default-value} as @code{boundp} is to
1486 @code{symbol-value}.
1487 @end defun
1488
1489 @defspec setq-default [symbol form]@dots{}
1490 This special form gives each @var{symbol} a new default value, which is
1491 the result of evaluating the corresponding @var{form}. It does not
1492 evaluate @var{symbol}, but does evaluate @var{form}. The value of the
1493 @code{setq-default} form is the value of the last @var{form}.
1494
1495 If a @var{symbol} is not buffer-local for the current buffer, and is not
1496 marked automatically buffer-local, @code{setq-default} has the same
1497 effect as @code{setq}. If @var{symbol} is buffer-local for the current
1498 buffer, then this changes the value that other buffers will see (as long
1499 as they don't have a buffer-local value), but not the value that the
1500 current buffer sees.
1501
1502 @example
1503 @group
1504 ;; @r{In buffer @samp{foo}:}
1505 (make-local-variable 'buffer-local)
1506 @result{} buffer-local
1507 @end group
1508 @group
1509 (setq buffer-local 'value-in-foo)
1510 @result{} value-in-foo
1511 @end group
1512 @group
1513 (setq-default buffer-local 'new-default)
1514 @result{} new-default
1515 @end group
1516 @group
1517 buffer-local
1518 @result{} value-in-foo
1519 @end group
1520 @group
1521 (default-value 'buffer-local)
1522 @result{} new-default
1523 @end group
1524
1525 @group
1526 ;; @r{In (the new) buffer @samp{bar}:}
1527 buffer-local
1528 @result{} new-default
1529 @end group
1530 @group
1531 (default-value 'buffer-local)
1532 @result{} new-default
1533 @end group
1534 @group
1535 (setq buffer-local 'another-default)
1536 @result{} another-default
1537 @end group
1538 @group
1539 (default-value 'buffer-local)
1540 @result{} another-default
1541 @end group
1542
1543 @group
1544 ;; @r{Back in buffer @samp{foo}:}
1545 buffer-local
1546 @result{} value-in-foo
1547 (default-value 'buffer-local)
1548 @result{} another-default
1549 @end group
1550 @end example
1551 @end defspec
1552
1553 @defun set-default symbol value
1554 This function is like @code{setq-default}, except that @var{symbol} is
1555 an ordinary evaluated argument.
1556
1557 @example
1558 @group
1559 (set-default (car '(a b c)) 23)
1560 @result{} 23
1561 @end group
1562 @group
1563 (default-value 'a)
1564 @result{} 23
1565 @end group
1566 @end example
1567 @end defun
1568
1569 @node File Local Variables
1570 @section File Local Variables
1571 @cindex file local variables
1572
1573 A file can specify local variable values; Emacs uses these to create
1574 buffer-local bindings for those variables in the buffer visiting that
1575 file. @xref{File Variables, , Local Variables in Files, emacs, The
1576 GNU Emacs Manual}, for basic information about file-local variables.
1577 This section describes the functions and variables that affect how
1578 file-local variables are processed.
1579
1580 If a file-local variable could specify an arbitrary function or Lisp
1581 expression that would be called later, visiting a file could take over
1582 your Emacs. Emacs protects against this by automatically setting only
1583 those file-local variables whose specified values are known to be
1584 safe. Other file-local variables are set only if the user agrees.
1585
1586 For additional safety, @code{read-circle} is temporarily bound to
1587 @code{nil} when Emacs reads file-local variables (@pxref{Input
1588 Functions}). This prevents the Lisp reader from recognizing circular
1589 and shared Lisp structures (@pxref{Circular Objects}).
1590
1591 @defopt enable-local-variables
1592 This variable controls whether to process file-local variables.
1593 The possible values are:
1594
1595 @table @asis
1596 @item @code{t} (the default)
1597 Set the safe variables, and query (once) about any unsafe variables.
1598 @item @code{:safe}
1599 Set only the safe variables and do not query.
1600 @item @code{:all}
1601 Set all the variables and do not query.
1602 @item @code{nil}
1603 Don't set any variables.
1604 @item anything else
1605 Query (once) about all the variables.
1606 @end table
1607 @end defopt
1608
1609 @defvar inhibit-local-variables-regexps
1610 This is a list of regular expressions. If a file has a name
1611 matching an element of this list, then it is not scanned for
1612 any form of file-local variable. For examples of why you might want
1613 to use this, @pxref{Auto Major Mode}.
1614 @end defvar
1615
1616 @defun hack-local-variables &optional mode-only
1617 This function parses, and binds or evaluates as appropriate, any local
1618 variables specified by the contents of the current buffer. The variable
1619 @code{enable-local-variables} has its effect here. However, this
1620 function does not look for the @samp{mode:} local variable in the
1621 @w{@samp{-*-}} line. @code{set-auto-mode} does that, also taking
1622 @code{enable-local-variables} into account (@pxref{Auto Major Mode}).
1623
1624 This function works by walking the alist stored in
1625 @code{file-local-variables-alist} and applying each local variable in
1626 turn. It calls @code{before-hack-local-variables-hook} and
1627 @code{hack-local-variables-hook} before and after applying the
1628 variables, respectively. It only calls the before-hook if the alist
1629 is non-@code{nil}; it always calls the other hook. This
1630 function ignores a @samp{mode} element if it specifies the same major
1631 mode as the buffer already has.
1632
1633 If the optional argument @var{mode-only} is non-@code{nil}, then all
1634 this function does is return a symbol specifying the major mode,
1635 if the @w{@samp{-*-}} line or the local variables list specifies one,
1636 and @code{nil} otherwise. It does not set the mode nor any other
1637 file-local variable.
1638 @end defun
1639
1640 @defvar file-local-variables-alist
1641 This buffer-local variable holds the alist of file-local variable
1642 settings. Each element of the alist is of the form
1643 @w{@code{(@var{var} . @var{value})}}, where @var{var} is a symbol of
1644 the local variable and @var{value} is its value. When Emacs visits a
1645 file, it first collects all the file-local variables into this alist,
1646 and then the @code{hack-local-variables} function applies them one by
1647 one.
1648 @end defvar
1649
1650 @defvar before-hack-local-variables-hook
1651 Emacs calls this hook immediately before applying file-local variables
1652 stored in @code{file-local-variables-alist}.
1653 @end defvar
1654
1655 @defvar hack-local-variables-hook
1656 Emacs calls this hook immediately after it finishes applying
1657 file-local variables stored in @code{file-local-variables-alist}.
1658 @end defvar
1659
1660 @cindex safe local variable
1661 You can specify safe values for a variable with a
1662 @code{safe-local-variable} property. The property has to be a
1663 function of one argument; any value is safe if the function returns
1664 non-@code{nil} given that value. Many commonly-encountered file
1665 variables have @code{safe-local-variable} properties; these include
1666 @code{fill-column}, @code{fill-prefix}, and @code{indent-tabs-mode}.
1667 For boolean-valued variables that are safe, use @code{booleanp} as the
1668 property value.
1669
1670 When defining a user option using @code{defcustom}, you can set its
1671 @code{safe-local-variable} property by adding the arguments
1672 @code{:safe @var{function}} to @code{defcustom} (@pxref{Variable
1673 Definitions}).
1674
1675 @defopt safe-local-variable-values
1676 This variable provides another way to mark some variable values as
1677 safe. It is a list of cons cells @code{(@var{var} . @var{val})},
1678 where @var{var} is a variable name and @var{val} is a value which is
1679 safe for that variable.
1680
1681 When Emacs asks the user whether or not to obey a set of file-local
1682 variable specifications, the user can choose to mark them as safe.
1683 Doing so adds those variable/value pairs to
1684 @code{safe-local-variable-values}, and saves it to the user's custom
1685 file.
1686 @end defopt
1687
1688 @defun safe-local-variable-p sym val
1689 This function returns non-@code{nil} if it is safe to give @var{sym}
1690 the value @var{val}, based on the above criteria.
1691 @end defun
1692
1693 @c @cindex risky local variable Duplicates risky-local-variable
1694 Some variables are considered @dfn{risky}. If a variable is risky,
1695 it is never entered automatically into
1696 @code{safe-local-variable-values}; Emacs always queries before setting
1697 a risky variable, unless the user explicitly allows a value by
1698 customizing @code{safe-local-variable-values} directly.
1699
1700 Any variable whose name has a non-@code{nil}
1701 @code{risky-local-variable} property is considered risky. When you
1702 define a user option using @code{defcustom}, you can set its
1703 @code{risky-local-variable} property by adding the arguments
1704 @code{:risky @var{value}} to @code{defcustom} (@pxref{Variable
1705 Definitions}). In addition, any variable whose name ends in any of
1706 @samp{-command}, @samp{-frame-alist}, @samp{-function},
1707 @samp{-functions}, @samp{-hook}, @samp{-hooks}, @samp{-form},
1708 @samp{-forms}, @samp{-map}, @samp{-map-alist}, @samp{-mode-alist},
1709 @samp{-program}, or @samp{-predicate} is automatically considered
1710 risky. The variables @samp{font-lock-keywords},
1711 @samp{font-lock-keywords} followed by a digit, and
1712 @samp{font-lock-syntactic-keywords} are also considered risky.
1713
1714 @defun risky-local-variable-p sym
1715 This function returns non-@code{nil} if @var{sym} is a risky variable,
1716 based on the above criteria.
1717 @end defun
1718
1719 @defvar ignored-local-variables
1720 This variable holds a list of variables that should not be given local
1721 values by files. Any value specified for one of these variables is
1722 completely ignored.
1723 @end defvar
1724
1725 The @samp{Eval:} ``variable'' is also a potential loophole, so Emacs
1726 normally asks for confirmation before handling it.
1727
1728 @defopt enable-local-eval
1729 This variable controls processing of @samp{Eval:} in @samp{-*-} lines
1730 or local variables
1731 lists in files being visited. A value of @code{t} means process them
1732 unconditionally; @code{nil} means ignore them; anything else means ask
1733 the user what to do for each file. The default value is @code{maybe}.
1734 @end defopt
1735
1736 @defopt safe-local-eval-forms
1737 This variable holds a list of expressions that are safe to
1738 evaluate when found in the @samp{Eval:} ``variable'' in a file
1739 local variables list.
1740 @end defopt
1741
1742 If the expression is a function call and the function has a
1743 @code{safe-local-eval-function} property, the property value
1744 determines whether the expression is safe to evaluate. The property
1745 value can be a predicate to call to test the expression, a list of
1746 such predicates (it's safe if any predicate succeeds), or @code{t}
1747 (always safe provided the arguments are constant).
1748
1749 Text properties are also potential loopholes, since their values
1750 could include functions to call. So Emacs discards all text
1751 properties from string values specified for file-local variables.
1752
1753 @node Directory Local Variables
1754 @section Directory Local Variables
1755 @cindex directory local variables
1756
1757 A directory can specify local variable values common to all files in
1758 that directory; Emacs uses these to create buffer-local bindings for
1759 those variables in buffers visiting any file in that directory. This
1760 is useful when the files in the directory belong to some @dfn{project}
1761 and therefore share the same local variables.
1762
1763 There are two different methods for specifying directory local
1764 variables: by putting them in a special file, or by defining a
1765 @dfn{project class} for that directory.
1766
1767 @defvr Constant dir-locals-file
1768 This constant is a wildcard pattern matching the name of files where
1769 Emacs expects to find directory-local variables. Its value is
1770 @file{.dir-locals*.el}@footnote{
1771 The MS-DOS version of Emacs uses @file{_dir-locals*.el} instead, due to
1772 limitations of the DOS filesystems.
1773 }, and the most common file name to use is @file{.dir-locals.el}.
1774
1775 Any file matching this name pattern in a directory causes Emacs to
1776 apply its settings when visiting files in that directory or any of its
1777 subdirectories (optionally, you can exclude subdirectories; see
1778 below).
1779 If some of the subdirectories have their own file matching
1780 @file{.dir-locals*.el}, Emacs uses the settings from the deepest file
1781 it finds starting from the file's directory and moving up the
1782 directory tree. The file specifies local variables as a specially
1783 formatted list; see @ref{Directory Variables, , Per-directory Local
1784 Variables, emacs, The GNU Emacs Manual}, for more details.
1785
1786 If the same directory contains multiple such files (for instance,
1787 @file{.dir-locals.el} and @file{.dir-locals2.el}), then all of them
1788 are used in @code{string<} order. This means that, if two files
1789 specify different values for the same variable, the file sorted after
1790 will override the value of the previous file (for instance, values in
1791 @file{.dir-locals2.el} override those in @file{.dir-locals.el}). Note
1792 that, because of how lexicographic order works, values in
1793 @file{.dir-locals10.el} are overridden by values in @file{.dir-locals2.el}.
1794 This can be avoided by using @file{.dir-locals02.el} instead.
1795 @end defvr
1796
1797 @defun hack-dir-local-variables
1798 This function reads the @code{.dir-locals.el} file and stores the
1799 directory-local variables in @code{file-local-variables-alist} that is
1800 local to the buffer visiting any file in the directory, without
1801 applying them. It also stores the directory-local settings in
1802 @code{dir-locals-class-alist}, where it defines a special class for
1803 the directory in which @file{.dir-locals.el} file was found. This
1804 function works by calling @code{dir-locals-set-class-variables} and
1805 @code{dir-locals-set-directory-class}, described below.
1806 @end defun
1807
1808 @defun hack-dir-local-variables-non-file-buffer
1809 This function looks for directory-local variables, and immediately
1810 applies them in the current buffer. It is intended to be called in
1811 the mode commands for non-file buffers, such as Dired buffers, to let
1812 them obey directory-local variable settings. For non-file buffers,
1813 Emacs looks for directory-local variables in @code{default-directory}
1814 and its parent directories.
1815 @end defun
1816
1817 @defun dir-locals-set-class-variables class variables
1818 This function defines a set of variable settings for the named
1819 @var{class}, which is a symbol. You can later assign the class to one
1820 or more directories, and Emacs will apply those variable settings to
1821 all files in those directories. The list in @var{variables} can be of
1822 one of the two forms: @code{(@var{major-mode} . @var{alist})} or
1823 @code{(@var{directory} . @var{list})}. With the first form, if the
1824 file's buffer turns on a mode that is derived from @var{major-mode},
1825 then the all the variables in the associated @var{alist} are applied;
1826 @var{alist} should be of the form @code{(@var{name} . @var{value})}.
1827 A special value @code{nil} for @var{major-mode} means the settings are
1828 applicable to any mode. In @var{alist}, you can use a special
1829 @var{name}: @code{subdirs}. If the associated value is
1830 @code{nil}, the alist is only applied to files in the relevant
1831 directory, not to those in any subdirectories.
1832
1833 With the second form of @var{variables}, if @var{directory} is the
1834 initial substring of the file's directory, then @var{list} is applied
1835 recursively by following the above rules; @var{list} should be of one
1836 of the two forms accepted by this function in @var{variables}.
1837 @end defun
1838
1839 @defun dir-locals-set-directory-class directory class &optional mtime
1840 This function assigns @var{class} to all the files in @code{directory}
1841 and its subdirectories. Thereafter, all the variable settings
1842 specified for @var{class} will be applied to any visited file in
1843 @var{directory} and its children. @var{class} must have been already
1844 defined by @code{dir-locals-set-class-variables}.
1845
1846 Emacs uses this function internally when it loads directory variables
1847 from a @code{.dir-locals.el} file. In that case, the optional
1848 argument @var{mtime} holds the file modification time (as returned by
1849 @code{file-attributes}). Emacs uses this time to check stored
1850 local variables are still valid. If you are assigning a class
1851 directly, not via a file, this argument should be @code{nil}.
1852 @end defun
1853
1854 @defvar dir-locals-class-alist
1855 This alist holds the class symbols and the associated variable
1856 settings. It is updated by @code{dir-locals-set-class-variables}.
1857 @end defvar
1858
1859 @defvar dir-locals-directory-cache
1860 This alist holds directory names, their assigned class names, and
1861 modification times of the associated directory local variables file
1862 (if there is one). The function @code{dir-locals-set-directory-class}
1863 updates this list.
1864 @end defvar
1865
1866 @defvar enable-dir-local-variables
1867 If @code{nil}, directory-local variables are ignored. This variable
1868 may be useful for modes that want to ignore directory-locals while
1869 still respecting file-local variables (@pxref{File Local Variables}).
1870 @end defvar
1871
1872 @node Variable Aliases
1873 @section Variable Aliases
1874 @cindex variable aliases
1875 @cindex alias, for variables
1876
1877 It is sometimes useful to make two variables synonyms, so that both
1878 variables always have the same value, and changing either one also
1879 changes the other. Whenever you change the name of a
1880 variable---either because you realize its old name was not well
1881 chosen, or because its meaning has partly changed---it can be useful
1882 to keep the old name as an @emph{alias} of the new one for
1883 compatibility. You can do this with @code{defvaralias}.
1884
1885 @defun defvaralias new-alias base-variable &optional docstring
1886 This function defines the symbol @var{new-alias} as a variable alias
1887 for symbol @var{base-variable}. This means that retrieving the value
1888 of @var{new-alias} returns the value of @var{base-variable}, and
1889 changing the value of @var{new-alias} changes the value of
1890 @var{base-variable}. The two aliased variable names always share the
1891 same value and the same bindings.
1892
1893 If the @var{docstring} argument is non-@code{nil}, it specifies the
1894 documentation for @var{new-alias}; otherwise, the alias gets the same
1895 documentation as @var{base-variable} has, if any, unless
1896 @var{base-variable} is itself an alias, in which case @var{new-alias} gets
1897 the documentation of the variable at the end of the chain of aliases.
1898
1899 This function returns @var{base-variable}.
1900 @end defun
1901
1902 Variable aliases are convenient for replacing an old name for a
1903 variable with a new name. @code{make-obsolete-variable} declares that
1904 the old name is obsolete and therefore that it may be removed at some
1905 stage in the future.
1906
1907 @defun make-obsolete-variable obsolete-name current-name when &optional access-type
1908 This function makes the byte compiler warn that the variable
1909 @var{obsolete-name} is obsolete. If @var{current-name} is a symbol,
1910 it is the variable's new name; then the warning message says to use
1911 @var{current-name} instead of @var{obsolete-name}. If
1912 @var{current-name} is a string, this is the message and there is no
1913 replacement variable. @var{when} should be a string indicating when
1914 the variable was first made obsolete (usually a version number
1915 string).
1916
1917 The optional argument @var{access-type}, if non-@code{nil}, should
1918 specify the kind of access that will trigger obsolescence warnings; it
1919 can be either @code{get} or @code{set}.
1920 @end defun
1921
1922 You can make two variables synonyms and declare one obsolete at the
1923 same time using the macro @code{define-obsolete-variable-alias}.
1924
1925 @defmac define-obsolete-variable-alias obsolete-name current-name &optional when docstring
1926 This macro marks the variable @var{obsolete-name} as obsolete and also
1927 makes it an alias for the variable @var{current-name}. It is
1928 equivalent to the following:
1929
1930 @example
1931 (defvaralias @var{obsolete-name} @var{current-name} @var{docstring})
1932 (make-obsolete-variable @var{obsolete-name} @var{current-name} @var{when})
1933 @end example
1934 @end defmac
1935
1936 @defun indirect-variable variable
1937 This function returns the variable at the end of the chain of aliases
1938 of @var{variable}. If @var{variable} is not a symbol, or if @var{variable} is
1939 not defined as an alias, the function returns @var{variable}.
1940
1941 This function signals a @code{cyclic-variable-indirection} error if
1942 there is a loop in the chain of symbols.
1943 @end defun
1944
1945 @example
1946 (defvaralias 'foo 'bar)
1947 (indirect-variable 'foo)
1948 @result{} bar
1949 (indirect-variable 'bar)
1950 @result{} bar
1951 (setq bar 2)
1952 bar
1953 @result{} 2
1954 @group
1955 foo
1956 @result{} 2
1957 @end group
1958 (setq foo 0)
1959 bar
1960 @result{} 0
1961 foo
1962 @result{} 0
1963 @end example
1964
1965 @node Variables with Restricted Values
1966 @section Variables with Restricted Values
1967 @cindex lisp variables defined in C, restrictions
1968
1969 Ordinary Lisp variables can be assigned any value that is a valid
1970 Lisp object. However, certain Lisp variables are not defined in Lisp,
1971 but in C@. Most of these variables are defined in the C code using
1972 @code{DEFVAR_LISP}. Like variables defined in Lisp, these can take on
1973 any value. However, some variables are defined using
1974 @code{DEFVAR_INT} or @code{DEFVAR_BOOL}. @xref{Defining Lisp
1975 variables in C,, Writing Emacs Primitives}, in particular the
1976 description of functions of the type @code{syms_of_@var{filename}},
1977 for a brief discussion of the C implementation.
1978
1979 Variables of type @code{DEFVAR_BOOL} can only take on the values
1980 @code{nil} or @code{t}. Attempting to assign them any other value
1981 will set them to @code{t}:
1982
1983 @example
1984 (let ((display-hourglass 5))
1985 display-hourglass)
1986 @result{} t
1987 @end example
1988
1989 @defvar byte-boolean-vars
1990 This variable holds a list of all variables of type @code{DEFVAR_BOOL}.
1991 @end defvar
1992
1993 Variables of type @code{DEFVAR_INT} can take on only integer values.
1994 Attempting to assign them any other value will result in an error:
1995
1996 @example
1997 (setq undo-limit 1000.0)
1998 @error{} Wrong type argument: integerp, 1000.0
1999 @end example
2000
2001 @node Generalized Variables
2002 @section Generalized Variables
2003
2004 @cindex generalized variable
2005 @cindex place form
2006 A @dfn{generalized variable} or @dfn{place form} is one of the many places
2007 in Lisp memory where values can be stored. The simplest place form is
2008 a regular Lisp variable. But the @sc{car}s and @sc{cdr}s of lists, elements
2009 of arrays, properties of symbols, and many other locations are also
2010 places where Lisp values are stored.
2011
2012 Generalized variables are analogous to lvalues in the C
2013 language, where @samp{x = a[i]} gets an element from an array
2014 and @samp{a[i] = x} stores an element using the same notation.
2015 Just as certain forms like @code{a[i]} can be lvalues in C, there
2016 is a set of forms that can be generalized variables in Lisp.
2017
2018 @menu
2019 * Setting Generalized Variables:: The @code{setf} macro.
2020 * Adding Generalized Variables:: Defining new @code{setf} forms.
2021 @end menu
2022
2023 @node Setting Generalized Variables
2024 @subsection The @code{setf} Macro
2025
2026 The @code{setf} macro is the most basic way to operate on generalized
2027 variables. The @code{setf} form is like @code{setq}, except that it
2028 accepts arbitrary place forms on the left side rather than just
2029 symbols. For example, @code{(setf (car a) b)} sets the car of
2030 @code{a} to @code{b}, doing the same operation as @code{(setcar a b)},
2031 but without having to remember two separate functions for setting and
2032 accessing every type of place.
2033
2034 @defmac setf [place form]@dots{}
2035 This macro evaluates @var{form} and stores it in @var{place}, which
2036 must be a valid generalized variable form. If there are several
2037 @var{place} and @var{form} pairs, the assignments are done sequentially
2038 just as with @code{setq}. @code{setf} returns the value of the last
2039 @var{form}.
2040 @end defmac
2041
2042 The following Lisp forms will work as generalized variables, and
2043 so may appear in the @var{place} argument of @code{setf}:
2044
2045 @itemize
2046 @item
2047 A symbol naming a variable. In other words, @code{(setf x y)} is
2048 exactly equivalent to @code{(setq x y)}, and @code{setq} itself is
2049 strictly speaking redundant given that @code{setf} exists. Many
2050 programmers continue to prefer @code{setq} for setting simple
2051 variables, though, purely for stylistic or historical reasons.
2052 The macro @code{(setf x y)} actually expands to @code{(setq x y)},
2053 so there is no performance penalty for using it in compiled code.
2054
2055 @item
2056 A call to any of the following standard Lisp functions:
2057
2058 @smallexample
2059 aref cddr symbol-function
2060 car elt symbol-plist
2061 caar get symbol-value
2062 cadr gethash
2063 cdr nth
2064 cdar nthcdr
2065 @end smallexample
2066
2067 @item
2068 A call to any of the following Emacs-specific functions:
2069
2070 @smallexample
2071 default-value process-get
2072 frame-parameter process-sentinel
2073 terminal-parameter window-buffer
2074 keymap-parent window-display-table
2075 match-data window-dedicated-p
2076 overlay-get window-hscroll
2077 overlay-start window-parameter
2078 overlay-end window-point
2079 process-buffer window-start
2080 process-filter
2081 @end smallexample
2082 @end itemize
2083
2084 @noindent
2085 @code{setf} signals an error if you pass a @var{place} form that it
2086 does not know how to handle.
2087
2088 @c And for cl-lib's cl-getf.
2089 Note that for @code{nthcdr}, the list argument of the function must
2090 itself be a valid @var{place} form. For example, @code{(setf (nthcdr
2091 0 foo) 7)} will set @code{foo} itself to 7.
2092 @c The use of @code{nthcdr} as a @var{place} form is an extension
2093 @c to standard Common Lisp.
2094
2095 @c FIXME I don't think is a particularly good way to do it,
2096 @c but these macros are introduced before generalized variables are.
2097 The macros @code{push} (@pxref{List Variables}) and @code{pop}
2098 (@pxref{List Elements}) can manipulate generalized variables,
2099 not just lists. @code{(pop @var{place})} removes and returns the first
2100 element of the list stored in @var{place}. It is analogous to
2101 @code{(prog1 (car @var{place}) (setf @var{place} (cdr @var{place})))},
2102 except that it takes care to evaluate all subforms only once.
2103 @code{(push @var{x} @var{place})} inserts @var{x} at the front of
2104 the list stored in @var{place}. It is analogous to @code{(setf
2105 @var{place} (cons @var{x} @var{place}))}, except for evaluation of the
2106 subforms. Note that @code{push} and @code{pop} on an @code{nthcdr}
2107 place can be used to insert or delete at any position in a list.
2108
2109 The @file{cl-lib} library defines various extensions for generalized
2110 variables, including additional @code{setf} places.
2111 @xref{Generalized Variables,,, cl, Common Lisp Extensions}.
2112
2113
2114 @node Adding Generalized Variables
2115 @subsection Defining new @code{setf} forms
2116
2117 This section describes how to define new forms that @code{setf} can
2118 operate on.
2119
2120 @defmac gv-define-simple-setter name setter &optional fix-return
2121 This macro enables you to easily define @code{setf} methods for simple
2122 cases. @var{name} is the name of a function, macro, or special form.
2123 You can use this macro whenever @var{name} has a directly
2124 corresponding @var{setter} function that updates it, e.g.,
2125 @code{(gv-define-simple-setter car setcar)}.
2126
2127 This macro translates a call of the form
2128
2129 @example
2130 (setf (@var{name} @var{args}@dots{}) @var{value})
2131 @end example
2132
2133 into
2134 @example
2135 (@var{setter} @var{args}@dots{} @var{value})
2136 @end example
2137
2138 @noindent
2139 Such a @code{setf} call is documented to return @var{value}. This is
2140 no problem with, e.g., @code{car} and @code{setcar}, because
2141 @code{setcar} returns the value that it set. If your @var{setter}
2142 function does not return @var{value}, use a non-@code{nil} value for
2143 the @var{fix-return} argument of @code{gv-define-simple-setter}. This
2144 expands into something equivalent to
2145 @example
2146 (let ((temp @var{value}))
2147 (@var{setter} @var{args}@dots{} temp)
2148 temp)
2149 @end example
2150 so ensuring that it returns the correct result.
2151 @end defmac
2152
2153
2154 @defmac gv-define-setter name arglist &rest body
2155 This macro allows for more complex @code{setf} expansions than the
2156 previous form. You may need to use this form, for example, if there
2157 is no simple setter function to call, or if there is one but it
2158 requires different arguments to the place form.
2159
2160 This macro expands the form
2161 @code{(setf (@var{name} @var{args}@dots{}) @var{value})} by
2162 first binding the @code{setf} argument forms
2163 @code{(@var{value} @var{args}@dots{})} according to @var{arglist},
2164 and then executing @var{body}. @var{body} should return a Lisp
2165 form that does the assignment, and finally returns the value that was
2166 set. An example of using this macro is:
2167
2168 @example
2169 (gv-define-setter caar (val x) `(setcar (car ,x) ,val))
2170 @end example
2171 @end defmac
2172
2173 @findex gv-define-expander
2174 @findex gv-letplace
2175 @c FIXME? Not sure what or how much to say about these.
2176 @c See cl.texi for an example of using gv-letplace.
2177 For more control over the expansion, see the macro @code{gv-define-expander}.
2178 The macro @code{gv-letplace} can be useful in defining macros that
2179 perform similarly to @code{setf}; for example, the @code{incf} macro
2180 of Common Lisp. Consult the source file @file{gv.el} for more details.
2181
2182 @cindex CL note---no @code{setf} functions
2183 @quotation
2184 @b{Common Lisp note:} Common Lisp defines another way to specify the
2185 @code{setf} behavior of a function, namely @code{setf} functions,
2186 whose names are lists @code{(setf @var{name})} rather than symbols.
2187 For example, @code{(defun (setf foo) @dots{})} defines the function
2188 that is used when @code{setf} is applied to @code{foo}. Emacs does
2189 not support this. It is a compile-time error to use @code{setf} on a
2190 form that has not already had an appropriate expansion defined. In
2191 Common Lisp, this is not an error since the function @code{(setf
2192 @var{func})} might be defined later.
2193 @end quotation