Say I define a buffer-local variable foo
, and its default value is "a":
(defvar foo "a")
(make-variable-buffer-local 'foo)
(default-value 'foo) ;; => "a"
Immediately after this I run the following code:
(let ((foo "b"))
(with-temp-buffer
(message "foo: %s" foo))) ;; => "b"
The result is "b", which is the value I set in let
.
Now if I use setq
to set the variable, then rerun the exact same code as before:
(setq foo "c") ;; => "c"
(let ((foo "b"))
(with-temp-buffer
(message "foo: %s" foo))) ;; => "a"
The result is "a", which is the default value now.
The question: for a temporary buffer, the default value of foo
is not set until I use setq
? And as long as I don't use setq
, I can use let
to change the default value in other buffers?
EDIT: as @npostavs said, this is what make-varible-buffer-local
really means. If I use make-variable-buffer-local
myself, I can always use setq
after that. But this becomes really tricky for the "built-in" buffer-local variables like case-fold-search
. if I, as a package author, bind case-fold-search
to nil
in the outer let
, and I want to use the default value (it might or might not be set by the user) in the with-temp-buffer
, I have to use setq
before with-temp-buffer
to make sure the default value is actually being used in case the user doesn't have that setq
in his/her init.el
. For buffer-local variables, it probably means setq
is always safer than let
when we want to set the value. I wonder whether the design or the documentation could be improved.