Mercurial > emacs
annotate lisp/international/kkc.el @ 19869:3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
map which only has default key binding to
quail-execute-non-quail-command.
(kkc-non-kkc-command): Handle the key sequence as the same way as
universal-argument-other-key.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Thu, 11 Sep 1997 11:52:38 +0000 |
parents | 0933bf4b82c9 |
children | a17cd573df1c |
rev | line source |
---|---|
17315
a3ca5e15c82a
Fix the format of the first line.
Kenichi Handa <handa@m17n.org>
parents:
17082
diff
changeset
|
1 ;;; kkc.el --- Kana Kanji converter |
17052 | 2 |
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. | |
18377
8b4a66c66dd6
Change copyright notice.
Richard M. Stallman <rms@gnu.org>
parents:
18198
diff
changeset
|
4 ;; Licensed to the Free Software Foundation. |
17052 | 5 |
6 ;; Keywords: mule, multilingual, Japanese, SKK | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
17071 | 21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
17052 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; These routines provide a simple and easy-to-use converter from | |
28 ;; Kana-string to Kana-Kanji-mixed-string. This converter (here after | |
29 ;; KKC) uses a SKK dictionary to get information how to convert | |
30 ;; Kana-string. Since KKC can't be fully automated, we need an | |
31 ;; interaction with a user to decide the correct conversion. For | |
32 ;; that, we provide KKC major mode. | |
33 | |
34 ;;; Code: | |
35 | |
36 (require 'skkdic-utl) | |
37 | |
38 (defvar kkc-input-method-title "漢" | |
39 "String denoting KKC input method. | |
40 This string is shown at mode line when users are in KKC mode.") | |
41 | |
42 (defvar kkc-init-file-name "~/.kkcrc" | |
43 "Name of a file which contains user's initial setup code for KKC.") | |
44 | |
45 ;; A flag to control a file specified by `kkc-init-file-name'. | |
46 ;; The value nil means the file is not yet consulted. | |
47 ;; The value t means the file has already been consulted but there's | |
48 ;; no need of updating it yet. | |
49 ;; Any other value means that we must update the file before exiting Emacs. | |
50 (defvar kkc-init-file-flag nil) | |
51 | |
52 ;; Cash data for `kkc-lookup-key'. This may be initialized by loading | |
53 ;; a file specified by `kkc-init-file-name'. If any elements are | |
54 ;; modified, the data is written out to the file when exiting Emacs. | |
55 (defvar kkc-lookup-cache '(kkc-lookup-cache)) | |
56 | |
57 (defun kkc-save-init-file () | |
58 "Save initial setup code for KKC to a file specified by `kkc-init-file-name'" | |
59 (if (and kkc-init-file-flag | |
60 (not (eq kkc-init-file-flag t))) | |
18198
8286b2dd4db6
(kkc-save-init-file): Coding system name changed from
Kenichi Handa <handa@m17n.org>
parents:
17315
diff
changeset
|
61 (let ((coding-system-for-write 'iso-2022-7bit)) |
17052 | 62 (write-region (format "(setq kkc-lookup-cache '%S)\n" kkc-lookup-cache) |
63 nil | |
64 kkc-init-file-name)))) | |
65 | |
66 ;; Sequence of characters to be used for indexes for shown list. The | |
67 ;; Nth character is for the Nth conversion in the list currently shown. | |
68 (defvar kkc-show-conversion-list-index-chars | |
69 "1234567890abcdefghijklmnopqrsuvwxyz") | |
70 | |
71 (defvar kkc-mode-map | |
72 (let ((map (make-keymap)) | |
73 (i 0)) | |
74 (while (< i ? ) | |
75 (define-key map (char-to-string i) 'undefined) | |
76 (setq i (1+ i))) | |
77 (while (< i 128) | |
78 (define-key map (char-to-string i) 'kkc-non-kkc-command) | |
79 (setq i (1+ i))) | |
80 (setq i 0) | |
81 (let ((len (length kkc-show-conversion-list-index-chars))) | |
82 (while (< i len) | |
83 (define-key map | |
84 (char-to-string (aref kkc-show-conversion-list-index-chars i)) | |
85 'kkc-select-from-list) | |
86 (setq i (1+ i)))) | |
87 (define-key map " " 'kkc-next) | |
88 (define-key map (char-to-string help-char) 'help-command) | |
89 (define-key map "\r" 'kkc-terminate) | |
90 (define-key map "\C-@" 'kkc-first-char-only) | |
91 (define-key map "\C-n" 'kkc-next) | |
92 (define-key map "\C-p" 'kkc-prev) | |
93 (define-key map "\C-i" 'kkc-shorter) | |
94 (define-key map "\C-o" 'kkc-longer) | |
95 (define-key map "\C-c" 'kkc-cancel) | |
96 (define-key map "\C-?" 'kkc-cancel) | |
97 (define-key map "\C-f" 'kkc-next-phrase) | |
98 (define-key map "K" 'kkc-katakana) | |
99 (define-key map "H" 'kkc-hiragana) | |
100 (define-key map "l" 'kkc-show-conversion-list-or-next-group) | |
101 (define-key map "L" 'kkc-show-conversion-list-or-prev-group) | |
102 (define-key map [?\C-\ ] 'kkc-first-char-only) | |
103 (define-key map [delete] 'kkc-cancel) | |
104 (define-key map [return] 'kkc-terminate) | |
19869
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
105 (let ((meta-map (make-sparse-keymap))) |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
106 (define-key map (char-to-string meta-prefix-char) meta-map) |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
107 (define-key map [escape] meta-map)) |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
108 (define-key map (vector meta-prefix-char t) 'kkc-non-kkc-command) |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
109 ;; At last, define default key binding. |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
110 (define-key map [t] 'kkc-non-kkc-command) |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
111 map) |
17052 | 112 "Keymap for KKC (Kana Kanji Conversion) mode.") |
113 | |
114 (defun kkc-mode () | |
115 "Major mode for converting Kana string to Kanji-Kana mixed string. | |
116 Commands: | |
117 \\{kkc-mode-map}" | |
118 (setq major-mode 'kkc-mode) | |
119 (setq mode-name "KKC") | |
120 (use-local-map kkc-mode-map) | |
121 (run-hooks 'kkc-mode-hook)) | |
122 | |
123 ;;; Internal variables used in KKC. | |
124 | |
125 ;; The current Kana string to be converted. | |
126 (defvar kkc-original-kana nil) | |
127 | |
128 ;; The current key sequence (vector of Kana characters) generated from | |
129 ;; `kkc-original-kana'. | |
130 (defvar kkc-current-key nil) | |
131 | |
132 ;; List of the current conversions for `kkc-current-key'. | |
133 (defvar kkc-current-conversions nil) | |
134 | |
135 ;; Vector of the same length as `kkc-current-conversion'. The first | |
136 ;; element is a vector of: | |
137 ;; o index number of the first conversion shown previously, | |
138 ;; o index number of a conversion next of the last one shown previously, | |
139 ;; o the shown string itself. | |
140 ;; The remaining elements are widths (including columns for index | |
141 ;; numbers) of conversions stored in the same order as in | |
142 ;; `kkc-current-conversion'. | |
143 (defvar kkc-current-conversions-width nil) | |
144 | |
145 (defvar kkc-show-conversion-list-count 4 | |
146 "Count of successive `kkc-next' or `kkc-prev' to show conversion list.") | |
147 | |
148 ;; Provided that `kkc-current-key' is [A B C D E F G H I], the current | |
149 ;; conversion target is [A B C D E F], the sequence of which | |
150 ;; conversion is found is [A B C D]: | |
151 ;; | |
152 ;; A B C D E F G H I | |
153 ;; kkc-overlay-head (black): |<--------->| | |
154 ;; kkc-overlay-tail (underline): |<------->| | |
155 ;; kkc-length-head: |<--------->| | |
156 ;; kkc-length-converted: |<----->| | |
157 ;; | |
158 (defvar kkc-overlay-head nil) | |
159 (defvar kkc-overlay-tail nil) | |
160 (defvar kkc-length-head nil) | |
161 (defvar kkc-length-converted nil) | |
162 | |
163 ;; Cursor type (`box' or `bar') of the current frame. | |
164 (defvar kkc-cursor-type nil) | |
165 | |
166 ;; Flag to tell if the current conversion is canceled. If non-nil, | |
167 ;; the value is a buffer position of the head of currently active | |
168 ;; conversion region. | |
169 (defvar kkc-canceled nil) | |
170 | |
171 ;; Lookup SKK dictionary to set list of conversions in | |
172 ;; kkc-current-conversions for key sequence kkc-current-key of length | |
173 ;; LEN. If no conversion is found in the dictionary, don't change | |
174 ;; kkc-current-conversions and return nil. | |
175 ;; Postfixes are handled only if POSTFIX is non-nil. | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
176 (defun kkc-lookup-key (len &optional postfix prefer-noun) |
17052 | 177 ;; At first, prepare cache data if any. |
178 (if (not kkc-init-file-flag) | |
179 (progn | |
180 (setq kkc-init-file-flag t) | |
181 (add-hook 'kill-emacs-hook 'kkc-save-init-file) | |
182 (if (file-readable-p kkc-init-file-name) | |
183 (condition-case nil | |
184 (load-file "~/.kkcrc") | |
185 (error (message "Invalid data in %s" kkc-init-file-name) | |
186 (ding)))))) | |
187 (let ((entry (lookup-nested-alist kkc-current-key kkc-lookup-cache len 0 t))) | |
188 (if (consp (car entry)) | |
189 (setq kkc-length-converted len | |
190 kkc-current-conversions-width nil | |
191 kkc-current-conversions (car entry)) | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
192 (setq entry (skkdic-lookup-key kkc-current-key len postfix prefer-noun)) |
17052 | 193 (if entry |
194 (progn | |
195 (setq kkc-length-converted len | |
196 kkc-current-conversions-width nil | |
197 kkc-current-conversions (cons 1 entry)) | |
198 (if postfix | |
199 ;; Store this conversions in the cache. | |
200 (progn | |
201 (set-nested-alist kkc-current-key kkc-current-conversions | |
202 kkc-lookup-cache kkc-length-converted) | |
203 (setq kkc-init-file-flag 'kkc-lookup-cache))) | |
204 t) | |
205 (if (= len 1) | |
206 (setq kkc-length-converted 1 | |
207 kkc-current-conversions-width nil | |
208 kkc-current-conversions (cons 0 nil))))))) | |
209 | |
210 ;;;###autoload | |
19406 | 211 (defun kkc-region (from to &optional kkc-mode-exit-function) |
17052 | 212 "Convert Kana string in the current region to Kanji-Kana mixed string. |
213 After one candidate of conversion is shown in the region, users are | |
19406 | 214 put in KKC major mode to select a desirable conversion. |
215 Optional arg KKC-MODE-EXIT-FUNCTION if non-nil is called on exiting KKC mode." | |
17052 | 216 (interactive "r") |
217 (setq kkc-original-kana (buffer-substring from to)) | |
218 (goto-char from) | |
219 | |
220 ;; Setup overlays. | |
221 (if (overlayp kkc-overlay-head) | |
222 (move-overlay kkc-overlay-head from to) | |
223 (setq kkc-overlay-head (make-overlay from to nil nil t)) | |
224 (overlay-put kkc-overlay-head 'face 'highlight)) | |
225 (if (overlayp kkc-overlay-tail) | |
226 (move-overlay kkc-overlay-tail to to) | |
227 (setq kkc-overlay-tail (make-overlay to to nil nil t)) | |
228 (overlay-put kkc-overlay-tail 'face 'underline)) | |
229 | |
230 ;; After updating the conversion region with the first candidate of | |
231 ;; conversion, jump into a recursive editing environment with KKC | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
232 ;; mode. |
17052 | 233 (let ((overriding-local-map nil) |
234 (previous-local-map (current-local-map)) | |
235 (minor-mode-alist nil) | |
236 (minor-mode-map-alist nil) | |
237 (current-input-method-title kkc-input-method-title) | |
238 major-mode mode-name) | |
239 (unwind-protect | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
240 (let (len) |
17052 | 241 (setq kkc-canceled nil) |
242 (setq kkc-current-key (string-to-vector kkc-original-kana)) | |
243 (setq kkc-length-head (length kkc-current-key)) | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
244 (setq len kkc-length-head) |
17052 | 245 (setq kkc-length-converted 0) |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
246 (while (not (kkc-lookup-key kkc-length-head nil |
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
247 (< kkc-length-head len))) |
17052 | 248 (setq kkc-length-head (1- kkc-length-head))) |
249 (goto-char to) | |
250 (kkc-update-conversion 'all) | |
251 (kkc-mode) | |
252 (recursive-edit)) | |
253 (goto-char (overlay-end kkc-overlay-tail)) | |
254 (delete-overlay kkc-overlay-head) | |
255 (delete-overlay kkc-overlay-tail) | |
19406 | 256 (use-local-map previous-local-map) |
257 (if (and kkc-mode-exit-function | |
258 (fboundp kkc-mode-exit-function)) | |
259 (funcall kkc-mode-exit-function (if kkc-canceled | |
260 (cons kkc-canceled (point)))))))) | |
17052 | 261 |
262 (defun kkc-terminate () | |
263 "Exit from KKC mode by fixing the current conversion." | |
264 (interactive) | |
265 (throw 'exit nil)) | |
266 | |
267 (defun kkc-non-kkc-command () | |
268 "Exit from KKC mode by fixing the current conversion. | |
269 After that, handle the event which invoked this command." | |
270 (interactive) | |
19869
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
271 (let* ((key (this-command-keys)) |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
272 (keylist (listify-key-sequence key))) |
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
273 (setq unread-command-events (append keylist unread-command-events))) |
17052 | 274 (kkc-terminate)) |
275 | |
276 (defun kkc-cancel () | |
277 "Exit from KKC mode by canceling any conversions." | |
278 (interactive) | |
279 (setq kkc-canceled (overlay-start kkc-overlay-head)) | |
280 (goto-char kkc-canceled) | |
281 (delete-region (overlay-start kkc-overlay-head) | |
282 (overlay-end kkc-overlay-tail)) | |
283 (insert kkc-original-kana) | |
284 (kkc-terminate)) | |
285 | |
286 (defun kkc-first-char-only () | |
287 "Select only the first character currently converted." | |
288 (interactive) | |
289 (goto-char (overlay-start kkc-overlay-head)) | |
290 (forward-char 1) | |
291 (delete-region (point) (overlay-end kkc-overlay-tail)) | |
292 (kkc-terminate)) | |
293 | |
294 ;; Count of successive invocations of `kkc-next'. | |
295 (defvar kkc-next-count nil) | |
296 | |
297 (defun kkc-next () | |
298 "Select the next candidate of conversion." | |
299 (interactive) | |
300 (if (eq this-command last-command) | |
301 (setq kkc-next-count (1+ kkc-next-count)) | |
302 (setq kkc-next-count 1)) | |
303 (let ((idx (1+ (car kkc-current-conversions)))) | |
304 (if (< idx 0) | |
305 (setq idx 1)) | |
306 (if (>= idx (length kkc-current-conversions)) | |
307 (setq idx 0)) | |
308 (setcar kkc-current-conversions idx) | |
309 (if (> idx 1) | |
310 (progn | |
311 (set-nested-alist kkc-current-key kkc-current-conversions | |
312 kkc-lookup-cache kkc-length-converted) | |
313 (setq kkc-init-file-flag 'kkc-lookup-cache))) | |
314 (if (or kkc-current-conversions-width | |
315 (>= kkc-next-count kkc-show-conversion-list-count)) | |
316 (kkc-show-conversion-list-update)) | |
317 (kkc-update-conversion))) | |
318 | |
319 ;; Count of successive invocations of `kkc-next'. | |
320 (defvar kkc-prev-count nil) | |
321 | |
322 (defun kkc-prev () | |
323 "Select the previous candidate of conversion." | |
324 (interactive) | |
325 (if (eq this-command last-command) | |
326 (setq kkc-prev-count (1+ kkc-prev-count)) | |
327 (setq kkc-prev-count 1)) | |
328 (let ((idx (1- (car kkc-current-conversions)))) | |
329 (if (< idx 0) | |
330 (setq idx (1- (length kkc-current-conversions)))) | |
331 (setcar kkc-current-conversions idx) | |
332 (if (> idx 1) | |
333 (progn | |
334 (set-nested-alist kkc-current-key kkc-current-conversions | |
335 kkc-lookup-cache kkc-length-converted) | |
336 (setq kkc-init-file-flag 'kkc-lookup-cache))) | |
337 (if (or kkc-current-conversions-width | |
338 (>= kkc-prev-count kkc-show-conversion-list-count)) | |
339 (kkc-show-conversion-list-update)) | |
340 (kkc-update-conversion))) | |
341 | |
342 (defun kkc-select-from-list () | |
343 "Select one candidate from the list currently shown in echo area." | |
344 (interactive) | |
345 (let (idx) | |
346 (if kkc-current-conversions-width | |
347 (let ((len (length kkc-show-conversion-list-index-chars)) | |
348 (maxlen (- (aref (aref kkc-current-conversions-width 0) 1) | |
349 (aref (aref kkc-current-conversions-width 0) 0))) | |
350 (i 0)) | |
351 (if (> len maxlen) | |
352 (setq len maxlen)) | |
353 (while (< i len) | |
354 (if (= (aref kkc-show-conversion-list-index-chars i) | |
355 last-input-char) | |
356 (setq idx i i len) | |
357 (setq i (1+ i)))))) | |
358 (if idx | |
359 (progn | |
360 (setcar kkc-current-conversions | |
361 (+ (aref (aref kkc-current-conversions-width 0) 0) idx)) | |
362 (kkc-show-conversion-list-update) | |
363 (kkc-update-conversion)) | |
364 (setq unread-command-events (list last-input-event)) | |
365 (kkc-terminate)))) | |
366 | |
367 (defun kkc-katakana () | |
368 "Convert to Katakana." | |
369 (interactive) | |
370 (setcar kkc-current-conversions -1) | |
371 (kkc-update-conversion 'all)) | |
372 | |
373 (defun kkc-hiragana () | |
374 "Convert to hiragana." | |
375 (interactive) | |
376 (setcar kkc-current-conversions 0) | |
377 (kkc-update-conversion)) | |
378 | |
379 (defun kkc-shorter () | |
380 "Make the Kana string to be converted shorter." | |
381 (interactive) | |
382 (if (<= kkc-length-head 1) | |
383 (error "Can't be shorter") | |
384 (setq kkc-length-head (1- kkc-length-head)) | |
385 (if (> kkc-length-converted kkc-length-head) | |
386 (let ((len kkc-length-head)) | |
387 (setq kkc-length-converted 0) | |
388 (while (not (kkc-lookup-key len)) | |
389 (setq len (1- len))))) | |
390 (kkc-update-conversion 'all))) | |
391 | |
392 (defun kkc-longer () | |
393 "Make the Kana string to be converted longer." | |
394 (interactive) | |
395 (if (>= kkc-length-head (length kkc-current-key)) | |
396 (error "Can't be longer") | |
397 (setq kkc-length-head (1+ kkc-length-head)) | |
398 ;; This time, try also entries with postfixes. | |
399 (kkc-lookup-key kkc-length-head 'postfix) | |
400 (kkc-update-conversion 'all))) | |
401 | |
402 (defun kkc-next-phrase () | |
403 "Fix the currently converted string and try to convert the remaining string." | |
404 (interactive) | |
405 (if (>= kkc-length-head (length kkc-current-key)) | |
406 (kkc-terminate) | |
407 (setq kkc-length-head (- (length kkc-current-key) kkc-length-head)) | |
408 (goto-char (overlay-end kkc-overlay-head)) | |
409 (while (and (< (point) (overlay-end kkc-overlay-tail)) | |
410 (looking-at "\\CH")) | |
411 (goto-char (match-end 0)) | |
412 (setq kkc-length-head (1- kkc-length-head))) | |
413 (if (= kkc-length-head 0) | |
414 (kkc-terminate) | |
415 (let ((newkey (make-vector kkc-length-head 0)) | |
416 (idx (- (length kkc-current-key) kkc-length-head)) | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
417 (len kkc-length-head) |
17052 | 418 (i 0)) |
419 ;; For the moment, (setq kkc-original-kana (concat newkey)) | |
420 ;; doesn't work. | |
421 (setq kkc-original-kana "") | |
422 (while (< i kkc-length-head) | |
423 (aset newkey i (aref kkc-current-key (+ idx i))) | |
424 (setq kkc-original-kana | |
425 (concat kkc-original-kana (char-to-string (aref newkey i)))) | |
426 (setq i (1+ i))) | |
427 (setq kkc-current-key newkey) | |
428 (setq kkc-length-converted 0) | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
429 (while (and (not (kkc-lookup-key kkc-length-head nil |
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
430 (< kkc-length-head len))) |
17052 | 431 (> kkc-length-head 1)) |
432 (setq kkc-length-head (1- kkc-length-head))) | |
433 (let ((pos (point)) | |
434 (tail (overlay-end kkc-overlay-tail))) | |
435 (move-overlay kkc-overlay-head pos tail) | |
436 (move-overlay kkc-overlay-tail tail tail)) | |
437 (kkc-update-conversion 'all))))) | |
438 | |
439 ;; We'll show users a list of available conversions in echo area with | |
440 ;; index numbers so that users can select one conversion with the | |
441 ;; number. | |
442 | |
443 ;; Set `kkc-current-conversions-width'. | |
444 (defun kkc-setup-current-conversions-width () | |
445 (let ((convs (cdr kkc-current-conversions)) | |
446 (len (length kkc-current-conversions)) | |
447 (idx 1)) | |
448 (setq kkc-current-conversions-width (make-vector len nil)) | |
449 ;; To tell `kkc-show-conversion-list-update' to generate | |
450 ;; message from scratch. | |
451 (aset kkc-current-conversions-width 0 (vector len -2 nil)) | |
452 ;; Fill the remaining slots. | |
453 (while convs | |
454 (aset kkc-current-conversions-width idx | |
455 (+ (string-width (car convs)) 4)) | |
456 (setq convs (cdr convs) | |
457 idx (1+ idx))))) | |
458 | |
459 (defun kkc-show-conversion-list-or-next-group () | |
460 "Show list of available conversions in echo area with index numbers. | |
461 If the list is already shown, show the next group of conversions, | |
462 and change the current conversion to the first one in the group." | |
463 (interactive) | |
464 (if (< (length kkc-current-conversions) 3) | |
465 (error "No alternative")) | |
466 (if kkc-current-conversions-width | |
467 (let ((next-idx (aref (aref kkc-current-conversions-width 0) 1))) | |
468 (if (< next-idx (length kkc-current-conversions-width)) | |
469 (setcar kkc-current-conversions next-idx) | |
470 (setcar kkc-current-conversions 1)) | |
471 (kkc-show-conversion-list-update) | |
472 (kkc-update-conversion)) | |
473 (kkc-setup-current-conversions-width) | |
474 (kkc-show-conversion-list-update))) | |
475 | |
476 (defun kkc-show-conversion-list-or-prev-group () | |
477 "Show list of available conversions in echo area with index numbers. | |
478 If the list is already shown, show the previous group of conversions, | |
479 and change the current conversion to the last one in the group." | |
480 (interactive) | |
481 (if (< (length kkc-current-conversions) 3) | |
482 (error "No alternative")) | |
483 (if kkc-current-conversions-width | |
484 (let ((this-idx (aref (aref kkc-current-conversions-width 0) 0))) | |
485 (if (> this-idx 1) | |
486 (setcar kkc-current-conversions (1- this-idx)) | |
487 (setcar kkc-current-conversions | |
488 (1- (length kkc-current-conversions-width)))) | |
489 (kkc-show-conversion-list-update) | |
490 (kkc-update-conversion)) | |
491 (kkc-setup-current-conversions-width) | |
492 (kkc-show-conversion-list-update))) | |
493 | |
494 ;; Update the conversion list shown in echo area. | |
495 (defun kkc-show-conversion-list-update () | |
496 (or kkc-current-conversions-width | |
497 (kkc-setup-current-conversions-width)) | |
498 (let* ((current-idx (car kkc-current-conversions)) | |
499 (first-slot (aref kkc-current-conversions-width 0)) | |
500 (this-idx (aref first-slot 0)) | |
501 (next-idx (aref first-slot 1)) | |
502 (msg (aref first-slot 2))) | |
503 (if (< current-idx this-idx) | |
504 ;; The currently selected conversion is before the list shown | |
505 ;; previously. We must start calculation of message width | |
506 ;; from the start again. | |
507 (setq this-idx 1 msg nil) | |
508 (if (>= current-idx next-idx) | |
509 ;; The currently selected conversion is after the list shown | |
510 ;; previously. We start calculation of message width from | |
511 ;; the conversion next of TO. | |
512 (setq this-idx next-idx msg nil) | |
513 ;; The current conversion is in MSG. Just clear brackets | |
514 ;; around index number. | |
515 (if (string-match "<.>" msg) | |
516 (progn | |
517 (aset msg (match-beginning 0) ?\ ) | |
518 (aset msg (1- (match-end 0)) ?\ ))))) | |
519 (if (not msg) | |
520 (let ((len (length kkc-current-conversions)) | |
521 (max-width (window-width (minibuffer-window))) | |
522 (width-table kkc-current-conversions-width) | |
523 (width 0) | |
524 (idx this-idx) | |
525 l) | |
526 (while (< idx current-idx) | |
527 (if (<= (+ width (aref width-table idx)) max-width) | |
528 (setq width (+ width (aref width-table idx))) | |
529 (setq this-idx idx width (aref width-table idx))) | |
530 (setq idx (1+ idx) | |
531 l (cdr l))) | |
532 (aset first-slot 0 this-idx) | |
533 (while (and (< idx len) | |
534 (<= (+ width (aref width-table idx)) max-width)) | |
535 (setq width (+ width (aref width-table idx)) | |
536 idx (1+ idx) | |
537 l (cdr l))) | |
538 (aset first-slot 1 (setq next-idx idx)) | |
539 (setq l (nthcdr this-idx kkc-current-conversions)) | |
540 (setq msg "") | |
541 (setq idx this-idx) | |
542 (while (< idx next-idx) | |
543 (setq msg (format "%s %c %s " | |
544 msg | |
545 (aref kkc-show-conversion-list-index-chars | |
546 (- idx this-idx)) | |
547 (car l))) | |
548 (setq idx (1+ idx) | |
549 l (cdr l))) | |
550 (aset first-slot 2 msg))) | |
551 (if (> current-idx 0) | |
552 (progn | |
553 ;; Highlight the current conversion by brackets. | |
554 (string-match (format " \\(%c\\) " | |
555 (aref kkc-show-conversion-list-index-chars | |
556 (- current-idx this-idx))) | |
557 msg) | |
558 (aset msg (match-beginning 0) ?<) | |
559 (aset msg (1- (match-end 0)) ?>))) | |
560 (message "%s" msg))) | |
561 | |
562 ;; Update the conversion area with the latest conversion selected. | |
563 ;; ALL if non nil means to update the whole area, else update only | |
564 ;; inside quail-overlay-head. | |
565 | |
566 (defun kkc-update-conversion (&optional all) | |
567 (goto-char (overlay-start kkc-overlay-head)) | |
568 (cond ((= (car kkc-current-conversions) 0) ; Hiragana | |
569 (let ((i 0)) | |
570 (while (< i kkc-length-converted) | |
571 (insert (aref kkc-current-key i)) | |
572 (setq i (1+ i))))) | |
573 ((= (car kkc-current-conversions) -1) ; Katakana | |
574 (let ((i 0)) | |
575 (while (< i kkc-length-converted) | |
576 (insert (japanese-katakana (aref kkc-current-key i))) | |
577 (setq i (1+ i))))) | |
578 (t | |
579 (insert (nth (car kkc-current-conversions) kkc-current-conversions)))) | |
580 (delete-region (point) (overlay-start kkc-overlay-tail)) | |
581 (if all | |
582 (let ((len (length kkc-current-key)) | |
583 (i kkc-length-converted)) | |
584 (delete-region (overlay-start kkc-overlay-tail) | |
585 (overlay-end kkc-overlay-head)) | |
586 (while (< i kkc-length-head) | |
587 (if (= (car kkc-current-conversions) -1) | |
588 (insert (japanese-katakana (aref kkc-current-key i))) | |
589 (insert (aref kkc-current-key i))) | |
590 (setq i (1+ i))) | |
591 (let ((pos (point))) | |
592 (while (< i len) | |
593 (insert (aref kkc-current-key i)) | |
594 (setq i (1+ i))) | |
595 (move-overlay kkc-overlay-head | |
596 (overlay-start kkc-overlay-head) pos) | |
597 (delete-region (point) (overlay-end kkc-overlay-tail))))) | |
598 (goto-char (overlay-end kkc-overlay-tail))) | |
599 | |
600 ;; | |
601 (provide 'kkc) | |
602 | |
603 ;; kkc.el ends here |