Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; quail.el --- provides simple input method for multilingual text |
4ed46869 | 2 | |
09877d5d | 3 | ;; Copyright (C) 1995, 2000 Electrotechnical Laboratory, JAPAN. |
fa526c4a | 4 | ;; Licensed to the Free Software Foundation. |
9372a49f | 5 | ;; Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
db328182 KH |
6 | ;; Copyright (C) 2005 |
7 | ;; National Institute of Advanced Industrial Science and Technology (AIST) | |
8 | ;; Registration Number H14PRO021 | |
4ed46869 KH |
9 | |
10 | ;; Author: Kenichi HANDA <handa@etl.go.jp> | |
11 | ;; Naoto TAKAHASHI <ntakahas@etl.go.jp> | |
12 | ;; Maintainer: Kenichi HANDA <handa@etl.go.jp> | |
35fffde1 | 13 | ;; Keywords: mule, multilingual, input method, i18n |
4ed46869 KH |
14 | |
15 | ;; This file is part of GNU Emacs. | |
16 | ||
17 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
18 | ;; it under the terms of the GNU General Public License as published by | |
19 | ;; the Free Software Foundation; either version 2, or (at your option) | |
20 | ;; any later version. | |
21 | ||
22 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
23 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | ;; GNU General Public License for more details. | |
26 | ||
27 | ;; You should have received a copy of the GNU General Public License | |
369314dc KH |
28 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
29 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
30 | ;; Boston, MA 02111-1307, USA. | |
4ed46869 KH |
31 | |
32 | ;;; Commentary: | |
33 | ||
34 | ;; In Quail minor mode, you can input multilingual text easily. By | |
35 | ;; defining a translation table (named Quail map) which maps ASCII key | |
36 | ;; string to multilingual character or string, you can input any text | |
37 | ;; from ASCII keyboard. | |
38 | ;; | |
39 | ;; We use words "translation" and "conversion" differently. The | |
40 | ;; former is done by Quail package itself, the latter is the further | |
41 | ;; process of converting a translated text to some more desirable | |
42 | ;; text. For instance, Quail package for Japanese (`quail-jp') | |
43 | ;; translates Roman text (transliteration of Japanese in Latin | |
44 | ;; alphabets) to Hiragana text, which is then converted to | |
45 | ;; Kanji-and-Kana mixed text or Katakana text by commands specified in | |
46 | ;; CONVERSION-KEYS argument of the Quail package. | |
47 | ||
35fffde1 DL |
48 | ;; [There was an input method for Mule 2.3 called `Tamago' from the |
49 | ;; Japanese `TAkusan MAtasete GOmenasai', or `Sorry for having you | |
50 | ;; wait so long'; this couldn't be included in Emacs 20. `Tamago' is | |
51 | ;; Japanese for `egg' (implicitly a hen's egg). Handa-san made a | |
52 | ;; smaller and simpler system; the smaller quail egg is also eaten in | |
53 | ;; Japan. Maybe others will be egged on to write more sorts of input | |
54 | ;; methods.] | |
55 | ||
4ed46869 KH |
56 | ;;; Code: |
57 | ||
ec9164f3 PJ |
58 | (require 'help-mode) |
59 | ||
95109387 KH |
60 | (defgroup quail nil |
61 | "Quail: multilingual input method." | |
62 | :group 'leim) | |
4ed46869 KH |
63 | |
64 | ;; Buffer local variables | |
65 | ||
66 | (defvar quail-current-package nil | |
105ef6bf | 67 | "The current Quail package, which depends on the current input method. |
4ed46869 KH |
68 | See the documentation of `quail-package-alist' for the format.") |
69 | (make-variable-buffer-local 'quail-current-package) | |
70 | (put 'quail-current-package 'permanent-local t) | |
71 | ||
17388a62 KH |
72 | ;; Quail uses the following variables to assist users. |
73 | ;; A string containing available key sequences or translation list. | |
74 | (defvar quail-guidance-str nil) | |
4ed46869 KH |
75 | ;; A buffer to show completion list of the current key sequence. |
76 | (defvar quail-completion-buf nil) | |
17388a62 KH |
77 | ;; We may display the guidance string in a buffer on a one-line frame. |
78 | (defvar quail-guidance-buf nil) | |
79 | (defvar quail-guidance-frame nil) | |
4ed46869 | 80 | |
7d842556 | 81 | ;; Each buffer in which Quail is activated should use different |
17388a62 KH |
82 | ;; guidance string. |
83 | (make-variable-buffer-local 'quail-guidance-str) | |
84 | (put 'quail-guidance-str 'permanent-local t) | |
7d842556 | 85 | |
4ed46869 KH |
86 | (defvar quail-overlay nil |
87 | "Overlay which covers the current translation region of Quail.") | |
88 | (make-variable-buffer-local 'quail-overlay) | |
89 | ||
90 | (defvar quail-conv-overlay nil | |
91 | "Overlay which covers the text to be converted in Quail mode.") | |
92 | (make-variable-buffer-local 'quail-conv-overlay) | |
93 | ||
94 | (defvar quail-current-key nil | |
95 | "Current key for translation in Quail mode.") | |
b58fc490 | 96 | (make-variable-buffer-local 'quail-current-key) |
4ed46869 KH |
97 | |
98 | (defvar quail-current-str nil | |
99 | "Currently selected translation of the current key.") | |
b58fc490 | 100 | (make-variable-buffer-local 'quail-current-str) |
4ed46869 KH |
101 | |
102 | (defvar quail-current-translations nil | |
7d842556 KH |
103 | "Cons of indices and vector of possible translations of the current key. |
104 | Indices is a list of (CURRENT START END BLOCK BLOCKS), where | |
105 | CURRENT is an index of the current translation, | |
106 | START and END are indices of the start and end of the current block, | |
107 | BLOCK is the current block index, | |
108 | BLOCKS is a number of blocks of translation.") | |
b58fc490 | 109 | (make-variable-buffer-local 'quail-current-translations) |
4ed46869 | 110 | |
ff913e92 KH |
111 | (defvar quail-current-data nil |
112 | "Any Lisp object holding information of current translation status. | |
113 | When a key sequence is mapped to TRANS and TRANS is a cons | |
cd30a521 | 114 | of actual translation and some Lisp object to be referred |
ff913e92 KH |
115 | for translating the longer key sequence, this variable is set |
116 | to that Lisp object.") | |
7d842556 | 117 | (make-variable-buffer-local 'quail-current-data) |
ff913e92 | 118 | |
4ed46869 KH |
119 | ;; Quail package handlers. |
120 | ||
121 | (defvar quail-package-alist nil | |
122 | "List of Quail packages. | |
123 | A Quail package is a list of these elements: | |
124 | NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS, | |
125 | FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT, | |
126 | DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION, | |
b55ba027 | 127 | CONVERSION-KEYS, SIMPLE. |
4ed46869 KH |
128 | |
129 | QUAIL-MAP is a data structure to map key strings to translations. For | |
130 | the format, see the documentation of `quail-map-p'. | |
131 | ||
132 | DECODE-MAP is an alist of translations and corresponding keys. | |
133 | ||
134 | See the documentation of `quail-define-package' for the other elements.") | |
135 | ||
136 | ;; Return various slots in the current quail-package. | |
137 | ||
138 | (defsubst quail-name () | |
139 | "Return the name of the current Quail package." | |
140 | (nth 0 quail-current-package)) | |
ace43887 KH |
141 | ;;;###autoload |
142 | (defun quail-title () | |
4ed46869 | 143 | "Return the title of the current Quail package." |
341cd4f0 KH |
144 | (let ((title (nth 1 quail-current-package))) |
145 | ;; TITLE may be a string or a list. If it is a list, each element | |
146 | ;; is a string or the form (VAR STR1 STR2), and the interpretation | |
147 | ;; of the list is the same as that of mode-line-format. | |
148 | (if (stringp title) | |
149 | title | |
150 | (condition-case nil | |
151 | (mapconcat | |
a1506d29 | 152 | (lambda (x) |
341cd4f0 KH |
153 | (cond ((stringp x) x) |
154 | ((and (listp x) (symbolp (car x)) (= (length x) 3)) | |
155 | (if (symbol-value (car x)) | |
156 | (nth 1 x) (nth 2 x))) | |
157 | (t ""))) | |
158 | title "") | |
159 | (error ""))))) | |
4ed46869 KH |
160 | (defsubst quail-map () |
161 | "Return the translation map of the current Quail package." | |
162 | (nth 2 quail-current-package)) | |
163 | (defsubst quail-guidance () | |
164 | "Return an object used for `guidance' feature of the current Quail package. | |
165 | See also the documentation of `quail-define-package'." | |
166 | (nth 3 quail-current-package)) | |
167 | (defsubst quail-docstring () | |
168 | "Return the documentation string of the current Quail package." | |
169 | (nth 4 quail-current-package)) | |
170 | (defsubst quail-translation-keymap () | |
171 | "Return translation keymap in the current Quail package. | |
172 | Translation keymap is a keymap used while translation region is active." | |
173 | (nth 5 quail-current-package)) | |
174 | (defsubst quail-forget-last-selection () | |
175 | "Return `forget-last-selection' flag of the current Quail package. | |
176 | See also the documentation of `quail-define-package'." | |
177 | (nth 6 quail-current-package)) | |
178 | (defsubst quail-deterministic () | |
179 | "Return `deterministic' flag of the current Quail package. | |
180 | See also the documentation of `quail-define-package'." | |
181 | (nth 7 quail-current-package)) | |
182 | (defsubst quail-kbd-translate () | |
183 | "Return `kbd-translate' flag of the current Quail package. | |
184 | See also the documentation of `quail-define-package'." | |
185 | (nth 8 quail-current-package)) | |
186 | (defsubst quail-show-layout () | |
187 | "Return `show-layout' flag of the current Quail package. | |
188 | See also the documentation of `quail-define-package'." | |
189 | (nth 9 quail-current-package)) | |
190 | (defsubst quail-decode-map () | |
191 | "Return decode map of the current Quail package. | |
192 | It is an alist of translations and corresponding keys." | |
193 | (nth 10 quail-current-package)) | |
194 | (defsubst quail-maximum-shortest () | |
195 | "Return `maximum-shortest' flag of the current Quail package. | |
196 | See also the documentation of `quail-define-package'." | |
197 | (nth 11 quail-current-package)) | |
198 | (defsubst quail-overlay-plist () | |
199 | "Return property list of an overly used in the current Quail package." | |
200 | (nth 12 quail-current-package)) | |
201 | (defsubst quail-update-translation-function () | |
202 | "Return a function for updating translation in the current Quail package." | |
203 | (nth 13 quail-current-package)) | |
204 | (defsubst quail-conversion-keymap () | |
205 | "Return conversion keymap in the current Quail package. | |
206 | Conversion keymap is a keymap used while conversion region is active | |
207 | but translation region is not active." | |
208 | (nth 14 quail-current-package)) | |
b55ba027 KH |
209 | (defsubst quail-simple () |
210 | "Return t if the current Quail package is simple." | |
211 | (nth 15 quail-current-package)) | |
4ed46869 KH |
212 | |
213 | (defsubst quail-package (name) | |
214 | "Return Quail package named NAME." | |
215 | (assoc name quail-package-alist)) | |
216 | ||
217 | (defun quail-add-package (package) | |
218 | "Add Quail package PACKAGE to `quail-package-alist'." | |
219 | (let ((pac (quail-package (car package)))) | |
220 | (if pac | |
221 | (setcdr pac (cdr package)) | |
222 | (setq quail-package-alist (cons package quail-package-alist))))) | |
223 | ||
224 | (defun quail-select-package (name) | |
225 | "Select Quail package named NAME as the current Quail package." | |
226 | (let ((package (quail-package name))) | |
227 | (if (null package) | |
228 | (error "No Quail package `%s'" name)) | |
229 | (setq quail-current-package package) | |
230 | (setq-default quail-current-package package) | |
231 | name)) | |
232 | ||
233 | ;;;###autoload | |
234 | (defun quail-use-package (package-name &rest libraries) | |
235 | "Start using Quail package PACKAGE-NAME. | |
3573bdbe EZ |
236 | The remaining arguments are libraries to be loaded before using the package. |
237 | ||
238 | This activates input method defined by PACKAGE-NAME by running | |
239 | `quail-activate', which see." | |
ff913e92 KH |
240 | (let ((package (quail-package package-name))) |
241 | (if (null package) | |
242 | ;; Perhaps we have not yet loaded necessary libraries. | |
243 | (while libraries | |
244 | (if (not (load (car libraries) t)) | |
245 | (progn | |
246 | (with-output-to-temp-buffer "*Help*" | |
247 | (princ "Quail package \"") | |
248 | (princ package-name) | |
249 | (princ "\" can't be activated\n because library \"") | |
250 | (princ (car libraries)) | |
251 | (princ "\" is not in `load-path'. | |
4ed46869 KH |
252 | |
253 | The most common case is that you have not yet installed appropriate | |
254 | libraries in LEIM (Libraries of Emacs Input Method) which is | |
255 | distributed separately from Emacs. | |
256 | ||
4ed46869 | 257 | LEIM is available from the same ftp directory as Emacs.")) |
ff913e92 KH |
258 | (error "Can't use the Quail package `%s'" package-name)) |
259 | (setq libraries (cdr libraries)))))) | |
4ed46869 KH |
260 | (quail-select-package package-name) |
261 | (setq current-input-method-title (quail-title)) | |
bcbeff85 KH |
262 | (quail-activate) |
263 | ;; Hide all '... loaded' message. | |
264 | (message nil)) | |
4ed46869 | 265 | |
d91eafdf | 266 | (defvar quail-translation-keymap |
4ed46869 | 267 | (let ((map (make-keymap)) |
d91eafdf KH |
268 | (i 0)) |
269 | (while (< i ?\ ) | |
270 | (define-key map (char-to-string i) 'quail-other-command) | |
271 | (setq i (1+ i))) | |
4ed46869 KH |
272 | (while (< i 127) |
273 | (define-key map (char-to-string i) 'quail-self-insert-command) | |
274 | (setq i (1+ i))) | |
f5c7c0eb | 275 | (setq i 128) |
094550e6 | 276 | (while (< i 256) |
f5c7c0eb KH |
277 | (define-key map (vector i) 'quail-self-insert-command) |
278 | (setq i (1+ i))) | |
4ed46869 | 279 | (define-key map "\177" 'quail-delete-last-char) |
4ed46869 KH |
280 | (define-key map "\C-f" 'quail-next-translation) |
281 | (define-key map "\C-b" 'quail-prev-translation) | |
282 | (define-key map "\C-n" 'quail-next-translation-block) | |
283 | (define-key map "\C-p" 'quail-prev-translation-block) | |
4afb4ca5 KH |
284 | (define-key map [right] 'quail-next-translation) |
285 | (define-key map [left] 'quail-prev-translation) | |
286 | (define-key map [down] 'quail-next-translation-block) | |
287 | (define-key map [up] 'quail-prev-translation-block) | |
4ed46869 KH |
288 | (define-key map "\C-i" 'quail-completion) |
289 | (define-key map "\C-@" 'quail-select-current) | |
5611ce7c KH |
290 | ;; Following simple.el, Enter key on numeric keypad selects the |
291 | ;; current translation just like `C-SPC', and `mouse-2' chooses | |
292 | ;; any completion visible in the *Quail Completions* buffer. | |
293 | (define-key map [kp-enter] 'quail-select-current) | |
294 | (define-key map [mouse-2] 'quail-mouse-choose-completion) | |
295 | (define-key map [down-mouse-2] nil) | |
4ed46869 | 296 | (define-key map "\C-h" 'quail-translation-help) |
e68e61b5 | 297 | (define-key map [?\C- ] 'quail-select-current) |
4ed46869 KH |
298 | (define-key map [tab] 'quail-completion) |
299 | (define-key map [delete] 'quail-delete-last-char) | |
300 | (define-key map [backspace] 'quail-delete-last-char) | |
e3799a72 | 301 | map) |
57a54470 RS |
302 | "Keymap used processing translation in complex Quail modes. |
303 | Only a few especially complex input methods use this map; | |
304 | most use `quail-simple-translation-keymap' instead. | |
305 | This map is activated while translation region is active.") | |
306 | ||
362a8065 KH |
307 | (defvar quail-translation-docstring |
308 | "When you type keys, the echo area shows the possible characters | |
309 | which correspond to that key sequence, each preceded by a digit. You | |
310 | can select one of the characters shown by typing the corresponding | |
311 | digit. Alternatively, you can use C-f and C-b to move through the | |
312 | line to select the character you want, then type a letter to begin | |
313 | entering another Chinese character or type a space or punctuation | |
314 | character. | |
315 | ||
316 | If there are more than ten possible characters for the given spelling, | |
317 | the echo area shows ten characters at a time; you can use C-n to move | |
318 | to the next group of ten, and C-p to move back to the previous group | |
319 | of ten.") | |
320 | ||
321 | ;; Categorize each Quail commands to make the output of quail-help | |
322 | ;; concise. This is done by putting `quail-help' property. The value | |
323 | ;; is: | |
324 | ;; hide -- never show this command | |
325 | ;; non-deterministic -- show only for non-deterministic input method | |
326 | (let ((l '((quail-other-command . hide) | |
327 | (quail-self-insert-command . hide) | |
328 | (quail-delete-last-char . hide) | |
329 | (quail-next-translation . non-deterministic) | |
330 | (quail-prev-translation . non-deterministic) | |
331 | (quail-next-translation-block . non-deterministic) | |
332 | (quail-prev-translation-block . non-deterministic)))) | |
95109387 | 333 | (while l |
362a8065 | 334 | (put (car (car l)) 'quail-help (cdr (car l))) |
95109387 KH |
335 | (setq l (cdr l)))) |
336 | ||
d91eafdf | 337 | (defvar quail-simple-translation-keymap |
57a54470 | 338 | (let ((map (make-keymap)) |
d91eafdf KH |
339 | (i 0)) |
340 | (while (< i ?\ ) | |
341 | (define-key map (char-to-string i) 'quail-other-command) | |
342 | (setq i (1+ i))) | |
57a54470 RS |
343 | (while (< i 127) |
344 | (define-key map (char-to-string i) 'quail-self-insert-command) | |
345 | (setq i (1+ i))) | |
99da6af3 KH |
346 | (setq i 128) |
347 | (while (< i 256) | |
348 | (define-key map (vector i) 'quail-self-insert-command) | |
349 | (setq i (1+ i))) | |
57a54470 | 350 | (define-key map "\177" 'quail-delete-last-char) |
959096f8 RS |
351 | (define-key map [delete] 'quail-delete-last-char) |
352 | (define-key map [backspace] 'quail-delete-last-char) | |
d91eafdf KH |
353 | ;;(let ((meta-map (make-sparse-keymap))) |
354 | ;;(define-key map (char-to-string meta-prefix-char) meta-map) | |
355 | ;;(define-key map [escape] meta-map)) | |
e3799a72 | 356 | map) |
57a54470 | 357 | "Keymap used while processing translation in simple Quail modes. |
348d1438 | 358 | A few especially complex input methods use `quail-translation-keymap' instead. |
4ed46869 KH |
359 | This map is activated while translation region is active.") |
360 | ||
d91eafdf | 361 | (defvar quail-conversion-keymap |
4ed46869 | 362 | (let ((map (make-keymap)) |
b58fc490 | 363 | (i ?\ )) |
4ed46869 | 364 | (while (< i 127) |
b58fc490 | 365 | (define-key map (char-to-string i) 'quail-self-insert-command) |
4ed46869 | 366 | (setq i (1+ i))) |
f5c7c0eb | 367 | (setq i 128) |
094550e6 | 368 | (while (< i 256) |
b58fc490 | 369 | (define-key map (vector i) 'quail-self-insert-command) |
f5c7c0eb | 370 | (setq i (1+ i))) |
4ed46869 KH |
371 | (define-key map "\C-b" 'quail-conversion-backward-char) |
372 | (define-key map "\C-f" 'quail-conversion-forward-char) | |
373 | (define-key map "\C-a" 'quail-conversion-beginning-of-region) | |
374 | (define-key map "\C-e" 'quail-conversion-end-of-region) | |
375 | (define-key map "\C-d" 'quail-conversion-delete-char) | |
b45d8d64 | 376 | (define-key map "\C-k" 'quail-conversion-delete-tail) |
d91eafdf | 377 | (define-key map "\C-h" 'quail-translation-help) |
4ed46869 KH |
378 | (define-key map "\177" 'quail-conversion-backward-delete-char) |
379 | (define-key map [delete] 'quail-conversion-backward-delete-char) | |
380 | (define-key map [backspace] 'quail-conversion-backward-delete-char) | |
e3799a72 | 381 | map) |
4ed46869 | 382 | "Keymap used for processing conversion in Quail mode. |
cd30a521 | 383 | This map is activated while conversion region is active but translation |
4ed46869 KH |
384 | region is not active.") |
385 | ||
95109387 KH |
386 | ;; Just a dummy definition. |
387 | (defun quail-other-command () | |
388 | (interactive) | |
389 | ) | |
390 | ||
ff913e92 | 391 | ;;;###autoload |
4ed46869 KH |
392 | (defun quail-define-package (name language title |
393 | &optional guidance docstring translation-keys | |
394 | forget-last-selection deterministic | |
395 | kbd-translate show-layout create-decode-map | |
396 | maximum-shortest overlay-plist | |
397 | update-translation-function | |
57a54470 | 398 | conversion-keys simple) |
4ed46869 KH |
399 | "Define NAME as a new Quail package for input LANGUAGE. |
400 | TITLE is a string to be displayed at mode-line to indicate this package. | |
d91eafdf | 401 | Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS, |
4ed46869 KH |
402 | FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT, |
403 | CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, | |
57a54470 | 404 | UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE. |
4ed46869 KH |
405 | |
406 | GUIDANCE specifies how a guidance string is shown in echo area. | |
407 | If it is t, list of all possible translations for the current key is shown | |
408 | with the currently selected translation being highlighted. | |
409 | If it is an alist, the element has the form (CHAR . STRING). Each character | |
410 | in the current key is searched in the list and the corresponding string is | |
411 | shown. | |
412 | If it is nil, the current key is shown. | |
413 | ||
362a8065 KH |
414 | DOCSTRING is the documentation string of this package. The command |
415 | `describe-input-method' shows this string while replacing the form | |
cd70a6ef KH |
416 | \\=\\<VAR> in the string by the value of VAR. That value should be a |
417 | string. For instance, the form \\=\\<quail-translation-docstring> is | |
362a8065 KH |
418 | replaced by a description about how to select a translation from a |
419 | list of candidates. | |
4ed46869 KH |
420 | |
421 | TRANSLATION-KEYS specifies additional key bindings used while translation | |
422 | region is active. It is an alist of single key character vs. corresponding | |
423 | command to be called. | |
424 | ||
425 | FORGET-LAST-SELECTION non-nil means a selected translation is not kept | |
426 | for the future to translate the same key. If this flag is nil, a | |
427 | translation selected for a key is remembered so that it can be the | |
428 | first candidate when the same key is entered later. | |
429 | ||
430 | DETERMINISTIC non-nil means the first candidate of translation is | |
431 | selected automatically without allowing users to select another | |
432 | translation for a key. In this case, unselected translations are of | |
433 | no use for an interactive use of Quail but can be used by some other | |
434 | programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set | |
435 | to t. | |
436 | ||
437 | KBD-TRANSLATE non-nil means input characters are translated from a | |
438 | user's keyboard layout to the standard keyboard layout. See the | |
439 | documentation of `quail-keyboard-layout' and | |
440 | `quail-keyboard-layout-standard' for more detail. | |
441 | ||
442 | SHOW-LAYOUT non-nil means the `quail-help' command should show | |
443 | the user's keyboard layout visually with translated characters. | |
444 | If KBD-TRANSLATE is set, it is desirable to set also this flag unless | |
445 | this package defines no translations for single character keys. | |
446 | ||
447 | CREATE-DECODE-MAP non-nil means decode map is also created. A decode | |
448 | map is an alist of translations and corresponding original keys. | |
449 | Although this map is not used by Quail itself, it can be used by some | |
450 | other programs. For instance, Vietnamese supporting needs this map to | |
451 | convert Vietnamese text to VIQR format which uses only ASCII | |
452 | characters to represent Vietnamese characters. | |
453 | ||
454 | MAXIMUM-SHORTEST non-nil means break key sequence to get maximum | |
455 | length of the shortest sequence. When we don't have a translation of | |
456 | key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break | |
457 | the key at \"..AB\" and start translation of \"CD..\". Hangul | |
458 | packages, for instance, use this facility. If this flag is nil, we | |
459 | break the key just at \"..ABC\" and start translation of \"D..\". | |
460 | ||
461 | OVERLAY-PLIST if non-nil is a property list put on an overlay which | |
462 | covers Quail translation region. | |
463 | ||
464 | UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update | |
cd30a521 KH |
465 | the current translation region according to a new translation data. By |
466 | default, a translated text or a user's key sequence (if no translation | |
4ed46869 KH |
467 | for it) is inserted. |
468 | ||
469 | CONVERSION-KEYS specifies additional key bindings used while | |
470 | conversion region is active. It is an alist of single key character | |
57a54470 RS |
471 | vs. corresponding command to be called. |
472 | ||
473 | If SIMPLE is non-nil, then we do not alter the meanings of | |
474 | commands such as C-f, C-b, C-n, C-p and TAB; they are treated as | |
475 | non-Quail commands." | |
4ed46869 KH |
476 | (let (translation-keymap conversion-keymap) |
477 | (if deterministic (setq forget-last-selection t)) | |
478 | (if translation-keys | |
20110571 | 479 | (progn |
57a54470 RS |
480 | (setq translation-keymap (copy-keymap |
481 | (if simple quail-simple-translation-keymap | |
482 | quail-translation-keymap))) | |
20110571 KH |
483 | (while translation-keys |
484 | (define-key translation-keymap | |
485 | (car (car translation-keys)) (cdr (car translation-keys))) | |
486 | (setq translation-keys (cdr translation-keys)))) | |
57a54470 RS |
487 | (setq translation-keymap |
488 | (if simple quail-simple-translation-keymap | |
489 | quail-translation-keymap))) | |
20110571 KH |
490 | (when conversion-keys |
491 | (setq conversion-keymap (copy-keymap quail-conversion-keymap)) | |
492 | (while conversion-keys | |
493 | (define-key conversion-keymap | |
494 | (car (car conversion-keys)) (cdr (car conversion-keys))) | |
495 | (setq conversion-keys (cdr conversion-keys)))) | |
4ed46869 KH |
496 | (quail-add-package |
497 | (list name title (list nil) guidance (or docstring "") | |
498 | translation-keymap | |
499 | forget-last-selection deterministic kbd-translate show-layout | |
500 | (if create-decode-map (list 'decode-map) nil) | |
501 | maximum-shortest overlay-plist update-translation-function | |
b55ba027 | 502 | conversion-keymap simple)) |
7d842556 KH |
503 | |
504 | ;; Update input-method-alist. | |
505 | (let ((slot (assoc name input-method-alist)) | |
506 | (val (list language 'quail-use-package title docstring))) | |
507 | (if slot (setcdr slot val) | |
508 | (setq input-method-alist (cons (cons name val) input-method-alist))))) | |
509 | ||
4ed46869 KH |
510 | (quail-select-package name)) |
511 | ||
512 | ;; Quail minor mode handlers. | |
513 | ||
514 | ;; Setup overlays used in Quail mode. | |
20110571 | 515 | (defun quail-setup-overlays (conversion-mode) |
4ed46869 KH |
516 | (let ((pos (point))) |
517 | (if (overlayp quail-overlay) | |
518 | (move-overlay quail-overlay pos pos) | |
17388a62 | 519 | (setq quail-overlay (make-overlay pos pos)) |
20110571 KH |
520 | (if input-method-highlight-flag |
521 | (overlay-put quail-overlay 'face 'underline)) | |
4ed46869 KH |
522 | (let ((l (quail-overlay-plist))) |
523 | (while l | |
524 | (overlay-put quail-overlay (car l) (car (cdr l))) | |
525 | (setq l (cdr (cdr l)))))) | |
20110571 KH |
526 | (if conversion-mode |
527 | (if (overlayp quail-conv-overlay) | |
528 | (if (not (overlay-start quail-conv-overlay)) | |
529 | (move-overlay quail-conv-overlay pos pos)) | |
17388a62 | 530 | (setq quail-conv-overlay (make-overlay pos pos)) |
20110571 KH |
531 | (if input-method-highlight-flag |
532 | (overlay-put quail-conv-overlay 'face 'underline)))))) | |
4ed46869 KH |
533 | |
534 | ;; Delete overlays used in Quail mode. | |
535 | (defun quail-delete-overlays () | |
b58fc490 | 536 | (if (and (overlayp quail-overlay) (overlay-start quail-overlay)) |
4ed46869 | 537 | (delete-overlay quail-overlay)) |
b58fc490 | 538 | (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay)) |
4ed46869 KH |
539 | (delete-overlay quail-conv-overlay))) |
540 | ||
b58fc490 | 541 | (defun quail-inactivate () |
3573bdbe EZ |
542 | "Inactivate Quail input method. |
543 | ||
544 | This function runs the normal hook `quail-inactivate-hook'." | |
b58fc490 KH |
545 | (interactive) |
546 | (quail-activate -1)) | |
547 | ||
548 | (defun quail-activate (&optional arg) | |
549 | "Activate Quail input method. | |
550 | With arg, activate Quail input method if and only if arg is positive. | |
551 | ||
3573bdbe EZ |
552 | This function runs `quail-activate-hook' if it activates the input |
553 | method, `quail-inactivate-hook' if it deactivates it. | |
554 | ||
b58fc490 KH |
555 | While this input method is active, the variable |
556 | `input-method-function' is bound to the function `quail-input-method'." | |
557 | (if (and arg | |
558 | (< (prefix-numeric-value arg) 0)) | |
559 | ;; Let's inactivate Quail input method. | |
560 | (unwind-protect | |
561 | (progn | |
b58fc490 KH |
562 | (quail-delete-overlays) |
563 | (setq describe-current-input-method-function nil) | |
17388a62 KH |
564 | (quail-hide-guidance) |
565 | (remove-hook 'post-command-hook 'quail-show-guidance t) | |
b58fc490 KH |
566 | (run-hooks 'quail-inactivate-hook)) |
567 | (kill-local-variable 'input-method-function)) | |
f0c968ff | 568 | ;; Let's activate Quail input method. |
4ed46869 KH |
569 | (if (null quail-current-package) |
570 | ;; Quail package is not yet selected. Select one now. | |
571 | (let (name) | |
572 | (if quail-package-alist | |
573 | (setq name (car (car quail-package-alist))) | |
4ed46869 KH |
574 | (error "No Quail package loaded")) |
575 | (quail-select-package name))) | |
20110571 | 576 | (setq inactivate-current-input-method-function 'quail-inactivate) |
4ed46869 | 577 | (setq describe-current-input-method-function 'quail-help) |
4ed46869 | 578 | (quail-delete-overlays) |
17388a62 KH |
579 | (setq quail-guidance-str "") |
580 | (quail-show-guidance) | |
05204016 KH |
581 | ;; If we are in minibuffer, turn off the current input method |
582 | ;; before exiting. | |
17388a62 KH |
583 | (when (eq (selected-window) (minibuffer-window)) |
584 | (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer) | |
585 | (add-hook 'post-command-hook 'quail-show-guidance nil t)) | |
b58fc490 KH |
586 | (run-hooks 'quail-activate-hook) |
587 | (make-local-variable 'input-method-function) | |
588 | (setq input-method-function 'quail-input-method))) | |
4ed46869 KH |
589 | |
590 | (defun quail-exit-from-minibuffer () | |
05204016 | 591 | (inactivate-input-method) |
4ed46869 KH |
592 | (if (<= (minibuffer-depth) 1) |
593 | (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))) | |
594 | ||
4ed46869 KH |
595 | ;; Keyboard layout translation handlers. |
596 | ||
597 | ;; Some Quail packages provide localized keyboard simulation which | |
598 | ;; requires a particular keyboard layout. In this case, what we need | |
599 | ;; is locations of keys the user entered, not character codes | |
600 | ;; generated by those keys. However, for the moment, there's no | |
601 | ;; common way to get such information. So, we ask a user to give | |
602 | ;; information of his own keyboard layout, then translate it to the | |
603 | ;; standard layout which we defined so that all Quail packages depend | |
604 | ;; just on it. | |
605 | ||
606 | (defconst quail-keyboard-layout-standard | |
607 | "\ | |
ea3fb7d2 | 608 | \ |
4ed46869 KH |
609 | 1!2@3#4$5%6^7&8*9(0)-_=+`~ \ |
610 | qQwWeErRtTyYuUiIoOpP[{]} \ | |
611 | aAsSdDfFgGhHjJkKlL;:'\"\\| \ | |
ea3fb7d2 KH |
612 | zZxXcCvVbBnNmM,<.>/? \ |
613 | " | |
4ed46869 KH |
614 | "Standard keyboard layout of printable characters Quail assumes. |
615 | See the documentation of `quail-keyboard-layout' for this format. | |
616 | This layout is almost the same as that of VT100, | |
617 | but the location of key \\ (backslash) is just right of key ' (single-quote), | |
618 | not right of RETURN key.") | |
619 | ||
ea3fb7d2 | 620 | (defconst quail-keyboard-layout-len 180) |
4ed46869 KH |
621 | |
622 | ;; Here we provide several examples of famous keyboard layouts. | |
9a02c067 | 623 | ;; This is a candidate for a language environment-dependent setting. |
4ed46869 KH |
624 | (defvar quail-keyboard-layout-alist |
625 | (list | |
95109387 | 626 | (cons "standard" quail-keyboard-layout-standard) |
4ed46869 | 627 | '("sun-type3" . "\ |
ea3fb7d2 | 628 | \ |
4ed46869 KH |
629 | 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\ |
630 | qQwWeErRtTyYuUiIoOpP[{]} \ | |
631 | aAsSdDfFgGhHjJkKlL;:'\" \ | |
ea3fb7d2 KH |
632 | zZxXcCvVbBnNmM,<.>/? \ |
633 | ") | |
50b190e4 KH |
634 | '("atari-german" . "\ |
635 | \ | |
636 | 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \ | |
637 | qQwWeErRtTzZuUiIoOpP\374\334+* \ | |
638 | aAsSdDfFgGhHjJkKlL\366\326\344\304~| \ | |
639 | <>yYxXcCvVbBnNmM,;.:-_ \ | |
640 | ") | |
99da6af3 KH |
641 | |
642 | '("pc102-de" . "\ | |
643 | \ | |
644 | ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \ | |
645 | qQwWeErRtTzZuUiIoOpP\374\334+* \ | |
646 | aAsSdDfFgGhHjJkKlL\366\326\344\304 \ | |
647 | <>yYxXcCvVbBnNmM,;.:-_ \ | |
648 | ") | |
649 | ||
95109387 KH |
650 | '("jp106" . "\ |
651 | \ | |
652 | 1!2\"3#4$5%6&7'8(9)0~-=^~\\| \ | |
653 | qQwWeErRtTyYuUiIoOpP@`[{ \ | |
654 | aAsSdDfFgGhHjJkKlL;+:*]} \ | |
655 | zZxXcCvVbBnNmM,<.>/?\\_ \ | |
656 | ") | |
35fffde1 DL |
657 | '("pc105-uk" . "\ |
658 | \ | |
d14a2a1f | 659 | `\2541!2\"3\2434$5%6^7&8*9(0)-_=+ \ |
35fffde1 DL |
660 | qQwWeErRtTyYuUiIoOpP[{]} \ |
661 | aAsSdDfFgGhHjJkKlL;:'@#~ \ | |
662 | \\|zZxXcCvVbBnNmM,<.>/? \ | |
663 | ") | |
95109387 | 664 | ) |
4ed46869 KH |
665 | "Alist of keyboard names and corresponding layout strings. |
666 | See the documentation of `quail-keyboard-layout' for the format of | |
95109387 KH |
667 | the layout string.") |
668 | ||
2d8a4bbe DL |
669 | (defcustom quail-keyboard-layout quail-keyboard-layout-standard |
670 | "A string which represents physical key layout of a particular keyboard. | |
671 | We assume there are six rows and each row has 15 keys (columns), | |
672 | the first row is above the `1' - `0' row, | |
673 | the first column of the second row is left of key `1', | |
674 | the first column of the third row is left of key `q', | |
675 | the first column of the fourth row is left of key `a', | |
676 | the first column of the fifth row is left of key `z', | |
677 | the sixth row is below the `z' - `/' row. | |
678 | Nth (N is even) and (N+1)th characters in the string are non-shifted | |
679 | and shifted characters respectively at the same location. | |
680 | The location of Nth character is row (N / 30) and column ((N mod 30) / 2). | |
681 | The command `quail-set-keyboard-layout' usually sets this variable." | |
682 | :group 'quail | |
683 | :type `(choice | |
684 | ,@(mapcar (lambda (pair) | |
685 | (list 'const :tag (car pair) (cdr pair))) | |
686 | quail-keyboard-layout-alist) | |
687 | (string :tag "Other"))) | |
688 | ||
95109387 KH |
689 | ;; A non-standard keyboard layout may miss some key locations of the |
690 | ;; standard layout while having additional key locations not in the | |
691 | ;; standard layout. This alist maps those additional key locations to | |
692 | ;; the missing locations. The value is updated automatically by | |
693 | ;; quail-set-keyboard-layout. | |
694 | (defvar quail-keyboard-layout-substitution nil) | |
695 | ||
696 | (defun quail-update-keyboard-layout (kbd-type) | |
697 | (let ((layout (assoc kbd-type quail-keyboard-layout-alist))) | |
698 | (if (null layout) | |
699 | ;; Here, we had better ask a user to define his own keyboard | |
700 | ;; layout interactively. | |
701 | (error "Unknown keyboard type `%s'" kbd-type)) | |
702 | (setq quail-keyboard-layout (cdr layout)) | |
703 | (let ((i quail-keyboard-layout-len) | |
704 | subst-list missing-list) | |
705 | ;; Sum up additional key locations not in the standard layout in | |
706 | ;; subst-list, and missing key locations in missing-list. | |
707 | (while (> i 0) | |
708 | (setq i (1- i)) | |
709 | (if (= (aref quail-keyboard-layout i) ? ) | |
710 | (if (/= (aref quail-keyboard-layout-standard i) ? ) | |
711 | (setq missing-list (cons i missing-list))) | |
712 | (if (= (aref quail-keyboard-layout-standard i) ? ) | |
713 | (setq subst-list (cons (cons i nil) subst-list))))) | |
714 | (setq quail-keyboard-layout-substitution subst-list) | |
715 | ;; If there are additional key locations, map them to missing | |
716 | ;; key locations. | |
717 | (while missing-list | |
718 | (while (and subst-list (cdr (car subst-list))) | |
719 | (setq subst-list (cdr subst-list))) | |
720 | (if subst-list | |
721 | (setcdr (car subst-list) (car missing-list))) | |
722 | (setq missing-list (cdr missing-list)))))) | |
723 | ||
724 | (defcustom quail-keyboard-layout-type "standard" | |
725 | "Type of keyboard layout used in Quail base input method. | |
726 | Available types are listed in the variable `quail-keyboard-layout-alist'." | |
727 | :group 'quail | |
35fffde1 DL |
728 | :type (cons 'choice (mapcar (lambda (elt) |
729 | (list 'const (car elt))) | |
730 | quail-keyboard-layout-alist)) | |
95109387 KH |
731 | :set #'(lambda (symbol value) |
732 | (quail-update-keyboard-layout value) | |
733 | (set symbol value))) | |
4ed46869 | 734 | |
44baad62 | 735 | ;;;###autoload |
4ed46869 KH |
736 | (defun quail-set-keyboard-layout (kbd-type) |
737 | "Set the current keyboard layout to the same as keyboard KBD-TYPE. | |
738 | ||
739 | Since some Quail packages depends on a physical layout of keys (not | |
740 | characters generated by them), those are created by assuming the | |
741 | standard layout defined in `quail-keyboard-layout-standard'. This | |
742 | function tells Quail system the layout of your keyboard so that what | |
743 | you type is correctly handled." | |
744 | (interactive | |
91e947ce | 745 | (let* ((completion-ignore-case t) |
4ed46869 KH |
746 | (type (completing-read "Keyboard type: " |
747 | quail-keyboard-layout-alist))) | |
748 | (list type))) | |
95109387 KH |
749 | (quail-update-keyboard-layout kbd-type) |
750 | (setq quail-keyboard-layout-type kbd-type)) | |
4ed46869 | 751 | |
95109387 KH |
752 | (defun quail-keyboard-translate (char) |
753 | "Translate CHAR to the one in the standard keyboard layout." | |
4ed46869 | 754 | (if (eq quail-keyboard-layout quail-keyboard-layout-standard) |
50b190e4 KH |
755 | ;; All Quail packages are designed based on |
756 | ;; `quail-keyboard-layout-standard'. | |
95109387 | 757 | char |
4ed46869 | 758 | (let ((i 0)) |
95109387 | 759 | ;; Find the key location on the current keyboard layout. |
4ed46869 | 760 | (while (and (< i quail-keyboard-layout-len) |
95109387 | 761 | (/= char (aref quail-keyboard-layout i))) |
4ed46869 KH |
762 | (setq i (1+ i))) |
763 | (if (= i quail-keyboard-layout-len) | |
95109387 | 764 | ;; CHAR is not in quail-keyboard-layout, which means that a |
50b190e4 | 765 | ;; user typed a key which generated a character code to be |
95109387 | 766 | ;; handled out of Quail. Just return CHAR and make |
50b190e4 | 767 | ;; quail-execute-non-quail-command handle it correctly. |
95109387 KH |
768 | char |
769 | (let ((ch (aref quail-keyboard-layout-standard i))) | |
770 | (if (= ch ?\ ) | |
771 | ;; This location not available in the standard keyboard | |
772 | ;; layout. Check if the location is used to substitute | |
773 | ;; for the other location of the standard layout. | |
774 | (if (setq i (cdr (assq i quail-keyboard-layout-substitution))) | |
775 | (aref quail-keyboard-layout-standard i) | |
776 | ;; Just return CHAR as well as above. | |
777 | char) | |
778 | ch)))))) | |
779 | ||
8179cccd KH |
780 | (defun quail-keyseq-translate (keyseq) |
781 | (apply 'string | |
782 | (mapcar (function (lambda (x) (quail-keyboard-translate x))) | |
783 | keyseq))) | |
784 | ||
95109387 | 785 | (defun quail-insert-kbd-layout (kbd-layout) |
74ace46a DL |
786 | "Insert the visual keyboard layout table according to KBD-LAYOUT. |
787 | The format of KBD-LAYOUT is the same as `quail-keyboard-layout'." | |
95109387 KH |
788 | (let (done-list layout i ch) |
789 | ;; At first, convert KBD-LAYOUT to the same size vector that | |
790 | ;; contains translated character or string. | |
791 | (setq layout (string-to-vector kbd-layout) | |
792 | i 0) | |
793 | (while (< i quail-keyboard-layout-len) | |
794 | (setq ch (aref kbd-layout i)) | |
795 | (if (quail-kbd-translate) | |
796 | (setq ch (quail-keyboard-translate ch))) | |
797 | (let* ((map (cdr (assq ch (cdr (quail-map))))) | |
798 | (translation (and map (quail-get-translation | |
799 | (car map) (char-to-string ch) 1)))) | |
800 | (if translation | |
801 | (progn | |
802 | (if (consp translation) | |
803 | (setq translation (aref (cdr translation) 0))) | |
804 | (setq done-list (cons translation done-list))) | |
805 | (setq translation ch)) | |
806 | (aset layout i translation)) | |
807 | (setq i (1+ i))) | |
808 | ||
809 | (let ((pos (point)) | |
810 | (bar "|") | |
811 | lower upper row) | |
812 | ;; Make table without horizontal lines. Each column for a key | |
813 | ;; has the form "| LU |" where L is for lower key and and U is | |
814 | ;; for a upper key. If width of L (U) is greater than 1, | |
815 | ;; preceding (following) space is not inserted. | |
816 | (put-text-property 0 1 'face 'bold bar) | |
817 | (setq i 0) | |
818 | (while (< i quail-keyboard-layout-len) | |
819 | (when (= (% i 30) 0) | |
820 | (setq row (/ i 30)) | |
821 | (if (> row 1) | |
822 | (insert-char 32 (+ row (/ (- row 2) 2))))) | |
823 | (setq lower (aref layout i) | |
824 | upper (aref layout (1+ i))) | |
825 | (if (and (integerp lower) (>= lower 128) (< lower 256)) | |
826 | (setq lower (unibyte-char-to-multibyte lower))) | |
827 | (if (and (integerp upper) (>= upper 128) (< upper 256)) | |
828 | (setq upper (unibyte-char-to-multibyte upper))) | |
829 | (insert bar) | |
830 | (if (= (if (stringp lower) (string-width lower) (char-width lower)) 1) | |
831 | (insert " ")) | |
832 | (insert lower upper) | |
833 | (if (= (if (stringp upper) (string-width upper) (char-width upper)) 1) | |
834 | (insert " ")) | |
835 | (setq i (+ i 2)) | |
836 | (if (= (% i 30) 0) | |
837 | (insert bar "\n"))) | |
838 | ;; Insert horizontal lines while deleting blank key columns at the | |
839 | ;; beginning and end of each line. | |
840 | (save-restriction | |
841 | (narrow-to-region pos (point)) | |
842 | (goto-char pos) | |
843 | ;;(while (looking-at "[| ]*$") | |
844 | ;;(forward-line 1) | |
845 | ;;(delete-region pos (point))) | |
846 | (let ((from1 100) (to1 0) from2 to2) | |
847 | (while (not (eobp)) | |
848 | (if (looking-at "[| ]*$") | |
849 | ;; The entire row is blank. | |
850 | (delete-region (point) (match-end 0)) | |
851 | ;; Delete blank key columns at the head. | |
852 | (if (looking-at " *\\(| \\)+") | |
853 | (subst-char-in-region (point) (match-end 0) ?| ? )) | |
854 | ;; Delete blank key columns at the tail. | |
855 | (if (re-search-forward "\\( |\\)+$" (line-end-position) t) | |
856 | (delete-region (match-beginning 0) (point))) | |
857 | (beginning-of-line)) | |
858 | ;; Calculate the start and end columns of a horizontal line. | |
859 | (if (eolp) | |
860 | (setq from2 from1 to2 to1) | |
861 | (skip-chars-forward " ") | |
862 | (setq from2 (current-column)) | |
863 | (end-of-line) | |
864 | (setq to2 (current-column)) | |
865 | (if (< from2 from1) | |
866 | (setq from1 from2)) | |
867 | (if (> to2 to1) | |
868 | (setq to1 to2)) | |
869 | (beginning-of-line)) | |
870 | ;; If the previous or the current line has at least one key | |
871 | ;; column, insert a horizontal line. | |
872 | (when (> to1 0) | |
873 | (insert-char 32 from1) | |
874 | (setq pos (point)) | |
875 | (insert "+") | |
876 | (insert-char ?- (- (- to1 from1) 2)) | |
877 | (insert "+") | |
878 | (put-text-property pos (point) 'face 'bold) | |
879 | (insert "\n")) | |
880 | (setq from1 from2 to1 to2) | |
881 | (forward-line 1))) | |
882 | ;; Insert "space bar" box. | |
883 | (forward-line -1) | |
884 | (setq pos (point)) | |
885 | (insert | |
886 | " +-----------------------------+ | |
887 | | space bar | | |
888 | +-----------------------------+ | |
889 | ") | |
890 | (put-text-property pos (point) 'face 'bold) | |
891 | (insert ?\n))) | |
892 | ||
893 | done-list)) | |
894 | ||
895 | ;;;###autoload | |
896 | (defun quail-show-keyboard-layout (&optional keyboard-type) | |
362a8065 KH |
897 | "Show the physical layout of the keyboard type KEYBOARD-TYPE. |
898 | ||
899 | The variable `quail-keyboard-layout-type' holds the currently selected | |
900 | keyboard type." | |
95109387 | 901 | (interactive |
d14a2a1f | 902 | (list (completing-read "Keyboard type (default, current choice): " |
95109387 KH |
903 | quail-keyboard-layout-alist |
904 | nil t))) | |
905 | (or (and keyboard-type (> (length keyboard-type) 0)) | |
906 | (setq keyboard-type quail-keyboard-layout-type)) | |
907 | (let ((layout (assoc keyboard-type quail-keyboard-layout-alist))) | |
908 | (or layout | |
909 | (error "Unknown keyboard type: %s" keyboard-type)) | |
910 | (with-output-to-temp-buffer "*Help*" | |
24790d0c | 911 | (with-current-buffer standard-output |
95109387 KH |
912 | (insert "Keyboard layout (keyboard type: " |
913 | keyboard-type | |
914 | ")\n") | |
915 | (quail-insert-kbd-layout (cdr layout)))))) | |
4ed46869 KH |
916 | |
917 | ;; Quail map | |
918 | ||
919 | (defsubst quail-map-p (object) | |
920 | "Return t if OBJECT is a Quail map. | |
921 | ||
922 | A Quail map holds information how a particular key should be translated. | |
923 | Its format is (TRANSLATION . ALIST). | |
924 | TRANSLATION is either a character, or a cons (INDEX . VECTOR). | |
925 | In the latter case, each element of VECTOR is a candidate for the translation, | |
926 | and INDEX points the currently selected translation. | |
927 | ||
928 | ALIST is normally a list of elements that look like (CHAR . DEFN), | |
929 | where DEFN is another Quail map for a longer key (CHAR added to the | |
930 | current key). It may also be a symbol of a function which returns an | |
931 | alist of the above format. | |
932 | ||
933 | Just after a Quail package is read, TRANSLATION may be a string or a | |
934 | vector. Then each element of the string or vector is a candidate for | |
935 | the translation. These objects are transformed to cons cells in the | |
936 | format \(INDEX . VECTOR), as described above." | |
937 | (and (consp object) | |
938 | (let ((translation (car object))) | |
ff913e92 | 939 | (or (integerp translation) (null translation) |
4ed46869 | 940 | (vectorp translation) (stringp translation) |
ff913e92 KH |
941 | (symbolp translation) |
942 | (and (consp translation) (not (vectorp (cdr translation)))))) | |
4ed46869 | 943 | (let ((alist (cdr object))) |
ff913e92 KH |
944 | (or (and (listp alist) (consp (car alist))) |
945 | (symbolp alist))))) | |
4ed46869 | 946 | |
ff913e92 | 947 | ;;;###autoload |
4ed46869 KH |
948 | (defmacro quail-define-rules (&rest rules) |
949 | "Define translation rules of the current Quail package. | |
950 | Each argument is a list of KEY and TRANSLATION. | |
951 | KEY is a string meaning a sequence of keystrokes to be translated. | |
952 | TRANSLATION is a character, a string, a vector, a Quail map, or a function. | |
9429dee6 | 953 | If it is a character, it is the sole translation of KEY. |
4ed46869 KH |
954 | If it is a string, each character is a candidate for the translation. |
955 | If it is a vector, each element (string or character) is a candidate | |
956 | for the translation. | |
957 | In these cases, a key specific Quail map is generated and assigned to KEY. | |
958 | ||
959 | If TRANSLATION is a Quail map or a function symbol which returns a Quail map, | |
bb63aae5 KH |
960 | it is used to handle KEY. |
961 | ||
962 | The first argument may be an alist of annotations for the following | |
963 | rules. Each element has the form (ANNOTATION . VALUE), where | |
964 | ANNOTATION is a symbol indicating the annotation type. Currently | |
965 | the following annotation types are supported. | |
966 | ||
967 | append -- the value non-nil means that the following rules should | |
968 | be appended to the rules of the current Quail package. | |
969 | ||
970 | face -- the value is a face to use for displaying TRANSLATIONs in | |
971 | candidate list. | |
972 | ||
973 | advice -- the value is a function to call after one of RULES is | |
974 | selected. The function is called with one argument, the | |
975 | selected TRANSLATION string, after the TRANSLATION is | |
976 | inserted. | |
977 | ||
978 | no-decode-map --- the value non-nil means that decoding map is not | |
979 | generated for the following translations." | |
980 | (let ((l rules) | |
981 | append no-decode-map props) | |
982 | ;; If the first argument is an alist of annotations, handle them. | |
983 | (if (consp (car (car l))) | |
984 | (let ((annotations (car l))) | |
985 | (setq append (assq 'append annotations)) | |
986 | (if append | |
987 | (setq annotations (delete append annotations) | |
988 | append (cdr append))) | |
989 | (setq no-decode-map (assq 'no-decode-map annotations)) | |
990 | (if no-decode-map | |
991 | (setq annotations (delete no-decode-map annotations) | |
992 | no-decode-map (cdr no-decode-map))) | |
993 | ;; Convert the remaining annoations to property list PROPS. | |
994 | (while annotations | |
995 | (setq props | |
996 | (cons (car (car annotations)) | |
997 | (cons (cdr (car annotations)) | |
998 | props)) | |
999 | annotations (cdr annotations))) | |
1000 | (setq l (cdr l)))) | |
1001 | ;; Process the remaining arguments one by one. | |
1002 | (if append | |
1003 | ;; There's no way to add new rules at compiling time. | |
1004 | `(let ((tail ',l) | |
1005 | (map (quail-map)) | |
1006 | (decode-map (and (quail-decode-map) (not ,no-decode-map))) | |
1007 | (properties ',props) | |
1008 | key trans) | |
1009 | (while tail | |
1010 | (setq key (car (car tail)) trans (car (cdr (car tail))) | |
1011 | tail (cdr tail)) | |
1012 | (quail-defrule-internal key trans map t decode-map properties))) | |
1013 | ;; We can build up quail map and decode map at compiling time. | |
1014 | (let ((map (list nil)) | |
1015 | (decode-map (if (not no-decode-map) (list 'decode-map))) | |
1016 | key trans) | |
4ed46869 | 1017 | (while l |
bb63aae5 KH |
1018 | (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l)) |
1019 | (quail-defrule-internal key trans map t decode-map props)) | |
1020 | `(if (not (quail-decode-map)) | |
1021 | (quail-install-map ',map) | |
1022 | (quail-install-map ',map) | |
1023 | (quail-install-decode-map ',decode-map)))))) | |
4ed46869 | 1024 | |
ff913e92 | 1025 | ;;;###autoload |
817e162f | 1026 | (defun quail-install-map (map &optional name) |
4ed46869 | 1027 | "Install the Quail map MAP in the current Quail package. |
817e162f KH |
1028 | |
1029 | Optional 2nd arg NAME, if non-nil, is a name of Quail package for | |
1030 | which to install MAP. | |
1031 | ||
4ed46869 KH |
1032 | The installed map can be referred by the function `quail-map'." |
1033 | (if (null quail-current-package) | |
1034 | (error "No current Quail package")) | |
1035 | (if (null (quail-map-p map)) | |
1036 | (error "Invalid Quail map `%s'" map)) | |
1037 | (setcar (cdr (cdr quail-current-package)) map)) | |
1038 | ||
bb63aae5 KH |
1039 | ;;;###autoload |
1040 | (defun quail-install-decode-map (decode-map &optional name) | |
1041 | "Install the Quail decode map DECODE-MAP in the current Quail package. | |
1042 | ||
1043 | Optional 2nd arg NAME, if non-nil, is a name of Quail package for | |
1044 | which to install MAP. | |
1045 | ||
1046 | The installed decode map can be referred by the function `quail-decode-map'." | |
1047 | (if (null quail-current-package) | |
1048 | (error "No current Quail package")) | |
4f013856 KH |
1049 | (if (if (consp decode-map) |
1050 | (eq (car decode-map) 'decode-map) | |
1051 | (if (char-table-p decode-map) | |
1052 | (eq (char-table-subtype decode-map) 'quail-decode-map))) | |
1053 | (setcar (nthcdr 10 quail-current-package) decode-map) | |
1054 | (error "Invalid Quail decode map `%s'" decode-map))) | |
1055 | ||
bb63aae5 | 1056 | |
ff913e92 | 1057 | ;;;###autoload |
7b5ebb00 | 1058 | (defun quail-defrule (key translation &optional name append) |
4ed46869 KH |
1059 | "Add one translation rule, KEY to TRANSLATION, in the current Quail package. |
1060 | KEY is a string meaning a sequence of keystrokes to be translated. | |
ff913e92 | 1061 | TRANSLATION is a character, a string, a vector, a Quail map, |
7d842556 | 1062 | a function, or a cons. |
4ed46869 KH |
1063 | It it is a character, it is the sole translation of KEY. |
1064 | If it is a string, each character is a candidate for the translation. | |
1065 | If it is a vector, each element (string or character) is a candidate | |
7d842556 | 1066 | for the translation. |
ff913e92 | 1067 | If it is a cons, the car is one of the above and the cdr is a function |
7d842556 KH |
1068 | to call when translating KEY (the return value is assigned to the |
1069 | variable `quail-current-data'). If the cdr part is not a function, | |
1070 | the value itself is assigned to `quail-current-data'. | |
4ed46869 KH |
1071 | In these cases, a key specific Quail map is generated and assigned to KEY. |
1072 | ||
1073 | If TRANSLATION is a Quail map or a function symbol which returns a Quail map, | |
1074 | it is used to handle KEY. | |
7b5ebb00 KH |
1075 | |
1076 | Optional 3rd argument NAME, if specified, says which Quail package | |
4ed46869 | 1077 | to define this translation rule in. The default is to define it in the |
7b5ebb00 KH |
1078 | current Quail package. |
1079 | ||
1080 | Optional 4th argument APPEND, if non-nil, appends TRANSLATION | |
1081 | to the current translations for KEY instead of replacing them." | |
4ed46869 KH |
1082 | (if name |
1083 | (let ((package (quail-package name))) | |
1084 | (if (null package) | |
1085 | (error "No Quail package `%s'" name)) | |
1086 | (setq quail-current-package package))) | |
7b5ebb00 | 1087 | (quail-defrule-internal key translation (quail-map) append)) |
4ed46869 | 1088 | |
ff913e92 | 1089 | ;;;###autoload |
bb63aae5 KH |
1090 | (defun quail-defrule-internal (key trans map &optional append decode-map props) |
1091 | "Define KEY as TRANS in a Quail map MAP. | |
1092 | ||
1093 | If Optional 4th arg APPEND is non-nil, TRANS is appended to the | |
1094 | current translations for KEY instead of replacing them. | |
1095 | ||
1096 | Optional 5th arg DECODE-MAP is a Quail decode map. | |
1097 | ||
1098 | Optional 6th arg PROPS is a property list annotating TRANS. See the | |
1099 | function `quail-define-rules' for the detail." | |
4ed46869 KH |
1100 | (if (null (stringp key)) |
1101 | "Invalid Quail key `%s'" key) | |
1102 | (if (not (or (numberp trans) (stringp trans) (vectorp trans) | |
ff913e92 | 1103 | (consp trans) |
4ed46869 KH |
1104 | (symbolp trans) |
1105 | (quail-map-p trans))) | |
1106 | (error "Invalid Quail translation `%s'" trans)) | |
1107 | (if (null (quail-map-p map)) | |
1108 | (error "Invalid Quail map `%s'" map)) | |
1109 | (let ((len (length key)) | |
1110 | (idx 0) | |
1111 | ch entry) | |
ff913e92 | 1112 | ;; Make a map for registering TRANS if necessary. |
4ed46869 KH |
1113 | (while (< idx len) |
1114 | (if (null (consp map)) | |
1115 | ;; We come here, for example, when we try to define a rule | |
1116 | ;; for "ABC" but a rule for "AB" is already defined as a | |
1117 | ;; symbol. | |
1118 | (error "Quail key %s is too long" key)) | |
1119 | (setq ch (aref key idx) | |
1120 | entry (assq ch (cdr map))) | |
1121 | (if (null entry) | |
1122 | (progn | |
1123 | (setq entry (cons ch (list nil))) | |
1124 | (setcdr map (cons entry (cdr map))))) | |
1125 | (setq map (cdr entry)) | |
1126 | (setq idx (1+ idx))) | |
1127 | (if (symbolp trans) | |
1128 | (if (cdr map) | |
1129 | ;; We come here, for example, when we try to define a rule | |
1130 | ;; for "AB" as a symbol but a rule for "ABC" is already | |
1131 | ;; defined. | |
1132 | (error "Quail key %s is too short" key) | |
1133 | (setcdr entry trans)) | |
1134 | (if (quail-map-p trans) | |
1135 | (if (not (listp (cdr map))) | |
1136 | ;; We come here, for example, when we try to define a rule | |
1137 | ;; for "AB" as a symbol but a rule for "ABC" is already | |
1138 | ;; defined. | |
1139 | (error "Quail key %s is too short" key) | |
1140 | (if (not (listp (cdr trans))) | |
1141 | (if (cdr map) | |
1142 | ;; We come here, for example, when we try to | |
1143 | ;; define a rule for "AB" as a symbol but a rule | |
1144 | ;; for "ABC" is already defined. | |
1145 | (error "Quail key %s is too short" key) | |
1146 | (setcdr entry trans)) | |
1147 | (setcdr entry (append trans (cdr map))))) | |
bb63aae5 KH |
1148 | ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS |
1149 | ;; to a vector of strings, add PROPS to each string and record | |
1150 | ;; this rule in DECODE-MAP. | |
1151 | (when (and (or props decode-map) | |
1152 | (not (consp trans)) (not (symbolp trans))) | |
1153 | (if (integerp trans) | |
1154 | (setq trans (vector trans)) | |
1155 | (if (stringp trans) | |
1156 | (setq trans (string-to-vector trans)))) | |
1157 | (let ((len (length trans)) | |
1158 | elt) | |
1159 | (while (> len 0) | |
1160 | (setq len (1- len)) | |
1161 | (setq elt (aref trans len)) | |
1162 | (if (integerp elt) | |
1163 | (setq elt (char-to-string elt))) | |
1164 | (aset trans len elt) | |
1165 | (if props | |
1166 | (add-text-properties 0 (length elt) props elt)) | |
1167 | (if decode-map | |
1168 | (setcdr decode-map | |
1169 | (cons (cons elt key) (cdr decode-map))))))) | |
7b5ebb00 KH |
1170 | (if (and (car map) append) |
1171 | (let ((prev (quail-get-translation (car map) key len))) | |
1172 | (if (integerp prev) | |
1173 | (setq prev (vector prev)) | |
1174 | (setq prev (cdr prev))) | |
1175 | (if (integerp trans) | |
1176 | (setq trans (vector trans)) | |
1177 | (if (stringp trans) | |
1178 | (setq trans (string-to-vector trans)))) | |
1179 | (setq trans | |
1180 | (cons (list 0 0 0 0 nil) | |
1181 | (vconcat prev trans))))) | |
1182 | (setcar map trans))))) | |
4ed46869 | 1183 | |
ff913e92 KH |
1184 | (defun quail-get-translation (def key len) |
1185 | "Return the translation specified as DEF for KEY of length LEN. | |
4ed46869 KH |
1186 | The translation is either a character or a cons of the form (INDEX . VECTOR), |
1187 | where VECTOR is a vector of candidates (character or string) for | |
1188 | the translation, and INDEX points into VECTOR to specify the currently | |
1189 | selected translation." | |
ff913e92 | 1190 | (if (and def (symbolp def)) |
817e162f KH |
1191 | (if (functionp def) |
1192 | ;; DEF is a symbol of a function which returns valid translation. | |
1193 | (setq def (funcall def key len)) | |
1194 | (setq def nil))) | |
ff913e92 KH |
1195 | (if (and (consp def) (not (vectorp (cdr def)))) |
1196 | (setq def (car def))) | |
1197 | ||
1198 | (cond | |
1199 | ((or (integerp def) (consp def)) | |
1200 | def) | |
1201 | ||
1202 | ((null def) | |
1203 | ;; No translation. | |
1204 | nil) | |
1205 | ||
1206 | ((stringp def) | |
95109387 KH |
1207 | ;; If the length is 1, we don't need vector but a single candidate |
1208 | ;; as the translation. | |
ff913e92 KH |
1209 | (if (= (length def) 1) |
1210 | (aref def 0) | |
95109387 KH |
1211 | ;; Each character in DEF is a candidate of translation. Reform |
1212 | ;; it as (INDICES . VECTOR). | |
1213 | (cons (list 0 0 0 0 nil) (string-to-vector def)))) | |
ff913e92 KH |
1214 | |
1215 | ((vectorp def) | |
95109387 KH |
1216 | ;; If the length is 1, and the length of element string is 1, we |
1217 | ;; don't need vector but a single candidate as the translation. | |
1218 | (if (and (= (length def) 1) | |
1219 | (= (length (aref def 0)) 1)) | |
1220 | (aref (aref def 0) 0) | |
1221 | ;; Each element (string or character) in DEF is a candidate of | |
1222 | ;; translation. Reform it as (INDICES . VECTOR). | |
1223 | (cons (list 0 0 0 0 nil) def))) | |
ff913e92 KH |
1224 | |
1225 | (t | |
1226 | (error "Invalid object in Quail map: %s" def)))) | |
4ed46869 | 1227 | |
5b6156fa | 1228 | (defun quail-lookup-key (key &optional len not-reset-indices) |
4ed46869 KH |
1229 | "Lookup KEY of length LEN in the current Quail map and return the definition. |
1230 | The returned value is a Quail map specific to KEY." | |
7d842556 KH |
1231 | (or len |
1232 | (setq len (length key))) | |
4ed46869 KH |
1233 | (let ((idx 0) |
1234 | (map (quail-map)) | |
1235 | (kbd-translate (quail-kbd-translate)) | |
ff913e92 | 1236 | slot ch translation def) |
4ed46869 KH |
1237 | (while (and map (< idx len)) |
1238 | (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx)) | |
1239 | (aref key idx))) | |
1240 | (setq idx (1+ idx)) | |
1241 | (if (and (cdr map) (symbolp (cdr map))) | |
1242 | (setcdr map (funcall (cdr map) key idx))) | |
1243 | (setq slot (assq ch (cdr map))) | |
1244 | (if (and (cdr slot) (symbolp (cdr slot))) | |
1245 | (setcdr slot (funcall (cdr slot) key idx))) | |
1246 | (setq map (cdr slot))) | |
ff913e92 | 1247 | (setq def (car map)) |
7d842556 | 1248 | (setq quail-current-translations nil) |
ff913e92 | 1249 | (if (and map (setq translation (quail-get-translation def key len))) |
4ed46869 | 1250 | (progn |
ff913e92 KH |
1251 | (if (and (consp def) (not (vectorp (cdr def)))) |
1252 | (progn | |
1253 | (if (not (equal (car def) translation)) | |
1254 | ;; We must reflect TRANSLATION to car part of DEF. | |
1255 | (setcar def translation)) | |
1256 | (setq quail-current-data | |
1257 | (if (functionp (cdr def)) | |
1258 | (funcall (cdr def)) | |
1259 | (cdr def)))) | |
1260 | (if (not (equal def translation)) | |
1261 | ;; We must reflect TRANSLATION to car part of MAP. | |
1262 | (setcar map translation))) | |
74ace46a | 1263 | (if (and (consp translation) (vectorp (cdr translation))) |
4ed46869 KH |
1264 | (progn |
1265 | (setq quail-current-translations translation) | |
5b6156fa | 1266 | (if (and (not not-reset-indices) (quail-forget-last-selection)) |
8024de45 KH |
1267 | (setcar (car quail-current-translations) 0)))))) |
1268 | ;; We may have to reform cdr part of MAP. | |
1269 | (if (and (cdr map) (functionp (cdr map))) | |
1270 | (setcdr map (funcall (cdr map) key len))) | |
4ed46869 KH |
1271 | map)) |
1272 | ||
b58fc490 KH |
1273 | (put 'quail-error 'error-conditions '(quail-error error)) |
1274 | (defun quail-error (&rest args) | |
1275 | (signal 'quail-error (apply 'format args))) | |
1276 | ||
c5f3770d | 1277 | (defun quail-input-string-to-events (str) |
74ace46a DL |
1278 | "Convert input string STR to a list of events. |
1279 | Do so while interleaving with the following special events: | |
1280 | \(compose-last-chars LEN COMPONENTS) | |
1281 | \(quail-advice INPUT-STRING)" | |
9a02c067 DL |
1282 | (let* ((events (mapcar |
1283 | (lambda (c) | |
35fffde1 | 1284 | ;; This gives us the chance to unify on input |
9372a49f DL |
1285 | ;; (e.g. using ucs-tables.el). |
1286 | (or (and translation-table-for-input | |
1287 | (aref translation-table-for-input c)) | |
e5ecf6e9 | 1288 | c)) |
9a02c067 | 1289 | str)) |
c5f3770d KH |
1290 | (len (length str)) |
1291 | (idx len) | |
1292 | composition from to) | |
1293 | (while (and (> idx 0) | |
1294 | (setq composition (find-composition idx 0 str t))) | |
1295 | (setq from (car composition) to (nth 1 composition)) | |
1296 | (setcdr (nthcdr (1- to) events) | |
1297 | (cons (list 'compose-last-chars (- to from) | |
1298 | (and (not (nth 3 composition)) (nth 2 composition))) | |
1299 | (nthcdr to events))) | |
1300 | (setq idx (1- from))) | |
1301 | (if (or (get-text-property 0 'advice str) | |
1302 | (next-single-property-change 0 'advice str)) | |
1303 | (setq events | |
1304 | (nconc events (list (list 'quail-advice str))))) | |
1305 | events)) | |
1306 | ||
b58fc490 KH |
1307 | (defvar quail-translating nil) |
1308 | (defvar quail-converting nil) | |
d91eafdf | 1309 | (defvar quail-conversion-str nil) |
b58fc490 KH |
1310 | |
1311 | (defun quail-input-method (key) | |
1312 | (if (or buffer-read-only | |
b45d8d64 KH |
1313 | overriding-terminal-local-map |
1314 | overriding-local-map) | |
b58fc490 KH |
1315 | (list key) |
1316 | (quail-setup-overlays (quail-conversion-keymap)) | |
d91eafdf | 1317 | (let ((modified-p (buffer-modified-p)) |
276bc002 KH |
1318 | (buffer-undo-list t) |
1319 | (inhibit-modification-hooks t)) | |
b58fc490 | 1320 | (unwind-protect |
c5f3770d KH |
1321 | (let ((input-string (if (quail-conversion-keymap) |
1322 | (quail-start-conversion key) | |
1323 | (quail-start-translation key)))) | |
17388a62 | 1324 | (setq quail-guidance-str "") |
c5f3770d KH |
1325 | (when (and (stringp input-string) |
1326 | (> (length input-string) 0)) | |
1327 | (if input-method-exit-on-first-char | |
1328 | (list (aref input-string 0)) | |
1329 | (quail-input-string-to-events input-string)))) | |
b45d8d64 | 1330 | (quail-delete-overlays) |
b58fc490 | 1331 | (set-buffer-modified-p modified-p) |
b45d8d64 KH |
1332 | ;; Run this hook only when the current input method doesn't require |
1333 | ;; conversion. When conversion is required, the conversion function | |
1334 | ;; should run this hook at a proper timing. | |
1335 | (unless (quail-conversion-keymap) | |
1336 | (run-hooks 'input-method-after-insert-chunk-hook)))))) | |
b58fc490 KH |
1337 | |
1338 | (defun quail-overlay-region-events (overlay) | |
1339 | (let ((start (overlay-start overlay)) | |
1340 | (end (overlay-end overlay))) | |
1341 | (if (< start end) | |
1342 | (prog1 | |
1343 | (string-to-list (buffer-substring start end)) | |
1344 | (delete-region start end))))) | |
1345 | ||
d91eafdf KH |
1346 | (defsubst quail-delete-region () |
1347 | "Delete the text in the current translation region of Quail." | |
1348 | (if (overlay-start quail-overlay) | |
1349 | (delete-region (overlay-start quail-overlay) | |
1350 | (overlay-end quail-overlay)))) | |
1351 | ||
b58fc490 | 1352 | (defun quail-start-translation (key) |
c5f3770d KH |
1353 | "Start translation of the typed character KEY by the current Quail package. |
1354 | Return the input string." | |
b58fc490 | 1355 | ;; Check the possibility of translating KEY. |
d91eafdf KH |
1356 | ;; If KEY is nil, we can anyway start translation. |
1357 | (if (or (and (integerp key) | |
1358 | (assq (if (quail-kbd-translate) | |
1359 | (quail-keyboard-translate key) key) | |
1360 | (cdr (quail-map)))) | |
1361 | (null key)) | |
cd30a521 | 1362 | ;; OK, we can start translation. |
d91eafdf KH |
1363 | (let* ((echo-keystrokes 0) |
1364 | (help-char nil) | |
1365 | (overriding-terminal-local-map (quail-translation-keymap)) | |
b58fc490 | 1366 | (generated-events nil) |
b0fdefb4 | 1367 | (input-method-function nil) |
414ecccc EZ |
1368 | (modified-p (buffer-modified-p)) |
1369 | last-command-event last-command this-command) | |
b58fc490 | 1370 | (setq quail-current-key "" |
d91eafdf KH |
1371 | quail-current-str "" |
1372 | quail-translating t) | |
1373 | (if key | |
1374 | (setq unread-command-events (cons key unread-command-events))) | |
b58fc490 | 1375 | (while quail-translating |
b0fdefb4 | 1376 | (set-buffer-modified-p modified-p) |
17388a62 | 1377 | (quail-show-guidance) |
bc3f38d9 KH |
1378 | (let* ((prompt (if input-method-use-echo-area |
1379 | (format "%s%s %s" | |
1380 | (or input-method-previous-message "") | |
1381 | quail-current-str | |
1382 | quail-guidance-str))) | |
1383 | (keyseq (read-key-sequence prompt nil nil t)) | |
d91eafdf KH |
1384 | (cmd (lookup-key (quail-translation-keymap) keyseq))) |
1385 | (if (if key | |
1386 | (and (commandp cmd) (not (eq cmd 'quail-other-command))) | |
1387 | (eq cmd 'quail-self-insert-command)) | |
414ecccc EZ |
1388 | (progn |
1389 | (setq last-command-event (aref keyseq (1- (length keyseq))) | |
1390 | last-command this-command | |
1391 | this-command cmd) | |
d91eafdf | 1392 | (setq key t) |
b58fc490 KH |
1393 | (condition-case err |
1394 | (call-interactively cmd) | |
1395 | (quail-error (message "%s" (cdr err)) (beep)))) | |
1396 | ;; KEYSEQ is not defined in the translation keymap. | |
1397 | ;; Let's return the event(s) to the caller. | |
817e162f | 1398 | (setq unread-command-events |
d91eafdf KH |
1399 | (string-to-list (this-single-command-raw-keys))) |
1400 | (setq quail-translating nil)))) | |
1401 | (quail-delete-region) | |
c5f3770d | 1402 | quail-current-str) |
b58fc490 KH |
1403 | |
1404 | ;; Since KEY doesn't start any translation, just return it. | |
8179cccd | 1405 | ;; But translate KEY if necessary. |
c5f3770d | 1406 | (if (quail-kbd-translate) |
195e6740 | 1407 | (setq key (quail-keyboard-translate key))) |
c5f3770d | 1408 | (char-to-string key))) |
b58fc490 KH |
1409 | |
1410 | (defun quail-start-conversion (key) | |
c5f3770d KH |
1411 | "Start conversion of the typed character KEY by the current Quail package. |
1412 | Return the input string." | |
b58fc490 | 1413 | ;; Check the possibility of translating KEY. |
d91eafdf KH |
1414 | ;; If KEY is nil, we can anyway start translation. |
1415 | (if (or (and (integerp key) | |
1416 | (assq (if (quail-kbd-translate) | |
1417 | (quail-keyboard-translate key) key) | |
1418 | (cdr (quail-map)))) | |
1419 | (null key)) | |
b58fc490 | 1420 | ;; Ok, we can start translation and conversion. |
d91eafdf KH |
1421 | (let* ((echo-keystrokes 0) |
1422 | (help-char nil) | |
1423 | (overriding-terminal-local-map (quail-conversion-keymap)) | |
b58fc490 | 1424 | (generated-events nil) |
b0fdefb4 | 1425 | (input-method-function nil) |
414ecccc EZ |
1426 | (modified-p (buffer-modified-p)) |
1427 | last-command-event last-command this-command) | |
b58fc490 | 1428 | (setq quail-current-key "" |
d91eafdf | 1429 | quail-current-str "" |
b58fc490 | 1430 | quail-translating t |
d91eafdf KH |
1431 | quail-converting t |
1432 | quail-conversion-str "") | |
1433 | (if key | |
1434 | (setq unread-command-events (cons key unread-command-events))) | |
b58fc490 | 1435 | (while quail-converting |
b0fdefb4 | 1436 | (set-buffer-modified-p modified-p) |
b58fc490 KH |
1437 | (or quail-translating |
1438 | (progn | |
1439 | (setq quail-current-key "" | |
d91eafdf | 1440 | quail-current-str "" |
b58fc490 KH |
1441 | quail-translating t) |
1442 | (quail-setup-overlays nil))) | |
17388a62 | 1443 | (quail-show-guidance) |
bc3f38d9 KH |
1444 | (let* ((prompt (if input-method-use-echo-area |
1445 | (format "%s%s%s %s" | |
1446 | (or input-method-previous-message "") | |
1447 | quail-conversion-str | |
1448 | quail-current-str | |
1449 | quail-guidance-str))) | |
1450 | (keyseq (read-key-sequence prompt nil nil t)) | |
d91eafdf KH |
1451 | (cmd (lookup-key (quail-conversion-keymap) keyseq))) |
1452 | (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command)) | |
414ecccc EZ |
1453 | (progn |
1454 | (setq last-command-event (aref keyseq (1- (length keyseq))) | |
1455 | last-command this-command | |
1456 | this-command cmd) | |
d91eafdf | 1457 | (setq key t) |
b58fc490 KH |
1458 | (condition-case err |
1459 | (call-interactively cmd) | |
d91eafdf KH |
1460 | (quail-error (message "%s" (cdr err)) (beep))) |
1461 | (or quail-translating | |
1462 | (progn | |
1463 | (if quail-current-str | |
1464 | (setq quail-conversion-str | |
1465 | (concat quail-conversion-str | |
1466 | (if (stringp quail-current-str) | |
1467 | quail-current-str | |
1468 | (char-to-string quail-current-str))))) | |
f9f1ed46 KH |
1469 | (if (or input-method-exit-on-first-char |
1470 | (= (length quail-conversion-str) 0)) | |
d91eafdf | 1471 | (setq quail-converting nil))))) |
b58fc490 KH |
1472 | ;; KEYSEQ is not defined in the conversion keymap. |
1473 | ;; Let's return the event(s) to the caller. | |
f9f1ed46 | 1474 | (setq unread-command-events |
d91eafdf KH |
1475 | (string-to-list (this-single-command-raw-keys))) |
1476 | (setq quail-converting nil)))) | |
db8f7303 | 1477 | (setq quail-translating nil) |
348d1438 | 1478 | (if (overlay-start quail-conv-overlay) |
d91eafdf KH |
1479 | (delete-region (overlay-start quail-conv-overlay) |
1480 | (overlay-end quail-conv-overlay))) | |
1481 | (if (> (length quail-conversion-str) 0) | |
c5f3770d | 1482 | quail-conversion-str)) |
b58fc490 KH |
1483 | |
1484 | ;; Since KEY doesn't start any translation, just return it. | |
8179cccd | 1485 | ;; But translate KEY if necessary. |
c5f3770d | 1486 | (if (quail-kbd-translate) |
195e6740 | 1487 | (setq key (quail-keyboard-translate key))) |
c5f3770d | 1488 | (char-to-string key))) |
4ed46869 KH |
1489 | |
1490 | (defun quail-terminate-translation () | |
ce9395a9 | 1491 | "Terminate the translation of the current key." |
b58fc490 | 1492 | (setq quail-translating nil) |
17388a62 | 1493 | (setq quail-guidance-str " ")) |
4ed46869 | 1494 | |
4ed46869 | 1495 | (defun quail-select-current () |
362a8065 | 1496 | "Accept the currently selected translation." |
4ed46869 KH |
1497 | (interactive) |
1498 | (quail-terminate-translation)) | |
1499 | ||
4ed46869 | 1500 | (defun quail-update-translation (control-flag) |
74ace46a DL |
1501 | "Update the current translation status according to CONTROL-FLAG. |
1502 | If CONTROL-FLAG is integer value, it is the number of keys in the | |
1503 | head `quail-current-key' which can be translated. The remaining keys | |
1504 | are put back to `unread-command-events' to be handled again. If | |
1505 | CONTROL-FLAG is t, terminate the translation for the whole keys in | |
1506 | `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation | |
1507 | with more keys." | |
4ed46869 KH |
1508 | (let ((func (quail-update-translation-function))) |
1509 | (if func | |
d91eafdf | 1510 | (setq control-flag (funcall func control-flag)) |
8179cccd KH |
1511 | (cond ((numberp control-flag) |
1512 | (let ((len (length quail-current-key))) | |
1513 | (if (= control-flag 0) | |
1514 | (setq quail-current-str | |
1515 | (if (quail-kbd-translate) | |
1516 | (quail-keyseq-translate quail-current-key) | |
1517 | quail-current-key))) | |
1518 | (or input-method-exit-on-first-char | |
1519 | (while (> len control-flag) | |
1520 | (setq len (1- len)) | |
1521 | (setq unread-command-events | |
1522 | (cons (aref quail-current-key len) | |
1523 | unread-command-events)))))) | |
1524 | ((null control-flag) | |
1525 | (unless quail-current-str | |
1526 | (setq quail-current-str | |
1527 | (if (quail-kbd-translate) | |
1528 | (quail-keyseq-translate quail-current-key) | |
1529 | quail-current-key)) | |
1530 | (if (and input-method-exit-on-first-char | |
1531 | (quail-simple)) | |
1532 | (setq control-flag t))))))) | |
bd21f930 | 1533 | (or input-method-use-echo-area |
17388a62 | 1534 | (let (pos) |
bd21f930 | 1535 | (quail-delete-region) |
17388a62 | 1536 | (setq pos (point)) |
fd7a117d KH |
1537 | (or enable-multibyte-characters |
1538 | (let (char) | |
1539 | (if (stringp quail-current-str) | |
1540 | (catch 'tag | |
1541 | (mapc #'(lambda (ch) | |
1542 | (when (/= (unibyte-char-to-multibyte | |
1543 | (multibyte-char-to-unibyte ch)) | |
1544 | ch) | |
1545 | (setq char ch) | |
1546 | (throw 'tag nil))) | |
1547 | quail-current-str)) | |
1548 | (if (/= (unibyte-char-to-multibyte | |
1549 | (multibyte-char-to-unibyte quail-current-str)) | |
1550 | quail-current-str) | |
1551 | (setq char quail-current-str))) | |
1552 | (when char | |
1553 | (message "Can't input %c in the current unibyte buffer" char) | |
1554 | (ding) | |
1555 | (sit-for 2) | |
1556 | (message nil) | |
1557 | (setq quail-current-str nil) | |
1558 | (throw 'quail-tag nil)))) | |
17388a62 KH |
1559 | (insert quail-current-str) |
1560 | (move-overlay quail-overlay pos (point)) | |
1561 | (if (overlayp quail-conv-overlay) | |
1562 | (if (not (overlay-start quail-conv-overlay)) | |
1563 | (move-overlay quail-conv-overlay pos (point)) | |
1564 | (if (< (overlay-end quail-conv-overlay) (point)) | |
1565 | (move-overlay quail-conv-overlay | |
1566 | (overlay-start quail-conv-overlay) | |
1567 | (point))))))) | |
bd21f930 KH |
1568 | (let (quail-current-str) |
1569 | (quail-update-guidance)) | |
d91eafdf KH |
1570 | (or (stringp quail-current-str) |
1571 | (setq quail-current-str (char-to-string quail-current-str))) | |
05204016 KH |
1572 | (if control-flag |
1573 | (quail-terminate-translation))) | |
4ed46869 KH |
1574 | |
1575 | (defun quail-self-insert-command () | |
f3abc411 | 1576 | "Translate the typed key by the current Quail map, and insert." |
4ed46869 KH |
1577 | (interactive "*") |
1578 | (setq quail-current-key | |
1579 | (concat quail-current-key (char-to-string last-command-event))) | |
d91eafdf KH |
1580 | (or (catch 'quail-tag |
1581 | (quail-update-translation (quail-translate-key)) | |
1582 | t) | |
1583 | ;; If someone throws for `quail-tag' by value nil, we exit from | |
1584 | ;; translation mode. | |
1585 | (setq quail-translating nil))) | |
4ed46869 | 1586 | |
7d842556 | 1587 | (defun quail-map-definition (map) |
74ace46a | 1588 | "Return the actual definition part of Quail map MAP." |
7d842556 KH |
1589 | (let ((def (car map))) |
1590 | (if (and (consp def) (not (vectorp (cdr def)))) | |
1591 | (setq def (car def))) | |
817e162f KH |
1592 | (if (eq def t) |
1593 | (setq def nil)) | |
7d842556 KH |
1594 | def)) |
1595 | ||
7d842556 | 1596 | (defun quail-get-current-str (len def) |
74ace46a DL |
1597 | "Return string to be shown as current translation of key sequence. |
1598 | LEN is the length of the sequence. DEF is a definition part of the | |
1599 | Quail map for the sequence." | |
7d842556 KH |
1600 | (or (and (consp def) (aref (cdr def) (car (car def)))) |
1601 | def | |
1602 | (and (> len 1) | |
1603 | (let ((str (quail-get-current-str | |
1604 | (1- len) | |
1605 | (quail-map-definition (quail-lookup-key | |
1606 | quail-current-key (1- len)))))) | |
1607 | (if str | |
1608 | (concat (if (stringp str) str (char-to-string str)) | |
1609 | (substring quail-current-key (1- len) len))))))) | |
1610 | ||
1611 | (defvar quail-guidance-translations-starting-column 20) | |
1612 | ||
7d842556 | 1613 | (defun quail-update-current-translations (&optional relative-index) |
74ace46a DL |
1614 | "Update `quail-current-translations'. |
1615 | Make RELATIVE-INDEX the current translation." | |
7d842556 KH |
1616 | (let* ((indices (car quail-current-translations)) |
1617 | (cur (car indices)) | |
1618 | (start (nth 1 indices)) | |
1619 | (end (nth 2 indices))) | |
1620 | ;; Validate the index number of current translation. | |
1621 | (if (< cur 0) | |
1622 | (setcar indices (setq cur 0)) | |
1623 | (if (>= cur (length (cdr quail-current-translations))) | |
1624 | (setcar indices | |
1625 | (setq cur (1- (length (cdr quail-current-translations))))))) | |
1626 | ||
1627 | (if (or (null end) ; We have not yet calculated END. | |
1628 | (< cur start) ; We moved to the previous block. | |
1629 | (>= cur end)) ; We moved to the next block. | |
1630 | (let ((len (length (cdr quail-current-translations))) | |
17388a62 | 1631 | (maxcol (- (window-width) |
7d842556 KH |
1632 | quail-guidance-translations-starting-column)) |
1633 | (block (nth 3 indices)) | |
1634 | col idx width trans num-items blocks) | |
1635 | (if (< cur start) | |
1636 | ;; We must calculate from the head. | |
1637 | (setq start 0 block 0) | |
1638 | (if end ; i.e. (>= cur end) | |
1639 | (setq start end))) | |
1640 | (setq idx start col 0 end start num-items 0) | |
1641 | ;; Loop until we hit the tail, or reach the block of CUR. | |
1642 | (while (and (< idx len) (>= cur end)) | |
1643 | (if (= num-items 0) | |
1644 | (setq start idx col 0 block (1+ block))) | |
1645 | (setq trans (aref (cdr quail-current-translations) idx)) | |
1646 | (setq width (if (integerp trans) (char-width trans) | |
1647 | (string-width trans))) | |
1648 | (setq col (+ col width 3) num-items (1+ num-items)) | |
1649 | (if (and (> num-items 0) | |
1650 | (or (>= col maxcol) (> num-items 10))) | |
1651 | (setq end idx num-items 0) | |
1652 | (setq idx (1+ idx)))) | |
1653 | (setcar (nthcdr 3 indices) block) | |
1654 | (if (>= idx len) | |
1655 | (progn | |
1656 | ;; We hit the tail before reaching MAXCOL. | |
1657 | (setq end idx) | |
1658 | (setcar (nthcdr 4 indices) block))) | |
1659 | (setcar (cdr indices) start) | |
1660 | (setcar (nthcdr 2 indices) end))) | |
1661 | (if relative-index | |
1662 | (if (>= (+ start relative-index) end) | |
95109387 | 1663 | (setcar indices (1- end)) |
7d842556 KH |
1664 | (setcar indices (+ start relative-index)))) |
1665 | (setq quail-current-str | |
d91eafdf KH |
1666 | (aref (cdr quail-current-translations) (car indices))) |
1667 | (or (stringp quail-current-str) | |
1668 | (setq quail-current-str (char-to-string quail-current-str))))) | |
7d842556 | 1669 | |
4ed46869 KH |
1670 | (defun quail-translate-key () |
1671 | "Translate the current key sequence according to the current Quail map. | |
1672 | Return t if we can terminate the translation. | |
1673 | Return nil if the current key sequence may be followed by more keys. | |
1674 | Return number if we can't find any translation for the current key | |
1675 | sequence. The number is the count of valid keys in the current | |
1676 | sequence counting from the head." | |
1677 | (let* ((len (length quail-current-key)) | |
1678 | (map (quail-lookup-key quail-current-key len)) | |
1679 | def ch) | |
1680 | (if map | |
7d842556 KH |
1681 | (let ((def (quail-map-definition map))) |
1682 | (setq quail-current-str (quail-get-current-str len def)) | |
4ed46869 KH |
1683 | ;; Return t only if we can terminate the current translation. |
1684 | (and | |
1685 | ;; No alternative translations. | |
1686 | (or (null (consp def)) (= (length (cdr def)) 1)) | |
1687 | ;; No translation for the longer key. | |
1688 | (null (cdr map)) | |
1689 | ;; No shorter breaking point. | |
1690 | (or (null (quail-maximum-shortest)) | |
1691 | (< len 3) | |
1692 | (null (quail-lookup-key quail-current-key (1- len))) | |
1693 | (null (quail-lookup-key | |
1694 | (substring quail-current-key -2 -1) 1))))) | |
1695 | ||
1696 | ;; There's no translation for the current key sequence. Before | |
1697 | ;; giving up, we must check two possibilities. | |
1698 | (cond ((and | |
1699 | (quail-maximum-shortest) | |
a1233357 | 1700 | (>= len 3) |
7d842556 KH |
1701 | (setq def (quail-map-definition |
1702 | (quail-lookup-key quail-current-key (- len 2)))) | |
4ed46869 KH |
1703 | (quail-lookup-key (substring quail-current-key -2) 2)) |
1704 | ;; Now the sequence is "...ABCD", which can be split into | |
1705 | ;; "...AB" and "CD..." to get valid translation. | |
1706 | ;; At first, get translation of "...AB". | |
7d842556 | 1707 | (setq quail-current-str (quail-get-current-str (- len 2) def)) |
4ed46869 KH |
1708 | ;; Then, return the length of "...AB". |
1709 | (- len 2)) | |
1710 | ||
55e30181 KH |
1711 | ((and (> len 0) |
1712 | (quail-lookup-key (substring quail-current-key 0 -1)) | |
1713 | quail-current-translations | |
4ed46869 KH |
1714 | (not (quail-deterministic)) |
1715 | (setq ch (aref quail-current-key (1- len))) | |
1716 | (>= ch ?0) (<= ch ?9)) | |
1717 | ;; A numeric key is entered to select a desirable translation. | |
1718 | (setq quail-current-key (substring quail-current-key 0 -1)) | |
7d842556 KH |
1719 | ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9. |
1720 | (setq ch (if (= ch ?0) 9 (- ch ?1))) | |
1721 | (quail-update-current-translations ch) | |
4ed46869 KH |
1722 | ;; And, we can terminate the current translation. |
1723 | t) | |
1724 | ||
1725 | (t | |
1726 | ;; No way to handle the last character in this context. | |
cbdbef2d KH |
1727 | (setq def (quail-map-definition |
1728 | (quail-lookup-key quail-current-key (1- len)))) | |
53e89326 KH |
1729 | (if def (setq quail-current-str |
1730 | (quail-get-current-str (1- len) def))) | |
4ed46869 KH |
1731 | (1- len)))))) |
1732 | ||
1733 | (defun quail-next-translation () | |
1734 | "Select next translation in the current batch of candidates." | |
1735 | (interactive) | |
1736 | (if quail-current-translations | |
7d842556 KH |
1737 | (let ((indices (car quail-current-translations))) |
1738 | (if (= (1+ (car indices)) (length (cdr quail-current-translations))) | |
5871092a | 1739 | ;; We are already at the tail. |
7d842556 KH |
1740 | (beep) |
1741 | (setcar indices (1+ (car indices))) | |
1742 | (quail-update-current-translations) | |
1743 | (quail-update-translation nil))) | |
b58fc490 KH |
1744 | (setq unread-command-events |
1745 | (cons last-command-event unread-command-events)) | |
1746 | (quail-terminate-translation))) | |
4ed46869 KH |
1747 | |
1748 | (defun quail-prev-translation () | |
1749 | "Select previous translation in the current batch of candidates." | |
1750 | (interactive) | |
1751 | (if quail-current-translations | |
7d842556 KH |
1752 | (let ((indices (car quail-current-translations))) |
1753 | (if (= (car indices) 0) | |
1754 | ;; We are already at the head. | |
1755 | (beep) | |
1756 | (setcar indices (1- (car indices))) | |
1757 | (quail-update-current-translations) | |
1758 | (quail-update-translation nil))) | |
b58fc490 KH |
1759 | (setq unread-command-events |
1760 | (cons last-command-event unread-command-events)) | |
1761 | (quail-terminate-translation))) | |
4ed46869 KH |
1762 | |
1763 | (defun quail-next-translation-block () | |
7d842556 | 1764 | "Select from the next block of translations." |
4ed46869 KH |
1765 | (interactive) |
1766 | (if quail-current-translations | |
7d842556 KH |
1767 | (let* ((indices (car quail-current-translations)) |
1768 | (offset (- (car indices) (nth 1 indices)))) | |
1769 | (if (>= (nth 2 indices) (length (cdr quail-current-translations))) | |
1770 | ;; We are already at the last block. | |
1771 | (beep) | |
1772 | (setcar indices (+ (nth 2 indices) offset)) | |
1773 | (quail-update-current-translations) | |
1774 | (quail-update-translation nil))) | |
b58fc490 | 1775 | (setq unread-command-events |
d91eafdf | 1776 | (cons last-command-event unread-command-events)) |
b58fc490 | 1777 | (quail-terminate-translation))) |
4ed46869 KH |
1778 | |
1779 | (defun quail-prev-translation-block () | |
1780 | "Select the previous batch of 10 translation candidates." | |
1781 | (interactive) | |
7d842556 KH |
1782 | (if quail-current-translations |
1783 | (let* ((indices (car quail-current-translations)) | |
1784 | (offset (- (car indices) (nth 1 indices)))) | |
1785 | (if (= (nth 1 indices) 0) | |
1786 | ;; We are already at the first block. | |
1787 | (beep) | |
1788 | (setcar indices (1- (nth 1 indices))) | |
1789 | (quail-update-current-translations) | |
1790 | (if (< (+ (nth 1 indices) offset) (nth 2 indices)) | |
1791 | (progn | |
1792 | (setcar indices (+ (nth 1 indices) offset)) | |
1793 | (quail-update-current-translations))) | |
1794 | (quail-update-translation nil))) | |
b58fc490 KH |
1795 | (setq unread-command-events |
1796 | (cons last-command-event unread-command-events)) | |
1797 | (quail-terminate-translation))) | |
4ed46869 | 1798 | |
4ed46869 KH |
1799 | (defun quail-abort-translation () |
1800 | "Abort translation and delete the current Quail key sequence." | |
1801 | (interactive) | |
1802 | (quail-delete-region) | |
d91eafdf | 1803 | (setq quail-current-str nil) |
4ed46869 KH |
1804 | (quail-terminate-translation)) |
1805 | ||
1806 | (defun quail-delete-last-char () | |
1807 | "Delete the last input character from the current Quail key sequence." | |
1808 | (interactive) | |
1809 | (if (= (length quail-current-key) 1) | |
1810 | (quail-abort-translation) | |
1811 | (setq quail-current-key (substring quail-current-key 0 -1)) | |
817e162f | 1812 | (quail-delete-region) |
4ed46869 KH |
1813 | (quail-update-translation (quail-translate-key)))) |
1814 | ||
1815 | ;; For conversion mode. | |
1816 | ||
407c6b94 KH |
1817 | (defsubst quail-point-in-conversion-region () |
1818 | "Return non-nil value if the point is in conversion region of Quail mode." | |
1819 | (let (start pos) | |
1820 | (and (setq start (overlay-start quail-conv-overlay)) | |
1821 | (>= (setq pos (point)) start) | |
1822 | (<= pos (overlay-end quail-conv-overlay))))) | |
1823 | ||
4ed46869 KH |
1824 | (defun quail-conversion-backward-char () |
1825 | (interactive) | |
1826 | (if (<= (point) (overlay-start quail-conv-overlay)) | |
b58fc490 KH |
1827 | (quail-error "Beginning of conversion region")) |
1828 | (setq quail-translating nil) | |
4ed46869 KH |
1829 | (forward-char -1)) |
1830 | ||
1831 | (defun quail-conversion-forward-char () | |
1832 | (interactive) | |
1833 | (if (>= (point) (overlay-end quail-conv-overlay)) | |
b58fc490 KH |
1834 | (quail-error "End of conversion region")) |
1835 | (setq quail-translating nil) | |
4ed46869 KH |
1836 | (forward-char 1)) |
1837 | ||
1838 | (defun quail-conversion-beginning-of-region () | |
1839 | (interactive) | |
b45d8d64 | 1840 | (setq quail-translating nil) |
4ed46869 KH |
1841 | (goto-char (overlay-start quail-conv-overlay))) |
1842 | ||
1843 | (defun quail-conversion-end-of-region () | |
1844 | (interactive) | |
b45d8d64 | 1845 | (setq quail-translating nil) |
4ed46869 KH |
1846 | (goto-char (overlay-end quail-conv-overlay))) |
1847 | ||
1848 | (defun quail-conversion-delete-char () | |
1849 | (interactive) | |
b45d8d64 | 1850 | (setq quail-translating nil) |
4ed46869 | 1851 | (if (>= (point) (overlay-end quail-conv-overlay)) |
b58fc490 | 1852 | (quail-error "End of conversion region")) |
4ed46869 | 1853 | (delete-char 1) |
d91eafdf KH |
1854 | (let ((start (overlay-start quail-conv-overlay)) |
1855 | (end (overlay-end quail-conv-overlay))) | |
1856 | (setq quail-conversion-str (buffer-substring start end)) | |
1857 | (if (= start end) | |
1858 | (setq quail-converting nil)))) | |
4ed46869 | 1859 | |
b45d8d64 KH |
1860 | (defun quail-conversion-delete-tail () |
1861 | (interactive) | |
1862 | (if (>= (point) (overlay-end quail-conv-overlay)) | |
1863 | (quail-error "End of conversion region")) | |
1864 | (delete-region (point) (overlay-end quail-conv-overlay)) | |
d91eafdf KH |
1865 | (let ((start (overlay-start quail-conv-overlay)) |
1866 | (end (overlay-end quail-conv-overlay))) | |
1867 | (setq quail-conversion-str (buffer-substring start end)) | |
1868 | (if (= start end) | |
1869 | (setq quail-converting nil)))) | |
b45d8d64 | 1870 | |
4ed46869 KH |
1871 | (defun quail-conversion-backward-delete-char () |
1872 | (interactive) | |
407c6b94 KH |
1873 | (if (> (length quail-current-key) 0) |
1874 | (quail-delete-last-char) | |
1875 | (if (<= (point) (overlay-start quail-conv-overlay)) | |
1876 | (quail-error "Beginning of conversion region")) | |
1877 | (delete-char -1) | |
1878 | (let ((start (overlay-start quail-conv-overlay)) | |
1879 | (end (overlay-end quail-conv-overlay))) | |
1880 | (setq quail-conversion-str (buffer-substring start end)) | |
1881 | (if (= start end) | |
1882 | (setq quail-converting nil))))) | |
4ed46869 KH |
1883 | |
1884 | (defun quail-do-conversion (func &rest args) | |
1885 | "Call FUNC to convert text in the current conversion region of Quail. | |
1886 | Remaining args are for FUNC." | |
1887 | (delete-overlay quail-overlay) | |
1888 | (apply func args)) | |
1889 | ||
1890 | (defun quail-no-conversion () | |
1891 | "Do no conversion of the current conversion region of Quail." | |
1892 | (interactive) | |
b45d8d64 | 1893 | (setq quail-converting nil)) |
4ed46869 KH |
1894 | |
1895 | ;; Guidance, Completion, and Help buffer handlers. | |
1896 | ||
17388a62 KH |
1897 | (defun quail-make-guidance-frame () |
1898 | "Make a new one-line frame for Quail guidance." | |
7d842556 KH |
1899 | (let* ((fparam (frame-parameters)) |
1900 | (top (cdr (assq 'top fparam))) | |
1901 | (border (cdr (assq 'border-width fparam))) | |
1902 | (internal-border (cdr (assq 'internal-border-width fparam))) | |
1903 | (newtop (- top | |
1904 | (frame-char-height) (* internal-border 2) (* border 2)))) | |
1905 | (if (< newtop 0) | |
17388a62 KH |
1906 | (setq newtop (+ top (frame-pixel-height) internal-border border))) |
1907 | (make-frame (append '((user-position . t) (height . 1) | |
1908 | (minibuffer) | |
1909 | (menu-bar-lines . 0) (tool-bar-lines . 0)) | |
1910 | (cons (cons 'top newtop) fparam))))) | |
7d842556 | 1911 | |
05204016 | 1912 | (defun quail-setup-completion-buf () |
74ace46a | 1913 | "Setup Quail completion buffer." |
05204016 | 1914 | (unless (buffer-live-p quail-completion-buf) |
83600a29 KH |
1915 | (let ((default-enable-multibyte-characters enable-multibyte-characters)) |
1916 | (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))) | |
24790d0c | 1917 | (with-current-buffer quail-completion-buf |
05204016 KH |
1918 | (setq quail-overlay (make-overlay 1 1)) |
1919 | (overlay-put quail-overlay 'face 'highlight)))) | |
1920 | ||
b55ba027 | 1921 | (defun quail-require-guidance-buf () |
74ace46a | 1922 | "Return t iff the current Quail package requires showing guidance buffer." |
b55ba027 | 1923 | (and input-method-verbose-flag |
9a0eac6e KH |
1924 | (if (eq input-method-verbose-flag 'default) |
1925 | (not (and (eq (selected-window) (minibuffer-window)) | |
1926 | (quail-simple))) | |
1927 | (if (eq input-method-verbose-flag 'complex-only) | |
1928 | (not (quail-simple)) | |
1929 | t)))) | |
b55ba027 | 1930 | |
17388a62 KH |
1931 | |
1932 | ;; Quail specific version of minibuffer-message. It displays STRING | |
1933 | ;; with timeout 1000000 seconds instead of two seconds. | |
1934 | ||
1935 | (defun quail-minibuffer-message (string) | |
1936 | (message nil) | |
1937 | (let ((point-max (point-max)) | |
1938 | (inhibit-quit t)) | |
1939 | (save-excursion | |
1940 | (goto-char point-max) | |
1941 | (insert string)) | |
1942 | (sit-for 1000000) | |
1943 | (delete-region point-max (point-max)) | |
1944 | (when quit-flag | |
1945 | (setq quit-flag nil | |
1946 | unread-command-events '(7))))) | |
1947 | ||
1948 | (defun quail-show-guidance () | |
1949 | "Display a guidance for Quail input method in some window. | |
1950 | The guidance is normally displayed at the echo area, | |
1951 | or in a newly created frame (if the current buffer is a | |
1952 | minibuffer and the selected frame has no other windows)." | |
1953 | ;; At first, setup a buffer for completion. | |
05204016 | 1954 | (quail-setup-completion-buf) |
17388a62 KH |
1955 | (bury-buffer quail-completion-buf) |
1956 | ||
1957 | ;; Then, show the guidance. | |
1958 | (when (and (quail-require-guidance-buf) | |
bc3f38d9 | 1959 | (not input-method-use-echo-area) |
17388a62 KH |
1960 | (null unread-command-events) |
1961 | (null unread-post-input-method-events)) | |
bc3f38d9 | 1962 | (if (eq (selected-window) (minibuffer-window)) |
17388a62 KH |
1963 | (if (eq (minibuffer-window) (frame-root-window)) |
1964 | ;; Use another frame. It is sure that we are using some | |
1965 | ;; window system. | |
1966 | (let ((guidance quail-guidance-str)) | |
1967 | (or (frame-live-p quail-guidance-frame) | |
1968 | (setq quail-guidance-frame | |
1969 | (quail-make-guidance-frame))) | |
1970 | (or (buffer-live-p quail-guidance-buf) | |
1971 | (setq quail-guidance-buf | |
1972 | (get-buffer-create " *Quail-guidance*"))) | |
1973 | (save-excursion | |
1974 | (set-buffer quail-guidance-buf) | |
1975 | (erase-buffer) | |
1976 | (setq cursor-type nil) | |
1977 | (insert guidance)) | |
1978 | (set-window-buffer (frame-root-window quail-guidance-frame) | |
1979 | quail-guidance-buf) | |
1980 | (quail-minibuffer-message | |
1981 | (format " [%s]" current-input-method-title))) | |
1982 | ;; Show the guidance in the next line of the currrent | |
1983 | ;; minibuffer. | |
1984 | (quail-minibuffer-message | |
1985 | (format " [%s]\n%s" | |
1986 | current-input-method-title quail-guidance-str))) | |
1987 | ;; Show the guidance in echo area without logging. | |
1988 | (let ((message-log-max nil)) | |
1989 | (message "%s" quail-guidance-str))))) | |
1990 | ||
1991 | (defun quail-hide-guidance () | |
1992 | "Hide the Quail guidance." | |
1993 | (when (and (quail-require-guidance-buf) | |
1994 | (or (eq (selected-window) (minibuffer-window)) | |
1995 | input-method-use-echo-area) | |
1996 | (eq (minibuffer-window) (frame-root-window))) | |
1997 | ;; We are using another frame for the guidance. | |
1998 | (if (frame-live-p quail-guidance-frame) | |
1999 | (delete-frame quail-guidance-frame)) | |
2000 | (if (buffer-live-p quail-guidance-buf) | |
2001 | (kill-buffer quail-guidance-buf)))) | |
4ed46869 KH |
2002 | |
2003 | (defun quail-update-guidance () | |
2004 | "Update the Quail guidance buffer and completion buffer (if displayed now)." | |
17388a62 KH |
2005 | ;; Update the guidance string. |
2006 | (when (quail-require-guidance-buf) | |
2007 | (let ((guidance (quail-guidance))) | |
2008 | (cond ((or (eq guidance t) | |
2009 | (consp guidance)) | |
2010 | ;; Show the current possible translations. | |
2011 | (setq quail-guidance-str | |
2012 | (quail-get-translations))) | |
2013 | ((null guidance) | |
2014 | ;; Show the current input keys. | |
2015 | (let ((key quail-current-key)) | |
2016 | (if (quail-kbd-translate) | |
2017 | (setq key (quail-keyseq-translate key))) | |
2018 | (setq quail-guidance-str (if (stringp key) key (string key))))) | |
2019 | (t | |
2020 | (setq quail-guidance-str " "))))) | |
4ed46869 KH |
2021 | |
2022 | ;; Update completion buffer if displayed now. We highlight the | |
2023 | ;; selected candidate string in *Completion* buffer if any. | |
2024 | (let ((win (get-buffer-window quail-completion-buf)) | |
2025 | key str pos) | |
2026 | (if win | |
2027 | (save-excursion | |
2028 | (setq str (if (stringp quail-current-str) | |
2029 | quail-current-str | |
2030 | (if (numberp quail-current-str) | |
2031 | (char-to-string quail-current-str))) | |
2032 | key quail-current-key) | |
2033 | (set-buffer quail-completion-buf) | |
2034 | (goto-char (point-min)) | |
2035 | (if (null (search-forward (concat " " key ":") nil t)) | |
2036 | (delete-overlay quail-overlay) | |
2037 | (setq pos (point)) | |
2038 | (if (and str (search-forward (concat "." str) nil t)) | |
17388a62 KH |
2039 | (move-overlay quail-overlay (1+ (match-beginning 0)) (point)) |
2040 | (move-overlay quail-overlay (match-beginning 0) (point))) | |
4ed46869 KH |
2041 | ;; Now POS points end of KEY and (point) points end of STR. |
2042 | (if (pos-visible-in-window-p (point) win) | |
2043 | ;; STR is already visible. | |
2044 | nil | |
2045 | ;; We want to make both KEY and STR visible, but if the | |
2046 | ;; window is too short, make at least STR visible. | |
2047 | (setq pos (progn (point) (goto-char pos))) | |
2048 | (beginning-of-line) | |
2049 | (set-window-start win (point)) | |
2050 | (if (not (pos-visible-in-window-p pos win)) | |
2051 | (set-window-start win pos)) | |
2052 | )))))) | |
2053 | ||
17388a62 KH |
2054 | (defun quail-get-translations () |
2055 | "Return a string containing the current possible translations." | |
5b6156fa | 2056 | (let ((map (quail-lookup-key quail-current-key nil t)) |
17388a62 | 2057 | (str (copy-sequence quail-current-key))) |
7d842556 KH |
2058 | (if quail-current-translations |
2059 | (quail-update-current-translations)) | |
4ed46869 | 2060 | |
17388a62 KH |
2061 | ;; Show the current key. |
2062 | (let ((guidance (quail-guidance))) | |
2063 | (if (listp guidance) | |
2064 | ;; We must replace thetyped key with the specified PROMPTKEY. | |
2065 | (dotimes (i (length str)) | |
2066 | (let ((prompt-key (cdr (assoc (aref str i) guidance)))) | |
2067 | (if prompt-key | |
2068 | (aset str i (aref prompt-key 0))))))) | |
4ed46869 | 2069 | |
17388a62 KH |
2070 | ;; Show followable keys. |
2071 | (if (and (> (length quail-current-key) 0) (cdr map)) | |
2072 | (setq str | |
2073 | (format "%s[%s]" | |
2074 | str | |
2075 | (concat (sort (mapcar (function (lambda (x) (car x))) | |
2076 | (cdr map)) | |
2077 | '<))))) | |
4ed46869 | 2078 | ;; Show list of translations. |
17388a62 | 2079 | (if (and quail-current-translations |
817e162f | 2080 | (not (quail-deterministic))) |
17388a62 | 2081 | (let* ((indices (car quail-current-translations)) |
7d842556 KH |
2082 | (cur (car indices)) |
2083 | (start (nth 1 indices)) | |
2084 | (end (nth 2 indices)) | |
2085 | (idx start)) | |
17388a62 KH |
2086 | (if (< (string-width str) |
2087 | (- quail-guidance-translations-starting-column 7)) | |
2088 | (setq str | |
2089 | (concat str | |
2090 | (make-string | |
2091 | (- quail-guidance-translations-starting-column | |
2092 | 7 (string-width str)) | |
2093 | 32)))) | |
2094 | (setq str (format "%s(%02d/%s)" | |
2095 | str (nth 3 indices) | |
2096 | (if (nth 4 indices) | |
2097 | (format "%02d" (nth 4 indices)) | |
2098 | "??"))) | |
7d842556 | 2099 | (while (< idx end) |
17388a62 KH |
2100 | (let ((len (length str)) |
2101 | (trans (aref (cdr quail-current-translations) idx))) | |
2102 | (or (stringp trans) | |
2103 | (setq trans (string trans))) | |
2104 | (setq str (format "%s %d.%s" | |
2105 | str | |
2106 | (if (= (- idx start) 9) 0 | |
2107 | (1+ (- idx start))) | |
2108 | trans)) | |
7d842556 | 2109 | (if (= idx cur) |
17388a62 KH |
2110 | (put-text-property (+ len 3) (length str) |
2111 | 'face 'highlight str)) | |
2112 | (setq idx (1+ idx)))))) | |
2113 | ||
2114 | str)) | |
4ed46869 | 2115 | |
817e162f KH |
2116 | (defvar quail-completion-max-depth 5 |
2117 | "The maximum depth of Quail completion list.") | |
2118 | ||
4ed46869 KH |
2119 | (defun quail-completion () |
2120 | "List all completions for the current key. | |
2121 | All possible translations of the current key and whole possible longer keys | |
817e162f | 2122 | are shown (at most to the depth specified `quail-completion-max-depth')." |
4ed46869 | 2123 | (interactive) |
05204016 | 2124 | (quail-setup-completion-buf) |
0548a7fd KH |
2125 | (let ((win (get-buffer-window quail-completion-buf 'visible)) |
2126 | (key quail-current-key) | |
5b6156fa | 2127 | (map (quail-lookup-key quail-current-key nil t)) |
0548a7fd | 2128 | (require-update nil)) |
24790d0c | 2129 | (with-current-buffer quail-completion-buf |
0548a7fd KH |
2130 | (if (and win |
2131 | (equal key quail-current-key) | |
2132 | (eq last-command 'quail-completion)) | |
2133 | ;; The window for Quail completion buffer has already been | |
2134 | ;; shown. We just scroll it appropriately. | |
2135 | (if (pos-visible-in-window-p (point-max) win) | |
2136 | (set-window-start win (point-min)) | |
d5ec6a2d KH |
2137 | (let ((other-window-scroll-buffer quail-completion-buf) |
2138 | ;; This nil binding is necessary to surely scroll | |
2139 | ;; quail-completion-buf. | |
2140 | (minibuffer-scroll-window nil)) | |
0548a7fd KH |
2141 | (scroll-other-window))) |
2142 | (setq quail-current-key key) | |
2143 | (erase-buffer) | |
8acadd06 | 2144 | (insert "Possible completion and corresponding characters are:\n") |
0548a7fd KH |
2145 | (quail-completion-1 key map 1) |
2146 | (goto-char (point-min)) | |
2147 | (display-buffer (current-buffer)) | |
2148 | (setq require-update t))) | |
2149 | (if require-update | |
2150 | (quail-update-guidance))) | |
2151 | (setq this-command 'quail-completion)) | |
4ed46869 | 2152 | |
4ed46869 | 2153 | (defun quail-completion-1 (key map indent) |
74ace46a | 2154 | "List all completions of KEY in MAP with indentation INDENT." |
4ed46869 KH |
2155 | (let ((len (length key))) |
2156 | (indent-to indent) | |
2157 | (insert key ":") | |
2158 | (if (and (symbolp map) (fboundp map)) | |
2159 | (setq map (funcall map key len))) | |
2160 | (if (car map) | |
2161 | (quail-completion-list-translations map key (+ indent len 1)) | |
2162 | (insert " -\n")) | |
2163 | (setq indent (+ indent 2)) | |
817e162f | 2164 | (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth)) |
4ed46869 KH |
2165 | (let ((l (cdr map)) |
2166 | (newkey (make-string (1+ len) 0)) | |
2167 | (i 0)) | |
817e162f KH |
2168 | (if (functionp l) |
2169 | (setq l (funcall l))) | |
4ed46869 KH |
2170 | ;; Set KEY in the first LEN characters of NEWKEY. |
2171 | (while (< i len) | |
2172 | (aset newkey i (aref key i)) | |
2173 | (setq i (1+ i))) | |
d5dc1e49 | 2174 | (setq l (reverse l)) |
4ed46869 KH |
2175 | (while l ; L = ((CHAR . DEFN) ....) ; |
2176 | (aset newkey len (car (car l))) | |
2177 | (quail-completion-1 newkey (cdr (car l)) indent) | |
2178 | (setq l (cdr l))))))) | |
2179 | ||
4ed46869 | 2180 | (defun quail-completion-list-translations (map key indent) |
74ace46a | 2181 | "List all possible translations of KEY in Quail MAP with indentation INDENT." |
5611ce7c | 2182 | (let (beg (translations |
ff913e92 | 2183 | (quail-get-translation (car map) key (length key)))) |
4ed46869 | 2184 | (if (integerp translations) |
5611ce7c KH |
2185 | (progn |
2186 | (insert "(1/1) 1.") | |
2187 | ;; Endow the character `translations' with `mouse-face' text | |
2188 | ;; property to enable `mouse-2' completion. | |
2189 | (setq beg (point)) | |
2190 | (insert translations) | |
2191 | (put-text-property beg (point) 'mouse-face 'highlight) | |
2192 | (insert "\n")) | |
4ed46869 KH |
2193 | ;; We need only vector part. |
2194 | (setq translations (cdr translations)) | |
2195 | ;; Insert every 10 elements with indices in a line. | |
2196 | (let ((len (length translations)) | |
2197 | (i 0) | |
4ed46869 KH |
2198 | num) |
2199 | (while (< i len) | |
8c14aa22 RS |
2200 | (when (zerop (% i 10)) |
2201 | (when (>= i 10) | |
5611ce7c | 2202 | (insert "\n") |
8c14aa22 RS |
2203 | (indent-to indent)) |
2204 | (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10))))) | |
4ed46869 KH |
2205 | ;; We show the last digit of FROM while converting |
2206 | ;; 0,1,..,9 to 1,2,..,0. | |
8c14aa22 | 2207 | (insert (format " %d." (% (1+ i) 10))) |
5611ce7c | 2208 | (setq beg (point)) |
4ed46869 | 2209 | (insert (aref translations i)) |
5611ce7c KH |
2210 | ;; Passing the mouse over a character will highlight. |
2211 | (put-text-property beg (point) 'mouse-face 'highlight) | |
4ed46869 | 2212 | (setq i (1+ i))) |
5611ce7c KH |
2213 | (insert "\n"))))) |
2214 | ||
5611ce7c KH |
2215 | (defun quail-mouse-choose-completion (event) |
2216 | "Click on an alternative in the `*Quail Completions*' buffer to choose it." | |
2217 | (interactive "e") | |
2218 | ;; This function is an exact copy of the mouse.el function | |
74ace46a | 2219 | ;; `mouse-choose-completion' except that we: |
5611ce7c KH |
2220 | ;; 1) add two lines from `choose-completion' in simple.el to give |
2221 | ;; the `mouse-2' click a little more leeway. | |
35fffde1 | 2222 | ;; 2) don't bury *Quail Completions* buffer, so comment a section, and |
5611ce7c KH |
2223 | ;; 3) delete/terminate the current quail selection here. |
2224 | ;; Give temporary modes such as isearch a chance to turn off. | |
2225 | (run-hooks 'mouse-leave-buffer-hook) | |
2226 | (let ((buffer (window-buffer)) | |
2227 | choice | |
2228 | base-size) | |
24790d0c | 2229 | (with-current-buffer (window-buffer (posn-window (event-start event))) |
5611ce7c KH |
2230 | (if completion-reference-buffer |
2231 | (setq buffer completion-reference-buffer)) | |
2232 | (setq base-size completion-base-size) | |
2233 | (save-excursion | |
2234 | (goto-char (posn-point (event-start event))) | |
2235 | (let (beg end) | |
2236 | (if (and (not (eobp)) (get-text-property (point) 'mouse-face)) | |
2237 | (setq end (point) beg (1+ (point)))) | |
2238 | (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face)) | |
2239 | (setq end (1- (point)) beg (point))) | |
2240 | (if (null beg) | |
b58fc490 | 2241 | (quail-error "No completion here")) |
5611ce7c KH |
2242 | (setq beg (previous-single-property-change beg 'mouse-face)) |
2243 | (setq end (or (next-single-property-change end 'mouse-face) | |
2244 | (point-max))) | |
2245 | (setq choice (buffer-substring beg end))))) | |
2246 | ; (let ((owindow (selected-window))) | |
2247 | ; (select-window (posn-window (event-start event))) | |
2248 | ; (if (and (one-window-p t 'selected-frame) | |
2249 | ; (window-dedicated-p (selected-window))) | |
2250 | ; ;; This is a special buffer's frame | |
2251 | ; (iconify-frame (selected-frame)) | |
2252 | ; (or (window-dedicated-p (selected-window)) | |
2253 | ; (bury-buffer))) | |
2254 | ; (select-window owindow)) | |
2255 | (quail-delete-region) | |
2256 | (quail-choose-completion-string choice buffer base-size) | |
2257 | (quail-terminate-translation))) | |
2258 | ||
35fffde1 DL |
2259 | ;; BASE-SIZE here is for compatibility with an (unused) arg of a |
2260 | ;; previous implementation. | |
5611ce7c | 2261 | (defun quail-choose-completion-string (choice &optional buffer base-size) |
35fffde1 DL |
2262 | (setq quail-current-str choice) |
2263 | (choose-completion-string choice buffer)) | |
4ed46869 | 2264 | |
362a8065 KH |
2265 | (defun quail-build-decode-map (map-list key decode-map num |
2266 | &optional maxnum ignores) | |
74ace46a DL |
2267 | "Build a decoding map. |
2268 | Accumulate in the cdr part of DECODE-MAP all pairs of key sequences | |
2269 | vs the corresponding translations defined in the Quail map | |
2270 | specified by the first element MAP-LIST. Each pair has the form | |
2271 | \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form | |
2272 | \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY | |
2273 | is a key sequence to reach MAP. | |
2274 | Optional 5th arg MAXNUM limits the number of accumulated pairs. | |
2275 | Optional 6th arg IGNORES is a list of translations to ignore." | |
362a8065 KH |
2276 | (let* ((map (car map-list)) |
2277 | (translation (quail-get-translation (car map) key (length key))) | |
2278 | elt) | |
95109387 | 2279 | (cond ((integerp translation) |
362a8065 | 2280 | ;; Accept only non-ASCII chars not listed in IGNORES. |
95109387 KH |
2281 | (when (and (> translation 255) (not (memq translation ignores))) |
2282 | (setcdr decode-map | |
2283 | (cons (cons key translation) (cdr decode-map))) | |
2284 | (setq num (1+ num)))) | |
2285 | ((consp translation) | |
2286 | (setq translation (cdr translation)) | |
2287 | (let ((multibyte nil)) | |
2288 | (mapc (function (lambda (x) | |
362a8065 KH |
2289 | ;; Accept only non-ASCII chars not |
2290 | ;; listed in IGNORES. | |
95109387 KH |
2291 | (if (and (if (integerp x) (> x 255) |
2292 | (> (string-bytes x) (length x))) | |
2293 | (not (member x ignores))) | |
2294 | (setq multibyte t)))) | |
2295 | translation) | |
2296 | (when multibyte | |
2297 | (setcdr decode-map | |
2298 | (cons (cons key translation) (cdr decode-map))) | |
2299 | (setq num (+ num (length translation))))))) | |
2300 | (if (and maxnum (> num maxnum)) | |
2301 | (- num) | |
2302 | (setq map (cdr map)) | |
362a8065 | 2303 | ;; Recursively check the deeper map. |
95109387 KH |
2304 | (while (and map (>= num 0)) |
2305 | (setq elt (car map) map (cdr map)) | |
362a8065 KH |
2306 | (when (and (integerp (car elt)) (consp (cdr elt)) |
2307 | (not (memq (cdr elt) map-list))) | |
2308 | (setq num (quail-build-decode-map (cons (cdr elt) map-list) | |
95109387 KH |
2309 | (format "%s%c" key (car elt)) |
2310 | decode-map num maxnum ignores)))) | |
2311 | num))) | |
2312 | ||
2313 | (defun quail-insert-decode-map (decode-map) | |
74ace46a DL |
2314 | "Insert pairs of key sequences vs the corresponding translations. |
2315 | These are stored in DECODE-MAP using the concise format. DECODE-MAP | |
2316 | should be made by `quail-build-decode-map' (which see)." | |
95109387 KH |
2317 | (setq decode-map |
2318 | (sort (cdr decode-map) | |
2319 | (function (lambda (x y) | |
2320 | (setq x (car x) y (car y)) | |
2321 | (or (> (length x) (length y)) | |
2322 | (and (= (length x) (length y)) | |
2323 | (not (string< x y)))))))) | |
93ca088c JL |
2324 | (let ((window-width (window-width (get-buffer-window |
2325 | (current-buffer) 'visible))) | |
362a8065 KH |
2326 | (single-key-width 3) |
2327 | (single-trans-width 4) | |
2328 | (multiple-key-width 3) | |
2329 | (single-list nil) | |
2330 | (multiple-list nil) | |
95109387 | 2331 | elt trans width pos cols rows col row str col-width) |
362a8065 KH |
2332 | ;; Divide the elements of decoding map into single ones (i.e. the |
2333 | ;; one that has single translation) and multibyte ones (i.e. the | |
2334 | ;; one that has multiple translations). | |
95109387 KH |
2335 | (while decode-map |
2336 | (setq elt (car decode-map) decode-map (cdr decode-map) | |
2337 | trans (cdr elt)) | |
2338 | (if (and (vectorp trans) (= (length trans) 1)) | |
2339 | (setq trans (aref trans 0))) | |
2340 | (if (vectorp trans) | |
362a8065 KH |
2341 | (setq multiple-list (cons elt multiple-list)) |
2342 | (setq single-list (cons (cons (car elt) trans) single-list) | |
95109387 KH |
2343 | width (if (stringp trans) (string-width trans) |
2344 | (char-width trans))) | |
362a8065 KH |
2345 | (if (> width single-trans-width) |
2346 | (setq single-trans-width width))) | |
95109387 | 2347 | (setq width (length (car elt))) |
362a8065 KH |
2348 | (if (> width single-key-width) |
2349 | (setq single-key-width width)) | |
2350 | (if (> width multiple-key-width) | |
2351 | (setq multiple-key-width width))) | |
2352 | (when single-list | |
2353 | (setq col-width (+ single-key-width 1 single-trans-width 1) | |
93ca088c | 2354 | cols (/ window-width col-width) |
362a8065 KH |
2355 | rows (/ (length single-list) cols)) |
2356 | (if (> (% (length single-list) cols) 0) | |
95109387 KH |
2357 | (setq rows (1+ rows))) |
2358 | (insert "key") | |
362a8065 | 2359 | (indent-to (1+ single-key-width)) |
95109387 KH |
2360 | (insert "char") |
2361 | (indent-to (1+ col-width)) | |
2362 | (insert "[type a key sequence to insert the corresponding character]\n") | |
2363 | (setq pos (point)) | |
2364 | (insert-char ?\n (+ rows 2)) | |
2365 | (goto-char pos) | |
2366 | (setq col (- col-width) row 0) | |
362a8065 KH |
2367 | (while single-list |
2368 | (setq elt (car single-list) single-list (cdr single-list)) | |
95109387 KH |
2369 | (when (= (% row rows) 0) |
2370 | (goto-char pos) | |
2371 | (setq col (+ col col-width)) | |
2372 | (move-to-column col t) | |
362a8065 | 2373 | (insert-char ?- single-key-width) |
95109387 | 2374 | (insert ? ) |
362a8065 | 2375 | (insert-char ?- single-trans-width) |
95109387 KH |
2376 | (forward-line 1)) |
2377 | (move-to-column col t) | |
2378 | (insert (car elt)) | |
362a8065 | 2379 | (indent-to (+ col single-key-width 1)) |
95109387 KH |
2380 | (insert (cdr elt)) |
2381 | (forward-line 1) | |
2382 | (setq row (1+ row))) | |
2383 | (goto-char (point-max))) | |
2384 | ||
362a8065 | 2385 | (when multiple-list |
95109387 | 2386 | (insert "key") |
362a8065 | 2387 | (indent-to (1+ multiple-key-width)) |
95109387 | 2388 | (insert "character(s) [type a key (sequence) and select one from the list]\n") |
362a8065 | 2389 | (insert-char ?- multiple-key-width) |
95109387 | 2390 | (insert " ------------\n") |
362a8065 KH |
2391 | (while multiple-list |
2392 | (setq elt (car multiple-list) multiple-list (cdr multiple-list)) | |
95109387 | 2393 | (insert (car elt)) |
362a8065 | 2394 | (indent-to multiple-key-width) |
95109387 KH |
2395 | (if (vectorp (cdr elt)) |
2396 | (mapc (function | |
2397 | (lambda (x) | |
2398 | (let ((width (if (integerp x) (char-width x) | |
2399 | (string-width x)))) | |
93ca088c | 2400 | (when (> (+ (current-column) 1 width) window-width) |
95109387 | 2401 | (insert "\n") |
362a8065 | 2402 | (indent-to multiple-key-width)) |
95109387 KH |
2403 | (insert " " x)))) |
2404 | (cdr elt)) | |
2405 | (insert " " (cdr elt))) | |
2406 | (insert ?\n)) | |
2407 | (insert ?\n)))) | |
2408 | ||
396ae608 RS |
2409 | (define-button-type 'quail-keyboard-layout-button |
2410 | :supertype 'help-xref | |
a1506d29 | 2411 | 'help-function '(lambda (layout) |
396ae608 RS |
2412 | (help-setup-xref `(quail-keyboard-layout-button ,layout) nil) |
2413 | (quail-show-keyboard-layout layout)) | |
2414 | 'help-echo (purecopy "mouse-2, RET: show keyboard layout")) | |
2415 | ||
2416 | (define-button-type 'quail-keyboard-customize-button | |
2417 | :supertype 'help-customize-variable | |
2418 | 'help-echo (purecopy "mouse-2, RET: customize keyboard layout")) | |
2419 | ||
6c7b13cf KH |
2420 | (defun quail-help (&optional package) |
2421 | "Show brief description of the current Quail package. | |
24790d0c | 2422 | Optional arg PACKAGE specifies the name of alternative Quail |
362a8065 | 2423 | package to describe." |
4ed46869 | 2424 | (interactive) |
83600a29 | 2425 | (let ((help-xref-mule-regexp help-xref-mule-regexp-template) |
396ae608 RS |
2426 | (default-enable-multibyte-characters enable-multibyte-characters) |
2427 | (package-def | |
2428 | (if package | |
2429 | (assoc package quail-package-alist) | |
2430 | quail-current-package))) | |
58d2b986 | 2431 | ;; At first, make sure that the help buffer has window. |
1ce2659d RS |
2432 | (let ((temp-buffer-show-hook nil)) |
2433 | (with-output-to-temp-buffer (help-buffer) | |
2434 | (with-current-buffer standard-output | |
2435 | (setq quail-current-package package-def)))) | |
58d2b986 | 2436 | ;; Then, insert text in the help buffer while paying attention to |
93ca088c | 2437 | ;; the width of the window in which the buffer displayed. |
24790d0c | 2438 | (with-current-buffer (help-buffer) |
09877d5d MB |
2439 | (setq buffer-read-only nil) |
2440 | (insert "Input method: " (quail-name) | |
2441 | " (mode line indicator:" | |
2442 | (quail-title) | |
2443 | ")\n\n") | |
2444 | (save-restriction | |
2445 | (narrow-to-region (point) (point)) | |
2446 | (insert (quail-docstring)) | |
2447 | (goto-char (point-min)) | |
2448 | (with-syntax-table emacs-lisp-mode-syntax-table | |
2449 | (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t) | |
2450 | (let ((sym (intern-soft | |
2451 | (buffer-substring (+ (match-beginning 0) 2) | |
2452 | (1- (point)))))) | |
2453 | (if (and (boundp sym) | |
2454 | (stringp (symbol-value sym))) | |
2455 | (replace-match (symbol-value sym) t t))))) | |
2456 | (goto-char (point-max))) | |
2457 | (or (bolp) | |
2458 | (insert "\n")) | |
2459 | (insert "\n") | |
2460 | ||
2461 | (let ((done-list nil)) | |
2462 | ;; Show keyboard layout if the current package requests it.. | |
2463 | (when (quail-show-layout) | |
2464 | (insert " | |
362a8065 KH |
2465 | KEYBOARD LAYOUT |
2466 | --------------- | |
8179cccd KH |
2467 | This input method works by translating individual input characters. |
2468 | Assuming that your actual keyboard has the `") | |
09877d5d MB |
2469 | (help-insert-xref-button |
2470 | quail-keyboard-layout-type | |
396ae608 RS |
2471 | 'quail-keyboard-layout-button |
2472 | quail-keyboard-layout-type) | |
09877d5d | 2473 | (insert "' layout, |
8179cccd | 2474 | translation results in the following \"virtual\" keyboard layout: |
195e6740 | 2475 | ") |
09877d5d MB |
2476 | (setq done-list |
2477 | (quail-insert-kbd-layout quail-keyboard-layout)) | |
2478 | (insert "If your keyboard has a different layout, rearranged from | |
8179cccd | 2479 | `") |
09877d5d MB |
2480 | (help-insert-xref-button |
2481 | "standard" | |
396ae608 | 2482 | 'quail-keyboard-layout-button "standard") |
09877d5d | 2483 | (insert "', the \"virtual\" keyboard you get with this input method |
8179cccd KH |
2484 | will be rearranged in the same way. |
2485 | ||
2486 | You can set the variable `quail-keyboard-layout-type' to specify | |
2487 | the physical layout of your keyboard; the tables shown in | |
2488 | documentation of input methods including this one are based on the | |
2489 | physical keyboard layout as specified with that variable. | |
2490 | ") | |
09877d5d MB |
2491 | (help-insert-xref-button |
2492 | "[customize keyboard layout]" | |
396ae608 | 2493 | 'quail-keyboard-customize-button 'quail-keyboard-layout-type) |
09877d5d MB |
2494 | (insert "\n")) |
2495 | ||
2496 | ;; Show key sequences. | |
2497 | (let ((decode-map (list 'decode-map)) | |
2498 | elt pos num) | |
2499 | (setq num (quail-build-decode-map (list (quail-map)) "" decode-map | |
2500 | 0 512 done-list)) | |
2501 | (when (> num 0) | |
2502 | (insert " | |
362a8065 KH |
2503 | KEY SEQUENCE |
2504 | ----------- | |
2505 | ") | |
09877d5d MB |
2506 | (if (quail-show-layout) |
2507 | (insert "You can also input more characters") | |
2508 | (insert "You can input characters")) | |
2509 | (insert " by the following key sequences:\n") | |
2510 | (quail-insert-decode-map decode-map)))) | |
2511 | ||
2512 | (quail-help-insert-keymap-description | |
2513 | (quail-translation-keymap) | |
2514 | "\ | |
362a8065 KH |
2515 | KEY BINDINGS FOR TRANSLATION |
2516 | ----------------------------\n") | |
09877d5d MB |
2517 | (insert ?\n) |
2518 | (if (quail-conversion-keymap) | |
2519 | (quail-help-insert-keymap-description | |
2520 | (quail-conversion-keymap) | |
2521 | "\ | |
362a8065 KH |
2522 | KEY BINDINGS FOR CONVERSION |
2523 | ---------------------------\n")) | |
74ace46a | 2524 | (setq quail-current-package nil) |
09877d5d MB |
2525 | ;; Resize the help window again, now that it has all its contents. |
2526 | (save-selected-window | |
2c72829f | 2527 | (select-window (get-buffer-window (current-buffer) t)) |
09877d5d | 2528 | (run-hooks 'temp-buffer-show-hook))))) |
9a6428f8 | 2529 | |
4ed46869 | 2530 | (defun quail-help-insert-keymap-description (keymap &optional header) |
95109387 KH |
2531 | (let (pos1 pos2 eol) |
2532 | (setq pos1 (point)) | |
4ed46869 KH |
2533 | (if header |
2534 | (insert header)) | |
362a8065 KH |
2535 | (save-excursion |
2536 | (insert (substitute-command-keys "\\{keymap}"))) | |
2537 | ;; Skip headers "key bindings", etc. | |
95109387 KH |
2538 | (forward-line 3) |
2539 | (setq pos2 (point)) | |
2540 | (with-syntax-table emacs-lisp-mode-syntax-table | |
2541 | (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t) | |
2542 | (let ((sym (intern-soft (buffer-substring (match-beginning 0) | |
2543 | (point))))) | |
2544 | (if (and sym (fboundp sym) | |
362a8065 KH |
2545 | (or (eq (get sym 'quail-help) 'hide) |
2546 | (and (quail-deterministic) | |
2547 | (eq (get sym 'quail-help) 'non-deterministic)))) | |
95109387 KH |
2548 | (delete-region (line-beginning-position) |
2549 | (1+ (line-end-position))))))) | |
2550 | (goto-char pos2) | |
2551 | (while (not (eobp)) | |
2552 | (if (looking-at "[ \t]*$") | |
2553 | (delete-region (point) (1+ (line-end-position))) | |
2554 | (forward-line 1))) | |
2555 | (goto-char pos2) | |
2556 | (if (eobp) | |
2557 | (delete-region pos1 (point))) | |
2558 | (goto-char (point-max)))) | |
4ed46869 KH |
2559 | |
2560 | (defun quail-translation-help () | |
d91eafdf | 2561 | "Show help message while translating in Quail input method." |
4ed46869 | 2562 | (interactive) |
d91eafdf KH |
2563 | (if (not (eq this-command last-command)) |
2564 | (let (state-msg keymap) | |
2565 | (if (and quail-converting (= (length quail-current-key) 0)) | |
2566 | (setq state-msg | |
2567 | (format "Converting string %S by input method %S.\n" | |
2568 | quail-conversion-str (quail-name)) | |
2569 | keymap (quail-conversion-keymap)) | |
2570 | (setq state-msg | |
2571 | (format "Translating key sequence %S by input method %S.\n" | |
2572 | quail-current-key (quail-name)) | |
2573 | keymap (quail-translation-keymap))) | |
6c7b13cf | 2574 | (with-output-to-temp-buffer "*Help*" |
24790d0c | 2575 | (with-current-buffer standard-output |
d91eafdf KH |
2576 | (insert state-msg) |
2577 | (quail-help-insert-keymap-description | |
2578 | keymap | |
6c7b13cf | 2579 | "-----------------------\n") |
24790d0c | 2580 | ;; Isn't this redundant ? -stef |
d91eafdf KH |
2581 | (help-mode))))) |
2582 | (let (scroll-help) | |
2583 | (save-selected-window | |
6c7b13cf | 2584 | (select-window (get-buffer-window "*Help*")) |
d91eafdf KH |
2585 | (if (eq this-command last-command) |
2586 | (if (< (window-end) (point-max)) | |
2587 | (scroll-up) | |
2588 | (if (> (window-start) (point-min)) | |
2589 | (set-window-start (selected-window) (point-min))))) | |
2590 | (setq scroll-help | |
2591 | (if (< (window-end (selected-window) 'up-to-date) (point-max)) | |
2592 | "Type \\[quail-translation-help] to scroll up the help" | |
2593 | (if (> (window-start) (point-min)) | |
2594 | "Type \\[quail-translation-help] to see the head of help")))) | |
2595 | (if scroll-help | |
2596 | (progn | |
2597 | (message "%s" (substitute-command-keys scroll-help)) | |
2598 | (sit-for 1) | |
2599 | (message nil) | |
2600 | (quail-update-guidance) | |
2601 | )))) | |
817e162f | 2602 | \f |
4f013856 KH |
2603 | ;; Add KEY (string) to the element of TABLE (char-table) for CHAR if |
2604 | ;; it is not yet stored. As a result, the element is a string or a | |
2605 | ;; list of strings. | |
2606 | ||
2607 | (defsubst quail-store-decode-map-key (table char key) | |
2608 | (let ((elt (aref table char))) | |
2609 | (if elt | |
2610 | (if (consp elt) | |
2611 | (or (member key elt) | |
2612 | (aset table char (cons key elt))) | |
2613 | (or (string= key elt) | |
2614 | (aset table char (list key elt)))) | |
2615 | (aset table char key)))) | |
2616 | ||
2617 | ;; Helper function for quail-gen-decode-map. Store key strings to | |
2618 | ;; type each character under MAP in TABLE (char-table). MAP is an | |
2619 | ;; element of the current Quail map reached by typing keys in KEY | |
2620 | ;; (string). | |
2621 | ||
2622 | (defun quail-gen-decode-map1 (map key table) | |
2623 | (when (and (consp map) (listp (cdr map))) | |
2624 | (let ((trans (car map))) | |
2625 | (cond ((integerp trans) | |
2626 | (quail-store-decode-map-key table trans key)) | |
2627 | ((stringp trans) | |
2628 | (dotimes (i (length trans)) | |
2629 | (quail-store-decode-map-key table (aref trans i) key))) | |
2630 | ((or (vectorp trans) | |
2631 | (and (consp trans) | |
2632 | (setq trans (cdr trans)))) | |
2633 | (dotimes (i (length trans)) | |
2634 | (let ((elt (aref trans i))) | |
2635 | (if (stringp elt) | |
2636 | (if (= (length elt) 1) | |
2637 | (quail-store-decode-map-key table (aref elt 0) key)) | |
2638 | (quail-store-decode-map-key table elt key))))))) | |
2639 | (if (> (length key) 1) | |
2640 | (dolist (elt (cdr map)) | |
2641 | (quail-gen-decode-map1 (cdr elt) key table)) | |
2642 | (dolist (elt (cdr map)) | |
2643 | (quail-gen-decode-map1 (cdr elt) (format "%s%c" key (car elt)) | |
2644 | table))))) | |
2645 | ||
2646 | (put 'quail-decode-map 'char-table-extra-slots 0) | |
2647 | ||
2648 | ;; Generate a halfly-cooked decode map (char-table) for the current | |
2649 | ;; Quail map. An element for a character C is a key string or a list | |
2650 | ;; of a key strings to type to input C. The lenth of key string is at | |
2651 | ;; most 2. If it is 2, more keys may be required to input C. | |
2652 | ||
2653 | (defun quail-gen-decode-map () | |
2654 | (let ((table (make-char-table 'quail-decode-map nil))) | |
2655 | (dolist (elt (cdr (quail-map))) | |
2656 | (quail-gen-decode-map1 (cdr elt) (string (car elt)) table)) | |
2657 | table)) | |
2658 | ||
2659 | ;; Helper function for quail-find-key. Prepend key strings to type | |
2660 | ;; for inputting CHAR by the current input method to KEY-LIST and | |
2661 | ;; return the result. MAP is an element of the current Quail map | |
2662 | ;; reached by typing keys in KEY. | |
2663 | ||
2664 | (defun quail-find-key1 (map key char key-list) | |
2665 | (let ((trans (car map)) | |
2666 | (found-here nil)) | |
2667 | (cond ((stringp trans) | |
2668 | (setq found-here | |
2669 | (and (= (length trans) 1) (= (aref trans 0) char)))) | |
2670 | ((or (vectorp trans) (consp trans)) | |
2671 | (if (consp trans) | |
2672 | (setq trans (cdr trans))) | |
2673 | (setq found-here | |
2674 | (catch 'tag | |
2675 | (dotimes (i (length trans)) | |
2676 | (let ((target (aref trans i))) | |
2677 | (if (integerp target) | |
2678 | (if (= target char) | |
2679 | (throw 'tag t)) | |
2680 | (if (and (= (length target) 1) | |
2681 | (= (aref target 0) char)) | |
2682 | (throw 'tag t)))))))) | |
2683 | ((integerp trans) | |
2684 | (if (= trans char) | |
2685 | (setq found-here t)))) | |
2686 | (if found-here | |
2687 | (setq key-list (cons key key-list))) | |
2688 | (if (> (length key) 1) | |
2689 | (dolist (elt (cdr map)) | |
2690 | (setq key-list | |
2691 | (quail-find-key1 (cdr elt) (format "%s%c" key (car elt)) | |
2692 | char key-list)))) | |
2693 | key-list)) | |
2694 | ||
2695 | (defun quail-find-key (char) | |
2a3e58e6 KH |
2696 | "Return a list of keys to type to input CHAR in the current input method. |
2697 | If CHAR is an ASCII character and can be input by typing itself, return t." | |
4f013856 KH |
2698 | (let ((decode-map (or (quail-decode-map) |
2699 | (setcar (nthcdr 10 quail-current-package) | |
2700 | (quail-gen-decode-map)))) | |
2701 | (key-list nil)) | |
2702 | (if (consp decode-map) | |
2703 | (let ((str (string char))) | |
2704 | (mapc #'(lambda (elt) | |
2705 | (if (string= str (car elt)) | |
2706 | (setq key-list (cons (cdr elt) key-list)))) | |
2707 | (cdr decode-map))) | |
2708 | (let ((key-head (aref decode-map char))) | |
2709 | (if (stringp key-head) | |
2710 | (setq key-list (quail-find-key1 | |
2711 | (quail-lookup-key key-head nil t) | |
2712 | key-head char nil)) | |
2713 | (mapc #'(lambda (elt) | |
2714 | (setq key-list | |
2715 | (quail-find-key1 | |
2716 | (quail-lookup-key elt nil t) elt char key-list))) | |
2717 | key-head)))) | |
2718 | (or key-list | |
2719 | (and (< char 128) | |
2720 | (not (quail-lookup-key (string char) 1)))))) | |
2721 | ||
2722 | (defun quail-show-key () | |
2723 | "Show a list of key strings to type for inputting a character at point." | |
2724 | (interactive) | |
2725 | (or current-input-method | |
2726 | (error "No input method is activated")) | |
2727 | (let* ((char (following-char)) | |
2728 | (key-list (quail-find-key char))) | |
2729 | (cond ((consp key-list) | |
2730 | (message "To input `%c', type \"%s\"" | |
2731 | char | |
2732 | (mapconcat 'identity key-list "\", \""))) | |
2733 | ((eq key-list t) | |
2734 | (message "To input `%s', just type it" | |
2735 | (single-key-description char))) | |
2736 | (t | |
2737 | (message "%c can't be input by the current input method" char))))) | |
2738 | ||
2739 | \f | |
817e162f KH |
2740 | ;; Quail map generator from state transition table. |
2741 | ||
2742 | (defun quail-map-from-table (table) | |
2743 | "Make quail map from state transition table TABLE. | |
2744 | ||
2745 | TABLE is an alist, the form is: | |
2746 | ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...) | |
2747 | ||
2748 | STATE-n are symbols to denote state. STATE-0 is the initial state. | |
2749 | ||
2750 | TRANSITION-n-m are transition rules from STATE-n, and have the form | |
2751 | \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above, | |
2752 | RULES is a symbol whose value is an alist of keys \(string) vs the | |
2753 | correponding characters or strings. The format of the symbol value of | |
2754 | RULES is the same as arguments to `quail-define-rules'. | |
2755 | ||
2756 | If TRANSITION-n-m has the form (RULES . STATE-x), it means that | |
2757 | STATE-n transits to STATE-x when keys in RULES are input. Recursive | |
2758 | transition is allowed, i.e. STATE-x may be STATE-n. | |
2759 | ||
2760 | If TRANSITION-n-m has the form RULES, the transition terminates | |
2761 | when keys in RULES are input. | |
2762 | ||
2763 | The generated map can be set for the current Quail package by the | |
2764 | function `quail-install-map' (which see)." | |
2765 | (let ((state-alist (mapcar (lambda (x) (list (car x))) table)) | |
2766 | tail elt) | |
2767 | ;; STATE-ALIST is an alist of states vs the correponding sub Quail | |
2768 | ;; map. It is now initialized to ((STATE-0) (STATE-1) ...). | |
2769 | ;; Set key sequence mapping rules in cdr part of each element. | |
2770 | (while table | |
2771 | (quail-map-from-table-1 state-alist (car table)) | |
2772 | (setq table (cdr table))) | |
2773 | ||
2774 | ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...). | |
2775 | ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace | |
2776 | ;; them with MAPPING-RULES of STATE-x to make elements of | |
2777 | ;; STATE-ALIST valid Quail maps. | |
2778 | (setq tail state-alist) | |
2779 | (while tail | |
2780 | (setq elt (car tail) tail (cdr tail)) | |
2781 | (quail-map-from-table-2 state-alist elt)) | |
2782 | ||
2783 | ;; Return the Quail map for the initial state. | |
2784 | (car state-alist))) | |
2785 | ||
2786 | ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence | |
2787 | ;; mapping rules in the element of STATE-ALIST that corresponds to | |
2788 | ;; STATE according to TRANSITION ... | |
2789 | (defun quail-map-from-table-1 (state-alist state-info) | |
2790 | (let* ((state (car state-info)) | |
2791 | (map (assq state state-alist)) | |
2792 | (transitions (cdr state-info)) | |
2793 | elt) | |
2794 | (while transitions | |
2795 | (setq elt (car transitions) transitions (cdr transitions)) | |
2796 | (let (rules dst-state key trans) | |
2797 | ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL. | |
2798 | ;; STATE-x is one of car parts of STATE-ALIST's elements. | |
2799 | (if (consp elt) | |
2800 | (setq rules (symbol-value (car elt)) | |
2801 | ;; Set (STATE-x) as branches for all keys in RULES. | |
2802 | ;; It is replaced with actual branches for STATE-x | |
2803 | ;; later in `quail-map-from-table-2'. | |
2804 | dst-state (list (cdr elt))) | |
2805 | (setq rules (symbol-value elt))) | |
2806 | (while rules | |
2807 | (setq key (car (car rules)) trans (cdr (car rules)) | |
2808 | rules (cdr rules)) | |
2809 | (if (stringp trans) | |
2810 | (if (= (length trans) 1) | |
2811 | (setq trans (aref trans 0)) | |
2812 | (setq trans (string-to-vector trans)))) | |
2813 | (set-nested-alist key trans map nil dst-state)))))) | |
2814 | ||
2815 | ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist; | |
2816 | ;; the form is: | |
2817 | ;; (STATE (CHAR NESTED-ALIST) ...) | |
2818 | ;; NESTED-ALIST is a nested alist; the form is: | |
2819 | ;; (TRANS (CHAR NESTED-ALIST) ...) | |
2820 | ;; or | |
2821 | ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x)) | |
2822 | ;; Here, the task is to replace all occurrences of (STATE-x) with: | |
2823 | ;; (cdr (assq STATE-x STATE-ALIST)) | |
2824 | ||
2825 | (defun quail-map-from-table-2 (state-alist element) | |
2826 | (let ((prev element) | |
2827 | (tail (cdr element)) | |
2828 | elt) | |
2829 | (while (cdr tail) | |
2830 | (setq elt (car tail) prev tail tail (cdr tail)) | |
2831 | (quail-map-from-table-2 state-alist (cdr elt))) | |
2832 | (setq elt (car tail)) | |
2833 | (if (consp elt) | |
2834 | (quail-map-from-table-2 state-alist (cdr elt)) | |
2835 | (setcdr prev (cdr (assq elt state-alist)))))) | |
2836 | ||
2837 | ;; Concatenate translations for all heading substrings of KEY in the | |
2838 | ;; current Quail map. Here, `heading substring' means (substring KEY | |
2839 | ;; 0 LEN), where LEN is 1, 2, ... (length KEY). | |
2840 | (defun quail-lookup-map-and-concat (key) | |
2841 | (let* ((len (length key)) | |
2842 | (translation-list nil) | |
2843 | map) | |
2844 | (while (> len 0) | |
5b6156fa | 2845 | (setq map (quail-lookup-key key len t) |
817e162f KH |
2846 | len (1- len)) |
2847 | (if map | |
2848 | (let* ((def (quail-map-definition map)) | |
2849 | (trans (if (consp def) (aref (cdr def) (car (car def))) | |
2850 | def))) | |
2851 | (if (integerp trans) | |
2852 | (setq trans (char-to-string trans))) | |
2853 | (setq translation-list (cons trans translation-list))))) | |
2854 | (apply 'concat translation-list))) | |
4ed46869 | 2855 | |
ff913e92 KH |
2856 | \f |
2857 | (defvar quail-directory-name "quail" | |
cd30a521 | 2858 | "Name of Quail directory which contains Quail packages. |
ff913e92 KH |
2859 | This is a sub-directory of LEIM directory.") |
2860 | ||
2861 | ;;;###autoload | |
70fd2661 KH |
2862 | (defun quail-update-leim-list-file (dirname &rest dirnames) |
2863 | "Update entries for Quail packages in `LEIM' list file in directory DIRNAME. | |
2864 | DIRNAME is a directory containing Emacs input methods; | |
8cbe9074 | 2865 | normally, it should specify the `leim' subdirectory |
70fd2661 KH |
2866 | of the Emacs source tree. |
2867 | ||
2868 | It searches for Quail packages under `quail' subdirectory of DIRNAME, | |
2869 | and update the file \"leim-list.el\" in DIRNAME. | |
ff913e92 | 2870 | |
70fd2661 KH |
2871 | When called from a program, the remaining arguments are additional |
2872 | directory names to search for Quail packages under `quail' subdirectory | |
2873 | of each directory." | |
2874 | (interactive "FDirectory of LEIM: ") | |
2875 | (setq dirname (expand-file-name dirname)) | |
2876 | (let ((leim-list (expand-file-name leim-list-file-name dirname)) | |
2877 | quail-dirs list-buf pkg-list pkg-buf pos) | |
2878 | (if (not (file-writable-p leim-list)) | |
2879 | (error "Can't write to file \"%s\"" leim-list)) | |
2880 | (message "Updating %s ..." leim-list) | |
2881 | (setq list-buf (find-file-noselect leim-list)) | |
2882 | ||
2883 | ;; At first, clean up the file. | |
24790d0c | 2884 | (with-current-buffer list-buf |
70fd2661 KH |
2885 | (goto-char 1) |
2886 | ||
2887 | ;; Insert the correct header. | |
2888 | (if (looking-at (regexp-quote leim-list-header)) | |
2889 | (goto-char (match-end 0)) | |
2890 | (insert leim-list-header)) | |
2891 | (setq pos (point)) | |
2892 | (if (not (re-search-forward leim-list-entry-regexp nil t)) | |
2893 | nil | |
2894 | ||
2895 | ;; Remove garbages after the header. | |
2896 | (goto-char (match-beginning 0)) | |
2897 | (if (< pos (point)) | |
2898 | (delete-region pos (point))) | |
2899 | ||
2900 | ;; Remove all entries for Quail. | |
2901 | (while (re-search-forward leim-list-entry-regexp nil 'move) | |
2902 | (goto-char (match-beginning 0)) | |
2903 | (setq pos (point)) | |
2904 | (condition-case nil | |
2905 | (let ((form (read list-buf))) | |
2906 | (when (equal (nth 3 form) ''quail-use-package) | |
2907 | (if (eolp) (forward-line 1)) | |
2908 | (delete-region pos (point)))) | |
2909 | (error | |
2910 | ;; Delete the remaining contents because it seems that | |
2911 | ;; this file is broken. | |
4be9beaf | 2912 | (message "Garbage in %s deleted" leim-list) |
70fd2661 KH |
2913 | (delete-region pos (point-max))))))) |
2914 | ||
cd30a521 | 2915 | ;; Search for `quail' subdirectory under each DIRNAMES. |
70fd2661 KH |
2916 | (setq dirnames (cons dirname dirnames)) |
2917 | (let ((l dirnames)) | |
2918 | (while l | |
2919 | (setcar l (expand-file-name (car l))) | |
2920 | (setq dirname (expand-file-name quail-directory-name (car l))) | |
2921 | (if (file-readable-p dirname) | |
2922 | (setq quail-dirs (cons dirname quail-dirs)) | |
4be9beaf | 2923 | (message "%s doesn't have `%s' subdirectory, just ignored" |
70fd2661 KH |
2924 | (car l) quail-directory-name) |
2925 | (setq quail-dirs (cons nil quail-dirs))) | |
2926 | (setq l (cdr l))) | |
2927 | (setq quail-dirs (nreverse quail-dirs))) | |
2928 | ||
2929 | ;; Insert input method registering forms. | |
2930 | (while quail-dirs | |
2931 | (setq dirname (car quail-dirs)) | |
2932 | (when dirname | |
2933 | (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort)) | |
2934 | (while pkg-list | |
2935 | (message "Checking %s ..." (car pkg-list)) | |
2936 | (with-temp-buffer | |
2937 | (insert-file-contents (car pkg-list)) | |
2938 | (goto-char (point-min)) | |
35fffde1 DL |
2939 | ;; Don't get fooled by commented-out code. |
2940 | (while (re-search-forward "^[ \t]*(quail-define-package" nil t) | |
ff913e92 | 2941 | (goto-char (match-beginning 0)) |
70fd2661 KH |
2942 | (condition-case nil |
2943 | (let ((form (read (current-buffer)))) | |
24790d0c | 2944 | (with-current-buffer list-buf |
70fd2661 KH |
2945 | (insert |
2946 | (format "(register-input-method | |
ff913e92 KH |
2947 | %S %S '%s |
2948 | %S %S | |
8cbe9074 | 2949 | %S)\n" |
70fd2661 KH |
2950 | (nth 1 form) ; PACKAGE-NAME |
2951 | (nth 2 form) ; LANGUAGE | |
2952 | 'quail-use-package ; ACTIVATE-FUNC | |
2953 | (nth 3 form) ; PACKAGE-TITLE | |
2954 | (progn ; PACKAGE-DESCRIPTION (one line) | |
2955 | (string-match ".*" (nth 5 form)) | |
2956 | (match-string 0 (nth 5 form))) | |
2957 | (file-relative-name ; PACKAGE-FILENAME | |
2958 | (file-name-sans-extension (car pkg-list)) | |
2959 | (car dirnames)))))) | |
2960 | (error | |
2961 | ;; Ignore the remaining contents of this file. | |
2962 | (goto-char (point-max)) | |
186b3352 | 2963 | (message "Some part of \"%s\" is broken" (car pkg-list)))))) |
70fd2661 KH |
2964 | (setq pkg-list (cdr pkg-list))) |
2965 | (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames)))) | |
2966 | ||
2967 | ;; At last, write out LEIM list file. | |
24790d0c | 2968 | (with-current-buffer list-buf |
70fd2661 KH |
2969 | (setq buffer-file-coding-system 'iso-2022-7bit) |
2970 | (save-buffer 0)) | |
2971 | (kill-buffer list-buf) | |
2972 | (message "Updating %s ... done" leim-list))) | |
bb63aae5 KH |
2973 | \f |
2974 | (defun quail-advice (args) | |
74ace46a | 2975 | "Advise users about the characters input by the current Quail package. |
bb63aae5 KH |
2976 | The argument is a parameterized event of the form: |
2977 | (quail-advice STRING) | |
2978 | where STRING is a string containing the input characters. | |
2979 | If STRING has property `advice' and the value is a function, | |
2980 | call it with one argument STRING." | |
2981 | (interactive "e") | |
2982 | (let* ((string (nth 1 args)) | |
2983 | (func (get-text-property 0 'advice string))) | |
2984 | (if (functionp func) | |
2985 | (funcall func string)))) | |
2986 | ||
2987 | (global-set-key [quail-advice] 'quail-advice) | |
2988 | ||
4ed46869 KH |
2989 | ;; |
2990 | (provide 'quail) | |
2991 | ||
ab5796a9 | 2992 | ;;; arch-tag: 46d7db54-5467-42c4-a2a9-53ca90a1e886 |
4ed46869 | 2993 | ;;; quail.el ends here |