]> code.delx.au - gnu-emacs/blobdiff - lisp/international/kkc.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / international / kkc.el
index 6338dc10ed8a2e88e142aa14106b1f22ff012917..d671d2900c2d9a81b81efbc56c03e70d5be44d40 100644 (file)
@@ -1,9 +1,13 @@
-;;; kkc.el --- Kana Kanji converter
+;;; kkc.el --- Kana Kanji converter    -*- coding: iso-2022-7bit; -*-
 
-;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
-;; Licensed to the Free Software Foundation.
+;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007  Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007
+;;   National Institute of Advanced Industrial Science and Technology (AIST)
+;;   Registration Number H14PRO021
 
-;; Keywords: mule, multilingual, Japanese, SKK
+;; Keywords: mule, multilingual, Japanese
 
 ;; This file is part of GNU Emacs.
 
@@ -19,8 +23,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -33,7 +37,7 @@
 
 ;;; Code:
 
-(require 'skkdic-utl)
+(require 'ja-dic-utl)
 
 (defvar kkc-input-method-title "\e$B4A\e(B"
   "String denoting KKC input method.
@@ -61,7 +65,8 @@ 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-7bit))
+      (let ((coding-system-for-write 'iso-2022-7bit)
+           (print-length nil))
        (write-region (format "(setq kkc-lookup-cache '%S)\n" kkc-lookup-cache)
                      nil
                      kkc-init-file-name))))
@@ -131,8 +136,19 @@ This string is shown at mode line when users are in KKC mode.")
 ;; `kkc-current-conversion'.
 (defvar kkc-current-conversions-width nil)
 
-(defvar kkc-show-conversion-list-count 4
-  "Count of successive `kkc-next' or `kkc-prev' to show conversion list.")
+(defcustom kkc-show-conversion-list-count 4
+  "*Count of successive `kkc-next' or `kkc-prev' to show conversion list.
+When you type SPC or C-p successively this count while using the input
+method `japanese', the conversion candidates are shown in the echo
+area while indicating the current selection by `<N>'."
+  :group 'mule
+  :type 'integer)
+
+;; Count of successive invocations of `kkc-next'.
+(defvar kkc-next-count nil)
+
+;; Count of successive invocations of `kkc-prev'.
+(defvar kkc-prev-count nil)
 
 ;; Provided that `kkc-current-key' is [A B C D E F G H I], the current
 ;; conversion target is [A B C D E F], and the sequence of which
@@ -152,11 +168,11 @@ This string is shown at mode line when users are in KKC mode.")
 ;; Cursor type (`box' or `bar') of the current frame.
 (defvar kkc-cursor-type nil)
 
-;; Lookup SKK dictionary to set list of conversions in
+;; Lookup Japanese dictionary to set list of conversions in
 ;; kkc-current-conversions for key sequence kkc-current-key of length
 ;; 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. 
+;; Postfixes are handled only if POSTFIX is non-nil.
 (defun kkc-lookup-key (len &optional postfix prefer-noun)
   ;; At first, prepare cache data if any.
   (unless kkc-init-file-flag
@@ -200,6 +216,14 @@ This string is shown at mode line when users are in KKC mode.")
 
 (defvar kkc-converting nil)
 
+;;;###autoload
+(defvar kkc-after-update-conversion-functions nil
+  "Functions to run after a conversion is selected in `japanese' input method.
+With this input method, a user can select a proper conversion from
+candidate list.  Each time he changes the selection, functions in this
+list are called with two arguments; starting and ending buffer
+positions that contains the current selection.")
+
 ;;;###autoload
 (defun kkc-region (from to)
   "Convert Kana string in the current region to Kanji-Kana mixed string.
@@ -230,29 +254,46 @@ and the return value is the length of the conversion."
       ;; At first convert the region to the first candidate.
       (let ((current-input-method-title kkc-input-method-title)
            (input-method-function nil)
+           (modified-p (buffer-modified-p))
            (first t))
        (while (not (kkc-lookup-key kkc-length-head nil first))
          (setq kkc-length-head (1- kkc-length-head)
                first nil))
        (goto-char to)
        (kkc-update-conversion 'all)
+       (setq kkc-next-count 1 kkc-prev-count 0)
+       (if (and (>= kkc-next-count kkc-show-conversion-list-count)
+                (>= (length kkc-current-conversions) 3))
+           (kkc-show-conversion-list-or-next-group))
 
        ;; Then, ask users to select a desirable conversion.
        (force-mode-line-update)
        (setq kkc-converting t)
+       ;; Hide "... loaded" message.
+       (message nil)
        (while kkc-converting
+         (set-buffer-modified-p modified-p)
          (let* ((overriding-terminal-local-map kkc-keymap)
                 (help-char nil)
                 (keyseq (read-key-sequence nil))
                 (cmd (lookup-key kkc-keymap keyseq)))
            (if (commandp cmd)
                (condition-case err
-                   (call-interactively cmd)
+                   (progn
+                     (cond ((eq cmd 'kkc-next)
+                            (setq kkc-next-count (1+ kkc-next-count)
+                                  kkc-prev-count 0))
+                           ((eq cmd 'kkc-prev)
+                            (setq kkc-prev-count (1+ kkc-prev-count)
+                                  kkc-next-count 0))
+                           (t
+                            (setq kkc-next-count 0 kkc-prev-count 0)))
+                     (call-interactively cmd))
                  (kkc-error (message "%s" (cdr err)) (beep)))
              ;; KEYSEQ is not defined in KKC keymap.
              ;; Let's put the event back.
              (setq unread-input-method-events
-                   (append (string-to-list keyseq)
+                   (append (string-to-list (this-single-command-raw-keys))
                            unread-input-method-events))
              (kkc-terminate))))
 
@@ -286,15 +327,9 @@ and the return value is the length of the conversion."
   (delete-region (point) (overlay-end kkc-overlay-tail))
   (kkc-terminate))
 
-;; Count of successive invocations of `kkc-next'.
-(defvar kkc-next-count nil)
-
 (defun kkc-next ()
   "Select the next candidate of conversion."
   (interactive)
-  (if (eq this-command last-command)
-      (setq kkc-next-count (1+ kkc-next-count))
-    (setq kkc-next-count 1))
   (let ((idx (1+ (car kkc-current-conversions))))
     (if (< idx 0)
        (setq idx 1))
@@ -311,15 +346,9 @@ and the return value is the length of the conversion."
        (kkc-show-conversion-list-update))
     (kkc-update-conversion)))
 
-;; Count of successive invocations of `kkc-next'.
-(defvar kkc-prev-count nil)
-
 (defun kkc-prev ()
   "Select the previous candidate of conversion."
   (interactive)
-  (if (eq this-command last-command)
-      (setq kkc-prev-count (1+ kkc-prev-count))
-    (setq kkc-prev-count 1))
   (let ((idx (1- (car kkc-current-conversions))))
     (if (< idx 0)
        (setq idx (1- (length kkc-current-conversions))))
@@ -524,13 +553,7 @@ and change the current conversion to the last one in the group."
          ;; The currently selected conversion is after the list shown
          ;; previously.  We start calculation of message width from
          ;; the conversion next of TO.
-         (setq this-idx next-idx msg nil)
-       ;; The current conversion is in MSG.  Just clear brackets
-       ;; around index number.
-       (if (string-match "<.>" msg)
-           (progn
-             (aset msg (match-beginning 0) ?\ )
-             (aset msg (1- (match-end 0)) ?\ )))))
+         (setq this-idx next-idx msg nil)))
     (if (not msg)
        (let ((len (length kkc-current-conversions))
              (max-width (window-width (minibuffer-window)))
@@ -539,7 +562,9 @@ and change the current conversion to the last one in the group."
              (idx this-idx)
              (max-items (length kkc-show-conversion-list-index-chars))
              l)
-         (while (< idx current-idx)
+         ;; Set THIS-IDX to the first index of conversion to be shown
+         ;; in MSG, and reflect it in kkc-current-conversions-width.
+         (while (<= idx current-idx)
            (if (and (<= (+ width (aref width-table idx)) max-width)
                     (< (- idx this-idx) max-items))
                (setq width (+ width (aref width-table idx)))
@@ -547,6 +572,9 @@ and change the current conversion to the last one in the group."
            (setq idx (1+ idx)
                  l (cdr l)))
          (aset first-slot 0 this-idx)
+         ;; Set NEXT-IDX to the next index of the last conversion
+         ;; shown in MSG, and reflect it in
+         ;; kkc-current-conversions-width.
          (while (and (< idx len)
                      (<= (+ width (aref width-table idx)) max-width)
                      (< (- idx this-idx) max-items))
@@ -555,27 +583,37 @@ and change the current conversion to the last one in the group."
                  l (cdr l)))
          (aset first-slot 1 (setq next-idx idx))
          (setq l (nthcdr this-idx kkc-current-conversions))
-         (setq msg "")
-         (setq idx this-idx)
+         (setq msg (format " %c %s"
+                           (aref kkc-show-conversion-list-index-chars 0)
+                           (propertize (car l)
+                                       'kkc-conversion-index this-idx))
+               idx (1+ this-idx)
+               l (cdr l))
          (while (< idx next-idx)
-           (setq msg (format "%s %c %s "
+           (setq msg (format "%s  %c %s"
                              msg
                              (aref kkc-show-conversion-list-index-chars
                                    (- idx this-idx))
-                             (car l)))
-           (setq idx (1+ idx)
+                             (propertize (car l)
+                                         'kkc-conversion-index idx))
+                 idx (1+ idx)
                  l (cdr l)))
          (aset first-slot 2 msg)))
+
+    ;; Highlight the current conversion.
     (if (> current-idx 0)
-       (progn
-         ;; Highlight the current conversion by brackets.
-         (string-match (format " \\(%c\\) "
-                               (aref kkc-show-conversion-list-index-chars
-                                     (- current-idx this-idx)))
-                       msg)
-         (aset msg (match-beginning 0) ?<)
-         (aset msg (1- (match-end 0)) ?>)))
-    (message "%s" msg)))
+       (let ((pos 3)
+             (limit (length msg)))
+         (remove-text-properties 0 (length msg) '(face nil) msg)
+         (while (not (eq (get-text-property pos 'kkc-conversion-index msg)
+                         current-idx))
+           (setq pos (next-single-property-change pos 'kkc-conversion-index
+                                                  msg limit)))
+         (put-text-property pos (next-single-property-change
+                                 pos 'kkc-conversion-index msg limit)
+                            'face 'highlight msg)))
+    (let ((message-log-max nil))
+      (message "%s" msg))))
 
 ;; Update the conversion area with the latest conversion selected.
 ;; ALL if non nil means to update the whole area, else update only
@@ -613,9 +651,14 @@ and change the current conversion to the last one in the group."
          (move-overlay kkc-overlay-head
                        (overlay-start kkc-overlay-head) pos)
          (delete-region (point) (overlay-end kkc-overlay-tail)))))
-  (goto-char (overlay-end kkc-overlay-tail)))
+  (unwind-protect
+      (run-hook-with-args 'kkc-after-update-conversion-functions
+                         (overlay-start kkc-overlay-head)
+                         (overlay-end kkc-overlay-head))
+    (goto-char (overlay-end kkc-overlay-tail))))
 
 ;;
 (provide 'kkc)
 
-;; kkc.el ends here
+;;; arch-tag: 3cbfd56e-74e6-4f60-bb46-ba7c2d366fbf
+;;; kkc.el ends here