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

    gptel: Activate link annotation support for Org and Markdown
    
    * gptel.el (gptel-mode): Plug in link annotation support for Org
    and Markdown into gptel-mode.  When `gptel-track-media' is
    enabled, links in gptel buffers will now be annotated to indicate
    that their sources will be sent.  If a link can not be sent for
    some reason, it will be marked as such and a link tooltip will
    explain why.
    
    * NEWS (New features and UI changes): Mention new link annotation
    feature.
---
 NEWS     | 32 +++++++++++++++++++++++---------
 gptel.el | 13 ++++++++++++-
 2 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/NEWS b/NEWS
index a6d65d2326d..c7dfb66d63f 100644
--- a/NEWS
+++ b/NEWS
@@ -40,15 +40,29 @@
 
 ** New features and UI changes
 
-- New user option ~gptel-org-validate-link~: When including links to
-  files in the prompt via ~gptel-track-media~, only "standalone" links
-  are included by default -- these are links placed on a line by
-  themselves, separated from other text.  This is to ensure that the
-  file is being included intentionally.  This behavior is now
-  customizable in Org buffers via ~gptel-org-validate-link~, which is a
-  function that determines if a link is to be considered valid for
-  inclusion with the gptel query.  You can set it to the function
-  ~always~ to try to send all links.
+- Link annotations: When ~gptel-track-media~ is enabled in gptel chat
+  buffers, gptel follows (Markdown/Org) links to files in the prompt and
+  includes these files with queries.  However, it was not clear if a
+  link type was supported and would be included, making this feature
+  unreliable and difficult to use.
+
+  Now all links in the prompt are explicitly annotated in real-time in
+  gptel buffers.  Links that will not be sent are marked as such, and
+  the link tooltip explains why.  Links that will be sent are explicitly
+  indicated as well.
+
+- New user options ~gptel-org-validate-link~ and
+  ~gptel-markdown-validate-link~: When including links to files in the
+  prompt via ~gptel-track-media~, only "standalone" links are included
+  by default -- these are links placed on a line by themselves,
+  separated from other text.  This is to ensure that the file is being
+  included intentionally.
+
+  This behavior is now customizable in Markdown/Org buffers via these
+  options.  Its value should be a function that determines if a link is
+  to be considered valid for inclusion with the gptel query.  You can
+  set it to the function ~always~ to try to send all links, irrespective
+  of their placement.
 
 - gptel preset specifications can now modify the current values of gptel
   options instead of replacing them, allowing better composition of
diff --git a/gptel.el b/gptel.el
index 1bd296eb9c4..bb8222ec880 100644
--- a/gptel.el
+++ b/gptel.el
@@ -204,6 +204,7 @@
 (declare-function gptel-org-set-topic "gptel-org")
 (declare-function gptel-org--save-state "gptel-org")
 (declare-function gptel-org--restore-state "gptel-org")
+(declare-function gptel-org--annotate-links "gptel-org")
 (define-obsolete-function-alias
   'gptel-set-topic 'gptel-org-set-topic "0.7.5")
 
@@ -695,12 +696,15 @@ Search between BEG and END."
         (gptel--prettify-preset)
         (cond
          ((derived-mode-p 'org-mode)
+          (require 'gptel-org)
+          (jit-lock-register 'gptel-org--annotate-links)
           ;; Work around bug in `org-fontify-extend-region'.
           (add-hook 'gptel-post-response-functions #'font-lock-flush nil t))
          ((derived-mode-p 'markdown-mode)
           (font-lock-add-keywords ;keymap is a font-lock-managed property in 
markdown-mode
            nil '(("^```[ \t]*\\([[:alpha:]][^\n]*\\)?$" ;match code fences
-                  0 (list 'face nil 'keymap gptel--markdown-block-map))))))
+                  0 (list 'face nil 'keymap gptel--markdown-block-map))))
+          (jit-lock-register 'gptel-markdown--annotate-links)))
         (gptel--restore-state)
         (if gptel-use-header-line
           (setq gptel--old-header-line header-line-format
@@ -793,6 +797,13 @@ Search between BEG and END."
                             (lambda (&rest _) (gptel-menu))))))))
     (remove-hook 'before-save-hook #'gptel--save-state t)
     (remove-hook 'after-change-functions 'gptel--inherit-stickiness t)
+    (cond
+     ((derived-mode-p 'org-mode)
+      (jit-lock-unregister #'gptel-org--annotate-links)
+      (without-restriction (gptel--annotate-link-clear)))
+     ((derived-mode-p 'markdown-mode)
+      (jit-lock-unregister #'gptel-markdown--annotate-links)
+      (without-restriction (gptel--annotate-link-clear))))
     (gptel--prettify-preset)
     (if gptel-use-header-line
         (setq header-line-format gptel--old-header-line

Reply via email to