* bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
@ 2025-01-30 17:04 Juri Linkov
2025-01-31 8:49 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 6+ messages in thread
From: Juri Linkov @ 2025-01-30 17:04 UTC (permalink / raw)
To: 75949; +Cc: martin rudalics
Please try to evaluate in the *scratch* buffer:
(progn
(setopt kill-buffer-quit-windows t)
(view-echo-area-messages)
(pop-to-buffer-same-window (get-buffer-create "foo"))
(add-hook 'kill-buffer-hook (lambda () (message "%S" (current-buffer))))
(add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
nil t)
(display-buffer (get-buffer-create "bar"))
(switch-to-buffer (get-buffer "*scratch*"))
(kill-buffer (get-buffer "foo")))
Then observe in the *Messages* buffer that the hook 'kill-buffer-hook'
is run in the *scratch* buffer that is never killed.
Afterwards try to set kill-buffer-quit-windows back to the default value nil,
and evaluate again. Then everything is correct.
It seems that kill-buffer-quit-windows somehow messes up the current buffer
while running 'kill-buffer-hook'.
This is a distilled test case from the real problem
in treesit--explorer-kill-explorer-buffer
that prevents (add-hook 'kill-buffer-hook #'save-place-to-alist)
from saving places in the right buffer.
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
2025-01-30 17:04 bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook Juri Linkov
@ 2025-01-31 8:49 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2025-02-01 17:25 ` Juri Linkov
0 siblings, 1 reply; 6+ messages in thread
From: martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2025-01-31 8:49 UTC (permalink / raw)
To: Juri Linkov, 75949
> Then observe in the *Messages* buffer that the hook 'kill-buffer-hook'
> is run in the *scratch* buffer that is never killed.
>
> Afterwards try to set kill-buffer-quit-windows back to the default value nil,
> and evaluate again. Then everything is correct.
>
> It seems that kill-buffer-quit-windows somehow messes up the current buffer
> while running 'kill-buffer-hook'.
I checked in a fix. In general, no function on 'kill-buffer-hook' can
be sure that the buffer to be killed is current because another function
on that hook might have changed the current buffer (or even killed it)
before. That promise, like similar ones in
'window-configuration-change-hook', should never have been made.
An appropriate fix could be possibly made in run_hook_with_args. But
that might change behaviors that rely on hook-2 running after hook-1
changed the current buffer. Ideally, we'd have 'kill-buffer-functions'
with the buffer to be killed as sole argument (and the function on the
hook would still have to check whether that buffer is alive).
Thanks, martin
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
2025-01-31 8:49 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2025-02-01 17:25 ` Juri Linkov
2025-02-02 8:52 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 6+ messages in thread
From: Juri Linkov @ 2025-02-01 17:25 UTC (permalink / raw)
To: martin rudalics; +Cc: 75949
close 75949 31.0.50
thanks
>> Then observe in the *Messages* buffer that the hook 'kill-buffer-hook'
>> is run in the *scratch* buffer that is never killed.
>>
>> Afterwards try to set kill-buffer-quit-windows back to the default value nil,
>> and evaluate again. Then everything is correct.
>>
>> It seems that kill-buffer-quit-windows somehow messes up the current buffer
>> while running 'kill-buffer-hook'.
>
> I checked in a fix.
Thanks, I confirm there is no problem anymore.
> In general, no function on 'kill-buffer-hook' can be sure
> that the buffer to be killed is current because another function
> on that hook might have changed the current buffer (or even killed it)
> before. That promise, like similar ones in
> 'window-configuration-change-hook', should never have been made.
>
> An appropriate fix could be possibly made in run_hook_with_args. But
> that might change behaviors that rely on hook-2 running after hook-1
> changed the current buffer. Ideally, we'd have 'kill-buffer-functions'
> with the buffer to be killed as sole argument (and the function on the
> hook would still have to check whether that buffer is alive).
Since a new argument can't be added, another variant would be at least
to set the current buffer before every call of the hook functions.
I.e. instead of
(run-hooks 'kill-buffer-hook)
to do something like
(run-hook-wrapped 'kill-buffer-hook
(lambda (fun)
(with-current-buffer current-buffer
(funcall fun))
nil))
But this won't help when buffers are switched inside hook functions.
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
2025-02-01 17:25 ` Juri Linkov
@ 2025-02-02 8:52 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2025-02-03 7:52 ` Juri Linkov
0 siblings, 1 reply; 6+ messages in thread
From: martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2025-02-02 8:52 UTC (permalink / raw)
To: Juri Linkov; +Cc: 75949
> Since a new argument can't be added, another variant would be at least
> to set the current buffer before every call of the hook functions.
> I.e. instead of
>
> (run-hooks 'kill-buffer-hook)
>
> to do something like
>
> (run-hook-wrapped 'kill-buffer-hook
> (lambda (fun)
> (with-current-buffer current-buffer
> (funcall fun))
> nil))
>
> But this won't help when buffers are switched inside hook functions.
The problem is that we run hooks completely in C so wrapping is at least
not that simple. Whatever it is, nesting 'kill-buffer-hook' as in
(add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
nil t)
is not really recommended.
martin
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
2025-02-02 8:52 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2025-02-03 7:52 ` Juri Linkov
2025-02-03 8:33 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 6+ messages in thread
From: Juri Linkov @ 2025-02-03 7:52 UTC (permalink / raw)
To: martin rudalics; +Cc: 75949
> Whatever it is, nesting 'kill-buffer-hook' as in
>
> (add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
> nil t)
>
> is not really recommended.
What is a recommended way to do this? This is a valid use case
when killing one buffer requires also killing a dependent buffer.
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
2025-02-03 7:52 ` Juri Linkov
@ 2025-02-03 8:33 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 0 replies; 6+ messages in thread
From: martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2025-02-03 8:33 UTC (permalink / raw)
To: Juri Linkov; +Cc: 75949
>> Whatever it is, nesting 'kill-buffer-hook' as in
>>
>> (add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
>> nil t)
>>
>> is not really recommended.
>
> What is a recommended way to do this? This is a valid use case
> when killing one buffer requires also killing a dependent buffer.
Any function on any hook that changes the corresponding infrastructure
should preserve the state around any such change. The basic values that
should remain unchanged are the selected window, the selected window of
each frame and the current buffer (unless the function deletes them).
martin
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-02-03 8:33 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-30 17:04 bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook Juri Linkov
2025-01-31 8:49 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2025-02-01 17:25 ` Juri Linkov
2025-02-02 8:52 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2025-02-03 7:52 ` Juri Linkov
2025-02-03 8:33 ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).