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