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