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