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