(titdic-convert): Set
[bpt/emacs.git] / lisp / international / kkc.el
index ba256b4..7d2baf8 100644 (file)
@@ -1,7 +1,7 @@
 ;;; kkc.el --- Kana Kanji converter
 
-;; Copyright (C) 1995 Free Software Foundation, Inc.
 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
+;; Licensed to the Free Software Foundation.
 
 ;; Keywords: mule, multilingual, Japanese, SKK
 
@@ -58,7 +58,7 @@ This string is shown at mode line when users are in KKC mode.")
   "Save initial setup code for KKC to a file specified by `kkc-init-file-name'"
   (if (and kkc-init-file-flag
           (not (eq kkc-init-file-flag t)))
-      (let ((coding-system-for-write 'iso-2022-7))
+      (let ((coding-system-for-write 'iso-2022-7bit))
        (write-region (format "(setq kkc-lookup-cache '%S)\n" kkc-lookup-cache)
                      nil
                      kkc-init-file-name))))
@@ -71,9 +71,6 @@ This string is shown at mode line when users are in KKC mode.")
 (defvar kkc-mode-map
   (let ((map (make-keymap))
        (i 0))
-    (while (< i ? )
-      (define-key map (char-to-string i) 'undefined)
-      (setq i (1+ i)))
     (while (< i 128)
       (define-key map (char-to-string i) 'kkc-non-kkc-command)
       (setq i (1+ i)))
@@ -99,10 +96,16 @@ This string is shown at mode line when users are in KKC mode.")
     (define-key map "H" 'kkc-hiragana)
     (define-key map "l" 'kkc-show-conversion-list-or-next-group)
     (define-key map "L" 'kkc-show-conversion-list-or-prev-group)
-    (define-key map [?\C-\ ] 'kkc-first-char-only)
+    (define-key map [?\C- ] 'kkc-first-char-only)
     (define-key map [delete] 'kkc-cancel)
     (define-key map [return] 'kkc-terminate)
-    (append map '((t . kkc-non-kkc-command))))
+    (let ((meta-map (make-sparse-keymap)))
+      (define-key map (char-to-string meta-prefix-char) meta-map)
+      (define-key map [escape] meta-map))
+    (define-key map (vector meta-prefix-char t) 'kkc-non-kkc-command)
+    ;; At last, define default key binding.
+    (define-key map [t] 'kkc-non-kkc-command)
+    map)
   "Keymap for KKC (Kana Kanji Conversion) mode.")
 
 (defun kkc-mode ()
@@ -167,7 +170,7 @@ Commands:
 ;; LEN.  If no conversion is found in the dictionary, don't change
 ;; kkc-current-conversions and return nil.
 ;; Postfixes are handled only if POSTFIX is non-nil. 
-(defun kkc-lookup-key (len &optional postfix)
+(defun kkc-lookup-key (len &optional postfix prefer-noun)
   ;; At first, prepare cache data if any.
   (if (not kkc-init-file-flag)
       (progn
@@ -183,7 +186,7 @@ Commands:
        (setq kkc-length-converted len
              kkc-current-conversions-width nil
              kkc-current-conversions (car entry))
-      (setq entry (skkdic-lookup-key kkc-current-key len postfix))
+      (setq entry (skkdic-lookup-key kkc-current-key len postfix prefer-noun))
       (if entry
          (progn
            (setq kkc-length-converted len
@@ -202,10 +205,11 @@ Commands:
                  kkc-current-conversions (cons 0 nil)))))))
 
 ;;;###autoload
-(defun kkc-region (from to)
+(defun kkc-region (from to &optional kkc-mode-exit-function)
   "Convert Kana string in the current region to Kanji-Kana mixed string.
 After one candidate of conversion is shown in the region, users are
-put in KKC major mode to select a desirable conversion."
+put in KKC major mode to select a desirable conversion.
+Optional arg KKC-MODE-EXIT-FUNCTION if non-nil is called on exiting KKC mode."
   (interactive "r")
   (setq kkc-original-kana (buffer-substring from to))
   (goto-char from)
@@ -222,7 +226,7 @@ put in KKC major mode to select a desirable conversion."
 
   ;; After updating the conversion region with the first candidate of
   ;; conversion, jump into a recursive editing environment with KKC
-  ;; mode .
+  ;; mode.
   (let ((overriding-local-map nil)
        (previous-local-map (current-local-map))
        (minor-mode-alist nil)
@@ -230,12 +234,14 @@ put in KKC major mode to select a desirable conversion."
        (current-input-method-title kkc-input-method-title)
        major-mode mode-name)
     (unwind-protect
-       (progn
+       (let (len)
          (setq kkc-canceled nil)
          (setq kkc-current-key (string-to-vector kkc-original-kana))
          (setq kkc-length-head (length kkc-current-key))
+         (setq len kkc-length-head)
          (setq kkc-length-converted 0)
-         (while (not (kkc-lookup-key kkc-length-head))
+         (while (not (kkc-lookup-key kkc-length-head nil
+                                     (< kkc-length-head len)))
            (setq kkc-length-head (1- kkc-length-head)))
          (goto-char to)
          (kkc-update-conversion 'all)
@@ -244,8 +250,11 @@ put in KKC major mode to select a desirable conversion."
       (goto-char (overlay-end kkc-overlay-tail))
       (delete-overlay kkc-overlay-head)
       (delete-overlay kkc-overlay-tail)
-      (use-local-map previous-local-map)))
-  kkc-canceled)
+      (use-local-map previous-local-map)
+      (if (and kkc-mode-exit-function
+              (fboundp kkc-mode-exit-function))
+         (funcall kkc-mode-exit-function (if kkc-canceled
+                                             (cons kkc-canceled (point))))))))
 
 (defun kkc-terminate ()
   "Exit from KKC mode by fixing the current conversion."
@@ -256,7 +265,9 @@ put in KKC major mode to select a desirable conversion."
   "Exit from KKC mode by fixing the current conversion.
 After that, handle the event which invoked this command."
   (interactive)
-  (setq unread-command-events (list last-input-event))
+  (let* ((key (this-command-keys))
+        (keylist (listify-key-sequence key)))
+    (setq unread-command-events (append keylist unread-command-events)))
   (kkc-terminate))
 
 (defun kkc-cancel ()
@@ -400,6 +411,7 @@ After that, handle the event which invoked this command."
        (kkc-terminate)
       (let ((newkey (make-vector kkc-length-head 0))
            (idx (- (length kkc-current-key) kkc-length-head))
+           (len kkc-length-head)
            (i 0))
        ;; For the moment, (setq kkc-original-kana (concat newkey))
        ;; doesn't work.
@@ -411,7 +423,8 @@ After that, handle the event which invoked this command."
          (setq i (1+ i)))
        (setq kkc-current-key newkey)
        (setq kkc-length-converted 0)
-       (while (and (not (kkc-lookup-key kkc-length-head))
+       (while (and (not (kkc-lookup-key kkc-length-head nil
+                                        (< kkc-length-head len)))
                    (> kkc-length-head 1))
          (setq kkc-length-head (1- kkc-length-head)))
        (let ((pos (point))