-
Notifications
You must be signed in to change notification settings - Fork 28
/
eim-table.el
289 lines (266 loc) · 10.5 KB
/
eim-table.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
;;; eim-table.el --- Common function for table like chinese input method
;; Copyright (C) 2008 Free Software Foundation, Inc.
;;
;; Author: Ye Wenbin <[email protected]>
;; Maintainer: Ye Wenbin <[email protected]>
;; Created: 27 Apr 2008
;; Version: 0.01
;; Keywords: convenience
;; 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:
;; - punctuation-list: A symbol to translate punctuation
;; - translate-chars: The first letter which will invoke reverse
;; search the code for char
;; - max-length: max input string length
;; - char-table: a obarray to search code for char
;; - all-completion-limit: A minimal length to add all completions
;; - table-create-word-function
;;
;; - table-user-file
;; - table-history-file
;; Put this file into your load-path and the following into your ~/.emacs:
;; (require 'eim-table)
;;; Code:
(eval-when-compile
(require 'cl))
(require 'eim)
(require 'eim-extra)
(defun eim-table-translate (char)
(eim-punc-translate (symbol-value (eim-get-option 'punctuation-list))
char))
(defun eim-table-get-char-code (char)
(eim-get-char-code char (eim-get-option 'char-table)))
(defun eim-table-format (key cp tp choice)
(if (memq (aref key 0) (eim-get-option 'translate-chars))
(setq choice
(mapcar (lambda (c)
(if (consp c)
(setq c (car c)))
(cons c
(eim-table-get-char-code (aref c 0))))
choice)))
(let ((i 0))
(format "%s[%d/%d]: %s"
key cp tp
(mapconcat 'identity
(mapcar
(lambda (c)
(format "%d.%s " (setq i (1+ i))
(if (consp c)
(concat (car c) (cdr c))
c)))
choice) " "))))
;;;_. 增加补全
(defun eim-table-add-completion ()
(if (= (length eim-current-key) 1)
t
(let ((reg (concat "^" (regexp-quote eim-current-key)))
(len (length eim-current-key))
(package eim-current-package)
(key eim-current-key)
line completion)
(save-excursion
(dolist (buf (mapcar 'cdar (eim-buffer-list)))
(set-buffer buf)
(setq eim-current-package package)
(beginning-of-line)
(if (or (string= (eim-code-at-point) key)
(not (looking-at reg)))
(forward-line 1))
(while (looking-at reg)
(setq line (eim-line-content))
(mapc (lambda (c)
(when (or (>= len (eim-get-option 'all-completion-limit))
(= (length c) 1))
(push (cons c (substring
(car line)
len))
completion)))
(cdr line))
(forward-line 1))))
(setq completion (sort (delete-dups (nreverse completion))
(lambda (a b)
(< (length (cdr a)) (length (cdr b))))))
;; (message "%s, %s" eim-current-choices completion)
(setcar eim-current-choices (append (car eim-current-choices)
completion))
;; (message "%s, %s" eim-current-choices completion))
t)))
(defun eim-table-stop-function ()
(if (memq (aref eim-current-key 0) (eim-get-option 'translate-chars))
nil
(> (length eim-current-key) (eim-get-option 'max-length))))
(defun eim-table-active-function ()
(setq eim-add-completion-function 'eim-table-add-completion
eim-translate-function 'eim-table-translate
eim-format-function 'eim-table-format
eim-stop-function 'eim-table-stop-function))
;; user file and history file
;;;_. eim-wb-add-user-file
(defun eim-table-add-user-file (file)
(when file
(let* ((buflist (eim-buffer-list))
(ufile (expand-file-name file))
user-buffer)
(or (file-exists-p ufile)
(setq ufile (locate-file file load-path)))
(when (and ufile (file-exists-p ufile))
;; make sure the file not load again
(mapc (lambda (buf)
(if (string= (expand-file-name (cdr (assoc "file" buf)))
ufile)
(setq user-buffer (cdr (assoc "buffer" buf)))))
buflist)
(unless user-buffer
(setq file (eim-read-file ufile (format eim-buffer-name-format
(eim-package-name))))
(eim-make-char-table (eim-table-get-user-char (cdar file)) (eim-get-option 'char-table))
(nconc buflist (list file))
(eim-set-option 'table-user-file (cons ufile (cdar file))))))))
(defun eim-table-get-user-char (buf)
"Add user characters. Currently eim-wb may not contain all
chinese characters, so if you want more characters to input, you
can add here."
(let (line chars)
(save-excursion
(set-buffer buf)
(goto-char (point-min))
(while (not (eobp))
(setq line (eim-line-content))
(forward-line 1)
(if (and (= (length (cadr line)) 1)
(> (length (car line)) 2))
(push line chars)))
chars)))
(defun eim-table-load-history (his-file)
(when (and his-file (file-exists-p his-file))
(ignore-errors
(eim-load-history his-file eim-current-package)
(eim-set-option 'record-position t)
(eim-set-option 'table-history-file his-file))))
(defun eim-table-save-history ()
"Save history and user files."
(dolist (package eim-package-list)
(let* ((eim-current-package (cdr package))
(his-file (eim-get-option 'table-history-file))
(user-file (eim-get-option 'table-user-file)))
(when (and his-file
(file-exists-p his-file)
(file-writable-p his-file))
(eim-save-history his-file eim-current-package))
(when (and user-file
(file-exists-p (car user-file))
(file-writable-p (car user-file)))
(with-current-buffer (cdr user-file)
(save-restriction
(widen)
(write-region (point-min) (point-max) (car user-file))))))))
;; 按 TAB 显示补全
(defun eim-table-show-completion ()
(interactive)
(if (eq last-command 'eim-table-show-completion)
(ignore-errors
(with-selected-window (get-buffer-window "*Completions*")
(scroll-up)))
(if (or (= (length eim-current-key) 1) (= (aref eim-current-key 0) ?z))
nil
(while (not (eim-add-completion)))
(let ((choices (car eim-current-choices))
completion)
(dolist (c choices)
(if (listp c)
(push (list (format "%-4s %s"
(concat eim-current-key (cdr c))
(car c)))
completion)))
(with-output-to-temp-buffer "*Completions*"
(display-completion-list
(all-completions eim-current-key (nreverse completion))
eim-current-key)))))
(funcall eim-handle-function))
;; 增加新词
(defvar eim-table-minibuffer-map nil)
(defvar eim-table-save-always nil)
(when (null eim-table-minibuffer-map)
(setq eim-table-minibuffer-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map "\C-e" 'eim-table-minibuffer-forward-char)
(define-key map "\C-a" 'eim-table-minibuffer-backward-char)
map)))
;;;_. 增加新词
(defun eim-table-minibuffer-forward-char ()
(interactive)
(end-of-line)
(let ((char (save-excursion
(set-buffer buffer)
(char-after end))))
(when char
(insert char)
(incf end))))
(defun eim-table-minibuffer-backward-char ()
(interactive)
(beginning-of-line)
(let ((char (save-excursion
(set-buffer buffer)
(when (>= start (point-min))
(decf start)
(char-after start)))))
(when char
(insert char))))
(defun eim-table-add-word ()
"Create a map for word. The default word is the two characters
before cursor. You can use C-a and C-e to add character at the
begining or end of the word.
默认新词为光标前的两个字,通过两个按键延长这个词:
C-e 在头部加入一个字
C-a 在尾部加入一个字
"
(interactive)
(let* ((buffer (current-buffer))
(end (point))
(start (- (point) 2))
(word (buffer-substring-no-properties
start end))
(user-file (eim-get-option 'table-user-file))
(func (eim-get-option 'table-create-word-function))
choice code words)
(when func
(setq word (read-from-minibuffer "加入新词: " word
eim-table-minibuffer-map)
code (funcall func word))
(setq choice (eim-get code))
(unless (member word (car choice))
(if (buffer-live-p (cdr user-file))
(save-excursion
(set-buffer (cdr user-file))
(if (string-match "^\\s-$" (buffer-string))
(insert "\n" code " " word)
(eim-bisearch-word code (point-min) (point-max))
(let ((words (eim-line-content)))
(goto-char (line-end-position))
(if (string= (car words) code)
(insert " " word)
(insert "\n" code " " word))))
(setcar choice (append (car choice) (list word)))
(if eim-table-save-always
(save-restriction
(widen)
(write-region (point-min) (point-max) (car user-file)))))
(error "the user buffer is closed!")))))
(message nil))
(add-hook 'kill-emacs-hook 'eim-table-save-history)
(provide 'eim-table)
;;; eim-table.el ends here