From f610eff001d527fae8f31e068957a82ce30f01a9 Mon Sep 17 00:00:00 2001 From: 45mm <45mm.cartridge421@slmail.me> Date: Wed, 7 Feb 2024 18:23:54 +0530 Subject: [PATCH] Restore window scroll after popup close Currently, if displaying the popup would hide point, the window is scrolled to prevent this; but it is not scrolled back when the popup closes. This can be disorienting when the popup is tall, as the scroll changes by a large amount. The solution is to store the scroll position of the selected window before showing the popup, and restore it after closing it. --- which-key.el | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/which-key.el b/which-key.el index d68942c..c11a36f 100644 --- a/which-key.el +++ b/which-key.el @@ -663,6 +663,9 @@ to a non-nil value for the execution of a command. Like this (defvar which-key--frame nil "Internal: Holds reference to which-key frame. Used when `which-key-popup-type' is frame.") +(defvar which-key--scroll-pos-table (make-hash-table :test 'equal) + "Internal: Holds initial scroll positions of windows +in which the which-key popup is showing.") (defvar which-key--echo-keystrokes-backup nil "Internal: Backup the initial value of `echo-keystrokes'.") (defvar which-key--prefix-help-cmd-backup nil @@ -1132,7 +1135,12 @@ total height." (when (and which-key-idle-secondary-delay which-key--secondary-timer-active) (which-key--start-timer)) (which-key--lighter-restore) - (which-key--hide-popup-ignore-command))) + (which-key--hide-popup-ignore-command) + ;; restore scroll position if saved earlier by `which-key--show-popup' + (let ((top-pos (gethash (selected-window) which-key--scroll-pos-table))) + (when top-pos + (scroll-down (count-screen-lines (window-start) top-pos)) + (remhash (selected-window) which-key--scroll-pos-table))))) (defun which-key--hide-popup-ignore-command () "Version of `which-key--hide-popup' without the check of @@ -1179,6 +1187,8 @@ buffer text to be displayed in the popup. Return nil if no window is shown, or if there is no need to start the closing timer." (when (and (> (car act-popup-dim) 0) (> (cdr act-popup-dim) 0)) + ;; save pos at top of this window + (puthash (selected-window) (window-start) which-key--scroll-pos-table) (cl-case which-key-popup-type ;; Not called for minibuffer ;; (minibuffer (which-key--show-buffer-minibuffer act-popup-dim))