-
Notifications
You must be signed in to change notification settings - Fork 8
/
eim-extra.el
210 lines (189 loc) · 7.62 KB
/
eim-extra.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
;;; -*- coding: utf-8 -*-
;;; eim-extra.el --- provide extra function for chinese input method
;; Copyright 2006 Ye Wenbin
;;
;; Author: [email protected]
;; Version: $Id: eim-extra.el,v 1.2 2007/01/14 01:51:51 ywb Exp $
;; Keywords:
;; 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:
;;
;; Put this file into your load-path and the following into your ~/.emacs:
;; (require 'eim-extra)
;;;_* Code:
(provide 'eim-extra)
(eval-when-compile
(require 'cl))
(require 'eim)
(defvar eim-punc-escape-list
(number-sequence ?0 ?9)
"Punctuation will not insert after this characters.
If you don't like this funciton, set the variable to nil")
(defvar eim-insert-ascii-char (cons ?\; ";")
"*Key used for `eim-insert-ascii'.")
(defvar eim-punc-translate-p t
"*Non-nil means will translate punctuation.")
;;;_. handle punctuation
(defun eim-read-punctuation (package)
(let ((eim-current-package package)
buf punc-list punc)
(setq buf (cdr (assoc "buffer" (car (eim-buffer-list)))))
(save-excursion
(set-buffer buf)
(save-restriction
(widen)
(let ((region (eim-section-region "Punctuation")))
(goto-char (car region))
(while (< (point) (cdr region))
(setq punc (eim-line-content))
(if (> (length punc) 3)
(error "标点不支持多个转换!"))
(add-to-list 'punc-list punc)
(forward-line 1)))))
punc-list))
(defun eim-punc-translate (punc-list char)
(if eim-punc-translate-p
(cond ((< char ? ) "")
((and eim-insert-ascii-char
(= char (car eim-insert-ascii-char)))
(char-to-string char))
(t (let ((str (char-to-string char))
punc)
(if (and (not (member (char-before) eim-punc-escape-list))
(setq punc (cdr (assoc str punc-list))))
(progn
(if (= char (char-before))
(delete-char -1))
(if (= (safe-length punc) 1)
(car punc)
(setcdr (cdr punc) (not (cddr punc)))
(if (cddr punc)
(car punc)
(nth 1 punc))))
str))))
(char-to-string char)))
(defun eim-punc-translate-toggle (arg)
(interactive "P")
(setq eim-punc-translate-p
(if (null arg)
(not eim-punc-translate-p)
(> (prefix-numeric-value arg) 0))))
;;;_. 一个快速插入英文的命令。按自己的需要绑定到 ";"
(defun eim-insert-ascii ()
(interactive)
(if current-input-method
(let (c)
(message (format "自定义输入(直接空格%s, 回车%c): "
(cdr eim-insert-ascii-char)
(car eim-insert-ascii-char)))
(setq c (read-event))
(cond ((= c ? ) (insert (cdr eim-insert-ascii-char)))
((= c ?\r) (insert-char (car eim-insert-ascii-char) 1))
(t
(setq unread-command-events (list last-input-event))
(insert (read-from-minibuffer "自定义输入: ")))))
(call-interactively 'self-insert-command)))
;;;_. load and save history
(defun eim-load-history (history-file package)
(let* ((eim-current-package package)
(history (eim-history))
item)
(when (file-exists-p history-file)
(with-current-buffer (find-file-noselect history-file)
(goto-char (point-min))
(while (not (eobp))
(if (and (setq item (eim-line-content))
(= (length item) 2))
(puthash (car item)
`(nil ("pos" . ,(string-to-number (cadr item))))
history))
(forward-line 1))
(kill-buffer (current-buffer))))))
(defun eim-save-history (history-file package)
(interactive)
(let* ((eim-current-package package)
(history (eim-history)))
(with-temp-buffer
(erase-buffer)
(let (pos)
(maphash (lambda (key val)
(unless (or (eim-string-emptyp key)
(= (setq pos (cdr (assoc "pos" (cdr val)))) 1))
(insert key " " (number-to-string pos) "\n")))
history))
(write-file history-file))))
;;;_. 增加两个快速选择的按键
(defun eim-quick-select-1 ()
"如果没有可选项,插入数字,否则选择对应的词条"
(interactive)
(if (car eim-current-choices)
(let ((index (eim-page-start))
(end (eim-page-end)))
(if (>= index end)
(eim-append-string (eim-translate last-command-event))
(eim-remember-select (1+ index))
(setq eim-current-str (eim-choice (nth index (car eim-current-choices))))))
(eim-append-string (eim-translate last-command-event)))
(eim-terminate-translation))
(defun eim-quick-select-2 ()
"如果没有可选项,插入数字,否则选择对应的词条"
(interactive)
(if (car eim-current-choices)
(let ((index (1+ (eim-page-start)))
(end (eim-page-end)))
(if (>= index end)
(eim-append-string (eim-translate last-command-event))
(eim-remember-select (1+ index))
(setq eim-current-str (eim-choice (nth index (car eim-current-choices))))))
(eim-append-string (eim-translate last-command-event)))
(eim-terminate-translation))
(defun eim-describe-char (pos package)
(interactive
(list (point)
(if (eq input-method-function 'eim-input-method)
(eim-package-name)
(let (eim-current-package)
(setq eim-current-package
(if (= (length eim-package-list) 1)
(cdar eim-package-list)
(assoc
(completing-read "In package: "
eim-package-list nil t
(caar eim-package-list))
eim-package-list)))
(eim-package-name)))))
(if (>= pos (point-max))
(error "No character follows specified position"))
(let ((char (char-after pos))
(func (intern-soft (format "%s-get-char-code" package)))
code)
(when func
(setq code (funcall func char))
(if code
(message "Type %S to input %c for input method %s"
code char package)
(message "Can't find char code for %c" char)))))
;;;_. char table
(defun eim-make-char-table (chars table)
"Set `eim-char-database'"
(dolist (char chars)
(let ((code (car char)))
(dolist (c (cdr char))
(set (intern c table) code)))))
(defsubst eim-get-char-code (char table)
"Get the code of the character CHAR"
(symbol-value (intern-soft (char-to-string char) table)))
;;; eim-extra.el ends here