-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpaw-gptel.el
79 lines (73 loc) · 3.43 KB
/
paw-gptel.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
;;; paw-gptel.el -*- lexical-binding: t; -*-
(require 'paw-vars)
(require 'gptel)
(require 'paw-db)
(defun paw-gptel-update-note (id word type &optional callback)
(gptel-request
(pcase type
('word (format "You are an English Expert, you carefully research the word, give me the English explanations/sentences from Famous Dictionaries, and the related Chinese explanation: %s, return your answer in Emacs Org mode format, heading starts from **, no need to tell me 'Sure'"
word))
(_ (format "Explain: %s directly, return your answer in Emacs Org Mode format, heading starts from **, no need to tell me 'Sure'"
word )))
:callback
(lambda (response info)
(if (not response)
(message "gptel-quick failed with message: %s" (plist-get info :status))
(paw-update-note id response)
(message "%s" response))
(if callback
(funcall callback)))))
(defun paw-gptel-update-exp (id word type &optional callback)
(gptel-request
(pcase type
('word (format "You are an English Expert, you carefully research the word, give me the English explanations/sentences from Famous Dictionaries, and the related Chinese explanation: %s, return your answer in Emacs Org mode format, heading starts from **, no need to tell me 'Sure'"
word))
(_ (format "Explain: %s directly, return your answer in Emacs Org Mode format, heading starts from **, no need to tell me 'Sure'"
word )))
:callback
(lambda (response info)
(if (not response)
(message "gptel-quick failed with message: %s" (plist-get info :status))
(paw-update-exp id response)
(message "%s" response))
(if callback
(funcall callback)))))
(defun paw-gptel-translate (word &optional prompt callback buffer section)
(let ((buffer (or buffer (current-buffer)))) ;; the button is pressed on current-buffer
(message "%s" prompt)
(gptel-request prompt
:callback
(lambda (response info)
(if (not response)
(message "paw-gptel-translate failed with message: %s" (plist-get info :status))
;; (message "%s" response)
)
(if callback
(funcall callback)
(with-current-buffer buffer
(save-excursion
(when-let* ((translation response))
(setq buffer-read-only nil)
;; (message translation)
(unless (string-match-p section (org-no-properties (org-get-heading t t t t)))
(goto-char (point-min))
(search-forward (format "** %s" section) nil t))
(org-end-of-subtree t t)
;; (forward-line)
;; (delete-region (region-beginning) (region-end))
(let ((bg-color paw-view-note-background-color))
(paw-insert-and-make-overlay
translation
'face `(:background ,bg-color :extend t))
(insert "\n"))
(if (or paw-ask-ai-p paw-ai-translate-p paw-ai-translate-context-p)
(insert "\n"))
(goto-char (point-min))
(search-forward "** Dictionaries" nil t)
(beginning-of-line)
(setq buffer-read-only t)
;; (message "Translation completed")
;; (message "Translation completed %s" translation)
) )
(deactivate-mark))))) ))
(provide 'paw-gptel)