57.3.3 Local Variables
- M-x make-local-variable <RET> var <RET>
- Make variable var have a local value in the current buffer.
- M-x kill-local-variable <RET> var <RET>
- Make variable var use its global value in the current buffer.
- M-x make-variable-buffer-local <RET> var <RET>
- Mark variable var so that setting it will make it local to the
buffer that is current at that time.
Almost any variable can be made local to a specific Emacs
buffer. This means that its value in that buffer is independent of its
value in other buffers. A few variables are always local in every
buffer. Every other Emacs variable has a global value which is in
effect in all buffers that have not made the variable local.
M-x make-local-variable reads the name of a variable and makes it
local to the current buffer. Further changes in this buffer will not
affect others, and further changes in the global value will not affect this
buffer.
M-x make-variable-buffer-local reads the name of a variable and
changes the future behavior of the variable so that it will become local
automatically when it is set. More precisely, once a variable has been
marked in this way, the usual ways of setting the variable automatically
do make-local-variable
first. We call such variables
per-buffer variables.
Major modes (see Major Modes) always make variables local to the
buffer before setting the variables. This is why changing major modes
in one buffer has no effect on other buffers. Minor modes also work by
setting variables—normally, each minor mode has one controlling
variable which is non-nil
when the mode is enabled (see Minor Modes). For most minor modes, the controlling variable is per buffer.
Emacs contains a number of variables that are always per-buffer.
These include abbrev-mode
, auto-fill-function
,
case-fold-search
, comment-column
, ctl-arrow
,
fill-column
, fill-prefix
, indent-tabs-mode
,
left-margin
, mode-line-format
, overwrite-mode
,
selective-display-ellipses
, selective-display
,
tab-width
, and truncate-lines
. Some other variables are
always local in every buffer, but they are used for internal
purposes.
A few variables cannot be local to a buffer because they are always
local to each display instead (see Multiple Displays). If you try to
make one of these variables buffer-local, you'll get an error message.
M-x kill-local-variable reads the name of a variable and makes
it cease to be local to the current buffer. The global value of the
variable henceforth is in effect in this buffer. Setting the major mode
kills all the local variables of the buffer except for a few variables
specially marked as permanent locals.
To set the global value of a variable, regardless of whether the
variable has a local value in the current buffer, you can use the Lisp
construct setq-default
. This construct is used just like
setq
, but it sets variables' global values instead of their local
values (if any). When the current buffer does have a local value, the
new global value may not be visible until you switch to another buffer.
Here is an example:
(setq-default fill-column 75)
setq-default
is the only way to set the global value of a variable
that has been marked with make-variable-buffer-local
.
Lisp programs can use default-value
to look at a variable's
default value. This function takes a symbol as argument and returns its
default value. The argument is evaluated; usually you must quote it
explicitly. For example, here's how to obtain the default value of
fill-column
:
(default-value 'fill-column)