Subject: bug#71616: closed (Re: bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .)

From: [email-masked] (GNU bug Tracking System)
Date: Sat, 06 Jul 2024 07:42:02 +0000

Your bug report

#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
71616: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=71616
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#71616: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .
From: Eli Zaretskii <[email-masked]>
To: martin rudalics <[email-masked]>
Cc: [email-masked], [email-masked], [email-masked]
Date: Sat, 06 Jul 2024 10:41:26 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

> Cc: [email-masked], Juri Linkov <[email-masked]>
> Date: Sun, 23 Jun 2024 10:39:37 +0200
> From:  martin rudalics via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
>  > 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.

No further comments in 2 weeks, so I'm now closing this bug.

Thanks.

Subject: switch-to-buffer behaves inconsistently when switch-to-buffer-obey-display-actions is t and nil .
From: Siyuan Chen <[email-masked]>
To: [email-masked]
Date: Tue, 18 Jun 2024 02:14:52 +0800
List-Id: <debbugs-submit.debbugs.gnu.org>

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