-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpt-macro.el
35 lines (30 loc) · 1.24 KB
/
gpt-macro.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(setq openai-api-key "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
(defun escape-quotes (start end)
"Escape all single and double quotes in the selected region."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (re-search-forward "[\"']" nil t)
(replace-match (format "\\%s" (match-string 0)) nil t)))))
(defun gpt-macro-call (api-key prompt data)
(setq cmd (concat "python3.9 /path-to-project/gpt-emacs-macro/gpt-macro.py \"" api-key "\" \"" prompt "\" \"" data "\""))
(shell-command-to-string cmd)
)
(defun gpt-macro()
(interactive)
(escape-quotes (region-beginning) (region-end))
(setq selection (buffer-substring (region-beginning) (region-end)))
(setq gpt-macro-input (read-string "Command: "))
(delete-region (region-beginning) (region-end))
(insert (gpt-macro-call openai-api-key gpt-macro-input selection))
)
(defun gpt-macro-noreplace()
(interactive)
(escape-quotes (region-beginning) (region-end))
(setq selection (buffer-substring (region-beginning) (region-end)))
(setq gpt-macro-input (read-string "Command: "))
(insert (gpt-macro-call openai-api-key gpt-macro-input selection))
)
(global-set-key (kbd "C-c g") 'gpt-macro)