Skip to content
This repository was archived by the owner on Jun 25, 2024. It is now read-only.

Commit 1105149

Browse files
committed
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.
1 parent 4d20bc8 commit 1105149

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

which-key.el

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@ to a non-nil value for the execution of a command. Like this
663663
(defvar which-key--frame nil
664664
"Internal: Holds reference to which-key frame.
665665
Used when `which-key-popup-type' is frame.")
666+
(defvar which-key--scroll-pos-table (make-hash-table :test 'equal)
667+
"Internal: Holds initial scroll positions of windows
668+
in which the which-key popup is showing.")
666669
(defvar which-key--echo-keystrokes-backup nil
667670
"Internal: Backup the initial value of `echo-keystrokes'.")
668671
(defvar which-key--prefix-help-cmd-backup nil
@@ -1132,7 +1135,12 @@ total height."
11321135
(when (and which-key-idle-secondary-delay which-key--secondary-timer-active)
11331136
(which-key--start-timer))
11341137
(which-key--lighter-restore)
1135-
(which-key--hide-popup-ignore-command)))
1138+
(which-key--hide-popup-ignore-command)
1139+
;; restore scroll position saved earlier by `which-key--show-popup'
1140+
(let ((top-pos (gethash (selected-window) which-key--scroll-pos-table)))
1141+
(when top-pos
1142+
(scroll-down (count-screen-lines (window-start) top-pos))
1143+
(remhash (selected-window) which-key--scroll-pos-table)))))
11361144

11371145
(defun which-key--hide-popup-ignore-command ()
11381146
"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
11791187
is shown, or if there is no need to start the closing timer."
11801188
(when (and (> (car act-popup-dim) 0)
11811189
(> (cdr act-popup-dim) 0))
1190+
;; save pos at top of this window
1191+
(puthash (selected-window) (window-start) which-key--scroll-pos-table)
11821192
(cl-case which-key-popup-type
11831193
;; Not called for minibuffer
11841194
;; (minibuffer (which-key--show-buffer-minibuffer act-popup-dim))

0 commit comments

Comments
 (0)