feat(ai): send quick explanation to *gptel* buffer instead of message

This commit is contained in:
2026-03-23 15:44:02 +08:00
parent 53fb673dae
commit 72726d6108
+19 -5
View File
@@ -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."