Update icon-type values.
[bpt/emacs.git] / lispref / variables.texi
CommitLineData
e6512bcf
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/variables
6@node Variables, Functions, Control Structures, Top
7@chapter Variables
8@cindex variable
9
10 A @dfn{variable} is a name used in a program to stand for a value.
11Nearly all programming languages have variables of some sort. In the
12text of a Lisp program, variables are written using the syntax for
13symbols.
14
15 In Lisp, unlike most programming languages, programs are represented
16primarily as Lisp objects and only secondarily as text. The Lisp
17objects used for variables are symbols: the symbol name is the variable
18name, and the variable's value is stored in the value cell of the
19symbol. The use of a symbol as a variable is independent of its use as
20a function name. @xref{Symbol Components}.
21
22 The Lisp objects that constitute a Lisp program determine the textual
f57ddf67 23form of the program---it is simply the read syntax for those Lisp
e6512bcf
RS
24objects. This is why, for example, a variable in a textual Lisp program
25is written using the read syntax for the symbol that represents the
26variable.
27
28@menu
29* Global Variables:: Variable values that exist permanently, everywhere.
30* Constant Variables:: Certain "variables" have values that never change.
31* Local Variables:: Variable values that exist only temporarily.
32* Void Variables:: Symbols that lack values.
33* Defining Variables:: A definition says a symbol is used as a variable.
34* Accessing Variables:: Examining values of variables whose names
35 are known only at run time.
36* Setting Variables:: Storing new values in variables.
37* Variable Scoping:: How Lisp chooses among local and global values.
38* Buffer-Local Variables:: Variable values in effect only in one buffer.
39@end menu
40
41@node Global Variables
42@section Global Variables
43@cindex global variable
44
45 The simplest way to use a variable is @dfn{globally}. This means that
46the variable has just one value at a time, and this value is in effect
47(at least for the moment) throughout the Lisp system. The value remains
48in effect until you specify a new one. When a new value replaces the
49old one, no trace of the old value remains in the variable.
50
51 You specify a value for a symbol with @code{setq}. For example,
52
53@example
54(setq x '(a b))
55@end example
56
57@noindent
58gives the variable @code{x} the value @code{(a b)}. Note that
59@code{setq} does not evaluate its first argument, the name of the
60variable, but it does evaluate the second argument, the new value.
61
62 Once the variable has a value, you can refer to it by using the symbol
63by itself as an expression. Thus,
64
65@example
66@group
67x @result{} (a b)
68@end group
69@end example
70
71@noindent
72assuming the @code{setq} form shown above has already been executed.
73
74 If you do another @code{setq}, the new value replaces the old one:
75
76@example
77@group
78x
79 @result{} (a b)
80@end group
81@group
82(setq x 4)
83 @result{} 4
84@end group
85@group
86x
87 @result{} 4
88@end group
89@end example
90
91@node Constant Variables
92@section Variables That Never Change
93@vindex nil
94@vindex t
95@kindex setting-constant
96
97 Emacs Lisp has two special symbols, @code{nil} and @code{t}, that
98always evaluate to themselves. These symbols cannot be rebound, nor can
99their value cells be changed. An attempt to change the value of
100@code{nil} or @code{t} signals a @code{setting-constant} error.
101
102@example
103@group
104nil @equiv{} 'nil
105 @result{} nil
106@end group
107@group
108(setq nil 500)
109@error{} Attempt to set constant symbol: nil
110@end group
111@end example
112
113@node Local Variables
114@section Local Variables
115@cindex binding local variables
116@cindex local variables
117@cindex local binding
118@cindex global binding
119
120 Global variables have values that last until explicitly superseded
121with new values. Sometimes it is useful to create variable values that
122exist temporarily---only while within a certain part of the program.
123These values are called @dfn{local}, and the variables so used are
124called @dfn{local variables}.
125
126 For example, when a function is called, its argument variables receive
127new local values that last until the function exits. The @code{let}
128special form explicitly establishes new local values for specified
129variables; these last until exit from the @code{let} form.
130
131@cindex shadowing of variables
132 Establishing a local value saves away the previous value (or lack of
133one) of the variable. When the life span of the local value is over,
134the previous value is restored. In the mean time, we say that the
135previous value is @dfn{shadowed} and @dfn{not visible}. Both global and
136local values may be shadowed (@pxref{Scope}).
137
138 If you set a variable (such as with @code{setq}) while it is local,
139this replaces the local value; it does not alter the global value, or
140previous local values that are shadowed. To model this behavior, we
141speak of a @dfn{local binding} of the variable as well as a local value.
142
143 The local binding is a conceptual place that holds a local value.
144Entry to a function, or a special form such as @code{let}, creates the
145local binding; exit from the function or from the @code{let} removes the
146local binding. As long as the local binding lasts, the variable's value
147is stored within it. Use of @code{setq} or @code{set} while there is a
148local binding stores a different value into the local binding; it does
149not create a new binding.
150
151 We also speak of the @dfn{global binding}, which is where
152(conceptually) the global value is kept.
153
154@cindex current binding
155 A variable can have more than one local binding at a time (for
156example, if there are nested @code{let} forms that bind it). In such a
157case, the most recently created local binding that still exists is the
158@dfn{current binding} of the variable. (This is called @dfn{dynamic
159scoping}; see @ref{Variable Scoping}.) If there are no local bindings,
160the variable's global binding is its current binding. We also call the
161current binding the @dfn{most-local existing binding}, for emphasis.
162Ordinary evaluation of a symbol always returns the value of its current
163binding.
164
165 The special forms @code{let} and @code{let*} exist to create
166local bindings.
167
168@defspec let (bindings@dots{}) forms@dots{}
f57ddf67 169This special form binds variables according to @var{bindings} and then
e6512bcf
RS
170evaluates all of the @var{forms} in textual order. The @code{let}-form
171returns the value of the last form in @var{forms}.
172
173Each of the @var{bindings} is either @w{(i) a} symbol, in which case
174that symbol is bound to @code{nil}; or @w{(ii) a} list of the form
175@code{(@var{symbol} @var{value-form})}, in which case @var{symbol} is
176bound to the result of evaluating @var{value-form}. If @var{value-form}
177is omitted, @code{nil} is used.
178
179All of the @var{value-form}s in @var{bindings} are evaluated in the
180order they appear and @emph{before} any of the symbols are bound. Here
181is an example of this: @code{Z} is bound to the old value of @code{Y},
182which is 2, not the new value, 1.
183
184@example
185@group
186(setq Y 2)
187 @result{} 2
188@end group
189@group
190(let ((Y 1)
191 (Z Y))
192 (list Y Z))
193 @result{} (1 2)
194@end group
195@end example
196@end defspec
197
198@defspec let* (bindings@dots{}) forms@dots{}
199This special form is like @code{let}, but it binds each variable right
200after computing its local value, before computing the local value for
201the next variable. Therefore, an expression in @var{bindings} can
202reasonably refer to the preceding symbols bound in this @code{let*}
203form. Compare the following example with the example above for
204@code{let}.
205
206@example
207@group
208(setq Y 2)
209 @result{} 2
210@end group
211@group
212(let* ((Y 1)
213 (Z Y)) ; @r{Use the just-established value of @code{Y}.}
214 (list Y Z))
215 @result{} (1 1)
216@end group
217@end example
218@end defspec
219
f57ddf67 220 Here is a complete list of the other facilities that create local
e6512bcf
RS
221bindings:
222
223@itemize @bullet
224@item
225Function calls (@pxref{Functions}).
226
227@item
228Macro calls (@pxref{Macros}).
229
230@item
231@code{condition-case} (@pxref{Errors}).
232@end itemize
233
234@defvar max-specpdl-size
235@cindex variable limit error
236@cindex evaluation error
237@cindex infinite recursion
238 This variable defines the limit on the total number of local variable
239bindings and @code{unwind-protect} cleanups (@pxref{Nonlocal Exits})
240that are allowed before signaling an error (with data @code{"Variable
241binding depth exceeds max-specpdl-size"}).
242
243 This limit, with the associated error when it is exceeded, is one way
244that Lisp avoids infinite recursion on an ill-defined function.
245
246 The default value is 600.
247
248 @code{max-lisp-eval-depth} provides another limit on depth of nesting.
249@xref{Eval}.
250@end defvar
251
252@node Void Variables
253@section When a Variable is ``Void''
254@kindex void-variable
255@cindex void variable
256
257 If you have never given a symbol any value as a global variable, we
258say that that symbol's global value is @dfn{void}. In other words, the
259symbol's value cell does not have any Lisp object in it. If you try to
260evaluate the symbol, you get a @code{void-variable} error rather than
261a value.
262
263 Note that a value of @code{nil} is not the same as void. The symbol
264@code{nil} is a Lisp object and can be the value of a variable just as any
265other object can be; but it is @emph{a value}. A void variable does not
266have any value.
267
268 After you have given a variable a value, you can make it void once more
269using @code{makunbound}.
270
271@defun makunbound symbol
272This function makes the current binding of @var{symbol} void.
273Subsequent attempts to use this symbol's value as a variable will signal
274the error @code{void-variable}, unless or until you set it again.
275
276@code{makunbound} returns @var{symbol}.
277
278@example
279@group
280(makunbound 'x) ; @r{Make the global value}
281 ; @r{of @code{x} void.}
282 @result{} x
283@end group
284@group
285x
286@error{} Symbol's value as variable is void: x
287@end group
288@end example
289
290If @var{symbol} is locally bound, @code{makunbound} affects the most
291local existing binding. This is the only way a symbol can have a void
292local binding, since all the constructs that create local bindings
293create them with values. In this case, the voidness lasts at most as
294long as the binding does; when the binding is removed due to exit from
295the construct that made it, the previous or global binding is reexposed
296as usual, and the variable is no longer void unless the newly reexposed
297binding was void all along.
298
299@smallexample
300@group
301(setq x 1) ; @r{Put a value in the global binding.}
302 @result{} 1
303(let ((x 2)) ; @r{Locally bind it.}
304 (makunbound 'x) ; @r{Void the local binding.}
305 x)
306@error{} Symbol's value as variable is void: x
307@end group
308@group
309x ; @r{The global binding is unchanged.}
310 @result{} 1
311
312(let ((x 2)) ; @r{Locally bind it.}
313 (let ((x 3)) ; @r{And again.}
314 (makunbound 'x) ; @r{Void the innermost-local binding.}
315 x)) ; @r{And refer: it's void.}
316@error{} Symbol's value as variable is void: x
317@end group
318
319@group
320(let ((x 2))
321 (let ((x 3))
322 (makunbound 'x)) ; @r{Void inner binding, then remove it.}
323 x) ; @r{Now outer @code{let} binding is visible.}
324 @result{} 2
325@end group
326@end smallexample
327@end defun
328
329 A variable that has been made void with @code{makunbound} is
330indistinguishable from one that has never received a value and has
331always been void.
332
333 You can use the function @code{boundp} to test whether a variable is
334currently void.
335
336@defun boundp variable
337@code{boundp} returns @code{t} if @var{variable} (a symbol) is not void;
338more precisely, if its current binding is not void. It returns
339@code{nil} otherwise.
340
341@smallexample
342@group
343(boundp 'abracadabra) ; @r{Starts out void.}
344 @result{} nil
345@end group
346@group
347(let ((abracadabra 5)) ; @r{Locally bind it.}
348 (boundp 'abracadabra))
349 @result{} t
350@end group
351@group
352(boundp 'abracadabra) ; @r{Still globally void.}
353 @result{} nil
354@end group
355@group
356(setq abracadabra 5) ; @r{Make it globally nonvoid.}
357 @result{} 5
358@end group
359@group
360(boundp 'abracadabra)
361 @result{} t
362@end group
363@end smallexample
364@end defun
365
366@node Defining Variables
367@section Defining Global Variables
f57ddf67 368@cindex variable definition
e6512bcf
RS
369
370 You may announce your intention to use a symbol as a global variable
f57ddf67
RS
371with a @dfn{variable definition}: a special form, either @code{defconst}
372or @code{defvar}.
e6512bcf
RS
373
374 In Emacs Lisp, definitions serve three purposes. First, they inform
375people who read the code that certain symbols are @emph{intended} to be
376used a certain way (as variables). Second, they inform the Lisp system
377of these things, supplying a value and documentation. Third, they
378provide information to utilities such as @code{etags} and
379@code{make-docfile}, which create data bases of the functions and
380variables in a program.
381
382 The difference between @code{defconst} and @code{defvar} is primarily
383a matter of intent, serving to inform human readers of whether programs
384will change the variable. Emacs Lisp does not restrict the ways in
385which a variable can be used based on @code{defconst} or @code{defvar}
f57ddf67 386declarations. However, it does make a difference for initialization:
e6512bcf
RS
387@code{defconst} unconditionally initializes the variable, while
388@code{defvar} initializes it only if it is void.
389
390 One would expect user option variables to be defined with
391@code{defconst}, since programs do not change them. Unfortunately, this
392has bad results if the definition is in a library that is not preloaded:
393@code{defconst} would override any prior value when the library is
394loaded. Users would like to be able to set user options in their init
395files, and override the default values given in the definitions. For
396this reason, user options must be defined with @code{defvar}.
397
398@defspec defvar symbol [value [doc-string]]
399This special form defines @var{symbol} as a value and initializes it.
400The definition informs a person reading your code that @var{symbol} is
401used as a variable that programs are likely to set or change. It is
402also used for all user option variables except in the preloaded parts of
403Emacs. Note that @var{symbol} is not evaluated; the symbol to be
404defined must appear explicitly in the @code{defvar}.
405
406If @var{symbol} already has a value (i.e., it is not void), @var{value}
407is not even evaluated, and @var{symbol}'s value remains unchanged. If
408@var{symbol} is void and @var{value} is specified, @code{defvar}
409evaluates it and sets @var{symbol} to the result. (If @var{value} is
410omitted, the value of @var{symbol} is not changed in any case.)
411
412If @var{symbol} has a buffer-local binding in the current buffer,
413@code{defvar} sets the default value, not the local value.
414@xref{Buffer-Local Variables}.
415
416If the @var{doc-string} argument appears, it specifies the documentation
417for the variable. (This opportunity to specify documentation is one of
418the main benefits of defining the variable.) The documentation is
419stored in the symbol's @code{variable-documentation} property. The
420Emacs help functions (@pxref{Documentation}) look for this property.
421
422If the first character of @var{doc-string} is @samp{*}, it means that
423this variable is considered a user option. This lets users set the
424variable conventiently using the commands @code{set-variable} and
425@code{edit-options}.
426
427For example, this form defines @code{foo} but does not set its value:
428
429@example
430@group
431(defvar foo)
432 @result{} foo
433@end group
434@end example
435
436The following example sets the value of @code{bar} to @code{23}, and
437gives it a documentation string:
438
439@example
440@group
441(defvar bar 23
442 "The normal weight of a bar.")
443 @result{} bar
444@end group
445@end example
446
447The following form changes the documentation string for @code{bar},
448making it a user option, but does not change the value, since @code{bar}
449already has a value. (The addition @code{(1+ 23)} is not even
450performed.)
451
452@example
453@group
454(defvar bar (1+ 23)
455 "*The normal weight of a bar.")
456 @result{} bar
457@end group
458@group
459bar
460 @result{} 23
461@end group
462@end example
463
464Here is an equivalent expression for the @code{defvar} special form:
465
466@example
467@group
468(defvar @var{symbol} @var{value} @var{doc-string})
469@equiv{}
470(progn
471 (if (not (boundp '@var{symbol}))
472 (setq @var{symbol} @var{value}))
473 (put '@var{symbol} 'variable-documentation '@var{doc-string})
474 '@var{symbol})
475@end group
476@end example
477
478The @code{defvar} form returns @var{symbol}, but it is normally used
479at top level in a file where its value does not matter.
480@end defspec
481
482@defspec defconst symbol [value [doc-string]]
483This special form defines @var{symbol} as a value and initializes it.
484It informs a person reading your code that @var{symbol} has a global
485value, established here, that will not normally be changed or locally
486bound by the execution of the program. The user, however, may be
487welcome to change it. Note that @var{symbol} is not evaluated; the
488symbol to be defined must appear explicitly in the @code{defconst}.
489
490@code{defconst} always evaluates @var{value} and sets the global value
491of @var{symbol} to the result, provided @var{value} is given. If
492@var{symbol} has a buffer-local binding in the current buffer,
493@code{defconst} sets the default value, not the local value.
494
b22f3a19 495@strong{Please note:} Don't use @code{defconst} for user option
e6512bcf
RS
496variables in libraries that are not standardly preloaded. The user
497should be able to specify a value for such a variable in the
498@file{.emacs} file, so that it will be in effect if and when the library
499is loaded later.
500
501Here, @code{pi} is a constant that presumably ought not to be changed
502by anyone (attempts by the Indiana State Legislature notwithstanding).
503As the second form illustrates, however, this is only advisory.
504
505@example
506@group
507(defconst pi 3.1415 "Pi to five places.")
508 @result{} pi
509@end group
510@group
511(setq pi 3)
512 @result{} pi
513@end group
514@group
515pi
516 @result{} 3
517@end group
518@end example
519@end defspec
520
521@defun user-variable-p variable
522@cindex user option
f57ddf67 523This function returns @code{t} if @var{variable} is a user option---a
e6512bcf
RS
524variable intended to be set by the user for customization---and
525@code{nil} otherwise. (Variables other than user options exist for the
526internal purposes of Lisp programs, and users need not know about them.)
527
528User option variables are distinguished from other variables by the
529first character of the @code{variable-documentation} property. If the
530property exists and is a string, and its first character is @samp{*},
531then the variable is a user option.
532@end defun
533
534 If a user option variable has a @code{variable-interactive} property,
535@code{set-variable} uses that value to control reading the new value for
536the variable. The property's value is used as if it were the argument
537to @code{interactive}.
538
b22f3a19 539 @strong{Warning:} If the @code{defconst} and @code{defvar} special
e6512bcf
RS
540forms are used while the variable has a local binding, they set the
541local binding's value; the global binding is not changed. This is not
542what we really want. To prevent it, use these special forms at top
543level in a file, where normally no local binding is in effect, and make
544sure to load the file before making a local binding for the variable.
545
546@node Accessing Variables
547@section Accessing Variable Values
548
549 The usual way to reference a variable is to write the symbol which
550names it (@pxref{Symbol Forms}). This requires you to specify the
551variable name when you write the program. Usually that is exactly what
552you want to do. Occasionally you need to choose at run time which
553variable to reference; then you can use @code{symbol-value}.
554
555@defun symbol-value symbol
556This function returns the value of @var{symbol}. This is the value in
557the innermost local binding of the symbol, or its global value if it
558has no local bindings.
559
560@example
561@group
562(setq abracadabra 5)
563 @result{} 5
564@end group
565@group
566(setq foo 9)
567 @result{} 9
568@end group
569
570@group
571;; @r{Here the symbol @code{abracadabra}}
572;; @r{is the symbol whose value is examined.}
573(let ((abracadabra 'foo))
574 (symbol-value 'abracadabra))
575 @result{} foo
576@end group
577
578@group
579;; @r{Here the value of @code{abracadabra},}
580;; @r{which is @code{foo},}
581;; @r{is the symbol whose value is examined.}
582(let ((abracadabra 'foo))
583 (symbol-value abracadabra))
584 @result{} 9
585@end group
586
587@group
588(symbol-value 'abracadabra)
589 @result{} 5
590@end group
591@end example
592
593A @code{void-variable} error is signaled if @var{symbol} has neither a
594local binding nor a global value.
595@end defun
596
597@node Setting Variables
598@section How to Alter a Variable Value
599
600 The usual way to change the value of a variable is with the special
601form @code{setq}. When you need to compute the choice of variable at
602run time, use the function @code{set}.
603
604@defspec setq [symbol form]@dots{}
605This special form is the most common method of changing a variable's
606value. Each @var{symbol} is given a new value, which is the result of
607evaluating the corresponding @var{form}. The most-local existing
608binding of the symbol is changed.
609
610@code{setq} does not evaluate @var{symbol}; it sets the symbol that you
611write. We say that this argument is @dfn{automatically quoted}. The
612@samp{q} in @code{setq} stands for ``quoted.''
613
614The value of the @code{setq} form is the value of the last @var{form}.
615
616@example
617@group
618(setq x (1+ 2))
619 @result{} 3
620@end group
621x ; @r{@code{x} now has a global value.}
622 @result{} 3
623@group
624(let ((x 5))
625 (setq x 6) ; @r{The local binding of @code{x} is set.}
626 x)
627 @result{} 6
628@end group
629x ; @r{The global value is unchanged.}
630 @result{} 3
631@end example
632
633Note that the first @var{form} is evaluated, then the first
634@var{symbol} is set, then the second @var{form} is evaluated, then the
635second @var{symbol} is set, and so on:
636
637@example
638@group
639(setq x 10 ; @r{Notice that @code{x} is set before}
640 y (1+ x)) ; @r{the value of @code{y} is computed.}
641 @result{} 11
642@end group
643@end example
644@end defspec
645
646@defun set symbol value
647This function sets @var{symbol}'s value to @var{value}, then returns
648@var{value}. Since @code{set} is a function, the expression written for
649@var{symbol} is evaluated to obtain the symbol to set.
650
651The most-local existing binding of the variable is the binding that is
f57ddf67 652set; shadowed bindings are not affected.
e6512bcf
RS
653
654@example
655@group
656(set one 1)
657@error{} Symbol's value as variable is void: one
658@end group
659@group
660(set 'one 1)
661 @result{} 1
662@end group
663@group
664(set 'two 'one)
665 @result{} one
666@end group
667@group
668(set two 2) ; @r{@code{two} evaluates to symbol @code{one}.}
669 @result{} 2
670@end group
671@group
672one ; @r{So it is @code{one} that was set.}
673 @result{} 2
674(let ((one 1)) ; @r{This binding of @code{one} is set,}
675 (set 'one 3) ; @r{not the global value.}
676 one)
677 @result{} 3
678@end group
679@group
680one
681 @result{} 2
682@end group
683@end example
684
f57ddf67
RS
685If @var{symbol} is not actually a symbol, a @code{wrong-type-argument}
686error is signaled.
687
688@example
689(set '(x y) 'z)
690@error{} Wrong type argument: symbolp, (x y)
691@end example
692
e6512bcf
RS
693Logically speaking, @code{set} is a more fundamental primitive than
694@code{setq}. Any use of @code{setq} can be trivially rewritten to use
695@code{set}; @code{setq} could even be defined as a macro, given the
696availability of @code{set}. However, @code{set} itself is rarely used;
f57ddf67
RS
697beginners hardly need to know about it. It is useful only for choosing
698at run time which variable to set. For example, the command
e6512bcf
RS
699@code{set-variable}, which reads a variable name from the user and then
700sets the variable, needs to use @code{set}.
701
702@cindex CL note---@code{set} local
703@quotation
f57ddf67 704@b{Common Lisp note:} In Common Lisp, @code{set} always changes the
e6512bcf
RS
705symbol's special value, ignoring any lexical bindings. In Emacs Lisp,
706all variables and all bindings are (in effect) special, so @code{set}
707always affects the most local existing binding.
708@end quotation
709@end defun
710
711@node Variable Scoping
712@section Scoping Rules for Variable Bindings
713
714 A given symbol @code{foo} may have several local variable bindings,
715established at different places in the Lisp program, as well as a global
716binding. The most recently established binding takes precedence over
717the others.
718
719@cindex scope
720@cindex extent
721@cindex dynamic scoping
722 Local bindings in Emacs Lisp have @dfn{indefinite scope} and
723@dfn{dynamic extent}. @dfn{Scope} refers to @emph{where} textually in
724the source code the binding can be accessed. Indefinite scope means
725that any part of the program can potentially access the variable
726binding. @dfn{Extent} refers to @emph{when}, as the program is
727executing, the binding exists. Dynamic extent means that the binding
728lasts as long as the activation of the construct that established it.
729
730 The combination of dynamic extent and indefinite scope is called
731@dfn{dynamic scoping}. By contrast, most programming languages use
732@dfn{lexical scoping}, in which references to a local variable must be
733located textually within the function or block that binds the variable.
734
735@cindex CL note---special variables
736@quotation
b22f3a19 737@b{Common Lisp note:} Variables declared ``special'' in Common Lisp
f57ddf67 738are dynamically scoped, like variables in Emacs Lisp.
e6512bcf
RS
739@end quotation
740
741@menu
742* Scope:: Scope means where in the program a value is visible.
743 Comparison with other languages.
744* Extent:: Extent means how long in time a value exists.
745* Impl of Scope:: Two ways to implement dynamic scoping.
746* Using Scoping:: How to use dynamic scoping carefully and avoid problems.
747@end menu
748
749@node Scope
750@subsection Scope
751
752 Emacs Lisp uses @dfn{indefinite scope} for local variable bindings.
753This means that any function anywhere in the program text might access a
754given binding of a variable. Consider the following function
755definitions:
756
757@example
758@group
759(defun binder (x) ; @r{@code{x} is bound in @code{binder}.}
760 (foo 5)) ; @r{@code{foo} is some other function.}
761@end group
762
763@group
764(defun user () ; @r{@code{x} is used in @code{user}.}
765 (list x))
766@end group
767@end example
768
769 In a lexically scoped language, the binding of @code{x} in
770@code{binder} would never be accessible in @code{user}, because
771@code{user} is not textually contained within the function
772@code{binder}. However, in dynamically scoped Emacs Lisp, @code{user}
773may or may not refer to the binding of @code{x} established in
774@code{binder}, depending on circumstances:
775
776@itemize @bullet
777@item
778If we call @code{user} directly without calling @code{binder} at all,
779then whatever binding of @code{x} is found, it cannot come from
780@code{binder}.
781
782@item
783If we define @code{foo} as follows and call @code{binder}, then the
784binding made in @code{binder} will be seen in @code{user}:
785
786@example
787@group
788(defun foo (lose)
789 (user))
790@end group
791@end example
792
793@item
794If we define @code{foo} as follows and call @code{binder}, then the
795binding made in @code{binder} @emph{will not} be seen in @code{user}:
796
797@example
798(defun foo (x)
799 (user))
800@end example
801
802@noindent
803Here, when @code{foo} is called by @code{binder}, it binds @code{x}.
804(The binding in @code{foo} is said to @dfn{shadow} the one made in
805@code{binder}.) Therefore, @code{user} will access the @code{x} bound
806by @code{foo} instead of the one bound by @code{binder}.
807@end itemize
808
809@node Extent
810@subsection Extent
811
812 @dfn{Extent} refers to the time during program execution that a
813variable name is valid. In Emacs Lisp, a variable is valid only while
814the form that bound it is executing. This is called @dfn{dynamic
815extent}. ``Local'' or ``automatic'' variables in most languages,
816including C and Pascal, have dynamic extent.
817
818 One alternative to dynamic extent is @dfn{indefinite extent}. This
819means that a variable binding can live on past the exit from the form
820that made the binding. Common Lisp and Scheme, for example, support
821this, but Emacs Lisp does not.
822
823 To illustrate this, the function below, @code{make-add}, returns a
824function that purports to add @var{n} to its own argument @var{m}.
825This would work in Common Lisp, but it does not work as intended in
826Emacs Lisp, because after the call to @code{make-add} exits, the
827variable @code{n} is no longer bound to the actual argument 2.
828
829@example
830(defun make-add (n)
831 (function (lambda (m) (+ n m)))) ; @r{Return a function.}
832 @result{} make-add
833(fset 'add2 (make-add 2)) ; @r{Define function @code{add2}}
834 ; @r{with @code{(make-add 2)}.}
835 @result{} (lambda (m) (+ n m))
836(add2 4) ; @r{Try to add 2 to 4.}
837@error{} Symbol's value as variable is void: n
838@end example
839
840@cindex closures not available
841 Some Lisp dialects have ``closures'', objects that are like functions
842but record additional variable bindings. Emacs Lisp does not have
843closures.
844
845@node Impl of Scope
846@subsection Implementation of Dynamic Scoping
847@cindex deep binding
848
849 A simple sample implementation (which is not how Emacs Lisp actually
850works) may help you understand dynamic binding. This technique is
851called @dfn{deep binding} and was used in early Lisp systems.
852
853 Suppose there is a stack of bindings: variable-value pairs. At entry
854to a function or to a @code{let} form, we can push bindings on the stack
855for the arguments or local variables created there. We can pop those
856bindings from the stack at exit from the binding construct.
857
858 We can find the value of a variable by searching the stack from top to
859bottom for a binding for that variable; the value from that binding is
860the value of the variable. To set the variable, we search for the
861current binding, then store the new value into that binding.
862
863 As you can see, a function's bindings remain in effect as long as it
864continues execution, even during its calls to other functions. That is
865why we say the extent of the binding is dynamic. And any other function
866can refer to the bindings, if it uses the same variables while the
867bindings are in effect. That is why we say the scope is indefinite.
868
869@cindex shallow binding
870 The actual implementation of variable scoping in GNU Emacs Lisp uses a
871technique called @dfn{shallow binding}. Each variable has a standard
872place in which its current value is always found---the value cell of the
873symbol.
874
875 In shallow binding, setting the variable works by storing a value in
876the value cell. Creating a new binding works by pushing the old value
877(belonging to a previous binding) on a stack, and storing the local value
878in the value cell. Eliminating a binding works by popping the old value
879off the stack, into the value cell.
880
881 We use shallow binding because it has the same results as deep
882binding, but runs faster, since there is never a need to search for a
883binding.
884
885@node Using Scoping
886@subsection Proper Use of Dynamic Scoping
887
888 Binding a variable in one function and using it in another is a
889powerful technique, but if used without restraint, it can make programs
890hard to understand. There are two clean ways to use this technique:
891
892@itemize @bullet
893@item
894Use or bind the variable only in a few related functions, written close
895together in one file. Such a variable is used for communication within
896one program.
897
898You should write comments to inform other programmers that they can see
899all uses of the variable before them, and to advise them not to add uses
900elsewhere.
901
902@item
903Give the variable a well-defined, documented meaning, and make all
904appropriate functions refer to it (but not bind it or set it) wherever
905that meaning is relevant. For example, the variable
906@code{case-fold-search} is defined as ``non-@code{nil} means ignore case
907when searching''; various search and replace functions refer to it
908directly or through their subroutines, but do not bind or set it.
909
910Then you can bind the variable in other programs, knowing reliably what
911the effect will be.
912@end itemize
913
914@node Buffer-Local Variables
915@section Buffer-Local Variables
916@cindex variables, buffer-local
917@cindex buffer-local variables
918
919 Global and local variable bindings are found in most programming
920languages in one form or another. Emacs also supports another, unusual
921kind of variable binding: @dfn{buffer-local} bindings, which apply only
922to one buffer. Emacs Lisp is meant for programming editing commands,
923and having different values for a variable in different buffers is an
924important customization method.
925
926@menu
927* Intro to Buffer-Local:: Introduction and concepts.
928* Creating Buffer-Local:: Creating and destroying buffer-local bindings.
929* Default Value:: The default value is seen in buffers
930 that don't have their own local values.
931@end menu
932
933@node Intro to Buffer-Local
934@subsection Introduction to Buffer-Local Variables
935
936 A buffer-local variable has a buffer-local binding associated with a
937particular buffer. The binding is in effect when that buffer is
938current; otherwise, it is not in effect. If you set the variable while
939a buffer-local binding is in effect, the new value goes in that binding,
940so the global binding is unchanged; this means that the change is
941visible in that buffer alone.
942
943 A variable may have buffer-local bindings in some buffers but not in
944others. The global binding is shared by all the buffers that don't have
945their own bindings. Thus, if you set the variable in a buffer that does
946not have a buffer-local binding for it, the new value is visible in all
947buffers except those with buffer-local bindings. (Here we are assuming
948that there are no @code{let}-style local bindings to complicate the issue.)
949
950 The most common use of buffer-local bindings is for major modes to change
951variables that control the behavior of commands. For example, C mode and
952Lisp mode both set the variable @code{paragraph-start} to specify that only
953blank lines separate paragraphs. They do this by making the variable
954buffer-local in the buffer that is being put into C mode or Lisp mode, and
955then setting it to the new value for that mode.
956
957 The usual way to make a buffer-local binding is with
958@code{make-local-variable}, which is what major mode commands use. This
959affects just the current buffer; all other buffers (including those yet to
960be created) continue to share the global value.
961
962@cindex automatically buffer-local
963 A more powerful operation is to mark the variable as
964@dfn{automatically buffer-local} by calling
965@code{make-variable-buffer-local}. You can think of this as making the
966variable local in all buffers, even those yet to be created. More
967precisely, the effect is that setting the variable automatically makes
968the variable local to the current buffer if it is not already so. All
969buffers start out by sharing the global value of the variable as usual,
970but any @code{setq} creates a buffer-local binding for the current
971buffer. The new value is stored in the buffer-local binding, leaving
972the (default) global binding untouched. The global value can no longer
973be changed with @code{setq}; you need to use @code{setq-default} to do
974that.
975
b22f3a19 976 @strong{Warning:} When a variable has local values in one or more
e6512bcf
RS
977buffers, you can get Emacs very confused by binding the variable with
978@code{let}, changing to a different current buffer in which a different
979binding is in effect, and then exiting the @code{let}. This can
980scramble the values of the global and local bindings.
981
982 To preserve your sanity, avoid that series of actions. If you use
983@code{save-excursion} around each piece of code that changes to a
984different current buffer, you will not have this problem. Here is an
985example of what to avoid:
986
987@example
988@group
989(setq foo 'b)
990(set-buffer "a")
991(make-local-variable 'foo)
992@end group
993(setq foo 'a)
994(let ((foo 'temp))
995 (set-buffer "b")
996 @dots{})
997@group
998foo @result{} 'a ; @r{The old buffer-local value from buffer @samp{a}}
999 ; @r{is now the default value.}
1000@end group
1001@group
1002(set-buffer "a")
1003foo @result{} 'temp ; @r{The local value that should be gone}
1004 ; @r{is now the buffer-local value in buffer @samp{a}.}
1005@end group
1006@end example
1007
1008@noindent
1009But @code{save-excursion} as shown here avoids the problem:
1010
1011@example
1012@group
1013(let ((foo 'temp))
1014 (save-excursion
1015 (set-buffer "b")
1016 @var{body}@dots{}))
1017@end group
1018@end example
1019
1020 Note that references to @code{foo} in @var{body} access the
1021buffer-local binding of buffer @samp{b}.
1022
1023 When a file specifies local variable values, these become buffer-local
f57ddf67 1024values when you visit the file. @xref{Auto Major Mode}.
e6512bcf
RS
1025
1026@node Creating Buffer-Local
1027@subsection Creating and Deleting Buffer-Local Bindings
1028
1029@deffn Command make-local-variable variable
1030This function creates a buffer-local binding in the current buffer for
1031@var{variable} (a symbol). Other buffers are not affected. The value
1032returned is @var{variable}.
1033
1034@c Emacs 19 feature
1035The buffer-local value of @var{variable} starts out as the same value
1036@var{variable} previously had. If @var{variable} was void, it remains
1037void.
1038
1039@example
1040@group
1041;; @r{In buffer @samp{b1}:}
1042(setq foo 5) ; @r{Affects all buffers.}
1043 @result{} 5
1044@end group
1045@group
1046(make-local-variable 'foo) ; @r{Now it is local in @samp{b1}.}
1047 @result{} foo
1048@end group
1049@group
1050foo ; @r{That did not change}
1051 @result{} 5 ; @r{the value.}
1052@end group
1053@group
1054(setq foo 6) ; @r{Change the value}
1055 @result{} 6 ; @r{in @samp{b1}.}
1056@end group
1057@group
1058foo
1059 @result{} 6
1060@end group
1061
1062@group
1063;; @r{In buffer @samp{b2}, the value hasn't changed.}
1064(save-excursion
1065 (set-buffer "b2")
1066 foo)
1067 @result{} 5
1068@end group
1069@end example
e8505179
RS
1070
1071Making a variable buffer-local within a @code{let}-binding for that
1072variable does not work. This is because @code{let} does not distinguish
1073between different kinds of bindings; it knows only which variable the
1074binding was made for.
e6512bcf
RS
1075@end deffn
1076
1077@deffn Command make-variable-buffer-local variable
1078This function marks @var{variable} (a symbol) automatically
1079buffer-local, so that any subsequent attempt to set it will make it
1080local to the current buffer at the time.
1081
1082The value returned is @var{variable}.
1083@end deffn
1084
1085@defun buffer-local-variables &optional buffer
1086This function returns a list describing the buffer-local variables in
1087buffer @var{buffer}. It returns an association list (@pxref{Association
1088Lists}) in which each association contains one buffer-local variable and
1089its value. When a buffer-local variable is void in @var{buffer}, then
1090it appears directly in the resulting list. If @var{buffer} is omitted,
1091the current buffer is used.
1092
1093@example
1094@group
1095(make-local-variable 'foobar)
1096(makunbound 'foobar)
1097(make-local-variable 'bind-me)
1098(setq bind-me 69)
1099@end group
1100(setq lcl (buffer-local-variables))
1101 ;; @r{First, built-in variables local in all buffers:}
1102@result{} ((mark-active . nil)
1103 (buffer-undo-list nil)
1104 (mode-name . "Fundamental")
1105 @dots{}
1106@group
1107 ;; @r{Next, non-built-in local variables.}
1108 ;; @r{This one is local and void:}
1109 foobar
1110 ;; @r{This one is local and nonvoid:}
1111 (bind-me . 69))
1112@end group
1113@end example
1114
1115Note that storing new values into the @sc{cdr}s of cons cells in this
1116list does @emph{not} change the local values of the variables.
1117@end defun
1118
1119@deffn Command kill-local-variable variable
1120This function deletes the buffer-local binding (if any) for
1121@var{variable} (a symbol) in the current buffer. As a result, the
1122global (default) binding of @var{variable} becomes visible in this
1123buffer. Usually this results in a change in the value of
1124@var{variable}, since the global value is usually different from the
1125buffer-local value just eliminated.
1126
1127If you kill the local binding of a variable that automatically becomes
1128local when set, this makes the global value visible in the current
1129buffer. However, if you set the variable again, that will once again
1130create a local binding for it.
1131
1132@code{kill-local-variable} returns @var{variable}.
f57ddf67
RS
1133
1134This function is a command because it is sometimes useful to kill one
1135buffer-local variable interactively, just as it is useful to create
1136buffer-local variables interactively.
e6512bcf
RS
1137@end deffn
1138
1139@defun kill-all-local-variables
1140This function eliminates all the buffer-local variable bindings of the
1141current buffer except for variables marked as ``permanent''. As a
1142result, the buffer will see the default values of most variables.
1143
1144This function also resets certain other information pertaining to the
1145buffer: it sets the local keymap to @code{nil}, the syntax table to the
1146value of @code{standard-syntax-table}, and the abbrev table to the value
1147of @code{fundamental-mode-abbrev-table}.
1148
1149Every major mode command begins by calling this function, which has the
1150effect of switching to Fundamental mode and erasing most of the effects
1151of the previous major mode. To ensure that this does its job, the
1152variables that major modes set should not be marked permanent.
1153
1154@code{kill-all-local-variables} returns @code{nil}.
1155@end defun
1156
1157@c Emacs 19 feature
1158@cindex permanent local variable
1159A local variable is @dfn{permanent} if the variable name (a symbol) has a
1160@code{permanent-local} property that is non-@code{nil}. Permanent
1161locals are appropriate for data pertaining to where the file came from
1162or how to save it, rather than with how to edit the contents.
1163
1164@node Default Value
1165@subsection The Default Value of a Buffer-Local Variable
1166@cindex default value
1167
1168 The global value of a variable with buffer-local bindings is also
1169called the @dfn{default} value, because it is the value that is in
1170effect except when specifically overridden.
1171
1172 The functions @code{default-value} and @code{setq-default} access and
1173change a variable's default value regardless of whether the current
1174buffer has a buffer-local binding. For example, you could use
1175@code{setq-default} to change the default setting of
1176@code{paragraph-start} for most buffers; and this would work even when
f57ddf67 1177you are in a C or Lisp mode buffer that has a buffer-local value for
e6512bcf
RS
1178this variable.
1179
1180@c Emacs 19 feature
1181 The special forms @code{defvar} and @code{defconst} also set the
1182default value (if they set the variable at all), rather than any local
1183value.
1184
1185@defun default-value symbol
1186This function returns @var{symbol}'s default value. This is the value
1187that is seen in buffers that do not have their own values for this
1188variable. If @var{symbol} is not buffer-local, this is equivalent to
1189@code{symbol-value} (@pxref{Accessing Variables}).
1190@end defun
1191
1192@c Emacs 19 feature
f57ddf67
RS
1193@defun default-boundp symbol
1194The function @code{default-boundp} tells you whether @var{symbol}'s
e6512bcf
RS
1195default value is nonvoid. If @code{(default-boundp 'foo)} returns
1196@code{nil}, then @code{(default-value 'foo)} would get an error.
1197
1198@code{default-boundp} is to @code{default-value} as @code{boundp} is to
1199@code{symbol-value}.
1200@end defun
1201
1202@defspec setq-default symbol value
1203This sets the default value of @var{symbol} to @var{value}. It does not
1204evaluate @var{symbol}, but does evaluate @var{value}. The value of the
1205@code{setq-default} form is @var{value}.
1206
1207If a @var{symbol} is not buffer-local for the current buffer, and is not
1208marked automatically buffer-local, @code{setq-default} has the same
1209effect as @code{setq}. If @var{symbol} is buffer-local for the current
1210buffer, then this changes the value that other buffers will see (as long
1211as they don't have a buffer-local value), but not the value that the
1212current buffer sees.
1213
1214@example
1215@group
1216;; @r{In buffer @samp{foo}:}
1217(make-local-variable 'local)
1218 @result{} local
1219@end group
1220@group
1221(setq local 'value-in-foo)
1222 @result{} value-in-foo
1223@end group
1224@group
1225(setq-default local 'new-default)
1226 @result{} new-default
1227@end group
1228@group
1229local
1230 @result{} value-in-foo
1231@end group
1232@group
1233(default-value 'local)
1234 @result{} new-default
1235@end group
1236
1237@group
1238;; @r{In (the new) buffer @samp{bar}:}
1239local
1240 @result{} new-default
1241@end group
1242@group
1243(default-value 'local)
1244 @result{} new-default
1245@end group
1246@group
1247(setq local 'another-default)
1248 @result{} another-default
1249@end group
1250@group
1251(default-value 'local)
1252 @result{} another-default
1253@end group
1254
1255@group
1256;; @r{Back in buffer @samp{foo}:}
1257local
1258 @result{} value-in-foo
1259(default-value 'local)
1260 @result{} another-default
1261@end group
1262@end example
1263@end defspec
1264
1265@defun set-default symbol value
1266This function is like @code{setq-default}, except that @var{symbol} is
1267evaluated.
1268
1269@example
1270@group
1271(set-default (car '(a b c)) 23)
1272 @result{} 23
1273@end group
1274@group
1275(default-value 'a)
1276 @result{} 23
1277@end group
1278@end example
1279@end defun
1280