annotate lispref/variables.texi @ 11555:4cc0a5e1bdac

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