Skip to content

Commit

Permalink
gptel-context: Add a menu command to remove all context
Browse files Browse the repository at this point in the history
* gptel-context.el (gptel-context-remove-all): Add a function to
clear all context.  Requested in #486, #487.

* gptel-transient.el (gptel-menu, gptel--infix-track-response,
gptel--infix-context-remove-all): Add `gptel-menu' item and
keybinding ("-d") to invoke `gptel-context-remove-all'.
  • Loading branch information
karthink committed Nov 29, 2024
1 parent a4ca92e commit a844612
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions gptel-context.el
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ If selection is active, removes all contexts within selection."
(when-let ((ctx (gptel-context--at-point)))
(delete-overlay ctx)))))

(defun gptel-context-remove-all ()
"Remove all gptel context."
(cl-loop
for (source . ovs) in gptel-context--alist
if (bufferp source) do ;Buffers and buffer regions
(mapc #'gptel-context-remove ovs)
else do (gptel-context-remove source) ;files or other types
finally do (setq gptel-context--alist nil)))

(defun gptel-context--make-overlay (start end &optional advance)
"Highlight the region from START to END.
Expand Down
15 changes: 14 additions & 1 deletion gptel-transient.el
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ Also format its value in the Transient menu."
(gptel--infix-context-add-region)
(gptel--infix-context-add-buffer)
(gptel--infix-context-add-file)
(gptel--infix-context-remove-all)
(gptel--suffix-context-buffer)]]
[["Request Parameters"
:pad-keys t
Expand Down Expand Up @@ -624,7 +625,7 @@ querying the LLM."
:set-value #'gptel--set-with-scope
:display-if-true "Yes"
:display-if-false "No"
:key "-d")
:key "-v")

(transient-define-infix gptel--infix-track-media ()
"Send media from \"standalone\" links in the prompt.
Expand Down Expand Up @@ -676,6 +677,7 @@ supports. See `gptel-track-media' for more information."
(transient-setup))

(declare-function gptel-add-file "gptel-context")
(declare-function gptel-context-remove-all "gptel-context")

(transient-define-suffix gptel--infix-context-add-file ()
"Add a file to gptel's context."
Expand All @@ -686,6 +688,17 @@ supports. See `gptel-track-media' for more information."
(call-interactively #'gptel-add-file)
(transient-setup))

(transient-define-suffix gptel--infix-context-remove-all ()
"Clear gptel's context."
:if (lambda () gptel-context--alist)
:transient 'transient--do-stay
:key "-d"
:description "Remove all"
(interactive)
(when (y-or-n-p "Remove all context? ")
(gptel-context-remove-all)
(transient-setup)))

;; ** Infix for the refactor/rewrite system message

(transient-define-infix gptel--infix-add-directive ()
Expand Down

0 comments on commit a844612

Please sign in to comment.