* Why does saving a buffer to a new file clear local variables?
@ 2024-12-11 4:05 Patrick Nicodemus
2024-12-12 7:31 ` Jean Louis
2024-12-12 12:50 ` Michael Heerdegen
0 siblings, 2 replies; 7+ messages in thread
From: Patrick Nicodemus @ 2024-12-11 4:05 UTC (permalink / raw)
To: help-gnu-emacs
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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Why does saving a buffer to a new file clear local variables?
2024-12-11 4:05 Why does saving a buffer to a new file clear local variables? Patrick Nicodemus
@ 2024-12-12 7:31 ` Jean Louis
2024-12-12 11:32 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-12-12 12:50 ` Michael Heerdegen
1 sibling, 1 reply; 7+ messages in thread
From: Jean Louis @ 2024-12-12 7:31 UTC (permalink / raw)
To: Patrick Nicodemus; +Cc: help-gnu-emacs
* 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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Why does saving a buffer to a new file clear local variables?
2024-12-12 7:31 ` Jean Louis
@ 2024-12-12 11:32 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-12-12 12:41 ` Jean Louis
0 siblings, 1 reply; 7+ messages in thread
From: Michael Heerdegen via Users list for the GNU Emacs text editor @ 2024-12-12 11:32 UTC (permalink / raw)
To: help-gnu-emacs
Jean Louis <bugs@gnu.support> writes:
> I have just checked it, try doing it this way:
>
> (defvar-local abc 123)
>
> save the buffer, and verify the abc is still there.
Are you sure that you don't just see the global binding of that
variable?
> This way, it doesn't work, variable must be symbol, not string:
> (make-variable-buffer-local "abc")
Please note that `make-variable-buffer-local' is a function (in contrast
to `defvar' or `defvar-local' which are macros), so the argument
(expression) is evaluated and should return a symbol. When you want to
specify a symbol quote its name: 'SYMBOL.
Michael.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Why does saving a buffer to a new file clear local variables?
2024-12-12 11:32 ` Michael Heerdegen via Users list for the GNU Emacs text editor
@ 2024-12-12 12:41 ` Jean Louis
2024-12-12 13:01 ` Michael Heerdegen via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 7+ messages in thread
From: Jean Louis @ 2024-12-12 12:41 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
* Michael Heerdegen via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2024-12-12 14:33]:
> Jean Louis <bugs@gnu.support> writes:
>
> > I have just checked it, try doing it this way:
> >
> > (defvar-local abc 123)
> >
> > save the buffer, and verify the abc is still there.
>
> Are you sure that you don't just see the global binding of that
> variable?
Unless you wish to reveal some new fact about it to me, I see that by
using `defvar-local' I can save file and variable `abc' still has
initially assigned value.
And it has this description:
abc’s value is 123
Not documented as a variable.
Automatically becomes buffer-local when set.
> > This way, it doesn't work, variable must be symbol, not string:
> > (make-variable-buffer-local "abc")
>
> Please note that `make-variable-buffer-local' is a function (in contrast
> to `defvar' or `defvar-local' which are macros), so the argument
> (expression) is evaluated and should return a symbol. When you want to
> specify a symbol quote its name: 'SYMBOL.
That is for original poster. I also said that way it would not work.
--
Jean Louis
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Why does saving a buffer to a new file clear local variables?
2024-12-12 12:41 ` Jean Louis
@ 2024-12-12 13:01 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-12-12 18:31 ` Jean Louis
0 siblings, 1 reply; 7+ messages in thread
From: Michael Heerdegen via Users list for the GNU Emacs text editor @ 2024-12-12 13:01 UTC (permalink / raw)
To: help-gnu-emacs
Jean Louis <bugs@gnu.support> writes:
> > Are you sure that you don't just see the global binding of that
> > variable?
>
> Unless you wish to reveal some new fact about it to me, I see that by
> using `defvar-local' I can save file and variable `abc' still has
> initially assigned value.
>
> And it has this description:
>
> abc’s value is 123
>
> Not documented as a variable.
>
> Automatically becomes buffer-local when set.
That's the global binding. A buffer local binding is reported as "local
in buffer XYZ" - even when the local binding is the same as the global
one.
`defvar-local' defines a global (aka "default") value. Until you make an
assignment in some buffer no buffer local bindings exist.
Michael.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Why does saving a buffer to a new file clear local variables?
2024-12-12 13:01 ` Michael Heerdegen via Users list for the GNU Emacs text editor
@ 2024-12-12 18:31 ` Jean Louis
0 siblings, 0 replies; 7+ messages in thread
From: Jean Louis @ 2024-12-12 18:31 UTC (permalink / raw)
To: Michael Heerdegen; +Cc: help-gnu-emacs
* Michael Heerdegen via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2024-12-12 16:02]:
> Jean Louis <bugs@gnu.support> writes:
>
> > > Are you sure that you don't just see the global binding of that
> > > variable?
> >
> > Unless you wish to reveal some new fact about it to me, I see that by
> > using `defvar-local' I can save file and variable `abc' still has
> > initially assigned value.
> >
> > And it has this description:
> >
> > abc’s value is 123
> >
> > Not documented as a variable.
> >
> > Automatically becomes buffer-local when set.
>
> That's the global binding. A buffer local binding is reported as "local
> in buffer XYZ" - even when the local binding is the same as the global
> one.
>
> `defvar-local' defines a global (aka "default") value. Until you make an
> assignment in some buffer no buffer local bindings exist.
Okay, I am lost, will review it again when I need it, even though I use heavily permanent-local variables:
(defvar-local rcd-tabulated-original-entries nil
"Original `tabulated-list-entries'.")
(put 'rcd-tabulated-original-entries 'permanent-local t)
But now when I use them, I just use them, I forgot pretty much about
it. It just works in the application.
--
Jean Louis
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Why does saving a buffer to a new file clear local variables?
2024-12-11 4:05 Why does saving a buffer to a new file clear local variables? Patrick Nicodemus
2024-12-12 7:31 ` Jean Louis
@ 2024-12-12 12:50 ` Michael Heerdegen
1 sibling, 0 replies; 7+ messages in thread
From: Michael Heerdegen @ 2024-12-12 12:50 UTC (permalink / raw)
To: Patrick Nicodemus; +Cc: help-gnu-emacs
Patrick Nicodemus <gadget142@gmail.com> writes:
> 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 recall we had a very similar question not long ago. I think Eli gave
an answer, and it was like that this situation counts as visiting a
file, and visiting a file by default clears all local bindings via
`normal-mode'.
There are ways to modify this behavior - in your scenario, turning
`change-major-mode-with-file-name' off prevents losing your local
binding.
Giving a variable a non-nil `permanent-local' property also would have
this effect - see `kill-all-local-variables's docstring. But the
details depend on the use case.
Michael.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-12-12 18:31 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-11 4:05 Why does saving a buffer to a new file clear local variables? Patrick Nicodemus
2024-12-12 7:31 ` Jean Louis
2024-12-12 11:32 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-12-12 12:41 ` Jean Louis
2024-12-12 13:01 ` Michael Heerdegen via Users list for the GNU Emacs text editor
2024-12-12 18:31 ` Jean Louis
2024-12-12 12:50 ` Michael Heerdegen
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.