gptel: Add post-response hook and fix org converter

* gptel.el (gptel--convert-playback-markdown->org,
gptel-post-response-hook): Add a hook that runs after the response
is received.  This will allow for custom actions like moving the
cursor to the next prompt. The markdown->org stream converter now
cleans up after itself using this hook.
This commit is contained in:
Karthik Chikmagalur 2023-04-06 16:00:29 -07:00
parent c9795fe9e8
commit c22c59d20a

View file

@ -104,6 +104,14 @@ return the transformed string."
:group 'gptel
:type 'hook)
(defcustom gptel-post-response-hook nil
"Hook run after inserting ChatGPT's response into the current buffer.
This hook is called in the buffer from which the prompt was sent
to ChatGPT. Note: this hook runs even if the request fails."
:group 'gptel
:type 'hook)
(defvar gptel-default-session "*ChatGPT*")
(defvar gptel-default-mode (if (featurep 'markdown-mode)
'markdown-mode
@ -551,10 +559,21 @@ elements."
(buffer-string)))
(defun gptel--convert-playback-markdown->org ()
""
(let ((in-src-block)
(temp-buf (generate-new-buffer-name "*gptel-temp*"))
(start-pt (make-marker)))
"Return a Markdown to Org converter.
This function parses a stream of Markdown text to Org
continuously when it is called with successive chunks of the
text stream."
(letrec ((in-src-block)
(temp-buf (generate-new-buffer-name "*gptel-temp*"))
(start-pt (make-marker))
(cleanup-fn
(lambda ()
(when (buffer-live-p (get-buffer temp-buf))
(set-marker start-pt nil)
(kill-buffer temp-buf))
(remove-hook 'gptel-post-response-hook cleanup-fn))))
(add-hook 'gptel-post-response-hook cleanup-fn)
(lambda (str)
(let ((noop-p))
(with-current-buffer (get-buffer-create temp-buf)