gptel: Skip to end of word before sending

* gptel.el (gptel--at-word-end, gptel-send, gptel-request):
Include the word the cursor is on in the prompt, and don't break
it when inserting the response.  This is primarily useful for
evil-mode users who frequenty end up one char before the end of a
word when they switch to normal-mode.

* gptel-transient.el (gptel-send): Same.  Also fix bug with
selecting an existing buffer to send the response to.
This commit is contained in:
Karthik Chikmagalur 2023-11-07 21:19:25 -08:00
parent cee5893d79
commit 57a70c23cb
2 changed files with 11 additions and 5 deletions

View file

@ -415,7 +415,7 @@ will get progressively longer!"
'gptel) 'gptel)
t)) t))
(point)) (point))
(point)))))) (gptel--at-word-end (point)))))))
(with-current-buffer buffer (with-current-buffer buffer
(setq gptel-backend backend) (setq gptel-backend backend)
(gptel--update-header-line " Waiting..." 'warning) (gptel--update-header-line " Waiting..." 'warning)
@ -423,7 +423,7 @@ will get progressively longer!"
(setq output-to-other-buffer-p t)) (setq output-to-other-buffer-p t))
((setq gptel-buffer-name ((setq gptel-buffer-name
(cl-some (lambda (s) (and (string-prefix-p "e" s) (cl-some (lambda (s) (and (string-prefix-p "e" s)
(substring s 2))) (substring s 1)))
args)) args))
(setq buffer (get-buffer gptel-buffer-name)) (setq buffer (get-buffer gptel-buffer-name))
(setq output-to-other-buffer-p t) (setq output-to-other-buffer-p t)
@ -440,7 +440,7 @@ will get progressively longer!"
'gptel) 'gptel)
t)) t))
(point)) (point))
(point)))))) (gptel--at-word-end (point)))))))
(with-current-buffer buffer (with-current-buffer buffer
(goto-char (point-max)) (goto-char (point-max))
(if (or buffer-read-only (if (or buffer-read-only

View file

@ -387,6 +387,12 @@ and \"apikey\" as USER."
"Ensure VAL is a number." "Ensure VAL is a number."
(if (stringp val) (string-to-number val) val)) (if (stringp val) (string-to-number val) val))
(defmacro gptel--at-word-end (&rest body)
"Execute BODY at end of the current word or punctuation."
`(save-excursion
(skip-syntax-forward "w.")
,@body))
(defun gptel-prompt-string () (defun gptel-prompt-string ()
(or (alist-get major-mode gptel-prompt-prefix-alist) "")) (or (alist-get major-mode gptel-prompt-prefix-alist) ""))
@ -621,7 +627,7 @@ Model parameters can be let-bound around calls to this function."
((null position) ((null position)
(if (use-region-p) (if (use-region-p)
(set-marker (make-marker) (region-end)) (set-marker (make-marker) (region-end))
(point-marker))) (gptel--at-word-end (point-marker))))
((markerp position) position) ((markerp position) position)
((integerp position) ((integerp position)
(set-marker (make-marker) position buffer)))) (set-marker (make-marker) position buffer))))
@ -658,7 +664,7 @@ instead."
(let* ((response-pt (let* ((response-pt
(if (use-region-p) (if (use-region-p)
(set-marker (make-marker) (region-end)) (set-marker (make-marker) (region-end))
(point-marker))) (gptel--at-word-end (point-marker))))
(gptel-buffer (current-buffer)) (gptel-buffer (current-buffer))
(full-prompt (gptel--create-prompt response-pt))) (full-prompt (gptel--create-prompt response-pt)))
(funcall (funcall