Fix apropos-print window selection

The call to get-buffer-window doesn't take into account other
frames, and in theory may even return a window other than the one
just displayed.  Work around this by buffer-locally mapping the
buffer to the window used by with-output-to-temp-buffer (bug#46034).

* lisp/apropos.el (apropos--window): New buffer-local variable.
(apropos--set-window): New hook function.
(apropos-print): Add it to the local temp-buffer-show-hook before
the end of with-output-to-temp-buffer.  Check apropos--window first,
falling back on a get-buffer-window across visible frames.
This commit is contained in:
Basil L. Contovounesios
2026-02-20 23:33:46 +01:00
parent e34b6caf13
commit c8f7e909da

View File

@@ -1116,6 +1116,14 @@ Will return nil instead."
"If non-nil, use a single line per binding."
:type 'boolean)
(defvar-local apropos--window nil
"Last window the `*Apropos*' buffer was displayed in.")
(defun apropos--set-window ()
"Associate the `*Apropos*' buffer with its selected window.
Intended for `temp-buffer-show-hook'."
(setq apropos--window (selected-window)))
(defun apropos-print (do-keys spacing &optional text nosubst)
"Output result of apropos searching into buffer `*Apropos*'.
The value of `apropos-accumulator' is the list of items to output.
@@ -1248,9 +1256,18 @@ as a heading."
(apropos-print-doc apropos-item 5 'apropos-widget t)
(apropos-print-doc apropos-item 4 'apropos-plist nil))
(setq-local truncate-partial-width-windows t)
(setq-local truncate-lines t)))
(when help-window-select
(select-window (get-buffer-window "*Apropos*"))))
(setq-local truncate-lines t))
;; Avoid stale value.
(setq apropos--window nil)
(when help-window-select
;; May not be run by custom `temp-buffer-show-function'.
(add-hook 'temp-buffer-show-hook #'apropos--set-window nil t)))
(let ((win (and help-window-select
(or (buffer-local-value 'apropos--window
(get-buffer "*Apropos*"))
;; In case `temp-buffer-show-hook' did not run.
(get-buffer-window "*Apropos*" 'visible)))))
(when win (select-window win))))
(prog1 apropos-accumulator
(setq apropos-accumulator ()))) ; permit gc