I'm not quite clear on all the variations of buffer-local variables, even after reading all the doc and a bunch of postings here on SX.
Here's a summary of my understandings:
(defvar foo ..)
declares a dynamic variable for the file. But the
variable is (1) not known to other files unless they include a defvar
statement as well, and (2) the variable is global in scope, not buffer
local.
(make-variable-buffer-local foo)
after the defvar
above tells the
compiler and everyone else that the variable foo is to be treated as
buffer-local everywhere it is set, when it is set. So this pattern is
good style for declaring a buffer-local variable, putting both statements
back-to-back in the file.
(defvar xxx ...) ;declare xxx with global scope
(make-variable-buffer-local 'xxx) ;but now make it buffer-local everywhere
For convenience, the (defvar-local xxx ...)
form can be used as one
line, in place of the two lines above:
(defvar-local xxx ...) ;make xxx buffer local everywhere
Once declared as above, the variable xxx can be used like any other variable in setq statements.
If I just want to have a single instance of a buffer-local variable that is already a global dynamic variable, I would use the following declarations. The first one declares the global-scope dynamic variable, and the second statement makes just one instance of a buffer-local version of that variable, in the current buffer:
(defvar xxx ...) ;declare xxx with global scope
(make-local-variable 'xxx) ;make xxx local in this buffer only
Now for my explict questions (all the above were implicit questions on whether my understanding is correct).
When setting the value of variables, I can use setq
or
setq-local
. When should setq-local
be used? Why?
What happens if I use setq-local
on buffer-local vars, or
non-buffer-local vars?
Is setq-local
required for a defvar-local
declared variable?
Will setq-local
on a normal defvar
declared variable turn it into a
buffer-local variable? (In other words, is setq-local
somehow the
equivalent of a (make-variable-local xxx)
declaration?