-
Notifications
You must be signed in to change notification settings - Fork 0
/
transtini.el
65 lines (55 loc) · 1.87 KB
/
transtini.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
;; Author: Reion Wong <[email protected]>
;; Maintainer: Reion Wong <[email protected]>
;; Created: 2023-02-19
;; Last-Updated: 2022/02/19 18:52:00
;; Keywords: translation
;; Compatibility: GNU Emacs 28
(require 'posframe)
(require 'epc)
(defgroup transtini nil
"Translator for Emacs."
:group 'edit)
(defcustom transtini-buffer-name "*TRANSTINI*"
"Buffer name"
:type 'string
:group 'transtini)
(defface transtini-posframe-face
'((t (:foreground "#e7eeff" :background "#7ca4ff")))
"Face for youdao-dictionary tooltip"
:group 'transtini)
(defvar transtini-epc-server-py
(expand-file-name "transtini.py"
(file-name-directory
(or load-file-name buffer-file-name))))
(defvar transtini-epc (epc:start-epc (or (getenv "PYTHON") "python3")
(list transtini-epc-server-py)))
(defun transtini-region-or-word()
"Returns the selected text"
(if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word t)))
(defun transtini-posframe-show(text)
(posframe-show transtini-buffer-name
:string text
:timeout 0
:tab-line-height 0
:header-line-height 0
:background-color (face-attribute 'transtini-posframe-face :background)
:foreground-color (face-attribute 'transtini-posframe-face :foreground)
:internal-border-width 20
:max-width 60
:min-width 30
:position (point))
(unwind-protect
(push (read-event " ") unread-command-events)
(posframe-delete transtini-buffer-name)))
(defun transtini-at-point+()
(interactive)
(let ((word (transtini-region-or-word)))
(if word
(deferred:$
(epc:call-deferred transtini-epc 'youdao_query (list word))
(deferred:nextc it
(lambda (x) (transtini-posframe-show x))))
(message "No content"))))
(provide 'transtini)