comparison lisp/international/kkc.el @ 22888:5bfe86125303

(kkc-lookup-cache): Initialize it to nil. (kkc-lookup-cache-tag): New constant. (kkc-lookup-key): If kkc-lookup-cache is nil, initialize it. Use kkc-init-file-name. (kkc-region): Fix previous change. Call kkc-error on error. (kkc-shorter-conversion, kkc-longer-phrase): New functions. (kkc-keymap): Bind them to "I" and "O" respectively. (kkc-error): New error symbol and new function. (kkc-longer, kkc-shorter): Call kkc-error on error. (kkc-show-conversion-list-or-next-group): Likewise. (kkc-show-conversion-list-or-prev-group): Likewise.
author Kenichi Handa <handa@m17n.org>
date Sun, 02 Aug 1998 01:06:57 +0000
parents 8f9d4edebbdd
children 95d147bbdce0
comparison
equal deleted inserted replaced
22887:e24b79da83d6 22888:5bfe86125303
50 (defvar kkc-init-file-flag nil) 50 (defvar kkc-init-file-flag nil)
51 51
52 ;; Cash data for `kkc-lookup-key'. This may be initialized by loading 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 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. 54 ;; modified, the data is written out to the file when exiting Emacs.
55 (defvar kkc-lookup-cache '(kkc-lookup-cache)) 55 (defvar kkc-lookup-cache nil)
56
57 ;; Tag symbol of `kkc-lookup-cache'.
58 (defconst kkc-lookup-cache-tag 'kkc-lookup-cache-2)
56 59
57 (defun kkc-save-init-file () 60 (defun kkc-save-init-file ()
58 "Save initial setup code for KKC to a file specified by `kkc-init-file-name'" 61 "Save initial setup code for KKC to a file specified by `kkc-init-file-name'"
59 (if (and kkc-init-file-flag 62 (if (and kkc-init-file-flag
60 (not (eq kkc-init-file-flag t))) 63 (not (eq kkc-init-file-flag t)))
82 (define-key map "\C-@" 'kkc-first-char-only) 85 (define-key map "\C-@" 'kkc-first-char-only)
83 (define-key map "\C-n" 'kkc-next) 86 (define-key map "\C-n" 'kkc-next)
84 (define-key map "\C-p" 'kkc-prev) 87 (define-key map "\C-p" 'kkc-prev)
85 (define-key map "\C-i" 'kkc-shorter) 88 (define-key map "\C-i" 'kkc-shorter)
86 (define-key map "\C-o" 'kkc-longer) 89 (define-key map "\C-o" 'kkc-longer)
90 (define-key map "I" 'kkc-shorter-conversion)
91 (define-key map "O" 'kkc-longer-phrase)
87 (define-key map "\C-c" 'kkc-cancel) 92 (define-key map "\C-c" 'kkc-cancel)
88 (define-key map "\C-?" 'kkc-cancel) 93 (define-key map "\C-?" 'kkc-cancel)
89 (define-key map "\C-f" 'kkc-next-phrase) 94 (define-key map "\C-f" 'kkc-next-phrase)
90 (define-key map "K" 'kkc-katakana) 95 (define-key map "K" 'kkc-katakana)
91 (define-key map "H" 'kkc-hiragana) 96 (define-key map "H" 'kkc-hiragana)
145 ;; LEN. If no conversion is found in the dictionary, don't change 150 ;; LEN. If no conversion is found in the dictionary, don't change
146 ;; kkc-current-conversions and return nil. 151 ;; kkc-current-conversions and return nil.
147 ;; Postfixes are handled only if POSTFIX is non-nil. 152 ;; Postfixes are handled only if POSTFIX is non-nil.
148 (defun kkc-lookup-key (len &optional postfix prefer-noun) 153 (defun kkc-lookup-key (len &optional postfix prefer-noun)
149 ;; At first, prepare cache data if any. 154 ;; At first, prepare cache data if any.
150 (if (not kkc-init-file-flag) 155 (unless kkc-init-file-flag
151 (progn 156 (setq kkc-init-file-flag t
152 (setq kkc-init-file-flag t) 157 kkc-lookup-cache nil)
153 (add-hook 'kill-emacs-hook 'kkc-save-init-file) 158 (add-hook 'kill-emacs-hook 'kkc-save-init-file)
154 (if (file-readable-p kkc-init-file-name) 159 (if (file-readable-p kkc-init-file-name)
155 (condition-case nil 160 (condition-case nil
156 (load-file "~/.kkcrc") 161 (load-file kkc-init-file-name)
157 (error (message "Invalid data in %s" kkc-init-file-name) 162 (kkc-error "Invalid data in %s" kkc-init-file-name))))
158 (ding)))))) 163 (or (and (nested-alist-p kkc-lookup-cache)
164 (eq (car kkc-lookup-cache) kkc-lookup-cache-tag))
165 (setq kkc-lookup-cache (list kkc-lookup-cache-tag)
166 kkc-init-file-flag 'kkc-lookup-cache))
159 (let ((entry (lookup-nested-alist kkc-current-key kkc-lookup-cache len 0 t))) 167 (let ((entry (lookup-nested-alist kkc-current-key kkc-lookup-cache len 0 t)))
160 (if (consp (car entry)) 168 (if (consp (car entry))
161 (setq kkc-length-converted len 169 (setq kkc-length-converted len
162 kkc-current-conversions-width nil 170 kkc-current-conversions-width nil
163 kkc-current-conversions (car entry)) 171 kkc-current-conversions (car entry))
177 (if (= len 1) 185 (if (= len 1)
178 (setq kkc-length-converted 1 186 (setq kkc-length-converted 1
179 kkc-current-conversions-width nil 187 kkc-current-conversions-width nil
180 kkc-current-conversions (cons 0 nil))))))) 188 kkc-current-conversions (cons 0 nil)))))))
181 189
190 (put 'kkc-error 'error-conditions '(kkc-error error))
191 (defun kkc-error (&rest args)
192 (signal 'kkc-error (apply 'format args)))
193
182 (defvar kkc-converting nil) 194 (defvar kkc-converting nil)
183 195
184 ;;;###autoload 196 ;;;###autoload
185 (defun kkc-region (from to) 197 (defun kkc-region (from to)
186 "Convert Kana string in the current region to Kanji-Kana mixed string. 198 "Convert Kana string in the current region to Kanji-Kana mixed string.
205 217
206 (setq kkc-current-key (string-to-vector kkc-original-kana)) 218 (setq kkc-current-key (string-to-vector kkc-original-kana))
207 (setq kkc-length-head (length kkc-current-key)) 219 (setq kkc-length-head (length kkc-current-key))
208 (setq kkc-length-converted 0) 220 (setq kkc-length-converted 0)
209 221
210 ;; At first convert the region to the first candidate.
211 (let ((first t))
212 (while (not (kkc-lookup-key kkc-length-head nil first))
213 (setq kkc-length-head (1- kkc-length-head)
214 first nil))
215 (goto-char to)
216 (kkc-update-conversion 'all))
217
218 ;; Then, ask users to selecte a desirable conversion.
219 (unwind-protect 222 (unwind-protect
223 ;; At first convert the region to the first candidate.
220 (let ((current-input-method-title kkc-input-method-title) 224 (let ((current-input-method-title kkc-input-method-title)
221 (input-method-function nil)) 225 (input-method-function nil)
226 (first t))
227 (while (not (kkc-lookup-key kkc-length-head nil first))
228 (setq kkc-length-head (1- kkc-length-head)
229 first nil))
230 (goto-char to)
231 (kkc-update-conversion 'all)
232
233 ;; Then, ask users to selecte a desirable conversion.
222 (force-mode-line-update) 234 (force-mode-line-update)
223 (setq kkc-converting t) 235 (setq kkc-converting t)
224 (while kkc-converting 236 (while kkc-converting
225 (let* ((echo-keystrokes 0) 237 (let* ((echo-keystrokes 0)
226 (keyseq (read-key-sequence nil)) 238 (keyseq (read-key-sequence nil))
354 366
355 (defun kkc-shorter () 367 (defun kkc-shorter ()
356 "Make the Kana string to be converted shorter." 368 "Make the Kana string to be converted shorter."
357 (interactive) 369 (interactive)
358 (if (<= kkc-length-head 1) 370 (if (<= kkc-length-head 1)
359 (error "Can't be shorter") 371 (kkc-error "Can't be shorter"))
360 (setq kkc-length-head (1- kkc-length-head)) 372 (setq kkc-length-head (1- kkc-length-head))
361 (if (> kkc-length-converted kkc-length-head) 373 (if (> kkc-length-converted kkc-length-head)
362 (let ((len kkc-length-head)) 374 (let ((len kkc-length-head))
363 (setq kkc-length-converted 0) 375 (setq kkc-length-converted 0)
364 (while (not (kkc-lookup-key len)) 376 (while (not (kkc-lookup-key len))
365 (setq len (1- len))))) 377 (setq len (1- len)))))
366 (kkc-update-conversion 'all))) 378 (kkc-update-conversion 'all))
367 379
368 (defun kkc-longer () 380 (defun kkc-longer ()
369 "Make the Kana string to be converted longer." 381 "Make the Kana string to be converted longer."
370 (interactive) 382 (interactive)
371 (if (>= kkc-length-head (length kkc-current-key)) 383 (if (>= kkc-length-head (length kkc-current-key))
372 (error "Can't be longer") 384 (kkc-error "Can't be longer"))
373 (setq kkc-length-head (1+ kkc-length-head)) 385 (setq kkc-length-head (1+ kkc-length-head))
374 ;; This time, try also entries with postfixes. 386 ;; This time, try also entries with postfixes.
375 (kkc-lookup-key kkc-length-head 'postfix) 387 (kkc-lookup-key kkc-length-head 'postfix)
376 (kkc-update-conversion 'all))) 388 (kkc-update-conversion 'all))
389
390 (defun kkc-shorter-conversion ()
391 "Make the Kana string to be converted shorter."
392 (interactive)
393 (if (<= kkc-length-converted 1)
394 (kkc-error "Can't be shorter"))
395 (let ((len (1- kkc-length-converted)))
396 (setq kkc-length-converted 0)
397 (while (not (kkc-lookup-key len))
398 (setq len (1- len))))
399 (kkc-update-conversion 'all))
400
401 (defun kkc-longer-phrase ()
402 "Make the current phrase (BUNSETSU) longer without looking up dictionary."
403 (interactive)
404 (if (>= kkc-length-head (length kkc-current-key))
405 (kkc-error "Can't be longer"))
406 (setq kkc-length-head (1+ kkc-length-head))
407 (kkc-update-conversion 'all))
377 408
378 (defun kkc-next-phrase () 409 (defun kkc-next-phrase ()
379 "Fix the currently converted string and try to convert the remaining string." 410 "Fix the currently converted string and try to convert the remaining string."
380 (interactive) 411 (interactive)
381 (if (>= kkc-length-head (length kkc-current-key)) 412 (if (>= kkc-length-head (length kkc-current-key))
436 "Show list of available conversions in echo area with index numbers. 467 "Show list of available conversions in echo area with index numbers.
437 If the list is already shown, show the next group of conversions, 468 If the list is already shown, show the next group of conversions,
438 and change the current conversion to the first one in the group." 469 and change the current conversion to the first one in the group."
439 (interactive) 470 (interactive)
440 (if (< (length kkc-current-conversions) 3) 471 (if (< (length kkc-current-conversions) 3)
441 (error "No alternative")) 472 (kkc-error "No alternative"))
442 (if kkc-current-conversions-width 473 (if kkc-current-conversions-width
443 (let ((next-idx (aref (aref kkc-current-conversions-width 0) 1))) 474 (let ((next-idx (aref (aref kkc-current-conversions-width 0) 1)))
444 (if (< next-idx (length kkc-current-conversions-width)) 475 (if (< next-idx (length kkc-current-conversions-width))
445 (setcar kkc-current-conversions next-idx) 476 (setcar kkc-current-conversions next-idx)
446 (setcar kkc-current-conversions 1)) 477 (setcar kkc-current-conversions 1))
453 "Show list of available conversions in echo area with index numbers. 484 "Show list of available conversions in echo area with index numbers.
454 If the list is already shown, show the previous group of conversions, 485 If the list is already shown, show the previous group of conversions,
455 and change the current conversion to the last one in the group." 486 and change the current conversion to the last one in the group."
456 (interactive) 487 (interactive)
457 (if (< (length kkc-current-conversions) 3) 488 (if (< (length kkc-current-conversions) 3)
458 (error "No alternative")) 489 (kkc-error "No alternative"))
459 (if kkc-current-conversions-width 490 (if kkc-current-conversions-width
460 (let ((this-idx (aref (aref kkc-current-conversions-width 0) 0))) 491 (let ((this-idx (aref (aref kkc-current-conversions-width 0) 0)))
461 (if (> this-idx 1) 492 (if (> this-idx 1)
462 (setcar kkc-current-conversions (1- this-idx)) 493 (setcar kkc-current-conversions (1- this-idx))
463 (setcar kkc-current-conversions 494 (setcar kkc-current-conversions