branch: elpa/gptel
commit 24032cb39df1b8527c50bbb2cee503655d8ae959
Author: John Wiegley <jo...@newartisans.com>
Commit: GitHub <nore...@github.com>

    gptel-context: add current kill to context with C-y (#832)
    
    Allow C-y to add the current kill to the context.
    
    This saves the user from having to first create a new buffer, yank
    the current kill into that buffer with C-y, and then add that
    buffer as context. With this change, they can just type C-y in the
    transient `gptel-send' buffer and the temporary is created as
    needed.
    
    If there are already kills that have been yanked, the new kills
    will be added to the context. In order to reset this buffer, use
    C-u C-y when yanking from the transient menu.
    
    * gptel-context.el (gptel-context-add-current-kill): Command to
      add current kill to the context.  Uses a hidden buffer.
    
    * gptel-transient.el (gptel-menu,
      gptel--infix-context-add-current-kill): Entry for this feature
      in `gptel-menu'.
---
 gptel-context.el   | 15 +++++++++++++++
 gptel-transient.el | 12 ++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/gptel-context.el b/gptel-context.el
index 5b831cc485..fd832019b1 100644
--- a/gptel-context.el
+++ b/gptel-context.el
@@ -84,6 +84,21 @@ context chunk.  This is accessible as, for example:
   :group 'gptel
   :type 'function)
 
+(defun gptel-context-add-current-kill (&optional arg)
+  "Add current-kill to gptel, accumulating if arg is non-nil"
+  (interactive "P")
+  (let ((kill (current-kill 0)))
+    (with-current-buffer (get-buffer-create " *gptel-kill-ring-context*")
+      (if (not arg)
+          (kill-region (point-min) (point-max))
+        (goto-char (point-max))
+        (unless (bobp)
+          (insert "\n----\n")))
+      (insert kill)
+      (gptel-context--add-region (current-buffer)
+                                 (point-min) (point-max))
+      (message "*current-kill* has been added as context."))))
+
 (defun gptel-context-add (&optional arg confirm)
   "Add context to gptel in a DWIM fashion.
 
diff --git a/gptel-transient.el b/gptel-transient.el
index d3cd48c3fd..b92bddc2ef 100644
--- a/gptel-transient.el
+++ b/gptel-transient.el
@@ -626,6 +626,7 @@ Also format its value in the Transient menu."
    [:pad-keys t ""
     (:info #'gptel--describe-infix-context
      :face transient-heading :format "%d")
+    (gptel--infix-context-add-current-kill)
     (gptel--infix-context-add-region)
     (gptel--infix-context-add-buffer)
     (gptel--infix-context-add-file)
@@ -1129,6 +1130,17 @@ supports.  See `gptel-track-media' for more information."
 (declare-function gptel-context--at-point "gptel-context")
 (declare-function gptel-add "gptel-context")
 
+(transient-define-suffix gptel--infix-context-add-current-kill (&optional arg)
+  "Add current kill to gptel's context."
+  :transient 'transient--do-stay
+  :key "C-y"
+  :if (lambda () gptel-expert-commands)
+  :description
+  "Yank to context"
+  (interactive "P")
+  (gptel-context-add-current-kill arg)
+  (transient-setup))
+
 (transient-define-suffix gptel--infix-context-add-region ()
   "Add current region to gptel's context."
   :transient 'transient--do-stay

Reply via email to