@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2000
-@c Free Software Foundation, Inc.
+@c Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/variables
@node Variables, Functions, Control Structures, Top
All of the @var{value-form}s in @var{bindings} are evaluated in the
order they appear and @emph{before} binding any of the symbols to them.
-Here is an example of this: @code{Z} is bound to the old value of
-@code{Y}, which is 2, not the new value of @code{Y}, which is 1.
+Here is an example of this: @code{z} is bound to the old value of
+@code{y}, which is 2, not the new value of @code{y}, which is 1.
@example
@group
-(setq Y 2)
+(setq y 2)
@result{} 2
@end group
@group
-(let ((Y 1)
- (Z Y))
- (list Y Z))
+(let ((y 1)
+ (z y))
+ (list y z))
@result{} (1 2)
@end group
@end example
@example
@group
-(setq Y 2)
+(setq y 2)
@result{} 2
@end group
@group
-(let* ((Y 1)
- (Z Y)) ; @r{Use the just-established value of @code{Y}.}
- (list Y Z))
+(let* ((y 1)
+ (z y)) ; @r{Use the just-established value of @code{y}.}
+ (list y z))
@result{} (1 1)
@end group
@end example
(@pxref{Customization}).
@strong{Warning:} If the @code{defconst} and @code{defvar} special
-forms are used while the variable has a local binding, they set the
-local binding's value; the global binding is not changed. This is not
-what you usually want. To prevent it, use these special forms at top
-level in a file, where normally no local binding is in effect, and make
-sure to load the file before making a local binding for the variable.
+forms are used while the variable has a local binding (made with
+@code{let}, or a function argument), they set the local-binding's
+value; the top-level binding is not changed. This is not what you
+usually want. To prevent it, use these special forms at top level in
+a file, where normally no local binding is in effect, and make sure to
+load the file before making a local binding for the variable.
@node Tips for Defining
@section Tips for Defining Variables Robustly
x ; @r{@code{x} now has a global value.}
@result{} 3
@group
-(let ((x 5))
+(let ((x 5))
(setq x 6) ; @r{The local binding of @code{x} is set.}
x)
@result{} 6
@group
(setq x 10 ; @r{Notice that @code{x} is set before}
y (1+ x)) ; @r{the value of @code{y} is computed.}
- @result{} 11
+ @result{} 11
@end group
@end example
@end defspec
be changed with @code{setq} in any buffer; the only way to change it is
with @code{setq-default}.
- @strong{Warning:} When a variable has buffer-local values in one or
-more buffers, you can get Emacs very confused by binding the variable
-with @code{let}, changing to a different current buffer in which a
-different binding is in effect, and then exiting the @code{let}. This
-can scramble the values of the buffer-local and default bindings.
-
- To preserve your sanity, avoid using a variable in that way. If you
-use @code{save-excursion} around each piece of code that changes to a
-different current buffer, you will not have this problem
-(@pxref{Excursions}). Here is an example of what to avoid:
+ @strong{Warning:} When a variable has buffer-local or frame-local
+bindings in one or more buffers, @code{let} rebinds the binding that's
+currently in effect. For instance, if the current buffer has a
+buffer-local value, @code{let} temporarily rebinds that. If no
+buffer-local or frame-local bindings are in effect, @code{let} rebinds
+the default value. If inside the @code{let} you then change to a
+different current buffer in which a different binding is in effect,
+you won't see the @code{let} binding any more. And if you exit the
+@code{let} while still in the other buffer, you won't see the
+unbinding occur (though it will occur properly). Here is an example
+to illustrate:
@example
@group
-(setq foo 'b)
+(setq foo 'g)
(set-buffer "a")
(make-local-variable 'foo)
@end group
(setq foo 'a)
(let ((foo 'temp))
+ ;; foo @result{} 'temp ; @r{let binding in buffer @samp{a}}
(set-buffer "b")
+ ;; foo @result{} 'g ; @r{the global value since foo is not local in @samp{b}}
@var{body}@dots{})
@group
-foo @result{} 'a ; @r{The old buffer-local value from buffer @samp{a}}
- ; @r{is now the default value.}
-@end group
-@group
-(set-buffer "a")
-foo @result{} 'temp ; @r{The local @code{let} value that should be gone}
- ; @r{is now the buffer-local value in buffer @samp{a}.}
+foo @result{} 'g ; @r{exiting restored the local value in buffer @samp{a},}
+ ; @r{but we don't see that in buffer @samp{b}}
@end group
-@end example
-
-@noindent
-But @code{save-excursion} as shown here avoids the problem:
-
-@example
@group
-(let ((foo 'temp))
- (save-excursion
- (set-buffer "b")
- @var{body}@dots{}))
+(set-buffer "a") ; @r{verify the local value was restored}
+foo @result{} 'a
@end group
@end example
Displays}.
@strong{Note:} Do not use @code{make-local-variable} for a hook
-variable. Instead, use @code{make-local-hook}. @xref{Hooks}.
+variable. The hook variables are automatically made buffer-local
+as needed if you use the @var{local} argument to @code{add-hook} or
+@code{remove-hook}.
@end deffn
@deffn Command make-variable-buffer-local variable
@code{nil}.
@end defun
+@defun buffer-local-value variable buffer
+This function returns the buffer-local binding of @var{variable} (a
+symbol) in buffer @var{buffer}. If @var{variable} does not have a
+buffer-local binding in buffer @var{buffer}, it returns the default
+value (@pxref{Default Value}) of @var{variable} instead.
+@end defun
+
@defun buffer-local-variables &optional buffer
This function returns a list describing the buffer-local variables in
buffer @var{buffer}. (If @var{buffer} is omitted, the current buffer is
(mode-name . "Fundamental")
@dots{}
@group
- ;; @r{Next, non-built-in buffer-local variables.}
+ ;; @r{Next, non-built-in buffer-local variables.}
;; @r{This one is buffer-local and void:}
foobar
;; @r{This one is buffer-local and nonvoid:}
list does @emph{not} change the buffer-local values of the variables.
@end defun
-@defun buffer-local-value variable buffer
-This function returns the buffer-local binding of @var{variable} (a
-symbol) in buffer @var{buffer}. If @var{variable} does not have a
-buffer-local binding in buffer @var{buffer}, it returns the default
-value (@pxref{Default Value}) of @var{variable} instead.
-@end defun
-
@deffn Command kill-local-variable variable
This function deletes the buffer-local binding (if any) for
@var{variable} (a symbol) in the current buffer. As a result, the
to keep the old name as an @emph{alias} of the new one for
compatibility. You can do this with @code{defvaralias}.
-@defmac defvaralias alias-var base-var
+@defun defvaralias alias-var base-var &optional docstring
This function defines the symbol @var{alias-var} as a variable alias
-for symbol @var{base-var}. This means that retrieving the value of
+for symbol @var{base-var}. This means that retrieving the value of
@var{alias-var} returns the value of @var{base-var}, and changing the
value of @var{alias-var} changes the value of @var{base-var}.
-@end defmac
+
+If the @var{docstring} argument is non-@code{nil}, it specifies the
+documentation for @var{alias-var}; otherwise, the alias gets the same
+documentation as @var{base-var} has, if any.
+@end defun
@defun indirect-variable variable
This function returns the variable at the end of the chain of aliases
given to @code{normal-mode}.
@end defun
- If a file local variable list could specify the a function that will
+ If a file local variable list could specify a function that will
be called later, or an expression that will be executed later, simply
visiting a file could take over your Emacs. To prevent this, Emacs
takes care not to allow local variable lists to set such variables.
- For one thing, any variable whose name ends in @samp{-function},
-@samp{-functions}, @samp{-hook}, @samp{-hooks}, @samp{-form},
-@samp{-forms}, @samp{-program}, @samp{-command} or @samp{-predicate}
-cannot be set in a local variable list. In general, you should use such
-a name whenever it is appropriate for the variable's meaning.
+ For one thing, any variable whose name ends in @samp{-command},
+@samp{-frame-alist}, @samp{-function}, @samp{-functions},
+@samp{-hook}, @samp{-hooks}, @samp{-form}, @samp{-forms}, @samp{-map},
+@samp{-map-alist}, @samp{-mode-alist}, @samp{-program}, or
+@samp{-predicate} cannot be set in a local variable list. In general,
+you should use such a name whenever it is appropriate for the
+variable's meaning. The variables @samp{font-lock-keywords},
+@samp{font-lock-keywords-[0-9]}, and
+@samp{font-lock-syntactic-keywords} cannot be set in a local variable
+list, either.
In addition, any variable whose name has a non-@code{nil}
-@code{risky-local-variable} property is also ignored. So are
-all variables listed in @code{ignored-local-variables}:
+@code{risky-local-variable} property is also ignored. So are all
+variables listed in @code{ignored-local-variables}:
@defvar ignored-local-variables
This variable holds a list of variables that should not be
for one of these variables is ignored.
@end defvar
+@defun risky-local-variable-p sym
+Returns non-@code{nil} if @var{sym} is risky for any of the reasons
+stated above.
+@end defun
+
The @samp{Eval:} ``variable'' is also a potential loophole, so Emacs
normally asks for confirmation before handling it.
unconditionally; @code{nil} means ignore them; anything else means ask
the user what to do for each file. The default value is @code{maybe}.
@end defopt
+
+ Text properties are also potential loopholes, since their values
+could include functions to call. So Emacs discards all text
+properties from string values specified in a file's local variables
+list.
+
+@ignore
+ arch-tag: 5ff62c44-2b51-47bb-99d4-fea5aeec5d3e
+@end ignore