branch: elpa/gptel
commit 5f59d52db15c16a35122509a971a8f23bbf333ad
Author: Karthik Chikmagalur <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>

    gptel-request: Run post-response after inserting prompt prefix
    
    * gptel.el (gptel--handle-post-insert): Insert the prompt
    prefix (if any) in gptel buffers before running
    `gptel-post-response-functions'.  This is the correct order
    because functions in this hook might depend on the presence of the
    prompt prefix for their logic.  Using `gptel-end-of-response' is
    one example. (#1163)
    
    * gptel-request.el (gptel-post-response-functions): We want the
    response pulsing to occur after the post-response hooks have run
    -- since we moved running the hook to the end, remove the
    hardcoded call to `pulse-momentary-highlight-region' in the
    post-insert handler, and add it to `gptel-post-response-functions'
    instead.  As a bonus, this can now be disabled.
---
 gptel-request.el |  2 ++
 gptel.el         | 22 +++++++++++-----------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/gptel-request.el b/gptel-request.el
index 00cc34736cc..7f20543e5e1 100644
--- a/gptel-request.el
+++ b/gptel-request.el
@@ -221,6 +221,8 @@ response beginning and end positions are both the cursor 
position
 at the time of the request."
   :type 'hook)
 
+(add-hook 'gptel-post-response-functions 'pulse-momentary-highlight-region 70)
+
 ;; (defcustom gptel-pre-stream-insert-hook nil
 ;;   "Hook run before each insertion of the LLM's streaming response.
 
diff --git a/gptel.el b/gptel.el
index 84656cd6f50..3d96e33a092 100644
--- a/gptel.el
+++ b/gptel.el
@@ -1136,6 +1136,16 @@ No state transition here since that's handled by the 
process sentinels."
                               start-marker))
          ;; start-marker may have been moved if :buffer was read-only
          (gptel-buffer (marker-buffer start-marker)))
+    (with-current-buffer gptel-buffer
+      (if (not tracking-marker)         ;Empty response
+          (when gptel-mode (gptel--update-status " Empty response" 'success))
+        (set-marker-insertion-type tracking-marker nil) ;Lock tracking-marker
+        (when gptel-mode
+          (unless (plist-get info :in-place)
+            (save-excursion (goto-char tracking-marker)
+                            (insert gptel-response-separator
+                                    (gptel-prompt-prefix-string))))
+          (gptel--update-status  " Ready" 'success))))
     ;; Run hook in visible window to set window-point, BUG #269
     (if-let* ((gptel-window (get-buffer-window gptel-buffer 'visible)))
         (with-selected-window gptel-window
@@ -1147,17 +1157,7 @@ No state transition here since that's handled by the 
process sentinels."
         (mapc (lambda (f) (funcall f info)) (plist-get info :post))
         (run-hook-with-args
          'gptel-post-response-functions
-         (marker-position start-marker) (marker-position tracking-marker))))
-    (with-current-buffer gptel-buffer
-      (if (not tracking-marker)         ;Empty response
-          (when gptel-mode (gptel--update-status " Empty response" 'success))
-        (pulse-momentary-highlight-region start-marker tracking-marker)
-        (when gptel-mode
-          (unless (plist-get info :in-place)
-            (save-excursion (goto-char tracking-marker)
-                            (insert gptel-response-separator
-                                    (gptel-prompt-prefix-string))))
-          (gptel--update-status  " Ready" 'success))))))
+         (marker-position start-marker) (marker-position tracking-marker))))))
 
 (defun gptel--handle-error (fsm)
   "Check for errors in request state FSM.

Reply via email to