Commit | Line | Data |
---|---|---|
b8d4c8d0 GM |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Emacs Lisp Reference Manual. | |
44e97401 | 3 | @c Copyright (C) 1990-1995, 1998-2012 Free Software Foundation, Inc. |
b8d4c8d0 | 4 | @c See the file elisp.texi for copying conditions. |
b8d4c8d0 GM |
5 | @node Variables, Functions, Control Structures, Top |
6 | @chapter Variables | |
7 | @cindex variable | |
8 | ||
9 | A @dfn{variable} is a name used in a program to stand for a value. | |
1021c761 | 10 | In Lisp, each variable is represented by a Lisp symbol |
735cc5ca CY |
11 | (@pxref{Symbols}). The variable name is simply the symbol's name, and |
12 | the variable's value is stored in the symbol's value cell@footnote{To | |
13 | be precise, under the default @dfn{dynamic binding} rules the value | |
14 | cell always holds the variable's current value, but this is not the | |
15 | case under @dfn{lexical binding} rules. @xref{Variable Scoping}, for | |
16 | details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a | |
17 | symbol as a variable is independent of its use as a function name. | |
1021c761 CY |
18 | |
19 | As previously noted in this manual, a Lisp program is represented | |
20 | primarily by Lisp objects, and only secondarily as text. The textual | |
21 | form of a Lisp program is given by the read syntax of the Lisp objects | |
22 | that constitute the program. Hence, the textual form of a variable in | |
23 | a Lisp program is written using the read syntax for the symbol | |
32770114 | 24 | representing the variable. |
b8d4c8d0 GM |
25 | |
26 | @menu | |
d032d5e7 SM |
27 | * Global Variables:: Variable values that exist permanently, everywhere. |
28 | * Constant Variables:: Certain "variables" have values that never change. | |
29 | * Local Variables:: Variable values that exist only temporarily. | |
30 | * Void Variables:: Symbols that lack values. | |
31 | * Defining Variables:: A definition says a symbol is used as a variable. | |
32 | * Tips for Defining:: Things you should think about when you | |
b8d4c8d0 | 33 | define a variable. |
d032d5e7 | 34 | * Accessing Variables:: Examining values of variables whose names |
b8d4c8d0 | 35 | are known only at run time. |
d032d5e7 SM |
36 | * Setting Variables:: Storing new values in variables. |
37 | * Variable Scoping:: How Lisp chooses among local and global values. | |
38 | * Buffer-Local Variables:: Variable values in effect only in one buffer. | |
39 | * File Local Variables:: Handling local variable lists in files. | |
40 | * Directory Local Variables:: Local variables common to all files in a directory. | |
d032d5e7 | 41 | * Variable Aliases:: Variables that are aliases for other variables. |
b8d4c8d0 GM |
42 | * Variables with Restricted Values:: Non-constant variables whose value can |
43 | @emph{not} be an arbitrary Lisp object. | |
44 | @end menu | |
45 | ||
46 | @node Global Variables | |
47 | @section Global Variables | |
48 | @cindex global variable | |
49 | ||
50 | The simplest way to use a variable is @dfn{globally}. This means that | |
51 | the variable has just one value at a time, and this value is in effect | |
52 | (at least for the moment) throughout the Lisp system. The value remains | |
53 | in effect until you specify a new one. When a new value replaces the | |
54 | old one, no trace of the old value remains in the variable. | |
55 | ||
56 | You specify a value for a symbol with @code{setq}. For example, | |
57 | ||
58 | @example | |
59 | (setq x '(a b)) | |
60 | @end example | |
61 | ||
62 | @noindent | |
63 | gives the variable @code{x} the value @code{(a b)}. Note that | |
32770114 CY |
64 | @code{setq} is a special form (@pxref{Special Forms}); it does not |
65 | evaluate its first argument, the name of the variable, but it does | |
66 | evaluate the second argument, the new value. | |
b8d4c8d0 | 67 | |
32770114 CY |
68 | Once the variable has a value, you can refer to it by using the |
69 | symbol itself as an expression. Thus, | |
b8d4c8d0 GM |
70 | |
71 | @example | |
72 | @group | |
73 | x @result{} (a b) | |
74 | @end group | |
75 | @end example | |
76 | ||
77 | @noindent | |
78 | assuming the @code{setq} form shown above has already been executed. | |
79 | ||
80 | If you do set the same variable again, the new value replaces the old | |
81 | one: | |
82 | ||
83 | @example | |
84 | @group | |
85 | x | |
86 | @result{} (a b) | |
87 | @end group | |
88 | @group | |
89 | (setq x 4) | |
90 | @result{} 4 | |
91 | @end group | |
92 | @group | |
93 | x | |
94 | @result{} 4 | |
95 | @end group | |
96 | @end example | |
97 | ||
98 | @node Constant Variables | |
99 | @section Variables that Never Change | |
7018dbe7 | 100 | @cindex @code{setting-constant} error |
b8d4c8d0 GM |
101 | @cindex keyword symbol |
102 | @cindex variable with constant value | |
103 | @cindex constant variables | |
104 | @cindex symbol that evaluates to itself | |
105 | @cindex symbol with constant value | |
106 | ||
107 | In Emacs Lisp, certain symbols normally evaluate to themselves. These | |
108 | include @code{nil} and @code{t}, as well as any symbol whose name starts | |
109 | with @samp{:} (these are called @dfn{keywords}). These symbols cannot | |
110 | be rebound, nor can their values be changed. Any attempt to set or bind | |
111 | @code{nil} or @code{t} signals a @code{setting-constant} error. The | |
112 | same is true for a keyword (a symbol whose name starts with @samp{:}), | |
113 | if it is interned in the standard obarray, except that setting such a | |
114 | symbol to itself is not an error. | |
115 | ||
116 | @example | |
117 | @group | |
118 | nil @equiv{} 'nil | |
119 | @result{} nil | |
120 | @end group | |
121 | @group | |
122 | (setq nil 500) | |
123 | @error{} Attempt to set constant symbol: nil | |
124 | @end group | |
125 | @end example | |
126 | ||
127 | @defun keywordp object | |
128 | function returns @code{t} if @var{object} is a symbol whose name | |
129 | starts with @samp{:}, interned in the standard obarray, and returns | |
130 | @code{nil} otherwise. | |
131 | @end defun | |
132 | ||
2640fa86 CY |
133 | These constants are fundamentally different from the ``constants'' |
134 | defined using the @code{defconst} special form (@pxref{Defining | |
135 | Variables}). A @code{defconst} form serves to inform human readers | |
136 | that you do not intend to change the value of a variable, but Emacs | |
137 | does not raise an error if you actually change it. | |
138 | ||
b8d4c8d0 GM |
139 | @node Local Variables |
140 | @section Local Variables | |
141 | @cindex binding local variables | |
142 | @cindex local variables | |
143 | @cindex local binding | |
144 | @cindex global binding | |
145 | ||
146 | Global variables have values that last until explicitly superseded | |
1021c761 CY |
147 | with new values. Sometimes it is useful to give a variable a |
148 | @dfn{local value}---a value that takes effect only within a certain | |
149 | part of a Lisp program. When a variable has a local value, we say | |
735cc5ca CY |
150 | that it is @dfn{locally bound} to that value, and that it is a |
151 | @dfn{local variable}. | |
1021c761 CY |
152 | |
153 | For example, when a function is called, its argument variables | |
154 | receive local values, which are the actual arguments supplied to the | |
155 | function call; these local bindings take effect within the body of the | |
156 | function. To take another example, the @code{let} special form | |
157 | explicitly establishes local bindings for specific variables, which | |
158 | take effect within the body of the @code{let} form. | |
b8d4c8d0 GM |
159 | |
160 | We also speak of the @dfn{global binding}, which is where | |
161 | (conceptually) the global value is kept. | |
162 | ||
1021c761 CY |
163 | @cindex shadowing of variables |
164 | Establishing a local binding saves away the variable's previous | |
165 | value (or lack of one). We say that the previous value is | |
166 | @dfn{shadowed}. Both global and local values may be shadowed. If a | |
167 | local binding is in effect, using @code{setq} on the local variable | |
168 | stores the specified value in the local binding. When that local | |
169 | binding is no longer in effect, the previously shadowed value (or lack | |
170 | of one) comes back. | |
171 | ||
b8d4c8d0 | 172 | @cindex current binding |
1021c761 CY |
173 | A variable can have more than one local binding at a time (e.g.@: if |
174 | there are nested @code{let} forms that bind the variable). The | |
175 | @dfn{current binding} is the local binding that is actually in effect. | |
176 | It determines the value returned by evaluating the variable symbol, | |
177 | and it is the binding acted on by @code{setq}. | |
178 | ||
179 | For most purposes, you can think of the current binding as the | |
180 | ``innermost'' local binding, or the global binding if there is no | |
181 | local binding. To be more precise, a rule called the @dfn{scoping | |
182 | rule} determines where in a program a local binding takes effect. The | |
183 | default scoping rule in Emacs Lisp is called @dfn{dynamic scoping}, | |
184 | which simply states that the current binding at any given point in the | |
735cc5ca CY |
185 | execution of a program is the most recently-created binding for that |
186 | variable that still exists. For details about dynamic scoping, and an | |
187 | alternative scoping rule called @dfn{lexical scoping}, @xref{Variable | |
188 | Scoping}. | |
1021c761 CY |
189 | |
190 | The special forms @code{let} and @code{let*} exist to create local | |
191 | bindings: | |
b8d4c8d0 GM |
192 | |
193 | @defspec let (bindings@dots{}) forms@dots{} | |
1021c761 CY |
194 | This special form sets up local bindings for a certain set of |
195 | variables, as specified by @var{bindings}, and then evaluates all of | |
196 | the @var{forms} in textual order. Its return value is the value of | |
197 | the last form in @var{forms}. | |
b8d4c8d0 GM |
198 | |
199 | Each of the @var{bindings} is either @w{(i) a} symbol, in which case | |
1021c761 CY |
200 | that symbol is locally bound to @code{nil}; or @w{(ii) a} list of the |
201 | form @code{(@var{symbol} @var{value-form})}, in which case | |
202 | @var{symbol} is locally bound to the result of evaluating | |
203 | @var{value-form}. If @var{value-form} is omitted, @code{nil} is used. | |
b8d4c8d0 GM |
204 | |
205 | All of the @var{value-form}s in @var{bindings} are evaluated in the | |
206 | order they appear and @emph{before} binding any of the symbols to them. | |
207 | Here is an example of this: @code{z} is bound to the old value of | |
208 | @code{y}, which is 2, not the new value of @code{y}, which is 1. | |
209 | ||
210 | @example | |
211 | @group | |
212 | (setq y 2) | |
213 | @result{} 2 | |
214 | @end group | |
1021c761 | 215 | |
b8d4c8d0 GM |
216 | @group |
217 | (let ((y 1) | |
218 | (z y)) | |
219 | (list y z)) | |
220 | @result{} (1 2) | |
221 | @end group | |
222 | @end example | |
223 | @end defspec | |
224 | ||
225 | @defspec let* (bindings@dots{}) forms@dots{} | |
226 | This special form is like @code{let}, but it binds each variable right | |
227 | after computing its local value, before computing the local value for | |
228 | the next variable. Therefore, an expression in @var{bindings} can | |
1021c761 CY |
229 | refer to the preceding symbols bound in this @code{let*} form. |
230 | Compare the following example with the example above for @code{let}. | |
b8d4c8d0 GM |
231 | |
232 | @example | |
233 | @group | |
234 | (setq y 2) | |
235 | @result{} 2 | |
236 | @end group | |
1021c761 | 237 | |
b8d4c8d0 GM |
238 | @group |
239 | (let* ((y 1) | |
240 | (z y)) ; @r{Use the just-established value of @code{y}.} | |
241 | (list y z)) | |
242 | @result{} (1 1) | |
243 | @end group | |
244 | @end example | |
245 | @end defspec | |
246 | ||
247 | Here is a complete list of the other facilities that create local | |
248 | bindings: | |
249 | ||
250 | @itemize @bullet | |
251 | @item | |
252 | Function calls (@pxref{Functions}). | |
253 | ||
254 | @item | |
255 | Macro calls (@pxref{Macros}). | |
256 | ||
257 | @item | |
258 | @code{condition-case} (@pxref{Errors}). | |
259 | @end itemize | |
260 | ||
261 | Variables can also have buffer-local bindings (@pxref{Buffer-Local | |
e388c68f | 262 | Variables}); a few variables have terminal-local bindings |
3ec61d4e | 263 | (@pxref{Multiple Terminals}). These kinds of bindings work somewhat |
e388c68f | 264 | like ordinary local bindings, but they are localized depending on |
1021c761 | 265 | ``where'' you are in Emacs. |
b8d4c8d0 | 266 | |
01f17ae2 | 267 | @defopt max-specpdl-size |
b8d4c8d0 GM |
268 | @anchor{Definition of max-specpdl-size} |
269 | @cindex variable limit error | |
270 | @cindex evaluation error | |
271 | @cindex infinite recursion | |
272 | This variable defines the limit on the total number of local variable | |
32770114 CY |
273 | bindings and @code{unwind-protect} cleanups (see @ref{Cleanups,, |
274 | Cleaning Up from Nonlocal Exits}) that are allowed before Emacs | |
275 | signals an error (with data @code{"Variable binding depth exceeds | |
b8d4c8d0 GM |
276 | max-specpdl-size"}). |
277 | ||
278 | This limit, with the associated error when it is exceeded, is one way | |
279 | that Lisp avoids infinite recursion on an ill-defined function. | |
280 | @code{max-lisp-eval-depth} provides another limit on depth of nesting. | |
281 | @xref{Definition of max-lisp-eval-depth,, Eval}. | |
282 | ||
1021c761 | 283 | The default value is 1300. Entry to the Lisp debugger increases the |
b8d4c8d0 GM |
284 | value, if there is little room left, to make sure the debugger itself |
285 | has room to execute. | |
01f17ae2 | 286 | @end defopt |
b8d4c8d0 GM |
287 | |
288 | @node Void Variables | |
289 | @section When a Variable is ``Void'' | |
7018dbe7 | 290 | @cindex @code{void-variable} error |
b8d4c8d0 GM |
291 | @cindex void variable |
292 | ||
735cc5ca CY |
293 | We say that a variable is void if its symbol has an unassigned value |
294 | cell (@pxref{Symbol Components}). Under Emacs Lisp's default dynamic | |
295 | binding rules (@pxref{Variable Scoping}), the value cell stores the | |
296 | variable's current (local or global) value. Note that an unassigned | |
297 | value cell is @emph{not} the same as having @code{nil} in the value | |
298 | cell. The symbol @code{nil} is a Lisp object and can be the value of | |
299 | a variable, just as any other object can be; but it is still a value. | |
300 | If a variable is void, trying to evaluate the variable signals a | |
301 | @code{void-variable} error rather than a value. | |
302 | ||
303 | Under lexical binding rules, the value cell only holds the | |
304 | variable's global value, i.e.@: the value outside of any lexical | |
34706efa | 305 | binding construct. When a variable is lexically bound, the local value |
735cc5ca CY |
306 | is determined by the lexical environment; the variable may have a |
307 | local value if its symbol's value cell is unassigned. | |
b8d4c8d0 GM |
308 | |
309 | @defun makunbound symbol | |
1021c761 CY |
310 | This function empties out the value cell of @var{symbol}, making the |
311 | variable void. It returns @var{symbol}. | |
b8d4c8d0 | 312 | |
735cc5ca | 313 | If @var{symbol} has a dynamic local binding, @code{makunbound} voids |
1021c761 CY |
314 | the current binding, and this voidness lasts only as long as the local |
315 | binding is in effect. Afterwards, the previously shadowed local or | |
316 | global binding is reexposed; then the variable will no longer be void, | |
317 | unless the reexposed binding is void too. | |
b8d4c8d0 | 318 | |
1021c761 | 319 | Here are some examples (assuming dynamic binding is in effect): |
b8d4c8d0 GM |
320 | |
321 | @smallexample | |
322 | @group | |
323 | (setq x 1) ; @r{Put a value in the global binding.} | |
324 | @result{} 1 | |
325 | (let ((x 2)) ; @r{Locally bind it.} | |
326 | (makunbound 'x) ; @r{Void the local binding.} | |
327 | x) | |
328 | @error{} Symbol's value as variable is void: x | |
329 | @end group | |
330 | @group | |
331 | x ; @r{The global binding is unchanged.} | |
332 | @result{} 1 | |
333 | ||
334 | (let ((x 2)) ; @r{Locally bind it.} | |
335 | (let ((x 3)) ; @r{And again.} | |
336 | (makunbound 'x) ; @r{Void the innermost-local binding.} | |
337 | x)) ; @r{And refer: it's void.} | |
338 | @error{} Symbol's value as variable is void: x | |
339 | @end group | |
340 | ||
341 | @group | |
342 | (let ((x 2)) | |
343 | (let ((x 3)) | |
344 | (makunbound 'x)) ; @r{Void inner binding, then remove it.} | |
345 | x) ; @r{Now outer @code{let} binding is visible.} | |
346 | @result{} 2 | |
347 | @end group | |
348 | @end smallexample | |
349 | @end defun | |
350 | ||
b8d4c8d0 | 351 | @defun boundp variable |
1021c761 CY |
352 | This function returns @code{t} if @var{variable} (a symbol) is not |
353 | void, and @code{nil} if it is void. | |
354 | ||
355 | Here are some examples (assuming dynamic binding is in effect): | |
b8d4c8d0 GM |
356 | |
357 | @smallexample | |
358 | @group | |
359 | (boundp 'abracadabra) ; @r{Starts out void.} | |
360 | @result{} nil | |
361 | @end group | |
362 | @group | |
363 | (let ((abracadabra 5)) ; @r{Locally bind it.} | |
364 | (boundp 'abracadabra)) | |
365 | @result{} t | |
366 | @end group | |
367 | @group | |
368 | (boundp 'abracadabra) ; @r{Still globally void.} | |
369 | @result{} nil | |
370 | @end group | |
371 | @group | |
372 | (setq abracadabra 5) ; @r{Make it globally nonvoid.} | |
373 | @result{} 5 | |
374 | @end group | |
375 | @group | |
376 | (boundp 'abracadabra) | |
377 | @result{} t | |
378 | @end group | |
379 | @end smallexample | |
380 | @end defun | |
381 | ||
382 | @node Defining Variables | |
383 | @section Defining Global Variables | |
384 | @cindex variable definition | |
385 | ||
1021c761 CY |
386 | A @dfn{variable definition} is a construct that announces your |
387 | intention to use a symbol as a global variable. It uses the special | |
388 | forms @code{defvar} or @code{defconst}, which are documented below. | |
389 | ||
390 | A variable definition serves three purposes. First, it informs | |
391 | people who read the code that the symbol is @emph{intended} to be used | |
392 | a certain way (as a variable). Second, it informs the Lisp system of | |
393 | this, optionally supplying an initial value and a documentation | |
394 | string. Third, it provides information to programming tools such as | |
395 | @command{etags}, allowing them to find where the variable was defined. | |
396 | ||
397 | The difference between @code{defconst} and @code{defvar} is mainly a | |
398 | matter of intent, serving to inform human readers of whether the value | |
399 | should ever change. Emacs Lisp does not actually prevent you from | |
400 | changing the value of a variable defined with @code{defconst}. One | |
401 | notable difference between the two forms is that @code{defconst} | |
402 | unconditionally initializes the variable, whereas @code{defvar} | |
403 | initializes it only if it is originally void. | |
404 | ||
405 | To define a customizable variable, you should use @code{defcustom} | |
406 | (which calls @code{defvar} as a subroutine). @xref{Customization}. | |
b8d4c8d0 GM |
407 | |
408 | @defspec defvar symbol [value [doc-string]] | |
1021c761 CY |
409 | This special form defines @var{symbol} as a variable. Note that |
410 | @var{symbol} is not evaluated; the symbol to be defined should appear | |
411 | explicitly in the @code{defvar} form. The variable is marked as | |
412 | @dfn{special}, meaning that it should always be dynamically bound | |
413 | (@pxref{Variable Scoping}). | |
b8d4c8d0 GM |
414 | |
415 | If @var{symbol} is void and @var{value} is specified, @code{defvar} | |
1021c761 CY |
416 | evaluates @var{value} and sets @var{symbol} to the result. But if |
417 | @var{symbol} already has a value (i.e.@: it is not void), @var{value} | |
418 | is not even evaluated, and @var{symbol}'s value remains unchanged. If | |
419 | @var{value} is omitted, the value of @var{symbol} is not changed in | |
420 | any case. | |
b8d4c8d0 GM |
421 | |
422 | If @var{symbol} has a buffer-local binding in the current buffer, | |
423 | @code{defvar} operates on the default value, which is buffer-independent, | |
424 | not the current (buffer-local) binding. It sets the default value if | |
425 | the default value is void. @xref{Buffer-Local Variables}. | |
426 | ||
427 | When you evaluate a top-level @code{defvar} form with @kbd{C-M-x} in | |
428 | Emacs Lisp mode (@code{eval-defun}), a special feature of | |
429 | @code{eval-defun} arranges to set the variable unconditionally, without | |
430 | testing whether its value is void. | |
431 | ||
1021c761 CY |
432 | If the @var{doc-string} argument is supplied, it specifies the |
433 | documentation string for the variable (stored in the symbol's | |
434 | @code{variable-documentation} property). @xref{Documentation}. | |
b8d4c8d0 GM |
435 | |
436 | Here are some examples. This form defines @code{foo} but does not | |
437 | initialize it: | |
438 | ||
439 | @example | |
440 | @group | |
441 | (defvar foo) | |
442 | @result{} foo | |
443 | @end group | |
444 | @end example | |
445 | ||
446 | This example initializes the value of @code{bar} to @code{23}, and gives | |
447 | it a documentation string: | |
448 | ||
449 | @example | |
450 | @group | |
451 | (defvar bar 23 | |
452 | "The normal weight of a bar.") | |
453 | @result{} bar | |
454 | @end group | |
455 | @end example | |
456 | ||
b8d4c8d0 GM |
457 | The @code{defvar} form returns @var{symbol}, but it is normally used |
458 | at top level in a file where its value does not matter. | |
459 | @end defspec | |
460 | ||
2640fa86 | 461 | @cindex constant variables |
b8d4c8d0 GM |
462 | @defspec defconst symbol value [doc-string] |
463 | This special form defines @var{symbol} as a value and initializes it. | |
464 | It informs a person reading your code that @var{symbol} has a standard | |
465 | global value, established here, that should not be changed by the user | |
466 | or by other programs. Note that @var{symbol} is not evaluated; the | |
467 | symbol to be defined must appear explicitly in the @code{defconst}. | |
468 | ||
1021c761 CY |
469 | The @code{defconst} form, like @code{defvar}, marks the variable as |
470 | @dfn{special}, meaning that it should always be dynamically bound | |
471 | (@pxref{Variable Scoping}). In addition, it marks the variable as | |
472 | risky (@pxref{File Local Variables}). | |
473 | ||
b8d4c8d0 GM |
474 | @code{defconst} always evaluates @var{value}, and sets the value of |
475 | @var{symbol} to the result. If @var{symbol} does have a buffer-local | |
476 | binding in the current buffer, @code{defconst} sets the default value, | |
477 | not the buffer-local value. (But you should not be making | |
478 | buffer-local bindings for a symbol that is defined with | |
479 | @code{defconst}.) | |
480 | ||
44e97401 | 481 | An example of the use of @code{defconst} is Emacs's definition of |
ec8a6295 CY |
482 | @code{float-pi}---the mathematical constant @math{pi}, which ought not |
483 | to be changed by anyone (attempts by the Indiana State Legislature | |
484 | notwithstanding). As the second form illustrates, however, | |
485 | @code{defconst} is only advisory. | |
b8d4c8d0 GM |
486 | |
487 | @example | |
488 | @group | |
ec8a6295 CY |
489 | (defconst float-pi 3.141592653589793 "The value of Pi.") |
490 | @result{} float-pi | |
b8d4c8d0 GM |
491 | @end group |
492 | @group | |
ec8a6295 CY |
493 | (setq float-pi 3) |
494 | @result{} float-pi | |
b8d4c8d0 GM |
495 | @end group |
496 | @group | |
ec8a6295 | 497 | float-pi |
b8d4c8d0 GM |
498 | @result{} 3 |
499 | @end group | |
500 | @end example | |
501 | @end defspec | |
502 | ||
1021c761 CY |
503 | @strong{Warning:} If you use a @code{defconst} or @code{defvar} |
504 | special form while the variable has a local binding (made with | |
505 | @code{let}, or a function argument), it sets the local binding rather | |
506 | than the global binding. This is not what you usually want. To | |
507 | prevent this, use these special forms at top level in a file, where | |
508 | normally no local binding is in effect, and make sure to load the file | |
509 | before making a local binding for the variable. | |
b8d4c8d0 GM |
510 | |
511 | @node Tips for Defining | |
512 | @section Tips for Defining Variables Robustly | |
513 | ||
514 | When you define a variable whose value is a function, or a list of | |
515 | functions, use a name that ends in @samp{-function} or | |
516 | @samp{-functions}, respectively. | |
517 | ||
518 | There are several other variable name conventions; | |
519 | here is a complete list: | |
520 | ||
521 | @table @samp | |
522 | @item @dots{}-hook | |
523 | The variable is a normal hook (@pxref{Hooks}). | |
524 | ||
525 | @item @dots{}-function | |
526 | The value is a function. | |
527 | ||
528 | @item @dots{}-functions | |
529 | The value is a list of functions. | |
530 | ||
531 | @item @dots{}-form | |
532 | The value is a form (an expression). | |
533 | ||
534 | @item @dots{}-forms | |
535 | The value is a list of forms (expressions). | |
536 | ||
537 | @item @dots{}-predicate | |
538 | The value is a predicate---a function of one argument that returns | |
539 | non-@code{nil} for ``good'' arguments and @code{nil} for ``bad'' | |
540 | arguments. | |
541 | ||
542 | @item @dots{}-flag | |
543 | The value is significant only as to whether it is @code{nil} or not. | |
cc5a5e2d | 544 | Since such variables often end up acquiring more values over time, |
0befcaca | 545 | this convention is not strongly recommended. |
b8d4c8d0 GM |
546 | |
547 | @item @dots{}-program | |
548 | The value is a program name. | |
549 | ||
550 | @item @dots{}-command | |
551 | The value is a whole shell command. | |
552 | ||
553 | @item @dots{}-switches | |
554 | The value specifies options for a command. | |
555 | @end table | |
556 | ||
557 | When you define a variable, always consider whether you should mark | |
32770114 | 558 | it as ``safe'' or ``risky''; see @ref{File Local Variables}. |
b8d4c8d0 GM |
559 | |
560 | When defining and initializing a variable that holds a complicated | |
561 | value (such as a keymap with bindings in it), it's best to put the | |
562 | entire computation of the value into the @code{defvar}, like this: | |
563 | ||
564 | @example | |
565 | (defvar my-mode-map | |
566 | (let ((map (make-sparse-keymap))) | |
567 | (define-key map "\C-c\C-a" 'my-command) | |
568 | @dots{} | |
569 | map) | |
570 | @var{docstring}) | |
571 | @end example | |
572 | ||
573 | @noindent | |
574 | This method has several benefits. First, if the user quits while | |
575 | loading the file, the variable is either still uninitialized or | |
576 | initialized properly, never in-between. If it is still uninitialized, | |
577 | reloading the file will initialize it properly. Second, reloading the | |
578 | file once the variable is initialized will not alter it; that is | |
1021c761 CY |
579 | important if the user has run hooks to alter part of the contents |
580 | (such as, to rebind keys). Third, evaluating the @code{defvar} form | |
581 | with @kbd{C-M-x} will reinitialize the map completely. | |
b8d4c8d0 GM |
582 | |
583 | Putting so much code in the @code{defvar} form has one disadvantage: | |
584 | it puts the documentation string far away from the line which names the | |
585 | variable. Here's a safe way to avoid that: | |
586 | ||
587 | @example | |
588 | (defvar my-mode-map nil | |
589 | @var{docstring}) | |
590 | (unless my-mode-map | |
591 | (let ((map (make-sparse-keymap))) | |
592 | (define-key map "\C-c\C-a" 'my-command) | |
593 | @dots{} | |
594 | (setq my-mode-map map))) | |
595 | @end example | |
596 | ||
597 | @noindent | |
598 | This has all the same advantages as putting the initialization inside | |
599 | the @code{defvar}, except that you must type @kbd{C-M-x} twice, once on | |
600 | each form, if you do want to reinitialize the variable. | |
601 | ||
b8d4c8d0 GM |
602 | @node Accessing Variables |
603 | @section Accessing Variable Values | |
604 | ||
605 | The usual way to reference a variable is to write the symbol which | |
1021c761 CY |
606 | names it. @xref{Symbol Forms}. |
607 | ||
608 | Occasionally, you may want to reference a variable which is only | |
609 | determined at run time. In that case, you cannot specify the variable | |
610 | name in the text of the program. You can use the @code{symbol-value} | |
611 | function to extract the value. | |
b8d4c8d0 GM |
612 | |
613 | @defun symbol-value symbol | |
735cc5ca CY |
614 | This function returns the value stored in @var{symbol}'s value cell. |
615 | This is where the variable's current (dynamic) value is stored. If | |
616 | the variable has no local binding, this is simply its global value. | |
617 | If the variable is void, a @code{void-variable} error is signaled. | |
1021c761 CY |
618 | |
619 | If the variable is lexically bound, the value reported by | |
735cc5ca CY |
620 | @code{symbol-value} is not necessarily the same as the variable's |
621 | lexical value, which is determined by the lexical environment rather | |
622 | than the symbol's value cell. @xref{Variable Scoping}. | |
b8d4c8d0 GM |
623 | |
624 | @example | |
625 | @group | |
626 | (setq abracadabra 5) | |
627 | @result{} 5 | |
628 | @end group | |
629 | @group | |
630 | (setq foo 9) | |
631 | @result{} 9 | |
632 | @end group | |
633 | ||
634 | @group | |
635 | ;; @r{Here the symbol @code{abracadabra}} | |
636 | ;; @r{is the symbol whose value is examined.} | |
637 | (let ((abracadabra 'foo)) | |
638 | (symbol-value 'abracadabra)) | |
639 | @result{} foo | |
640 | @end group | |
641 | ||
642 | @group | |
643 | ;; @r{Here, the value of @code{abracadabra},} | |
644 | ;; @r{which is @code{foo},} | |
645 | ;; @r{is the symbol whose value is examined.} | |
646 | (let ((abracadabra 'foo)) | |
647 | (symbol-value abracadabra)) | |
648 | @result{} 9 | |
649 | @end group | |
650 | ||
651 | @group | |
652 | (symbol-value 'abracadabra) | |
653 | @result{} 5 | |
654 | @end group | |
655 | @end example | |
b8d4c8d0 GM |
656 | @end defun |
657 | ||
658 | @node Setting Variables | |
1021c761 | 659 | @section Setting Variable Values |
b8d4c8d0 GM |
660 | |
661 | The usual way to change the value of a variable is with the special | |
662 | form @code{setq}. When you need to compute the choice of variable at | |
663 | run time, use the function @code{set}. | |
664 | ||
665 | @defspec setq [symbol form]@dots{} | |
666 | This special form is the most common method of changing a variable's | |
667 | value. Each @var{symbol} is given a new value, which is the result of | |
1021c761 CY |
668 | evaluating the corresponding @var{form}. The current binding of the |
669 | symbol is changed. | |
b8d4c8d0 GM |
670 | |
671 | @code{setq} does not evaluate @var{symbol}; it sets the symbol that you | |
672 | write. We say that this argument is @dfn{automatically quoted}. The | |
16152b76 | 673 | @samp{q} in @code{setq} stands for ``quoted''. |
b8d4c8d0 GM |
674 | |
675 | The value of the @code{setq} form is the value of the last @var{form}. | |
676 | ||
677 | @example | |
678 | @group | |
679 | (setq x (1+ 2)) | |
680 | @result{} 3 | |
681 | @end group | |
682 | x ; @r{@code{x} now has a global value.} | |
683 | @result{} 3 | |
684 | @group | |
685 | (let ((x 5)) | |
686 | (setq x 6) ; @r{The local binding of @code{x} is set.} | |
687 | x) | |
688 | @result{} 6 | |
689 | @end group | |
690 | x ; @r{The global value is unchanged.} | |
691 | @result{} 3 | |
692 | @end example | |
693 | ||
694 | Note that the first @var{form} is evaluated, then the first | |
695 | @var{symbol} is set, then the second @var{form} is evaluated, then the | |
696 | second @var{symbol} is set, and so on: | |
697 | ||
698 | @example | |
699 | @group | |
700 | (setq x 10 ; @r{Notice that @code{x} is set before} | |
701 | y (1+ x)) ; @r{the value of @code{y} is computed.} | |
702 | @result{} 11 | |
703 | @end group | |
704 | @end example | |
705 | @end defspec | |
706 | ||
707 | @defun set symbol value | |
1021c761 CY |
708 | This function puts @var{value} in the value cell of @var{symbol}. |
709 | Since it is a function rather than a special form, the expression | |
710 | written for @var{symbol} is evaluated to obtain the symbol to set. | |
711 | The return value is @var{value}. | |
712 | ||
713 | When dynamic variable binding is in effect (the default), @code{set} | |
714 | has the same effect as @code{setq}, apart from the fact that | |
715 | @code{set} evaluates its @var{symbol} argument whereas @code{setq} | |
716 | does not. But when a variable is lexically bound, @code{set} affects | |
717 | its @emph{dynamic} value, whereas @code{setq} affects its current | |
718 | (lexical) value. @xref{Variable Scoping}. | |
b8d4c8d0 GM |
719 | |
720 | @example | |
721 | @group | |
722 | (set one 1) | |
723 | @error{} Symbol's value as variable is void: one | |
724 | @end group | |
725 | @group | |
726 | (set 'one 1) | |
727 | @result{} 1 | |
728 | @end group | |
729 | @group | |
730 | (set 'two 'one) | |
731 | @result{} one | |
732 | @end group | |
733 | @group | |
734 | (set two 2) ; @r{@code{two} evaluates to symbol @code{one}.} | |
735 | @result{} 2 | |
736 | @end group | |
737 | @group | |
738 | one ; @r{So it is @code{one} that was set.} | |
739 | @result{} 2 | |
740 | (let ((one 1)) ; @r{This binding of @code{one} is set,} | |
741 | (set 'one 3) ; @r{not the global value.} | |
742 | one) | |
743 | @result{} 3 | |
744 | @end group | |
745 | @group | |
746 | one | |
747 | @result{} 2 | |
748 | @end group | |
749 | @end example | |
750 | ||
751 | If @var{symbol} is not actually a symbol, a @code{wrong-type-argument} | |
752 | error is signaled. | |
753 | ||
754 | @example | |
755 | (set '(x y) 'z) | |
756 | @error{} Wrong type argument: symbolp, (x y) | |
757 | @end example | |
b8d4c8d0 GM |
758 | @end defun |
759 | ||
760 | @node Variable Scoping | |
761 | @section Scoping Rules for Variable Bindings | |
762 | ||
1021c761 CY |
763 | When you create a local binding for a variable, that binding takes |
764 | effect only within a limited portion of the program (@pxref{Local | |
765 | Variables}). This section describes exactly what this means. | |
b8d4c8d0 GM |
766 | |
767 | @cindex scope | |
768 | @cindex extent | |
1021c761 CY |
769 | Each local binding has a certain @dfn{scope} and @dfn{extent}. |
770 | @dfn{Scope} refers to @emph{where} in the textual source code the | |
771 | binding can be accessed. @dfn{Extent} refers to @emph{when}, as the | |
772 | program is executing, the binding exists. | |
773 | ||
774 | @cindex dynamic binding | |
775 | @cindex indefinite scope | |
776 | @cindex dynamic extent | |
777 | By default, the local bindings that Emacs creates are @dfn{dynamic | |
778 | bindings}. Such a binding has @dfn{indefinite scope}, meaning that | |
779 | any part of the program can potentially access the variable binding. | |
780 | It also has @dfn{dynamic extent}, meaning that the binding lasts only | |
781 | while the binding construct (such as the body of a @code{let} form) is | |
782 | being executed. | |
783 | ||
784 | @cindex lexical binding | |
785 | @cindex lexical scope | |
786 | @cindex indefinite extent | |
787 | Emacs can optionally create @dfn{lexical bindings}. A lexical | |
788 | binding has @dfn{lexical scope}, meaning that any reference to the | |
789 | variable must be located textually within the binding construct. It | |
790 | also has @dfn{indefinite extent}, meaning that under some | |
791 | circumstances the binding can live on even after the binding construct | |
792 | has finished executing, by means of special objects called | |
793 | @dfn{closures}. | |
794 | ||
795 | The following subsections describe dynamic binding and lexical | |
796 | binding in greater detail, and how to enable lexical binding in Emacs | |
797 | Lisp programs. | |
b8d4c8d0 GM |
798 | |
799 | @menu | |
1021c761 CY |
800 | * Dynamic Binding:: The default for binding local variables in Emacs. |
801 | * Dynamic Binding Tips:: Avoiding problems with dynamic binding. | |
802 | * Lexical Binding:: A different type of local variable binding. | |
803 | * Using Lexical Binding:: How to enable lexical binding. | |
b8d4c8d0 GM |
804 | @end menu |
805 | ||
1021c761 CY |
806 | @node Dynamic Binding |
807 | @subsection Dynamic Binding | |
808 | ||
809 | By default, the local variable bindings made by Emacs are dynamic | |
810 | bindings. When a variable is dynamically bound, its current binding | |
811 | at any point in the execution of the Lisp program is simply the most | |
812 | recently-created dynamic local binding for that symbol, or the global | |
813 | binding if there is no such local binding. | |
b8d4c8d0 | 814 | |
1021c761 CY |
815 | Dynamic bindings have indefinite scope and dynamic extent, as shown |
816 | by the following example: | |
b8d4c8d0 GM |
817 | |
818 | @example | |
819 | @group | |
1021c761 | 820 | (defvar x -99) ; @r{@code{x} receives an initial value of -99.} |
b8d4c8d0 | 821 | |
1021c761 CY |
822 | (defun getx () |
823 | x) ; @r{@code{x} is used ``free'' in this function.} | |
824 | ||
825 | (let ((x 1)) ; @r{@code{x} is dynamically bound.} | |
826 | (getx)) | |
827 | @result{} 1 | |
828 | ||
829 | ;; @r{After the @code{let} form finishes, @code{x} reverts to its} | |
830 | ;; @r{previous value, which is -99.} | |
831 | ||
832 | (getx) | |
833 | @result{} -99 | |
b8d4c8d0 GM |
834 | @end group |
835 | @end example | |
836 | ||
1021c761 CY |
837 | @noindent |
838 | The function @code{getx} refers to @code{x}. This is a ``free'' | |
839 | reference, in the sense that there is no binding for @code{x} within | |
840 | that @code{defun} construct itself. When we call @code{getx} from | |
841 | within a @code{let} form in which @code{x} is (dynamically) bound, it | |
842 | retrieves the local value of @code{x} (i.e.@: 1). But when we call | |
843 | @code{getx} outside the @code{let} form, it retrieves the global value | |
844 | of @code{x} (i.e.@: -99). | |
b8d4c8d0 | 845 | |
1021c761 CY |
846 | Here is another example, which illustrates setting a dynamically |
847 | bound variable using @code{setq}: | |
b8d4c8d0 GM |
848 | |
849 | @example | |
850 | @group | |
1021c761 CY |
851 | (defvar x -99) ; @r{@code{x} receives an initial value of -99.} |
852 | ||
853 | (defun addx () | |
854 | (setq x (1+ x))) ; @r{Add 1 to @code{x} and return its new value.} | |
855 | ||
856 | (let ((x 1)) | |
857 | (addx) | |
858 | (addx)) | |
859 | @result{} 3 ; @r{The two @code{addx} calls add to @code{x} twice.} | |
860 | ||
861 | ;; @r{After the @code{let} form finishes, @code{x} reverts to its} | |
862 | ;; @r{previous value, which is -99.} | |
863 | ||
864 | (addx) | |
865 | @result{} -98 | |
b8d4c8d0 GM |
866 | @end group |
867 | @end example | |
868 | ||
1021c761 CY |
869 | Dynamic binding is implemented in Emacs Lisp in a simple way. Each |
870 | symbol has a value cell, which specifies its current dynamic value (or | |
871 | absence of value). @xref{Symbol Components}. When a symbol is given | |
872 | a dynamic local binding, Emacs records the contents of the value cell | |
873 | (or absence thereof) in a stack, and stores the new local value in the | |
874 | value cell. When the binding construct finishes executing, Emacs pops | |
875 | the old value off the stack, and puts it in the value cell. | |
b8d4c8d0 | 876 | |
1021c761 CY |
877 | @node Dynamic Binding Tips |
878 | @subsection Proper Use of Dynamic Binding | |
b8d4c8d0 | 879 | |
1021c761 CY |
880 | Dynamic binding is a powerful feature, as it allows programs to |
881 | refer to variables that are not defined within their local textual | |
882 | scope. However, if used without restraint, this can also make | |
883 | programs hard to understand. There are two clean ways to use this | |
884 | technique: | |
b8d4c8d0 | 885 | |
1021c761 CY |
886 | @itemize @bullet |
887 | @item | |
888 | If a variable has no global definition, use it as a local variable | |
889 | only within a binding construct, e.g.@: the body of the @code{let} | |
890 | form where the variable was bound, or the body of the function for an | |
891 | argument variable. If this convention is followed consistently | |
892 | throughout a program, the value of the variable will not affect, nor | |
893 | be affected by, any uses of the same variable symbol elsewhere in the | |
894 | program. | |
895 | ||
896 | @item | |
897 | Otherwise, define the variable with @code{defvar}, @code{defconst}, or | |
898 | @code{defcustom}. @xref{Defining Variables}. Usually, the definition | |
899 | should be at top-level in an Emacs Lisp file. As far as possible, it | |
900 | should include a documentation string which explains the meaning and | |
901 | purpose of the variable. You should also choose the variable's name | |
902 | to avoid name conflicts (@pxref{Coding Conventions}). | |
903 | ||
904 | Then you can bind the variable anywhere in a program, knowing reliably | |
905 | what the effect will be. Wherever you encounter the variable, it will | |
906 | be easy to refer back to the definition, e.g.@: via the @kbd{C-h v} | |
907 | command (provided the variable definition has been loaded into Emacs). | |
908 | @xref{Name Help,,, emacs, The GNU Emacs Manual}. | |
909 | ||
910 | For example, it is common to use local bindings for customizable | |
911 | variables like @code{case-fold-search}: | |
b8d4c8d0 GM |
912 | |
913 | @example | |
1021c761 CY |
914 | @group |
915 | (defun search-for-abc () | |
916 | "Search for the string \"abc\", ignoring case differences." | |
917 | (let ((case-fold-search nil)) | |
918 | (re-search-forward "abc"))) | |
919 | @end group | |
b8d4c8d0 | 920 | @end example |
1021c761 | 921 | @end itemize |
b8d4c8d0 | 922 | |
1021c761 CY |
923 | @node Lexical Binding |
924 | @subsection Lexical Binding | |
b8d4c8d0 | 925 | |
1021c761 CY |
926 | Optionally, you can create lexical bindings in Emacs Lisp. A |
927 | lexically bound variable has @dfn{lexical scope}, meaning that any | |
928 | reference to the variable must be located textually within the binding | |
b8d4c8d0 GM |
929 | construct. |
930 | ||
1021c761 CY |
931 | Here is an example |
932 | @iftex | |
933 | (see the next subsection, for how to actually enable lexical binding): | |
934 | @end iftex | |
935 | @ifnottex | |
936 | (@pxref{Using Lexical Binding}, for how to actually enable lexical binding): | |
937 | @end ifnottex | |
b8d4c8d0 | 938 | |
1021c761 CY |
939 | @example |
940 | @group | |
1021c761 CY |
941 | (let ((x 1)) ; @r{@code{x} is lexically bound.} |
942 | (+ x 3)) | |
943 | @result{} 4 | |
b8d4c8d0 | 944 | |
735cc5ca CY |
945 | (defun getx () |
946 | x) ; @r{@code{x} is used ``free'' in this function.} | |
947 | ||
1021c761 CY |
948 | (let ((x 1)) ; @r{@code{x} is lexically bound.} |
949 | (getx)) | |
950 | @error{} Symbol's value as variable is void: x | |
951 | @end group | |
952 | @end example | |
b8d4c8d0 | 953 | |
1021c761 CY |
954 | @noindent |
955 | Here, the variable @code{x} has no global value. When it is lexically | |
956 | bound within a @code{let} form, it can be used in the textual confines | |
957 | of that @code{let} form. But it can @emph{not} be used from within a | |
958 | @code{getx} function called from the @code{let} form, since the | |
959 | function definition of @code{getx} occurs outside the @code{let} form | |
960 | itself. | |
961 | ||
962 | @cindex lexical environment | |
963 | Here is how lexical binding works. Each binding construct defines a | |
964 | @dfn{lexical environment}, specifying the symbols that are bound | |
965 | within the construct and their local values. When the Lisp evaluator | |
966 | wants the current value of a variable, it looks first in the lexical | |
967 | environment; if the variable is not specified in there, it looks in | |
735cc5ca | 968 | the symbol's value cell, where the dynamic value is stored. |
1021c761 CY |
969 | |
970 | @cindex closures | |
971 | Lexical bindings have indefinite extent. Even after a binding | |
972 | construct has finished executing, its lexical environment can be | |
973 | ``kept around'' in Lisp objects called @dfn{closures}. A closure is | |
735cc5ca CY |
974 | created when you create a named or anonymous function with lexical |
975 | binding enabled. @xref{Closures}, for details. | |
1021c761 | 976 | |
735cc5ca CY |
977 | When a closure is called as a function, any lexical variable |
978 | references within its definition use the retained lexical environment. | |
979 | Here is an example: | |
b8d4c8d0 | 980 | |
1021c761 CY |
981 | @example |
982 | (defvar my-ticker nil) ; @r{We will use this dynamically bound} | |
983 | ; @r{variable to store a closure.} | |
b8d4c8d0 | 984 | |
1021c761 CY |
985 | (let ((x 0)) ; @r{@code{x} is lexically bound.} |
986 | (setq my-ticker (lambda () | |
987 | (setq x (1+ x))))) | |
988 | @result{} (closure ((x . 0) t) () | |
989 | (1+ x)) | |
b8d4c8d0 | 990 | |
1021c761 CY |
991 | (funcall my-ticker) |
992 | @result{} 1 | |
b8d4c8d0 | 993 | |
1021c761 CY |
994 | (funcall my-ticker) |
995 | @result{} 2 | |
b8d4c8d0 | 996 | |
1021c761 CY |
997 | (funcall my-ticker) |
998 | @result{} 3 | |
d032d5e7 | 999 | |
1021c761 CY |
1000 | x ; @r{Note that @code{x} has no global value.} |
1001 | @error{} Symbol's value as variable is void: x | |
1002 | @end example | |
d032d5e7 | 1003 | |
1021c761 CY |
1004 | @noindent |
1005 | The @code{let} binding defines a lexical environment in which the | |
1006 | variable @code{x} is locally bound to 0. Within this binding | |
1007 | construct, we define a lambda expression which increments @code{x} by | |
1008 | one and returns the incremented value. This lambda expression is | |
1009 | automatically turned into a closure, in which the lexical environment | |
1010 | lives on even after the @code{let} binding construct has exited. Each | |
1011 | time we evaluate the closure, it increments @code{x}, using the | |
1012 | binding of @code{x} in that lexical environment. | |
1013 | ||
1014 | Note that functions like @code{symbol-value}, @code{boundp}, and | |
1015 | @code{set} only retrieve or modify a variable's dynamic binding | |
1016 | (i.e.@: the contents of its symbol's value cell). Also, the code in | |
1017 | the body of a @code{defun} or @code{defmacro} cannot refer to | |
1018 | surrounding lexical variables. | |
1019 | ||
1020 | Currently, lexical binding is not much used within the Emacs | |
1021 | sources. However, we expect its importance to increase in the future. | |
1022 | Lexical binding opens up a lot more opportunities for optimization, so | |
1023 | Emacs Lisp code that makes use of lexical binding is likely to run | |
1024 | faster in future Emacs versions. Such code is also much more friendly | |
1025 | to concurrency, which we want to add to Emacs in the near future. | |
1026 | ||
1027 | @node Using Lexical Binding | |
1028 | @subsection Using Lexical Binding | |
1029 | ||
1030 | When loading an Emacs Lisp file or evaluating a Lisp buffer, lexical | |
1031 | binding is enabled if the buffer-local variable @code{lexical-binding} | |
1032 | is non-@code{nil}: | |
d032d5e7 SM |
1033 | |
1034 | @defvar lexical-binding | |
1021c761 CY |
1035 | If this buffer-local variable is non-@code{nil}, Emacs Lisp files and |
1036 | buffers are evaluated using lexical binding instead of dynamic | |
1037 | binding. (However, special variables are still dynamically bound; see | |
1038 | below.) If @code{nil}, dynamic binding is used for all local | |
1039 | variables. This variable is typically set for a whole Emacs Lisp | |
1040 | file, as a file local variable (@pxref{File Local Variables}). | |
48da7392 GM |
1041 | Note that unlike other such variables, this one must be set in the |
1042 | first line of a file. | |
d032d5e7 SM |
1043 | @end defvar |
1044 | ||
1021c761 CY |
1045 | @noindent |
1046 | When evaluating Emacs Lisp code directly using an @code{eval} call, | |
1047 | lexical binding is enabled if the @var{lexical} argument to | |
1048 | @code{eval} is non-@code{nil}. @xref{Eval}. | |
1049 | ||
1050 | @cindex special variables | |
1051 | Even when lexical binding is enabled, certain variables will | |
1052 | continue to be dynamically bound. These are called @dfn{special | |
1053 | variables}. Every variable that has been defined with @code{defvar}, | |
1054 | @code{defcustom} or @code{defconst} is a special variable | |
1055 | (@pxref{Defining Variables}). All other variables are subject to | |
1056 | lexical binding. | |
1057 | ||
d032d5e7 | 1058 | @defun special-variable-p SYMBOL |
1021c761 CY |
1059 | This function returns non-@code{nil} if @var{symbol} is a special |
1060 | variable (i.e.@: it has a @code{defvar}, @code{defcustom}, or | |
1061 | @code{defconst} variable definition). Otherwise, the return value is | |
1062 | @code{nil}. | |
d032d5e7 SM |
1063 | @end defun |
1064 | ||
1021c761 CY |
1065 | The use of a special variable as a formal argument in a function is |
1066 | discouraged. Doing so gives rise to unspecified behavior when lexical | |
1067 | binding mode is enabled (it may use lexical binding sometimes, and | |
1068 | dynamic binding other times). | |
1069 | ||
1070 | Converting an Emacs Lisp program to lexical binding is pretty easy. | |
1071 | First, add a file-local variable setting of @code{lexical-binding} to | |
1072 | @code{t} in the Emacs Lisp source file. Second, check that every | |
1073 | variable in the program which needs to be dynamically bound has a | |
1074 | variable definition, so that it is not inadvertently bound lexically. | |
1075 | ||
1076 | A simple way to find out which variables need a variable definition | |
1077 | is to byte-compile the source file. @xref{Byte Compilation}. If a | |
1078 | non-special variable is used outside of a @code{let} form, the | |
1079 | byte-compiler will warn about reference or assignment to a ``free | |
1080 | variable''. If a non-special variable is bound but not used within a | |
1081 | @code{let} form, the byte-compiler will warn about an ``unused lexical | |
1082 | variable''. The byte-compiler will also issue a warning if you use a | |
1083 | special variable as a function argument. | |
1084 | ||
1085 | (To silence byte-compiler warnings about unused variables, just use | |
1086 | a variable name that start with an underscore. The byte-compiler | |
1087 | interprets this as an indication that this is a variable known not to | |
1088 | be used.) | |
d032d5e7 | 1089 | |
b8d4c8d0 GM |
1090 | @node Buffer-Local Variables |
1091 | @section Buffer-Local Variables | |
1092 | @cindex variable, buffer-local | |
1093 | @cindex buffer-local variables | |
1094 | ||
1095 | Global and local variable bindings are found in most programming | |
e388c68f RS |
1096 | languages in one form or another. Emacs, however, also supports |
1097 | additional, unusual kinds of variable binding, such as | |
1098 | @dfn{buffer-local} bindings, which apply only in one buffer. Having | |
1099 | different values for a variable in different buffers is an important | |
32770114 | 1100 | customization method. (Variables can also have bindings that are |
c830e5ae | 1101 | local to each terminal. @xref{Multiple Terminals}.) |
b8d4c8d0 GM |
1102 | |
1103 | @menu | |
d032d5e7 SM |
1104 | * Intro to Buffer-Local:: Introduction and concepts. |
1105 | * Creating Buffer-Local:: Creating and destroying buffer-local bindings. | |
1106 | * Default Value:: The default value is seen in buffers | |
b8d4c8d0 GM |
1107 | that don't have their own buffer-local values. |
1108 | @end menu | |
1109 | ||
1110 | @node Intro to Buffer-Local | |
1111 | @subsection Introduction to Buffer-Local Variables | |
1112 | ||
1113 | A buffer-local variable has a buffer-local binding associated with a | |
1114 | particular buffer. The binding is in effect when that buffer is | |
1115 | current; otherwise, it is not in effect. If you set the variable while | |
1116 | a buffer-local binding is in effect, the new value goes in that binding, | |
1117 | so its other bindings are unchanged. This means that the change is | |
1118 | visible only in the buffer where you made it. | |
1119 | ||
1120 | The variable's ordinary binding, which is not associated with any | |
1121 | specific buffer, is called the @dfn{default binding}. In most cases, | |
1122 | this is the global binding. | |
1123 | ||
1124 | A variable can have buffer-local bindings in some buffers but not in | |
1125 | other buffers. The default binding is shared by all the buffers that | |
1126 | don't have their own bindings for the variable. (This includes all | |
1127 | newly-created buffers.) If you set the variable in a buffer that does | |
e388c68f | 1128 | not have a buffer-local binding for it, this sets the default binding, |
b8d4c8d0 GM |
1129 | so the new value is visible in all the buffers that see the default |
1130 | binding. | |
1131 | ||
1132 | The most common use of buffer-local bindings is for major modes to change | |
1133 | variables that control the behavior of commands. For example, C mode and | |
1134 | Lisp mode both set the variable @code{paragraph-start} to specify that only | |
1135 | blank lines separate paragraphs. They do this by making the variable | |
1136 | buffer-local in the buffer that is being put into C mode or Lisp mode, and | |
1137 | then setting it to the new value for that mode. @xref{Major Modes}. | |
1138 | ||
1139 | The usual way to make a buffer-local binding is with | |
1140 | @code{make-local-variable}, which is what major mode commands typically | |
1141 | use. This affects just the current buffer; all other buffers (including | |
1142 | those yet to be created) will continue to share the default value unless | |
1143 | they are explicitly given their own buffer-local bindings. | |
1144 | ||
1145 | @cindex automatically buffer-local | |
1146 | A more powerful operation is to mark the variable as | |
1147 | @dfn{automatically buffer-local} by calling | |
1148 | @code{make-variable-buffer-local}. You can think of this as making the | |
1149 | variable local in all buffers, even those yet to be created. More | |
1150 | precisely, the effect is that setting the variable automatically makes | |
1151 | the variable local to the current buffer if it is not already so. All | |
1152 | buffers start out by sharing the default value of the variable as usual, | |
1153 | but setting the variable creates a buffer-local binding for the current | |
1154 | buffer. The new value is stored in the buffer-local binding, leaving | |
1155 | the default binding untouched. This means that the default value cannot | |
1156 | be changed with @code{setq} in any buffer; the only way to change it is | |
1157 | with @code{setq-default}. | |
1158 | ||
e388c68f | 1159 | @strong{Warning:} When a variable has buffer-local |
b8d4c8d0 GM |
1160 | bindings in one or more buffers, @code{let} rebinds the binding that's |
1161 | currently in effect. For instance, if the current buffer has a | |
1162 | buffer-local value, @code{let} temporarily rebinds that. If no | |
e388c68f | 1163 | buffer-local bindings are in effect, @code{let} rebinds |
b8d4c8d0 GM |
1164 | the default value. If inside the @code{let} you then change to a |
1165 | different current buffer in which a different binding is in effect, | |
1166 | you won't see the @code{let} binding any more. And if you exit the | |
1167 | @code{let} while still in the other buffer, you won't see the | |
1168 | unbinding occur (though it will occur properly). Here is an example | |
1169 | to illustrate: | |
1170 | ||
1171 | @example | |
1172 | @group | |
1173 | (setq foo 'g) | |
1174 | (set-buffer "a") | |
1175 | (make-local-variable 'foo) | |
1176 | @end group | |
1177 | (setq foo 'a) | |
1178 | (let ((foo 'temp)) | |
1179 | ;; foo @result{} 'temp ; @r{let binding in buffer @samp{a}} | |
1180 | (set-buffer "b") | |
1181 | ;; foo @result{} 'g ; @r{the global value since foo is not local in @samp{b}} | |
1182 | @var{body}@dots{}) | |
1183 | @group | |
1184 | foo @result{} 'g ; @r{exiting restored the local value in buffer @samp{a},} | |
1185 | ; @r{but we don't see that in buffer @samp{b}} | |
1186 | @end group | |
1187 | @group | |
1188 | (set-buffer "a") ; @r{verify the local value was restored} | |
1189 | foo @result{} 'a | |
1190 | @end group | |
1191 | @end example | |
1192 | ||
735cc5ca CY |
1193 | @noindent |
1194 | Note that references to @code{foo} in @var{body} access the | |
b8d4c8d0 GM |
1195 | buffer-local binding of buffer @samp{b}. |
1196 | ||
1197 | When a file specifies local variable values, these become buffer-local | |
1198 | values when you visit the file. @xref{File Variables,,, emacs, The | |
1199 | GNU Emacs Manual}. | |
1200 | ||
c830e5ae CY |
1201 | A buffer-local variable cannot be made terminal-local |
1202 | (@pxref{Multiple Terminals}). | |
0f7766a4 | 1203 | |
b8d4c8d0 GM |
1204 | @node Creating Buffer-Local |
1205 | @subsection Creating and Deleting Buffer-Local Bindings | |
1206 | ||
1207 | @deffn Command make-local-variable variable | |
1208 | This function creates a buffer-local binding in the current buffer for | |
1209 | @var{variable} (a symbol). Other buffers are not affected. The value | |
1210 | returned is @var{variable}. | |
1211 | ||
b8d4c8d0 GM |
1212 | The buffer-local value of @var{variable} starts out as the same value |
1213 | @var{variable} previously had. If @var{variable} was void, it remains | |
1214 | void. | |
1215 | ||
1216 | @example | |
1217 | @group | |
1218 | ;; @r{In buffer @samp{b1}:} | |
1219 | (setq foo 5) ; @r{Affects all buffers.} | |
1220 | @result{} 5 | |
1221 | @end group | |
1222 | @group | |
1223 | (make-local-variable 'foo) ; @r{Now it is local in @samp{b1}.} | |
1224 | @result{} foo | |
1225 | @end group | |
1226 | @group | |
1227 | foo ; @r{That did not change} | |
1228 | @result{} 5 ; @r{the value.} | |
1229 | @end group | |
1230 | @group | |
1231 | (setq foo 6) ; @r{Change the value} | |
1232 | @result{} 6 ; @r{in @samp{b1}.} | |
1233 | @end group | |
1234 | @group | |
1235 | foo | |
1236 | @result{} 6 | |
1237 | @end group | |
1238 | ||
1239 | @group | |
1240 | ;; @r{In buffer @samp{b2}, the value hasn't changed.} | |
c57008f6 | 1241 | (with-current-buffer "b2" |
b8d4c8d0 GM |
1242 | foo) |
1243 | @result{} 5 | |
1244 | @end group | |
1245 | @end example | |
1246 | ||
1247 | Making a variable buffer-local within a @code{let}-binding for that | |
1248 | variable does not work reliably, unless the buffer in which you do this | |
1249 | is not current either on entry to or exit from the @code{let}. This is | |
1250 | because @code{let} does not distinguish between different kinds of | |
1251 | bindings; it knows only which variable the binding was made for. | |
1252 | ||
c830e5ae CY |
1253 | If the variable is terminal-local (@pxref{Multiple Terminals}), this |
1254 | function signals an error. Such variables cannot have buffer-local | |
1255 | bindings as well. | |
b8d4c8d0 GM |
1256 | |
1257 | @strong{Warning:} do not use @code{make-local-variable} for a hook | |
1258 | variable. The hook variables are automatically made buffer-local as | |
1259 | needed if you use the @var{local} argument to @code{add-hook} or | |
1260 | @code{remove-hook}. | |
1261 | @end deffn | |
1262 | ||
1263 | @deffn Command make-variable-buffer-local variable | |
1264 | This function marks @var{variable} (a symbol) automatically | |
1265 | buffer-local, so that any subsequent attempt to set it will make it | |
6a43ef8e CY |
1266 | local to the current buffer at the time. Unlike |
1267 | @code{make-local-variable}, with which it is often confused, this | |
1268 | cannot be undone, and affects the behavior of the variable in all | |
1269 | buffers. | |
b8d4c8d0 GM |
1270 | |
1271 | A peculiar wrinkle of this feature is that binding the variable (with | |
1272 | @code{let} or other binding constructs) does not create a buffer-local | |
1273 | binding for it. Only setting the variable (with @code{set} or | |
1274 | @code{setq}), while the variable does not have a @code{let}-style | |
1275 | binding that was made in the current buffer, does so. | |
1276 | ||
1277 | If @var{variable} does not have a default value, then calling this | |
1278 | command will give it a default value of @code{nil}. If @var{variable} | |
1279 | already has a default value, that value remains unchanged. | |
1280 | Subsequently calling @code{makunbound} on @var{variable} will result | |
1281 | in a void buffer-local value and leave the default value unaffected. | |
1282 | ||
1283 | The value returned is @var{variable}. | |
1284 | ||
1285 | @strong{Warning:} Don't assume that you should use | |
1286 | @code{make-variable-buffer-local} for user-option variables, simply | |
1287 | because users @emph{might} want to customize them differently in | |
1288 | different buffers. Users can make any variable local, when they wish | |
1289 | to. It is better to leave the choice to them. | |
1290 | ||
1291 | The time to use @code{make-variable-buffer-local} is when it is crucial | |
1292 | that no two buffers ever share the same binding. For example, when a | |
1293 | variable is used for internal purposes in a Lisp program which depends | |
1294 | on having separate values in separate buffers, then using | |
1295 | @code{make-variable-buffer-local} can be the best solution. | |
1296 | @end deffn | |
1297 | ||
1298 | @defun local-variable-p variable &optional buffer | |
1299 | This returns @code{t} if @var{variable} is buffer-local in buffer | |
1300 | @var{buffer} (which defaults to the current buffer); otherwise, | |
1301 | @code{nil}. | |
1302 | @end defun | |
1303 | ||
1304 | @defun local-variable-if-set-p variable &optional buffer | |
1305 | This returns @code{t} if @var{variable} will become buffer-local in | |
1306 | buffer @var{buffer} (which defaults to the current buffer) if it is | |
1307 | set there. | |
1308 | @end defun | |
1309 | ||
1310 | @defun buffer-local-value variable buffer | |
1311 | This function returns the buffer-local binding of @var{variable} (a | |
1312 | symbol) in buffer @var{buffer}. If @var{variable} does not have a | |
1313 | buffer-local binding in buffer @var{buffer}, it returns the default | |
1314 | value (@pxref{Default Value}) of @var{variable} instead. | |
1315 | @end defun | |
1316 | ||
1317 | @defun buffer-local-variables &optional buffer | |
1318 | This function returns a list describing the buffer-local variables in | |
0992bd9c CY |
1319 | buffer @var{buffer}. (If @var{buffer} is omitted, the current buffer |
1320 | is used.) Normally, each list element has the form | |
1321 | @w{@code{(@var{sym} . @var{val})}}, where @var{sym} is a buffer-local | |
1322 | variable (a symbol) and @var{val} is its buffer-local value. But when | |
1323 | a variable's buffer-local binding in @var{buffer} is void, its list | |
1324 | element is just @var{sym}. | |
b8d4c8d0 GM |
1325 | |
1326 | @example | |
1327 | @group | |
1328 | (make-local-variable 'foobar) | |
1329 | (makunbound 'foobar) | |
1330 | (make-local-variable 'bind-me) | |
1331 | (setq bind-me 69) | |
1332 | @end group | |
1333 | (setq lcl (buffer-local-variables)) | |
1334 | ;; @r{First, built-in variables local in all buffers:} | |
1335 | @result{} ((mark-active . nil) | |
1336 | (buffer-undo-list . nil) | |
1337 | (mode-name . "Fundamental") | |
1338 | @dots{} | |
1339 | @group | |
1340 | ;; @r{Next, non-built-in buffer-local variables.} | |
1341 | ;; @r{This one is buffer-local and void:} | |
1342 | foobar | |
1343 | ;; @r{This one is buffer-local and nonvoid:} | |
1344 | (bind-me . 69)) | |
1345 | @end group | |
1346 | @end example | |
1347 | ||
1348 | Note that storing new values into the @sc{cdr}s of cons cells in this | |
1349 | list does @emph{not} change the buffer-local values of the variables. | |
1350 | @end defun | |
1351 | ||
1352 | @deffn Command kill-local-variable variable | |
1353 | This function deletes the buffer-local binding (if any) for | |
1354 | @var{variable} (a symbol) in the current buffer. As a result, the | |
1355 | default binding of @var{variable} becomes visible in this buffer. This | |
1356 | typically results in a change in the value of @var{variable}, since the | |
1357 | default value is usually different from the buffer-local value just | |
1358 | eliminated. | |
1359 | ||
1360 | If you kill the buffer-local binding of a variable that automatically | |
1361 | becomes buffer-local when set, this makes the default value visible in | |
1362 | the current buffer. However, if you set the variable again, that will | |
1363 | once again create a buffer-local binding for it. | |
1364 | ||
1365 | @code{kill-local-variable} returns @var{variable}. | |
1366 | ||
1367 | This function is a command because it is sometimes useful to kill one | |
1368 | buffer-local variable interactively, just as it is useful to create | |
1369 | buffer-local variables interactively. | |
1370 | @end deffn | |
1371 | ||
1372 | @defun kill-all-local-variables | |
1373 | This function eliminates all the buffer-local variable bindings of the | |
ee666f84 EZ |
1374 | current buffer except for variables marked as ``permanent'' and local |
1375 | hook functions that have a non-@code{nil} @code{permanent-local-hook} | |
1376 | property (@pxref{Setting Hooks}). As a result, the buffer will see | |
1377 | the default values of most variables. | |
b8d4c8d0 GM |
1378 | |
1379 | This function also resets certain other information pertaining to the | |
1380 | buffer: it sets the local keymap to @code{nil}, the syntax table to the | |
1381 | value of @code{(standard-syntax-table)}, the case table to | |
1382 | @code{(standard-case-table)}, and the abbrev table to the value of | |
1383 | @code{fundamental-mode-abbrev-table}. | |
1384 | ||
1385 | The very first thing this function does is run the normal hook | |
1386 | @code{change-major-mode-hook} (see below). | |
1387 | ||
1388 | Every major mode command begins by calling this function, which has the | |
1389 | effect of switching to Fundamental mode and erasing most of the effects | |
1390 | of the previous major mode. To ensure that this does its job, the | |
1391 | variables that major modes set should not be marked permanent. | |
1392 | ||
1393 | @code{kill-all-local-variables} returns @code{nil}. | |
1394 | @end defun | |
1395 | ||
1396 | @defvar change-major-mode-hook | |
1397 | The function @code{kill-all-local-variables} runs this normal hook | |
1398 | before it does anything else. This gives major modes a way to arrange | |
1399 | for something special to be done if the user switches to a different | |
1400 | major mode. It is also useful for buffer-specific minor modes | |
1401 | that should be forgotten if the user changes the major mode. | |
1402 | ||
1403 | For best results, make this variable buffer-local, so that it will | |
1404 | disappear after doing its job and will not interfere with the | |
1405 | subsequent major mode. @xref{Hooks}. | |
1406 | @end defvar | |
1407 | ||
1408 | @c Emacs 19 feature | |
1409 | @cindex permanent local variable | |
1410 | A buffer-local variable is @dfn{permanent} if the variable name (a | |
1411 | symbol) has a @code{permanent-local} property that is non-@code{nil}. | |
86ec878a EZ |
1412 | Such variables are unaffected by @code{kill-all-local-variables}, and |
1413 | their local bindings are therefore not cleared by changing major modes. | |
b8d4c8d0 GM |
1414 | Permanent locals are appropriate for data pertaining to where the file |
1415 | came from or how to save it, rather than with how to edit the contents. | |
1416 | ||
1417 | @node Default Value | |
1418 | @subsection The Default Value of a Buffer-Local Variable | |
1419 | @cindex default value | |
1420 | ||
1421 | The global value of a variable with buffer-local bindings is also | |
1422 | called the @dfn{default} value, because it is the value that is in | |
1423 | effect whenever neither the current buffer nor the selected frame has | |
1424 | its own binding for the variable. | |
1425 | ||
1426 | The functions @code{default-value} and @code{setq-default} access and | |
1427 | change a variable's default value regardless of whether the current | |
1428 | buffer has a buffer-local binding. For example, you could use | |
1429 | @code{setq-default} to change the default setting of | |
1430 | @code{paragraph-start} for most buffers; and this would work even when | |
1431 | you are in a C or Lisp mode buffer that has a buffer-local value for | |
1432 | this variable. | |
1433 | ||
1434 | @c Emacs 19 feature | |
1435 | The special forms @code{defvar} and @code{defconst} also set the | |
1436 | default value (if they set the variable at all), rather than any | |
e388c68f | 1437 | buffer-local value. |
b8d4c8d0 GM |
1438 | |
1439 | @defun default-value symbol | |
1440 | This function returns @var{symbol}'s default value. This is the value | |
1441 | that is seen in buffers and frames that do not have their own values for | |
1442 | this variable. If @var{symbol} is not buffer-local, this is equivalent | |
1443 | to @code{symbol-value} (@pxref{Accessing Variables}). | |
1444 | @end defun | |
1445 | ||
1446 | @c Emacs 19 feature | |
1447 | @defun default-boundp symbol | |
1448 | The function @code{default-boundp} tells you whether @var{symbol}'s | |
1449 | default value is nonvoid. If @code{(default-boundp 'foo)} returns | |
1450 | @code{nil}, then @code{(default-value 'foo)} would get an error. | |
1451 | ||
1452 | @code{default-boundp} is to @code{default-value} as @code{boundp} is to | |
1453 | @code{symbol-value}. | |
1454 | @end defun | |
1455 | ||
1456 | @defspec setq-default [symbol form]@dots{} | |
1457 | This special form gives each @var{symbol} a new default value, which is | |
1458 | the result of evaluating the corresponding @var{form}. It does not | |
1459 | evaluate @var{symbol}, but does evaluate @var{form}. The value of the | |
1460 | @code{setq-default} form is the value of the last @var{form}. | |
1461 | ||
1462 | If a @var{symbol} is not buffer-local for the current buffer, and is not | |
1463 | marked automatically buffer-local, @code{setq-default} has the same | |
1464 | effect as @code{setq}. If @var{symbol} is buffer-local for the current | |
1465 | buffer, then this changes the value that other buffers will see (as long | |
1466 | as they don't have a buffer-local value), but not the value that the | |
1467 | current buffer sees. | |
1468 | ||
1469 | @example | |
1470 | @group | |
1471 | ;; @r{In buffer @samp{foo}:} | |
1472 | (make-local-variable 'buffer-local) | |
1473 | @result{} buffer-local | |
1474 | @end group | |
1475 | @group | |
1476 | (setq buffer-local 'value-in-foo) | |
1477 | @result{} value-in-foo | |
1478 | @end group | |
1479 | @group | |
1480 | (setq-default buffer-local 'new-default) | |
1481 | @result{} new-default | |
1482 | @end group | |
1483 | @group | |
1484 | buffer-local | |
1485 | @result{} value-in-foo | |
1486 | @end group | |
1487 | @group | |
1488 | (default-value 'buffer-local) | |
1489 | @result{} new-default | |
1490 | @end group | |
1491 | ||
1492 | @group | |
1493 | ;; @r{In (the new) buffer @samp{bar}:} | |
1494 | buffer-local | |
1495 | @result{} new-default | |
1496 | @end group | |
1497 | @group | |
1498 | (default-value 'buffer-local) | |
1499 | @result{} new-default | |
1500 | @end group | |
1501 | @group | |
1502 | (setq buffer-local 'another-default) | |
1503 | @result{} another-default | |
1504 | @end group | |
1505 | @group | |
1506 | (default-value 'buffer-local) | |
1507 | @result{} another-default | |
1508 | @end group | |
1509 | ||
1510 | @group | |
1511 | ;; @r{Back in buffer @samp{foo}:} | |
1512 | buffer-local | |
1513 | @result{} value-in-foo | |
1514 | (default-value 'buffer-local) | |
1515 | @result{} another-default | |
1516 | @end group | |
1517 | @end example | |
1518 | @end defspec | |
1519 | ||
1520 | @defun set-default symbol value | |
1521 | This function is like @code{setq-default}, except that @var{symbol} is | |
1522 | an ordinary evaluated argument. | |
1523 | ||
1524 | @example | |
1525 | @group | |
1526 | (set-default (car '(a b c)) 23) | |
1527 | @result{} 23 | |
1528 | @end group | |
1529 | @group | |
1530 | (default-value 'a) | |
1531 | @result{} 23 | |
1532 | @end group | |
1533 | @end example | |
1534 | @end defun | |
1535 | ||
b8d4c8d0 GM |
1536 | @node File Local Variables |
1537 | @section File Local Variables | |
1538 | @cindex file local variables | |
1539 | ||
1540 | A file can specify local variable values; Emacs uses these to create | |
1541 | buffer-local bindings for those variables in the buffer visiting that | |
1542 | file. @xref{File variables, , Local Variables in Files, emacs, The | |
32770114 CY |
1543 | GNU Emacs Manual}, for basic information about file-local variables. |
1544 | This section describes the functions and variables that affect how | |
1545 | file-local variables are processed. | |
1546 | ||
1547 | If a file-local variable could specify an arbitrary function or Lisp | |
1548 | expression that would be called later, visiting a file could take over | |
1549 | your Emacs. Emacs protects against this by automatically setting only | |
1550 | those file-local variables whose specified values are known to be | |
1551 | safe. Other file-local variables are set only if the user agrees. | |
b8d4c8d0 | 1552 | |
dd449674 CY |
1553 | For additional safety, @code{read-circle} is temporarily bound to |
1554 | @code{nil} when Emacs reads file-local variables (@pxref{Input | |
1555 | Functions}). This prevents the Lisp reader from recognizing circular | |
1556 | and shared Lisp structures (@pxref{Circular Objects}). | |
1557 | ||
b8d4c8d0 | 1558 | @defopt enable-local-variables |
32770114 | 1559 | This variable controls whether to process file-local variables. |
b8d4c8d0 GM |
1560 | The possible values are: |
1561 | ||
1562 | @table @asis | |
1563 | @item @code{t} (the default) | |
1564 | Set the safe variables, and query (once) about any unsafe variables. | |
1565 | @item @code{:safe} | |
1566 | Set only the safe variables and do not query. | |
1567 | @item @code{:all} | |
1568 | Set all the variables and do not query. | |
1569 | @item @code{nil} | |
1570 | Don't set any variables. | |
1571 | @item anything else | |
1572 | Query (once) about all the variables. | |
1573 | @end table | |
1574 | @end defopt | |
1575 | ||
61086eb6 GM |
1576 | @defvar inhibit-local-variables-regexps |
1577 | This is a list of regular expressions. If a file has a name | |
1578 | matching an element of this list, then it is not scanned for | |
1579 | any form of file-local variable. For examples of why you might want | |
1580 | to use this, @pxref{Auto Major Mode}. | |
1581 | @end defvar | |
1582 | ||
b8d4c8d0 GM |
1583 | @defun hack-local-variables &optional mode-only |
1584 | This function parses, and binds or evaluates as appropriate, any local | |
1585 | variables specified by the contents of the current buffer. The variable | |
1586 | @code{enable-local-variables} has its effect here. However, this | |
1587 | function does not look for the @samp{mode:} local variable in the | |
1588 | @w{@samp{-*-}} line. @code{set-auto-mode} does that, also taking | |
1589 | @code{enable-local-variables} into account (@pxref{Auto Major Mode}). | |
1590 | ||
291703b5 | 1591 | This function works by walking the alist stored in |
3a57591a | 1592 | @code{file-local-variables-alist} and applying each local variable in |
291703b5 EZ |
1593 | turn. It calls @code{before-hack-local-variables-hook} and |
1594 | @code{hack-local-variables-hook} before and after applying the | |
2d3ba9e7 GM |
1595 | variables, respectively. It only calls the before-hook if the alist |
1596 | is non-@code{nil}; it always calls the other hook. This | |
1597 | function ignores a @samp{mode} element if it specifies the same major | |
1598 | mode as the buffer already has. | |
291703b5 | 1599 | |
b8d4c8d0 | 1600 | If the optional argument @var{mode-only} is non-@code{nil}, then all |
eafed945 GM |
1601 | this function does is return a symbol specifying the major mode, |
1602 | if the @w{@samp{-*-}} line or the local variables list specifies one, | |
1603 | and @code{nil} otherwise. It does not set the mode nor any other | |
1604 | file-local variable. | |
b8d4c8d0 GM |
1605 | @end defun |
1606 | ||
291703b5 EZ |
1607 | @defvar file-local-variables-alist |
1608 | This buffer-local variable holds the alist of file-local variable | |
1609 | settings. Each element of the alist is of the form | |
1610 | @w{@code{(@var{var} . @var{value})}}, where @var{var} is a symbol of | |
1611 | the local variable and @var{value} is its value. When Emacs visits a | |
1612 | file, it first collects all the file-local variables into this alist, | |
1613 | and then the @code{hack-local-variables} function applies them one by | |
1614 | one. | |
1615 | @end defvar | |
1616 | ||
1617 | @defvar before-hack-local-variables-hook | |
1618 | Emacs calls this hook immediately before applying file-local variables | |
1619 | stored in @code{file-local-variables-alist}. | |
1620 | @end defvar | |
1621 | ||
1622 | @defvar hack-local-variables-hook | |
1623 | Emacs calls this hook immediately after it finishes applying | |
1624 | file-local variables stored in @code{file-local-variables-alist}. | |
1625 | @end defvar | |
1626 | ||
b8d4c8d0 GM |
1627 | @cindex safe local variable |
1628 | You can specify safe values for a variable with a | |
32770114 CY |
1629 | @code{safe-local-variable} property. The property has to be a |
1630 | function of one argument; any value is safe if the function returns | |
1631 | non-@code{nil} given that value. Many commonly-encountered file | |
1632 | variables have @code{safe-local-variable} properties; these include | |
1633 | @code{fill-column}, @code{fill-prefix}, and @code{indent-tabs-mode}. | |
1634 | For boolean-valued variables that are safe, use @code{booleanp} as the | |
1635 | property value. Lambda expressions should be quoted so that | |
1636 | @code{describe-variable} can display the predicate. | |
b8d4c8d0 | 1637 | |
735cc5ca CY |
1638 | When defining a user option using @code{defcustom}, you can set its |
1639 | @code{safe-local-variable} property by adding the arguments | |
1640 | @code{:safe @var{function}} to @code{defcustom} (@pxref{Variable | |
1641 | Definitions}). | |
1642 | ||
b8d4c8d0 GM |
1643 | @defopt safe-local-variable-values |
1644 | This variable provides another way to mark some variable values as | |
1645 | safe. It is a list of cons cells @code{(@var{var} . @var{val})}, | |
1646 | where @var{var} is a variable name and @var{val} is a value which is | |
1647 | safe for that variable. | |
1648 | ||
32770114 | 1649 | When Emacs asks the user whether or not to obey a set of file-local |
b8d4c8d0 GM |
1650 | variable specifications, the user can choose to mark them as safe. |
1651 | Doing so adds those variable/value pairs to | |
1652 | @code{safe-local-variable-values}, and saves it to the user's custom | |
1653 | file. | |
1654 | @end defopt | |
1655 | ||
1656 | @defun safe-local-variable-p sym val | |
1657 | This function returns non-@code{nil} if it is safe to give @var{sym} | |
1658 | the value @var{val}, based on the above criteria. | |
1659 | @end defun | |
1660 | ||
1661 | @c @cindex risky local variable Duplicates risky-local-variable | |
735cc5ca CY |
1662 | Some variables are considered @dfn{risky}. If a variable is risky, |
1663 | it is never entered automatically into | |
1664 | @code{safe-local-variable-values}; Emacs always queries before setting | |
1665 | a risky variable, unless the user explicitly allows a value by | |
1666 | customizing @code{safe-local-variable-values} directly. | |
1667 | ||
1668 | Any variable whose name has a non-@code{nil} | |
1669 | @code{risky-local-variable} property is considered risky. When you | |
1670 | define a user option using @code{defcustom}, you can set its | |
1671 | @code{risky-local-variable} property by adding the arguments | |
1672 | @code{:risky @var{value}} to @code{defcustom} (@pxref{Variable | |
1673 | Definitions}). In addition, any variable whose name ends in any of | |
1674 | @samp{-command}, @samp{-frame-alist}, @samp{-function}, | |
b8d4c8d0 GM |
1675 | @samp{-functions}, @samp{-hook}, @samp{-hooks}, @samp{-form}, |
1676 | @samp{-forms}, @samp{-map}, @samp{-map-alist}, @samp{-mode-alist}, | |
735cc5ca CY |
1677 | @samp{-program}, or @samp{-predicate} is automatically considered |
1678 | risky. The variables @samp{font-lock-keywords}, | |
1679 | @samp{font-lock-keywords} followed by a digit, and | |
1680 | @samp{font-lock-syntactic-keywords} are also considered risky. | |
b8d4c8d0 GM |
1681 | |
1682 | @defun risky-local-variable-p sym | |
1683 | This function returns non-@code{nil} if @var{sym} is a risky variable, | |
1684 | based on the above criteria. | |
1685 | @end defun | |
1686 | ||
b8d4c8d0 GM |
1687 | @defvar ignored-local-variables |
1688 | This variable holds a list of variables that should not be given local | |
1689 | values by files. Any value specified for one of these variables is | |
1690 | completely ignored. | |
1691 | @end defvar | |
1692 | ||
1693 | The @samp{Eval:} ``variable'' is also a potential loophole, so Emacs | |
1694 | normally asks for confirmation before handling it. | |
1695 | ||
1696 | @defopt enable-local-eval | |
1697 | This variable controls processing of @samp{Eval:} in @samp{-*-} lines | |
1698 | or local variables | |
1699 | lists in files being visited. A value of @code{t} means process them | |
1700 | unconditionally; @code{nil} means ignore them; anything else means ask | |
1701 | the user what to do for each file. The default value is @code{maybe}. | |
1702 | @end defopt | |
1703 | ||
1704 | @defopt safe-local-eval-forms | |
1705 | This variable holds a list of expressions that are safe to | |
1706 | evaluate when found in the @samp{Eval:} ``variable'' in a file | |
1707 | local variables list. | |
1708 | @end defopt | |
1709 | ||
1710 | If the expression is a function call and the function has a | |
1711 | @code{safe-local-eval-function} property, the property value | |
1712 | determines whether the expression is safe to evaluate. The property | |
1713 | value can be a predicate to call to test the expression, a list of | |
1714 | such predicates (it's safe if any predicate succeeds), or @code{t} | |
1715 | (always safe provided the arguments are constant). | |
1716 | ||
1717 | Text properties are also potential loopholes, since their values | |
1718 | could include functions to call. So Emacs discards all text | |
32770114 | 1719 | properties from string values specified for file-local variables. |
b8d4c8d0 | 1720 | |
eb22b78c EZ |
1721 | @node Directory Local Variables |
1722 | @section Directory Local Variables | |
1723 | @cindex directory local variables | |
1724 | ||
1725 | A directory can specify local variable values common to all files in | |
1726 | that directory; Emacs uses these to create buffer-local bindings for | |
1727 | those variables in buffers visiting any file in that directory. This | |
1728 | is useful when the files in the directory belong to some @dfn{project} | |
1729 | and therefore share the same local variables. | |
1730 | ||
1731 | There are two different methods for specifying directory local | |
1732 | variables: by putting them in a special file, or by defining a | |
1733 | @dfn{project class} for that directory. | |
1734 | ||
1735 | @defvr Constant dir-locals-file | |
1736 | This constant is the name of the file where Emacs expects to find the | |
1737 | directory-local variables. The name of the file is | |
1738 | @file{.dir-locals.el}@footnote{ | |
1739 | The MS-DOS version of Emacs uses @file{_dir-locals.el} instead, due to | |
1740 | limitations of the DOS filesystems. | |
1741 | }. A file by that name in a directory causes Emacs to apply its | |
6640b281 GM |
1742 | settings to any file in that directory or any of its subdirectories |
1743 | (optionally, you can exclude subdirectories; see below). | |
eb22b78c EZ |
1744 | If some of the subdirectories have their own @file{.dir-locals.el} |
1745 | files, Emacs uses the settings from the deepest file it finds starting | |
1746 | from the file's directory and moving up the directory tree. The file | |
1747 | specifies local variables as a specially formatted list; see | |
1748 | @ref{Directory Variables, , Per-directory Local Variables, emacs, The | |
1749 | GNU Emacs Manual}, for more details. | |
1750 | @end defvr | |
1751 | ||
1752 | @defun hack-dir-local-variables | |
1753 | This function reads the @code{.dir-locals.el} file and stores the | |
1754 | directory-local variables in @code{file-local-variables-alist} that is | |
1755 | local to the buffer visiting any file in the directory, without | |
1756 | applying them. It also stores the directory-local settings in | |
1757 | @code{dir-locals-class-alist}, where it defines a special class for | |
1758 | the directory in which @file{.dir-locals.el} file was found. This | |
1759 | function works by calling @code{dir-locals-set-class-variables} and | |
1760 | @code{dir-locals-set-directory-class}, described below. | |
1761 | @end defun | |
1762 | ||
89bd9ccd CY |
1763 | @defun hack-dir-local-variables-non-file-buffer |
1764 | This function looks for directory-local variables, and immediately | |
1765 | applies them in the current buffer. It is intended to be called in | |
1766 | the mode commands for non-file buffers, such as Dired buffers, to let | |
1767 | them obey directory-local variable settings. For non-file buffers, | |
1768 | Emacs looks for directory-local variables in @code{default-directory} | |
1769 | and its parent directories. | |
1770 | @end defun | |
1771 | ||
eb22b78c EZ |
1772 | @defun dir-locals-set-class-variables class variables |
1773 | This function defines a set of variable settings for the named | |
1774 | @var{class}, which is a symbol. You can later assign the class to one | |
1775 | or more directories, and Emacs will apply those variable settings to | |
1776 | all files in those directories. The list in @var{variables} can be of | |
1777 | one of the two forms: @code{(@var{major-mode} . @var{alist})} or | |
1778 | @code{(@var{directory} . @var{list})}. With the first form, if the | |
1779 | file's buffer turns on a mode that is derived from @var{major-mode}, | |
1780 | then the all the variables in the associated @var{alist} are applied; | |
1781 | @var{alist} should be of the form @code{(@var{name} . @var{value})}. | |
1782 | A special value @code{nil} for @var{major-mode} means the settings are | |
6640b281 GM |
1783 | applicable to any mode. In @var{alist}, you can use a special |
1784 | @var{name}: @code{subdirs}. If the associated value is | |
1785 | @code{nil}, the alist is only applied to files in the relevant | |
1786 | directory, not to those in any subdirectories. | |
eb22b78c EZ |
1787 | |
1788 | With the second form of @var{variables}, if @var{directory} is the | |
1789 | initial substring of the file's directory, then @var{list} is applied | |
1790 | recursively by following the above rules; @var{list} should be of one | |
1791 | of the two forms accepted by this function in @var{variables}. | |
1792 | @end defun | |
1793 | ||
081f7640 | 1794 | @defun dir-locals-set-directory-class directory class &optional mtime |
eb22b78c EZ |
1795 | This function assigns @var{class} to all the files in @code{directory} |
1796 | and its subdirectories. Thereafter, all the variable settings | |
1797 | specified for @var{class} will be applied to any visited file in | |
1798 | @var{directory} and its children. @var{class} must have been already | |
081f7640 GM |
1799 | defined by @code{dir-locals-set-class-variables}. |
1800 | ||
1801 | Emacs uses this function internally when it loads directory variables | |
1802 | from a @code{.dir-locals.el} file. In that case, the optional | |
1803 | argument @var{mtime} holds the file modification time (as returned by | |
1804 | @code{file-attributes}). Emacs uses this time to check stored | |
1805 | local variables are still valid. If you are assigning a class | |
1806 | directly, not via a file, this argument should be @code{nil}. | |
eb22b78c EZ |
1807 | @end defun |
1808 | ||
1809 | @defvar dir-locals-class-alist | |
1810 | This alist holds the class symbols and the associated variable | |
1811 | settings. It is updated by @code{dir-locals-set-class-variables}. | |
1812 | @end defvar | |
1813 | ||
d259fc4b GM |
1814 | @defvar dir-locals-directory-cache |
1815 | This alist holds directory names, their assigned class names, and | |
081f7640 GM |
1816 | modification times of the associated directory local variables file |
1817 | (if there is one). The function @code{dir-locals-set-directory-class} | |
1818 | updates this list. | |
eb22b78c EZ |
1819 | @end defvar |
1820 | ||
b8d4c8d0 GM |
1821 | @node Variable Aliases |
1822 | @section Variable Aliases | |
1823 | @cindex variable aliases | |
1824 | ||
1825 | It is sometimes useful to make two variables synonyms, so that both | |
1826 | variables always have the same value, and changing either one also | |
1827 | changes the other. Whenever you change the name of a | |
1828 | variable---either because you realize its old name was not well | |
1829 | chosen, or because its meaning has partly changed---it can be useful | |
1830 | to keep the old name as an @emph{alias} of the new one for | |
1831 | compatibility. You can do this with @code{defvaralias}. | |
1832 | ||
1833 | @defun defvaralias new-alias base-variable &optional docstring | |
1834 | This function defines the symbol @var{new-alias} as a variable alias | |
1835 | for symbol @var{base-variable}. This means that retrieving the value | |
1836 | of @var{new-alias} returns the value of @var{base-variable}, and | |
1837 | changing the value of @var{new-alias} changes the value of | |
1838 | @var{base-variable}. The two aliased variable names always share the | |
1839 | same value and the same bindings. | |
1840 | ||
1841 | If the @var{docstring} argument is non-@code{nil}, it specifies the | |
1842 | documentation for @var{new-alias}; otherwise, the alias gets the same | |
1843 | documentation as @var{base-variable} has, if any, unless | |
1844 | @var{base-variable} is itself an alias, in which case @var{new-alias} gets | |
1845 | the documentation of the variable at the end of the chain of aliases. | |
1846 | ||
1847 | This function returns @var{base-variable}. | |
1848 | @end defun | |
1849 | ||
1850 | Variable aliases are convenient for replacing an old name for a | |
1851 | variable with a new name. @code{make-obsolete-variable} declares that | |
1852 | the old name is obsolete and therefore that it may be removed at some | |
1853 | stage in the future. | |
1854 | ||
1855 | @defun make-obsolete-variable obsolete-name current-name &optional when | |
fc997332 | 1856 | This function makes the byte compiler warn that the variable |
b8d4c8d0 GM |
1857 | @var{obsolete-name} is obsolete. If @var{current-name} is a symbol, it is |
1858 | the variable's new name; then the warning message says to use | |
1859 | @var{current-name} instead of @var{obsolete-name}. If @var{current-name} | |
1860 | is a string, this is the message and there is no replacement variable. | |
1861 | ||
1862 | If provided, @var{when} should be a string indicating when the | |
1863 | variable was first made obsolete---for example, a date or a release | |
1864 | number. | |
1865 | @end defun | |
1866 | ||
1867 | You can make two variables synonyms and declare one obsolete at the | |
1868 | same time using the macro @code{define-obsolete-variable-alias}. | |
1869 | ||
1870 | @defmac define-obsolete-variable-alias obsolete-name current-name &optional when docstring | |
1871 | This macro marks the variable @var{obsolete-name} as obsolete and also | |
1872 | makes it an alias for the variable @var{current-name}. It is | |
1873 | equivalent to the following: | |
1874 | ||
1875 | @example | |
1876 | (defvaralias @var{obsolete-name} @var{current-name} @var{docstring}) | |
1877 | (make-obsolete-variable @var{obsolete-name} @var{current-name} @var{when}) | |
1878 | @end example | |
1879 | @end defmac | |
1880 | ||
1881 | @defun indirect-variable variable | |
1882 | This function returns the variable at the end of the chain of aliases | |
1883 | of @var{variable}. If @var{variable} is not a symbol, or if @var{variable} is | |
1884 | not defined as an alias, the function returns @var{variable}. | |
1885 | ||
1886 | This function signals a @code{cyclic-variable-indirection} error if | |
1887 | there is a loop in the chain of symbols. | |
1888 | @end defun | |
1889 | ||
1890 | @example | |
1891 | (defvaralias 'foo 'bar) | |
1892 | (indirect-variable 'foo) | |
1893 | @result{} bar | |
1894 | (indirect-variable 'bar) | |
1895 | @result{} bar | |
1896 | (setq bar 2) | |
1897 | bar | |
1898 | @result{} 2 | |
1899 | @group | |
1900 | foo | |
1901 | @result{} 2 | |
1902 | @end group | |
1903 | (setq foo 0) | |
1904 | bar | |
1905 | @result{} 0 | |
1906 | foo | |
1907 | @result{} 0 | |
1908 | @end example | |
1909 | ||
1910 | @node Variables with Restricted Values | |
1911 | @section Variables with Restricted Values | |
1912 | ||
1913 | Ordinary Lisp variables can be assigned any value that is a valid | |
1914 | Lisp object. However, certain Lisp variables are not defined in Lisp, | |
1915 | but in C. Most of these variables are defined in the C code using | |
1916 | @code{DEFVAR_LISP}. Like variables defined in Lisp, these can take on | |
1917 | any value. However, some variables are defined using | |
1918 | @code{DEFVAR_INT} or @code{DEFVAR_BOOL}. @xref{Defining Lisp | |
1919 | variables in C,, Writing Emacs Primitives}, in particular the | |
1920 | description of functions of the type @code{syms_of_@var{filename}}, | |
1921 | for a brief discussion of the C implementation. | |
1922 | ||
1923 | Variables of type @code{DEFVAR_BOOL} can only take on the values | |
1924 | @code{nil} or @code{t}. Attempting to assign them any other value | |
1925 | will set them to @code{t}: | |
1926 | ||
1927 | @example | |
1928 | (let ((display-hourglass 5)) | |
1929 | display-hourglass) | |
1930 | @result{} t | |
1931 | @end example | |
1932 | ||
1933 | @defvar byte-boolean-vars | |
1934 | This variable holds a list of all variables of type @code{DEFVAR_BOOL}. | |
1935 | @end defvar | |
1936 | ||
1937 | Variables of type @code{DEFVAR_INT} can only take on integer values. | |
1938 | Attempting to assign them any other value will result in an error: | |
1939 | ||
1940 | @example | |
9022ae07 JB |
1941 | (setq undo-limit 1000.0) |
1942 | @error{} Wrong type argument: integerp, 1000.0 | |
b8d4c8d0 | 1943 | @end example |