|
39 | 39 | :group 'comm |
40 | 40 | :link '(url-link :tag "Repository" "https://github.com/emacs-openai/codegpt")) |
41 | 41 |
|
| 42 | +(defcustom codegpt-focus-p t |
| 43 | + "If this value is `nil`, do not move focus to output buffer." |
| 44 | + :type 'boolean |
| 45 | + :group 'codegpt) |
| 46 | + |
| 47 | +(defconst codegpt-buffer-name "*CodeGPT*" |
| 48 | + "Buffer name to do completion task.") |
| 49 | + |
42 | 50 | ;; |
43 | 51 | ;;; Application |
44 | 52 |
|
| 53 | +(defmacro codegpt--ask-in-buffer (instruction &rest body) |
| 54 | + "Insert INSTRUCTION then execute BODY form." |
| 55 | + `(progn |
| 56 | + (openai--pop-to-buffer codegpt-buffer-name) ; create it |
| 57 | + (openai--with-buffer codegpt-buffer-name |
| 58 | + (erase-buffer) |
| 59 | + (insert ,instruction "\n\n") |
| 60 | + ,@body))) |
| 61 | + |
45 | 62 | (defun codegpt--internal (instruction start end) |
46 | 63 | "Do INSTRUCTION with partial code. |
47 | 64 |
|
48 | 65 | The partial code is defined in with the region, and the START nad END are |
49 | 66 | boundaries of that region in buffer." |
50 | | - (let ((text (string-trim (buffer-substring start end)))) |
51 | | - (openai-completon--ask-in-buffer |
| 67 | + (let ((text (string-trim (buffer-substring start end))) |
| 68 | + (original-window (selected-window))) |
| 69 | + (codegpt--ask-in-buffer |
52 | 70 | instruction |
53 | 71 | (insert text "\n\n") |
54 | 72 | (openai-completion |
55 | 73 | (buffer-string) |
56 | 74 | (lambda (data) |
57 | | - (openai--with-buffer openai-completion-buffer-name |
58 | | - (openai--pop-to-buffer openai-completion-buffer-name) |
| 75 | + (openai--with-buffer codegpt-buffer-name |
| 76 | + (openai--pop-to-buffer codegpt-buffer-name) |
59 | 77 | (let* ((choices (openai-completion--data-choices data)) |
60 | 78 | (result (openai-completion--get-choice choices)) |
61 | 79 | (original-point (point))) |
62 | 80 | (insert (string-trim result) "\n") |
63 | | - (fill-region original-point (point))))))))) |
| 81 | + (fill-region original-point (point)))) |
| 82 | + (unless codegpt-focus-p |
| 83 | + (select-window original-window)))) |
| 84 | + (unless codegpt-focus-p |
| 85 | + (select-window original-window))))) |
64 | 86 |
|
65 | 87 | ;;;###autoload |
66 | 88 | (defun codegpt-doc (start end) |
|
0 commit comments