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."