-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdcv-mode.el
227 lines (207 loc) · 7.93 KB
/
sdcv-mode.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
;;; sdcv-mode.el --- major mode to do dictionary query through sdcv
;; Copyright 2006 pluskid
;;
;; Author: pluskid@gmail.com
;; Version: $Id: sdcv-mode.el,v 0.0 2006/12/21 11:20:51 kid Exp $
;; Keywords: sdcv dictionary
;; X-URL: not distributed yet
;; 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 2, 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, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; This is a major mode to view output of dictionary search of sdcv.
;; Put this file into your load-path and the following into your
;; ~/.emacs:
;; (require 'sdcv-mode)
;; (global-set-key (kbd "C-c d") 'sdcv-search)
;;; Code:
(require 'outline)
(provide 'sdcv-mode)
(eval-when-compile
(require 'cl))
;;; ==================================================================
;;; Frontend, search word and display sdcv buffer
(defun sdcv-search (force-all-dictionaries)
"Prompt for a word to search through sdcv.
When provided with a prefix argument, use all the dictionaries
no matter what `sdcv-dictionary-list' is."
(interactive "P")
(let ((word (if (and transient-mark-mode mark-active)
(buffer-substring-no-properties (region-beginning)
(region-end))
(sdcv-current-word)))
;; note that elisp is dynamic-scoped
(sdcv-dictionary-list (if force-all-dictionaries
nil
sdcv-dictionary-list)))
(setq word (read-string
(format "Search the dictionary for (default %s): "
word)
nil nil word))
(sdcv-search-word word)))
(defun sdcv-search-word (word)
"Search WORD through the command-line tool sdcv.
The result will be displayed in buffer named with
`sdcv-buffer-name' with `sdcv-mode'."
(with-current-buffer (get-buffer-create sdcv-buffer-name)
(setq buffer-read-only nil)
(erase-buffer)
(let ((process (start-process
"sdcv"
sdcv-buffer-name
"sdcv"
(sdcv-generate-dictionary-argument)
"-n"
(shell-quote-argument word))))
(set-process-sentinel
process
(lambda (process signal)
(when (memq (process-status process) '(exit signal))
(unless (eq (current-buffer) (sdcv-get-buffer))
(sdcv-goto-sdcv))
(sdcv-mode-reinit)))))))
(defun sdcv-generate-dictionary-argument ()
"Generate dictionary argument for sdcv from `sdcv-dictionary-list'."
(if (null sdcv-dictionary-list)
""
(mapconcat (lambda (dict)
(concat "-u "
(shell-quote-argument dict)))
sdcv-dictionary-list
" ")))
;;; ==================================================================
;;; utilities to switch from and to sdcv buffer
(defun sdcv-current-word ()
"Get the current word under the cursor."
(if (or (< emacs-major-version 21)
(and (= emacs-major-version 21)
(< emacs-minor-version 4)))
(sdcv-current-word-1)
;; We have a powerful `current-word' function since 21.4
(current-word nil t)))
(defun sdcv-current-word-1 ()
(save-excursion
(backward-word 1)
(mark-word 1)
(buffer-substring-no-properties (region-beginning)
(region-end))))
(defvar sdcv-previous-window-conf nil
"Window configuration before switching to sdcv buffer.")
(defun sdcv-goto-sdcv ()
"Switch to sdcv buffer in other window."
(interactive)
(setq sdcv-previous-window-conf (current-window-configuration))
(let* ((buffer (sdcv-get-buffer))
(window (get-buffer-window buffer)))
(if (null window)
(switch-to-buffer-other-window buffer)
(select-window window))))
(defun sdcv-return-from-sdcv ()
"Bury sdcv buffer and restore the previous window configuration."
(interactive)
(if (window-configuration-p sdcv-previous-window-conf)
(progn
(set-window-configuration sdcv-previous-window-conf)
(setq sdcv-previous-window-conf nil)
(bury-buffer (sdcv-get-buffer)))
(bury-buffer)))
(defun sdcv-get-buffer ()
"Get the sdcv buffer. Create one if there's none."
(let ((buffer (get-buffer-create sdcv-buffer-name)))
(with-current-buffer buffer
(unless (eq major-mode 'sdcv-mode)
(sdcv-mode)))
buffer))
;;; ==================================================================
;;; The very major mode
(defvar sdcv-mode-font-lock-keywords
'(
;; dictionary name
("^-->\\(.*\\)$" . (1 font-lock-type-face))
;; property of word
("^<<\\([^>]*\\)>>$" . (1 font-lock-comment-face))
;; phonetic symbol
("^\\[\\([^]]*\\)\\]$" . (1 font-lock-string-face))
)
"Expressions to hilight in `sdcv-mode'")
(defvar sdcv-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "q" 'sdcv-return-from-sdcv)
(define-key map "s" 'isearch-forward-regexp)
(define-key map "r" 'isearch-backward-regexp)
(define-key map (kbd "C-s") 'isearch-forward)
(define-key map (kbd "C-r") 'isearch-backward)
(define-key map (kbd "RET") 'sdcv-mode-scroll-up-one-line)
(define-key map (kbd "M-RET") 'sdcv-mode-scroll-down-one-line)
(define-key map "v" 'scroll-up)
(define-key map (kbd "M-v") 'scroll-down)
(define-key map " " 'scroll-up)
(define-key map (kbd "DEL") 'scroll-down)
(define-key map (kbd "C-n") 'sdcv-mode-next-line)
(define-key map "n" 'sdcv-mode-next-line)
(define-key map (kbd "C-p") 'sdcv-mode-previous-line)
(define-key map "p" 'sdcv-mode-previous-line)
(define-key map "d" 'sdcv-search)
(define-key map "?" 'describe-mode)
(define-key map "a" 'show-all)
(define-key map "h" 'hide-body)
(define-key map "e" 'show-entry)
(define-key map "c" 'hide-entry)
map)
"Keymap for `sdcv-mode'.")
(define-derived-mode sdcv-mode nil "sdcv"
"Major mode to look up word through sdcv.
\\{sdcv-mode-map}
Turning on Text mode runs the normal hook `sdcv-mode-hook'."
(setq font-lock-defaults '(sdcv-mode-font-lock-keywords))
(setq buffer-read-only t)
(set (make-local-variable 'outline-regexp) "^-->.*\n-->"))
(defun sdcv-mode-reinit ()
"Re-initialize buffer.
Hide all entrys but the first one and goto
the beginning of the buffer."
(ignore-errors
(setq buffer-read-only t)
(hide-body)
(goto-char (point-min))
(next-line 1)
(show-entry)))
(defun sdcv-mode-scroll-up-one-line ()
(interactive)
(scroll-up 1))
(defun sdcv-mode-scroll-down-one-line ()
(interactive)
(scroll-down 1))
(defun sdcv-mode-next-line ()
(interactive)
(ignore-errors
(next-line 1)
(save-excursion
(beginning-of-line nil)
(when (looking-at outline-regexp)
(show-entry)))))
;; I decide not to fold the definition entry when
;; doing previous-line. So `sdcv-mode-previous-line'
;; is only an alias of `previous-line'.
(defalias 'sdcv-mode-previous-line 'previous-line)
;;;;##################################################################
;;;; User Options, Variables
;;;;##################################################################
(defvar sdcv-buffer-name "*sdcv*"
"The name of the buffer of sdcv.")
(defvar sdcv-dictionary-list nil
"A list of dictionaries to use.
Each entry is a string denoting the name of a dictionary, which
is then passed to sdcv through the '-u' command line option. If
this list is nil then all the dictionaries will be used.")
;;; sdcv-mode.el ends here