-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathydcv.el
419 lines (367 loc) · 12.6 KB
/
ydcv.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
;;; ydcv.el --- Interface for ydcv
;; Filename: ydcv.el
;; Description: Interface for ydcv (YouDao console version).
;; Author: Linjun Zheng <zhenglj89@gmail.com>
;; Maintainer: Linjun Zheng <zhenglj89@gmail.com>
;; Copyright (C) 2015, Linjun Zheng, all rights reserved.
;; Created: 2015-06-19
;; Version: 1.0
;; Keywords: startdict, ydcv
;;
;; Features that might be required by this library:
;;
;; `outline' `cl'
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Interface for ydcv.py (YouDao console version).
;;
;; Translate word by ydcv (console version of Stardict), and display
;; translation use showtip or buffer.
;;
;; Below are commands you can use:
;;
;; `ydcv-search-pointer'
;; Search around word and display with buffer.
;; `ydcv-search-pointer+'
;; Search around word and display with `showtip'.
;; `ydcv-search-input'
;; Search input word and display with buffer.
;; `ydcv-search-input+'
;; Search input word and display with `showtip'.
;;
;; Tips:
;;
;; If current mark is active, ydcv commands will translate
;; region string, otherwise translate word around point.
;;
;;; Installation:
;;
;; To use this extension, you have to install Stardict and ydcv
;; If you use Debian, it's simply, just:
;;
;; sudo aptitude install stardict ydcv -y
;;
;; And make sure have install `popup.el',
;; this extension depend it.
;; You can install get it from:
;; http://www.emacswiki.org/cgi-bin/emacs/popup.el
;;
;; Put ydcv.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'ydcv)
;;
;;; Customize:
;;
;; `ydcv-buffer-name'
;; The name of ydcv buffer.
;;
;; `ydcv-dictionary-simple-list'
;; The dictionary list for simple describe.
;;
;; `ydcv-dictionary-complete-list'
;; The dictionary list for complete describe.
;;
;; All of the above can customize by:
;; M-x customize-group RET ydcv RET
;;
;;; Change log:
;;
;;
;;; Acknowledgements:
;;
;; zhenglj1989@gmail.com (Linjun zheng) for ydcv-mode.el
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'outline)
(eval-when-compile
(require 'cl))
(require 'popup)
;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Customize ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgroup ydcv nil
"Interface for ydcv (StartDict console version)."
:group 'edit)
(defcustom ydcv-buffer-name "*YDCV*"
"The name of the buffer of ydcv."
:type 'string
:group 'ydcv)
(defcustom ydcv-py-directory ""
"The name of the buffer of ydcv."
:type 'string
:group 'ydcv)
(defcustom ydcv-dictionary-simple-list ""
"The list of the ydcv simple dictionary."
:type 'string
:group 'ydcv)
(defcustom ydcv-dictionary-complete-list ""
"The list of the ydcv complete dictionary."
:type 'string
:group 'ydcv)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Variable ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar ydcv-previous-window-configuration nil
"Window configuration before switching to ydcv buffer.")
(defvar ydcv-current-translate-object nil
"The search object.")
(defvar ydcv-filter-string "^对不起,没有发现和.*\n"
"The filter string that ydcv output.")
(defvar ydcv-fail-notify-string "没有发现解释\n用更多的词典查询试试"
"This string is for notify user when search fail.")
(defvar ydcv-mode-font-lock-keywords ;keyword for buffer display
'(
;; Dictionary name
("^-->\\(.*\\)\n-" . (1 font-lock-type-face))
;; Search word
("^-->\\(.*\\)[ \t\n]*" . (1 font-lock-function-name-face))
;; Serial number
("\\(^[0-9] \\|[0-9]+:\\|[0-9]+\\.\\)" . (1 font-lock-constant-face))
;; Type name
("^<<\\([^>]*\\)>>$" . (1 font-lock-comment-face))
;; Phonetic symbol
("^\\/\\([^>]*\\)\\/$" . (1 font-lock-string-face))
("^\\[\\([^]]*\\)\\]$" . (1 font-lock-string-face))
)
"Expressions to highlight in `ydcv-mode'.")
(defvar ydcv-mode-map ;key map
(let ((map (make-sparse-keymap)))
;; Ydcv command.
(define-key map "q" 'ydcv-quit)
(define-key map "j" 'ydcv-next-line)
(define-key map "k" 'ydcv-prev-line)
(define-key map "J" 'ydcv-scroll-up-one-line)
(define-key map "K" 'ydcv-scroll-down-one-line)
(define-key map "d" 'ydcv-next-dictionary)
(define-key map "f" 'ydcv-previous-dictionary)
(define-key map "i" 'ydcv-search-input)
(define-key map ";" 'ydcv-search-input+)
(define-key map "p" 'ydcv-search-pointer)
(define-key map "y" 'ydcv-search-pointer+)
;; Isearch.
(define-key map "S" 'isearch-forward-regexp)
(define-key map "R" 'isearch-backward-regexp)
(define-key map "s" 'isearch-forward)
(define-key map "r" 'isearch-backward)
;; Hideshow.
(define-key map "a" 'show-all)
(define-key map "A" 'hide-body)
(define-key map "v" 'show-entry)
(define-key map "V" 'hide-entry)
;; Misc.
(define-key map "e" 'scroll-down)
(define-key map " " 'scroll-up)
(define-key map "l" 'forward-char)
(define-key map "h" 'backward-char)
(define-key map "?" 'describe-mode)
map)
"Keymap for `ydcv-mode'.")
(define-derived-mode ydcv-mode nil "ydcv"
"Major mode to look up word through ydcv.
\\{ydcv-mode-map}
Turning on Text mode runs the normal hook `ydcv-mode-hook'."
(setq font-lock-defaults '(ydcv-mode-font-lock-keywords))
(setq buffer-read-only t)
(set (make-local-variable 'outline-regexp) "^-->.*\n-->"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Interactive Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ydcv-search-pointer (&optional word)
"Get current WORD.
And display complete translations in other buffer."
(interactive)
;; Display details translate result.
(ydcv-search-detail (or word (ydcv-region-or-word)) ydcv-dictionary-complete-list))
(defun ydcv-search-pointer+ ()
"Translate current point word.
And show information use tooltip.
But this function use a simple dictionary list."
(interactive)
;; Display simple translate result.
(ydcv-search-simple))
(defun ydcv-search-input (&optional word)
"Translate current input WORD.
And show information in other buffer."
(interactive)
;; Display details translate result.
(ydcv-search-detail (or word (ydcv-prompt-input)) ydcv-dictionary-complete-list))
(defun ydcv-search-input+ (&optional word)
"Translate current point WORD.
And show information use tooltip."
(interactive)
;; Display simple translate result.
(ydcv-search-simple (or word (ydcv-prompt-input)))
;; I set this delay for fast finger.
(sit-for 0.5))
(defun ydcv-quit ()
"Bury ydcv buffer and restore the previous window configuration."
(interactive)
(if (window-configuration-p ydcv-previous-window-configuration)
(progn
(set-window-configuration ydcv-previous-window-configuration)
(setq ydcv-previous-window-configuration nil)
(bury-buffer (ydcv-get-buffer)))
(bury-buffer)))
(defun ydcv-next-dictionary ()
"Jump to next dictionary."
(interactive)
(show-all)
(if (search-forward-regexp "^-->.*\n-" nil t) ;don't show error when search failed
(progn
(call-interactively 'previous-line)
(recenter 0))
(message "Have reach last dictionary.")))
(defun ydcv-previous-dictionary ()
"Jump to previous dictionary."
(interactive)
(show-all)
(if (search-backward-regexp "^-->.*\n-" nil t) ;don't show error when search failed
(progn
(forward-char 1)
(recenter 0)) ;adjust position
(message "Have reach first dictionary.")))
(defun ydcv-scroll-up-one-line ()
"Scroll up one line."
(interactive)
(scroll-up 1))
(defun ydcv-scroll-down-one-line ()
"Scroll down one line."
(interactive)
(scroll-down 1))
(defun ydcv-next-line (arg)
"Next ARG line and show item."
(interactive "P")
(ignore-errors
(call-interactively 'next-line arg)
(save-excursion
(beginning-of-line nil)
(when (looking-at outline-regexp)
(show-entry)))))
(defun ydcv-prev-line (arg)
"Previous ARG line."
(interactive "P")
(ignore-errors
(call-interactively 'previous-line arg)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Utilities Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ydcv-search-detail (word dictionary-list)
"Search WORD through the `command-line' tool \"ydcv.py\" with DICTIONARY-LIST.
The result will be displayed in buffer named with
`ydcv-buffer-name' with `ydcv-mode'."
(message "Search...")
;; Get translate object.
(or word (setq word (ydcv-region-or-word)))
;; Record current translate object.
(setq ydcv-current-translate-object word)
(with-current-buffer (get-buffer-create ydcv-buffer-name)
(ydcv-goto-ydcv)
(setq buffer-read-only nil)
(erase-buffer)
;; Return translate result.
(let (cmd)
(setq cmd (format "%sydcv.py %s \"%s\""
ydcv-py-directory
(mapconcat (lambda (dict)
(concat "-u " dict))
dictionary-list " ")
word))
(insert (ydcv-filter (shell-command-to-string cmd))))
(unless (eq (current-buffer) (ydcv-get-buffer))
(switch-to-buffer-other-window (ydcv-get-buffer)))
(ydcv-mode-reinit)
))
(defun ydcv-search-args (word dict-list)
"Search arguments WORD and DICT-LIST."
(append (apply 'append (mapcar (lambda (d) `("-u" ,d)) dict-list))
(list "-n" word)))
(defun ydcv-search-simple (&optional word)
"Search WORD simple translate result."
(popup-tip
(ydcv-search-with-dictionary word ydcv-dictionary-simple-list)))
(defun ydcv-search-with-dictionary (word dictionary-list)
"Search some WORD with dictionary list.
Argument DICTIONARY-LIST the word that need transform."
;; Get translate object.
(or word (setq word (ydcv-region-or-word)))
;; Record current translate object.
(setq ydcv-current-translate-object word)
;; Return translate result.
(let (cmd)
(setq cmd (format "%sydcv.py %s \"%s\""
ydcv-py-directory
(mapconcat (lambda (dict)
(concat "-u " dict))
dictionary-list " ")
word))
(ydcv-filter (shell-command-to-string cmd)))
)
(defun ydcv-filter (ydcv-string)
"This function is for filter ydcv output string,.
Argument YDCV-STRING the search string from ydcv."
(setq ydcv-string (replace-regexp-in-string ydcv-filter-string "" ydcv-string))
(if (equal ydcv-string "")
ydcv-fail-notify-string
(with-temp-buffer
(insert ydcv-string)
(goto-char (point-min))
;; (kill-line 1) ;remove unnecessary information.
(buffer-string))))
(defun ydcv-goto-ydcv ()
"Switch to ydcv buffer in other window."
(setq ydcv-previous-window-configuration (current-window-configuration))
(let* ((buffer (ydcv-get-buffer))
(window (get-buffer-window buffer)))
(if (null window)
(switch-to-buffer-other-window buffer)
(select-window window))))
(defun ydcv-get-buffer ()
"Get the ydcv buffer. Create one if there's none."
(let ((buffer (get-buffer-create ydcv-buffer-name)))
(with-current-buffer buffer
(unless (eq major-mode 'ydcv-mode)
(ydcv-mode)))
buffer))
(defun ydcv-mode-reinit ()
"Re-initialize buffer.
Hide all entry but the first one and goto
the beginning of the buffer."
(ignore-errors
(setq buffer-read-only t)
(goto-char (point-min))
(ydcv-next-dictionary)
(show-all)
(message "Have search finished with `%s'." ydcv-current-translate-object)))
(defun ydcv-prompt-input ()
"Prompt input object for translate."
(read-string (format "Word (%s): " (or (ydcv-region-or-word) ""))
nil nil
(ydcv-region-or-word)))
(defun ydcv-region-or-word ()
"Return region or word around point.
If `mark-active' on, return region string.
Otherwise return word around point."
(if mark-active
(buffer-substring-no-properties (region-beginning)
(region-end))
(thing-at-point 'word)))
(provide 'ydcv)
;;; ydcv.el ends here