Subject: bug#73716: 31.0.50; help-go-back creates second buffer in non-default help buffers

From: "J.P." <[email-masked]>
Date: Tue, 08 Oct 2024 20:21:45 -0700

Severity: minor

This concerns a peculiarity affecting help buffers not named "*Help*".
It was noticed while investigating bug#73637 and bug#73686 but is
ultimately unrelated and dates back to at least Emacs 27.

Normally, returning to a previous help buffer via a back button replaces
the window buffer with the previous one. And though typing "l" does that
too, the latter also re-creates the previous buffer in a new window,
resulting in different buffers with the same content (the new one being
the default "*Help*").

>From emacs -Q (on Emacs 30):

1. M-x list-faces-display RET
2. C-x o TAB TAB RET
3. In the help buffer for `abbrev-table-name':
   <backtab> <backtab> RET
4. In the help buffer for `font-lock-function-name-face':
   <backtab> RET
5. Back in `abbrev-table-name':
   TAB RET (should take you back to "*Faces*")

Now start a new session, repeating 1-3. For 4, hit "l" instead, and
witness an unwanted duplicate "*Help*" buffer pop up in another window.
A similar phenomenon exists between forward buttons and "r".

Superficially, I think the disparity results from `help-xref-following'
being t in the base recipe (at the hands of `help-do-xref'):

  * help-xref-go-back(#<buffer *Faces*>)
    help-do-xref(nil help-xref-go-back (#<buffer *Faces*>))
    help-button-action(#<marker (moves after insertion) at 751 in *Faces*>)
    button-activate(#<marker (moves after insertion) at 751 in *Faces*> nil)
    push-button(751)

Which allows for buffer reuse in `help-buffer'. No such arrangement
occurs with the "l" variant:

  * help-xref-go-back(#<buffer *Faces*>)
    help-go-back()
    funcall-interactively(help-go-back)

As an experiment, I tried using `help-do-xref' instead of `apply' in
both `help-xref-go-back' and `help-xref-go-forward' (see attached diff).
While this appears to work, I'm guessing it's breaking something
somewhere. Hopefully, someone who understands the intricacies of the
Emacs help system will know what to do.

Thanks.

In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.43, cairo version 1.18.0) of 2024-10-07 built on localhost
Repository revision: ff4de9eff30ade164655354f71e7cbca48135858
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12401002
System Description: Fedora Linux 40 (Workstation Edition)

Configured using:
 'configure --enable-check-lisp-object-type --enable-checking=yes,glyphs
 'CFLAGS=-O0 -g3'
 PKG_CONFIG_PATH=:/usr/lib64/pkgconfig:/usr/share/pkgconfig'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP
NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  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
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: 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 compile comint ansi-osc ansi-color ring comp-run
bytecomp byte-compile comp-common rx 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 cl-loaddefs cl-lib sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils 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 touch-screen 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 gtk
x-toolkit xinput2 x multi-tty move-toolbar make-network-process
native-compile emacs)

Memory information:
((conses 16 59412 9167) (symbols 48 6747 0) (strings 32 16822 4165)
 (string-bytes 1 492565) (vectors 16 11411)
 (vector-slots 8 139223 13079) (floats 8 21 4) (intervals 56 248 0)
 (buffers 984 11))

diff --git a/lisp/help-mode.el b/lisp/help-mode.el
index 4ee4f4156a1..f44f892ba27 100644
--- a/lisp/help-mode.el
+++ b/lisp/help-mode.el
@@ -792,7 +792,7 @@ help-xref-go-back
 	      position (car item)
 	      method (cadr item)
 	      args (cddr item))))
-    (apply method args)
+    (help-do-xref nil method args)
     (with-current-buffer buffer
       (if (get-buffer-window buffer)
 	  (set-window-point (get-buffer-window buffer) position)
@@ -812,7 +812,7 @@ help-xref-go-forward
 	      position (car item)
 	      method (cadr item)
 	      args (cddr item))))
-    (apply method args)
+    (help-do-xref nil method args)
     (with-current-buffer buffer
       (if (get-buffer-window buffer)
 	  (set-window-point (get-buffer-window buffer) position)