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