Next: , Previous: , Up: Variables   [Contents][Index]


27.2.4 Local Variables

M-x make-local-variable

Make a variable have a local value in the current buffer.

M-x kill-local-variable

Make a variable use its global value in the current buffer.

M-x make-variable-buffer-local

Mark a variable so that setting it will make it local to the buffer that is current at that time.

You can make any variable local to a specific Emacs buffer. This means that the variable’s value in that buffer is independent of its value in other buffers. A few variables are always local in every buffer. All other Emacs variables have a global value which is in effect in all buffers that have not made the variable local.

Major modes always make the variables they set local to the buffer. This is why changing major modes in one buffer has no effect on other buffers.

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 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 automatically becomes local when it is set. More precisely, once you have marked a variable in this way, the usual ways of setting the variable will automatically invoke make-local-variable first. We call such variables per-buffer variables.

Some important variables have been marked per-buffer already. They 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.

Note: the variable auto-fill-function was formerly named auto-fill-hook.

If you want a variable to cease to be local to the current buffer, call M-x kill-local-variable and provide the name of a variable to the prompt. The global value of the variable is again in effect in this buffer. Setting the major mode kills all the local variables of the buffer.

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 function setq-default. It works like setq. If there is a local value in the current buffer, the local value is not affected by setq-default; thus, the new global value may not be visible until you switch to another buffer, as in the case of:

(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.

Programs can look at a variable’s default value with default-value. This function takes a symbol as an argument and returns its default value. The argument is evaluated; usually you must quote it explicitly, as in the case of:

(default-value 'fill-column)

Next: , Previous: , Up: Variables   [Contents][Index]