[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why does saving a buffer to a new file clear local variables?
From: |
Jean Louis |
Subject: |
Re: Why does saving a buffer to a new file clear local variables? |
Date: |
Thu, 12 Dec 2024 10:31:55 +0300 |
User-agent: |
Mutt/2.2.12 (2023-09-09) |
* Patrick Nicodemus <gadget142@gmail.com> [2024-12-11 16:59]:
> If I create a new buffer like (get-buffer-create "tmpbuf"),
> switch to the buffer, and execute the commands
> (make-variable-buffer-local "abc")
> (setq abc 3)
> and then save the buffer to, say, "tmpbuf", then abc becomes nil.
> Why is this the case?
> Is this behavior documented anywhere?
> I checked that it doesn't change the major mode. It is "Fundamental" before
> and after saving.
I have just checked it, try doing it this way:
(defvar-local abc 123)
save the buffer, and verify the abc is still there.
This way, it doesn't work, variable must be symbol, not string:
(make-variable-buffer-local "abc")
You can try this way too:
- open new buffer ~/tmp/new2 which shall look as file
- (setq-local def 123)
- save buffer
- evaluate def -> 123
It worked that way. And it works this way:
- open new buffer ~/tmp/new4 which shall look as file
- (defvar-local def 123)
- save buffer
- evaluate def -> 123
And also it works this way:
- C-x b RET WriteAnyNameOfBuffer
- (defvar-local def 123)
- save buffer as any other name
- evaluate def -> 123
But if you try it this way:
(make-variable-buffer-local 'abcde)
(setq abcde 4)
After saving, the variable could lose the value.
Then you must consider what documentation says:
make-variable-buffer-local is an interactive primitive-function in ‘C
source code’.
(make-variable-buffer-local VARIABLE)
Make VARIABLE become buffer-local whenever it is set.
At any time, the value for the current buffer is in effect,
unless the variable has never been set in this buffer,
in which case the default value is in effect.
Note that binding the variable with ‘let’, or setting it while
a ‘let’-style binding made in this buffer is in effect,
does not make the variable buffer-local. Return VARIABLE.
Unless the variable has never been set in this buffer... it becomes default.
So that may be the reason of such behavior, and you may be right something is
wrong with that function, and myself I do not know if it is.
But the way how I have shown you works well that buffer-local variable remains
buffer local even after saving.
--
Jean Louis
Re: Why does saving a buffer to a new file clear local variables?, Michael Heerdegen, 2024/12/12