66; ; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
77; ; URL: https://github.com/emacs-openai/codegpt
88; ; Version: 0.1.0
9- ; ; Package-Requires: ((emacs "26.1") (openai "0.1.0") (spinner "1.7.4"))
9+ ; ; Package-Requires: ((emacs "26.1") (openai "0.1.0") (markdown-mode "2.1") ( spinner "1.7.4"))
1010; ; Keywords: convenience codegpt
1111
1212; ; This file is not part of GNU Emacs.
3636(require 'openai )
3737(require 'openai-chat )
3838(require 'openai-completion )
39+ (require 'markdown-mode )
3940(require 'spinner )
4041
4142(defgroup codegpt nil
138139; ;
139140; ;; Application
140141
141- (defmacro codegpt--ask-in-buffer (instruction &rest body )
142- " Insert INSTRUCTION then execute BODY form."
143- (declare (indent 1 ))
144- `(progn
145- (openai--pop-to-buffer codegpt-buffer-name) ; create it
146- (openai--with-buffer codegpt-buffer-name
147- (codegpt-mode)
148- (erase-buffer )
149- (insert , instruction " \n\n " )
150- ,@body )))
142+ (defun codegpt--render-markdown (content )
143+ " Render CONTENT in markdown."
144+ (if (featurep 'markdown-mode )
145+ (with-temp-buffer
146+ (insert content)
147+ (delay-mode-hooks (markdown-mode ))
148+ (ignore-errors (font-lock-ensure ))
149+ (buffer-string ))
150+ content))
151151
152152(defun codegpt--fill-region (start end )
153153 " Like function `fill-region' (START to END), improve readability."
160160 (fill-region (line-beginning-position ) (line-end-position )))
161161 (forward-line 1 ))))
162162
163+ (defmacro codegpt--ask-in-buffer (instruction &rest body )
164+ " Insert INSTRUCTION then execute BODY form."
165+ (declare (indent 1 ))
166+ `(progn
167+ (openai--pop-to-buffer codegpt-buffer-name) ; create it
168+ (openai--with-buffer codegpt-buffer-name
169+ (codegpt-mode)
170+ (erase-buffer )
171+ (insert , instruction " \n\n " )
172+ ,@body )))
173+
163174(defun codegpt--internal (instruction start end )
164175 " Do INSTRUCTION with partial code.
165176
@@ -187,8 +198,10 @@ boundaries of that region in buffer."
187198 (cl-case codegpt-tunnel
188199 (`completion
189200 (let* ((choices (openai--data-choices data))
190- (result (openai--get-choice choices)))
191- (insert (string-trim result) " \n " )))
201+ (result (openai--get-choice choices))
202+ (result (string-trim result))
203+ (result (codegpt--render-markdown result)))
204+ (insert result " \n " )))
192205 (`chat
193206 (let ((choices (let-alist data .choices))
194207 (result))
@@ -197,7 +210,9 @@ boundaries of that region in buffer."
197210 (let-alist .message
198211 (setq result (string-trim .content)))))
199212 choices)
200- (insert (string-trim result) " \n " ))))
213+ (setq result (string-trim result)
214+ result (codegpt--render-markdown result))
215+ (insert result " \n " ))))
201216 (codegpt--fill-region original-point (point ))))
202217 (unless codegpt-focus-p
203218 (select-window original-window)))
0 commit comments