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