(lispref.info)Intro to Buffer-Local
Next: Creating Buffer-Local Prev: Buffer-Local Variables Up: Buffer-Local Variables
Introduction to Buffer-Local Variables
A buffer-local variable has a buffer-local binding associated with a
particular buffer. The binding is in effect when that buffer is
current; otherwise, it is not in effect. If you set the variable while
a buffer-local binding is in effect, the new value goes in that binding,
so the global binding is unchanged; this means that the change is
visible in that buffer alone.
A variable may have buffer-local bindings in some buffers but not in
others. The global binding is shared by all the buffers that don't have
their own bindings. Thus, if you set the variable in a buffer that does
not have a buffer-local binding for it, the new value is visible in all
buffers except those with buffer-local bindings. (Here we are assuming
that there are no `let'-style local bindings to complicate the issue.)
The most common use of buffer-local bindings is for major modes to
change variables that control the behavior of commands. For example, C
mode and Lisp mode both set the variable `paragraph-start' to specify
that only blank lines separate paragraphs. They do this by making the
variable buffer-local in the buffer that is being put into C mode or
Lisp mode, and then setting it to the new value for that mode.
The usual way to make a buffer-local binding is with
`make-local-variable', which is what major mode commands use. This
affects just the current buffer; all other buffers (including those yet
to be created) continue to share the global value.
A more powerful operation is to mark the variable as "automatically
buffer-local" by calling `make-variable-buffer-local'. You can think
of this as making the variable local in all buffers, even those yet to
be created. More precisely, the effect is that setting the variable
automatically makes the variable local to the current buffer if it is
not already so. All buffers start out by sharing the global value of
the variable as usual, but any `setq' creates a buffer-local binding
for the current buffer. The new value is stored in the buffer-local
binding, leaving the (default) global binding untouched. The global
value can no longer be changed with `setq'; you need to use
`setq-default' to do that.
*Warning:* when a variable has local values in one or more buffers,
you can get Emacs very confused by binding the variable with `let',
changing to a different current buffer in which a different binding is
in effect, and then exiting the `let'. To preserve your sanity, it is
wise to avoid such situations. If you use `save-excursion' around each
piece of code that changes to a different current buffer, you will not
have this problem. Here is an example of incorrect code:
(setq foo 'b)
(setq foo 'a)
(let ((foo 'temp))
foo => 'a ; The old buffer-local value from buffer `a'
; is now the default value.
foo => 'temp ; The local value that should be gone
; is now the buffer-local value in buffer `a'.
But `save-excursion' as shown here avoids the problem:
(let ((foo 'temp))
Local variables in a file you edit are also represented by
buffer-local bindings for the buffer that holds the file within Emacs.
Note: Auto Major Mode.
automatically generated by info2www