Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
> Released N/A

* Add support for ChatGPT (#12)
* Render result with markdown (#13)

## 0.1.0
> Released Feb 05, 2023
Expand Down
1 change: 1 addition & 0 deletions Eask
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

(depends-on "emacs" "26.1")
(depends-on "openai")
(depends-on "markdown-mode")
(depends-on "spinner")

(setq network-security-level 'low) ; see https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432
43 changes: 29 additions & 14 deletions codegpt.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/emacs-openai/codegpt
;; Version: 0.1.0
;; Package-Requires: ((emacs "26.1") (openai "0.1.0") (spinner "1.7.4"))
;; Package-Requires: ((emacs "26.1") (openai "0.1.0") (markdown-mode "2.1") (spinner "1.7.4"))
;; Keywords: convenience codegpt

;; This file is not part of GNU Emacs.
Expand Down Expand Up @@ -36,6 +36,7 @@
(require 'openai)
(require 'openai-chat)
(require 'openai-completion)
(require 'markdown-mode)
(require 'spinner)

(defgroup codegpt nil
Expand Down Expand Up @@ -138,16 +139,15 @@
;;
;;; Application

(defmacro codegpt--ask-in-buffer (instruction &rest body)
"Insert INSTRUCTION then execute BODY form."
(declare (indent 1))
`(progn
(openai--pop-to-buffer codegpt-buffer-name) ; create it
(openai--with-buffer codegpt-buffer-name
(codegpt-mode)
(erase-buffer)
(insert ,instruction "\n\n")
,@body)))
(defun codegpt--render-markdown (content)
"Render CONTENT in markdown."
(if (featurep 'markdown-mode)
(with-temp-buffer
(insert content)
(delay-mode-hooks (markdown-mode))
(ignore-errors (font-lock-ensure))
(buffer-string))
content))

(defun codegpt--fill-region (start end)
"Like function `fill-region' (START to END), improve readability."
Expand All @@ -160,6 +160,17 @@
(fill-region (line-beginning-position) (line-end-position)))
(forward-line 1))))

(defmacro codegpt--ask-in-buffer (instruction &rest body)
"Insert INSTRUCTION then execute BODY form."
(declare (indent 1))
`(progn
(openai--pop-to-buffer codegpt-buffer-name) ; create it
(openai--with-buffer codegpt-buffer-name
(codegpt-mode)
(erase-buffer)
(insert ,instruction "\n\n")
,@body)))

(defun codegpt--internal (instruction start end)
"Do INSTRUCTION with partial code.

Expand Down Expand Up @@ -187,8 +198,10 @@ boundaries of that region in buffer."
(cl-case codegpt-tunnel
(`completion
(let* ((choices (openai--data-choices data))
(result (openai--get-choice choices)))
(insert (string-trim result) "\n")))
(result (openai--get-choice choices))
(result (string-trim result))
(result (codegpt--render-markdown result)))
(insert result "\n")))
(`chat
(let ((choices (let-alist data .choices))
(result))
Expand All @@ -197,7 +210,9 @@ boundaries of that region in buffer."
(let-alist .message
(setq result (string-trim .content)))))
choices)
(insert (string-trim result) "\n"))))
(setq result (string-trim result)
result (codegpt--render-markdown result))
(insert result "\n"))))
(codegpt--fill-region original-point (point))))
(unless codegpt-focus-p
(select-window original-window)))
Expand Down