From c8f7e909daac853b196eae8188177fd4b89cef96 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Fri, 20 Feb 2026 23:33:46 +0100 Subject: [PATCH] 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. --- lisp/apropos.el | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lisp/apropos.el b/lisp/apropos.el index 1a992d83bfe..169e4660075 100644 --- a/lisp/apropos.el +++ b/lisp/apropos.el @@ -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