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

    gptel: Add hook for buffer UI updates
    
    * gptel.el (gptel-refresh-buffer-hook): New hook to udpate gptel's
    UI in gptel buffers.  This is intended to run after changing gptel
    settings from the header-line or gptel-menu.  Currently it is used
    to update link annotations after toggling `gptel-track-media'.
    
    This hook is currently intended for internal use only.
    
    (gptel-use-header-line): Run `gptel-refresh-buffer-hook' as
    required.
    
    * gptel-transient.el (gptel--infix-track-media): Run
    `gptel-refresh-buffer-hook' as required.
---
 gptel-transient.el | 13 +++++++++++--
 gptel.el           | 20 ++++++++++++++------
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/gptel-transient.el b/gptel-transient.el
index 18b147ab4ff..6ed53ba523d 100644
--- a/gptel-transient.el
+++ b/gptel-transient.el
@@ -1241,6 +1241,7 @@ responses."
   :prompt "Response length in tokens (leave empty: default, 80-200: short, 
200-500: long): "
   :reader 'gptel--transient-read-number)
 
+;; TODO(links): Run `gptel-refresh-buffer-hook' after a model change
 (transient-define-infix gptel--infix-provider ()
   "AI Provider for Chat."
   :description "Model"
@@ -1319,7 +1320,7 @@ querying the LLM."
   :display-if-false "No"
   :key "-R")
 
-(transient-define-infix gptel--infix-track-media ()
+(transient-define-suffix gptel--infix-track-media ()
   "Send media from \"standalone\" links in the prompt.
 
 When the active `gptel-model' supports it, gptel can send images
@@ -1330,12 +1331,20 @@ line with no surrounding text.
 What link types are sent depends on the mime-types the model
 supports.  See `gptel-track-media' for more information."
   :description "Send media from links"
+  :transient t
   :class 'gptel--switches
   :variable 'gptel-track-media
   :set-value #'gptel--set-with-scope
   :display-if-true "Yes"
   :display-if-false "No"
-  :key "-I")
+  :key "-I"
+  (interactive)
+  (let ((obj (transient-suffix-object)))
+    (transient-infix-set obj (transient-infix-read obj))
+    (transient--show))
+  (if gptel-track-media
+      (run-hooks 'gptel-refresh-buffer-hook)
+    (gptel--annotate-link-clear)))
 
 ;; ** Infixes for adding and removing context
 
diff --git a/gptel.el b/gptel.el
index a6aca95ac39..37d1062d6ac 100644
--- a/gptel.el
+++ b/gptel.el
@@ -279,6 +279,12 @@ transient menu interface provided by `gptel-menu'."
   :type 'file
   :group 'gptel)
 
+(defvar gptel-refresh-buffer-hook '(jit-lock-refontify)
+  "Hook run in gptel buffers after changing gptel's configuration.
+
+This hook runs in gptel chat buffers after making a change to gptel's
+configuration that might require a UI update.")
+
 (defvar-local gptel--bounds nil)
 (put 'gptel--bounds 'safe-local-variable #'always)
 
@@ -722,13 +728,15 @@ Search between BEG and END."
                      'help-echo "Active gptel context"))))
              (toggle-track-media
               (lambda (&rest _)
-                (setq-local gptel-track-media
-                            (not gptel-track-media))
+                (setq-local gptel-track-media (not gptel-track-media))
                 (if gptel-track-media
-                    (message
-                     (concat
-                      "Sending media from included links.  To include media, 
create "
-                      "a \"standalone\" link in a paragraph by itself, 
separated from surrounding text."))
+                    (progn
+                      (run-hooks 'gptel-refresh-buffer-hook)
+                      (message
+                       (concat
+                        "Sending media from included links.  To include media, 
create "
+                        "a \"standalone\" link in a paragraph by itself, 
separated from surrounding text.")))
+                  (without-restriction (gptel--annotate-link-clear))
                   (message "Ignoring image links.  Only link text will be 
sent."))
                 (run-at-time 0 nil #'force-mode-line-update)))
              (track-media

Reply via email to