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