annotate man/lispref/variables.texi @ 369:1d62742628b6 r21-1-14

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