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