(log-edit-menu): New menu.
[emacs.git] / lisp / international / kkc.el
blobf79c4c87bb85b0ef65940c2610b001b7730e9994
1 ;;; kkc.el --- Kana Kanji converter
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: mule, multilingual, Japanese
8 ;; This file is part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
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.
25 ;;; Commentary:
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.
34 ;;; Code:
36 (require 'ja-dic-utl)
38 (defvar kkc-input-method-title "\e$B4A\e(B"
39 "String denoting KKC input method.
40 This string is shown at mode line when users are in KKC mode.")
42 (defvar kkc-init-file-name "~/.kkcrc"
43 "Name of a file which contains user's initial setup code for KKC.")
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)
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 nil)
57 ;; Tag symbol of `kkc-lookup-cache'.
58 (defconst kkc-lookup-cache-tag 'kkc-lookup-cache-2)
60 (defun kkc-save-init-file ()
61 "Save initial setup code for KKC to a file specified by `kkc-init-file-name'"
62 (if (and kkc-init-file-flag
63 (not (eq kkc-init-file-flag t)))
64 (let ((coding-system-for-write 'iso-2022-7bit))
65 (write-region (format "(setq kkc-lookup-cache '%S)\n" kkc-lookup-cache)
66 nil
67 kkc-init-file-name))))
69 ;; Sequence of characters to be used for indexes for shown list. The
70 ;; Nth character is for the Nth conversion in the list currently shown.
71 (defvar kkc-show-conversion-list-index-chars
72 "1234567890")
74 (defun kkc-help ()
75 "Show key bindings available while converting by KKC."
76 (interactive)
77 (with-output-to-temp-buffer "*Help*"
78 (princ (substitute-command-keys "\\{kkc-keymap}"))))
80 (defvar kkc-keymap
81 (let ((map (make-sparse-keymap))
82 (len (length kkc-show-conversion-list-index-chars))
83 (i 0))
84 (while (< i len)
85 (define-key map
86 (char-to-string (aref kkc-show-conversion-list-index-chars i))
87 'kkc-select-from-list)
88 (setq i (1+ i)))
89 (define-key map " " 'kkc-next)
90 (define-key map "\r" 'kkc-terminate)
91 (define-key map "\C-@" 'kkc-first-char-only)
92 (define-key map "\C-n" 'kkc-next)
93 (define-key map "\C-p" 'kkc-prev)
94 (define-key map "\C-i" 'kkc-shorter)
95 (define-key map "\C-o" 'kkc-longer)
96 (define-key map "I" 'kkc-shorter-conversion)
97 (define-key map "O" 'kkc-longer-phrase)
98 (define-key map "\C-c" 'kkc-cancel)
99 (define-key map "\C-?" 'kkc-cancel)
100 (define-key map "\C-f" 'kkc-next-phrase)
101 (define-key map "K" 'kkc-katakana)
102 (define-key map "H" 'kkc-hiragana)
103 (define-key map "l" 'kkc-show-conversion-list-or-next-group)
104 (define-key map "L" 'kkc-show-conversion-list-or-prev-group)
105 (define-key map [?\C- ] 'kkc-first-char-only)
106 (define-key map [delete] 'kkc-cancel)
107 (define-key map [return] 'kkc-terminate)
108 (define-key map "\C-h" 'kkc-help)
109 map)
110 "Keymap for KKC (Kana Kanji Converter).")
112 ;;; Internal variables used in KKC.
114 ;; The current Kana string to be converted.
115 (defvar kkc-original-kana nil)
117 ;; The current key sequence (vector of Kana characters) generated from
118 ;; `kkc-original-kana'.
119 (defvar kkc-current-key nil)
121 ;; List of the current conversions for `kkc-current-key'.
122 (defvar kkc-current-conversions nil)
124 ;; Vector of the same length as `kkc-current-conversion'. The first
125 ;; element is a vector of:
126 ;; o index number of the first conversion shown previously,
127 ;; o index number of a conversion next of the last one shown previously,
128 ;; o the shown string itself.
129 ;; The remaining elements are widths (including columns for index
130 ;; numbers) of conversions stored in the same order as in
131 ;; `kkc-current-conversion'.
132 (defvar kkc-current-conversions-width nil)
134 (defcustom kkc-show-conversion-list-count 4
135 "*Count of successive `kkc-next' or `kkc-prev' to show conversion list.
136 When you type SPC or C-p successively this count while using the input
137 method `japanese', the conversion candidates are shown in the echo
138 area while indicating the current selection by `<N>'."
139 :group 'mule
140 :type 'integer)
142 ;; Count of successive invocations of `kkc-next'.
143 (defvar kkc-next-count nil)
145 ;; Count of successive invocations of `kkc-prev'.
146 (defvar kkc-prev-count nil)
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], and the sequence of which
150 ;; conversion is found is [A B C D]:
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: |<----->|
158 (defvar kkc-overlay-head nil)
159 (defvar kkc-overlay-tail nil)
160 (defvar kkc-length-head nil)
161 (defvar kkc-length-converted nil)
163 ;; Cursor type (`box' or `bar') of the current frame.
164 (defvar kkc-cursor-type nil)
166 ;; Lookup Japanese dictionary to set list of conversions in
167 ;; kkc-current-conversions for key sequence kkc-current-key of length
168 ;; LEN. If no conversion is found in the dictionary, don't change
169 ;; kkc-current-conversions and return nil.
170 ;; Postfixes are handled only if POSTFIX is non-nil.
171 (defun kkc-lookup-key (len &optional postfix prefer-noun)
172 ;; At first, prepare cache data if any.
173 (unless kkc-init-file-flag
174 (setq kkc-init-file-flag t
175 kkc-lookup-cache nil)
176 (add-hook 'kill-emacs-hook 'kkc-save-init-file)
177 (if (file-readable-p kkc-init-file-name)
178 (condition-case nil
179 (load-file kkc-init-file-name)
180 (kkc-error "Invalid data in %s" kkc-init-file-name))))
181 (or (and (nested-alist-p kkc-lookup-cache)
182 (eq (car kkc-lookup-cache) kkc-lookup-cache-tag))
183 (setq kkc-lookup-cache (list kkc-lookup-cache-tag)
184 kkc-init-file-flag 'kkc-lookup-cache))
185 (let ((entry (lookup-nested-alist kkc-current-key kkc-lookup-cache len 0 t)))
186 (if (consp (car entry))
187 (setq kkc-length-converted len
188 kkc-current-conversions-width nil
189 kkc-current-conversions (car entry))
190 (setq entry (skkdic-lookup-key kkc-current-key len postfix prefer-noun))
191 (if entry
192 (progn
193 (setq kkc-length-converted len
194 kkc-current-conversions-width nil
195 kkc-current-conversions (cons 1 entry))
196 (if postfix
197 ;; Store this conversions in the cache.
198 (progn
199 (set-nested-alist kkc-current-key kkc-current-conversions
200 kkc-lookup-cache kkc-length-converted)
201 (setq kkc-init-file-flag 'kkc-lookup-cache)))
203 (if (= len 1)
204 (setq kkc-length-converted 1
205 kkc-current-conversions-width nil
206 kkc-current-conversions (cons 0 nil)))))))
208 (put 'kkc-error 'error-conditions '(kkc-error error))
209 (defun kkc-error (&rest args)
210 (signal 'kkc-error (apply 'format args)))
212 (defvar kkc-converting nil)
214 ;;;###autoload
215 (defvar kkc-after-update-conversion-functions nil
216 "Functions to run after a conversion is selected in `japanese' input method.
217 With this input method, a user can select a proper conversion from
218 candidate list. Each time he changes the selection, functions in this
219 list are called with two arguments; starting and ending buffer
220 positions that contains the current selection.")
222 ;;;###autoload
223 (defun kkc-region (from to)
224 "Convert Kana string in the current region to Kanji-Kana mixed string.
225 Users can select a desirable conversion interactively.
226 When called from a program, expects two arguments,
227 positions FROM and TO (integers or markers) specifying the target region.
228 When it returns, the point is at the tail of the selected conversion,
229 and the return value is the length of the conversion."
230 (interactive "r")
231 (setq kkc-original-kana (buffer-substring from to))
232 (goto-char from)
234 ;; Setup overlays.
235 (if (overlayp kkc-overlay-head)
236 (move-overlay kkc-overlay-head from to)
237 (setq kkc-overlay-head (make-overlay from to nil nil t))
238 (overlay-put kkc-overlay-head 'face 'highlight))
239 (if (overlayp kkc-overlay-tail)
240 (move-overlay kkc-overlay-tail to to)
241 (setq kkc-overlay-tail (make-overlay to to nil nil t))
242 (overlay-put kkc-overlay-tail 'face 'underline))
244 (setq kkc-current-key (string-to-vector kkc-original-kana))
245 (setq kkc-length-head (length kkc-current-key))
246 (setq kkc-length-converted 0)
248 (unwind-protect
249 ;; At first convert the region to the first candidate.
250 (let ((current-input-method-title kkc-input-method-title)
251 (input-method-function nil)
252 (modified-p (buffer-modified-p))
253 (first t))
254 (while (not (kkc-lookup-key kkc-length-head nil first))
255 (setq kkc-length-head (1- kkc-length-head)
256 first nil))
257 (goto-char to)
258 (kkc-update-conversion 'all)
259 (setq kkc-next-count 1 kkc-prev-count 0)
260 (if (and (>= kkc-next-count kkc-show-conversion-list-count)
261 (>= (length kkc-current-conversions) 3))
262 (kkc-show-conversion-list-or-next-group))
264 ;; Then, ask users to select a desirable conversion.
265 (force-mode-line-update)
266 (setq kkc-converting t)
267 ;; Hide "... loaded" message.
268 (message nil)
269 (while kkc-converting
270 (set-buffer-modified-p modified-p)
271 (let* ((overriding-terminal-local-map kkc-keymap)
272 (help-char nil)
273 (keyseq (read-key-sequence nil))
274 (cmd (lookup-key kkc-keymap keyseq)))
275 (if (commandp cmd)
276 (condition-case err
277 (progn
278 (cond ((eq cmd 'kkc-next)
279 (setq kkc-next-count (1+ kkc-next-count)
280 kkc-prev-count 0))
281 ((eq cmd 'kkc-prev)
282 (setq kkc-prev-count (1+ kkc-prev-count)
283 kkc-next-count 0))
285 (setq kkc-next-count 0 kkc-prev-count 0)))
286 (call-interactively cmd))
287 (kkc-error (message "%s" (cdr err)) (beep)))
288 ;; KEYSEQ is not defined in KKC keymap.
289 ;; Let's put the event back.
290 (setq unread-input-method-events
291 (append (string-to-list keyseq)
292 unread-input-method-events))
293 (kkc-terminate))))
295 (force-mode-line-update)
296 (goto-char (overlay-end kkc-overlay-tail))
297 (- (overlay-start kkc-overlay-head) from))
298 (delete-overlay kkc-overlay-head)
299 (delete-overlay kkc-overlay-tail)))
301 (defun kkc-terminate ()
302 "Exit from KKC mode by fixing the current conversion."
303 (interactive)
304 (goto-char (overlay-end kkc-overlay-tail))
305 (move-overlay kkc-overlay-head (point) (point))
306 (setq kkc-converting nil))
308 (defun kkc-cancel ()
309 "Exit from KKC mode by canceling any conversions."
310 (interactive)
311 (goto-char (overlay-start kkc-overlay-head))
312 (delete-region (overlay-start kkc-overlay-head)
313 (overlay-end kkc-overlay-tail))
314 (insert kkc-original-kana)
315 (setq kkc-converting nil))
317 (defun kkc-first-char-only ()
318 "Select only the first character currently converted."
319 (interactive)
320 (goto-char (overlay-start kkc-overlay-head))
321 (forward-char 1)
322 (delete-region (point) (overlay-end kkc-overlay-tail))
323 (kkc-terminate))
325 (defun kkc-next ()
326 "Select the next candidate of conversion."
327 (interactive)
328 (let ((idx (1+ (car kkc-current-conversions))))
329 (if (< idx 0)
330 (setq idx 1))
331 (if (>= idx (length kkc-current-conversions))
332 (setq idx 0))
333 (setcar kkc-current-conversions idx)
334 (if (> idx 1)
335 (progn
336 (set-nested-alist kkc-current-key kkc-current-conversions
337 kkc-lookup-cache kkc-length-converted)
338 (setq kkc-init-file-flag 'kkc-lookup-cache)))
339 (if (or kkc-current-conversions-width
340 (>= kkc-next-count kkc-show-conversion-list-count))
341 (kkc-show-conversion-list-update))
342 (kkc-update-conversion)))
344 (defun kkc-prev ()
345 "Select the previous candidate of conversion."
346 (interactive)
347 (let ((idx (1- (car kkc-current-conversions))))
348 (if (< idx 0)
349 (setq idx (1- (length kkc-current-conversions))))
350 (setcar kkc-current-conversions idx)
351 (if (> idx 1)
352 (progn
353 (set-nested-alist kkc-current-key kkc-current-conversions
354 kkc-lookup-cache kkc-length-converted)
355 (setq kkc-init-file-flag 'kkc-lookup-cache)))
356 (if (or kkc-current-conversions-width
357 (>= kkc-prev-count kkc-show-conversion-list-count))
358 (kkc-show-conversion-list-update))
359 (kkc-update-conversion)))
361 (defun kkc-select-from-list ()
362 "Select one candidate from the list currently shown in echo area."
363 (interactive)
364 (let (idx)
365 (if kkc-current-conversions-width
366 (let ((len (length kkc-show-conversion-list-index-chars))
367 (maxlen (- (aref (aref kkc-current-conversions-width 0) 1)
368 (aref (aref kkc-current-conversions-width 0) 0)))
369 (i 0))
370 (if (> len maxlen)
371 (setq len maxlen))
372 (while (< i len)
373 (if (= (aref kkc-show-conversion-list-index-chars i)
374 last-input-event)
375 (setq idx i i len)
376 (setq i (1+ i))))))
377 (if idx
378 (progn
379 (setcar kkc-current-conversions
380 (+ (aref (aref kkc-current-conversions-width 0) 0) idx))
381 (kkc-show-conversion-list-update)
382 (kkc-update-conversion))
383 (setq unread-input-method-events
384 (cons last-input-event unread-input-method-events))
385 (kkc-terminate))))
387 (defun kkc-katakana ()
388 "Convert to Katakana."
389 (interactive)
390 (setcar kkc-current-conversions -1)
391 (kkc-update-conversion 'all))
393 (defun kkc-hiragana ()
394 "Convert to hiragana."
395 (interactive)
396 (setcar kkc-current-conversions 0)
397 (kkc-update-conversion))
399 (defun kkc-shorter ()
400 "Make the Kana string to be converted shorter."
401 (interactive)
402 (if (<= kkc-length-head 1)
403 (kkc-error "Can't be shorter"))
404 (setq kkc-length-head (1- kkc-length-head))
405 (if (> kkc-length-converted kkc-length-head)
406 (let ((len kkc-length-head))
407 (setq kkc-length-converted 0)
408 (while (not (kkc-lookup-key len))
409 (setq len (1- len)))))
410 (kkc-update-conversion 'all))
412 (defun kkc-longer ()
413 "Make the Kana string to be converted longer."
414 (interactive)
415 (if (>= kkc-length-head (length kkc-current-key))
416 (kkc-error "Can't be longer"))
417 (setq kkc-length-head (1+ kkc-length-head))
418 ;; This time, try also entries with postfixes.
419 (kkc-lookup-key kkc-length-head 'postfix)
420 (kkc-update-conversion 'all))
422 (defun kkc-shorter-conversion ()
423 "Make the Kana string to be converted shorter."
424 (interactive)
425 (if (<= kkc-length-converted 1)
426 (kkc-error "Can't be shorter"))
427 (let ((len (1- kkc-length-converted)))
428 (setq kkc-length-converted 0)
429 (while (not (kkc-lookup-key len))
430 (setq len (1- len))))
431 (kkc-update-conversion 'all))
433 (defun kkc-longer-phrase ()
434 "Make the current phrase (BUNSETSU) longer without looking up dictionary."
435 (interactive)
436 (if (>= kkc-length-head (length kkc-current-key))
437 (kkc-error "Can't be longer"))
438 (setq kkc-length-head (1+ kkc-length-head))
439 (kkc-update-conversion 'all))
441 (defun kkc-next-phrase ()
442 "Fix the currently converted string and try to convert the remaining string."
443 (interactive)
444 (if (>= kkc-length-head (length kkc-current-key))
445 (kkc-terminate)
446 (setq kkc-length-head (- (length kkc-current-key) kkc-length-head))
447 (goto-char (overlay-end kkc-overlay-head))
448 (while (and (< (point) (overlay-end kkc-overlay-tail))
449 (looking-at "\\CH"))
450 (goto-char (match-end 0))
451 (setq kkc-length-head (1- kkc-length-head)))
452 (if (= kkc-length-head 0)
453 (kkc-terminate)
454 (let ((newkey (make-vector kkc-length-head 0))
455 (idx (- (length kkc-current-key) kkc-length-head))
456 (len kkc-length-head)
457 (i 0))
458 ;; For the moment, (setq kkc-original-kana (concat newkey))
459 ;; doesn't work.
460 (setq kkc-original-kana "")
461 (while (< i kkc-length-head)
462 (aset newkey i (aref kkc-current-key (+ idx i)))
463 (setq kkc-original-kana
464 (concat kkc-original-kana (char-to-string (aref newkey i))))
465 (setq i (1+ i)))
466 (setq kkc-current-key newkey)
467 (setq kkc-length-converted 0)
468 (while (and (not (kkc-lookup-key kkc-length-head nil
469 (< kkc-length-head len)))
470 (> kkc-length-head 1))
471 (setq kkc-length-head (1- kkc-length-head)))
472 (let ((pos (point))
473 (tail (overlay-end kkc-overlay-tail)))
474 (move-overlay kkc-overlay-head pos tail)
475 (move-overlay kkc-overlay-tail tail tail))
476 (kkc-update-conversion 'all)))))
478 ;; We'll show users a list of available conversions in echo area with
479 ;; index numbers so that users can select one conversion with the
480 ;; number.
482 ;; Set `kkc-current-conversions-width'.
483 (defun kkc-setup-current-conversions-width ()
484 (let ((convs (cdr kkc-current-conversions))
485 (len (length kkc-current-conversions))
486 (idx 1))
487 (setq kkc-current-conversions-width (make-vector len nil))
488 ;; To tell `kkc-show-conversion-list-update' to generate
489 ;; message from scratch.
490 (aset kkc-current-conversions-width 0 (vector len -2 nil))
491 ;; Fill the remaining slots.
492 (while convs
493 (aset kkc-current-conversions-width idx
494 (+ (string-width (car convs)) 4))
495 (setq convs (cdr convs)
496 idx (1+ idx)))))
498 (defun kkc-show-conversion-list-or-next-group ()
499 "Show list of available conversions in echo area with index numbers.
500 If the list is already shown, show the next group of conversions,
501 and change the current conversion to the first one in the group."
502 (interactive)
503 (if (< (length kkc-current-conversions) 3)
504 (kkc-error "No alternative"))
505 (if kkc-current-conversions-width
506 (let ((next-idx (aref (aref kkc-current-conversions-width 0) 1)))
507 (if (< next-idx (length kkc-current-conversions-width))
508 (setcar kkc-current-conversions next-idx)
509 (setcar kkc-current-conversions 1))
510 (kkc-show-conversion-list-update)
511 (kkc-update-conversion))
512 (kkc-setup-current-conversions-width)
513 (kkc-show-conversion-list-update)))
515 (defun kkc-show-conversion-list-or-prev-group ()
516 "Show list of available conversions in echo area with index numbers.
517 If the list is already shown, show the previous group of conversions,
518 and change the current conversion to the last one in the group."
519 (interactive)
520 (if (< (length kkc-current-conversions) 3)
521 (kkc-error "No alternative"))
522 (if kkc-current-conversions-width
523 (let ((this-idx (aref (aref kkc-current-conversions-width 0) 0)))
524 (if (> this-idx 1)
525 (setcar kkc-current-conversions (1- this-idx))
526 (setcar kkc-current-conversions
527 (1- (length kkc-current-conversions-width))))
528 (kkc-show-conversion-list-update)
529 (kkc-update-conversion))
530 (kkc-setup-current-conversions-width)
531 (kkc-show-conversion-list-update)))
533 ;; Update the conversion list shown in echo area.
534 (defun kkc-show-conversion-list-update ()
535 (or kkc-current-conversions-width
536 (kkc-setup-current-conversions-width))
537 (let* ((current-idx (car kkc-current-conversions))
538 (first-slot (aref kkc-current-conversions-width 0))
539 (this-idx (aref first-slot 0))
540 (next-idx (aref first-slot 1))
541 (msg (aref first-slot 2)))
542 (if (< current-idx this-idx)
543 ;; The currently selected conversion is before the list shown
544 ;; previously. We must start calculation of message width
545 ;; from the start again.
546 (setq this-idx 1 msg nil)
547 (if (>= current-idx next-idx)
548 ;; The currently selected conversion is after the list shown
549 ;; previously. We start calculation of message width from
550 ;; the conversion next of TO.
551 (setq this-idx next-idx msg nil)
552 ;; The current conversion is in MSG. Just clear brackets
553 ;; around index number.
554 (if (string-match "<.>" msg)
555 (progn
556 (aset msg (match-beginning 0) ?\ )
557 (aset msg (1- (match-end 0)) ?\ )))))
558 (if (not msg)
559 (let ((len (length kkc-current-conversions))
560 (max-width (window-width (minibuffer-window)))
561 (width-table kkc-current-conversions-width)
562 (width 0)
563 (idx this-idx)
564 (max-items (length kkc-show-conversion-list-index-chars))
566 ;; Set THIS-IDX to the first index of conversion to be shown
567 ;; in MSG, and reflect it in kkc-current-conversions-width.
568 (while (<= idx current-idx)
569 (if (and (<= (+ width (aref width-table idx)) max-width)
570 (< (- idx this-idx) max-items))
571 (setq width (+ width (aref width-table idx)))
572 (setq this-idx idx width (aref width-table idx)))
573 (setq idx (1+ idx)
574 l (cdr l)))
575 (aset first-slot 0 this-idx)
576 ;; Set NEXT-IDX to the next index of the last conversion
577 ;; shown in MSG, and reflect it in
578 ;; kkc-current-conversions-width.
579 (while (and (< idx len)
580 (<= (+ width (aref width-table idx)) max-width)
581 (< (- idx this-idx) max-items))
582 (setq width (+ width (aref width-table idx))
583 idx (1+ idx)
584 l (cdr l)))
585 (aset first-slot 1 (setq next-idx idx))
586 (setq l (nthcdr this-idx kkc-current-conversions))
587 (setq msg (format " %c %s"
588 (aref kkc-show-conversion-list-index-chars 0)
589 (car l))
590 idx (1+ this-idx)
591 l (cdr l))
592 (while (< idx next-idx)
593 (setq msg (format "%s %c %s"
595 (aref kkc-show-conversion-list-index-chars
596 (- idx this-idx))
597 (car l)))
598 (setq idx (1+ idx)
599 l (cdr l)))
600 (aset first-slot 2 msg)))
601 (if (> current-idx 0)
602 (progn
603 ;; Highlight the current conversion by brackets.
604 (string-match (format " \\(%c\\) "
605 (aref kkc-show-conversion-list-index-chars
606 (- current-idx this-idx)))
607 msg)
608 (aset msg (match-beginning 0) ?<)
609 (aset msg (1- (match-end 0)) ?>)))
610 (message "%s" msg)))
612 ;; Update the conversion area with the latest conversion selected.
613 ;; ALL if non nil means to update the whole area, else update only
614 ;; inside quail-overlay-head.
616 (defun kkc-update-conversion (&optional all)
617 (goto-char (overlay-start kkc-overlay-head))
618 (cond ((= (car kkc-current-conversions) 0) ; Hiragana
619 (let ((i 0))
620 (while (< i kkc-length-converted)
621 (insert (aref kkc-current-key i))
622 (setq i (1+ i)))))
623 ((= (car kkc-current-conversions) -1) ; Katakana
624 (let ((i 0))
625 (while (< i kkc-length-converted)
626 (insert (japanese-katakana (aref kkc-current-key i)))
627 (setq i (1+ i)))))
629 (insert (nth (car kkc-current-conversions) kkc-current-conversions))))
630 (delete-region (point) (overlay-start kkc-overlay-tail))
631 (if all
632 (let ((len (length kkc-current-key))
633 (i kkc-length-converted))
634 (delete-region (overlay-start kkc-overlay-tail)
635 (overlay-end kkc-overlay-head))
636 (while (< i kkc-length-head)
637 (if (= (car kkc-current-conversions) -1)
638 (insert (japanese-katakana (aref kkc-current-key i)))
639 (insert (aref kkc-current-key i)))
640 (setq i (1+ i)))
641 (let ((pos (point)))
642 (while (< i len)
643 (insert (aref kkc-current-key i))
644 (setq i (1+ i)))
645 (move-overlay kkc-overlay-head
646 (overlay-start kkc-overlay-head) pos)
647 (delete-region (point) (overlay-end kkc-overlay-tail)))))
648 (unwind-protect
649 (run-hook-with-args 'kkc-after-update-conversion-functions
650 (overlay-start kkc-overlay-head)
651 (overlay-end kkc-overlay-head))
652 (goto-char (overlay-end kkc-overlay-tail))))
655 (provide 'kkc)
657 ;; kkc.el ends here