1 ;;; quail.el --- provides simple input method for multilingual text
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; Author: Kenichi HANDA <handa@etl.go.jp>
11 ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
12 ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
13 ;; Keywords: mule, multilingual, input method, i18n
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 3, or (at your option)
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
34 ;; In Quail minor mode, you can input multilingual text easily. By
35 ;; defining a translation table (named Quail map) which maps ASCII key
36 ;; string to multilingual character or string, you can input any text
37 ;; from ASCII keyboard.
39 ;; We use words "translation" and "conversion" differently. The
40 ;; former is done by Quail package itself, the latter is the further
41 ;; process of converting a translated text to some more desirable
42 ;; text. For instance, Quail package for Japanese (`quail-jp')
43 ;; translates Roman text (transliteration of Japanese in Latin
44 ;; alphabets) to Hiragana text, which is then converted to
45 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
46 ;; CONVERSION-KEYS argument of the Quail package.
48 ;; [There was an input method for Mule 2.3 called `Tamago' from the
49 ;; Japanese `TAkusan MAtasete GOmenasai', or `Sorry for having you
50 ;; wait so long'; this couldn't be included in Emacs 20. `Tamago' is
51 ;; Japanese for `egg' (implicitly a hen's egg). Handa-san made a
52 ;; smaller and simpler system; the smaller quail egg is also eaten in
53 ;; Japan. Maybe others will be egged on to write more sorts of input
58 (eval-when-compile (require 'help-mode
))
61 "Quail: multilingual input method."
64 ;; Buffer local variables
66 (defvar quail-current-package nil
67 "The current Quail package, which depends on the current input method.
68 See the documentation of `quail-package-alist' for the format.")
69 (make-variable-buffer-local 'quail-current-package
)
70 (put 'quail-current-package
'permanent-local t
)
72 ;; Quail uses the following variables to assist users.
73 ;; A string containing available key sequences or translation list.
74 (defvar quail-guidance-str nil
)
75 ;; A buffer to show completion list of the current key sequence.
76 (defvar quail-completion-buf nil
)
77 ;; We may display the guidance string in a buffer on a one-line frame.
78 (defvar quail-guidance-buf nil
)
79 (defvar quail-guidance-frame nil
)
81 ;; Each buffer in which Quail is activated should use different
83 (make-variable-buffer-local 'quail-guidance-str
)
84 (put 'quail-guidance-str
'permanent-local t
)
86 (defvar quail-overlay nil
87 "Overlay which covers the current translation region of Quail.")
88 (make-variable-buffer-local 'quail-overlay
)
90 (defvar quail-conv-overlay nil
91 "Overlay which covers the text to be converted in Quail mode.")
92 (make-variable-buffer-local 'quail-conv-overlay
)
94 (defvar quail-current-key nil
95 "Current key for translation in Quail mode.")
96 (make-variable-buffer-local 'quail-current-key
)
98 (defvar quail-current-str nil
99 "Currently selected translation of the current key.")
100 (make-variable-buffer-local 'quail-current-str
)
102 (defvar quail-current-translations nil
103 "Cons of indices and vector of possible translations of the current key.
104 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
105 CURRENT is an index of the current translation,
106 START and END are indices of the start and end of the current block,
107 BLOCK is the current block index,
108 BLOCKS is a number of blocks of translation.")
109 (make-variable-buffer-local 'quail-current-translations
)
111 (defvar quail-current-data nil
112 "Any Lisp object holding information of current translation status.
113 When a key sequence is mapped to TRANS and TRANS is a cons
114 of actual translation and some Lisp object to be referred
115 for translating the longer key sequence, this variable is set
116 to that Lisp object.")
117 (make-variable-buffer-local 'quail-current-data
)
119 ;; Quail package handlers.
121 (defvar quail-package-alist nil
122 "List of Quail packages.
123 A Quail package is a list of these elements:
124 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
125 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
126 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
127 CONVERSION-KEYS, SIMPLE.
129 QUAIL-MAP is a data structure to map key strings to translations. For
130 the format, see the documentation of `quail-map-p'.
132 DECODE-MAP is an alist of translations and corresponding keys.
134 See the documentation of `quail-define-package' for the other elements.")
136 ;; Return various slots in the current quail-package.
138 (defsubst quail-name
()
139 "Return the name of the current Quail package."
140 (nth 0 quail-current-package
))
142 (defun quail-title ()
143 "Return the title of the current Quail package."
144 (let ((title (nth 1 quail-current-package
)))
145 ;; TITLE may be a string or a list. If it is a list, each element
146 ;; is a string or the form (VAR STR1 STR2), and the interpretation
147 ;; of the list is the same as that of mode-line-format.
153 (cond ((stringp x
) x
)
154 ((and (listp x
) (symbolp (car x
)) (= (length x
) 3))
155 (if (symbol-value (car x
))
156 (nth 1 x
) (nth 2 x
)))
160 (defsubst quail-map
()
161 "Return the translation map of the current Quail package."
162 (nth 2 quail-current-package
))
163 (defsubst quail-guidance
()
164 "Return an object used for `guidance' feature of the current Quail package.
165 See also the documentation of `quail-define-package'."
166 (nth 3 quail-current-package
))
167 (defsubst quail-docstring
()
168 "Return the documentation string of the current Quail package."
169 (nth 4 quail-current-package
))
170 (defsubst quail-translation-keymap
()
171 "Return translation keymap in the current Quail package.
172 Translation keymap is a keymap used while translation region is active."
173 (nth 5 quail-current-package
))
174 (defsubst quail-forget-last-selection
()
175 "Return `forget-last-selection' flag of the current Quail package.
176 See also the documentation of `quail-define-package'."
177 (nth 6 quail-current-package
))
178 (defsubst quail-deterministic
()
179 "Return `deterministic' flag of the current Quail package.
180 See also the documentation of `quail-define-package'."
181 (nth 7 quail-current-package
))
182 (defsubst quail-kbd-translate
()
183 "Return `kbd-translate' flag of the current Quail package.
184 See also the documentation of `quail-define-package'."
185 (nth 8 quail-current-package
))
186 (defsubst quail-show-layout
()
187 "Return `show-layout' flag of the current Quail package.
188 See also the documentation of `quail-define-package'."
189 (nth 9 quail-current-package
))
190 (defsubst quail-decode-map
()
191 "Return decode map of the current Quail package.
192 It is an alist of translations and corresponding keys."
193 (nth 10 quail-current-package
))
194 (defsubst quail-maximum-shortest
()
195 "Return `maximum-shortest' flag of the current Quail package.
196 See also the documentation of `quail-define-package'."
197 (nth 11 quail-current-package
))
198 (defsubst quail-overlay-plist
()
199 "Return property list of an overly used in the current Quail package."
200 (nth 12 quail-current-package
))
201 (defsubst quail-update-translation-function
()
202 "Return a function for updating translation in the current Quail package."
203 (nth 13 quail-current-package
))
204 (defsubst quail-conversion-keymap
()
205 "Return conversion keymap in the current Quail package.
206 Conversion keymap is a keymap used while conversion region is active
207 but translation region is not active."
208 (nth 14 quail-current-package
))
209 (defsubst quail-simple
()
210 "Return t if the current Quail package is simple."
211 (nth 15 quail-current-package
))
213 (defsubst quail-package
(name)
214 "Return Quail package named NAME."
215 (assoc name quail-package-alist
))
217 (defun quail-add-package (package)
218 "Add Quail package PACKAGE to `quail-package-alist'."
219 (let ((pac (quail-package (car package
))))
221 (setcdr pac
(cdr package
))
222 (setq quail-package-alist
(cons package quail-package-alist
)))))
224 (defun quail-select-package (name)
225 "Select Quail package named NAME as the current Quail package."
226 (let ((package (quail-package name
)))
228 (error "No Quail package `%s'" name
))
229 (setq quail-current-package package
)
230 (setq-default quail-current-package package
)
234 (defun quail-use-package (package-name &rest libraries
)
235 "Start using Quail package PACKAGE-NAME.
236 The remaining arguments are libraries to be loaded before using the package.
238 This activates input method defined by PACKAGE-NAME by running
239 `quail-activate', which see."
240 (let ((package (quail-package package-name
)))
242 ;; Perhaps we have not yet loaded necessary libraries.
244 (if (not (load (car libraries
) t
))
246 (with-output-to-temp-buffer "*Help*"
247 (princ "Quail package \"")
249 (princ "\" can't be activated\n because library \"")
250 (princ (car libraries
))
251 (princ "\" is not in `load-path'.
253 The most common case is that you have not yet installed appropriate
254 libraries in LEIM (Libraries of Emacs Input Method) which is
255 distributed separately from Emacs.
257 LEIM is available from the same ftp directory as Emacs."))
258 (error "Can't use the Quail package `%s'" package-name
))
259 (setq libraries
(cdr libraries
))))))
260 (quail-select-package package-name
)
261 (setq current-input-method-title
(quail-title))
263 ;; Hide all '... loaded' message.
266 (defvar quail-translation-keymap
267 (let ((map (make-keymap))
270 (define-key map
(char-to-string i
) 'quail-other-command
)
273 (define-key map
(char-to-string i
) 'quail-self-insert-command
)
277 (define-key map
(vector i
) 'quail-self-insert-command
)
279 (define-key map
"\177" 'quail-delete-last-char
)
280 (define-key map
"\C-f" 'quail-next-translation
)
281 (define-key map
"\C-b" 'quail-prev-translation
)
282 (define-key map
"\C-n" 'quail-next-translation-block
)
283 (define-key map
"\C-p" 'quail-prev-translation-block
)
284 (define-key map
[right] 'quail-next-translation)
285 (define-key map [left] 'quail-prev-translation)
286 (define-key map [down] 'quail-next-translation-block)
287 (define-key map [up] 'quail-prev-translation-block)
288 (define-key map "\C-i" 'quail-completion)
289 (define-key map "\C-@" 'quail-select-current)
290 ;; Following simple.el, Enter key on numeric keypad selects the
291 ;; current translation just like `C-SPC', and `mouse-2' chooses
292 ;; any completion visible in the *Quail Completions* buffer.
293 (define-key map [kp-enter] 'quail-select-current)
294 (define-key map [mouse-2] 'quail-mouse-choose-completion)
295 (define-key map [down-mouse-2] nil)
296 (define-key map "\C-h" 'quail-translation-help)
297 (define-key map [?\C- ] 'quail-select-current)
298 (define-key map [tab] 'quail-completion)
299 (define-key map [delete] 'quail-delete-last-char)
300 (define-key map [backspace] 'quail-delete-last-char)
302 "Keymap used processing translation in complex Quail modes.
303 Only a few especially complex input methods use this map;
304 most use `quail-simple-translation-keymap' instead.
305 This map is activated while translation region is active.")
307 (defvar quail-translation-docstring
308 "When you type keys, the echo area shows the possible characters
309 which correspond to that key sequence, each preceded by a digit. You
310 can select one of the characters shown by typing the corresponding
311 digit. Alternatively, you can use C-f and C-b to move through the
312 line to select the character you want, then type a letter to begin
313 entering another Chinese character or type a space or punctuation
316 If there are more than ten possible characters for the given spelling,
317 the echo area shows ten characters at a time; you can use C-n to move
318 to the next group of ten, and C-p to move back to the previous group
321 ;; Categorize each Quail commands to make the output of quail-help
322 ;; concise. This is done by putting `quail-help' property. The value
324 ;; hide -- never show this command
325 ;; non-deterministic -- show only for non-deterministic input method
326 (let ((l '((quail-other-command . hide)
327 (quail-self-insert-command . hide)
328 (quail-delete-last-char . hide)
329 (quail-next-translation . non-deterministic)
330 (quail-prev-translation . non-deterministic)
331 (quail-next-translation-block . non-deterministic)
332 (quail-prev-translation-block . non-deterministic))))
334 (put (car (car l)) 'quail-help (cdr (car l)))
337 (defvar quail-simple-translation-keymap
338 (let ((map (make-keymap))
341 (define-key map (char-to-string i) 'quail-other-command)
344 (define-key map (char-to-string i) 'quail-self-insert-command)
348 (define-key map (vector i) 'quail-self-insert-command)
350 (define-key map "\177" 'quail-delete-last-char)
351 (define-key map [delete] 'quail-delete-last-char)
352 (define-key map [backspace] 'quail-delete-last-char)
353 ;;(let ((meta-map (make-sparse-keymap)))
354 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
355 ;;(define-key map [escape] meta-map))
357 "Keymap used while processing translation in simple Quail modes.
358 A few especially complex input methods use `quail-translation-keymap' instead.
359 This map is activated while translation region is active.")
361 (defvar quail-conversion-keymap
362 (let ((map (make-keymap))
365 (define-key map (char-to-string i) 'quail-self-insert-command)
369 (define-key map (vector i) 'quail-self-insert-command)
371 (define-key map "\C-b" 'quail-conversion-backward-char)
372 (define-key map "\C-f" 'quail-conversion-forward-char)
373 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
374 (define-key map "\C-e" 'quail-conversion-end-of-region)
375 (define-key map "\C-d" 'quail-conversion-delete-char)
376 (define-key map "\C-k" 'quail-conversion-delete-tail)
377 (define-key map "\C-h" 'quail-translation-help)
378 (define-key map "\177" 'quail-conversion-backward-delete-char)
379 (define-key map [delete] 'quail-conversion-backward-delete-char)
380 (define-key map [backspace] 'quail-conversion-backward-delete-char)
382 "Keymap used for processing conversion in Quail mode.
383 This map is activated while conversion region is active but translation
384 region is not active.")
386 ;; Just a dummy definition.
387 (defun quail-other-command ()
392 (defun quail-define-package (name language title
393 &optional guidance docstring translation-keys
394 forget-last-selection deterministic
395 kbd-translate show-layout create-decode-map
396 maximum-shortest overlay-plist
397 update-translation-function
398 conversion-keys simple)
399 "Define NAME as a new Quail package for input LANGUAGE.
400 TITLE is a string to be displayed at mode-line to indicate this package.
401 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
402 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
403 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
404 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
406 GUIDANCE specifies how a guidance string is shown in echo area.
407 If it is t, list of all possible translations for the current key is shown
408 with the currently selected translation being highlighted.
409 If it is an alist, the element has the form (CHAR . STRING). Each character
410 in the current key is searched in the list and the corresponding string is
412 If it is nil, the current key is shown.
414 DOCSTRING is the documentation string of this package. The command
415 `describe-input-method' shows this string while replacing the form
416 \\=\\<VAR> in the string by the value of VAR. That value should be a
417 string. For instance, the form \\=\\<quail-translation-docstring> is
418 replaced by a description about how to select a translation from a
421 TRANSLATION-KEYS specifies additional key bindings used while translation
422 region is active. It is an alist of single key character vs. corresponding
423 command to be called.
425 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
426 for the future to translate the same key. If this flag is nil, a
427 translation selected for a key is remembered so that it can be the
428 first candidate when the same key is entered later.
430 DETERMINISTIC non-nil means the first candidate of translation is
431 selected automatically without allowing users to select another
432 translation for a key. In this case, unselected translations are of
433 no use for an interactive use of Quail but can be used by some other
434 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
437 KBD-TRANSLATE non-nil means input characters are translated from a
438 user's keyboard layout to the standard keyboard layout. See the
439 documentation of `quail-keyboard-layout' and
440 `quail-keyboard-layout-standard' for more detail.
442 SHOW-LAYOUT non-nil means the `quail-help' command should show
443 the user's keyboard layout visually with translated characters.
444 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
445 this package defines no translations for single character keys.
447 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
448 map is an alist of translations and corresponding original keys.
449 Although this map is not used by Quail itself, it can be used by some
450 other programs. For instance, Vietnamese supporting needs this map to
451 convert Vietnamese text to VIQR format which uses only ASCII
452 characters to represent Vietnamese characters.
454 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
455 length of the shortest sequence. When we don't have a translation of
456 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
457 the key at \"..AB\" and start translation of \"CD..\". Hangul
458 packages, for instance, use this facility. If this flag is nil, we
459 break the key just at \"..ABC\" and start translation of \"D..\".
461 OVERLAY-PLIST if non-nil is a property list put on an overlay which
462 covers Quail translation region.
464 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
465 the current translation region according to a new translation data. By
466 default, a translated text or a user's key sequence (if no translation
469 CONVERSION-KEYS specifies additional key bindings used while
470 conversion region is active. It is an alist of single key character
471 vs. corresponding command to be called.
473 If SIMPLE is non-nil, then we do not alter the meanings of
474 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
476 (let (translation-keymap conversion-keymap)
477 (if deterministic (setq forget-last-selection t))
480 (setq translation-keymap (copy-keymap
481 (if simple quail-simple-translation-keymap
482 quail-translation-keymap)))
483 (while translation-keys
484 (define-key translation-keymap
485 (car (car translation-keys)) (cdr (car translation-keys)))
486 (setq translation-keys (cdr translation-keys))))
487 (setq translation-keymap
488 (if simple quail-simple-translation-keymap
489 quail-translation-keymap)))
490 (when conversion-keys
491 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
492 (while conversion-keys
493 (define-key conversion-keymap
494 (car (car conversion-keys)) (cdr (car conversion-keys)))
495 (setq conversion-keys (cdr conversion-keys))))
497 (list name title (list nil) guidance (or docstring "")
499 forget-last-selection deterministic kbd-translate show-layout
500 (if create-decode-map (list 'decode-map) nil)
501 maximum-shortest overlay-plist update-translation-function
502 conversion-keymap simple))
504 ;; Update input-method-alist.
505 (let ((slot (assoc name input-method-alist))
506 (val (list language 'quail-use-package title docstring)))
507 (if slot (setcdr slot val)
508 (setq input-method-alist (cons (cons name val) input-method-alist)))))
510 (quail-select-package name))
512 ;; Quail minor mode handlers.
514 ;; Setup overlays used in Quail mode.
515 (defun quail-setup-overlays (conversion-mode)
517 (if (overlayp quail-overlay)
518 (move-overlay quail-overlay pos pos)
519 (setq quail-overlay (make-overlay pos pos))
520 (if input-method-highlight-flag
521 (overlay-put quail-overlay 'face 'underline))
522 (let ((l (quail-overlay-plist)))
524 (overlay-put quail-overlay (car l) (car (cdr l)))
525 (setq l (cdr (cdr l))))))
527 (if (overlayp quail-conv-overlay)
528 (if (not (overlay-start quail-conv-overlay))
529 (move-overlay quail-conv-overlay pos pos))
530 (setq quail-conv-overlay (make-overlay pos pos))
531 (if input-method-highlight-flag
532 (overlay-put quail-conv-overlay 'face 'underline))))))
534 ;; Delete overlays used in Quail mode.
535 (defun quail-delete-overlays ()
536 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
537 (delete-overlay quail-overlay))
538 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
539 (delete-overlay quail-conv-overlay)))
541 (defun quail-inactivate ()
542 "Inactivate Quail input method.
544 This function runs the normal hook `quail-inactivate-hook'."
548 (defun quail-activate (&optional arg)
549 "Activate Quail input method.
550 With arg, activate Quail input method if and only if arg is positive.
552 This function runs `quail-activate-hook' if it activates the input
553 method, `quail-inactivate-hook' if it deactivates it.
555 While this input method is active, the variable
556 `input-method-function' is bound to the function `quail-input-method'."
558 (< (prefix-numeric-value arg) 0))
559 ;; Let's inactivate Quail input method.
562 (quail-delete-overlays)
563 (setq describe-current-input-method-function nil)
564 (quail-hide-guidance)
565 (remove-hook 'post-command-hook 'quail-show-guidance t)
566 (run-hooks 'quail-inactivate-hook))
567 (kill-local-variable 'input-method-function))
568 ;; Let's activate Quail input method.
569 (if (null quail-current-package)
570 ;; Quail package is not yet selected. Select one now.
572 (if quail-package-alist
573 (setq name (car (car quail-package-alist)))
574 (error "No Quail package loaded"))
575 (quail-select-package name)))
576 (setq inactivate-current-input-method-function 'quail-inactivate)
577 (setq describe-current-input-method-function 'quail-help)
578 (quail-delete-overlays)
579 (setq quail-guidance-str "")
580 (quail-show-guidance)
581 ;; If we are in minibuffer, turn off the current input method
583 (when (eq (selected-window) (minibuffer-window))
584 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)
585 (add-hook 'post-command-hook 'quail-show-guidance nil t))
586 (run-hooks 'quail-activate-hook)
587 (make-local-variable 'input-method-function)
588 (setq input-method-function 'quail-input-method)))
590 (defun quail-exit-from-minibuffer ()
591 (inactivate-input-method)
592 (if (<= (minibuffer-depth) 1)
593 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
595 ;; Keyboard layout translation handlers.
597 ;; Some Quail packages provide localized keyboard simulation which
598 ;; requires a particular keyboard layout. In this case, what we need
599 ;; is locations of keys the user entered, not character codes
600 ;; generated by those keys. However, for the moment, there's no
601 ;; common way to get such information. So, we ask a user to give
602 ;; information of his own keyboard layout, then translate it to the
603 ;; standard layout which we defined so that all Quail packages depend
606 (defconst quail-keyboard-layout-standard
609 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
610 qQwWeErRtTyYuUiIoOpP[{]} \
611 aAsSdDfFgGhHjJkKlL;:'\"\\| \
612 zZxXcCvVbBnNmM,<.>/? \
614 "Standard keyboard layout of printable characters Quail assumes.
615 See the documentation of `quail-keyboard-layout' for this format.
616 This layout is almost the same as that of VT100,
617 but the location of key \\ (backslash) is just right of key ' (single-quote),
618 not right of RETURN key.")
620 (defconst quail-keyboard-layout-len 180)
622 ;; Here we provide several examples of famous keyboard layouts.
623 ;; This is a candidate for a language environment-dependent setting.
624 (defvar quail-keyboard-layout-alist
626 (cons "standard" quail-keyboard-layout-standard)
629 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
630 qQwWeErRtTyYuUiIoOpP[{]} \
631 aAsSdDfFgGhHjJkKlL;:'\" \
632 zZxXcCvVbBnNmM,<.>/? \
634 '("atari-german" . "\
636 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
637 qQwWeErRtTzZuUiIoOpP\374\334+* \
638 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
639 <>yYxXcCvVbBnNmM,;.:-_ \
644 ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
645 qQwWeErRtTzZuUiIoOpP\374\334+* \
646 aAsSdDfFgGhHjJkKlL\366\326\344\304 \
647 <>yYxXcCvVbBnNmM,;.:-_ \
652 1!2\"3#4$5%6&7'8(9)0~-=^~\\| \
653 qQwWeErRtTyYuUiIoOpP@`[{ \
654 aAsSdDfFgGhHjJkKlL;+:*]} \
655 zZxXcCvVbBnNmM,<.>/?\\_ \
659 `\2541!2\"3\2434$5%6^7&8*9(0)-_=+ \
660 qQwWeErRtTyYuUiIoOpP[{]} \
661 aAsSdDfFgGhHjJkKlL;:'@#~ \
662 \\|zZxXcCvVbBnNmM,<.>/? \
665 "Alist of keyboard names and corresponding layout strings.
666 See the documentation of `quail-keyboard-layout' for the format of
669 (defcustom quail-keyboard-layout quail-keyboard-layout-standard
670 "A string which represents physical key layout of a particular keyboard.
671 We assume there are six rows and each row has 15 keys (columns),
672 the first row is above the `1' - `0' row,
673 the first column of the second row is left of key `1',
674 the first column of the third row is left of key `q',
675 the first column of the fourth row is left of key `a',
676 the first column of the fifth row is left of key `z',
677 the sixth row is below the `z' - `/' row.
678 Nth (N is even) and (N+1)th characters in the string are non-shifted
679 and shifted characters respectively at the same location.
680 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
681 The command `quail-set-keyboard-layout' usually sets this variable."
684 ,@(mapcar (lambda (pair)
685 (list 'const :tag (car pair) (cdr pair)))
686 quail-keyboard-layout-alist)
687 (string :tag "Other")))
689 ;; A non-standard keyboard layout may miss some key locations of the
690 ;; standard layout while having additional key locations not in the
691 ;; standard layout. This alist maps those additional key locations to
692 ;; the missing locations. The value is updated automatically by
693 ;; quail-set-keyboard-layout.
694 (defvar quail-keyboard-layout-substitution nil)
696 (defun quail-update-keyboard-layout (kbd-type)
697 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
699 ;; Here, we had better ask a user to define his own keyboard
700 ;; layout interactively.
701 (error "Unknown keyboard type `%s'" kbd-type))
702 (setq quail-keyboard-layout (cdr layout))
703 (let ((i quail-keyboard-layout-len)
704 subst-list missing-list)
705 ;; Sum up additional key locations not in the standard layout in
706 ;; subst-list, and missing key locations in missing-list.
709 (if (= (aref quail-keyboard-layout i) ? )
710 (if (/= (aref quail-keyboard-layout-standard i) ? )
711 (setq missing-list (cons i missing-list)))
712 (if (= (aref quail-keyboard-layout-standard i) ? )
713 (setq subst-list (cons (cons i nil) subst-list)))))
714 (setq quail-keyboard-layout-substitution subst-list)
715 ;; If there are additional key locations, map them to missing
718 (while (and subst-list (cdr (car subst-list)))
719 (setq subst-list (cdr subst-list)))
721 (setcdr (car subst-list) (car missing-list)))
722 (setq missing-list (cdr missing-list))))))
724 (defcustom quail-keyboard-layout-type "standard"
725 "Type of keyboard layout used in Quail base input method.
726 Available types are listed in the variable `quail-keyboard-layout-alist'."
728 :type (cons 'choice (mapcar (lambda (elt)
729 (list 'const (car elt)))
730 quail-keyboard-layout-alist))
731 :set #'(lambda (symbol value)
732 (quail-update-keyboard-layout value)
736 (defun quail-set-keyboard-layout (kbd-type)
737 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
739 Since some Quail packages depends on a physical layout of keys (not
740 characters generated by them), those are created by assuming the
741 standard layout defined in `quail-keyboard-layout-standard'. This
742 function tells Quail system the layout of your keyboard so that what
743 you type is correctly handled."
745 (let* ((completion-ignore-case t)
746 (type (completing-read "Keyboard type: "
747 quail-keyboard-layout-alist)))
749 (quail-update-keyboard-layout kbd-type)
750 (setq quail-keyboard-layout-type kbd-type))
752 (defun quail-keyboard-translate (char)
753 "Translate CHAR to the one in the standard keyboard layout."
754 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
755 ;; All Quail packages are designed based on
756 ;; `quail-keyboard-layout-standard'.
759 ;; Find the key location on the current keyboard layout.
760 (while (and (< i quail-keyboard-layout-len)
761 (/= char (aref quail-keyboard-layout i)))
763 (if (= i quail-keyboard-layout-len)
764 ;; CHAR is not in quail-keyboard-layout, which means that a
765 ;; user typed a key which generated a character code to be
766 ;; handled out of Quail. Just return CHAR and make
767 ;; quail-execute-non-quail-command handle it correctly.
769 (let ((ch (aref quail-keyboard-layout-standard i)))
771 ;; This location not available in the standard keyboard
772 ;; layout. Check if the location is used to substitute
773 ;; for the other location of the standard layout.
774 (if (setq i (cdr (assq i quail-keyboard-layout-substitution)))
775 (aref quail-keyboard-layout-standard i)
776 ;; Just return CHAR as well as above.
780 (defun quail-keyseq-translate (keyseq)
782 (mapcar (function (lambda (x) (quail-keyboard-translate x)))
785 (defun quail-insert-kbd-layout (kbd-layout)
786 "Insert the visual keyboard layout table according to KBD-LAYOUT.
787 The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
788 (let (done-list layout i ch)
789 ;; At first, convert KBD-LAYOUT to the same size vector that
790 ;; contains translated character or string.
791 (setq layout (string-to-vector kbd-layout)
793 (while (< i quail-keyboard-layout-len)
794 (setq ch (aref kbd-layout i))
795 (if (quail-kbd-translate)
796 (setq ch (quail-keyboard-translate ch)))
797 (let* ((map (cdr (assq ch (cdr (quail-map)))))
798 (translation (and map (quail-get-translation
799 (car map) (char-to-string ch) 1))))
802 (if (consp translation)
803 (setq translation (aref (cdr translation) 0)))
804 (setq done-list (cons translation done-list)))
805 (setq translation ch))
806 (aset layout i translation))
812 ;; Make table without horizontal lines. Each column for a key
813 ;; has the form "| LU |" where L is for lower key and and U is
814 ;; for a upper key. If width of L (U) is greater than 1,
815 ;; preceding (following) space is not inserted.
816 (put-text-property 0 1 'face 'bold bar)
818 (while (< i quail-keyboard-layout-len)
822 (insert-char 32 (+ row (/ (- row 2) 2)))))
823 (setq lower (aref layout i)
824 upper (aref layout (1+ i)))
825 (if (and (integerp lower) (>= lower 128) (< lower 256))
826 (setq lower (unibyte-char-to-multibyte lower)))
827 (if (and (integerp upper) (>= upper 128) (< upper 256))
828 (setq upper (unibyte-char-to-multibyte upper)))
830 (if (= (if (stringp lower) (string-width lower) (char-width lower)) 1)
833 (if (= (if (stringp upper) (string-width upper) (char-width upper)) 1)
838 ;; Insert horizontal lines while deleting blank key columns at the
839 ;; beginning and end of each line.
841 (narrow-to-region pos (point))
843 ;;(while (looking-at "[| ]*$")
845 ;;(delete-region pos (point)))
846 (let ((from1 100) (to1 0) from2 to2)
848 (if (looking-at "[| ]*$")
849 ;; The entire row is blank.
850 (delete-region (point) (match-end 0))
851 ;; Delete blank key columns at the head.
852 (if (looking-at " *\\(| \\)+")
853 (subst-char-in-region (point) (match-end 0) ?| ? ))
854 ;; Delete blank key columns at the tail.
855 (if (re-search-forward "\\( |\\)+$" (line-end-position) t)
856 (delete-region (match-beginning 0) (point)))
858 ;; Calculate the start and end columns of a horizontal line.
860 (setq from2 from1 to2 to1)
861 (skip-chars-forward " ")
862 (setq from2 (current-column))
864 (setq to2 (current-column))
870 ;; If the previous or the current line has at least one key
871 ;; column, insert a horizontal line.
873 (insert-char 32 from1)
876 (insert-char ?- (- (- to1 from1) 2))
878 (put-text-property pos (point) 'face 'bold)
880 (setq from1 from2 to1 to2)
882 ;; Insert "space bar" box.
886 " +-----------------------------+
888 +-----------------------------+
890 (put-text-property pos (point) 'face 'bold)
896 (defun quail-show-keyboard-layout (&optional keyboard-type)
897 "Show the physical layout of the keyboard type KEYBOARD-TYPE.
899 The variable `quail-keyboard-layout-type' holds the currently selected
902 (list (completing-read "Keyboard type (default current choice): "
903 quail-keyboard-layout-alist
905 (or (and keyboard-type (> (length keyboard-type) 0))
906 (setq keyboard-type quail-keyboard-layout-type))
907 (let ((layout (assoc keyboard-type quail-keyboard-layout-alist)))
909 (error "Unknown keyboard type: %s" keyboard-type))
910 (with-output-to-temp-buffer "*Help*"
911 (with-current-buffer standard-output
912 (insert "Keyboard layout (keyboard type: "
915 (quail-insert-kbd-layout (cdr layout))))))
919 (defsubst quail-map-p (object)
920 "Return t if OBJECT is a Quail map.
922 A Quail map holds information how a particular key should be translated.
923 Its format is (TRANSLATION . ALIST).
924 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
925 In the latter case, each element of VECTOR is a candidate for the translation,
926 and INDEX points the currently selected translation.
928 ALIST is normally a list of elements that look like (CHAR . DEFN),
929 where DEFN is another Quail map for a longer key (CHAR added to the
930 current key). It may also be a symbol of a function which returns an
931 alist of the above format.
933 Just after a Quail package is read, TRANSLATION may be a string or a
934 vector. Then each element of the string or vector is a candidate for
935 the translation. These objects are transformed to cons cells in the
936 format \(INDEX . VECTOR), as described above."
938 (let ((translation (car object)))
939 (or (integerp translation) (null translation)
940 (vectorp translation) (stringp translation)
941 (symbolp translation)
942 (and (consp translation) (not (vectorp (cdr translation))))))
943 (let ((alist (cdr object)))
944 (or (and (listp alist) (consp (car alist)))
948 (defmacro quail-define-rules (&rest rules)
949 "Define translation rules of the current Quail package.
950 Each argument is a list of KEY and TRANSLATION.
951 KEY is a string meaning a sequence of keystrokes to be translated.
952 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
953 If it is a character, it is the sole translation of KEY.
954 If it is a string, each character is a candidate for the translation.
955 If it is a vector, each element (string or character) is a candidate
957 In these cases, a key specific Quail map is generated and assigned to KEY.
959 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
960 it is used to handle KEY.
962 The first argument may be an alist of annotations for the following
963 rules. Each element has the form (ANNOTATION . VALUE), where
964 ANNOTATION is a symbol indicating the annotation type. Currently
965 the following annotation types are supported.
967 append -- the value non-nil means that the following rules should
968 be appended to the rules of the current Quail package.
970 face -- the value is a face to use for displaying TRANSLATIONs in
973 advice -- the value is a function to call after one of RULES is
974 selected. The function is called with one argument, the
975 selected TRANSLATION string, after the TRANSLATION is
978 no-decode-map --- the value non-nil means that decoding map is not
979 generated for the following translations."
981 append no-decode-map props)
982 ;; If the first argument is an alist of annotations, handle them.
983 (if (consp (car (car l)))
984 (let ((annotations (car l)))
985 (setq append (assq 'append annotations))
987 (setq annotations (delete append annotations)
988 append (cdr append)))
989 (setq no-decode-map (assq 'no-decode-map annotations))
991 (setq annotations (delete no-decode-map annotations)
992 no-decode-map (cdr no-decode-map)))
993 ;; Convert the remaining annoations to property list PROPS.
996 (cons (car (car annotations))
997 (cons (cdr (car annotations))
999 annotations (cdr annotations)))
1001 ;; Process the remaining arguments one by one.
1003 ;; There's no way to add new rules at compiling time.
1006 (decode-map (and (quail-decode-map) (not ,no-decode-map)))
1007 (properties ',props)
1010 (setq key (car (car tail)) trans (car (cdr (car tail)))
1012 (quail-defrule-internal key trans map t decode-map properties)))
1013 ;; We can build up quail map and decode map at compiling time.
1014 (let ((map (list nil))
1015 (decode-map (if (not no-decode-map) (list 'decode-map)))
1018 (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
1019 (quail-defrule-internal key trans map t decode-map props))
1020 `(if (not (quail-decode-map))
1021 (quail-install-map ',map)
1022 (quail-install-map ',map)
1023 (quail-install-decode-map ',decode-map))))))
1026 (defun quail-install-map (map &optional name)
1027 "Install the Quail map MAP in the current Quail package.
1029 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1030 which to install MAP.
1032 The installed map can be referred by the function `quail-map'."
1033 (if (null quail-current-package)
1034 (error "No current Quail package"))
1035 (if (null (quail-map-p map))
1036 (error "Invalid Quail map `%s'" map))
1037 (setcar (cdr (cdr quail-current-package)) map))
1040 (defun quail-install-decode-map (decode-map &optional name)
1041 "Install the Quail decode map DECODE-MAP in the current Quail package.
1043 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1044 which to install MAP.
1046 The installed decode map can be referred by the function `quail-decode-map'."
1047 (if (null quail-current-package)
1048 (error "No current Quail package"))
1049 (if (if (consp decode-map)
1050 (eq (car decode-map) 'decode-map)
1051 (if (char-table-p decode-map)
1052 (eq (char-table-subtype decode-map) 'quail-decode-map)))
1053 (setcar (nthcdr 10 quail-current-package) decode-map)
1054 (error "Invalid Quail decode map `%s'" decode-map)))
1058 (defun quail-defrule (key translation &optional name append)
1059 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
1060 KEY is a string meaning a sequence of keystrokes to be translated.
1061 TRANSLATION is a character, a string, a vector, a Quail map,
1062 a function, or a cons.
1063 It it is a character, it is the sole translation of KEY.
1064 If it is a string, each character is a candidate for the translation.
1065 If it is a vector, each element (string or character) is a candidate
1066 for the translation.
1067 If it is a cons, the car is one of the above and the cdr is a function
1068 to call when translating KEY (the return value is assigned to the
1069 variable `quail-current-data'). If the cdr part is not a function,
1070 the value itself is assigned to `quail-current-data'.
1071 In these cases, a key specific Quail map is generated and assigned to KEY.
1073 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
1074 it is used to handle KEY.
1076 Optional 3rd argument NAME, if specified, says which Quail package
1077 to define this translation rule in. The default is to define it in the
1078 current Quail package.
1080 Optional 4th argument APPEND, if non-nil, appends TRANSLATION
1081 to the current translations for KEY instead of replacing them."
1083 (let ((package (quail-package name)))
1085 (error "No Quail package `%s'" name))
1086 (setq quail-current-package package)))
1087 (quail-defrule-internal key translation (quail-map) append))
1090 (defun quail-defrule-internal (key trans map &optional append decode-map props)
1091 "Define KEY as TRANS in a Quail map MAP.
1093 If Optional 4th arg APPEND is non-nil, TRANS is appended to the
1094 current translations for KEY instead of replacing them.
1096 Optional 5th arg DECODE-MAP is a Quail decode map.
1098 Optional 6th arg PROPS is a property list annotating TRANS. See the
1099 function `quail-define-rules' for the detail."
1100 (if (not (or (stringp key) (vectorp key)))
1101 (error "Invalid Quail key `%s'" key))
1102 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
1105 (quail-map-p trans)))
1106 (error "Invalid Quail translation `%s'" trans))
1107 (if (null (quail-map-p map))
1108 (error "Invalid Quail map `%s'" map))
1109 (let ((len (length key))
1112 ;; Make a map for registering TRANS if necessary.
1114 (if (null (consp map))
1115 ;; We come here, for example, when we try to define a rule
1116 ;; for "ABC" but a rule for "AB" is already defined as a
1118 (error "Quail key %s is too long" key))
1119 (setq ch (aref key idx)
1120 entry (assq ch (cdr map)))
1123 (setq entry (cons ch (list nil)))
1124 (setcdr map (cons entry (cdr map)))))
1125 (setq map (cdr entry))
1126 (setq idx (1+ idx)))
1129 ;; We come here, for example, when we try to define a rule
1130 ;; for "AB" as a symbol but a rule for "ABC" is already
1132 (error "Quail key %s is too short" key)
1133 (setcdr entry trans))
1134 (if (quail-map-p trans)
1135 (if (not (listp (cdr map)))
1136 ;; We come here, for example, when we try to define a rule
1137 ;; for "AB" as a symbol but a rule for "ABC" is already
1139 (error "Quail key %s is too short" key)
1140 (if (not (listp (cdr trans)))
1142 ;; We come here, for example, when we try to
1143 ;; define a rule for "AB" as a symbol but a rule
1144 ;; for "ABC" is already defined.
1145 (error "Quail key %s is too short" key)
1146 (setcdr entry trans))
1147 (setcdr entry (append trans (cdr map)))))
1148 ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS
1149 ;; to a vector of strings, add PROPS to each string and record
1150 ;; this rule in DECODE-MAP.
1151 (when (and (or props decode-map)
1152 (not (consp trans)) (not (symbolp trans)))
1153 (if (integerp trans)
1154 (setq trans (vector trans))
1156 (setq trans (string-to-vector trans))))
1157 (let ((len (length trans))
1161 (setq elt (aref trans len))
1163 (setq elt (char-to-string elt)))
1164 (aset trans len elt)
1166 (add-text-properties 0 (length elt) props elt))
1169 (cons (cons elt key) (cdr decode-map)))))))
1170 (if (and (car map) append)
1171 (let ((prev (quail-get-translation (car map) key len)))
1173 (setq prev (vector prev))
1174 (setq prev (cdr prev)))
1175 (if (integerp trans)
1176 (setq trans (vector trans))
1178 (setq trans (string-to-vector trans))))
1180 (cons (list 0 0 0 0 nil)
1181 (vconcat prev trans)))))
1182 (setcar map trans)))))
1184 (defun quail-get-translation (def key len)
1185 "Return the translation specified as DEF for KEY of length LEN.
1186 The translation is either a character or a cons of the form (INDEX . VECTOR),
1187 where VECTOR is a vector of candidates (character or string) for
1188 the translation, and INDEX points into VECTOR to specify the currently
1189 selected translation."
1190 (if (and def (symbolp def))
1192 ;; DEF is a symbol of a function which returns valid translation.
1193 (setq def (funcall def key len))
1195 (if (and (consp def) (not (vectorp (cdr def))))
1196 (setq def (car def)))
1199 ((or (integerp def) (consp def))
1207 ;; If the length is 1, we don't need vector but a single candidate
1208 ;; as the translation.
1209 (if (= (length def) 1)
1211 ;; Each character in DEF is a candidate of translation. Reform
1212 ;; it as (INDICES . VECTOR).
1213 (cons (list 0 0 0 0 nil) (string-to-vector def))))
1216 ;; If the length is 1, and the length of element string is 1, we
1217 ;; don't need vector but a single candidate as the translation.
1218 (if (and (= (length def) 1)
1219 (= (length (aref def 0)) 1))
1220 (aref (aref def 0) 0)
1221 ;; Each element (string or character) in DEF is a candidate of
1222 ;; translation. Reform it as (INDICES . VECTOR).
1223 (cons (list 0 0 0 0 nil) def)))
1226 (error "Invalid object in Quail map: %s" def))))
1228 (defun quail-lookup-key (key &optional len not-reset-indices)
1229 "Lookup KEY of length LEN in the current Quail map and return the definition.
1230 The returned value is a Quail map specific to KEY."
1232 (setq len (length key)))
1235 (kbd-translate (quail-kbd-translate))
1236 slot ch translation def)
1237 (while (and map (< idx len))
1238 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
1241 (if (and (cdr map) (symbolp (cdr map)))
1242 (setcdr map (funcall (cdr map) key idx)))
1243 (setq slot (assq ch (cdr map)))
1244 (if (and (cdr slot) (symbolp (cdr slot)))
1245 (setcdr slot (funcall (cdr slot) key idx)))
1246 (setq map (cdr slot)))
1247 (setq def (car map))
1248 (setq quail-current-translations nil)
1249 (if (and map (setq translation (quail-get-translation def key len)))
1251 (if (and (consp def) (not (vectorp (cdr def))))
1253 (if (not (equal (car def) translation))
1254 ;; We must reflect TRANSLATION to car part of DEF.
1255 (setcar def translation))
1256 (setq quail-current-data
1257 (if (functionp (cdr def))
1260 (if (not (equal def translation))
1261 ;; We must reflect TRANSLATION to car part of MAP.
1262 (setcar map translation)))
1263 (if (and (consp translation) (vectorp (cdr translation)))
1265 (setq quail-current-translations translation)
1266 (if (and (not not-reset-indices) (quail-forget-last-selection))
1267 (setcar (car quail-current-translations) 0))))))
1268 ;; We may have to reform cdr part of MAP.
1269 (if (and (cdr map) (functionp (cdr map)))
1270 (setcdr map (funcall (cdr map) key len)))
1273 (put 'quail-error 'error-conditions '(quail-error error))
1274 (defun quail-error (&rest args)
1275 (signal 'quail-error (apply 'format args)))
1277 (defun quail-input-string-to-events (str)
1278 "Convert input string STR to a list of events.
1279 Do so while interleaving with the following special events:
1280 \(compose-last-chars LEN COMPONENTS)
1281 \(quail-advice INPUT-STRING)"
1282 (let* ((events (mapcar
1284 ;; This gives us the chance to unify on input
1285 ;; (e.g. using ucs-tables.el).
1286 (or (and translation-table-for-input
1287 (aref translation-table-for-input c))
1292 composition from to)
1293 (while (and (> idx 0)
1294 (setq composition (find-composition idx 0 str t)))
1295 (setq from (car composition) to (nth 1 composition))
1296 (setcdr (nthcdr (1- to) events)
1297 (cons (list 'compose-last-chars (- to from)
1298 (and (not (nth 3 composition)) (nth 2 composition)))
1299 (nthcdr to events)))
1300 (setq idx (1- from)))
1301 (if (or (get-text-property 0 'advice str)
1302 (next-single-property-change 0 'advice str))
1304 (nconc events (list (list 'quail-advice str)))))
1307 (defvar quail-translating nil)
1308 (defvar quail-converting nil)
1309 (defvar quail-conversion-str nil)
1311 (defun quail-input-method (key)
1312 (if (or buffer-read-only
1313 overriding-terminal-local-map
1314 overriding-local-map)
1316 (quail-setup-overlays (quail-conversion-keymap))
1317 (let ((modified-p (buffer-modified-p))
1318 (buffer-undo-list t)
1319 (inhibit-modification-hooks t))
1321 (let ((input-string (if (quail-conversion-keymap)
1322 (quail-start-conversion key)
1323 (quail-start-translation key))))
1324 (setq quail-guidance-str "")
1325 (when (and (stringp input-string)
1326 (> (length input-string) 0))
1327 (if input-method-exit-on-first-char
1328 (list (aref input-string 0))
1329 (quail-input-string-to-events input-string))))
1330 (quail-delete-overlays)
1331 (set-buffer-modified-p modified-p)
1332 ;; Run this hook only when the current input method doesn't require
1333 ;; conversion. When conversion is required, the conversion function
1334 ;; should run this hook at a proper timing.
1335 (unless (quail-conversion-keymap)
1336 (run-hooks 'input-method-after-insert-chunk-hook))))))
1338 (defun quail-overlay-region-events (overlay)
1339 (let ((start (overlay-start overlay))
1340 (end (overlay-end overlay)))
1343 (string-to-list (buffer-substring start end))
1344 (delete-region start end)))))
1346 (defsubst quail-delete-region ()
1347 "Delete the text in the current translation region of Quail."
1348 (if (overlay-start quail-overlay)
1349 (delete-region (overlay-start quail-overlay)
1350 (overlay-end quail-overlay))))
1352 (defun quail-start-translation (key)
1353 "Start translation of the typed character KEY by the current Quail package.
1354 Return the input string."
1355 ;; Check the possibility of translating KEY.
1356 ;; If KEY is nil, we can anyway start translation.
1357 (if (or (and (integerp key)
1358 (assq (if (quail-kbd-translate)
1359 (quail-keyboard-translate key) key)
1362 ;; OK, we can start translation.
1363 (let* ((echo-keystrokes 0)
1365 (overriding-terminal-local-map (quail-translation-keymap))
1366 (generated-events nil)
1367 (input-method-function nil)
1368 (modified-p (buffer-modified-p))
1369 last-command-event last-command this-command)
1370 (setq quail-current-key ""
1371 quail-current-str ""
1372 quail-translating t)
1374 (setq unread-command-events (cons key unread-command-events)))
1375 (while quail-translating
1376 (set-buffer-modified-p modified-p)
1377 (quail-show-guidance)
1378 (let* ((prompt (if input-method-use-echo-area
1380 (or input-method-previous-message "")
1382 quail-guidance-str)))
1383 (keyseq (read-key-sequence prompt nil nil t))
1384 (cmd (lookup-key (quail-translation-keymap) keyseq)))
1386 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
1387 (eq cmd 'quail-self-insert-command))
1389 (setq last-command-event (aref keyseq (1- (length keyseq)))
1390 last-command this-command
1394 (call-interactively cmd)
1395 (quail-error (message "%s" (cdr err)) (beep))))
1396 ;; KEYSEQ is not defined in the translation keymap.
1397 ;; Let's return the event(s) to the caller.
1398 (setq unread-command-events
1399 (string-to-list (this-single-command-raw-keys)))
1400 (setq quail-translating nil))))
1401 (quail-delete-region)
1404 ;; Since KEY doesn't start any translation, just return it.
1405 ;; But translate KEY if necessary.
1406 (if (quail-kbd-translate)
1407 (setq key (quail-keyboard-translate key)))
1408 (char-to-string key)))
1410 (defun quail-start-conversion (key)
1411 "Start conversion of the typed character KEY by the current Quail package.
1412 Return the input string."
1413 ;; Check the possibility of translating KEY.
1414 ;; If KEY is nil, we can anyway start translation.
1415 (if (or (and (integerp key)
1416 (assq (if (quail-kbd-translate)
1417 (quail-keyboard-translate key) key)
1420 ;; Ok, we can start translation and conversion.
1421 (let* ((echo-keystrokes 0)
1423 (overriding-terminal-local-map (quail-conversion-keymap))
1424 (generated-events nil)
1425 (input-method-function nil)
1426 (modified-p (buffer-modified-p))
1427 last-command-event last-command this-command)
1428 (setq quail-current-key ""
1429 quail-current-str ""
1432 quail-conversion-str "")
1434 (setq unread-command-events (cons key unread-command-events)))
1435 (while quail-converting
1436 (set-buffer-modified-p modified-p)
1437 (or quail-translating
1439 (setq quail-current-key ""
1440 quail-current-str ""
1441 quail-translating t)
1442 (quail-setup-overlays nil)))
1443 (quail-show-guidance)
1444 (let* ((prompt (if input-method-use-echo-area
1446 (or input-method-previous-message "")
1447 quail-conversion-str
1449 quail-guidance-str)))
1450 (keyseq (read-key-sequence prompt nil nil t))
1451 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1452 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1454 (setq last-command-event (aref keyseq (1- (length keyseq)))
1455 last-command this-command
1459 (call-interactively cmd)
1460 (quail-error (message "%s" (cdr err)) (beep)))
1461 (or quail-translating
1463 (if quail-current-str
1464 (setq quail-conversion-str
1465 (concat quail-conversion-str
1466 (if (stringp quail-current-str)
1468 (char-to-string quail-current-str)))))
1469 (if (or input-method-exit-on-first-char
1470 (= (length quail-conversion-str) 0))
1471 (setq quail-converting nil)))))
1472 ;; KEYSEQ is not defined in the conversion keymap.
1473 ;; Let's return the event(s) to the caller.
1474 (setq unread-command-events
1475 (string-to-list (this-single-command-raw-keys)))
1476 (setq quail-converting nil))))
1477 (setq quail-translating nil)
1478 (if (overlay-start quail-conv-overlay)
1479 (delete-region (overlay-start quail-conv-overlay)
1480 (overlay-end quail-conv-overlay)))
1481 (if (> (length quail-conversion-str) 0)
1482 quail-conversion-str))
1484 ;; Since KEY doesn't start any translation, just return it.
1485 ;; But translate KEY if necessary.
1486 (if (quail-kbd-translate)
1487 (setq key (quail-keyboard-translate key)))
1488 (char-to-string key)))
1490 (defun quail-terminate-translation ()
1491 "Terminate the translation of the current key."
1492 (setq quail-translating nil)
1493 (setq quail-guidance-str " "))
1495 (defun quail-select-current ()
1496 "Accept the currently selected translation."
1498 (quail-terminate-translation))
1500 (defun quail-update-translation (control-flag)
1501 "Update the current translation status according to CONTROL-FLAG.
1502 If CONTROL-FLAG is integer value, it is the number of keys in the
1503 head `quail-current-key' which can be translated. The remaining keys
1504 are put back to `unread-command-events' to be handled again. If
1505 CONTROL-FLAG is t, terminate the translation for the whole keys in
1506 `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation
1508 (let ((func (quail-update-translation-function)))
1510 (setq control-flag (funcall func control-flag))
1511 (cond ((numberp control-flag)
1512 (let ((len (length quail-current-key)))
1513 (if (= control-flag 0)
1514 (setq quail-current-str
1515 (if (quail-kbd-translate)
1516 (quail-keyseq-translate quail-current-key)
1517 quail-current-key)))
1518 (or input-method-exit-on-first-char
1519 (while (> len control-flag)
1521 (setq unread-command-events
1522 (cons (aref quail-current-key len)
1523 unread-command-events))))))
1524 ((null control-flag)
1525 (unless quail-current-str
1526 (setq quail-current-str
1527 (if (quail-kbd-translate)
1528 (quail-keyseq-translate quail-current-key)
1530 (if (and input-method-exit-on-first-char
1532 (setq control-flag t)))))))
1533 (or input-method-use-echo-area
1535 (quail-delete-region)
1537 (or enable-multibyte-characters
1539 (if (stringp quail-current-str)
1541 (mapc #'(lambda (ch)
1542 (when (/= (unibyte-char-to-multibyte
1543 (multibyte-char-to-unibyte ch))
1548 (if (/= (unibyte-char-to-multibyte
1549 (multibyte-char-to-unibyte quail-current-str))
1551 (setq char quail-current-str)))
1553 (message "Can't input %c in the current unibyte buffer" char)
1557 (setq quail-current-str nil)
1558 (throw 'quail-tag nil))))
1559 (insert quail-current-str)
1560 (move-overlay quail-overlay pos (point))
1561 (if (overlayp quail-conv-overlay)
1562 (if (not (overlay-start quail-conv-overlay))
1563 (move-overlay quail-conv-overlay pos (point))
1564 (if (< (overlay-end quail-conv-overlay) (point))
1565 (move-overlay quail-conv-overlay
1566 (overlay-start quail-conv-overlay)
1568 (let (quail-current-str)
1569 (quail-update-guidance))
1570 (or (stringp quail-current-str)
1571 (setq quail-current-str (char-to-string quail-current-str)))
1573 (quail-terminate-translation)))
1575 (defun quail-self-insert-command ()
1576 "Translate the typed key by the current Quail map, and insert."
1578 (setq quail-current-key
1579 (concat quail-current-key (char-to-string last-command-event)))
1580 (or (catch 'quail-tag
1581 (quail-update-translation (quail-translate-key))
1583 ;; If someone throws for `quail-tag' by value nil, we exit from
1584 ;; translation mode.
1585 (setq quail-translating nil)))
1587 (defun quail-map-definition (map)
1588 "Return the actual definition part of Quail map MAP."
1589 (let ((def (car map)))
1590 (if (and (consp def) (not (vectorp (cdr def))))
1591 (setq def (car def)))
1596 (defun quail-get-current-str (len def)
1597 "Return string to be shown as current translation of key sequence.
1598 LEN is the length of the sequence. DEF is a definition part of the
1599 Quail map for the sequence."
1600 (or (and (consp def) (aref (cdr def) (car (car def))))
1603 (let* ((str (quail-get-current-str
1605 (quail-map-definition (quail-lookup-key
1606 quail-current-key (1- len)))))
1607 (substr1 (substring quail-current-key (1- len) len))
1608 (str1 (and (quail-deterministic)
1609 (quail-get-current-str
1611 (quail-map-definition (quail-lookup-key
1614 (concat (if (stringp str) str (char-to-string str))
1616 (if (stringp str1) str1 (char-to-string str1))
1619 (defvar quail-guidance-translations-starting-column 20)
1621 (defun quail-update-current-translations (&optional relative-index)
1622 "Update `quail-current-translations'.
1623 Make RELATIVE-INDEX the current translation."
1624 (let* ((indices (car quail-current-translations))
1626 (start (nth 1 indices))
1627 (end (nth 2 indices)))
1628 ;; Validate the index number of current translation.
1630 (setcar indices (setq cur 0))
1631 (if (>= cur (length (cdr quail-current-translations)))
1633 (setq cur (1- (length (cdr quail-current-translations)))))))
1635 (if (or (null end) ; We have not yet calculated END.
1636 (< cur start) ; We moved to the previous block.
1637 (>= cur end)) ; We moved to the next block.
1638 (let ((len (length (cdr quail-current-translations)))
1639 (maxcol (- (window-width)
1640 quail-guidance-translations-starting-column))
1641 (block (nth 3 indices))
1642 col idx width trans num-items blocks)
1644 ;; We must calculate from the head.
1645 (setq start 0 block 0)
1646 (if end ; i.e. (>= cur end)
1648 (setq idx start col 0 end start num-items 0)
1649 ;; Loop until we hit the tail, or reach the block of CUR.
1650 (while (and (< idx len) (>= cur end))
1652 (setq start idx col 0 block (1+ block)))
1653 (setq trans (aref (cdr quail-current-translations) idx))
1654 (setq width (if (integerp trans) (char-width trans)
1655 (string-width trans)))
1656 (setq col (+ col width 3) num-items (1+ num-items))
1657 (if (and (> num-items 0)
1658 (or (>= col maxcol) (> num-items 10)))
1659 (setq end idx num-items 0)
1660 (setq idx (1+ idx))))
1661 (setcar (nthcdr 3 indices) block)
1664 ;; We hit the tail before reaching MAXCOL.
1666 (setcar (nthcdr 4 indices) block)))
1667 (setcar (cdr indices) start)
1668 (setcar (nthcdr 2 indices) end)))
1670 (if (>= (+ start relative-index) end)
1671 (setcar indices (1- end))
1672 (setcar indices (+ start relative-index))))
1673 (setq quail-current-str
1674 (aref (cdr quail-current-translations) (car indices)))
1675 (or (stringp quail-current-str)
1676 (setq quail-current-str (char-to-string quail-current-str)))))
1678 (defun quail-translate-key ()
1679 "Translate the current key sequence according to the current Quail map.
1680 Return t if we can terminate the translation.
1681 Return nil if the current key sequence may be followed by more keys.
1682 Return number if we can't find any translation for the current key
1683 sequence. The number is the count of valid keys in the current
1684 sequence counting from the head."
1685 (let* ((len (length quail-current-key))
1686 (map (quail-lookup-key quail-current-key len))
1689 (let ((def (quail-map-definition map)))
1690 (setq quail-current-str (quail-get-current-str len def))
1691 ;; Return t only if we can terminate the current translation.
1693 ;; No alternative translations.
1694 (or (null (consp def)) (= (length (cdr def)) 1))
1695 ;; No translation for the longer key.
1697 ;; No shorter breaking point.
1698 (or (null (quail-maximum-shortest))
1700 (null (quail-lookup-key quail-current-key (1- len)))
1701 (null (quail-lookup-key
1702 (substring quail-current-key -2 -1) 1)))))
1704 ;; There's no translation for the current key sequence. Before
1705 ;; giving up, we must check two possibilities.
1707 (quail-maximum-shortest)
1709 (setq def (quail-map-definition
1710 (quail-lookup-key quail-current-key (- len 2))))
1711 (quail-lookup-key (substring quail-current-key -2) 2))
1712 ;; Now the sequence is "...ABCD", which can be split into
1713 ;; "...AB" and "CD..." to get valid translation.
1714 ;; At first, get translation of "...AB".
1715 (setq quail-current-str (quail-get-current-str (- len 2) def))
1716 ;; Then, return the length of "...AB".
1720 (quail-lookup-key (substring quail-current-key 0 -1))
1721 quail-current-translations
1722 (not (quail-deterministic))
1723 (setq ch (aref quail-current-key (1- len)))
1724 (>= ch ?0) (<= ch ?9))
1725 ;; A numeric key is entered to select a desirable translation.
1726 (setq quail-current-key (substring quail-current-key 0 -1))
1727 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1728 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1729 (quail-update-current-translations ch)
1730 ;; And, we can terminate the current translation.
1733 ((quail-deterministic)
1734 ;; No way to handle the last character in this context.
1735 ;; Commit the longest successfully translated characters, and
1736 ;; handle the remaining characters in a new loop.
1738 (while (and (not def) (> len 1))
1740 (setq def (quail-map-definition
1741 (quail-lookup-key quail-current-key len))))
1742 (if def (setq quail-current-str
1743 (quail-get-current-str len def))
1744 (setq quail-current-str (aref quail-current-key 0)))
1748 ;; No way to handle the last character in this context.
1749 (setq def (quail-map-definition
1750 (quail-lookup-key quail-current-key (1- len))))
1751 (if def (setq quail-current-str
1752 (quail-get-current-str (1- len) def)))
1755 (defun quail-next-translation ()
1756 "Select next translation in the current batch of candidates."
1758 (if quail-current-translations
1759 (let ((indices (car quail-current-translations)))
1760 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1761 ;; We are already at the tail.
1763 (setcar indices (1+ (car indices)))
1764 (quail-update-current-translations)
1765 (quail-update-translation nil)))
1766 (setq unread-command-events
1767 (cons last-command-event unread-command-events))
1768 (quail-terminate-translation)))
1770 (defun quail-prev-translation ()
1771 "Select previous translation in the current batch of candidates."
1773 (if quail-current-translations
1774 (let ((indices (car quail-current-translations)))
1775 (if (= (car indices) 0)
1776 ;; We are already at the head.
1778 (setcar indices (1- (car indices)))
1779 (quail-update-current-translations)
1780 (quail-update-translation nil)))
1781 (setq unread-command-events
1782 (cons last-command-event unread-command-events))
1783 (quail-terminate-translation)))
1785 (defun quail-next-translation-block ()
1786 "Select from the next block of translations."
1788 (if quail-current-translations
1789 (let* ((indices (car quail-current-translations))
1790 (offset (- (car indices) (nth 1 indices))))
1791 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1792 ;; We are already at the last block.
1794 (setcar indices (+ (nth 2 indices) offset))
1795 (quail-update-current-translations)
1796 (quail-update-translation nil)))
1797 (setq unread-command-events
1798 (cons last-command-event unread-command-events))
1799 (quail-terminate-translation)))
1801 (defun quail-prev-translation-block ()
1802 "Select the previous batch of 10 translation candidates."
1804 (if quail-current-translations
1805 (let* ((indices (car quail-current-translations))
1806 (offset (- (car indices) (nth 1 indices))))
1807 (if (= (nth 1 indices) 0)
1808 ;; We are already at the first block.
1810 (setcar indices (1- (nth 1 indices)))
1811 (quail-update-current-translations)
1812 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1814 (setcar indices (+ (nth 1 indices) offset))
1815 (quail-update-current-translations)))
1816 (quail-update-translation nil)))
1817 (setq unread-command-events
1818 (cons last-command-event unread-command-events))
1819 (quail-terminate-translation)))
1821 (defun quail-abort-translation ()
1822 "Abort translation and delete the current Quail key sequence."
1824 (quail-delete-region)
1825 (setq quail-current-str nil)
1826 (quail-terminate-translation))
1828 (defun quail-delete-last-char ()
1829 "Delete the last input character from the current Quail key sequence."
1831 (if (= (length quail-current-key) 1)
1832 (quail-abort-translation)
1833 (setq quail-current-key (substring quail-current-key 0 -1))
1834 (quail-delete-region)
1835 (quail-update-translation (quail-translate-key))))
1837 ;; For conversion mode.
1839 (defsubst quail-point-in-conversion-region ()
1840 "Return non-nil value if the point is in conversion region of Quail mode."
1842 (and (setq start (overlay-start quail-conv-overlay))
1843 (>= (setq pos (point)) start)
1844 (<= pos (overlay-end quail-conv-overlay)))))
1846 (defun quail-conversion-backward-char ()
1848 (if (<= (point) (overlay-start quail-conv-overlay))
1849 (quail-error "Beginning of conversion region"))
1850 (setq quail-translating nil)
1853 (defun quail-conversion-forward-char ()
1855 (if (>= (point) (overlay-end quail-conv-overlay))
1856 (quail-error "End of conversion region"))
1857 (setq quail-translating nil)
1860 (defun quail-conversion-beginning-of-region ()
1862 (setq quail-translating nil)
1863 (goto-char (overlay-start quail-conv-overlay)))
1865 (defun quail-conversion-end-of-region ()
1867 (setq quail-translating nil)
1868 (goto-char (overlay-end quail-conv-overlay)))
1870 (defun quail-conversion-delete-char ()
1872 (setq quail-translating nil)
1873 (if (>= (point) (overlay-end quail-conv-overlay))
1874 (quail-error "End of conversion region"))
1876 (let ((start (overlay-start quail-conv-overlay))
1877 (end (overlay-end quail-conv-overlay)))
1878 (setq quail-conversion-str (buffer-substring start end))
1880 (setq quail-converting nil))))
1882 (defun quail-conversion-delete-tail ()
1884 (if (>= (point) (overlay-end quail-conv-overlay))
1885 (quail-error "End of conversion region"))
1886 (delete-region (point) (overlay-end quail-conv-overlay))
1887 (let ((start (overlay-start quail-conv-overlay))
1888 (end (overlay-end quail-conv-overlay)))
1889 (setq quail-conversion-str (buffer-substring start end))
1891 (setq quail-converting nil))))
1893 (defun quail-conversion-backward-delete-char ()
1895 (if (> (length quail-current-key) 0)
1896 (quail-delete-last-char)
1897 (if (<= (point) (overlay-start quail-conv-overlay))
1898 (quail-error "Beginning of conversion region"))
1900 (let ((start (overlay-start quail-conv-overlay))
1901 (end (overlay-end quail-conv-overlay)))
1902 (setq quail-conversion-str (buffer-substring start end))
1904 (setq quail-converting nil)))))
1906 (defun quail-do-conversion (func &rest args)
1907 "Call FUNC to convert text in the current conversion region of Quail.
1908 Remaining args are for FUNC."
1909 (delete-overlay quail-overlay)
1912 (defun quail-no-conversion ()
1913 "Do no conversion of the current conversion region of Quail."
1915 (setq quail-converting nil))
1917 ;; Guidance, Completion, and Help buffer handlers.
1919 (defun quail-make-guidance-frame ()
1920 "Make a new one-line frame for Quail guidance."
1921 (let* ((fparam (frame-parameters))
1922 (top (cdr (assq 'top fparam)))
1923 (border (cdr (assq 'border-width fparam)))
1924 (internal-border (cdr (assq 'internal-border-width fparam)))
1926 (frame-char-height) (* internal-border 2) (* border 2))))
1928 (setq newtop (+ top (frame-pixel-height) internal-border border)))
1929 (make-frame (append '((user-position . t) (height . 1)
1931 (menu-bar-lines . 0) (tool-bar-lines . 0))
1932 (cons (cons 'top newtop) fparam)))))
1934 (defun quail-setup-completion-buf ()
1935 "Setup Quail completion buffer."
1936 (unless (buffer-live-p quail-completion-buf)
1937 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1938 (setq quail-completion-buf (get-buffer-create "*Quail Completions*")))
1939 (with-current-buffer quail-completion-buf
1940 (setq buffer-read-only t)
1941 (setq quail-overlay (make-overlay 1 1))
1942 (overlay-put quail-overlay 'face 'highlight))))
1944 (defun quail-require-guidance-buf ()
1945 "Return t if the current Quail package requires showing guidance buffer."
1946 (and input-method-verbose-flag
1947 (if (eq input-method-verbose-flag 'default)
1948 (not (and (eq (selected-window) (minibuffer-window))
1950 (if (eq input-method-verbose-flag 'complex-only)
1951 (not (quail-simple))
1955 ;; Quail specific version of minibuffer-message. It displays STRING
1956 ;; with timeout 1000000 seconds instead of two seconds.
1958 (defun quail-minibuffer-message (string)
1960 (let ((point-max (point-max))
1963 (goto-char point-max)
1966 (delete-region point-max (point-max))
1969 unread-command-events '(7)))))
1971 (defun quail-show-guidance ()
1972 "Display a guidance for Quail input method in some window.
1973 The guidance is normally displayed at the echo area,
1974 or in a newly created frame (if the current buffer is a
1975 minibuffer and the selected frame has no other windows)."
1976 ;; At first, setup a buffer for completion.
1977 (quail-setup-completion-buf)
1978 (bury-buffer quail-completion-buf)
1980 ;; Then, show the guidance.
1981 (when (and (quail-require-guidance-buf)
1982 (not input-method-use-echo-area)
1983 (null unread-command-events)
1984 (null unread-post-input-method-events))
1985 (if (eq (selected-window) (minibuffer-window))
1986 (if (eq (minibuffer-window) (frame-root-window))
1987 ;; Use another frame. It is sure that we are using some
1989 (let ((guidance quail-guidance-str))
1990 (or (frame-live-p quail-guidance-frame)
1991 (setq quail-guidance-frame
1992 (quail-make-guidance-frame)))
1993 (or (buffer-live-p quail-guidance-buf)
1994 (setq quail-guidance-buf
1995 (get-buffer-create " *Quail-guidance*")))
1997 (set-buffer quail-guidance-buf)
1999 (setq cursor-type nil)
2001 (set-window-buffer (frame-root-window quail-guidance-frame)
2003 (quail-minibuffer-message
2004 (format " [%s]" current-input-method-title)))
2005 ;; Show the guidance in the next line of the currrent
2007 (quail-minibuffer-message
2009 current-input-method-title quail-guidance-str)))
2010 ;; Show the guidance in echo area without logging.
2011 (let ((message-log-max nil))
2012 (message "%s" quail-guidance-str)))))
2014 (defun quail-hide-guidance ()
2015 "Hide the Quail guidance."
2016 (when (and (quail-require-guidance-buf)
2017 (or (eq (selected-window) (minibuffer-window))
2018 input-method-use-echo-area)
2019 (eq (minibuffer-window) (frame-root-window)))
2020 ;; We are using another frame for the guidance.
2021 (if (frame-live-p quail-guidance-frame)
2022 (delete-frame quail-guidance-frame))
2023 (if (buffer-live-p quail-guidance-buf)
2024 (kill-buffer quail-guidance-buf))))
2026 (defun quail-update-guidance ()
2027 "Update the Quail guidance buffer and completion buffer (if displayed now)."
2028 ;; Update the guidance string.
2029 (when (quail-require-guidance-buf)
2030 (let ((guidance (quail-guidance)))
2031 (cond ((or (eq guidance t)
2033 ;; Show the current possible translations.
2034 (setq quail-guidance-str
2035 (quail-get-translations)))
2037 ;; Show the current input keys.
2038 (let ((key quail-current-key))
2039 (if (quail-kbd-translate)
2040 (setq key (quail-keyseq-translate key)))
2041 (setq quail-guidance-str (if (stringp key) key (string key)))))
2043 (setq quail-guidance-str " ")))))
2045 ;; Update completion buffer if displayed now. We highlight the
2046 ;; selected candidate string in *Completion* buffer if any.
2047 (let ((win (get-buffer-window quail-completion-buf))
2051 (setq str (if (stringp quail-current-str)
2053 (if (numberp quail-current-str)
2054 (char-to-string quail-current-str)))
2055 key quail-current-key)
2056 (set-buffer quail-completion-buf)
2057 (goto-char (point-min))
2058 (if (null (search-forward (concat " " key ":") nil t))
2059 (delete-overlay quail-overlay)
2061 (if (and str (search-forward (concat "." str) nil t))
2062 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
2063 (move-overlay quail-overlay (match-beginning 0) (point)))
2064 ;; Now POS points end of KEY and (point) points end of STR.
2065 (if (pos-visible-in-window-p (point) win)
2066 ;; STR is already visible.
2068 ;; We want to make both KEY and STR visible, but if the
2069 ;; window is too short, make at least STR visible.
2070 (setq pos (progn (point) (goto-char pos)))
2072 (set-window-start win (point))
2073 (if (not (pos-visible-in-window-p pos win))
2074 (set-window-start win pos))
2077 (defun quail-get-translations ()
2078 "Return a string containing the current possible translations."
2079 (let ((map (quail-lookup-key quail-current-key nil t))
2080 (str (copy-sequence quail-current-key)))
2081 (if quail-current-translations
2082 (quail-update-current-translations))
2084 ;; Show the current key.
2085 (let ((guidance (quail-guidance)))
2086 (if (listp guidance)
2087 ;; We must replace thetyped key with the specified PROMPTKEY.
2088 (dotimes (i (length str))
2089 (let ((prompt-key (cdr (assoc (aref str i) guidance))))
2091 (aset str i (aref prompt-key 0)))))))
2093 ;; Show followable keys.
2094 (if (and (> (length quail-current-key) 0) (cdr map))
2098 (concat (sort (mapcar (function (lambda (x) (car x)))
2101 ;; Show list of translations.
2102 (if (and quail-current-translations
2103 (not (quail-deterministic)))
2104 (let* ((indices (car quail-current-translations))
2106 (start (nth 1 indices))
2107 (end (nth 2 indices))
2109 (if (< (string-width str)
2110 (- quail-guidance-translations-starting-column 7))
2114 (- quail-guidance-translations-starting-column
2115 7 (string-width str))
2117 (setq str (format "%s(%02d/%s)"
2120 (format "%02d" (nth 4 indices))
2123 (let ((len (length str))
2124 (trans (aref (cdr quail-current-translations) idx)))
2126 (setq trans (string trans)))
2127 (setq str (format "%s %d.%s"
2129 (if (= (- idx start) 9) 0
2133 (put-text-property (+ len 3) (length str)
2134 'face 'highlight str))
2135 (setq idx (1+ idx))))))
2139 (defvar quail-completion-max-depth 5
2140 "The maximum depth of Quail completion list.")
2142 (defun quail-completion ()
2143 "List all completions for the current key.
2144 All possible translations of the current key and whole possible longer keys
2145 are shown (at most to the depth specified `quail-completion-max-depth')."
2147 (quail-setup-completion-buf)
2148 (let ((win (get-buffer-window quail-completion-buf 'visible))
2149 (key quail-current-key)
2150 (map (quail-lookup-key quail-current-key nil t))
2151 (require-update nil))
2152 (with-current-buffer quail-completion-buf
2154 (equal key quail-current-key)
2155 (eq last-command 'quail-completion))
2156 ;; The window for Quail completion buffer has already been
2157 ;; shown. We just scroll it appropriately.
2158 (if (pos-visible-in-window-p (point-max) win)
2159 (set-window-start win (point-min))
2160 (let ((other-window-scroll-buffer quail-completion-buf)
2161 ;; This nil binding is necessary to surely scroll
2162 ;; quail-completion-buf.
2163 (minibuffer-scroll-window nil))
2164 (scroll-other-window)))
2165 (setq quail-current-key key)
2166 (let ((inhibit-read-only t))
2168 (insert "Possible completion and corresponding characters are:\n")
2169 (quail-completion-1 key map 1)
2170 (set-buffer-modified-p nil))
2171 (goto-char (point-min))
2172 (display-buffer (current-buffer))
2173 (setq require-update t)))
2175 (quail-update-guidance)))
2176 (setq this-command 'quail-completion))
2178 (defun quail-completion-1 (key map indent)
2179 "List all completions of KEY in MAP with indentation INDENT."
2180 (let ((len (length key)))
2183 (if (and (symbolp map) (fboundp map))
2184 (setq map (funcall map key len)))
2186 (quail-completion-list-translations map key (+ indent len 1))
2188 (setq indent (+ indent 2))
2189 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
2191 (newkey (make-string (1+ len) 0))
2194 (setq l (funcall l)))
2195 ;; Set KEY in the first LEN characters of NEWKEY.
2197 (aset newkey i (aref key i))
2199 (setq l (reverse l))
2200 (while l ; L = ((CHAR . DEFN) ....) ;
2201 (aset newkey len (car (car l)))
2202 (quail-completion-1 newkey (cdr (car l)) indent)
2203 (setq l (cdr l)))))))
2205 (defun quail-completion-list-translations (map key indent)
2206 "List all possible translations of KEY in Quail MAP with indentation INDENT."
2207 (let (beg (translations
2208 (quail-get-translation (car map) key (length key))))
2209 (if (integerp translations)
2212 ;; Endow the character `translations' with `mouse-face' text
2213 ;; property to enable `mouse-2' completion.
2215 (insert translations)
2216 (put-text-property beg (point) 'mouse-face 'highlight)
2218 ;; We need only vector part.
2219 (setq translations (cdr translations))
2220 ;; Insert every 10 elements with indices in a line.
2221 (let ((len (length translations))
2225 (when (zerop (% i 10))
2229 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
2230 ;; We show the last digit of FROM while converting
2231 ;; 0,1,..,9 to 1,2,..,0.
2232 (insert (format " %d." (% (1+ i) 10)))
2234 (insert (aref translations i))
2235 ;; Passing the mouse over a character will highlight.
2236 (put-text-property beg (point) 'mouse-face 'highlight)
2240 (defun quail-mouse-choose-completion (event)
2241 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
2243 ;; This function is an exact copy of the mouse.el function
2244 ;; `mouse-choose-completion' except that we:
2245 ;; 1) add two lines from `choose-completion' in simple.el to give
2246 ;; the `mouse-2' click a little more leeway.
2247 ;; 2) don't bury *Quail Completions* buffer, so comment a section, and
2248 ;; 3) delete/terminate the current quail selection here.
2249 ;; Give temporary modes such as isearch a chance to turn off.
2250 (run-hooks 'mouse-leave-buffer-hook)
2251 (let ((buffer (window-buffer))
2254 (with-current-buffer (window-buffer (posn-window (event-start event)))
2255 (if completion-reference-buffer
2256 (setq buffer completion-reference-buffer))
2257 (setq base-size completion-base-size)
2259 (goto-char (posn-point (event-start event)))
2261 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2262 (setq end (point) beg (1+ (point))))
2263 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
2264 (setq end (1- (point)) beg (point)))
2266 (quail-error "No completion here"))
2267 (setq beg (previous-single-property-change beg 'mouse-face))
2268 (setq end (or (next-single-property-change end 'mouse-face)
2270 (setq choice (buffer-substring beg end)))))
2271 ; (let ((owindow (selected-window)))
2272 ; (select-window (posn-window (event-start event)))
2273 ; (if (and (one-window-p t 'selected-frame)
2274 ; (window-dedicated-p (selected-window)))
2275 ; ;; This is a special buffer's frame
2276 ; (iconify-frame (selected-frame))
2277 ; (or (window-dedicated-p (selected-window))
2279 ; (select-window owindow))
2280 (quail-delete-region)
2281 (quail-choose-completion-string choice buffer base-size)
2282 (quail-terminate-translation)))
2284 ;; BASE-SIZE here is for compatibility with an (unused) arg of a
2285 ;; previous implementation.
2286 (defun quail-choose-completion-string (choice &optional buffer base-size)
2287 (setq quail-current-str choice)
2288 (choose-completion-string choice buffer))
2290 (defun quail-build-decode-map (map-list key decode-map num
2291 &optional maxnum ignores)
2292 "Build a decoding map.
2293 Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
2294 vs the corresponding translations defined in the Quail map
2295 specified by the first element MAP-LIST. Each pair has the form
2296 \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
2297 \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
2298 is a key sequence to reach MAP.
2299 Optional 5th arg MAXNUM limits the number of accumulated pairs.
2300 Optional 6th arg IGNORES is a list of translations to ignore."
2301 (let* ((map (car map-list))
2302 (translation (quail-get-translation (car map) key (length key)))
2304 (cond ((integerp translation)
2305 ;; Accept only non-ASCII chars not listed in IGNORES.
2306 (when (and (> translation 255) (not (memq translation ignores)))
2308 (cons (cons key translation) (cdr decode-map)))
2309 (setq num (1+ num))))
2310 ((consp translation)
2311 (setq translation (cdr translation))
2312 (let ((multibyte nil))
2313 (mapc (function (lambda (x)
2314 ;; Accept only non-ASCII chars not
2315 ;; listed in IGNORES.
2316 (if (and (if (integerp x) (> x 255)
2317 (> (string-bytes x) (length x)))
2318 (not (member x ignores)))
2319 (setq multibyte t))))
2323 (cons (cons key translation) (cdr decode-map)))
2324 (setq num (+ num (length translation)))))))
2325 (if (and maxnum (> num maxnum))
2327 (setq map (cdr map))
2328 ;; Recursively check the deeper map.
2329 (while (and map (>= num 0))
2330 (setq elt (car map) map (cdr map))
2331 (when (and (integerp (car elt)) (consp (cdr elt))
2332 (not (memq (cdr elt) map-list)))
2333 (setq num (quail-build-decode-map (cons (cdr elt) map-list)
2334 (format "%s%c" key (car elt))
2335 decode-map num maxnum ignores))))
2338 (defun quail-insert-decode-map (decode-map)
2339 "Insert pairs of key sequences vs the corresponding translations.
2340 These are stored in DECODE-MAP using the concise format. DECODE-MAP
2341 should be made by `quail-build-decode-map' (which see)."
2343 (sort (cdr decode-map)
2344 (function (lambda (x y)
2345 (setq x (car x) y (car y))
2346 (or (> (length x) (length y))
2347 (and (= (length x) (length y))
2348 (not (string< x y))))))))
2349 (let ((window-width (window-width (get-buffer-window
2350 (current-buffer) 'visible)))
2351 (single-key-width 3)
2352 (single-trans-width 4)
2353 (multiple-key-width 3)
2356 elt trans width pos cols rows col row str col-width)
2357 ;; Divide the elements of decoding map into single ones (i.e. the
2358 ;; one that has single translation) and multibyte ones (i.e. the
2359 ;; one that has multiple translations).
2361 (setq elt (car decode-map) decode-map (cdr decode-map)
2363 (if (and (vectorp trans) (= (length trans) 1))
2364 (setq trans (aref trans 0)))
2366 (setq multiple-list (cons elt multiple-list))
2367 (setq single-list (cons (cons (car elt) trans) single-list)
2368 width (if (stringp trans) (string-width trans)
2369 (char-width trans)))
2370 (if (> width single-trans-width)
2371 (setq single-trans-width width)))
2372 (setq width (length (car elt)))
2373 (if (> width single-key-width)
2374 (setq single-key-width width))
2375 (if (> width multiple-key-width)
2376 (setq multiple-key-width width)))
2378 (setq col-width (+ single-key-width 1 single-trans-width 1)
2379 cols (/ window-width col-width)
2380 rows (/ (length single-list) cols))
2381 (if (> (% (length single-list) cols) 0)
2382 (setq rows (1+ rows)))
2384 (indent-to (1+ single-key-width))
2386 (indent-to (1+ col-width))
2387 (insert "[type a key sequence to insert the corresponding character]\n")
2389 (insert-char ?\n (+ rows 2))
2391 (setq col (- col-width) row 0)
2393 (setq elt (car single-list) single-list (cdr single-list))
2394 (when (= (% row rows) 0)
2396 (setq col (+ col col-width))
2397 (move-to-column col t)
2398 (insert-char ?- single-key-width)
2400 (insert-char ?- single-trans-width)
2402 (move-to-column col t)
2404 (indent-to (+ col single-key-width 1))
2407 (setq row (1+ row)))
2408 (goto-char (point-max)))
2412 (indent-to (1+ multiple-key-width))
2413 (insert "character(s) [type a key (sequence) and select one from the list]\n")
2414 (insert-char ?- multiple-key-width)
2415 (insert " ------------\n")
2416 (while multiple-list
2417 (setq elt (car multiple-list) multiple-list (cdr multiple-list))
2419 (indent-to multiple-key-width)
2420 (if (vectorp (cdr elt))
2423 (let ((width (if (integerp x) (char-width x)
2425 (when (> (+ (current-column) 1 width) window-width)
2427 (indent-to multiple-key-width))
2430 (insert " " (cdr elt)))
2434 (defun quail-help-init ()
2435 (unless (featurep 'help-mode)
2436 (require 'help-mode)
2437 (define-button-type 'quail-keyboard-layout-button
2438 :supertype 'help-xref
2439 'help-function '(lambda (layout)
2440 (help-setup-xref `(quail-keyboard-layout-button ,layout)
2442 (quail-show-keyboard-layout layout))
2443 'help-echo (purecopy "mouse-2, RET: show keyboard layout"))
2445 (define-button-type 'quail-keyboard-customize-button
2446 :supertype 'help-customize-variable
2447 'help-echo (purecopy "mouse-2, RET: customize keyboard layout"))))
2449 (defun quail-help (&optional package)
2450 "Show brief description of the current Quail package.
2451 Optional arg PACKAGE specifies the name of alternative Quail
2452 package to describe."
2455 (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
2456 (default-enable-multibyte-characters enable-multibyte-characters)
2459 (assoc package quail-package-alist)
2460 quail-current-package)))
2461 ;; At first, make sure that the help buffer has window.
2462 (let ((temp-buffer-show-hook nil))
2463 (with-output-to-temp-buffer (help-buffer)
2464 (with-current-buffer standard-output
2465 (setq quail-current-package package-def))))
2466 ;; Then, insert text in the help buffer while paying attention to
2467 ;; the width of the window in which the buffer displayed.
2468 (with-current-buffer (help-buffer)
2469 (setq buffer-read-only nil)
2470 (insert "Input method: " (quail-name)
2471 " (mode line indicator:"
2475 (narrow-to-region (point) (point))
2476 (insert (quail-docstring))
2477 (goto-char (point-min))
2478 (with-syntax-table emacs-lisp-mode-syntax-table
2479 (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
2480 (let ((sym (intern-soft
2481 (buffer-substring (+ (match-beginning 0) 2)
2483 (if (and (boundp sym)
2484 (stringp (symbol-value sym)))
2485 (replace-match (symbol-value sym) t t)))))
2486 (goto-char (point-max)))
2491 (let ((done-list nil))
2492 ;; Show keyboard layout if the current package requests it..
2493 (when (quail-show-layout)
2497 This input method works by translating individual input characters.
2498 Assuming that your actual keyboard has the `")
2499 (help-insert-xref-button
2500 quail-keyboard-layout-type
2501 'quail-keyboard-layout-button
2502 quail-keyboard-layout-type)
2504 translation results in the following \"virtual\" keyboard layout:
2507 (quail-insert-kbd-layout quail-keyboard-layout))
2508 (insert "If your keyboard has a different layout, rearranged from
2510 (help-insert-xref-button
2512 'quail-keyboard-layout-button "standard")
2513 (insert "', the \"virtual\" keyboard you get with this input method
2514 will be rearranged in the same way.
2516 You can set the variable `quail-keyboard-layout-type' to specify
2517 the physical layout of your keyboard; the tables shown in
2518 documentation of input methods including this one are based on the
2519 physical keyboard layout as specified with that variable.
2521 (help-insert-xref-button
2522 "[customize keyboard layout]"
2523 'quail-keyboard-customize-button 'quail-keyboard-layout-type)
2526 ;; Show key sequences.
2527 (let ((decode-map (list 'decode-map))
2529 (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
2536 (if (quail-show-layout)
2537 (insert "You can also input more characters")
2538 (insert "You can input characters"))
2539 (insert " by the following key sequences:\n")
2540 (quail-insert-decode-map decode-map))))
2542 (quail-help-insert-keymap-description
2543 (quail-translation-keymap)
2545 KEY BINDINGS FOR TRANSLATION
2546 ----------------------------\n")
2548 (if (quail-conversion-keymap)
2549 (quail-help-insert-keymap-description
2550 (quail-conversion-keymap)
2552 KEY BINDINGS FOR CONVERSION
2553 ---------------------------\n"))
2554 (setq quail-current-package nil)
2555 ;; Resize the help window again, now that it has all its contents.
2556 (save-selected-window
2557 (select-window (get-buffer-window (current-buffer) t))
2558 (run-hooks 'temp-buffer-show-hook)))))
2560 (defun quail-help-insert-keymap-description (keymap &optional header)
2561 (let (pos1 pos2 eol)
2566 (insert (substitute-command-keys "\\{keymap}")))
2567 ;; Skip headers "key bindings", etc.
2570 (with-syntax-table emacs-lisp-mode-syntax-table
2571 (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
2572 (let ((sym (intern-soft (buffer-substring (match-beginning 0)
2574 (if (and sym (fboundp sym)
2575 (or (eq (get sym 'quail-help) 'hide)
2576 (and (quail-deterministic)
2577 (eq (get sym 'quail-help) 'non-deterministic))))
2578 (delete-region (line-beginning-position)
2579 (1+ (line-end-position)))))))
2582 (if (looking-at "[ \t]*$")
2583 (delete-region (point) (1+ (line-end-position)))
2587 (delete-region pos1 (point)))
2588 (goto-char (point-max))))
2590 (defun quail-translation-help ()
2591 "Show help message while translating in Quail input method."
2593 (if (not (eq this-command last-command))
2594 (let (state-msg keymap)
2595 (if (and quail-converting (= (length quail-current-key) 0))
2597 (format "Converting string %S by input method %S.\n"
2598 quail-conversion-str (quail-name))
2599 keymap (quail-conversion-keymap))
2601 (format "Translating key sequence %S by input method %S.\n"
2602 quail-current-key (quail-name))
2603 keymap (quail-translation-keymap)))
2604 (with-output-to-temp-buffer "*Help*"
2605 (with-current-buffer standard-output
2607 (quail-help-insert-keymap-description
2609 "-----------------------\n")
2610 ;; Isn't this redundant ? -stef
2613 (save-selected-window
2614 (select-window (get-buffer-window "*Help*"))
2615 (if (eq this-command last-command)
2616 (if (< (window-end) (point-max))
2618 (if (> (window-start) (point-min))
2619 (set-window-start (selected-window) (point-min)))))
2621 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2622 "Type \\[quail-translation-help] to scroll up the help"
2623 (if (> (window-start) (point-min))
2624 "Type \\[quail-translation-help] to see the head of help"))))
2627 (message "%s" (substitute-command-keys scroll-help))
2630 (quail-update-guidance)
2633 ;; Add KEY (string) to the element of TABLE (char-table) for CHAR if
2634 ;; it is not yet stored. As a result, the element is a string or a
2637 (defun quail-store-decode-map-key (table char key)
2638 (let ((elt (aref table char)))
2641 (or (member key elt)
2642 (aset table char (cons key elt)))
2643 (or (string= key elt)
2644 (aset table char (list key elt))))
2645 (aset table char key))
2646 (if (and translation-table-for-input
2647 (setq char (aref translation-table-for-input char)))
2648 (let ((translation-table-for-input nil))
2649 (quail-store-decode-map-key table char key)))))
2651 ;; Helper function for quail-gen-decode-map. Store key strings to
2652 ;; type each character under MAP in TABLE (char-table). MAP is an
2653 ;; element of the current Quail map reached by typing keys in KEY
2656 (defun quail-gen-decode-map1 (map key table)
2657 (when (and (consp map) (listp (cdr map)))
2658 (let ((trans (car map)))
2659 (cond ((integerp trans)
2660 (quail-store-decode-map-key table trans key))
2662 (dotimes (i (length trans))
2663 (quail-store-decode-map-key table (aref trans i) key)))
2664 ((or (vectorp trans)
2666 (setq trans (cdr trans))))
2667 (dotimes (i (length trans))
2668 (let ((elt (aref trans i)))
2670 (if (= (length elt) 1)
2671 (quail-store-decode-map-key table (aref elt 0) key))
2672 (quail-store-decode-map-key table elt key)))))))
2673 (if (> (length key) 1)
2674 (dolist (elt (cdr map))
2675 (quail-gen-decode-map1 (cdr elt) key table))
2676 (dolist (elt (cdr map))
2677 (quail-gen-decode-map1 (cdr elt) (format "%s%c" key (car elt))
2680 (put 'quail-decode-map 'char-table-extra-slots 0)
2682 ;; Generate a halfly-cooked decode map (char-table) for the current
2683 ;; Quail map. An element for a character C is a key string or a list
2684 ;; of a key strings to type to input C. The lenth of key string is at
2685 ;; most 2. If it is 2, more keys may be required to input C.
2687 (defun quail-gen-decode-map ()
2688 (let ((table (make-char-table 'quail-decode-map nil)))
2689 (dolist (elt (cdr (quail-map)))
2690 (quail-gen-decode-map1 (cdr elt) (string (car elt)) table))
2693 ;; Check if CHAR equals to TARGET while also trying to translate CHAR
2694 ;; by translation-table-for-input.
2696 (defsubst quail-char-equal-p (char target)
2698 (and translation-table-for-input
2699 (setq char (aref translation-table-for-input char))
2702 ;; Helper function for quail-find-key. Prepend key strings to type
2703 ;; for inputting CHAR by the current input method to KEY-LIST and
2704 ;; return the result. MAP is an element of the current Quail map
2705 ;; reached by typing keys in KEY.
2707 (defun quail-find-key1 (map key char key-list)
2708 (let ((trans (car map))
2710 (cond ((stringp trans)
2712 (and (= (length trans) 1)
2713 (quail-char-equal-p (aref trans 0) char))))
2714 ((or (vectorp trans) (consp trans))
2716 (setq trans (cdr trans)))
2719 (dotimes (i (length trans))
2720 (let ((target (aref trans i)))
2721 (if (integerp target)
2722 (if (quail-char-equal-p target char)
2724 (if (and (= (length target) 1)
2725 (quail-char-equal-p (aref target 0) char))
2726 (throw 'tag t))))))))
2728 (setq found-here (quail-char-equal-p trans char))))
2730 (setq key-list (cons key key-list)))
2731 (if (> (length key) 1)
2732 (dolist (elt (cdr map))
2734 (quail-find-key1 (cdr elt) (format "%s%c" key (car elt))
2738 ;; If non-nil, the value has the form (QUAIL-MAP . CODING-SYSTEM)
2739 ;; where QUAIL-MAP is a quail-map of which decode map was generated
2740 ;; while buffer-file-coding-system was CODING-SYSTEM.
2742 (defvar quail-decode-map-generated nil)
2744 (defun quail-find-key (char)
2745 "Return a list of keys to type to input CHAR in the current input method.
2746 If CHAR is an ASCII character and can be input by typing itself, return t."
2747 (let ((decode-map (or (and (or (not quail-decode-map-generated)
2748 (and (eq (car quail-decode-map-generated) (quail-map))
2749 (eq (cdr quail-decode-map-generated)
2750 (or buffer-file-coding-system t))))
2752 (let ((map (quail-gen-decode-map)))
2753 (setq quail-decode-map-generated
2754 (cons (quail-map) (or buffer-file-coding-system t)))
2755 (setcar (nthcdr 10 quail-current-package) map)
2758 (if (consp decode-map)
2759 (let ((str (string char)))
2760 (mapc #'(lambda (elt)
2761 (if (string= str (car elt))
2762 (setq key-list (cons (cdr elt) key-list))))
2764 (let ((key-head (aref decode-map char)))
2765 (if (stringp key-head)
2766 (setq key-list (quail-find-key1
2767 (quail-lookup-key key-head nil t)
2769 (mapc #'(lambda (elt)
2772 (quail-lookup-key elt nil t) elt char key-list)))
2776 (not (quail-lookup-key (string char) 1))))))
2778 (defun quail-show-key ()
2779 "Show a list of key strings to type for inputting a character at point."
2781 (or current-input-method
2782 (error "No input method is activated"))
2783 (or (assoc current-input-method quail-package-alist)
2784 (error "The current input method does not use Quail"))
2785 (let* ((char (following-char))
2786 (key-list (quail-find-key char)))
2787 (cond ((consp key-list)
2788 (message "To input `%c', type \"%s\""
2790 (mapconcat 'identity key-list "\", \"")))
2792 (message "To input `%s', just type it"
2793 (single-key-description char)))
2795 (message "%c can't be input by the current input method" char)))))
2798 ;; Quail map generator from state transition table.
2800 (defun quail-map-from-table (table)
2801 "Make quail map from state transition table TABLE.
2803 TABLE is an alist, the form is:
2804 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2806 STATE-n are symbols to denote state. STATE-0 is the initial state.
2808 TRANSITION-n-m are transition rules from STATE-n, and have the form
2809 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2810 RULES is a symbol whose value is an alist of keys \(string) vs the
2811 correponding characters or strings. The format of the symbol value of
2812 RULES is the same as arguments to `quail-define-rules'.
2814 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2815 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2816 transition is allowed, i.e. STATE-x may be STATE-n.
2818 If TRANSITION-n-m has the form RULES, the transition terminates
2819 when keys in RULES are input.
2821 The generated map can be set for the current Quail package by the
2822 function `quail-install-map' (which see)."
2823 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2825 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2826 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2827 ;; Set key sequence mapping rules in cdr part of each element.
2829 (quail-map-from-table-1 state-alist (car table))
2830 (setq table (cdr table)))
2832 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2833 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2834 ;; them with MAPPING-RULES of STATE-x to make elements of
2835 ;; STATE-ALIST valid Quail maps.
2836 (setq tail state-alist)
2838 (setq elt (car tail) tail (cdr tail))
2839 (quail-map-from-table-2 state-alist elt))
2841 ;; Return the Quail map for the initial state.
2844 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2845 ;; mapping rules in the element of STATE-ALIST that corresponds to
2846 ;; STATE according to TRANSITION ...
2847 (defun quail-map-from-table-1 (state-alist state-info)
2848 (let* ((state (car state-info))
2849 (map (assq state state-alist))
2850 (transitions (cdr state-info))
2853 (setq elt (car transitions) transitions (cdr transitions))
2854 (let (rules dst-state key trans)
2855 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2856 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2858 (setq rules (symbol-value (car elt))
2859 ;; Set (STATE-x) as branches for all keys in RULES.
2860 ;; It is replaced with actual branches for STATE-x
2861 ;; later in `quail-map-from-table-2'.
2862 dst-state (list (cdr elt)))
2863 (setq rules (symbol-value elt)))
2865 (setq key (car (car rules)) trans (cdr (car rules))
2868 (if (= (length trans) 1)
2869 (setq trans (aref trans 0))
2870 (setq trans (string-to-vector trans))))
2871 (set-nested-alist key trans map nil dst-state))))))
2873 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2875 ;; (STATE (CHAR NESTED-ALIST) ...)
2876 ;; NESTED-ALIST is a nested alist; the form is:
2877 ;; (TRANS (CHAR NESTED-ALIST) ...)
2879 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2880 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2881 ;; (cdr (assq STATE-x STATE-ALIST))
2883 (defun quail-map-from-table-2 (state-alist element)
2884 (let ((prev element)
2885 (tail (cdr element))
2888 (setq elt (car tail) prev tail tail (cdr tail))
2889 (quail-map-from-table-2 state-alist (cdr elt)))
2890 (setq elt (car tail))
2892 (quail-map-from-table-2 state-alist (cdr elt))
2893 (setcdr prev (cdr (assq elt state-alist))))))
2895 ;; Concatenate translations for all heading substrings of KEY in the
2896 ;; current Quail map. Here, `heading substring' means (substring KEY
2897 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2898 (defun quail-lookup-map-and-concat (key)
2899 (let* ((len (length key))
2900 (translation-list nil)
2903 (setq map (quail-lookup-key key len t)
2906 (let* ((def (quail-map-definition map))
2907 (trans (if (consp def) (aref (cdr def) (car (car def)))
2909 (if (integerp trans)
2910 (setq trans (char-to-string trans)))
2911 (setq translation-list (cons trans translation-list)))))
2912 (apply 'concat translation-list)))
2915 (defvar quail-directory-name "quail"
2916 "Name of Quail directory which contains Quail packages.
2917 This is a sub-directory of LEIM directory.")
2920 (defun quail-update-leim-list-file (dirname &rest dirnames)
2921 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2922 DIRNAME is a directory containing Emacs input methods;
2923 normally, it should specify the `leim' subdirectory
2924 of the Emacs source tree.
2926 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2927 and update the file \"leim-list.el\" in DIRNAME.
2929 When called from a program, the remaining arguments are additional
2930 directory names to search for Quail packages under `quail' subdirectory
2932 (interactive "FDirectory of LEIM: ")
2933 (setq dirname (expand-file-name dirname))
2934 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2935 quail-dirs list-buf pkg-list pkg-buf pos)
2936 (if (not (file-writable-p leim-list))
2937 (error "Can't write to file \"%s\"" leim-list))
2938 (message "Updating %s ..." leim-list)
2939 (setq list-buf (find-file-noselect leim-list))
2941 ;; At first, clean up the file.
2942 (with-current-buffer list-buf
2945 ;; Insert the correct header.
2946 (if (looking-at (regexp-quote leim-list-header))
2947 (goto-char (match-end 0))
2948 (insert leim-list-header))
2950 (if (not (re-search-forward leim-list-entry-regexp nil t))
2953 ;; Remove garbages after the header.
2954 (goto-char (match-beginning 0))
2956 (delete-region pos (point)))
2958 ;; Remove all entries for Quail.
2959 (while (re-search-forward leim-list-entry-regexp nil 'move)
2960 (goto-char (match-beginning 0))
2963 (let ((form (read list-buf)))
2964 (when (equal (nth 3 form) ''quail-use-package)
2965 (if (eolp) (forward-line 1))
2966 (delete-region pos (point))))
2968 ;; Delete the remaining contents because it seems that
2969 ;; this file is broken.
2970 (message "Garbage in %s deleted" leim-list)
2971 (delete-region pos (point-max)))))))
2973 ;; Search for `quail' subdirectory under each DIRNAMES.
2974 (setq dirnames (cons dirname dirnames))
2977 (setcar l (expand-file-name (car l)))
2978 (setq dirname (expand-file-name quail-directory-name (car l)))
2979 (if (file-readable-p dirname)
2980 (setq quail-dirs (cons dirname quail-dirs))
2981 (message "%s doesn't have `%s' subdirectory, just ignored"
2982 (car l) quail-directory-name)
2983 (setq quail-dirs (cons nil quail-dirs)))
2985 (setq quail-dirs (nreverse quail-dirs)))
2987 ;; Insert input method registering forms.
2989 (setq dirname (car quail-dirs))
2991 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2993 (message "Checking %s ..." (car pkg-list))
2995 (insert-file-contents (car pkg-list))
2996 (goto-char (point-min))
2997 ;; Don't get fooled by commented-out code.
2998 (while (re-search-forward "^[ \t]*(quail-define-package" nil t)
2999 (goto-char (match-beginning 0))
3001 (let ((form (read (current-buffer))))
3002 (with-current-buffer list-buf
3004 (format "(register-input-method
3008 (nth 1 form) ; PACKAGE-NAME
3009 (nth 2 form) ; LANGUAGE
3010 'quail-use-package ; ACTIVATE-FUNC
3011 (nth 3 form) ; PACKAGE-TITLE
3012 (progn ; PACKAGE-DESCRIPTION (one line)
3013 (string-match ".*" (nth 5 form))
3014 (match-string 0 (nth 5 form)))
3015 (file-relative-name ; PACKAGE-FILENAME
3016 (file-name-sans-extension (car pkg-list))
3019 ;; Ignore the remaining contents of this file.
3020 (goto-char (point-max))
3021 (message "Some part of \"%s\" is broken" (car pkg-list))))))
3022 (setq pkg-list (cdr pkg-list)))
3023 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
3025 ;; At last, write out LEIM list file.
3026 (with-current-buffer list-buf
3027 (setq buffer-file-coding-system 'iso-2022-7bit)
3029 (kill-buffer list-buf)
3030 (message "Updating %s ... done" leim-list)))
3032 (defun quail-advice (args)
3033 "Advise users about the characters input by the current Quail package.
3034 The argument is a parameterized event of the form:
3035 (quail-advice STRING)
3036 where STRING is a string containing the input characters.
3037 If STRING has property `advice' and the value is a function,
3038 call it with one argument STRING."
3040 (let* ((string (nth 1 args))
3041 (func (get-text-property 0 'advice string)))
3042 (if (functionp func)
3043 (funcall func string))))
3045 (global-set-key [quail-advice] 'quail-advice)
3050 ;;; arch-tag: 46d7db54-5467-42c4-a2a9-53ca90a1e886
3051 ;;; quail.el ends here