Skip to content

Commit abbaa8e

Browse files
committed
Add focus flag
1 parent 528950d commit abbaa8e

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

codegpt.el

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,50 @@
3939
:group 'comm
4040
:link '(url-link :tag "Repository" "https://github.com/emacs-openai/codegpt"))
4141

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+
4250
;;
4351
;;; Application
4452

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+
4562
(defun codegpt--internal (instruction start end)
4663
"Do INSTRUCTION with partial code.
4764
4865
The partial code is defined in with the region, and the START nad END are
4966
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
5270
instruction
5371
(insert text "\n\n")
5472
(openai-completion
5573
(buffer-string)
5674
(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)
5977
(let* ((choices (openai-completion--data-choices data))
6078
(result (openai-completion--get-choice choices))
6179
(original-point (point)))
6280
(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)))))
6486

6587
;;;###autoload
6688
(defun codegpt-doc (start end)

0 commit comments

Comments
 (0)