-
Notifications
You must be signed in to change notification settings - Fork 4
/
paw-go-translate.el
183 lines (165 loc) · 8.11 KB
/
paw-go-translate.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
;;; paw-go-translate.el -*- lexical-binding: t; -*-
(require 'paw-vars)
(require 'go-translate)
(require 'dash)
(defcustom paw-go-translate-langs '(en zh ja)
"The languages to translate. If `paw-detect-language-p' is t, then will
detect the language first, and append it to
`paw-go-translate-langs' to translate."
:type 'list
:group 'paw)
(define-obsolete-variable-alias 'paw-go-transalte-langs
'paw-go-translate-langs "paw 1.1.1")
(defclass paw-gt-translate-render (gt-render)
((buffer-name :initarg :buffer-name :initform nil)
(section :initarg :section :initform nil))
:documentation "Used to save the translate result into BUFFER.")
;; override the original gt-init, to remove the Processing message
(cl-defmethod gt-init :around ((render gt-render) translator)
(gt-log-funcall "init (%s %s)" render translator)
(condition-case err
(progn (cl-call-next-method render translator)
(gt-update-state translator))
(error (gt-log 'render (format "%s initialize failed, abort" (eieio-object-class render)))
(user-error (format "[output init error] %s" err)))))
(cl-defmethod gt-output ((render paw-gt-translate-render) translator)
(deactivate-mark)
(when (= (oref translator state) 3)
(let* ((ret (gt-extract render translator))
(buffer (get-buffer (oref render buffer-name)))
(section (oref render section)))
(if-let (err (cl-find-if (lambda (r) (<= (plist-get r :state) 1)) ret))
(setq paw-go-translate-running-p nil)
;; (error "%s" (plist-get err :result))
;; (error "Translation Error")
(if (buffer-live-p buffer)
(with-current-buffer buffer
(save-excursion
(let* ((buffer-read-only nil)
(translation (mapconcat (lambda (r) (string-join (plist-get r :result) "\n")) ret "\n\n")))
(unless (string-equal section (org-no-properties (org-get-heading t t t t)))
(goto-char (point-min))
(search-forward (format "** %s" section) nil t))
(org-mark-subtree)
(goto-char (mark-marker))
;; (forward-line)
;; (delete-region (region-beginning) (region-end))
(paw-insert-and-make-overlay (concat translation "\n" ) 'face 'org-block)
(goto-char (point-min))
(search-forward "** Dictionaries" nil t)
(beginning-of-line)
;; (message "Translation completed")
;; (message "Translation completed %s" translation)
) )
(deactivate-mark))))))
(setq paw-go-translate-running-p nil))
(defun paw-translate()
(interactive)
(gt-do-translate))
(defun paw-immersive-translate()
"HACK: Override the original immersive-translate functions.
Because the original functions don't work well on many cases,
especially on nov-mode and org-mode. So hack them here, it may
not need if immersive-translate improve in the future."
(interactive)
;; put advice here, if user don't call this function, it will not override the original functions
(advice-add #'immersive-translate--get-paragraph :override 'paw-immersive-translate--get-paragraph)
(advice-add #'immersive-translate-end-of-paragraph :override 'paw-immersive-translate-end-of-paragraph)
(advice-add #'immersive-translate-region :override 'paw-immersive-translate-region)
(if immersive-translate--translation-overlays
(immersive-translate-clear)
(immersive-translate-buffer)))
(defun paw-immersive-translate--get-paragraph ()
"TODO Return the paragraph or line at point."
(pcase major-mode
('Info-mode
(immersive-translate--info-get-paragraph))
('helpful-mode
(immersive-translate--helpful-get-paragraph))
((pred immersive-translate--elfeed-tube-p)
(immersive-translate--elfeed-tube-get-paragraph))
((or 'elfeed-show-mode 'mu4e-view-mode)
(immersive-translate--elfeed-get-paragraph))
(_
(let ((paragraph (thing-at-point 'paragraph t)))
;; HACK for org-media-note
(replace-regexp-in-string "[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9]"
""
(replace-regexp-in-string "\\[\\[.*?\\]\\[\\(.*?\\)\\]\\]"
"\\1"
(if (save-excursion (re-search-forward "[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9]" nil t) )
(thing-at-point 'line t)
(if paragraph
paragraph
(thing-at-point 'line t)))))))))
(defun paw-immersive-translate-end-of-paragraph ()
"TODO: Move to the end of the current paragraph or line."
(pcase major-mode
((and (or 'elfeed-show-mode 'mu4e-view-mode)
(pred (not immersive-translate--elfeed-tube-p)))
(unless (get-text-property (point) 'immersive-translate--beg)
(text-property-search-backward 'immersive-translate--beg))
(text-property-search-forward 'immersive-translate--end)
(end-of-line))
;; HACK for org-media-note
(_ (if (save-excursion (re-search-forward "[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9]" nil t))
(end-of-line)
(if (thing-at-point 'paragraph t)
(end-of-paragraph-text)
(end-of-line) )))))
(defun paw-immersive-translate-region (start end)
"TODO Translate the text between START and END."
(save-excursion
(goto-char start)
(pcase major-mode
((and (or 'elfeed-show-mode 'mu4e-view-mode)
(pred (not immersive-translate--elfeed-tube-p)))
(while (and (text-property-search-forward 'immersive-translate--end)
(< (point) end))
(immersive-translate-paragraph)))
(_ (while (and
(< (point) end)
;; (re-search-forward "^\\s-*$" end 'noerror)
(not (eobp)))
(forward-line)
(immersive-translate-paragraph))))))
(defun paw-nov-translate()
(interactive)
(gt-start
(gt-translator
:taker (gt-taker :langs '(en zh) :text
(lambda()
(cond ((use-region-p)
(replace-regexp-in-string "[ \n]+" " " (replace-regexp-in-string "^[ \n]+" "" (buffer-substring-no-properties (region-beginning) (region-end)))))
(t (current-word t t)))))
:engines (list (gt-bing-engine)
(gt-google-engine)
(gt-google-rpc-engine)
(gt-youdao-dict-engine)
(gt-youdao-suggest-engine))
:render (gt-buffer-render)) ))
(defun paw-go-translate-detect-language-convert(lang)
"TODO Convert the detected langauge to go-translate recognized language."
(pcase lang
("zh-Hant" "zh")
(_ lang)))
(defun paw-go-translate-insert(&optional word lang buffer section)
"Translate the WORD and insert the result into BUFFER on SECTION.
if `paw-detect-language-p' is t, then will detect the language of WORD
first, and append it to `paw-go-translate-langs' to translate."
(interactive)
(setq paw-go-translate-running-p t)
(let* ((detected-lang (paw-go-translate-detect-language-convert (if lang lang (paw-check-language word))))
(langs (-union `(,(intern detected-lang)) paw-go-translate-langs)))
(gt-start
(gt-translator
:taker (gt-taker :langs langs :text
(lambda()
(let ((word (if word (replace-regexp-in-string "[ \n]+" " " (replace-regexp-in-string "^[ \n]+" "" word)) nil)))
(cond ((use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end)))
(t (if word word (current-word t t)))))))
:engines (list (gt-bing-engine))
:render (paw-gt-translate-render :buffer-name (buffer-name buffer)
:section (or section "Translation")))) ))
(provide 'paw-go-translate)