Mercurial > emacs
annotate lisp/international/kkc.el @ 22841:330629423307
(push_key_description): Output multibyte char as itself.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 28 Jul 1998 23:10:44 +0000 |
parents | 8f9d4edebbdd |
children | 5bfe86125303 |
rev | line source |
---|---|
17315
a3ca5e15c82a
Fix the format of the first line.
Kenichi Handa <handa@m17n.org>
parents:
17082
diff
changeset
|
1 ;;; kkc.el --- Kana Kanji converter |
17052 | 2 |
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. | |
18377
8b4a66c66dd6
Change copyright notice.
Richard M. Stallman <rms@gnu.org>
parents:
18198
diff
changeset
|
4 ;; Licensed to the Free Software Foundation. |
17052 | 5 |
6 ;; Keywords: mule, multilingual, Japanese, SKK | |
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 | |
17071 | 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. | |
17052 | 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 | |
36 (require 'skkdic-utl) | |
37 | |
38 (defvar kkc-input-method-title "漢" | |
39 "String denoting KKC input method. | |
40 This 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. | |
55 (defvar kkc-lookup-cache '(kkc-lookup-cache)) | |
56 | |
57 (defun kkc-save-init-file () | |
58 "Save initial setup code for KKC to a file specified by `kkc-init-file-name'" | |
59 (if (and kkc-init-file-flag | |
60 (not (eq kkc-init-file-flag t))) | |
18198
8286b2dd4db6
(kkc-save-init-file): Coding system name changed from
Kenichi Handa <handa@m17n.org>
parents:
17315
diff
changeset
|
61 (let ((coding-system-for-write 'iso-2022-7bit)) |
17052 | 62 (write-region (format "(setq kkc-lookup-cache '%S)\n" kkc-lookup-cache) |
63 nil | |
64 kkc-init-file-name)))) | |
65 | |
66 ;; Sequence of characters to be used for indexes for shown list. The | |
67 ;; Nth character is for the Nth conversion in the list currently shown. | |
68 (defvar kkc-show-conversion-list-index-chars | |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
69 "1234567890") |
17052 | 70 |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
71 (defvar kkc-keymap |
17052 | 72 (let ((map (make-keymap)) |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
73 (len (length kkc-show-conversion-list-index-chars)) |
17052 | 74 (i 0)) |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
75 (while (< i len) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
76 (define-key map |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
77 (char-to-string (aref kkc-show-conversion-list-index-chars i)) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
78 'kkc-select-from-list) |
17052 | 79 (setq i (1+ i))) |
80 (define-key map " " 'kkc-next) | |
81 (define-key map "\r" 'kkc-terminate) | |
82 (define-key map "\C-@" 'kkc-first-char-only) | |
83 (define-key map "\C-n" 'kkc-next) | |
84 (define-key map "\C-p" 'kkc-prev) | |
85 (define-key map "\C-i" 'kkc-shorter) | |
86 (define-key map "\C-o" 'kkc-longer) | |
87 (define-key map "\C-c" 'kkc-cancel) | |
88 (define-key map "\C-?" 'kkc-cancel) | |
89 (define-key map "\C-f" 'kkc-next-phrase) | |
90 (define-key map "K" 'kkc-katakana) | |
91 (define-key map "H" 'kkc-hiragana) | |
92 (define-key map "l" 'kkc-show-conversion-list-or-next-group) | |
93 (define-key map "L" 'kkc-show-conversion-list-or-prev-group) | |
20638
c3360a392ae4
Change "?\C-\ " to "?\C- " because "\ " is changed
Kenichi Handa <handa@m17n.org>
parents:
19931
diff
changeset
|
94 (define-key map [?\C- ] 'kkc-first-char-only) |
17052 | 95 (define-key map [delete] 'kkc-cancel) |
96 (define-key map [return] 'kkc-terminate) | |
19869
3c4025f3be8f
(kkc-mode-map): Bind meta-prefix-char to a
Kenichi Handa <handa@m17n.org>
parents:
19406
diff
changeset
|
97 map) |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
98 "Keymap for KKC (Kana Kanji Converter).") |
17052 | 99 |
100 ;;; Internal variables used in KKC. | |
101 | |
102 ;; The current Kana string to be converted. | |
103 (defvar kkc-original-kana nil) | |
104 | |
105 ;; The current key sequence (vector of Kana characters) generated from | |
106 ;; `kkc-original-kana'. | |
107 (defvar kkc-current-key nil) | |
108 | |
109 ;; List of the current conversions for `kkc-current-key'. | |
110 (defvar kkc-current-conversions nil) | |
111 | |
112 ;; Vector of the same length as `kkc-current-conversion'. The first | |
113 ;; element is a vector of: | |
114 ;; o index number of the first conversion shown previously, | |
115 ;; o index number of a conversion next of the last one shown previously, | |
116 ;; o the shown string itself. | |
117 ;; The remaining elements are widths (including columns for index | |
118 ;; numbers) of conversions stored in the same order as in | |
119 ;; `kkc-current-conversion'. | |
120 (defvar kkc-current-conversions-width nil) | |
121 | |
122 (defvar kkc-show-conversion-list-count 4 | |
123 "Count of successive `kkc-next' or `kkc-prev' to show conversion list.") | |
124 | |
125 ;; Provided that `kkc-current-key' is [A B C D E F G H I], the current | |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
126 ;; conversion target is [A B C D E F], and the sequence of which |
17052 | 127 ;; conversion is found is [A B C D]: |
128 ;; | |
129 ;; A B C D E F G H I | |
130 ;; kkc-overlay-head (black): |<--------->| | |
131 ;; kkc-overlay-tail (underline): |<------->| | |
132 ;; kkc-length-head: |<--------->| | |
133 ;; kkc-length-converted: |<----->| | |
134 ;; | |
135 (defvar kkc-overlay-head nil) | |
136 (defvar kkc-overlay-tail nil) | |
137 (defvar kkc-length-head nil) | |
138 (defvar kkc-length-converted nil) | |
139 | |
140 ;; Cursor type (`box' or `bar') of the current frame. | |
141 (defvar kkc-cursor-type nil) | |
142 | |
143 ;; Lookup SKK dictionary to set list of conversions in | |
144 ;; kkc-current-conversions for key sequence kkc-current-key of length | |
145 ;; LEN. If no conversion is found in the dictionary, don't change | |
146 ;; kkc-current-conversions and return nil. | |
147 ;; Postfixes are handled only if POSTFIX is non-nil. | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
148 (defun kkc-lookup-key (len &optional postfix prefer-noun) |
17052 | 149 ;; At first, prepare cache data if any. |
150 (if (not kkc-init-file-flag) | |
151 (progn | |
152 (setq kkc-init-file-flag t) | |
153 (add-hook 'kill-emacs-hook 'kkc-save-init-file) | |
154 (if (file-readable-p kkc-init-file-name) | |
155 (condition-case nil | |
156 (load-file "~/.kkcrc") | |
157 (error (message "Invalid data in %s" kkc-init-file-name) | |
158 (ding)))))) | |
159 (let ((entry (lookup-nested-alist kkc-current-key kkc-lookup-cache len 0 t))) | |
160 (if (consp (car entry)) | |
161 (setq kkc-length-converted len | |
162 kkc-current-conversions-width nil | |
163 kkc-current-conversions (car entry)) | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
164 (setq entry (skkdic-lookup-key kkc-current-key len postfix prefer-noun)) |
17052 | 165 (if entry |
166 (progn | |
167 (setq kkc-length-converted len | |
168 kkc-current-conversions-width nil | |
169 kkc-current-conversions (cons 1 entry)) | |
170 (if postfix | |
171 ;; Store this conversions in the cache. | |
172 (progn | |
173 (set-nested-alist kkc-current-key kkc-current-conversions | |
174 kkc-lookup-cache kkc-length-converted) | |
175 (setq kkc-init-file-flag 'kkc-lookup-cache))) | |
176 t) | |
177 (if (= len 1) | |
178 (setq kkc-length-converted 1 | |
179 kkc-current-conversions-width nil | |
180 kkc-current-conversions (cons 0 nil))))))) | |
181 | |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
182 (defvar kkc-converting nil) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
183 |
17052 | 184 ;;;###autoload |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
185 (defun kkc-region (from to) |
17052 | 186 "Convert Kana string in the current region to Kanji-Kana mixed string. |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
187 Users can select a desirable conversion interactively. |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
188 When called from a program, expects two arguments, |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
189 positions FROM and TO (integers or markers) specifying the target region. |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
190 When it returns, the point is at the tail of the selected conversion, |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
191 and the return value is the length of the conversion." |
17052 | 192 (interactive "r") |
193 (setq kkc-original-kana (buffer-substring from to)) | |
194 (goto-char from) | |
195 | |
196 ;; Setup overlays. | |
197 (if (overlayp kkc-overlay-head) | |
198 (move-overlay kkc-overlay-head from to) | |
199 (setq kkc-overlay-head (make-overlay from to nil nil t)) | |
200 (overlay-put kkc-overlay-head 'face 'highlight)) | |
201 (if (overlayp kkc-overlay-tail) | |
202 (move-overlay kkc-overlay-tail to to) | |
203 (setq kkc-overlay-tail (make-overlay to to nil nil t)) | |
204 (overlay-put kkc-overlay-tail 'face 'underline)) | |
205 | |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
206 (setq kkc-current-key (string-to-vector kkc-original-kana)) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
207 (setq kkc-length-head (length kkc-current-key)) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
208 (setq kkc-length-converted 0) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
209 |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
210 ;; At first convert the region to the first candidate. |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
211 (let ((first t)) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
212 (while (not (kkc-lookup-key kkc-length-head nil first)) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
213 (setq kkc-length-head (1- kkc-length-head) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
214 first nil)) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
215 (goto-char to) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
216 (kkc-update-conversion 'all)) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
217 |
22816
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
218 ;; Then, ask users to selecte a desirable conversion. |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
219 (unwind-protect |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
220 (let ((current-input-method-title kkc-input-method-title) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
221 (input-method-function nil)) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
222 (force-mode-line-update) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
223 (setq kkc-converting t) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
224 (while kkc-converting |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
225 (let* ((echo-keystrokes 0) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
226 (keyseq (read-key-sequence nil)) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
227 (cmd (lookup-key kkc-keymap keyseq))) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
228 (if (commandp cmd) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
229 (condition-case err |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
230 (call-interactively cmd) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
231 (kkc-error (message "%s" (cdr err)) (beep))) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
232 ;; KEYSEQ is not defined in KKC keymap. |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
233 ;; Let's put the event back. |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
234 (setq unread-input-method-events |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
235 (append (string-to-list keyseq) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
236 unread-input-method-events)) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
237 (kkc-terminate)))) |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
238 |
22816
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
239 (force-mode-line-update) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
240 (goto-char (overlay-end kkc-overlay-tail)) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
241 (- (overlay-start kkc-overlay-head) from)) |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
242 (delete-overlay kkc-overlay-head) |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
243 (delete-overlay kkc-overlay-tail))) |
17052 | 244 |
245 (defun kkc-terminate () | |
246 "Exit from KKC mode by fixing the current conversion." | |
247 (interactive) | |
22794
9ace35333789
(kkc-region): Handled the case that
Kenichi Handa <handa@m17n.org>
parents:
22779
diff
changeset
|
248 (goto-char (overlay-end kkc-overlay-tail)) |
9ace35333789
(kkc-region): Handled the case that
Kenichi Handa <handa@m17n.org>
parents:
22779
diff
changeset
|
249 (move-overlay kkc-overlay-head (point) (point)) |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
250 (setq kkc-converting nil)) |
17052 | 251 |
252 (defun kkc-cancel () | |
253 "Exit from KKC mode by canceling any conversions." | |
254 (interactive) | |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
255 (goto-char (overlay-start kkc-overlay-head)) |
17052 | 256 (delete-region (overlay-start kkc-overlay-head) |
257 (overlay-end kkc-overlay-tail)) | |
258 (insert kkc-original-kana) | |
22794
9ace35333789
(kkc-region): Handled the case that
Kenichi Handa <handa@m17n.org>
parents:
22779
diff
changeset
|
259 (setq kkc-converting nil)) |
17052 | 260 |
261 (defun kkc-first-char-only () | |
262 "Select only the first character currently converted." | |
263 (interactive) | |
264 (goto-char (overlay-start kkc-overlay-head)) | |
265 (forward-char 1) | |
266 (delete-region (point) (overlay-end kkc-overlay-tail)) | |
267 (kkc-terminate)) | |
268 | |
269 ;; Count of successive invocations of `kkc-next'. | |
270 (defvar kkc-next-count nil) | |
271 | |
272 (defun kkc-next () | |
273 "Select the next candidate of conversion." | |
274 (interactive) | |
275 (if (eq this-command last-command) | |
276 (setq kkc-next-count (1+ kkc-next-count)) | |
277 (setq kkc-next-count 1)) | |
278 (let ((idx (1+ (car kkc-current-conversions)))) | |
279 (if (< idx 0) | |
280 (setq idx 1)) | |
281 (if (>= idx (length kkc-current-conversions)) | |
282 (setq idx 0)) | |
283 (setcar kkc-current-conversions idx) | |
284 (if (> idx 1) | |
285 (progn | |
286 (set-nested-alist kkc-current-key kkc-current-conversions | |
287 kkc-lookup-cache kkc-length-converted) | |
288 (setq kkc-init-file-flag 'kkc-lookup-cache))) | |
289 (if (or kkc-current-conversions-width | |
290 (>= kkc-next-count kkc-show-conversion-list-count)) | |
291 (kkc-show-conversion-list-update)) | |
292 (kkc-update-conversion))) | |
293 | |
294 ;; Count of successive invocations of `kkc-next'. | |
295 (defvar kkc-prev-count nil) | |
296 | |
297 (defun kkc-prev () | |
298 "Select the previous candidate of conversion." | |
299 (interactive) | |
300 (if (eq this-command last-command) | |
301 (setq kkc-prev-count (1+ kkc-prev-count)) | |
302 (setq kkc-prev-count 1)) | |
303 (let ((idx (1- (car kkc-current-conversions)))) | |
304 (if (< idx 0) | |
305 (setq idx (1- (length kkc-current-conversions)))) | |
306 (setcar kkc-current-conversions idx) | |
307 (if (> idx 1) | |
308 (progn | |
309 (set-nested-alist kkc-current-key kkc-current-conversions | |
310 kkc-lookup-cache kkc-length-converted) | |
311 (setq kkc-init-file-flag 'kkc-lookup-cache))) | |
312 (if (or kkc-current-conversions-width | |
313 (>= kkc-prev-count kkc-show-conversion-list-count)) | |
314 (kkc-show-conversion-list-update)) | |
315 (kkc-update-conversion))) | |
316 | |
317 (defun kkc-select-from-list () | |
318 "Select one candidate from the list currently shown in echo area." | |
319 (interactive) | |
320 (let (idx) | |
321 (if kkc-current-conversions-width | |
322 (let ((len (length kkc-show-conversion-list-index-chars)) | |
323 (maxlen (- (aref (aref kkc-current-conversions-width 0) 1) | |
324 (aref (aref kkc-current-conversions-width 0) 0))) | |
325 (i 0)) | |
326 (if (> len maxlen) | |
327 (setq len maxlen)) | |
328 (while (< i len) | |
329 (if (= (aref kkc-show-conversion-list-index-chars i) | |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
330 last-input-event) |
17052 | 331 (setq idx i i len) |
332 (setq i (1+ i)))))) | |
333 (if idx | |
334 (progn | |
335 (setcar kkc-current-conversions | |
336 (+ (aref (aref kkc-current-conversions-width 0) 0) idx)) | |
337 (kkc-show-conversion-list-update) | |
338 (kkc-update-conversion)) | |
22766
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
339 (setq unread-input-method-events |
a83818674920
(kkc-show-conversion-list-index-chars):
Kenichi Handa <handa@m17n.org>
parents:
20638
diff
changeset
|
340 (cons last-input-event unread-input-method-events)) |
17052 | 341 (kkc-terminate)))) |
342 | |
343 (defun kkc-katakana () | |
344 "Convert to Katakana." | |
345 (interactive) | |
346 (setcar kkc-current-conversions -1) | |
347 (kkc-update-conversion 'all)) | |
348 | |
349 (defun kkc-hiragana () | |
350 "Convert to hiragana." | |
351 (interactive) | |
352 (setcar kkc-current-conversions 0) | |
353 (kkc-update-conversion)) | |
354 | |
355 (defun kkc-shorter () | |
356 "Make the Kana string to be converted shorter." | |
357 (interactive) | |
358 (if (<= kkc-length-head 1) | |
359 (error "Can't be shorter") | |
360 (setq kkc-length-head (1- kkc-length-head)) | |
361 (if (> kkc-length-converted kkc-length-head) | |
362 (let ((len kkc-length-head)) | |
363 (setq kkc-length-converted 0) | |
364 (while (not (kkc-lookup-key len)) | |
365 (setq len (1- len))))) | |
366 (kkc-update-conversion 'all))) | |
367 | |
368 (defun kkc-longer () | |
369 "Make the Kana string to be converted longer." | |
370 (interactive) | |
371 (if (>= kkc-length-head (length kkc-current-key)) | |
372 (error "Can't be longer") | |
373 (setq kkc-length-head (1+ kkc-length-head)) | |
374 ;; This time, try also entries with postfixes. | |
375 (kkc-lookup-key kkc-length-head 'postfix) | |
376 (kkc-update-conversion 'all))) | |
377 | |
378 (defun kkc-next-phrase () | |
379 "Fix the currently converted string and try to convert the remaining string." | |
380 (interactive) | |
381 (if (>= kkc-length-head (length kkc-current-key)) | |
382 (kkc-terminate) | |
383 (setq kkc-length-head (- (length kkc-current-key) kkc-length-head)) | |
384 (goto-char (overlay-end kkc-overlay-head)) | |
385 (while (and (< (point) (overlay-end kkc-overlay-tail)) | |
386 (looking-at "\\CH")) | |
387 (goto-char (match-end 0)) | |
388 (setq kkc-length-head (1- kkc-length-head))) | |
389 (if (= kkc-length-head 0) | |
390 (kkc-terminate) | |
391 (let ((newkey (make-vector kkc-length-head 0)) | |
392 (idx (- (length kkc-current-key) kkc-length-head)) | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
393 (len kkc-length-head) |
17052 | 394 (i 0)) |
395 ;; For the moment, (setq kkc-original-kana (concat newkey)) | |
396 ;; doesn't work. | |
397 (setq kkc-original-kana "") | |
398 (while (< i kkc-length-head) | |
399 (aset newkey i (aref kkc-current-key (+ idx i))) | |
400 (setq kkc-original-kana | |
401 (concat kkc-original-kana (char-to-string (aref newkey i)))) | |
402 (setq i (1+ i))) | |
403 (setq kkc-current-key newkey) | |
404 (setq kkc-length-converted 0) | |
18644
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
405 (while (and (not (kkc-lookup-key kkc-length-head nil |
69c91eee7ba1
(kkc-region): Call skkdic-lookup-key with
Kenichi Handa <handa@m17n.org>
parents:
18377
diff
changeset
|
406 (< kkc-length-head len))) |
17052 | 407 (> kkc-length-head 1)) |
408 (setq kkc-length-head (1- kkc-length-head))) | |
409 (let ((pos (point)) | |
410 (tail (overlay-end kkc-overlay-tail))) | |
411 (move-overlay kkc-overlay-head pos tail) | |
412 (move-overlay kkc-overlay-tail tail tail)) | |
413 (kkc-update-conversion 'all))))) | |
414 | |
415 ;; We'll show users a list of available conversions in echo area with | |
416 ;; index numbers so that users can select one conversion with the | |
417 ;; number. | |
418 | |
419 ;; Set `kkc-current-conversions-width'. | |
420 (defun kkc-setup-current-conversions-width () | |
421 (let ((convs (cdr kkc-current-conversions)) | |
422 (len (length kkc-current-conversions)) | |
423 (idx 1)) | |
424 (setq kkc-current-conversions-width (make-vector len nil)) | |
425 ;; To tell `kkc-show-conversion-list-update' to generate | |
426 ;; message from scratch. | |
427 (aset kkc-current-conversions-width 0 (vector len -2 nil)) | |
428 ;; Fill the remaining slots. | |
429 (while convs | |
430 (aset kkc-current-conversions-width idx | |
431 (+ (string-width (car convs)) 4)) | |
432 (setq convs (cdr convs) | |
433 idx (1+ idx))))) | |
434 | |
435 (defun kkc-show-conversion-list-or-next-group () | |
436 "Show list of available conversions in echo area with index numbers. | |
437 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." | |
439 (interactive) | |
440 (if (< (length kkc-current-conversions) 3) | |
441 (error "No alternative")) | |
442 (if kkc-current-conversions-width | |
443 (let ((next-idx (aref (aref kkc-current-conversions-width 0) 1))) | |
444 (if (< next-idx (length kkc-current-conversions-width)) | |
445 (setcar kkc-current-conversions next-idx) | |
446 (setcar kkc-current-conversions 1)) | |
447 (kkc-show-conversion-list-update) | |
448 (kkc-update-conversion)) | |
449 (kkc-setup-current-conversions-width) | |
450 (kkc-show-conversion-list-update))) | |
451 | |
452 (defun kkc-show-conversion-list-or-prev-group () | |
453 "Show list of available conversions in echo area with index numbers. | |
454 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." | |
456 (interactive) | |
457 (if (< (length kkc-current-conversions) 3) | |
458 (error "No alternative")) | |
459 (if kkc-current-conversions-width | |
460 (let ((this-idx (aref (aref kkc-current-conversions-width 0) 0))) | |
461 (if (> this-idx 1) | |
462 (setcar kkc-current-conversions (1- this-idx)) | |
463 (setcar kkc-current-conversions | |
464 (1- (length kkc-current-conversions-width)))) | |
465 (kkc-show-conversion-list-update) | |
466 (kkc-update-conversion)) | |
467 (kkc-setup-current-conversions-width) | |
468 (kkc-show-conversion-list-update))) | |
469 | |
470 ;; Update the conversion list shown in echo area. | |
471 (defun kkc-show-conversion-list-update () | |
472 (or kkc-current-conversions-width | |
473 (kkc-setup-current-conversions-width)) | |
474 (let* ((current-idx (car kkc-current-conversions)) | |
475 (first-slot (aref kkc-current-conversions-width 0)) | |
476 (this-idx (aref first-slot 0)) | |
477 (next-idx (aref first-slot 1)) | |
478 (msg (aref first-slot 2))) | |
479 (if (< current-idx this-idx) | |
480 ;; The currently selected conversion is before the list shown | |
481 ;; previously. We must start calculation of message width | |
482 ;; from the start again. | |
483 (setq this-idx 1 msg nil) | |
484 (if (>= current-idx next-idx) | |
485 ;; The currently selected conversion is after the list shown | |
486 ;; previously. We start calculation of message width from | |
487 ;; the conversion next of TO. | |
488 (setq this-idx next-idx msg nil) | |
489 ;; The current conversion is in MSG. Just clear brackets | |
490 ;; around index number. | |
491 (if (string-match "<.>" msg) | |
492 (progn | |
493 (aset msg (match-beginning 0) ?\ ) | |
494 (aset msg (1- (match-end 0)) ?\ ))))) | |
495 (if (not msg) | |
496 (let ((len (length kkc-current-conversions)) | |
497 (max-width (window-width (minibuffer-window))) | |
498 (width-table kkc-current-conversions-width) | |
499 (width 0) | |
500 (idx this-idx) | |
22816
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
501 (max-items (length kkc-show-conversion-list-index-chars)) |
17052 | 502 l) |
503 (while (< idx current-idx) | |
22816
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
504 (if (and (<= (+ width (aref width-table idx)) max-width) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
505 (< (- idx this-idx) max-items)) |
17052 | 506 (setq width (+ width (aref width-table idx))) |
507 (setq this-idx idx width (aref width-table idx))) | |
508 (setq idx (1+ idx) | |
509 l (cdr l))) | |
510 (aset first-slot 0 this-idx) | |
511 (while (and (< idx len) | |
22816
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
512 (<= (+ width (aref width-table idx)) max-width) |
8f9d4edebbdd
(kkc-region): Unwind-protect the conversion
Kenichi Handa <handa@m17n.org>
parents:
22794
diff
changeset
|
513 (< (- idx this-idx) max-items)) |
17052 | 514 (setq width (+ width (aref width-table idx)) |
515 idx (1+ idx) | |
516 l (cdr l))) | |
517 (aset first-slot 1 (setq next-idx idx)) | |
518 (setq l (nthcdr this-idx kkc-current-conversions)) | |
519 (setq msg "") | |
520 (setq idx this-idx) | |
521 (while (< idx next-idx) | |
522 (setq msg (format "%s %c %s " | |
523 msg | |
524 (aref kkc-show-conversion-list-index-chars | |
525 (- idx this-idx)) | |
526 (car l))) | |
527 (setq idx (1+ idx) | |
528 l (cdr l))) | |
529 (aset first-slot 2 msg))) | |
530 (if (> current-idx 0) | |
531 (progn | |
532 ;; Highlight the current conversion by brackets. | |
533 (string-match (format " \\(%c\\) " | |
534 (aref kkc-show-conversion-list-index-chars | |
535 (- current-idx this-idx))) | |
536 msg) | |
537 (aset msg (match-beginning 0) ?<) | |
538 (aset msg (1- (match-end 0)) ?>))) | |
539 (message "%s" msg))) | |
540 | |
541 ;; Update the conversion area with the latest conversion selected. | |
542 ;; ALL if non nil means to update the whole area, else update only | |
543 ;; inside quail-overlay-head. | |
544 | |
545 (defun kkc-update-conversion (&optional all) | |
546 (goto-char (overlay-start kkc-overlay-head)) | |
547 (cond ((= (car kkc-current-conversions) 0) ; Hiragana | |
548 (let ((i 0)) | |
549 (while (< i kkc-length-converted) | |
550 (insert (aref kkc-current-key i)) | |
551 (setq i (1+ i))))) | |
552 ((= (car kkc-current-conversions) -1) ; Katakana | |
553 (let ((i 0)) | |
554 (while (< i kkc-length-converted) | |
555 (insert (japanese-katakana (aref kkc-current-key i))) | |
556 (setq i (1+ i))))) | |
557 (t | |
558 (insert (nth (car kkc-current-conversions) kkc-current-conversions)))) | |
559 (delete-region (point) (overlay-start kkc-overlay-tail)) | |
560 (if all | |
561 (let ((len (length kkc-current-key)) | |
562 (i kkc-length-converted)) | |
563 (delete-region (overlay-start kkc-overlay-tail) | |
564 (overlay-end kkc-overlay-head)) | |
565 (while (< i kkc-length-head) | |
566 (if (= (car kkc-current-conversions) -1) | |
567 (insert (japanese-katakana (aref kkc-current-key i))) | |
568 (insert (aref kkc-current-key i))) | |
569 (setq i (1+ i))) | |
570 (let ((pos (point))) | |
571 (while (< i len) | |
572 (insert (aref kkc-current-key i)) | |
573 (setq i (1+ i))) | |
574 (move-overlay kkc-overlay-head | |
575 (overlay-start kkc-overlay-head) pos) | |
576 (delete-region (point) (overlay-end kkc-overlay-tail))))) | |
577 (goto-char (overlay-end kkc-overlay-tail))) | |
578 | |
579 ;; | |
580 (provide 'kkc) | |
581 | |
582 ;; kkc.el ends here |