-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexhub-chat.el
384 lines (338 loc) · 14.3 KB
/
exhub-chat.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
;;; exhub-chat.el --- Exhub Chat in Emacs -*- lexical-binding: t -*-
;; Copyright (C) 2023 EdmondFrank
;; Author: Edmond Frank <[email protected]>
;; Version: 0.1
;; Package-Requires: ((emacs "26.1") (posframe "0.9"))
;; Keywords: convenience, translation
;; URL: https://github.com/edmondfrank/exhub
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides translation functionality for Emacs using Exhub.
;;; Code:
(require 'cl-lib)
(require 'json)
(require 'map)
(require 'seq)
(require 'subr-x)
(require 'markdown-mode)
(require 'exhub)
(defgroup exhub-chat nil
"Exhub Chat group."
:group 'applications)
(defcustom exhub-chat-drafts (list)
"The drafts that Gemini returned."
:type 'listp
:group 'exhub-chat)
(defcustom exhub-chat-draft--begin 0
"Where the draft begins."
:type 'numberp
:group 'exhub-chat)
(defcustom exhub-chat-draft--end 0
"Where the draft ends."
:type 'numberp
:group 'exhub-chat)
(defcustom exhub-chat-buffer-name nil
"The buffer name of the chat buffer.
If nil, it uses the current buffer."
:type 'string
:group 'exhub-chat)
(defvar exhub-chat-lang (or (ignore-errors (car (split-string (getenv "LANG") "\\.")))
(car (split-string current-language-environment "-"))))
(defun exhub-chat-output-lang ()
(pcase exhub-chat-lang
("zh_CN" "中文")
("Chinese" "中文")
(_ "English")))
(add-to-list 'auto-mode-alist '("\\.exhub-chat$" . markdown-mode))
(defun exhub-chat--get-chat-buffer ()
"Get the chat buffer."
(if exhub-chat-buffer-name
(get-buffer-create exhub-chat-buffer-name)
(current-buffer)))
(defun exhub-chat-with-message (prompt)
(let ((buffer (exhub-chat--get-chat-buffer)))
(with-current-buffer buffer
(save-excursion
(goto-char (point-max))
(insert "## User:\n")
(insert (format "%s\n" prompt)))
(message "[Exhub-Chat] Please wait for Exhub ...")
(exhub-call "exhub-chat"
prompt
(or exhub-chat-buffer-name
(buffer-name))))))
(defun exhub-chat-finish-answer (buffer)
(save-excursion
(switch-to-buffer buffer)
(goto-char (point-max))
(insert "\n\n")
(message "[Exhub-Chat] Exhub finished replying.")))
(defun exhub-chat-response (serial-number content buffer)
(if (equal serial-number 1)
(progn
(setq exhub-chat-drafts (list))
(push content exhub-chat-drafts)
(with-current-buffer buffer
(save-excursion
(goto-char (point-max))
(insert "\n### Exhub-Chat:\n")
(setq exhub-chat-draft--begin (point-max))
(insert content)
(setq exhub-chat-draft--end (point-max)))))
(push content exhub-chat-drafts)))
(define-derived-mode exhub-chat-edit-mode text-mode "exhub-chat/edit"
"The major mode to edit focus text input.")
(setq exhub-chat-edit-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'exhub-chat-edit-mode-confirm)
(define-key map (kbd "C-c C-k") #'exhub-chat-edit-mode-cancel)
map))
(defun exhub-chat ()
(interactive)
(let ((prompt (read-string "Chat with Exhub: ")))
(if (string-empty-p (string-trim prompt))
(message "Please do not enter an empty prompt.")
(exhub-chat-with-message prompt))))
(defun exhub-chat-with-multiline ()
(interactive)
(let* ((bufname (buffer-name))
(edit-buffer (generate-new-buffer (format "*exhub-chat-edit-buffer-%s*" bufname))))
(split-window-below -12)
(other-window 1)
(with-current-buffer edit-buffer
(exhub-chat-edit-mode)
(set (make-local-variable 'exhub-chat-edit-buffer-name) bufname))
(switch-to-buffer edit-buffer)
(exhub-chat--edit-set-header-line)))
(defun exhub-chat--edit-set-header-line ()
"Set header line."
(setq header-line-format
(substitute-command-keys
(concat
" Exhub-Chat Edit Mode: "
"Confirm with C-c C-c, "
"Cancel with C-c C-k. "
))))
(defun exhub-chat-get-buffer-string ()
(buffer-substring-no-properties (point-min) (point-max)))
(defun exhub-chat-edit-mode-cancel ()
(interactive)
(kill-buffer)
(delete-window)
(message "[Exhub-Chat] Edit cancelled!"))
(defun exhub-chat-edit-mode-confirm ()
(interactive)
(let* ((bufname exhub-chat-edit-buffer-name)
(prompt (exhub-chat-get-buffer-string)))
(kill-buffer)
(delete-window)
(switch-to-buffer bufname)
(exhub-chat-with-message prompt)))
(defun exhub-chat-return-code (serial-number content buffer begin end)
(let* ((block-start (string-match "```" content))
(code-begin (when block-start
(+ (string-match "\n" content (+ block-start 3)) 1)))
(code-end (when code-begin
(string-match "```" content code-begin)))
(code (when (and code-begin code-end)
(substring content code-begin code-end))))
(if (and code (equal serial-number 1))
(progn
(setq exhub-chat-drafts (list))
(push code exhub-chat-drafts)
(with-current-buffer buffer
(delete-region begin end)
(goto-char begin)
(setq exhub-chat-draft--begin begin)
(insert code)
(setq exhub-chat-draft--end (+ exhub-chat-draft--begin (length code)))))
(when code
(push code exhub-chat-drafts)))))
(defun exhub-chat-generate-code ()
(interactive)
(let* ((selection (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))))
(mode (replace-regexp-in-string "\\(-ts\\)?-mode$" "" (symbol-name major-mode)))
(prompt (if (= (length selection) 0)
(format "%s, please only output the code, without any explanations or instructions." (read-string "Prompt: "))
(format "%s, please only output the code, without any explanations or instructions." (concat mode " " selection)))))
(exhub-call "exhub-chat"
prompt
(buffer-name)
""
"Generating..."
"Generate code done."
(point)
(point)
"exhub-chat-return-code")))
(defun exhub-chat-adjust-code ()
(interactive)
(let* ((selection (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))))
(mode (replace-regexp-in-string "\\(-ts\\)?-mode$" "" (symbol-name major-mode)))
(prompt (format "%s the %s code below, please only output the code, without any explanations or instructions."
(read-string "Adjust: ") mode)))
(exhub-call "exhub-chat"
prompt
(buffer-name)
selection
"Adgjusting..."
"Adjust code done."
(region-beginning)
(region-end)
"exhub-chat-return-code")))
(defun exhub-chat-polish-document ()
(interactive)
(let* ((document (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))
(string-trim (buffer-substring-no-properties (point-min) (point-max)))))
(buffer (generate-new-buffer (format "*exhub-chat-doc-buffer*"))))
(split-window-right)
(other-window 1)
(switch-to-buffer buffer)
(markdown-mode)
(delete-region (point-min) (point-max))
(exhub-call "exhub-chat"
"Please help me proofread and polish the following text:\n"
(buffer-name)
document
"Polishing..."
"Polish document done.")))
(defun exhub-chat-explain-code ()
(interactive)
(let* ((code (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))
(string-trim (buffer-substring-no-properties (point-min) (point-max)))))
(mode (replace-regexp-in-string "\\(-ts\\)?-mode$" "" (symbol-name major-mode)))
(buffer (generate-new-buffer (format "*exhub-chat-explain-buffer*"))))
(split-window-right)
(other-window 1)
(switch-to-buffer buffer)
(markdown-mode)
(delete-region (point-min) (point-max))
(exhub-call "exhub-chat"
(format "Please explain in detail the meaning of the following %s code, in %s, leave a blank line between each sentence:\n" mode (exhub-chat-output-lang))
(buffer-name)
code
"Explaining..."
"Explain code done.")))
(defun exhub-chat-comment-code ()
(interactive)
(let* ((code (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))
(string-trim (buffer-substring-no-properties (point-min) (point-max)))))
(begin (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(mode (replace-regexp-in-string "\\(-ts\\)?-mode$" "" (symbol-name major-mode))))
(exhub-call "exhub-chat"
(format "Please add code comments to the following %s code, with the comments written in %s within the code, and output the code including the comments." mode (exhub-chat-output-lang))
(buffer-name)
code
"Commenting..."
"Comment code done."
begin
end
"exhub-chat-return-code")))
(defun exhub-chat-format-code ()
(interactive)
(let* ((code (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))
(string-trim (buffer-substring-no-properties (point-min) (point-max)))))
(begin (if (region-active-p)
(region-beginning)
(point-min)))
(end (if (region-active-p)
(region-end)
(point-max)))
(mode (replace-regexp-in-string "\\(-ts\\)?-mode$" "" (symbol-name major-mode))))
(exhub-call "exhub-chat"
(format "Please format the following %s code and output the code with pretty format and indent based on the indentation of the first line." mode)
(buffer-name)
code
"Formatting..."
"Format code done."
begin
end
"exhub-chat-return-code")))
(defun exhub-chat-refactory-code ()
(interactive)
(let* ((code (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))
(string-trim (buffer-substring-no-properties (point-min) (point-max)))))
(mode (replace-regexp-in-string "\\(-ts\\)?-mode$" "" (symbol-name major-mode)))
(buffer (generate-new-buffer (format "*exhub-chat-refactory-buffer*"))))
(split-window-right)
(other-window 1)
(switch-to-buffer buffer)
(markdown-mode)
(delete-region (point-min) (point-max))
(exhub-call "exhub-chat"
(format "Please help me refactor the following %s code, in %s. Please reply with the refactoring explanation, refactored code, and diff between two versions. Please ignore the comments and strings in the code during the refactoring. If the code remains unchanged after refactoring, please say 'No need to refactor'." mode (exhub-chat-output-lang))
(buffer-name)
code
"Refactorying..."
"Refactory code done.")))
(defun exhub-chat-generate-commit-message ()
(interactive)
(exhub-call "exhub-chat"
"generate-git-commit-message"
(file-truename (file-name-directory buffer-file-name))
(buffer-name)
(point)
(point)))
(defun exhub-chat-translate-into-chinese ()
(interactive)
(let* ((buffer (generate-new-buffer (format "*exhub-chat-translate-buffer*")))
(content (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))
(string-trim (buffer-substring-no-properties (point-min) (point-max))))))
(split-window-right)
(other-window 1)
(switch-to-buffer buffer)
(markdown-mode)
(delete-region (point-min) (point-max))
(exhub-call "exhub-chat"
(format "请把下面的文段翻译成中文:\n%s" content)
(buffer-name)
""
"Translating..."
"Translate text done.")))
(defun exhub-chat-translate-into-english ()
(interactive)
(let* ((buffer (generate-new-buffer (format "*exhub-chat-translate-buffer*")))
(content (if (region-active-p)
(string-trim (buffer-substring-no-properties (region-beginning) (region-end)))
(string-trim (buffer-substring-no-properties (point-min) (point-max))))))
(split-window-right)
(other-window 1)
(switch-to-buffer buffer)
(markdown-mode)
(delete-region (point-min) (point-max))
(exhub-call "exhub-chat"
(format "Please translate the following passage into English:\n%s" content)
(buffer-name)
""
"Translating..."
"Translate text done.")))
(defun exhub-chat-choose-drafts (draft)
(interactive (list (completing-read "Choose Draft: " exhub-chat-drafts nil t)))
(delete-region exhub-chat-draft--begin exhub-chat-draft--end)
(save-excursion
(goto-char exhub-chat-draft--begin)
(insert draft))
(setq exhub-chat-draft--end (+ exhub-chat-draft--begin (length draft))))
(provide 'exhub-chat)
;;; exhub-chat.el ends here