Subject: bug#64911: 30.0.50; switch-to-buffer-preserve-window-point not respected by switch-to-(next|prev)-buffer

From: Phil Sainty <[email-masked]>
Date: Fri, 28 Jul 2023 17:24:36 +1200

The `switch-to-buffer-preserve-window-point' variable determines
whether the (BUFFER WINDOW-START POS) data returned by
`window-prev-buffers' will be used by `switch-to-buffer' to set
the window's start and point positions.

I think this variable should additionally be respected by both:

- `switch-to-prev-buffer'
- `switch-to-next-buffer'

(If not others?  These are the cases I'm presently aware of.)

Each of the latter functions has two calls like this:

  (set-window-buffer-start-and-point
   window new-buffer (nth 1 entry) (nth 2 entry))

where `entry' comes from a call to `window-prev-buffers', and the
`switch-to-buffer-preserve-window-point' value is not checked in
any of those cases.

In practice this is a problem because the position stored in the
`window-prev-buffers' data is a marker (or at least that is the case
in the scenario I am dealing with), and the buffer in question is
periodically erased and regenerated.  Erasing the buffer causes all
its markers to be moved to position 1, so the end result is that the
user loses their place.  (The buffer contents are rebuilt, but the
new content is typically similar if not identical to the old content,
and so maintaining the original position is desirable).

Tangentially there is a similar case with `quit-window' where an
internal marker is set in (window-parameter WIN 'quit-restore) and
subject to the above issue whenever the buffer is erased.

In that instance, `display-buffer-record-window' does this:

  (set-window-parameter
   window 'quit-restore
   (list 'other
         ;; A quadruple of WINDOW's buffer, start, point and height.
         (list (current-buffer) (window-start window)
               ;; Preserve window-point-insertion-type (Bug#12855).
               (copy-marker
                (window-point window) window-point-insertion-type)
               (if (window-combined-p window)
                   (window-total-height window)
                 (window-total-width window)))
         (selected-window) buffer)))

And `quit-restore-window' does this:

  ;; Restore WINDOW's previous buffer, start and point position.
  (set-window-buffer-start-and-point
   window (nth 0 quad) (nth 1 quad) (nth 2 quad))

This also doesn't test `switch-to-buffer-preserve-window-point' (or
anything similar), so the only way of circumventing this behaviour
that I can see is to clobber the marker with its own marker position
after it's been set.

I note that the cases above comprise the *only* standard calls
to `set-window-buffer-start-and-point'.

Maybe `set-window-buffer-start-and-point' itself should be
testing `switch-to-buffer-preserve-window-point' ?

-Phil

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
  version 1.16.0, Xaw scroll bars) of 2023-04-30 built on phil-lp
Repository revision: e03cfec0a455dd8c496d33c422c8edb9ac5a4005
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 
11.0.12101004
System Description: Ubuntu 22.04.2 LTS

Configured using:
  'configure --prefix=/home/phil/emacs/trunk/usr/local
  --with-x-toolkit=lucid --without-sound
  '--program-transform-name=s/^ctags$/ctags_emacs/''

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG JSON
LCMS2 LIBSELINUX LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP
THREADS TIFF TOOLKIT_SCROLL_BARS WEBP X11 XDBE XIM XINPUT2 XPM LUCID
ZLIB

Important settings:
   value of $LC_MONETARY: en_NZ.UTF-8
   value of $LC_NUMERIC: en_NZ.UTF-8
   value of $LC_TIME: en_NZ.UTF-8
   value of $LANG: en_GB.UTF-8
   value of $XMODIFIERS: @im=ibus
   locale-coding-system: utf-8-unix

Major mode: Help

Minor modes in effect:
   tooltip-mode: t
   global-eldoc-mode: t
   show-paren-mode: t
   electric-indent-mode: t
   mouse-wheel-mode: t
   tool-bar-mode: t
   menu-bar-mode: t
   file-name-shadow-mode: t
   isearch-fold-quotes-mode: t
   global-font-lock-mode: t
   font-lock-mode: t
   blink-cursor-mode: t
   buffer-read-only: t
   line-number-mode: t
   indent-tabs-mode: t
   transient-mark-mode: t
   auto-composition-mode: t
   auto-encryption-mode: t
   auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date subr-x mm-decode
mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader
sendmail rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils
cl-extra cl-print byte-opt gv bytecomp byte-compile help-fns radix-tree
help-mode misearch multi-isearch vc-git diff-mode easy-mmode
vc-dispatcher jka-compr thingatpt cl-loaddefs cl-lib find-func rmc
iso-transl tooltip cconv eldoc paren electric uniquify ediff-hook
vc-hooks lisp-float-type elisp-mode mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode lisp-mode prog-mode register
page tab-bar menu-bar rfn-eshadow isearch easymenu timer select
scroll-bar mouse jit-lock font-lock syntax font-core term/tty-colors
frame minibuffer nadvice seq simple cl-generic indonesian philippine
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads dbusbind inotify lcms2 dynamic-setting system-font-setting
font-render-setting cairo x-toolkit xinput2 x multi-tty
make-network-process emacs)

Memory information:
((conses 16 61926 21737)
  (symbols 48 6728 0)
  (strings 32 21712 1476)
  (string-bytes 1 576559)
  (vectors 16 12858)
  (vector-slots 8 183318 15975)
  (floats 8 32 45)
  (intervals 56 1270 0)
  (buffers 976 14))