From 9126bed43f91a4e042ad76ddd26848fbd1b2c5a7 Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Mon, 25 Dec 2023 14:02:06 -0800 Subject: [PATCH] gptel: Set window when doing auto-scrolling * gptel.el (gptel-auto-scroll): After calling `gptel-send`, the window focus could have changed as the response is received. Set the window correctly when running `gptel-auto-scroll` to ensure the correct buffer is scrolled. --- gptel.el | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gptel.el b/gptel.el index ce93574..79e805e 100644 --- a/gptel.el +++ b/gptel.el @@ -462,14 +462,18 @@ and \"apikey\" as USER." "Scroll window if LLM response continues below viewport. Note: This will move the cursor." - (when (and (window-live-p (get-buffer-window (current-buffer))) - (not (pos-visible-in-window-p))) - (scroll-up-command))) + (when-let* ((win (get-buffer-window (current-buffer) 'visible)) + ((not (pos-visible-in-window-p (point) win))) + (scroll-error-top-bottom t)) + (condition-case nil + (with-selected-window win + (scroll-up-command)) + (error nil)))) (defun gptel-end-of-response (&optional arg) "Move point to the end of the LLM response ARG times." (interactive "p") - (dotimes (if arg (abs arg) 1) + (dotimes (_ (if arg (abs arg) 1)) (text-property-search-forward 'gptel 'response t) (when (looking-at (concat "\n\\{1,2\\}" (regexp-quote