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