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, SKK
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)
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.
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.
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)
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
75 "Show key bindings available while converting by KKC."
77 (with-output-to-temp-buffer "*Help*"
78 (princ (substitute-command-keys "\\{kkc-keymap}"))))
81 (let ((map (make-sparse-keymap))
82 (len (length kkc-show-conversion-list-index-chars))
86 (char-to-string (aref kkc-show-conversion-list-index-chars i))
87 'kkc-select-from-list)
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)
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 (defvar kkc-show-conversion-list-count 4
135 "Count of successive `kkc-next' or `kkc-prev' to show conversion list.")
137 ;; Provided that `kkc-current-key' is [A B C D E F G H I], the current
138 ;; conversion target is [A B C D E F], and the sequence of which
139 ;; conversion is found is [A B C D]:
142 ;; kkc-overlay-head (black): |<--------->|
143 ;; kkc-overlay-tail (underline): |<------->|
144 ;; kkc-length-head: |<--------->|
145 ;; kkc-length-converted: |<----->|
147 (defvar kkc-overlay-head nil)
148 (defvar kkc-overlay-tail nil)
149 (defvar kkc-length-head nil)
150 (defvar kkc-length-converted nil)
152 ;; Cursor type (`box' or `bar') of the current frame.
153 (defvar kkc-cursor-type nil)
155 ;; Lookup SKK dictionary to set list of conversions in
156 ;; kkc-current-conversions for key sequence kkc-current-key of length
157 ;; LEN. If no conversion is found in the dictionary, don't change
158 ;; kkc-current-conversions and return nil.
159 ;; Postfixes are handled only if POSTFIX is non-nil.
160 (defun kkc-lookup-key (len &optional postfix prefer-noun)
161 ;; At first, prepare cache data if any.
162 (unless kkc-init-file-flag
163 (setq kkc-init-file-flag t
164 kkc-lookup-cache nil)
165 (add-hook 'kill-emacs-hook 'kkc-save-init-file)
166 (if (file-readable-p kkc-init-file-name)
168 (load-file kkc-init-file-name)
169 (kkc-error "Invalid data in %s" kkc-init-file-name))))
170 (or (and (nested-alist-p kkc-lookup-cache)
171 (eq (car kkc-lookup-cache) kkc-lookup-cache-tag))
172 (setq kkc-lookup-cache (list kkc-lookup-cache-tag)
173 kkc-init-file-flag 'kkc-lookup-cache))
174 (let ((entry (lookup-nested-alist kkc-current-key kkc-lookup-cache len 0 t)))
175 (if (consp (car entry))
176 (setq kkc-length-converted len
177 kkc-current-conversions-width nil
178 kkc-current-conversions (car entry))
179 (setq entry (skkdic-lookup-key kkc-current-key len postfix prefer-noun))
182 (setq kkc-length-converted len
183 kkc-current-conversions-width nil
184 kkc-current-conversions (cons 1 entry))
186 ;; Store this conversions in the cache.
188 (set-nested-alist kkc-current-key kkc-current-conversions
189 kkc-lookup-cache kkc-length-converted)
190 (setq kkc-init-file-flag 'kkc-lookup-cache)))
193 (setq kkc-length-converted 1
194 kkc-current-conversions-width nil
195 kkc-current-conversions (cons 0 nil)))))))
197 (put 'kkc-error 'error-conditions '(kkc-error error))
198 (defun kkc-error (&rest args)
199 (signal 'kkc-error (apply 'format args)))
201 (defvar kkc-converting nil)
204 (defun kkc-region (from to)
205 "Convert Kana string in the current region to Kanji-Kana mixed string.
206 Users can select a desirable conversion interactively.
207 When called from a program, expects two arguments,
208 positions FROM and TO (integers or markers) specifying the target region.
209 When it returns, the point is at the tail of the selected conversion,
210 and the return value is the length of the conversion."
212 (setq kkc-original-kana (buffer-substring from to))
216 (if (overlayp kkc-overlay-head)
217 (move-overlay kkc-overlay-head from to)
218 (setq kkc-overlay-head (make-overlay from to nil nil t))
219 (overlay-put kkc-overlay-head 'face 'highlight))
220 (if (overlayp kkc-overlay-tail)
221 (move-overlay kkc-overlay-tail to to)
222 (setq kkc-overlay-tail (make-overlay to to nil nil t))
223 (overlay-put kkc-overlay-tail 'face 'underline))
225 (setq kkc-current-key (string-to-vector kkc-original-kana))
226 (setq kkc-length-head (length kkc-current-key))
227 (setq kkc-length-converted 0)
230 ;; At first convert the region to the first candidate.
231 (let ((current-input-method-title kkc-input-method-title)
232 (input-method-function nil)
234 (while (not (kkc-lookup-key kkc-length-head nil first))
235 (setq kkc-length-head (1- kkc-length-head)
238 (kkc-update-conversion 'all)
240 ;; Then, ask users to selecte a desirable conversion.
241 (force-mode-line-update)
242 (setq kkc-converting t)
243 (while kkc-converting
244 (let* ((overriding-terminal-local-map kkc-keymap)
246 (keyseq (read-key-sequence nil))
247 (cmd (lookup-key kkc-keymap keyseq)))
250 (call-interactively cmd)
251 (kkc-error (message "%s" (cdr err)) (beep)))
252 ;; KEYSEQ is not defined in KKC keymap.
253 ;; Let's put the event back.
254 (setq unread-input-method-events
255 (append (string-to-list keyseq)
256 unread-input-method-events))
259 (force-mode-line-update)
260 (goto-char (overlay-end kkc-overlay-tail))
261 (- (overlay-start kkc-overlay-head) from))
262 (delete-overlay kkc-overlay-head)
263 (delete-overlay kkc-overlay-tail)))
265 (defun kkc-terminate ()
266 "Exit from KKC mode by fixing the current conversion."
268 (goto-char (overlay-end kkc-overlay-tail))
269 (move-overlay kkc-overlay-head (point) (point))
270 (setq kkc-converting nil))
273 "Exit from KKC mode by canceling any conversions."
275 (goto-char (overlay-start kkc-overlay-head))
276 (delete-region (overlay-start kkc-overlay-head)
277 (overlay-end kkc-overlay-tail))
278 (insert kkc-original-kana)
279 (setq kkc-converting nil))
281 (defun kkc-first-char-only ()
282 "Select only the first character currently converted."
284 (goto-char (overlay-start kkc-overlay-head))
286 (delete-region (point) (overlay-end kkc-overlay-tail))
289 ;; Count of successive invocations of `kkc-next'.
290 (defvar kkc-next-count nil)
293 "Select the next candidate of conversion."
295 (if (eq this-command last-command)
296 (setq kkc-next-count (1+ kkc-next-count))
297 (setq kkc-next-count 1))
298 (let ((idx (1+ (car kkc-current-conversions))))
301 (if (>= idx (length kkc-current-conversions))
303 (setcar kkc-current-conversions idx)
306 (set-nested-alist kkc-current-key kkc-current-conversions
307 kkc-lookup-cache kkc-length-converted)
308 (setq kkc-init-file-flag 'kkc-lookup-cache)))
309 (if (or kkc-current-conversions-width
310 (>= kkc-next-count kkc-show-conversion-list-count))
311 (kkc-show-conversion-list-update))
312 (kkc-update-conversion)))
314 ;; Count of successive invocations of `kkc-next'.
315 (defvar kkc-prev-count nil)
318 "Select the previous candidate of conversion."
320 (if (eq this-command last-command)
321 (setq kkc-prev-count (1+ kkc-prev-count))
322 (setq kkc-prev-count 1))
323 (let ((idx (1- (car kkc-current-conversions))))
325 (setq idx (1- (length kkc-current-conversions))))
326 (setcar kkc-current-conversions idx)
329 (set-nested-alist kkc-current-key kkc-current-conversions
330 kkc-lookup-cache kkc-length-converted)
331 (setq kkc-init-file-flag 'kkc-lookup-cache)))
332 (if (or kkc-current-conversions-width
333 (>= kkc-prev-count kkc-show-conversion-list-count))
334 (kkc-show-conversion-list-update))
335 (kkc-update-conversion)))
337 (defun kkc-select-from-list ()
338 "Select one candidate from the list currently shown in echo area."
341 (if kkc-current-conversions-width
342 (let ((len (length kkc-show-conversion-list-index-chars))
343 (maxlen (- (aref (aref kkc-current-conversions-width 0) 1)
344 (aref (aref kkc-current-conversions-width 0) 0)))
349 (if (= (aref kkc-show-conversion-list-index-chars i)
355 (setcar kkc-current-conversions
356 (+ (aref (aref kkc-current-conversions-width 0) 0) idx))
357 (kkc-show-conversion-list-update)
358 (kkc-update-conversion))
359 (setq unread-input-method-events
360 (cons last-input-event unread-input-method-events))
363 (defun kkc-katakana ()
364 "Convert to Katakana."
366 (setcar kkc-current-conversions -1)
367 (kkc-update-conversion 'all))
369 (defun kkc-hiragana ()
370 "Convert to hiragana."
372 (setcar kkc-current-conversions 0)
373 (kkc-update-conversion))
375 (defun kkc-shorter ()
376 "Make the Kana string to be converted shorter."
378 (if (<= kkc-length-head 1)
379 (kkc-error "Can't be shorter"))
380 (setq kkc-length-head (1- kkc-length-head))
381 (if (> kkc-length-converted kkc-length-head)
382 (let ((len kkc-length-head))
383 (setq kkc-length-converted 0)
384 (while (not (kkc-lookup-key len))
385 (setq len (1- len)))))
386 (kkc-update-conversion 'all))
389 "Make the Kana string to be converted longer."
391 (if (>= kkc-length-head (length kkc-current-key))
392 (kkc-error "Can't be longer"))
393 (setq kkc-length-head (1+ kkc-length-head))
394 ;; This time, try also entries with postfixes.
395 (kkc-lookup-key kkc-length-head 'postfix)
396 (kkc-update-conversion 'all))
398 (defun kkc-shorter-conversion ()
399 "Make the Kana string to be converted shorter."
401 (if (<= kkc-length-converted 1)
402 (kkc-error "Can't be shorter"))
403 (let ((len (1- kkc-length-converted)))
404 (setq kkc-length-converted 0)
405 (while (not (kkc-lookup-key len))
406 (setq len (1- len))))
407 (kkc-update-conversion 'all))
409 (defun kkc-longer-phrase ()
410 "Make the current phrase (BUNSETSU) longer without looking up dictionary."
412 (if (>= kkc-length-head (length kkc-current-key))
413 (kkc-error "Can't be longer"))
414 (setq kkc-length-head (1+ kkc-length-head))
415 (kkc-update-conversion 'all))
417 (defun kkc-next-phrase ()
418 "Fix the currently converted string and try to convert the remaining string."
420 (if (>= kkc-length-head (length kkc-current-key))
422 (setq kkc-length-head (- (length kkc-current-key) kkc-length-head))
423 (goto-char (overlay-end kkc-overlay-head))
424 (while (and (< (point) (overlay-end kkc-overlay-tail))
426 (goto-char (match-end 0))
427 (setq kkc-length-head (1- kkc-length-head)))
428 (if (= kkc-length-head 0)
430 (let ((newkey (make-vector kkc-length-head 0))
431 (idx (- (length kkc-current-key) kkc-length-head))
432 (len kkc-length-head)
434 ;; For the moment, (setq kkc-original-kana (concat newkey))
436 (setq kkc-original-kana "")
437 (while (< i kkc-length-head)
438 (aset newkey i (aref kkc-current-key (+ idx i)))
439 (setq kkc-original-kana
440 (concat kkc-original-kana (char-to-string (aref newkey i))))
442 (setq kkc-current-key newkey)
443 (setq kkc-length-converted 0)
444 (while (and (not (kkc-lookup-key kkc-length-head nil
445 (< kkc-length-head len)))
446 (> kkc-length-head 1))
447 (setq kkc-length-head (1- kkc-length-head)))
449 (tail (overlay-end kkc-overlay-tail)))
450 (move-overlay kkc-overlay-head pos tail)
451 (move-overlay kkc-overlay-tail tail tail))
452 (kkc-update-conversion 'all)))))
454 ;; We'll show users a list of available conversions in echo area with
455 ;; index numbers so that users can select one conversion with the
458 ;; Set `kkc-current-conversions-width'.
459 (defun kkc-setup-current-conversions-width ()
460 (let ((convs (cdr kkc-current-conversions))
461 (len (length kkc-current-conversions))
463 (setq kkc-current-conversions-width (make-vector len nil))
464 ;; To tell `kkc-show-conversion-list-update' to generate
465 ;; message from scratch.
466 (aset kkc-current-conversions-width 0 (vector len -2 nil))
467 ;; Fill the remaining slots.
469 (aset kkc-current-conversions-width idx
470 (+ (string-width (car convs)) 4))
471 (setq convs (cdr convs)
474 (defun kkc-show-conversion-list-or-next-group ()
475 "Show list of available conversions in echo area with index numbers.
476 If the list is already shown, show the next group of conversions,
477 and change the current conversion to the first one in the group."
479 (if (< (length kkc-current-conversions) 3)
480 (kkc-error "No alternative"))
481 (if kkc-current-conversions-width
482 (let ((next-idx (aref (aref kkc-current-conversions-width 0) 1)))
483 (if (< next-idx (length kkc-current-conversions-width))
484 (setcar kkc-current-conversions next-idx)
485 (setcar kkc-current-conversions 1))
486 (kkc-show-conversion-list-update)
487 (kkc-update-conversion))
488 (kkc-setup-current-conversions-width)
489 (kkc-show-conversion-list-update)))
491 (defun kkc-show-conversion-list-or-prev-group ()
492 "Show list of available conversions in echo area with index numbers.
493 If the list is already shown, show the previous group of conversions,
494 and change the current conversion to the last one in the group."
496 (if (< (length kkc-current-conversions) 3)
497 (kkc-error "No alternative"))
498 (if kkc-current-conversions-width
499 (let ((this-idx (aref (aref kkc-current-conversions-width 0) 0)))
501 (setcar kkc-current-conversions (1- this-idx))
502 (setcar kkc-current-conversions
503 (1- (length kkc-current-conversions-width))))
504 (kkc-show-conversion-list-update)
505 (kkc-update-conversion))
506 (kkc-setup-current-conversions-width)
507 (kkc-show-conversion-list-update)))
509 ;; Update the conversion list shown in echo area.
510 (defun kkc-show-conversion-list-update ()
511 (or kkc-current-conversions-width
512 (kkc-setup-current-conversions-width))
513 (let* ((current-idx (car kkc-current-conversions))
514 (first-slot (aref kkc-current-conversions-width 0))
515 (this-idx (aref first-slot 0))
516 (next-idx (aref first-slot 1))
517 (msg (aref first-slot 2)))
518 (if (< current-idx this-idx)
519 ;; The currently selected conversion is before the list shown
520 ;; previously. We must start calculation of message width
521 ;; from the start again.
522 (setq this-idx 1 msg nil)
523 (if (>= current-idx next-idx)
524 ;; The currently selected conversion is after the list shown
525 ;; previously. We start calculation of message width from
526 ;; the conversion next of TO.
527 (setq this-idx next-idx msg nil)
528 ;; The current conversion is in MSG. Just clear brackets
529 ;; around index number.
530 (if (string-match "<.>" msg)
532 (aset msg (match-beginning 0) ?\ )
533 (aset msg (1- (match-end 0)) ?\ )))))
535 (let ((len (length kkc-current-conversions))
536 (max-width (window-width (minibuffer-window)))
537 (width-table kkc-current-conversions-width)
540 (max-items (length kkc-show-conversion-list-index-chars))
542 (while (< idx current-idx)
543 (if (and (<= (+ width (aref width-table idx)) max-width)
544 (< (- idx this-idx) max-items))
545 (setq width (+ width (aref width-table idx)))
546 (setq this-idx idx width (aref width-table idx)))
549 (aset first-slot 0 this-idx)
550 (while (and (< idx len)
551 (<= (+ width (aref width-table idx)) max-width)
552 (< (- idx this-idx) max-items))
553 (setq width (+ width (aref width-table idx))
556 (aset first-slot 1 (setq next-idx idx))
557 (setq l (nthcdr this-idx kkc-current-conversions))
560 (while (< idx next-idx)
561 (setq msg (format "%s %c %s "
563 (aref kkc-show-conversion-list-index-chars
568 (aset first-slot 2 msg)))
569 (if (> current-idx 0)
571 ;; Highlight the current conversion by brackets.
572 (string-match (format " \\(%c\\) "
573 (aref kkc-show-conversion-list-index-chars
574 (- current-idx this-idx)))
576 (aset msg (match-beginning 0) ?<)
577 (aset msg (1- (match-end 0)) ?>)))
580 ;; Update the conversion area with the latest conversion selected.
581 ;; ALL if non nil means to update the whole area, else update only
582 ;; inside quail-overlay-head.
584 (defun kkc-update-conversion (&optional all)
585 (goto-char (overlay-start kkc-overlay-head))
586 (cond ((= (car kkc-current-conversions) 0) ; Hiragana
588 (while (< i kkc-length-converted)
589 (insert (aref kkc-current-key i))
591 ((= (car kkc-current-conversions) -1) ; Katakana
593 (while (< i kkc-length-converted)
594 (insert (japanese-katakana (aref kkc-current-key i)))
597 (insert (nth (car kkc-current-conversions) kkc-current-conversions))))
598 (delete-region (point) (overlay-start kkc-overlay-tail))
600 (let ((len (length kkc-current-key))
601 (i kkc-length-converted))
602 (delete-region (overlay-start kkc-overlay-tail)
603 (overlay-end kkc-overlay-head))
604 (while (< i kkc-length-head)
605 (if (= (car kkc-current-conversions) -1)
606 (insert (japanese-katakana (aref kkc-current-key i)))
607 (insert (aref kkc-current-key i)))
611 (insert (aref kkc-current-key i))
613 (move-overlay kkc-overlay-head
614 (overlay-start kkc-overlay-head) pos)
615 (delete-region (point) (overlay-end kkc-overlay-tail)))))
616 (goto-char (overlay-end kkc-overlay-tail)))