-
Notifications
You must be signed in to change notification settings - Fork 7
/
fanyi-base.el
222 lines (188 loc) · 7.54 KB
/
fanyi-base.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
;;; fanyi-base.el --- The base class of all dictionaries -*- lexical-binding: t -*-
;; Copyright (C) 2021, 2022 Zhiwei Chen
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Author: Zhiwei Chen <[email protected]>
;; Keywords: convenience, tools
;; URL: https://github.com/condy0919/fanyi.el
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1"))
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; The base class of all dictionaries.
;;; Code:
(require 'url)
(require 'eieio)
;; Silence compile warning.
(defvar url-http-end-of-headers)
(defcustom fanyi-sound-player
(or (executable-find "mpv")
(executable-find "mplayer")
(executable-find "mpg123"))
"Program to play sound."
:type 'string
:group 'fanyi)
(defcustom fanyi-sound-player-support-https nil
"Does `fanyi-sound-player' support https?
If non-nil, url will be passed to `fanyi-sound-player' directly.
Otherwise, `url-retrieve' first, the data will then be sent to
`fanyi-sound-player' process through a pipe."
:type 'boolean
:group 'fanyi)
(defcustom fanyi-use-glyphs t
"Non-nil means use glyphs when available."
:type 'boolean
:group 'fanyi)
(defcustom fanyi-verbose nil
"Whether to make `fanyi-dwim' verbose."
:type 'boolean
:group 'fanyi)
(defcustom fanyi-log nil
"Whether to enable log."
:type 'boolean
:group 'fanyi)
(defface fanyi-female-speaker-face
'((t :foreground "#ec5e66"))
"Face used for female speaker button."
:group 'fanyi)
(defface fanyi-male-speaker-face
'((t :foreground "#57a7b7"))
"Face used for male speaker button."
:group 'fanyi)
(defun fanyi-display-glyphs-p ()
"Can we use glyphs instead of plain text?"
(and fanyi-use-glyphs (display-images-p)))
(defun fanyi--sound-player-options (player)
"Get the necessary options for PLAYER."
(pcase player
((pred (string-match-p "mplayer"))
'("-cache" "1024"))
(_ nil)))
(defun fanyi-play-sound (url)
"Play URL via external program.
See `fanyi-sound-player'."
(unless fanyi-sound-player
(user-error "Set `fanyi-sound-player' first"))
(when (string-empty-p url)
(user-error "No voice found"))
(if fanyi-sound-player-support-https
(start-process fanyi-sound-player nil fanyi-sound-player url)
;; Some programs, e.g. mpg123, can't play https files. So we download then
;; play them via `fanyi-sound-player'.
;;
;; "-" stands for standard input, which is an UNIX convention.
;;
;; mplayer needs an additional option, or exits with an error:
;;
;; Cannot seek backward in linear streams!
(url-retrieve url (lambda (status)
(cl-block nil
;; Something went wrong, but we don't care.
(when (or (not status) (plist-member status :error))
(cl-return))
;; Move point to the real http content. Plus 1 for '\n'.
(goto-char (1+ url-http-end-of-headers))
(let ((proc (make-process :name "fanyi-player-process"
:buffer nil
:command `(,fanyi-sound-player "-" ,@(fanyi--sound-player-options fanyi-sound-player))
:noquery t
:connection-type 'pipe)))
(process-send-region proc (point) (point-max))
(when (process-live-p proc)
(process-send-eof proc)))))
nil
t
t)))
(defconst fanyi-buffer-name "*fanyi*"
"The default name of translation buffer.")
(defconst fanyi-log-buffer-name "*fanyi-log*"
"The default name of fanyi log buffer.")
(defvar fanyi-log-buffer-mtx (make-mutex)
"The mutex for \"*fanyi-log*\" buffer.")
(defvar fanyi-buffer-mtx (make-mutex)
"The mutex for \"*fanyi*\" buffer.")
(defvar fanyi-mode-font-lock-keywords-extra nil
"Dicts can define their own font-lock keywords.")
(defclass fanyi-base-service ()
((word :initarg :word
:type string
:protection :protected
:documentation "The query word.")
(url :initarg :url
:type string
:protection :protected
:documentation "Dictionary translation url.")
(sound-url :initarg :sound-url
:type string
:protection :protected
:documentation "Dictionary sound url.")
(method :initarg :method
:initform "GET"
:type string
:protection :protected
:documentation "HTTP method. Default to GET.")
(headers :initarg :headers
:initform nil
:type list
:protection :protected
:documentation "Extra HTTP headers.")
(body :initarg :body
:initform nil
:protection :protected
:documentation "HTTP body.")
(api-type :initarg :api-type
:type symbol
:protection :protected
:documentation "API type. Currently it could be either 'xml or 'json."))
"The base class of dictionary service."
:abstract t)
;; Silence unknown slots warning.
(eieio-declare-slots :word :url :sound-url :method :headers :body :api-type)
(cl-defmethod fanyi-set-query-word ((this fanyi-base-service) query)
"Set QUERY word to THIS.
QUERY could be a sentence for some services."
(oset this word (url-hexify-string query)))
(cl-defmethod fanyi-parse-from ((this fanyi-base-service) _dom)
"Implement your own `fanyi-parse-from' for THIS class."
(error "Implement `fanyi-parse-from' for class '%s'" (eieio-object-class-name this)))
(cl-defmethod fanyi-render ((this fanyi-base-service))
"Implement your own `fanyi-render' for THIS class."
(error "Implement `fanyi-render' for class '%s'" (eieio-object-class-name this)))
(defmacro fanyi-with-fanyi-buffer (&rest body)
"Evaluate BODY with `*fanyi*' buffer temporarily current."
(declare (indent 0))
`(with-current-buffer (get-buffer-create ,fanyi-buffer-name)
(save-excursion
;; Go to the end of buffer.
(goto-char (point-max))
(let ((inhibit-read-only t))
,@body))))
(defun fanyi-log (fmt &rest args)
"Collect the messages into *fanyi-log* with non-nil `fanyi-log'.
The FMT and ARGS will be passed to `format-message'."
(when fanyi-log
(let ((timestamp (format-time-string "[%F %T]"))
(text (format-message fmt args)))
(with-mutex fanyi-log-buffer-mtx
(with-current-buffer (get-buffer-create fanyi-log-buffer-name)
(insert timestamp " " text "\n"))))))
(defun fanyi-user-error (fmt &rest args)
"Signal a user error by passing FMT and ARGS to `user-error'.
If `fanyi-verbose' is nil (default), message won't be displayed.
If `fanyi-log' is non-nil, the message will also be collected in
\"*fanyi-log*\" buffer."
(fanyi-log fmt args)
(if fanyi-verbose
(user-error fmt args)
(signal 'user-error nil)))
(provide 'fanyi-base)
;;; fanyi-base.el ends here