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

    gptel-transient: Update menu for context actions
    
    * gptel-transient.el (gptel--suffix-context-add-buffer,
    gptel-menu, gptel--infix-use-context,
    gptel--suffix-context-buffer): Organize context action items in
    transient menu.
    
    * gptel-context.el (gptel-context--buffer-setup,
    gptel-context--add-region): Fix bug with add-region, improve
    context buffer header.
    
    gptel-transient: Group together context options
    
    * gptel-transient.el (gptel-menu, gptel--infix-use-context): Group
    together all the context options.  This is an experiment.
    
    Fix grammar of context counter
---
 gptel-context.el   | 20 ++++++-------
 gptel-transient.el | 83 ++++++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 73 insertions(+), 30 deletions(-)

diff --git a/gptel-context.el b/gptel-context.el
index e1aa7d12c2..c794c0e534 100644
--- a/gptel-context.el
+++ b/gptel-context.el
@@ -173,7 +173,8 @@ The message is usually either a system message or user 
prompt."
   ;; Remove existing contexts in the same region, if any.
   (mapc #'gptel-context-remove
         (gptel-context--in-region buffer region-beginning region-end))
-    (prog1 (gptel-context--make-overlay region-beginning region-end)
+  (prog1 (with-current-buffer buffer
+           (gptel-context--make-overlay region-beginning region-end))
       (message "Region added to context buffer.")))
 
 (defun gptel-context--in-region (buffer start end)
@@ -283,17 +284,12 @@ context overlays, see `gptel-context--overlay-alist'."
       (erase-buffer)
       (setq header-line-format
             (concat
-             "Mark/unmark deletion with "
-             (propertize "d" 'face 'help-key-binding)
-             ", jump to next/previous with "
-             (propertize "n" 'face 'help-key-binding)
-             "/"
-             (propertize "p" 'face 'help-key-binding)
-             ", respectively. "
-             (propertize "C-c C-c" 'face 'help-key-binding)
-             " to apply, or "
-             (propertize "C-c C-k" 'face 'help-key-binding)
-             " to abort."))
+             (propertize "d" 'face 'help-key-binding) ": Mark/unmark deletion, 
"
+             (propertize "n" 'face 'help-key-binding) "/"
+             (propertize "p" 'face 'help-key-binding) ": jump to 
next/previous, "
+             (propertize "C-c C-c" 'face 'help-key-binding) ": apply, "
+             (propertize "C-c C-k" 'face 'help-key-binding) ": cancel, "
+             (propertize "q" 'face 'help-key-binding) ": quit"))
       (save-excursion
         (let ((contexts gptel-context--overlay-alist))
           (if (length> contexts 0)
diff --git a/gptel-transient.el b/gptel-transient.el
index 53ebc6a04b..195cd737f2 100644
--- a/gptel-transient.el
+++ b/gptel-transient.el
@@ -289,11 +289,14 @@ Also format its value in the Transient menu."
    [""
     "Instructions"
     ("s" "Set system message" gptel-system-prompt :transient t)
-    (gptel--infix-add-directive)]]
-  [["Context" :if (lambda () gptel-expert-commands)
-    (gptel--suffix-context-buffer)
-    (gptel--infix-use-context)]]
-  [["Model Parameters"
+    (gptel--infix-add-directive)]
+   [""
+    "Context"
+    (gptel--infix-use-context)
+    (gptel--suffix-context-add-region)
+    (gptel--suffix-context-add-buffer)
+    (gptel--suffix-context-buffer)]]
+  [["Request Parameters"
     :pad-keys t
     (gptel--infix-variable-scope)
     (gptel--infix-provider)
@@ -476,16 +479,24 @@ Customize `gptel-directives' for task-specific prompts."
 ;; ** Infixes for context aggregation
 
 (transient-define-infix gptel--infix-use-context ()
-  "Describe target destination for context injection."
-  :description "Include context"
+  "Describe target destination for context injection.
+
+gptel will include with the LLM request any additional context
+added with `gptel-add'.  This context can be ignored, included
+with the system message or included with the user prompt.
+
+Where in the request this context is included depends on the
+value of `gptel-use-context', set from here."
+  :description "Include"
   :class 'gptel-lisp-variable
   :variable 'gptel-use-context
+  :format " %k %d %v"
   :set-value #'gptel--set-with-scope
   :display-nil "No"
   :display-map '((nil    . "No")
                  (system . "with system message")
                  (user   . "with user prompt"))
-  :key "-xd"
+  :key "-i"
   :reader (lambda (prompt &rest _)
             (let* ((choices '(("No"                  . nil)
                               ("with system message" . system)
@@ -907,14 +918,15 @@ When LOCAL is non-nil, set the system message only in the 
current buffer."
                          (funcall quit-to-menu)))
         (local-set-key (kbd "C-c C-k") quit-to-menu)))))
 
-;; ** Suffix for displaying and removing context
+;; ** Suffix for adding, displaying and removing context
 (declare-function gptel-context--buffer-setup "gptel-context")
 (declare-function gptel-context--collect "gptel-context")
 
 (transient-define-suffix gptel--suffix-context-buffer ()
   "Display all contexts from all buffers & files."
   :transient 'transient--do-exit
-  :key "-xb"
+  :key "C"
+  :if (lambda () gptel-context--overlay-alist)
   :description
   (lambda ()
     (let* ((contexts (and gptel-context--overlay-alist 
(gptel-context--collect)))
@@ -923,19 +935,54 @@ When LOCAL is non-nil, set the system message only in the 
current buffer."
                          (cl-loop for (_ . ovs) in contexts
                                   sum (length ovs))
                        0)))
-      (concat "Display context buffer "
+      (concat "Inspect context "
               (format
                (propertize "(%s)" 'face 'transient-delimiter)
-               (propertize (format "%d context%s in %d buffer%s"
-                                   ov-count (if (/= ov-count 1) "s" "")
-                                   buffer-count
-                                   (if (/= buffer-count 1) "s" ""))
-                           'face (if (zerop (length contexts))
-                                     'transient-inactive-value
-                                   'transient-value))))))
+               (propertize
+                (concat
+                 (and (> ov-count 0)
+                      (format "%d region%s in %d buffer%s"
+                              ov-count (if (> ov-count 1) "s" "")
+                              (- buffer-count file-count)
+                              (if (> ( - buffer-count file-count) 1) "s" "")))
+                 (and (> file-count 0)
+                      (propertize
+                       (format "%s%d file%s"
+                               (if (> ov-count 0) ", " "") file-count
+                               (if (> file-count 1) "s" "")))))
+                'face (if (zerop (length contexts))
+                          'transient-inactive-value
+                        'transient-value))))))
   (interactive)
   (gptel-context--buffer-setup))
 
+(declare-function gptel-context--at-point "gptel-context")
+
+(transient-define-suffix gptel--suffix-context-add-region ()
+  "Add current region to gptel's context."
+  :transient 'transient--do-stay
+  :key "cr"
+  :if (lambda () (or (use-region-p)
+                (and (fboundp 'gptel-context--at-point)
+                     (gptel-context--at-point))))
+  :description
+  (lambda ()
+    (if (and (fboundp 'gptel-context--at-point)
+             (gptel-context--at-point))
+        "Remove context at point"
+      "Add region to context"))
+  (interactive)
+  (gptel-add)
+  (transient-setup))
+
+(transient-define-suffix gptel--suffix-context-add-buffer ()
+  "Add a buffer to gptel's context."
+  :transient 'transient--do-stay
+  :key "cb"
+  :description "Add a buffer to context"
+  (interactive)
+  (gptel-add '(4)))
+
 ;; ** Suffixes for rewriting/refactoring
 
 (transient-define-suffix gptel--suffix-rewrite ()

Reply via email to