branch: elpa/gptel
commit 2b1dbf77b1e807dc902e1b94a0df50ded0dd8f75
Author: Karthik Chikmagalur <[email protected]>
Commit: karthink <[email protected]>
gptel-context: Make overlays front/rear-advance
* gptel-context.el (gptel-context--add-region,
gptel-context--make-overlay, gptel-context-add): When adding an
overlay over a buffer, make it front and rear advancing,
i.e. include text inserted at the ends of the overlay.
* gptel-context.el (gptel-context-add): Tweak documentation.
---
gptel-context.el | 44 +++++++++++++++++++++++++++-----------------
1 file changed, 27 insertions(+), 17 deletions(-)
diff --git a/gptel-context.el b/gptel-context.el
index c794c0e534..3625022dc9 100644
--- a/gptel-context.el
+++ b/gptel-context.el
@@ -73,19 +73,25 @@ context chunk. This is accessible as, for example:
:group 'gptel
:type 'function)
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;;; ------------------------------ FUNCTIONS -------------------------------
;;;
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
(defun gptel-context-add (&optional arg)
- "Add context to GPTel.
+ "Add context to gptel in a DWIM fashion.
+
+When called without prefix argument ARG:
+
+- If a region is selected, add the selected region to the
+context.
+
+- If there is a gptel context under point, remove it.
-When called without a prefix argument, adds the current buffer as context.
-When ARG is positive, prompts for a buffer name and adds it as context.
-When ARG is negative, removes all contexts from the current buffer.
-When called with a region selected, adds the selected region as context.
+- Otherwise add the current buffer to the context.
-If there is a context under point, it is removed when called without a prefix."
+Otherwise:
+
+- When ARG is positive, prompt for a buffer name and add it to
+ the context.
+
+- When ARG is negative, removes all gptel contexts from the
+ current buffer."
(interactive "P")
(cond
;; A region is selected.
@@ -98,7 +104,8 @@ If there is a context under point, it is removed when called
without a prefix."
;; No region is selected, and ARG is positive.
((and arg (> (prefix-numeric-value arg) 0))
(let ((buffer-name (read-buffer "Choose buffer to add as context: " nil
t)))
- (gptel-context--add-region (get-buffer buffer-name) (point-min)
(point-max))
+ (gptel-context--add-region
+ (get-buffer buffer-name) (point-min) (point-max) t)
(message "Buffer '%s' added as context." buffer-name)))
;; No region is selected, and ARG is negative.
((and arg (< (prefix-numeric-value arg) 0))
@@ -119,7 +126,7 @@ If there is a context under point, it is removed when
called without a prefix."
(max
(point-min) (1- (point)))
(point))))
(message "Context under point has been removed."))
- (gptel-context--add-region (current-buffer) (point-min) (point-max))
+ (gptel-context--add-region (current-buffer) (point-min) (point-max) t)
(message "Current buffer added as context.")))))
;;;###autoload (autoload 'gptel-add "gptel-context" "Add or remove context to
gptel's requests." t)
@@ -144,9 +151,9 @@ If selection is active, removes all contexts within
selection."
(when ctx
(delete-overlay ctx))))))
-(defun gptel-context--make-overlay (start end)
+(defun gptel-context--make-overlay (start end &optional advance)
"Highlight the region from START to END."
- (let ((overlay (make-overlay start end)))
+ (let ((overlay (make-overlay start end nil (not advance) advance)))
(overlay-put overlay 'evaporate t)
(overlay-put overlay 'face 'gptel-context-highlight-face)
(overlay-put overlay 'gptel-context t)
@@ -168,13 +175,16 @@ The message is usually either a system message or user
prompt."
('nil message))
message)))
-(cl-defun gptel-context--add-region (buffer region-beginning region-end)
- "Add region delimited by REGION-BEGINNING, REGION-END in BUFFER as context."
+(cl-defun gptel-context--add-region (buffer region-beginning region-end
&optional advance)
+ "Add region delimited by REGION-BEGINNING, REGION-END in BUFFER as context.
+
+If ADVANCE is non-nil, the context overlay envelopes changes at
+the beginning and end."
;; Remove existing contexts in the same region, if any.
(mapc #'gptel-context-remove
(gptel-context--in-region buffer region-beginning region-end))
(prog1 (with-current-buffer buffer
- (gptel-context--make-overlay region-beginning region-end))
+ (gptel-context--make-overlay region-beginning region-end advance))
(message "Region added to context buffer.")))
(defun gptel-context--in-region (buffer start end)