Undo prev change.
[bpt/emacs.git] / lisp / international / kkc.el
CommitLineData
3fdc9c8f 1;;; kkc.el --- Kana Kanji converter
4ed46869 2
4ed46869 3;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
fa526c4a 4;; Licensed to the Free Software Foundation.
4ed46869 5
1b333492 6;; Keywords: mule, multilingual, Japanese
4ed46869
KH
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
369314dc
KH
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.
4ed46869
KH
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
1b333492 36(require 'ja-dic-utl)
4ed46869
KH
37
38(defvar kkc-input-method-title "\e$B4A\e(B"
39 "String denoting KKC input method.
40This 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.
f9628a49
KH
55(defvar kkc-lookup-cache nil)
56
57;; Tag symbol of `kkc-lookup-cache'.
58(defconst kkc-lookup-cache-tag 'kkc-lookup-cache-2)
4ed46869
KH
59
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)))
0ae6d482 64 (let ((coding-system-for-write 'iso-2022-7bit))
4ed46869
KH
65 (write-region (format "(setq kkc-lookup-cache '%S)\n" kkc-lookup-cache)
66 nil
67 kkc-init-file-name))))
68
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
9045dc7e 72 "1234567890")
4ed46869 73
15f7f59e
KH
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}"))))
79
9045dc7e 80(defvar kkc-keymap
15f7f59e 81 (let ((map (make-sparse-keymap))
9045dc7e 82 (len (length kkc-show-conversion-list-index-chars))
4ed46869 83 (i 0))
9045dc7e
KH
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)
4ed46869 88 (setq i (1+ i)))
4ed46869 89 (define-key map " " 'kkc-next)
4ed46869
KH
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)
f9628a49
KH
96 (define-key map "I" 'kkc-shorter-conversion)
97 (define-key map "O" 'kkc-longer-phrase)
4ed46869
KH
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)
e68e61b5 105 (define-key map [?\C- ] 'kkc-first-char-only)
4ed46869
KH
106 (define-key map [delete] 'kkc-cancel)
107 (define-key map [return] 'kkc-terminate)
15f7f59e 108 (define-key map "\C-h" 'kkc-help)
5d3cb559 109 map)
9045dc7e 110 "Keymap for KKC (Kana Kanji Converter).")
4ed46869
KH
111
112;;; Internal variables used in KKC.
113
114;; The current Kana string to be converted.
115(defvar kkc-original-kana nil)
116
117;; The current key sequence (vector of Kana characters) generated from
118;; `kkc-original-kana'.
119(defvar kkc-current-key nil)
120
121;; List of the current conversions for `kkc-current-key'.
122(defvar kkc-current-conversions nil)
123
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)
133
a9fbabda
KH
134(defcustom kkc-show-conversion-list-count 4
135 "*Count of successive `kkc-next' or `kkc-prev' to show conversion list.
136When you type SPC or C-p successively this count while using the input
137method `japanese', the conversion candidates are shown in the echo
138area while indicating the current selection by `<N>'."
139 :group 'mule
140 :type 'integer)
141
142;; Count of successive invocations of `kkc-next'.
143(defvar kkc-next-count nil)
144
145;; Count of successive invocations of `kkc-prev'.
146(defvar kkc-prev-count nil)
4ed46869
KH
147
148;; Provided that `kkc-current-key' is [A B C D E F G H I], the current
9045dc7e 149;; conversion target is [A B C D E F], and the sequence of which
4ed46869
KH
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
1b333492 166;; Lookup Japanese dictionary to set list of conversions in
4ed46869
KH
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.
93303c99 171(defun kkc-lookup-key (len &optional postfix prefer-noun)
4ed46869 172 ;; At first, prepare cache data if any.
f9628a49
KH
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))
4ed46869
KH
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))
93303c99 190 (setq entry (skkdic-lookup-key kkc-current-key len postfix prefer-noun))
4ed46869
KH
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)))
202 t)
203 (if (= len 1)
204 (setq kkc-length-converted 1
205 kkc-current-conversions-width nil
206 kkc-current-conversions (cons 0 nil)))))))
207
f9628a49
KH
208(put 'kkc-error 'error-conditions '(kkc-error error))
209(defun kkc-error (&rest args)
210 (signal 'kkc-error (apply 'format args)))
211
9045dc7e
KH
212(defvar kkc-converting nil)
213
44bec171
KH
214;;;###autoload
215(defvar kkc-after-update-conversion-functions nil
216 "Functions to run after a conversion is selected in `japanese' input method.
217With this input method, a user can select a proper conversion from
218candidate list. Each time he changes the selection, functions in this
219list are called with two arguments; starting and ending buffer
220positions that contains the current selection.")
221
4ed46869 222;;;###autoload
9045dc7e 223(defun kkc-region (from to)
4ed46869 224 "Convert Kana string in the current region to Kanji-Kana mixed string.
9045dc7e
KH
225Users can select a desirable conversion interactively.
226When called from a program, expects two arguments,
227positions FROM and TO (integers or markers) specifying the target region.
228When it returns, the point is at the tail of the selected conversion,
229and the return value is the length of the conversion."
4ed46869
KH
230 (interactive "r")
231 (setq kkc-original-kana (buffer-substring from to))
232 (goto-char from)
233
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))
243
9045dc7e
KH
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)
247
7efe5dbc 248 (unwind-protect
f9628a49 249 ;; At first convert the region to the first candidate.
7efe5dbc 250 (let ((current-input-method-title kkc-input-method-title)
f9628a49 251 (input-method-function nil)
e0d77f0d 252 (modified-p (buffer-modified-p))
f9628a49
KH
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)
a9fbabda
KH
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))
f9628a49 263
e8dd0160 264 ;; Then, ask users to select a desirable conversion.
7efe5dbc
KH
265 (force-mode-line-update)
266 (setq kkc-converting t)
9dfb0fa2
KH
267 ;; Hide "... loaded" message.
268 (message nil)
7efe5dbc 269 (while kkc-converting
e0d77f0d 270 (set-buffer-modified-p modified-p)
f12d44e5 271 (let* ((overriding-terminal-local-map kkc-keymap)
15f7f59e 272 (help-char nil)
7efe5dbc
KH
273 (keyseq (read-key-sequence nil))
274 (cmd (lookup-key kkc-keymap keyseq)))
275 (if (commandp cmd)
276 (condition-case err
a9fbabda
KH
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))
284 (t
285 (setq kkc-next-count 0 kkc-prev-count 0)))
286 (call-interactively cmd))
7efe5dbc
KH
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))))
294
295 (force-mode-line-update)
296 (goto-char (overlay-end kkc-overlay-tail))
297 (- (overlay-start kkc-overlay-head) from))
9045dc7e
KH
298 (delete-overlay kkc-overlay-head)
299 (delete-overlay kkc-overlay-tail)))
4ed46869
KH
300
301(defun kkc-terminate ()
302 "Exit from KKC mode by fixing the current conversion."
303 (interactive)
f370eb4c
KH
304 (goto-char (overlay-end kkc-overlay-tail))
305 (move-overlay kkc-overlay-head (point) (point))
9045dc7e 306 (setq kkc-converting nil))
4ed46869
KH
307
308(defun kkc-cancel ()
309 "Exit from KKC mode by canceling any conversions."
310 (interactive)
9045dc7e 311 (goto-char (overlay-start kkc-overlay-head))
4ed46869
KH
312 (delete-region (overlay-start kkc-overlay-head)
313 (overlay-end kkc-overlay-tail))
314 (insert kkc-original-kana)
f370eb4c 315 (setq kkc-converting nil))
4ed46869
KH
316
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))
324
4ed46869
KH
325(defun kkc-next ()
326 "Select the next candidate of conversion."
327 (interactive)
4ed46869
KH
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)))
343
4ed46869
KH
344(defun kkc-prev ()
345 "Select the previous candidate of conversion."
346 (interactive)
4ed46869
KH
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)))
360
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)
9045dc7e 374 last-input-event)
4ed46869
KH
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))
9045dc7e
KH
383 (setq unread-input-method-events
384 (cons last-input-event unread-input-method-events))
4ed46869
KH
385 (kkc-terminate))))
386
387(defun kkc-katakana ()
388 "Convert to Katakana."
389 (interactive)
390 (setcar kkc-current-conversions -1)
391 (kkc-update-conversion 'all))
392
393(defun kkc-hiragana ()
394 "Convert to hiragana."
395 (interactive)
396 (setcar kkc-current-conversions 0)
397 (kkc-update-conversion))
398
399(defun kkc-shorter ()
400 "Make the Kana string to be converted shorter."
401 (interactive)
402 (if (<= kkc-length-head 1)
f9628a49
KH
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))
4ed46869
KH
411
412(defun kkc-longer ()
413 "Make the Kana string to be converted longer."
414 (interactive)
415 (if (>= kkc-length-head (length kkc-current-key))
f9628a49
KH
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))
421
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))
432
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))
4ed46869
KH
440
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))
93303c99 456 (len kkc-length-head)
4ed46869
KH
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)
93303c99
KH
468 (while (and (not (kkc-lookup-key kkc-length-head nil
469 (< kkc-length-head len)))
4ed46869
KH
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)))))
477
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.
481
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)))))
497
498(defun kkc-show-conversion-list-or-next-group ()
499 "Show list of available conversions in echo area with index numbers.
500If the list is already shown, show the next group of conversions,
501and change the current conversion to the first one in the group."
502 (interactive)
503 (if (< (length kkc-current-conversions) 3)
f9628a49 504 (kkc-error "No alternative"))
4ed46869
KH
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)))
514
515(defun kkc-show-conversion-list-or-prev-group ()
516 "Show list of available conversions in echo area with index numbers.
517If the list is already shown, show the previous group of conversions,
518and change the current conversion to the last one in the group."
519 (interactive)
520 (if (< (length kkc-current-conversions) 3)
f9628a49 521 (kkc-error "No alternative"))
4ed46869
KH
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)))
532
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)
7efe5dbc 564 (max-items (length kkc-show-conversion-list-index-chars))
4ed46869 565 l)
a9fbabda
KH
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)
7efe5dbc
KH
569 (if (and (<= (+ width (aref width-table idx)) max-width)
570 (< (- idx this-idx) max-items))
4ed46869
KH
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)
a9fbabda
KH
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.
4ed46869 579 (while (and (< idx len)
7efe5dbc
KH
580 (<= (+ width (aref width-table idx)) max-width)
581 (< (- idx this-idx) max-items))
4ed46869
KH
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))
a9fbabda
KH
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))
4ed46869 592 (while (< idx next-idx)
a9fbabda 593 (setq msg (format "%s %c %s"
4ed46869
KH
594 msg
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)))
611
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.
615
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)))))
628 (t
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)))))
44bec171
KH
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))))
4ed46869
KH
653
654;;
655(provide 'kkc)
656
657;; kkc.el ends here