unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .
@ 2024-06-17 18:14 Siyuan Chen
  2024-06-17 18:34 ` Juri Linkov
  0 siblings, 1 reply; 5+ messages in thread
From: Siyuan Chen @ 2024-06-17 18:14 UTC (permalink / raw)
  To: 71616

[-- Attachment #1: Type: text/plain, Size: 3677 bytes --]

Hi,

Recently I found a strange behavior about `switch-to-buffer' when
`switch-to-buffer-obey-display-actions' is t.

At the moment, by default, `switch-to-buffer-obey-display-actions' is set
as nil, so `switch-to-buffer' does not be affected by some display-buffer
options, such as `display-buffer-overriding-action',
`display-buffer-alist', etc.

However, if `switch-to-buffer-obey-display-actions' is t,
 `switch-to-buffer' can adapt these display-buffer options. This feature
was introduced by the commit 3f36651c6470bab951f12f486eb4928235f1ba50 and
the implementation is to call `pop-to-buffer-same-window' inside
`switch-to-buffer'.

More details, see
bug#32790
https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=3f36651c6470bab951f12f486eb4928235f1ba50
https://mail.gnu.org/archive/html/bug-gnu-emacs/2018-11/msg00962.html

The problem is that the `switch-to-buffer' also obeys
`switch-to-buffer-preserve-window-point' while `pop-to-buffer-same-window'
doesn't. Therefore this new feature has to be compatible with
`switch-to-buffer-preserve-window-point' too. This has been implemented in
that commit but not quite right IMHO.

Let's examine the different behaviors when
`switch-to-buffer-obey-display-actions' is nil and t in `switch-to-buffer':

- When `switch-to-buffer-obey-display-actions' is nil,
`switch-to-buffer-preserve-window-point' is t (or nil, no matter actually)
and `(eq buffer (window-buffer))`, the `switch-to-buffer' will do nothing.

- When `switch-to-buffer-obey-display-actions' is t and
`switch-to-buffer-preserve-window-point' is t, `switch-to-buffer' will set
window-point if the target window is the same as the current window (yep,
that is the switch semantics). However, it does not consider what happens
when the target buffer is the same as the current buffer, just like `(eq
buffer (window-buffer))` in the 1st situation!

BTW the `switch-to-buffer-preserve-window-point' docs is a bit confusion:

> This variable is ignored if the buffer is already displayed in
> the selected window or never appeared in it before, or if
> ‘switch-to-buffer’ calls ‘pop-to-buffer’ to display the buffer,
> or non-nil ‘switch-to-buffer-obey-display-actions’ displays it
> in another window.

It doesn't mention what happens if non-nil
‘switch-to-buffer-obey-display-actions’ displays it in the same window with
the buffer that is already displayed. I also don't know whether the
sentence i.e. "if the buffer is already displayed in the selected window"
includes ‘switch-to-buffer-obey-display-actions’ is t.

The following are the minimal steps to reproduce.

## Test with default settings:

1. Emacs -Q

2. Open a .el file, e.g. window.el, and keep the cursor location at 1

3. Click buffers menu -> *scratch* to switch buffer

4. Click buffers menu -> window.el to switch buffer back

5. Move the current location to L24

6. M-x switch-to-buffer window.el

The result is that the buffer and window state keeps original, everything
is fine.

## Test with switch-to-buffer-obey-display-actions = t

1. Emacs -Q

2. M-x eval-expression (setq switch-to-buffer-obey-display-actions t)

2. Open a .el file, e.g. window.el, and keep the cursor location at 1

3. Click buffers menu -> *scratch* to switch buffer

4. Click buffers menu -> window.el to switch buffer back

5. Move the current location to L24

6. M-x switch-to-buffer window.el

The result is that the current cursor location jumps to location at 1!

This inconsistent behavior convinced me that this is a bug.

Emacs 29.3 on Window.

Thanks.

Best regards,

Siyuan Chen

[-- Attachment #2: Type: text/html, Size: 4257 bytes --]

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

* bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .
  2024-06-17 18:14 bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil Siyuan Chen
@ 2024-06-17 18:34 ` Juri Linkov
  2024-06-19  9:37   ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 5+ messages in thread
From: Juri Linkov @ 2024-06-17 18:34 UTC (permalink / raw)
  To: martin rudalics; +Cc: 71616, Siyuan Chen

> Recently I found a strange behavior about `switch-to-buffer' when
> `switch-to-buffer-obey-display-actions' is t.
>
> At the moment, by default, `switch-to-buffer-obey-display-actions' is set
> as nil, so `switch-to-buffer' does not be affected by some display-buffer
> options, such as `display-buffer-overriding-action',
> `display-buffer-alist', etc.
>
> However, if `switch-to-buffer-obey-display-actions' is t, 
> `switch-to-buffer' can adapt these display-buffer options. This feature was
> introduced by the commit 3f36651c6470bab951f12f486eb4928235f1ba50 and the
> implementation is to call `pop-to-buffer-same-window' inside
> `switch-to-buffer'.
>
> More details, see 
> bug#32790
> https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=3f36651c6470bab951f12f486eb4928235f1ba50
>
> https://mail.gnu.org/archive/html/bug-gnu-emacs/2018-11/msg00962.html
>
> The problem is that the `switch-to-buffer' also obeys
> `switch-to-buffer-preserve-window-point' while `pop-to-buffer-same-window'
> doesn't. Therefore this new feature has to be compatible with
> `switch-to-buffer-preserve-window-point' too. This has been implemented in
> that commit but not quite right IMHO.
>
> Let's examine the different behaviors when
> `switch-to-buffer-obey-display-actions' is nil and t in `switch-to-buffer':
>
> - When `switch-to-buffer-obey-display-actions' is nil,
> `switch-to-buffer-preserve-window-point' is t (or nil, no matter actually)
> and `(eq buffer (window-buffer))`, the `switch-to-buffer' will do nothing.
>
> - When `switch-to-buffer-obey-display-actions' is t and
> `switch-to-buffer-preserve-window-point' is t, `switch-to-buffer' will set
> window-point if the target window is the same as the current window (yep,
> that is the switch semantics). However, it does not consider what happens
> when the target buffer is the same as the current buffer, just like `(eq
> buffer (window-buffer))` in the 1st situation!
>
> BTW the `switch-to-buffer-preserve-window-point' docs is a bit confusion:
>
>> This variable is ignored if the buffer is already displayed in
>> the selected window or never appeared in it before, or if
>> ‘switch-to-buffer’ calls ‘pop-to-buffer’ to display the buffer,
>> or non-nil ‘switch-to-buffer-obey-display-actions’ displays it
>> in another window.
>
> It doesn't mention what happens if non-nil
> ‘switch-to-buffer-obey-display-actions’ displays it in the same window with
> the buffer that is already displayed. I also don't know whether the
> sentence i.e. "if the buffer is already displayed in the selected window"
> includes ‘switch-to-buffer-obey-display-actions’ is t.
>
> The following are the minimal steps to reproduce.
>
> ## Test with default settings:
> 1. Emacs -Q
> 2. Open a .el file, e.g. window.el, and keep the cursor location at 1
> 3. Click buffers menu -> *scratch* to switch buffer
> 4. Click buffers menu -> window.el to switch buffer back
> 5. Move the current location to L24
> 6. M-x switch-to-buffer window.el
>
> The result is that the buffer and window state keeps original, everything
> is fine.
>
> ## Test with switch-to-buffer-obey-display-actions = t
> 1. Emacs -Q
> 2. M-x eval-expression (setq switch-to-buffer-obey-display-actions t)
> 2. Open a .el file, e.g. window.el, and keep the cursor location at 1
> 3. Click buffers menu -> *scratch* to switch buffer
> 4. Click buffers menu -> window.el to switch buffer back
> 5. Move the current location to L24
> 6. M-x switch-to-buffer window.el 
> The result is that the current cursor location jumps to location at 1!
> This inconsistent behavior convinced me that this is a bug.

I confirm this in Emacs 30.  Maybe Martin could suggest how to fix this.





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

* bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .
  2024-06-17 18:34 ` Juri Linkov
@ 2024-06-19  9:37   ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-06-23  5:44     ` Siyuan Chen
  0 siblings, 1 reply; 5+ messages in thread
From: martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-06-19  9:37 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 71616, Siyuan Chen

[-- Attachment #1: Type: text/plain, Size: 1334 bytes --]

 >> ## Test with switch-to-buffer-obey-display-actions = t
 >> 1. Emacs -Q
 >> 2. M-x eval-expression (setq switch-to-buffer-obey-display-actions t)
 >> 2. Open a .el file, e.g. window.el, and keep the cursor location at 1
 >> 3. Click buffers menu -> *scratch* to switch buffer
 >> 4. Click buffers menu -> window.el to switch buffer back
 >> 5. Move the current location to L24
 >> 6. M-x switch-to-buffer window.el
 >> The result is that the current cursor location jumps to location at 1!
 >> This inconsistent behavior convinced me that this is a bug.
 >
 > I confirm this in Emacs 30.  Maybe Martin could suggest how to fix
 >> this.

A recipe without involving a file is

1. M-: (setq switch-to-buffer-obey-display-actions t)
2. C-x b *Messages*
3. C-x b *scratch*
4. Move point
5. C-x b *scratch*

In step 2 we record the point of *scratch*.  In 5 we go there because
the 'set-window-buffer' called by C-x b decides that the window already
shows buffer and does not record its current position.  I attach a patch
that should fix this and the original scenario.  It does _not_ fix the
case where instead of C-x b plain 'set-window-buffer' is used.  If we
wanted to fix that, we'd have to call 'record-window-buffer' from there
even when the old and the new buffer are the same.  I'm not sure whether
we want to do that.

martin

[-- Attachment #2: switch-to-buffer-obey-display-actions.diff --]
[-- Type: text/x-patch, Size: 902 bytes --]

diff --git a/lisp/window.el b/lisp/window.el
index 604b9868921..2ff4928c11e 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -9171,9 +9243,14 @@ switch-to-buffer
         (pop-to-buffer buffer norecord)))
      (t
       (when switch-to-buffer-obey-display-actions
-        (let ((selected-window (selected-window)))
+        (let* ((selected-window (selected-window))
+	       (old-window-buffer (window-buffer selected-window)))
           (pop-to-buffer-same-window buffer norecord)
-          (when (eq (selected-window) selected-window)
+	  ;; Do not ask for setting start and point when showing the
+	  ;; same buffer in the old selected window (Bug#71616).
+          (when (and (eq (selected-window) selected-window)
+		     (not (eq (window-buffer selected-window)
+			      old-window-buffer)))
             (setq set-window-start-and-point t))))
 
       (when set-window-start-and-point

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

* bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .
  2024-06-19  9:37   ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-06-23  5:44     ` Siyuan Chen
  2024-06-23  8:39       ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 5+ messages in thread
From: Siyuan Chen @ 2024-06-23  5:44 UTC (permalink / raw)
  To: martin rudalics; +Cc: 71616, Juri Linkov

[-- Attachment #1: Type: text/plain, Size: 1630 bytes --]

This patch fixed my original use case, which did not touch
'set-window-buffer'.

Thanks.

Best regards,
Siyuan Chen

On Wed, Jun 19, 2024 at 5:37 PM martin rudalics <rudalics@gmx.at> wrote:

>  >> ## Test with switch-to-buffer-obey-display-actions = t
>  >> 1. Emacs -Q
>  >> 2. M-x eval-expression (setq switch-to-buffer-obey-display-actions t)
>  >> 2. Open a .el file, e.g. window.el, and keep the cursor location at 1
>  >> 3. Click buffers menu -> *scratch* to switch buffer
>  >> 4. Click buffers menu -> window.el to switch buffer back
>  >> 5. Move the current location to L24
>  >> 6. M-x switch-to-buffer window.el
>  >> The result is that the current cursor location jumps to location at 1!
>  >> This inconsistent behavior convinced me that this is a bug.
>  >
>  > I confirm this in Emacs 30.  Maybe Martin could suggest how to fix
>  >> this.
>
> A recipe without involving a file is
>
> 1. M-: (setq switch-to-buffer-obey-display-actions t)
> 2. C-x b *Messages*
> 3. C-x b *scratch*
> 4. Move point
> 5. C-x b *scratch*
>
> In step 2 we record the point of *scratch*.  In 5 we go there because
> the 'set-window-buffer' called by C-x b decides that the window already
> shows buffer and does not record its current position.  I attach a patch
> that should fix this and the original scenario.  It does _not_ fix the
> case where instead of C-x b plain 'set-window-buffer' is used.  If we
> wanted to fix that, we'd have to call 'record-window-buffer' from there
> even when the old and the new buffer are the same.  I'm not sure whether
> we want to do that.
>
> martin

[-- Attachment #2: Type: text/html, Size: 2167 bytes --]

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

* bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .
  2024-06-23  5:44     ` Siyuan Chen
@ 2024-06-23  8:39       ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 5+ messages in thread
From: martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-06-23  8:39 UTC (permalink / raw)
  To: Siyuan Chen; +Cc: 71616, Juri Linkov

 > This patch fixed my original use case, which did not touch
 > 'set-window-buffer'.

Pushed to master.  The 'set-window-buffer' scenario is not likely to
cause any harm unless someone uses it in combination with
'set-window-start-and-point' from a previous buffer entry of that window
in which case the author will have to do the same thing I did.

Please consider closing this bug if you see no further problems.

Thanks, martin





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

end of thread, other threads:[~2024-06-23  8:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-17 18:14 bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil Siyuan Chen
2024-06-17 18:34 ` Juri Linkov
2024-06-19  9:37   ` martin rudalics via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-23  5:44     ` Siyuan Chen
2024-06-23  8:39       ` 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).