Skip to content

Commit 25efd55

Browse files
committed
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.
1 parent f6ec9af commit 25efd55

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

gptel-curl.el

+3-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ See `gptel--url-get-response' for details."
272272
(when transformer
273273
(setq response (funcall transformer response)))
274274

275-
(put-text-property
276-
0 (length response) 'gptel 'response response)
275+
(add-text-properties
276+
0 (length response) '(gptel response front-sticky (gptel))
277+
response)
277278
(goto-char tracking-marker)
278279
;; (run-hooks 'gptel-pre-stream-hook)
279280
(insert response)

gptel-org.el

+2-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ ARGS are the original function call arguments."
357357
(progn
358358
(when-let ((bounds (org-entry-get (point-min) "GPTEL_BOUNDS")))
359359
(mapc (pcase-lambda (`(,beg . ,end))
360-
(put-text-property beg end 'gptel 'response))
360+
(add-text-properties
361+
beg end '(gptel response front-sticky (gptel))))
361362
(read bounds)))
362363
(pcase-let ((`(,system ,backend ,model ,temperature ,tokens ,num)
363364
(gptel-org--entry-properties (point-min))))

gptel.el

+9-7
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,8 @@ Valid JSON unless NO-JSON is t."
10981098
(gptel-org--restore-state))
10991099
(when gptel--bounds
11001100
(mapc (pcase-lambda (`(,beg . ,end))
1101-
(put-text-property beg end 'gptel 'response))
1101+
(add-text-properties
1102+
beg end '(gptel response front-sticky (gptel))))
11021103
gptel--bounds)
11031104
(message "gptel chat restored."))
11041105
(when gptel--backend-name
@@ -1591,8 +1592,8 @@ See `gptel--url-get-response' for details."
15911592
(when-let* ((transformer (plist-get info :transformer)))
15921593
(setq response (funcall transformer response)))
15931594
(save-excursion
1594-
(put-text-property
1595-
0 (length response) 'gptel 'response response)
1595+
(add-text-properties
1596+
0 (length response) '(gptel response front-sticky (gptel)) response)
15961597
(with-current-buffer (marker-buffer start-marker)
15971598
(goto-char start-marker)
15981599
(run-hooks 'gptel-pre-response-hook)
@@ -1971,10 +1972,11 @@ against if required."
19711972
(letrec ((gptel--attach-after
19721973
(lambda (b e)
19731974
(when (and b e)
1974-
(put-text-property
1975-
b e 'gptel-history
1976-
(append (ensure-list history)
1977-
(get-char-property (1- e) 'gptel-history))))
1975+
(add-text-properties
1976+
b e `(gptel-history
1977+
,(append (ensure-list history)
1978+
(get-char-property (1- e) 'gptel-history))
1979+
front-sticky (gptel gptel-history))))
19781980
(remove-hook 'gptel-post-response-functions
19791981
gptel--attach-after 'local))))
19801982
(add-hook 'gptel-post-response-functions gptel--attach-after

0 commit comments

Comments
 (0)