GNU bug report logs - #61102
28.1; with-selected-window/save-window-excursion calls buffer-list-update-hook only once

Previous Next

Package: emacs;

Reported by: Al Haji-Ali <abdo.haji.ali <at> gmail.com>

Date: Fri, 27 Jan 2023 18:41:02 UTC

Severity: normal

Found in version 28.1

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 61102 in the body.
You can then email your comments to 61102 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Fri, 27 Jan 2023 18:41:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Al Haji-Ali <abdo.haji.ali <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 27 Jan 2023 18:41:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Al Haji-Ali <abdo.haji.ali <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
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).

Best regards,
-- Al




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Thu, 02 Feb 2023 10:24:02 GMT) Full text and rfc822 format available.

Message #8 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Al Haji-Ali <abdo.haji.ali <at> gmail.com>, martin rudalics <rudalics <at> gmx.at>
Cc: 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
Date: Thu, 02 Feb 2023 12:22:53 +0200
> From: Al Haji-Ali <abdo.haji.ali <at> 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.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Thu, 02 Feb 2023 15:06:02 GMT) Full text and rfc822 format available.

Message #11 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Eli Zaretskii <eliz <at> gnu.org>, Al Haji-Ali <abdo.haji.ali <at> gmail.com>
Cc: 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
Date: Thu, 2 Feb 2023 16:04:52 +0100
>> 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

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Thu, 02 Feb 2023 17:07:02 GMT) Full text and rfc822 format available.

Message #14 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: martin rudalics <rudalics <at> gmx.at>
Cc: abdo.haji.ali <at> gmail.com, 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
Date: Thu, 02 Feb 2023 19:06:00 +0200
> Date: Thu, 2 Feb 2023 16:04:52 +0100
> Cc: 61102 <at> debbugs.gnu.org
> From: martin rudalics <rudalics <at> 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?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Thu, 02 Feb 2023 17:21:03 GMT) Full text and rfc822 format available.

Message #17 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: Al Haji-Ali <abdo.haji.ali <at> gmail.com>
To: martin rudalics <rudalics <at> gmx.at>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion
 calls buffer-list-update-hook only once
Date: Thu, 02 Feb 2023 17:08:55 +0000
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




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Thu, 02 Feb 2023 18:17:02 GMT) Full text and rfc822 format available.

Message #20 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: abdo.haji.ali <at> gmail.com, 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
Date: Thu, 2 Feb 2023 19:16:34 +0100
> So do you agree with me that there's no bug here?

Yes.

martin




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Thu, 02 Feb 2023 18:18:01 GMT) Full text and rfc822 format available.

Message #23 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Al Haji-Ali <abdo.haji.ali <at> gmail.com>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
Date: Thu, 2 Feb 2023 19:17:10 +0100
> 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




Reply sent to Eli Zaretskii <eliz <at> gnu.org>:
You have taken responsibility. (Thu, 02 Feb 2023 19:58:01 GMT) Full text and rfc822 format available.

Notification sent to Al Haji-Ali <abdo.haji.ali <at> gmail.com>:
bug acknowledged by developer. (Thu, 02 Feb 2023 19:58:02 GMT) Full text and rfc822 format available.

Message #28 received at 61102-done <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: martin rudalics <rudalics <at> gmx.at>
Cc: abdo.haji.ali <at> gmail.com, 61102-done <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
Date: Thu, 02 Feb 2023 21:56:56 +0200
> Date: Thu, 2 Feb 2023 19:16:34 +0100
> Cc: abdo.haji.ali <at> gmail.com, 61102 <at> debbugs.gnu.org
> From: martin rudalics <rudalics <at> 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.)




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Thu, 02 Feb 2023 21:31:02 GMT) Full text and rfc822 format available.

Message #31 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: Al Haji-Ali <abdo.haji.ali <at> gmail.com>
To: martin rudalics <rudalics <at> gmx.at>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion
 calls buffer-list-update-hook only once
Date: Thu, 02 Feb 2023 21:21:12 +0000
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




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Fri, 03 Feb 2023 08:34:02 GMT) Full text and rfc822 format available.

Message #34 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Al Haji-Ali <abdo.haji.ali <at> gmail.com>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion calls
 buffer-list-update-hook only once
Date: Fri, 3 Feb 2023 09:33:27 +0100
> 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




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#61102; Package emacs. (Fri, 03 Feb 2023 12:01:02 GMT) Full text and rfc822 format available.

Message #37 received at 61102 <at> debbugs.gnu.org (full text, mbox):

From: Al Haji-Ali <abdo.haji.ali <at> gmail.com>
To: martin rudalics <rudalics <at> gmx.at>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 61102 <at> debbugs.gnu.org
Subject: Re: bug#61102: 28.1; with-selected-window/save-window-excursion
 calls buffer-list-update-hook only once
Date: Fri, 03 Feb 2023 11:49:14 +0000
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




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 03 Mar 2023 12:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 48 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.