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

    gptel: Extract link annotation code to new function
    
    * gptel.el (gptel--annotate-link): In preparation for link
    annotation support in Markdown buffers, extract the code that
    actually applies the link overlay annotations to
    `gptel--annotate-link'.  This is a defsubst since it will be
    called many times during jit-lock.
    
    * gptel-org.el (gptel-org--annotate-links): Move annotation code
    to new function.
---
 gptel-org.el | 29 +----------------------------
 gptel.el     | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 28 deletions(-)

diff --git a/gptel-org.el b/gptel-org.el
index 634a6209ff4..06a2bf79efc 100644
--- a/gptel-org.el
+++ b/gptel-org.el
@@ -487,34 +487,7 @@ Search between BEG and END."
                 (overlay-put ov 'evaporate t)
                 (overlay-put ov 'priority -80))
               ;; Check if link will be sent, and annotate accordingly
-              (cl-destructuring-bind
-                  (valid _ path filep placementp readablep supportedp _mime)
-                  link-status
-                (if valid
-                    (progn
-                      (overlay-put
-                       ov 'before-string
-                       (concat (propertize "SEND" 'face '(:inherit success 
:height 0.8))
-                               (if (display-graphic-p)
-                                   (propertize " " 'display '(space :width 
0.5)) " ")))
-                      (overlay-put ov 'help-echo
-                                   (format "Sending file %s with gptel 
requests" path)))
-                  (overlay-put ov 'before-string
-                               (concat (propertize "!" 'face '(:inherit error))
-                                       (propertize " " 'display '(space :width 
0.3))))
-                  (overlay-put
-                   ov 'help-echo
-                   (concat
-                    "Sending only link text with gptel requests, "
-                    "link will not be followed to source.\n\nReason: "
-                    (cond
-                     ((not filep) "Not a supported link type \
-(Only \"file\" or \"attachment\" are supported)")
-                     ((not placementp)
-                      "Not a standalone link.  (Separate link from text around 
it.)")
-                     ((not readablep) (format "File %s is not readable" path))
-                     ((not supportedp) (format "%s does not support binary 
file %s"
-                                               gptel-model path))))))))))
+              (gptel--annotate-link ov link-status))))
         (and link-ovs (mapc #'delete-overlay link-ovs))))
     `(jit-lock-bounds ,beg . ,end)))
 
diff --git a/gptel.el b/gptel.el
index 045e13050bb..d9d55a67755 100644
--- a/gptel.el
+++ b/gptel.el
@@ -407,6 +407,42 @@ Note: This will move the cursor."
             (overlay-put hide-ov 'before-string
                          (propertize "..." 'face 'shadow))))))))
 
+(defsubst gptel--annotate-link (ov link-status)
+  "Annotate link overlay OV according to LINK-STATUS.
+
+LINK-STATUS is a list of link properties relevant to gptel queries, of
+the form (valid . REST).  See `gptel-markdown--validate-link' for
+details.  Indicate the (in)validity of the link for inclusion with gptel
+queries via OV."
+  (cl-destructuring-bind
+      (valid _ path filep placementp readablep supportedp _mime)
+      link-status
+    (if valid
+        (progn
+          (overlay-put
+           ov 'before-string
+           (concat (propertize "SEND" 'face '(:inherit success :height 0.8))
+                   (if (display-graphic-p)
+                       (propertize " " 'display '(space :width 0.5)) " ")))
+          (overlay-put ov 'help-echo
+                       (format "Sending file %s with gptel requests" path)))
+      (overlay-put ov 'before-string
+                   (concat (propertize "!" 'face '(:inherit error))
+                           (propertize " " 'display '(space :width 0.3))))
+      (overlay-put
+       ov 'help-echo
+       (concat
+        "Sending only link text with gptel requests, "
+        "this link will not be followed to its source.\n\nReason: "
+        (cond
+         ((not filep) "Not a supported link type\
+ (Only \"file\" or \"attachment\" are supported)")
+         ((not placementp)
+          "Not a standalone link.  (Separate link from text around it.)")
+         ((not readablep) (format "File %s is not readable" path))
+         ((not supportedp) (format "%s does not support binary file %s"
+                                   gptel-model path))))))))
+
 (defun gptel--annotate-link-clear (&optional beg end)
   "Delete all gptel org link annotations between BEG and END."
   (mapc #'delete-overlay

Reply via email to