From 72726d6108933eabd0948c07fc860fb7f470064c Mon Sep 17 00:00:00 2001 From: Wong Ding Feng Date: Mon, 23 Mar 2026 15:44:02 +0800 Subject: [PATCH] feat(ai): send quick explanation to *gptel* buffer instead of message --- ai.el | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/ai.el b/ai.el index e724372..14ee4c9 100644 --- a/ai.el +++ b/ai.el @@ -156,14 +156,28 @@ ;;; ============================================================================ (defun my/gptel-quick-explain-region () - "Quickly explain selected code region." + "Quickly explain selected code region in the gptel buffer." (interactive) (when (use-region-p) - (let ((code (buffer-substring-no-properties (region-beginning) (region-end)))) - (gptel-request code - :system "Explain this code concisely in 2-3 sentences." + (let* ((code (buffer-substring-no-properties (region-beginning) (region-end))) + (buf (get-buffer-create "*gptel*"))) + ;; Prepare the buffer and insert the query immediately + (with-current-buffer buf + (goto-char (point-max)) + (unless (derived-mode-p 'gptel-mode) + (funcall gptel-default-mode) + (gptel-mode 1)) + (insert "\n\n--- Code to Explain ---\n\n" code "\n\n### Explanation Request Sent...\n")) + (display-buffer buf) + ;; Send the request + (gptel-request + (format "Explain this code concisely in 2-3 sentences:\n\n%s" code) + :system "You are an expert programmer explaining code." :callback (lambda (response info) - (message "Explanation: %s" response)))))) + (with-current-buffer (get-buffer "*gptel*") + (goto-char (point-max)) + (insert "\n### Code Explanation\n\n" response "\n\n")) + (message "Explanation received in *gptel* buffer.")))))) (defun my/gptel-proofread-region () "Proofread and improve selected text."