all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
@ 2023-01-27 18:29 Al Haji-Ali
  2023-02-02 10:22 ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: Al Haji-Ali @ 2023-01-27 18:29 UTC (permalink / raw)
  To: 61102


The following code:

--8<---------------cut here---------------start------------->8---

(let ((fn (lambda () (message "Buffer changed: %S" (current-buffer)))))
  (add-hook 'buffer-list-update-hook fn)
  (with-selected-window (minibuffer-window)  
    (save-window-excursion nil) nil)
  (remove-hook 'buffer-list-update-hook fn)
  nil)
  
--8<---------------cut here---------------end--------------->8---

Prints "Buffer changed: #<buffer  *Minibuf-0*>" once, even though the selection should return to the currently selected buffer (which is different from the mini-buffer).

I would expect either that the hook 'buffer-list-update-hook is not called at all, or that it is called twice (one time for every change of the selected buffer).

Best regards,
-- Al





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-01-27 18:29 bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once Al Haji-Ali
@ 2023-02-02 10:22 ` Eli Zaretskii
  2023-02-02 15:04   ` martin rudalics
  0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2023-02-02 10:22 UTC (permalink / raw)
  To: Al Haji-Ali, martin rudalics; +Cc: 61102

> From: Al Haji-Ali <abdo.haji.ali@gmail.com>
> Date: Fri, 27 Jan 2023 18:29:06 +0000
> 
> 
> The following code:
> 
> --8<---------------cut here---------------start------------->8---
> 
> (let ((fn (lambda () (message "Buffer changed: %S" (current-buffer)))))
>   (add-hook 'buffer-list-update-hook fn)
>   (with-selected-window (minibuffer-window)  
>     (save-window-excursion nil) nil)
>   (remove-hook 'buffer-list-update-hook fn)
>   nil)
>   
> --8<---------------cut here---------------end--------------->8---
> 
> Prints "Buffer changed: #<buffer  *Minibuf-0*>" once, even though the selection should return to the currently selected buffer (which is different from the mini-buffer).
> 
> I would expect either that the hook 'buffer-list-update-hook is not called at all, or that it is called twice (one time for every change of the selected buffer).

Martin, any comments on this?  FWIW, I'm not sure this is a bug, but
maybe I'm missing something.





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 10:22 ` Eli Zaretskii
@ 2023-02-02 15:04   ` martin rudalics
  2023-02-02 17:06     ` Eli Zaretskii
  2023-02-02 17:08     ` Al Haji-Ali
  0 siblings, 2 replies; 11+ messages in thread
From: martin rudalics @ 2023-02-02 15:04 UTC (permalink / raw)
  To: Eli Zaretskii, Al Haji-Ali; +Cc: 61102

 >> The following code:
 >>
 >> --8<---------------cut here---------------start------------->8---
 >>
 >> (let ((fn (lambda () (message "Buffer changed: %S" (current-buffer)))))
 >>    (add-hook 'buffer-list-update-hook fn)
 >>    (with-selected-window (minibuffer-window)
 >>      (save-window-excursion nil) nil)
 >>    (remove-hook 'buffer-list-update-hook fn)
 >>    nil)
 >>
 >> --8<---------------cut here---------------end--------------->8---
 >>
 >> Prints "Buffer changed: #<buffer  *Minibuf-0*>" once, even though the selection should return to the currently selected buffer (which is different from the mini-buffer).
 >>
 >> I would expect either that the hook 'buffer-list-update-hook is not called at all, or that it is called twice (one time for every change of the selected buffer).

"for every change of the selected buffer" is ambiguous.  I suppose the
OP means "for every change of the selected window" here.

 > Martin, any comments on this?  FWIW, I'm not sure this is a bug, but
 > maybe I'm missing something.

The doc-string of 'with-selected-window' says that:

   "It does not alter the buffer list ordering."

and the Elisp manual is more explicit by saying that

   "The ordering of recently selected windows and the buffer list remain
    unchanged unless you deliberately change them within FORMS; for
    example, by calling ‘select-window’ with argument NORECORD ‘nil’.
    Hence, this macro is the preferred way to temporarily work with
    WINDOW as the selected window without needlessly running
    ‘buffer-list-update-hook’."

The one message the OP sees comes from this call in
'set-window-configuration'

	select_window (data->current_window, Qnil, false);

'set-window-configuration' itself is called by 'save-window-excursion'.

martin

^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 15:04   ` martin rudalics
@ 2023-02-02 17:06     ` Eli Zaretskii
  2023-02-02 18:16       ` martin rudalics
  2023-02-02 17:08     ` Al Haji-Ali
  1 sibling, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2023-02-02 17:06 UTC (permalink / raw)
  To: martin rudalics; +Cc: abdo.haji.ali, 61102

> Date: Thu, 2 Feb 2023 16:04:52 +0100
> Cc: 61102@debbugs.gnu.org
> From: martin rudalics <rudalics@gmx.at>
> 
>  > Martin, any comments on this?  FWIW, I'm not sure this is a bug, but
>  > maybe I'm missing something.
> 
> The doc-string of 'with-selected-window' says that:
> 
>    "It does not alter the buffer list ordering."
> 
> and the Elisp manual is more explicit by saying that
> 
>    "The ordering of recently selected windows and the buffer list remain
>     unchanged unless you deliberately change them within FORMS; for
>     example, by calling ‘select-window’ with argument NORECORD ‘nil’.
>     Hence, this macro is the preferred way to temporarily work with
>     WINDOW as the selected window without needlessly running
>     ‘buffer-list-update-hook’."
> 
> The one message the OP sees comes from this call in
> 'set-window-configuration'
> 
> 	select_window (data->current_window, Qnil, false);
> 
> 'set-window-configuration' itself is called by 'save-window-excursion'.

So do you agree with me that there's no bug here?





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 15:04   ` martin rudalics
  2023-02-02 17:06     ` Eli Zaretskii
@ 2023-02-02 17:08     ` Al Haji-Ali
  2023-02-02 18:17       ` martin rudalics
  1 sibling, 1 reply; 11+ messages in thread
From: Al Haji-Ali @ 2023-02-02 17:08 UTC (permalink / raw)
  To: martin rudalics, Eli Zaretskii; +Cc: 61102


On 02/02/2023, martin rudalics wrote:
>    "The ordering of recently selected windows and the buffer list remain
>     unchanged unless you deliberately change them within FORMS; for
>     example, by calling ‘select-window’ with argument NORECORD ‘nil’.
>     Hence, this macro is the preferred way to temporarily work with
>     WINDOW as the selected window without needlessly running
>     ‘buffer-list-update-hook’."

I see that the behaviour is not a bug as per the documentation.

But it does lead to strange behaviours in code or packages that rely on tracking the selected window since there is no way (that I see at least) to detect that the selected window did not change, or changed back at the end of executing this code.

Best regards,
-- Al





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 17:06     ` Eli Zaretskii
@ 2023-02-02 18:16       ` martin rudalics
  2023-02-02 19:56         ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: martin rudalics @ 2023-02-02 18:16 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: abdo.haji.ali, 61102

 > So do you agree with me that there's no bug here?

Yes.

martin





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 17:08     ` Al Haji-Ali
@ 2023-02-02 18:17       ` martin rudalics
  2023-02-02 21:21         ` Al Haji-Ali
  0 siblings, 1 reply; 11+ messages in thread
From: martin rudalics @ 2023-02-02 18:17 UTC (permalink / raw)
  To: Al Haji-Ali, Eli Zaretskii; +Cc: 61102

 > I see that the behaviour is not a bug as per the documentation.
 >
 > But it does lead to strange behaviours in code or packages that rely
 >  on tracking the selected window since there is no way (that I see at
 >  least) to detect that the selected window did not change, or changed
 >  back at the end of executing this code.

You can save the selected window, call 'select-window', run your code,
call 'select-window' with the previously selected window again.  Or have
a look at 'window-selection-change-functions'.  If you told me what your
code is supposed to do within 'with-selected-window', I might be able to
give you further advice.  As a rule, both 'save-window-excursion' and
'with-selected-window' should be avoided, if possible.

martin





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 18:16       ` martin rudalics
@ 2023-02-02 19:56         ` Eli Zaretskii
  0 siblings, 0 replies; 11+ messages in thread
From: Eli Zaretskii @ 2023-02-02 19:56 UTC (permalink / raw)
  To: martin rudalics; +Cc: abdo.haji.ali, 61102-done

> Date: Thu, 2 Feb 2023 19:16:34 +0100
> Cc: abdo.haji.ali@gmail.com, 61102@debbugs.gnu.org
> From: martin rudalics <rudalics@gmx.at>
> 
>  > So do you agree with me that there's no bug here?
> 
> Yes.

Thanks, I'm therefore closing this bug.  (The issue can still be
discussed even though the bug is closed.)





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 18:17       ` martin rudalics
@ 2023-02-02 21:21         ` Al Haji-Ali
  2023-02-03  8:33           ` martin rudalics
  0 siblings, 1 reply; 11+ messages in thread
From: Al Haji-Ali @ 2023-02-02 21:21 UTC (permalink / raw)
  To: martin rudalics, Eli Zaretskii; +Cc: 61102


On 02/02/2023, martin rudalics wrote:
> You can save the selected window, call 'select-window', run your code,
> call 'select-window' with the previously selected window again.  Or have
> a look at 'window-selection-change-functions'.  If you told me what your
> code is supposed to do within 'with-selected-window', I might be able to
> give you further advice.  As a rule, both 'save-window-excursion' and
> 'with-selected-window' should be avoided, if possible.

Thanks! I should have clarified that I am developing a package that keeps track of the currently active window and executes certain code when the selected window changes (similar to auto-dim-other-buffers, if you are aware of it). I can't control what the user of my package does, in terms of combining `save-window-excursion` with `with-selected-window` or otherwise, so I am looking for a solution that works regardless

I guess my only option is have an idle timer that checks if the current selected window is different compared to the last time a `buffer-list-update-hook` was called.

-- Al





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-02 21:21         ` Al Haji-Ali
@ 2023-02-03  8:33           ` martin rudalics
  2023-02-03 11:49             ` Al Haji-Ali
  0 siblings, 1 reply; 11+ messages in thread
From: martin rudalics @ 2023-02-03  8:33 UTC (permalink / raw)
  To: Al Haji-Ali, Eli Zaretskii; +Cc: 61102

 > Thanks! I should have clarified that I am developing a package that
 > keeps track of the currently active window and executes certain code
 > when the selected window changes (similar to auto-dim-other-buffers,
 > if you are aware of it). I can't control what the user of my package
 > does, in terms of combining `save-window-excursion` with
 > `with-selected-window` or otherwise, so I am looking for a solution
 > that works regardless

'window-selection-change-functions' (or 'window-state-change-hook')
should handle that.  If these don't work for you, please tell us why.

 > I guess my only option is have an idle timer that checks if the
 > current selected window is different compared to the last time a
 > `buffer-list-update-hook` was called.

I rather suppose you want to check if it is different to that of the
last time your package did something particular to it.

martin





^ permalink raw reply	[flat|nested] 11+ messages in thread

* bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once
  2023-02-03  8:33           ` martin rudalics
@ 2023-02-03 11:49             ` Al Haji-Ali
  0 siblings, 0 replies; 11+ messages in thread
From: Al Haji-Ali @ 2023-02-03 11:49 UTC (permalink / raw)
  To: martin rudalics, Eli Zaretskii; +Cc: 61102


On 03/02/2023, martin rudalics wrote:
> 'window-selection-change-functions' (or 'window-state-change-hook')
> should handle that.  If these don't work for you, please tell us why.

Indeed! I was using the code of `auto-dim-other-buffers` as a guide, and I am not sure why
`buffer-list-update-hook` is used there instead of `window-selection-change-functions`.
Your suggestion makes sense and I will stick to it.

Best regards,
-- Al





^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-02-03 11:49 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-27 18:29 bug#61102: 28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once Al Haji-Ali
2023-02-02 10:22 ` Eli Zaretskii
2023-02-02 15:04   ` martin rudalics
2023-02-02 17:06     ` Eli Zaretskii
2023-02-02 18:16       ` martin rudalics
2023-02-02 19:56         ` Eli Zaretskii
2023-02-02 17:08     ` Al Haji-Ali
2023-02-02 18:17       ` martin rudalics
2023-02-02 21:21         ` Al Haji-Ali
2023-02-03  8:33           ` martin rudalics
2023-02-03 11:49             ` Al Haji-Ali

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.