1

I'm learning to program with buffer local variables. The emacs documentation describes buffer local variables as:

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 its other bindings are unchanged. This means that the change is visible only in the buffer where you made it.

How does a buffer-local binding go into effect? This is an amateurish question. I assume it has something to do with the buffer being loaded, but I can't begin to imagine what this looks like in code.

1 Answer 1

3

Not exactly sure what the question is, but this might help. It's hard to add to what is said in the doc section you cite - I think that section describes the situation well.

A buffer is in a (major) mode. When the major mode is turned on (becomes active), one of the things that happens (by the major-mode function's code) is that all (buffer) local variables are killed (using kill-all-local-variables). That clears the slate. (That's not directly important to the question, but it is important to realize, in general.)

Code that is executed while the given buffer is current sets the (buffer) local value of a variable by first declaring the variable to be buffer local, using make-local-variable. E.g.:

(make-local-variable 'foo)

Then, whenever that buffer is current and you set that variable, it is the buffer-local value that gets set. E.g.:

(setq foo 42)

make-local-variable returns the variable, so you often see the two operations combined in one sexp:

(set (make-local-variable 'foo) 42)

A different declaration, make-variable-buffer-local declares a variable to become local to every buffer where it is set. So if you do this:

(make-variable-buffer-local 'bar)

Then whenever bar is given a value, that value is local to the buffer that is current when the setting takes place.

(I feel, without looking, that I am no doubt repeating what the doc says, using other words. Dunno what else to say, to make things clear.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.