branch: elpa/gptel
commit 25efd55002c591b3721cdd2c96dac93d70dce814
Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>

    gptel: Make gptel responses front-sticky
    
    * gptel.el (gptel--attach-response-history,
    gptel--insert-response, gptel--restore-state): Make the `gptel'
    text property front-sticky, so that they are now front-sticky and
    rear-nonsticky.  This means text typed at the start of a response
    is considered part of the response , while text typed at the end
    is not. (#249, #321, #343, #546)
    
    This is another experiment to address the longstanding problem of
    being able to edit gptel responses as responses without creating
    the many edge cases caused by rear-sticky text properties.  In the
    process we hopefully also avoid a whole bunch of API validation
    errors caused by whitespace user-edits in the buffer. (#351, #409,
    
    This change is tentative and might be reverted in the future.
    
    * gptel-org.el (gptel-org--restore-state): Concomitant changes.
    
    * gptel-curl.el (gptel-curl--stream-insert-response): Concomitant
    changes.
---
 gptel-curl.el |  5 +++--
 gptel-org.el  |  3 ++-
 gptel.el      | 16 +++++++++-------
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/gptel-curl.el b/gptel-curl.el
index ea25277ef7..2c90cadc8b 100644
--- a/gptel-curl.el
+++ b/gptel-curl.el
@@ -272,8 +272,9 @@ See `gptel--url-get-response' for details."
           (when transformer
             (setq response (funcall transformer response)))
 
-          (put-text-property
-           0 (length response) 'gptel 'response response)
+          (add-text-properties
+           0 (length response) '(gptel response front-sticky (gptel))
+           response)
           (goto-char tracking-marker)
           ;; (run-hooks 'gptel-pre-stream-hook)
           (insert response)
diff --git a/gptel-org.el b/gptel-org.el
index 9061a38123..6ad343c09a 100644
--- a/gptel-org.el
+++ b/gptel-org.el
@@ -357,7 +357,8 @@ ARGS are the original function call arguments."
         (progn
           (when-let ((bounds (org-entry-get (point-min) "GPTEL_BOUNDS")))
             (mapc (pcase-lambda (`(,beg . ,end))
-                    (put-text-property beg end 'gptel 'response))
+                    (add-text-properties
+                     beg end '(gptel response front-sticky (gptel))))
                   (read bounds)))
           (pcase-let ((`(,system ,backend ,model ,temperature ,tokens ,num)
                        (gptel-org--entry-properties (point-min))))
diff --git a/gptel.el b/gptel.el
index b0d32b110b..b8a2ae05c6 100644
--- a/gptel.el
+++ b/gptel.el
@@ -1098,7 +1098,8 @@ Valid JSON unless NO-JSON is t."
           (gptel-org--restore-state))
       (when gptel--bounds
         (mapc (pcase-lambda (`(,beg . ,end))
-                (put-text-property beg end 'gptel 'response))
+                (add-text-properties
+                 beg end '(gptel response front-sticky (gptel))))
               gptel--bounds)
         (message "gptel chat restored."))
       (when gptel--backend-name
@@ -1591,8 +1592,8 @@ See `gptel--url-get-response' for details."
             (when-let* ((transformer (plist-get info :transformer)))
               (setq response (funcall transformer response)))
             (save-excursion
-              (put-text-property
-               0 (length response) 'gptel 'response response)
+              (add-text-properties
+               0 (length response) '(gptel response front-sticky (gptel)) 
response)
               (with-current-buffer (marker-buffer start-marker)
                 (goto-char start-marker)
                 (run-hooks 'gptel-pre-response-hook)
@@ -1971,10 +1972,11 @@ against if required."
     (letrec ((gptel--attach-after
               (lambda (b e)
                 (when (and b e)
-                  (put-text-property
-                   b e 'gptel-history
-                   (append (ensure-list history)
-                           (get-char-property (1- e) 'gptel-history))))
+                  (add-text-properties
+                   b e `(gptel-history
+                         ,(append (ensure-list history)
+                           (get-char-property (1- e) 'gptel-history))
+                         front-sticky (gptel gptel-history))))
                 (remove-hook 'gptel-post-response-functions
                              gptel--attach-after 'local))))
       (add-hook 'gptel-post-response-functions gptel--attach-after

Reply via email to