branch: elpa/gptel
commit 7095771846805e032d8c6d733c7948ad2c95e2fd
Author: Tim Ruffing <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>

    gptel-rewrite: Finish gptel-rewrite before post-command-hook
    
    * gptel-rewrite.el (gptel--rewrite-read-message): Before this
    commit, (run-at-time 0 ...) was used in multiple places to run
    gptel--suffix-rewrite after exit-minibuffer and thus in the buffer
    that was active before the minibuffer was entered.
    
    However, this trick has the disadvantage that the gptel-rewrite
    editor command is considered finished before the code
    in (run-at-time 0 ...)  runs, and `post-command-hook' runs before
    our code has properly finished.  This commit
    uses (with-current-buffer ...)  instead to run
    `gptel--suffix-rewrite' in the right buffer
    before (exit-minibuffer) and thus before post-command-hook.
    
    This fixes an interaction with evil's visual selection mode, which
    relies on `pre-command-hook' and `post-command-hook' to adjust the
    buffer region to the visual selection whenever an editor command
    runs to make sure that the command runs on whatever the user has
    selected visually.  (#1062)
    
    `run-at-time' is still required to move the point back by 1 to
    ensure it will be on the overlay once this is inserted.
---
 gptel-rewrite.el | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/gptel-rewrite.el b/gptel-rewrite.el
index acd08b24193..e49afbe38da 100644
--- a/gptel-rewrite.el
+++ b/gptel-rewrite.el
@@ -281,12 +281,12 @@ input history list."
                     (delete-dups (cons message transient--history))))))
          (start-rewrite-maybe
           (lambda () (interactive)
+            (when (minibufferp) (funcall set-rewrite-message))
             (if transient--prefix    ;Called from transient? Don't start 
rewrite
                 (run-at-time 0 nil #'transient-setup 'gptel-rewrite)
-              (run-at-time 0 nil #'gptel--suffix-rewrite 
gptel--rewrite-message))
-            (when (minibufferp)
-              (funcall set-rewrite-message)
-              (exit-minibuffer))))
+              (with-current-buffer cb
+                (gptel--suffix-rewrite gptel--rewrite-message)))
+            (when (minibufferp) (exit-minibuffer))))
          (start-transient
           (lambda () (interactive)
             (run-at-time 0 nil #'transient-setup 'gptel-rewrite)
@@ -302,9 +302,9 @@ input history list."
                 :callback
                 (lambda (msg)
                   (when msg
-                    (run-at-time 0 nil #'gptel--suffix-rewrite)
                     (push (buffer-local-value 'gptel--rewrite-message cb)
-                          (alist-get 'gptel--infix-rewrite-extra 
transient-history)))
+                          (alist-get 'gptel--infix-rewrite-extra 
transient-history))
+                    (with-current-buffer cb (gptel--suffix-rewrite)))
                   (when (minibufferp) (exit-minibuffer)))))))
          (minibuffer-local-map
           (make-composed-keymap (define-keymap
@@ -742,7 +742,7 @@ generated from functions."
              :callback #'gptel--rewrite-callback)
       ;; Move back so that the cursor is on the overlay when done.
       (unless (get-char-property (point) 'gptel-rewrite)
-        (when (= (point) (region-end)) (backward-char 1)))
+        (when (= (point) (region-end)) (run-at-time 0 nil #'backward-char 1)))
       (deactivate-mark))))
 
 ;; Allow this to be called non-interactively for dry runs

Reply via email to