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

    gptel: Add context info to header
    
    * gptel.el (gptel-mode): Add context info to header, taking care
    to not slow down Emacs.
    
    * gptel-context.el (gptel-context-remove): When clearing a
    context, if a buffer has no context overlays left, remove it
    eagerly from `gptel-context--alist`.  This is required for the
    header line to update immediately.
    
    * README.org (Usage): Tweak phrasing of usage of `gptel-add`.
---
 README.org       | 12 ++++++------
 gptel-context.el |  8 +++++++-
 gptel.el         | 41 ++++++++++++++++++++++++++++++-----------
 3 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/README.org b/README.org
index e4b5a2e0ca..6c832f4c6f 100644
--- a/README.org
+++ b/README.org
@@ -615,12 +615,12 @@ The above code makes the backend available to select.  If 
you want it to be the
 | =gptel-menu=       | /(Same)/                                                
      |
 
|--------------------+---------------------------------------------------------------|
 
-|------------------+---------------------------------------------------------------------------|
-| *To add context* | Will be sent with each request. (Also available from the 
Transient menu)  |
-|------------------+---------------------------------------------------------------------------|
-| =gptel-add=      | Add a region or buffer to gptel's context.  Add marked 
files if in Dired. |
-| =gptel-add-file= | Add a (text-readable) file to gptel's context.            
                |
-|------------------+---------------------------------------------------------------------------|
+|------------------+--------------------------------------------------------------------------------------|
+| *To add context* | Also available from the Transient menu                    
                           |
+|------------------+--------------------------------------------------------------------------------------|
+| =gptel-add=      | Add/remove a region or buffer to gptel's context.  
Add/remove marked files in Dired. |
+| =gptel-add-file= | Add a (text-readable) file to gptel's context.            
                           |
+|------------------+--------------------------------------------------------------------------------------|
 
 
|----------------------------+----------------------------------------------------------------------------|
 | *In Org mode only*         |                                                 
                           |
diff --git a/gptel-context.el b/gptel-context.el
index 1b77557d7f..705eeb8e72 100644
--- a/gptel-context.el
+++ b/gptel-context.el
@@ -165,7 +165,13 @@ If CONTEXT is nil, removes the context at point.
 If selection is active, removes all contexts within selection."
   (cond
    ((overlayp context)
-    (delete-overlay context))
+    (delete-overlay context)
+    ;; FIXME: Quadratic cost when clearing a bunch of contexts at once
+    (unless
+        (cl-loop
+         for ov in (alist-get (current-buffer) gptel-context--alist)
+         thereis (overlay-start ov))
+      (setf (alist-get (current-buffer) gptel-context--alist nil 'remove) 
nil)))
    ((stringp context)                   ;file
     (setf (alist-get context gptel-context--alist nil 'remove #'equal) 
           nil))
diff --git a/gptel.el b/gptel.el
index f3f05a8579..9bb9d7b076 100644
--- a/gptel.el
+++ b/gptel.el
@@ -803,20 +803,39 @@ file."
                       (propertize " Ready" 'face 'success)
                       '(:eval
                         (let ((system
-                               (format "[Prompt: %s]"
-                                (or (car-safe (rassoc gptel--system-message 
gptel-directives))
-                                 (truncate-string-to-width 
gptel--system-message 15 nil nil t)))))
+                               (propertize
+                                (buttonize
+                                 (format "[Prompt: %s]"
+                                  (or (car-safe (rassoc gptel--system-message 
gptel-directives))
+                                   (truncate-string-to-width 
gptel--system-message 15 nil nil t)))
+                                 (lambda (&rest _) (gptel-system-prompt)))
+                                'mouse-face 'highlight
+                                'help-echo "System message for session"))
+                              (context
+                               (and gptel-context--alist
+                                (cl-loop for entry in gptel-context--alist
+                                 if (bufferp (car entry)) count it into bufs
+                                 else count (stringp (car entry)) into files
+                                 finally return
+                                 (propertize
+                                  (buttonize
+                                   (concat "[Context: "
+                                    (and (> bufs 0) (format "%d buf" bufs))
+                                    (and (> bufs 1) "s")
+                                    (and (> bufs 0) (> files 0) ", ")
+                                    (and (> files 0) (format "%d file" files))
+                                    (and (> files 1) "s")
+                                    "]")
+                                   (lambda (&rest _)
+                                     (require 'gptel-context)
+                                     (gptel-context--buffer-setup)))
+                                  'mouse-face 'highlight
+                                  'help-echo "Active gptel context")))))
                          (concat
                           (propertize
                            " " 'display
-                           `(space :align-to (- right ,(+ 2 (length 
gptel-model) (length system)))))
-                          (propertize
-                           (buttonize system
-                            (lambda (&rest _) (gptel-system-prompt)))
-                           'mouse-face 'highlight
-                           'help-echo
-                           "System message for buffer")
-                          " "
+                           `(space :align-to (- right ,(+ 4 (length 
gptel-model) (length system) (length context)))))
+                          context " " system " "
                           (propertize
                            (buttonize (concat "[" gptel-model "]")
                             (lambda (&rest _) (gptel-menu)))

Reply via email to