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

    gptel: Split header-line into segments
    
    * gptel.el (gptel-use-header-line, gptel--header-line-info):
    Formalize gptel's header-line-format into three sections.  From
    left to right:
    1. The backend
    2. The status (Ready/Waiting etc)
    3. The info segment (the context, tools, system prompt and model).
       This is `gptel--header-line-info', a new variable.
    
    Each of these can now be individually replaced by tools or other
    UI extensions using
    
      (setf (nth N header-line-format) 'foo)
    
    This is currently used by the todo-list tool in gptel-agent.
---
 gptel.el | 173 +++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 90 insertions(+), 83 deletions(-)

diff --git a/gptel.el b/gptel.el
index 47695e47c7f..ff73d8319bc 100644
--- a/gptel.el
+++ b/gptel.el
@@ -697,92 +697,99 @@ Search between BEG and END."
         (and link-ovs (mapc #'delete-overlay link-ovs))))
     `(jit-lock-bounds ,beg . ,end)))
 
-(defun gptel-use-header-line ()
-  "Set up the header-line for a gptel buffer."
-  (setq
-   header-line-format
-   (list
-    '(:eval (concat (propertize " " 'display '(space :align-to 0))
-                    (format "%s" (gptel-backend-name gptel-backend))))
-    (propertize " Ready" 'face 'success)
-    '(:eval
-      (let* ((model (gptel--model-name gptel-model))
-             (system
-              (propertize
-               (buttonize
-                (format "[Prompt: %s]"
-                        (or (car-safe (rassoc gptel--system-message 
gptel-directives))
-                            (gptel--describe-directive gptel--system-message 
15)))
-                (lambda (&rest _) (gptel-system-prompt)))
-               'mouse-face 'highlight
-               'help-echo "System message for session"))
-             (context
-              (and gptel-context
-                   (cl-loop
-                    for entry in gptel-context
-                    if (bufferp (or (car-safe entry) entry)) count it into bufs
-                    else count (stringp (or (car-safe entry) 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"))))
-             (toggle-track-media
-              (lambda (&rest _)
-                (setq-local gptel-track-media (not gptel-track-media))
-                (if gptel-track-media
-                    (progn
-                      (run-hooks 'gptel-refresh-buffer-hook)
-                      (message "Sending media from included links."))
-                  (without-restriction (gptel--annotate-link-clear))
-                  (message "Ignoring links.  Only link text will be sent."))
-                (run-at-time 0 nil #'force-mode-line-update)))
-             (track-media
-              (and (gptel--model-capable-p 'media)
-                   (if gptel-track-media
-                       (propertize
-                        (buttonize "[Sending media]" toggle-track-media)
-                        'mouse-face 'highlight
-                        'help-echo
-                        "Sending media from links/urls when supported.\nClick 
to toggle")
+(defvar gptel--header-line-info
+  '(:eval
+    (let* ((model (gptel--model-name gptel-model))
+           (system
+            (propertize
+             (buttonize
+              (format "[Prompt: %s]"
+                      (or (car-safe (rassoc gptel--system-message 
gptel-directives))
+                          (gptel--describe-directive gptel--system-message 
15)))
+              (lambda (&rest _) (gptel-system-prompt)))
+             'mouse-face 'highlight
+             'help-echo "System message for session"))
+           (context
+            (and gptel-context
+                 (cl-loop
+                  for entry in gptel-context
+                  if (bufferp (or (car-safe entry) entry)) count it into bufs
+                  else count (stringp (or (car-safe entry) 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"))))
+           (toggle-track-media
+            (lambda (&rest _)
+              (setq-local gptel-track-media (not gptel-track-media))
+              (if gptel-track-media
+                  (progn
+                    (run-hooks 'gptel-refresh-buffer-hook)
+                    (message "Sending media from included links."))
+                (without-restriction (gptel--annotate-link-clear))
+                (message "Ignoring links.  Only link text will be sent."))
+              (run-at-time 0 nil #'force-mode-line-update)))
+           (track-media
+            (and (gptel--model-capable-p 'media)
+                 (if gptel-track-media
                      (propertize
-                      (buttonize "[Ignoring media]" toggle-track-media)
+                      (buttonize "[Sending media]" toggle-track-media)
                       'mouse-face 'highlight
                       'help-echo
-                      "Ignoring media from links/urls.\nClick to toggle"))))
-             (toggle-tools (lambda (&rest _) (interactive)
-                             (run-at-time 0 nil
-                                          (lambda () (call-interactively 
#'gptel-tools)))))
-             (tools (when (and gptel-use-tools gptel-tools)
-                      (propertize
-                       (buttonize (pcase (length gptel-tools)
-                                    (0 "[No tools]") (1 "[1 tool]")
-                                    (len (format "[%d tools]" len)))
-                                  toggle-tools)
-                       'mouse-face 'highlight
-                       'help-echo "Select tools"))))
-        (concat
-         (propertize
-          " " 'display
-          `(space :align-to (- right
-                               ,(+ 5 (length model) (length system)
-                                   (length track-media) (length context) 
(length tools)))))
-         tools (and track-media " ") track-media (and context " ") context " " 
system " "
-         (propertize
-          (buttonize (concat "[" model "]")
-                     (lambda (&rest _) (gptel-menu)))
-          'mouse-face 'highlight
-          'help-echo "Model in use")))))))
+                      "Sending media from links/urls when supported.\nClick to 
toggle")
+                   (propertize
+                    (buttonize "[Ignoring media]" toggle-track-media)
+                    'mouse-face 'highlight
+                    'help-echo
+                    "Ignoring media from links/urls.\nClick to toggle"))))
+           (toggle-tools (lambda (&rest _) (interactive)
+                           (run-at-time 0 nil
+                                        (lambda () (call-interactively 
#'gptel-tools)))))
+           (tools (when (and gptel-use-tools gptel-tools)
+                    (propertize
+                     (buttonize (pcase (length gptel-tools)
+                                  (0 "[No tools]") (1 "[1 tool]")
+                                  (len (format "[%d tools]" len)))
+                                toggle-tools)
+                     'mouse-face 'highlight
+                     'help-echo "Select tools"))))
+      (concat
+       (propertize
+        " " 'display
+        `(space :align-to (- right
+                             ,(+ 5 (length model) (length system)
+                                 (length track-media) (length context) (length 
tools)))))
+       tools (and track-media " ") track-media (and context " ") context " " 
system " "
+       (propertize
+        (buttonize (concat "[" model "]")
+                   (lambda (&rest _) (gptel-menu)))
+        'mouse-face 'highlight
+        'help-echo "Model in use"))))
+  "Information segment for the header-line in gptel-mode.")
+
+(defun gptel-use-header-line ()
+  "Set up the header-line for a gptel buffer.
+
+It is composed of three segments: the backend name, the
+status (Ready/Waiting etc) and the info segment, showing the current
+context, tools, system prompt, model and more."
+  (setq
+   header-line-format
+   (list '(:eval (concat (propertize " " 'display '(space :align-to 0))
+                         (format "%s" (gptel-backend-name gptel-backend))))
+         (propertize " Ready" 'face 'success)
+         gptel--header-line-info)))
 
 ;;;###autoload
 (define-minor-mode gptel-mode

Reply via email to