1 ;;; quail.el --- Provides simple input method for multilingual text
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Author: Kenichi HANDA <handa@etl.go.jp>
7 ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
8 ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
9 ;; Keywords: mule, multilingual, input method
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; In Quail minor mode, you can input multilingual text easily. By
31 ;; defining a translation table (named Quail map) which maps ASCII key
32 ;; string to multilingual character or string, you can input any text
33 ;; from ASCII keyboard.
35 ;; We use words "translation" and "conversion" differently. The
36 ;; former is done by Quail package itself, the latter is the further
37 ;; process of converting a translated text to some more desirable
38 ;; text. For instance, Quail package for Japanese (`quail-jp')
39 ;; translates Roman text (transliteration of Japanese in Latin
40 ;; alphabets) to Hiragana text, which is then converted to
41 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
42 ;; CONVERSION-KEYS argument of the Quail package.
48 ;; Buffer local variables
50 (defvar quail-current-package nil
51 "The current Quail package, which depends on the current input method.
52 See the documentation of `quail-package-alist' for the format.")
53 (make-variable-buffer-local 'quail-current-package)
54 (put 'quail-current-package 'permanent-local t)
56 ;; Quail uses the following two buffers to assist users.
57 ;; A buffer to show available key sequence or translation list.
58 (defvar quail-guidance-buf nil)
59 ;; A buffer to show completion list of the current key sequence.
60 (defvar quail-completion-buf nil)
62 ;; Each buffer in which Quail is activated should use different
64 (make-variable-buffer-local 'quail-guidance-buf)
65 (put 'quail-guidance-buf 'permanent-local t)
67 ;; A main window showing Quail guidance buffer.
68 (defvar quail-guidance-win nil)
69 (make-variable-buffer-local 'quail-guidance-win)
71 (defvar quail-overlay nil
72 "Overlay which covers the current translation region of Quail.")
73 (make-variable-buffer-local 'quail-overlay)
75 (defvar quail-conv-overlay nil
76 "Overlay which covers the text to be converted in Quail mode.")
77 (make-variable-buffer-local 'quail-conv-overlay)
79 (defvar quail-current-key nil
80 "Current key for translation in Quail mode.")
81 (make-variable-buffer-local 'quail-current-key)
83 (defvar quail-current-str nil
84 "Currently selected translation of the current key.")
85 (make-variable-buffer-local 'quail-current-str)
87 (defvar quail-current-translations nil
88 "Cons of indices and vector of possible translations of the current key.
89 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
90 CURRENT is an index of the current translation,
91 START and END are indices of the start and end of the current block,
92 BLOCK is the current block index,
93 BLOCKS is a number of blocks of translation.")
94 (make-variable-buffer-local 'quail-current-translations)
96 (defvar quail-current-data nil
97 "Any Lisp object holding information of current translation status.
98 When a key sequence is mapped to TRANS and TRANS is a cons
99 of actual translation and some Lisp object to be referred
100 for translating the longer key sequence, this variable is set
101 to that Lisp object.")
102 (make-variable-buffer-local 'quail-current-data)
104 ;; Quail package handlers.
106 (defvar quail-package-alist nil
107 "List of Quail packages.
108 A Quail package is a list of these elements:
109 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
110 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
111 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
112 CONVERSION-KEYS, SIMPLE.
114 QUAIL-MAP is a data structure to map key strings to translations. For
115 the format, see the documentation of `quail-map-p'.
117 DECODE-MAP is an alist of translations and corresponding keys.
119 See the documentation of `quail-define-package' for the other elements.")
121 ;; Return various slots in the current quail-package.
123 (defsubst quail-name ()
124 "Return the name of the current Quail package."
125 (nth 0 quail-current-package))
126 (defsubst quail-title ()
127 "Return the title of the current Quail package."
128 (nth 1 quail-current-package))
129 (defsubst quail-map ()
130 "Return the translation map of the current Quail package."
131 (nth 2 quail-current-package))
132 (defsubst quail-guidance ()
133 "Return an object used for `guidance' feature of the current Quail package.
134 See also the documentation of `quail-define-package'."
135 (nth 3 quail-current-package))
136 (defsubst quail-docstring ()
137 "Return the documentation string of the current Quail package."
138 (nth 4 quail-current-package))
139 (defsubst quail-translation-keymap ()
140 "Return translation keymap in the current Quail package.
141 Translation keymap is a keymap used while translation region is active."
142 (nth 5 quail-current-package))
143 (defsubst quail-forget-last-selection ()
144 "Return `forget-last-selection' flag of the current Quail package.
145 See also the documentation of `quail-define-package'."
146 (nth 6 quail-current-package))
147 (defsubst quail-deterministic ()
148 "Return `deterministic' flag of the current Quail package.
149 See also the documentation of `quail-define-package'."
150 (nth 7 quail-current-package))
151 (defsubst quail-kbd-translate ()
152 "Return `kbd-translate' flag of the current Quail package.
153 See also the documentation of `quail-define-package'."
154 (nth 8 quail-current-package))
155 (defsubst quail-show-layout ()
156 "Return `show-layout' flag of the current Quail package.
157 See also the documentation of `quail-define-package'."
158 (nth 9 quail-current-package))
159 (defsubst quail-decode-map ()
160 "Return decode map of the current Quail package.
161 It is an alist of translations and corresponding keys."
162 (nth 10 quail-current-package))
163 (defsubst quail-maximum-shortest ()
164 "Return `maximum-shortest' flag of the current Quail package.
165 See also the documentation of `quail-define-package'."
166 (nth 11 quail-current-package))
167 (defsubst quail-overlay-plist ()
168 "Return property list of an overly used in the current Quail package."
169 (nth 12 quail-current-package))
170 (defsubst quail-update-translation-function ()
171 "Return a function for updating translation in the current Quail package."
172 (nth 13 quail-current-package))
173 (defsubst quail-conversion-keymap ()
174 "Return conversion keymap in the current Quail package.
175 Conversion keymap is a keymap used while conversion region is active
176 but translation region is not active."
177 (nth 14 quail-current-package))
178 (defsubst quail-simple ()
179 "Return t if the current Quail package is simple."
180 (nth 15 quail-current-package))
182 (defsubst quail-package (name)
183 "Return Quail package named NAME."
184 (assoc name quail-package-alist))
186 (defun quail-add-package (package)
187 "Add Quail package PACKAGE to `quail-package-alist'."
188 (let ((pac (quail-package (car package))))
190 (setcdr pac (cdr package))
191 (setq quail-package-alist (cons package quail-package-alist)))))
193 (defun quail-select-package (name)
194 "Select Quail package named NAME as the current Quail package."
195 (let ((package (quail-package name)))
197 (error "No Quail package `%s'" name))
198 (setq quail-current-package package)
199 (setq-default quail-current-package package)
203 (defun quail-use-package (package-name &rest libraries)
204 "Start using Quail package PACKAGE-NAME.
205 The remaining arguments are libraries to be loaded before using the package."
206 (let ((package (quail-package package-name)))
208 ;; Perhaps we have not yet loaded necessary libraries.
210 (if (not (load (car libraries) t))
212 (with-output-to-temp-buffer "*Help*"
213 (princ "Quail package \"")
215 (princ "\" can't be activated\n because library \"")
216 (princ (car libraries))
217 (princ "\" is not in `load-path'.
219 The most common case is that you have not yet installed appropriate
220 libraries in LEIM (Libraries of Emacs Input Method) which is
221 distributed separately from Emacs.
223 LEIM is available from the same ftp directory as Emacs."))
224 (error "Can't use the Quail package `%s'" package-name))
225 (setq libraries (cdr libraries))))))
226 (quail-select-package package-name)
227 (setq current-input-method-title (quail-title))
229 ;; Hide all '... loaded' message.
232 (defvar quail-translation-keymap
233 (let ((map (make-keymap))
236 (define-key map (char-to-string i) 'quail-other-command)
239 (define-key map (char-to-string i) 'quail-self-insert-command)
243 (define-key map (vector i) 'quail-self-insert-command)
245 (define-key map "\177" 'quail-delete-last-char)
246 (define-key map "\C-f" 'quail-next-translation)
247 (define-key map "\C-b" 'quail-prev-translation)
248 (define-key map "\C-n" 'quail-next-translation-block)
249 (define-key map "\C-p" 'quail-prev-translation-block)
250 (define-key map [right] 'quail-next-translation)
251 (define-key map [left] 'quail-prev-translation)
252 (define-key map [down] 'quail-next-translation-block)
253 (define-key map [up] 'quail-prev-translation-block)
254 (define-key map "\C-i" 'quail-completion)
255 (define-key map "\C-@" 'quail-select-current)
256 ;; Following simple.el, Enter key on numeric keypad selects the
257 ;; current translation just like `C-SPC', and `mouse-2' chooses
258 ;; any completion visible in the *Quail Completions* buffer.
259 (define-key map [kp-enter] 'quail-select-current)
260 (define-key map [mouse-2] 'quail-mouse-choose-completion)
261 (define-key map [down-mouse-2] nil)
262 (define-key map "\C-h" 'quail-translation-help)
263 (define-key map [?\C- ] 'quail-select-current)
264 (define-key map [tab] 'quail-completion)
265 (define-key map [delete] 'quail-delete-last-char)
266 (define-key map [backspace] 'quail-delete-last-char)
268 "Keymap used processing translation in complex Quail modes.
269 Only a few especially complex input methods use this map;
270 most use `quail-simple-translation-keymap' instead.
271 This map is activated while translation region is active.")
273 (defvar quail-simple-translation-keymap
274 (let ((map (make-keymap))
277 (define-key map (char-to-string i) 'quail-other-command)
280 (define-key map (char-to-string i) 'quail-self-insert-command)
284 (define-key map (vector i) 'quail-self-insert-command)
286 (define-key map "\177" 'quail-delete-last-char)
287 (define-key map [delete] 'quail-delete-last-char)
288 (define-key map [backspace] 'quail-delete-last-char)
289 ;;(let ((meta-map (make-sparse-keymap)))
290 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
291 ;;(define-key map [escape] meta-map))
293 "Keymap used while processing translation in simple Quail modes.
294 A few especially complex input methods use `quail-translation-keymap' instead.
295 This map is activated while translation region is active.")
297 (defvar quail-conversion-keymap
298 (let ((map (make-keymap))
301 (define-key map (char-to-string i) 'quail-self-insert-command)
305 (define-key map (vector i) 'quail-self-insert-command)
307 (define-key map "\C-b" 'quail-conversion-backward-char)
308 (define-key map "\C-f" 'quail-conversion-forward-char)
309 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
310 (define-key map "\C-e" 'quail-conversion-end-of-region)
311 (define-key map "\C-d" 'quail-conversion-delete-char)
312 (define-key map "\C-k" 'quail-conversion-delete-tail)
313 (define-key map "\C-h" 'quail-translation-help)
314 (define-key map "\177" 'quail-conversion-backward-delete-char)
315 (define-key map [delete] 'quail-conversion-backward-delete-char)
316 (define-key map [backspace] 'quail-conversion-backward-delete-char)
318 "Keymap used for processing conversion in Quail mode.
319 This map is activated while conversion region is active but translation
320 region is not active.")
323 (defun quail-define-package (name language title
324 &optional guidance docstring translation-keys
325 forget-last-selection deterministic
326 kbd-translate show-layout create-decode-map
327 maximum-shortest overlay-plist
328 update-translation-function
329 conversion-keys simple)
330 "Define NAME as a new Quail package for input LANGUAGE.
331 TITLE is a string to be displayed at mode-line to indicate this package.
332 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
333 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
334 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
335 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
337 GUIDANCE specifies how a guidance string is shown in echo area.
338 If it is t, list of all possible translations for the current key is shown
339 with the currently selected translation being highlighted.
340 If it is an alist, the element has the form (CHAR . STRING). Each character
341 in the current key is searched in the list and the corresponding string is
343 If it is nil, the current key is shown.
345 DOCSTRING is the documentation string of this package.
347 TRANSLATION-KEYS specifies additional key bindings used while translation
348 region is active. It is an alist of single key character vs. corresponding
349 command to be called.
351 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
352 for the future to translate the same key. If this flag is nil, a
353 translation selected for a key is remembered so that it can be the
354 first candidate when the same key is entered later.
356 DETERMINISTIC non-nil means the first candidate of translation is
357 selected automatically without allowing users to select another
358 translation for a key. In this case, unselected translations are of
359 no use for an interactive use of Quail but can be used by some other
360 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
363 KBD-TRANSLATE non-nil means input characters are translated from a
364 user's keyboard layout to the standard keyboard layout. See the
365 documentation of `quail-keyboard-layout' and
366 `quail-keyboard-layout-standard' for more detail.
368 SHOW-LAYOUT non-nil means the `quail-help' command should show
369 the user's keyboard layout visually with translated characters.
370 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
371 this package defines no translations for single character keys.
373 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
374 map is an alist of translations and corresponding original keys.
375 Although this map is not used by Quail itself, it can be used by some
376 other programs. For instance, Vietnamese supporting needs this map to
377 convert Vietnamese text to VIQR format which uses only ASCII
378 characters to represent Vietnamese characters.
380 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
381 length of the shortest sequence. When we don't have a translation of
382 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
383 the key at \"..AB\" and start translation of \"CD..\". Hangul
384 packages, for instance, use this facility. If this flag is nil, we
385 break the key just at \"..ABC\" and start translation of \"D..\".
387 OVERLAY-PLIST if non-nil is a property list put on an overlay which
388 covers Quail translation region.
390 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
391 the current translation region according to a new translation data. By
392 default, a translated text or a user's key sequence (if no translation
395 CONVERSION-KEYS specifies additional key bindings used while
396 conversion region is active. It is an alist of single key character
397 vs. corresponding command to be called.
399 If SIMPLE is non-nil, then we do not alter the meanings of
400 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
402 (let (translation-keymap conversion-keymap)
403 (if deterministic (setq forget-last-selection t))
406 (setq translation-keymap (copy-keymap
407 (if simple quail-simple-translation-keymap
408 quail-translation-keymap)))
409 (while translation-keys
410 (define-key translation-keymap
411 (car (car translation-keys)) (cdr (car translation-keys)))
412 (setq translation-keys (cdr translation-keys))))
413 (setq translation-keymap
414 (if simple quail-simple-translation-keymap
415 quail-translation-keymap)))
416 (when conversion-keys
417 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
418 (while conversion-keys
419 (define-key conversion-keymap
420 (car (car conversion-keys)) (cdr (car conversion-keys)))
421 (setq conversion-keys (cdr conversion-keys))))
423 (list name title (list nil) guidance (or docstring "")
425 forget-last-selection deterministic kbd-translate show-layout
426 (if create-decode-map (list 'decode-map) nil)
427 maximum-shortest overlay-plist update-translation-function
428 conversion-keymap simple))
430 ;; Update input-method-alist.
431 (let ((slot (assoc name input-method-alist))
432 (val (list language 'quail-use-package title docstring)))
433 (if slot (setcdr slot val)
434 (setq input-method-alist (cons (cons name val) input-method-alist)))))
436 (quail-select-package name))
438 ;; Quail minor mode handlers.
440 ;; Setup overlays used in Quail mode.
441 (defun quail-setup-overlays (conversion-mode)
443 (if (overlayp quail-overlay)
444 (move-overlay quail-overlay pos pos)
445 (setq quail-overlay (make-overlay pos pos nil nil t))
446 (if input-method-highlight-flag
447 (overlay-put quail-overlay 'face 'underline))
448 (let ((l (quail-overlay-plist)))
450 (overlay-put quail-overlay (car l) (car (cdr l)))
451 (setq l (cdr (cdr l))))))
453 (if (overlayp quail-conv-overlay)
454 (if (not (overlay-start quail-conv-overlay))
455 (move-overlay quail-conv-overlay pos pos))
456 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
457 (if input-method-highlight-flag
458 (overlay-put quail-conv-overlay 'face 'underline))))))
460 ;; Delete overlays used in Quail mode.
461 (defun quail-delete-overlays ()
462 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
463 (delete-overlay quail-overlay))
464 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
465 (delete-overlay quail-conv-overlay)))
467 ;; Kill Quail guidance buffer. Set in kill-buffer-hook.
468 (defun quail-kill-guidance-buf ()
469 (if (buffer-live-p quail-guidance-buf)
470 (kill-buffer quail-guidance-buf)))
472 (defun quail-inactivate ()
473 "Inactivate Quail input method."
477 (defun quail-activate (&optional arg)
478 "Activate Quail input method.
479 With arg, activate Quail input method if and only if arg is positive.
481 While this input method is active, the variable
482 `input-method-function' is bound to the function `quail-input-method'."
484 (< (prefix-numeric-value arg) 0))
485 ;; Let's inactivate Quail input method.
488 (quail-hide-guidance-buf)
489 (quail-delete-overlays)
490 (setq describe-current-input-method-function nil)
491 (run-hooks 'quail-inactivate-hook))
492 (kill-local-variable 'input-method-function))
493 ;; Let's active Quail input method.
494 (if (null quail-current-package)
495 ;; Quail package is not yet selected. Select one now.
497 (if quail-package-alist
498 (setq name (car (car quail-package-alist)))
499 (error "No Quail package loaded"))
500 (quail-select-package name)))
501 (setq inactivate-current-input-method-function 'quail-inactivate)
502 (setq describe-current-input-method-function 'quail-help)
503 (quail-delete-overlays)
504 (quail-show-guidance-buf)
505 ;; If we are in minibuffer, turn off the current input method
507 (if (eq (selected-window) (minibuffer-window))
508 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
509 (make-local-hook 'kill-buffer-hook)
510 (add-hook 'kill-buffer-hook 'quail-kill-guidance-buf nil t)
511 (run-hooks 'quail-activate-hook)
512 (make-local-variable 'input-method-function)
513 (setq input-method-function 'quail-input-method)))
515 (defun quail-exit-from-minibuffer ()
516 (inactivate-input-method)
517 (if (<= (minibuffer-depth) 1)
518 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
520 ;; Keyboard layout translation handlers.
522 ;; Some Quail packages provide localized keyboard simulation which
523 ;; requires a particular keyboard layout. In this case, what we need
524 ;; is locations of keys the user entered, not character codes
525 ;; generated by those keys. However, for the moment, there's no
526 ;; common way to get such information. So, we ask a user to give
527 ;; information of his own keyboard layout, then translate it to the
528 ;; standard layout which we defined so that all Quail packages depend
531 (defconst quail-keyboard-layout-standard
534 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
535 qQwWeErRtTyYuUiIoOpP[{]} \
536 aAsSdDfFgGhHjJkKlL;:'\"\\| \
537 zZxXcCvVbBnNmM,<.>/? \
539 "Standard keyboard layout of printable characters Quail assumes.
540 See the documentation of `quail-keyboard-layout' for this format.
541 This layout is almost the same as that of VT100,
542 but the location of key \\ (backslash) is just right of key ' (single-quote),
543 not right of RETURN key.")
545 (defvar quail-keyboard-layout quail-keyboard-layout-standard
546 "A string which represents physical key layout of a particular keyboard.
547 We assume there are six rows and each row has 15 keys (columns),
548 the first row is above the `1' - `0' row,
549 the first column of the second row is left of key `1',
550 the first column of the third row is left of key `q',
551 the first column of the fourth row is left of key `a',
552 the first column of the fifth row is left of key `z',
553 the sixth row is below the `z' - `/' row.
554 Nth (N is even) and (N+1)th characters in the string are non-shifted
555 and shifted characters respectively at the same location.
556 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
557 The command `quail-set-keyboard-layout' usually sets this variable.")
559 (defconst quail-keyboard-layout-len 180)
561 ;; Here we provide several examples of famous keyboard layouts.
563 (defvar quail-keyboard-layout-alist
567 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
568 qQwWeErRtTyYuUiIoOpP[{]} \
569 aAsSdDfFgGhHjJkKlL;:'\" \
570 zZxXcCvVbBnNmM,<.>/? \
572 '("atari-german" . "\
574 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
575 qQwWeErRtTzZuUiIoOpP\374\334+* \
576 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
577 <>yYxXcCvVbBnNmM,;.:-_ \
582 ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
583 qQwWeErRtTzZuUiIoOpP\374\334+* \
584 aAsSdDfFgGhHjJkKlL\366\326\344\304 \
585 <>yYxXcCvVbBnNmM,;.:-_ \
588 (cons "standard" quail-keyboard-layout-standard))
589 "Alist of keyboard names and corresponding layout strings.
590 See the documentation of `quail-keyboard-layout' for the format of
594 (defun quail-set-keyboard-layout (kbd-type)
595 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
597 Since some Quail packages depends on a physical layout of keys (not
598 characters generated by them), those are created by assuming the
599 standard layout defined in `quail-keyboard-layout-standard'. This
600 function tells Quail system the layout of your keyboard so that what
601 you type is correctly handled."
603 (let* ((completion-ignore-case t)
604 (type (completing-read "Keyboard type: "
605 quail-keyboard-layout-alist)))
607 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
609 ;; Here, we had better ask a user to define his own keyboard
610 ;; layout interactively.
611 (error "Unknown keyboard type `%s'" kbd-type))
612 (setq quail-keyboard-layout (cdr layout))))
614 (defun quail-keyboard-translate (ch)
615 "Translate CHAR according to `quail-keyboard-layout' and return the result."
616 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
617 ;; All Quail packages are designed based on
618 ;; `quail-keyboard-layout-standard'.
621 (while (and (< i quail-keyboard-layout-len)
622 (/= ch (aref quail-keyboard-layout i)))
624 (if (= i quail-keyboard-layout-len)
625 ;; CH is not in quail-keyboard-layout, which means that a
626 ;; user typed a key which generated a character code to be
627 ;; handled out of Quail. Just return CH and make
628 ;; quail-execute-non-quail-command handle it correctly.
630 (let ((char (aref quail-keyboard-layout-standard i)))
632 ;; A user typed a key at the location not converted by
633 ;; quail-keyboard-layout-standard. Just return CH as
640 (defsubst quail-map-p (object)
641 "Return t if OBJECT is a Quail map.
643 A Quail map holds information how a particular key should be translated.
644 Its format is (TRANSLATION . ALIST).
645 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
646 In the latter case, each element of VECTOR is a candidate for the translation,
647 and INDEX points the currently selected translation.
649 ALIST is normally a list of elements that look like (CHAR . DEFN),
650 where DEFN is another Quail map for a longer key (CHAR added to the
651 current key). It may also be a symbol of a function which returns an
652 alist of the above format.
654 Just after a Quail package is read, TRANSLATION may be a string or a
655 vector. Then each element of the string or vector is a candidate for
656 the translation. These objects are transformed to cons cells in the
657 format \(INDEX . VECTOR), as described above."
659 (let ((translation (car object)))
660 (or (integerp translation) (null translation)
661 (vectorp translation) (stringp translation)
662 (symbolp translation)
663 (and (consp translation) (not (vectorp (cdr translation))))))
664 (let ((alist (cdr object)))
665 (or (and (listp alist) (consp (car alist)))
669 (defmacro quail-define-rules (&rest rules)
670 "Define translation rules of the current Quail package.
671 Each argument is a list of KEY and TRANSLATION.
672 KEY is a string meaning a sequence of keystrokes to be translated.
673 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
674 If it is a character, it is the sole translation of KEY.
675 If it is a string, each character is a candidate for the translation.
676 If it is a vector, each element (string or character) is a candidate
678 In these cases, a key specific Quail map is generated and assigned to KEY.
680 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
681 it is used to handle KEY.
683 The first argument may be an alist of annotations for the following
684 rules. Each element has the form (ANNOTATION . VALUE), where
685 ANNOTATION is a symbol indicating the annotation type. Currently
686 the following annotation types are supported.
688 append -- the value non-nil means that the following rules should
689 be appended to the rules of the current Quail package.
691 face -- the value is a face to use for displaying TRANSLATIONs in
694 advice -- the value is a function to call after one of RULES is
695 selected. The function is called with one argument, the
696 selected TRANSLATION string, after the TRANSLATION is
699 no-decode-map --- the value non-nil means that decoding map is not
700 generated for the following translations."
702 append no-decode-map props)
703 ;; If the first argument is an alist of annotations, handle them.
704 (if (consp (car (car l)))
705 (let ((annotations (car l)))
706 (setq append (assq 'append annotations))
708 (setq annotations (delete append annotations)
709 append (cdr append)))
710 (setq no-decode-map (assq 'no-decode-map annotations))
712 (setq annotations (delete no-decode-map annotations)
713 no-decode-map (cdr no-decode-map)))
714 ;; Convert the remaining annoations to property list PROPS.
717 (cons (car (car annotations))
718 (cons (cdr (car annotations))
720 annotations (cdr annotations)))
722 ;; Process the remaining arguments one by one.
724 ;; There's no way to add new rules at compiling time.
727 (decode-map (and (quail-decode-map) (not ,no-decode-map)))
731 (setq key (car (car tail)) trans (car (cdr (car tail)))
733 (quail-defrule-internal key trans map t decode-map properties)))
734 ;; We can build up quail map and decode map at compiling time.
735 (let ((map (list nil))
736 (decode-map (if (not no-decode-map) (list 'decode-map)))
739 (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
740 (quail-defrule-internal key trans map t decode-map props))
741 `(if (not (quail-decode-map))
742 (quail-install-map ',map)
743 (quail-install-map ',map)
744 (quail-install-decode-map ',decode-map))))))
747 (defun quail-install-map (map &optional name)
748 "Install the Quail map MAP in the current Quail package.
750 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
751 which to install MAP.
753 The installed map can be referred by the function `quail-map'."
754 (if (null quail-current-package)
755 (error "No current Quail package"))
756 (if (null (quail-map-p map))
757 (error "Invalid Quail map `%s'" map))
758 (setcar (cdr (cdr quail-current-package)) map))
761 (defun quail-install-decode-map (decode-map &optional name)
762 "Install the Quail decode map DECODE-MAP in the current Quail package.
764 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
765 which to install MAP.
767 The installed decode map can be referred by the function `quail-decode-map'."
768 (if (null quail-current-package)
769 (error "No current Quail package"))
770 (if (not (and (consp decode-map) (eq (car decode-map) 'decode-map)))
771 (error "Invalid Quail decode map `%s'" decode-map))
772 (setcar (nthcdr 10 quail-current-package) decode-map))
775 (defun quail-defrule (key translation &optional name append)
776 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
777 KEY is a string meaning a sequence of keystrokes to be translated.
778 TRANSLATION is a character, a string, a vector, a Quail map,
779 a function, or a cons.
780 It it is a character, it is the sole translation of KEY.
781 If it is a string, each character is a candidate for the translation.
782 If it is a vector, each element (string or character) is a candidate
784 If it is a cons, the car is one of the above and the cdr is a function
785 to call when translating KEY (the return value is assigned to the
786 variable `quail-current-data'). If the cdr part is not a function,
787 the value itself is assigned to `quail-current-data'.
788 In these cases, a key specific Quail map is generated and assigned to KEY.
790 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
791 it is used to handle KEY.
793 Optional 3rd argument NAME, if specified, says which Quail package
794 to define this translation rule in. The default is to define it in the
795 current Quail package.
797 Optional 4th argument APPEND, if non-nil, appends TRANSLATION
798 to the current translations for KEY instead of replacing them."
800 (let ((package (quail-package name)))
802 (error "No Quail package `%s'" name))
803 (setq quail-current-package package)))
804 (quail-defrule-internal key translation (quail-map) append))
807 (defun quail-defrule-internal (key trans map &optional append decode-map props)
808 "Define KEY as TRANS in a Quail map MAP.
810 If Optional 4th arg APPEND is non-nil, TRANS is appended to the
811 current translations for KEY instead of replacing them.
813 Optional 5th arg DECODE-MAP is a Quail decode map.
815 Optional 6th arg PROPS is a property list annotating TRANS. See the
816 function `quail-define-rules' for the detail."
817 (if (null (stringp key))
818 "Invalid Quail key `%s'" key)
819 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
822 (quail-map-p trans)))
823 (error "Invalid Quail translation `%s'" trans))
824 (if (null (quail-map-p map))
825 (error "Invalid Quail map `%s'" map))
826 (let ((len (length key))
829 ;; Make a map for registering TRANS if necessary.
831 (if (null (consp map))
832 ;; We come here, for example, when we try to define a rule
833 ;; for "ABC" but a rule for "AB" is already defined as a
835 (error "Quail key %s is too long" key))
836 (setq ch (aref key idx)
837 entry (assq ch (cdr map)))
840 (setq entry (cons ch (list nil)))
841 (setcdr map (cons entry (cdr map)))))
842 (setq map (cdr entry))
846 ;; We come here, for example, when we try to define a rule
847 ;; for "AB" as a symbol but a rule for "ABC" is already
849 (error "Quail key %s is too short" key)
850 (setcdr entry trans))
851 (if (quail-map-p trans)
852 (if (not (listp (cdr map)))
853 ;; We come here, for example, when we try to define a rule
854 ;; for "AB" as a symbol but a rule for "ABC" is already
856 (error "Quail key %s is too short" key)
857 (if (not (listp (cdr trans)))
859 ;; We come here, for example, when we try to
860 ;; define a rule for "AB" as a symbol but a rule
861 ;; for "ABC" is already defined.
862 (error "Quail key %s is too short" key)
863 (setcdr entry trans))
864 (setcdr entry (append trans (cdr map)))))
865 ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS
866 ;; to a vector of strings, add PROPS to each string and record
867 ;; this rule in DECODE-MAP.
868 (when (and (or props decode-map)
869 (not (consp trans)) (not (symbolp trans)))
871 (setq trans (vector trans))
873 (setq trans (string-to-vector trans))))
874 (let ((len (length trans))
878 (setq elt (aref trans len))
880 (setq elt (char-to-string elt)))
883 (add-text-properties 0 (length elt) props elt))
886 (cons (cons elt key) (cdr decode-map)))))))
887 (if (and (car map) append)
888 (let ((prev (quail-get-translation (car map) key len)))
890 (setq prev (vector prev))
891 (setq prev (cdr prev)))
893 (setq trans (vector trans))
895 (setq trans (string-to-vector trans))))
897 (cons (list 0 0 0 0 nil)
898 (vconcat prev trans)))))
899 (setcar map trans)))))
901 (defun quail-get-translation (def key len)
902 "Return the translation specified as DEF for KEY of length LEN.
903 The translation is either a character or a cons of the form (INDEX . VECTOR),
904 where VECTOR is a vector of candidates (character or string) for
905 the translation, and INDEX points into VECTOR to specify the currently
906 selected translation."
907 (if (and def (symbolp def))
909 ;; DEF is a symbol of a function which returns valid translation.
910 (setq def (funcall def key len))
912 (if (and (consp def) (not (vectorp (cdr def))))
913 (setq def (car def)))
916 ((or (integerp def) (consp def))
924 ;; Each character in DEF is a candidate of translation. Reform
925 ;; it as (INDICES . VECTOR).
926 (setq def (string-to-vector def))
927 ;; But if the length is 1, we don't need vector but a single
928 ;; candidate as the translation.
929 (if (= (length def) 1)
931 (cons (list 0 0 0 0 nil) def)))
934 ;; Each element (string or character) in DEF is a candidate of
935 ;; translation. Reform it as (INDICES . VECTOR).
936 (cons (list 0 0 0 0 nil) def))
939 (error "Invalid object in Quail map: %s" def))))
941 (defun quail-lookup-key (key &optional len)
942 "Lookup KEY of length LEN in the current Quail map and return the definition.
943 The returned value is a Quail map specific to KEY."
945 (setq len (length key)))
948 (kbd-translate (quail-kbd-translate))
949 slot ch translation def)
950 (while (and map (< idx len))
951 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
954 (if (and (cdr map) (symbolp (cdr map)))
955 (setcdr map (funcall (cdr map) key idx)))
956 (setq slot (assq ch (cdr map)))
957 (if (and (cdr slot) (symbolp (cdr slot)))
958 (setcdr slot (funcall (cdr slot) key idx)))
959 (setq map (cdr slot)))
961 (setq quail-current-translations nil)
962 (if (and map (setq translation (quail-get-translation def key len)))
964 (if (and (consp def) (not (vectorp (cdr def))))
966 (if (not (equal (car def) translation))
967 ;; We must reflect TRANSLATION to car part of DEF.
968 (setcar def translation))
969 (setq quail-current-data
970 (if (functionp (cdr def))
973 (if (not (equal def translation))
974 ;; We must reflect TRANSLATION to car part of MAP.
975 (setcar map translation)))
976 (if (and (consp translation) (vectorp (cdr translation)))
978 (setq quail-current-translations translation)
979 (if (quail-forget-last-selection)
980 (setcar (car quail-current-translations) 0))))))
981 ;; We may have to reform cdr part of MAP.
982 (if (and (cdr map) (functionp (cdr map)))
983 (setcdr map (funcall (cdr map) key len)))
986 (put 'quail-error 'error-conditions '(quail-error error))
987 (defun quail-error (&rest args)
988 (signal 'quail-error (apply 'format args)))
990 (defvar quail-translating nil)
991 (defvar quail-converting nil)
992 (defvar quail-conversion-str nil)
994 (defun quail-input-method (key)
995 (if (or buffer-read-only
996 overriding-terminal-local-map
997 overriding-local-map)
999 (quail-setup-overlays (quail-conversion-keymap))
1000 (let ((modified-p (buffer-modified-p))
1001 (buffer-undo-list t))
1002 (or (and quail-guidance-win
1003 (window-live-p quail-guidance-win)
1004 (eq (window-buffer quail-guidance-win) quail-guidance-buf)
1005 (not input-method-use-echo-area))
1006 (quail-show-guidance-buf))
1008 (if (quail-conversion-keymap)
1009 (quail-start-conversion key)
1010 (quail-start-translation key))
1011 (quail-delete-overlays)
1012 (if (buffer-live-p quail-guidance-buf)
1014 (set-buffer quail-guidance-buf)
1016 (if input-method-use-echo-area
1017 (quail-hide-guidance-buf))
1018 (set-buffer-modified-p modified-p)
1019 ;; Run this hook only when the current input method doesn't require
1020 ;; conversion. When conversion is required, the conversion function
1021 ;; should run this hook at a proper timing.
1022 (unless (quail-conversion-keymap)
1023 (run-hooks 'input-method-after-insert-chunk-hook))))))
1025 (defun quail-overlay-region-events (overlay)
1026 (let ((start (overlay-start overlay))
1027 (end (overlay-end overlay)))
1030 (string-to-list (buffer-substring start end))
1031 (delete-region start end)))))
1033 (defsubst quail-delete-region ()
1034 "Delete the text in the current translation region of Quail."
1035 (if (overlay-start quail-overlay)
1036 (delete-region (overlay-start quail-overlay)
1037 (overlay-end quail-overlay))))
1039 (defun quail-start-translation (key)
1040 "Start translation of the typed character KEY by the current Quail package."
1041 ;; Check the possibility of translating KEY.
1042 ;; If KEY is nil, we can anyway start translation.
1043 (if (or (and (integerp key)
1044 (assq (if (quail-kbd-translate)
1045 (quail-keyboard-translate key) key)
1048 ;; OK, we can start translation.
1049 (let* ((echo-keystrokes 0)
1051 (overriding-terminal-local-map (quail-translation-keymap))
1052 (generated-events nil)
1053 (input-method-function nil)
1054 (modified-p (buffer-modified-p)))
1055 (setq quail-current-key ""
1056 quail-current-str ""
1057 quail-translating t)
1059 (setq unread-command-events (cons key unread-command-events)))
1060 (while quail-translating
1061 (set-buffer-modified-p modified-p)
1062 ;; Hide '... loaded' message.
1064 (let* ((keyseq (read-key-sequence
1065 (and input-method-use-echo-area
1066 (concat input-method-previous-message
1069 (cmd (lookup-key (quail-translation-keymap) keyseq)))
1071 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
1072 (eq cmd 'quail-self-insert-command))
1073 (let ((last-command-event (aref keyseq (1- (length keyseq))))
1074 (last-command this-command)
1078 (call-interactively cmd)
1079 (quail-error (message "%s" (cdr err)) (beep))))
1080 ;; KEYSEQ is not defined in the translation keymap.
1081 ;; Let's return the event(s) to the caller.
1082 (setq unread-command-events
1083 (string-to-list (this-single-command-raw-keys)))
1084 (setq quail-translating nil))))
1085 (quail-delete-region)
1086 (if (and quail-current-str (> (length quail-current-str) 0))
1087 (let* ((len (length quail-current-str))
1089 (val (find-composition 0 len quail-current-str))
1090 (advice (get-text-property idx 'advice quail-current-str))
1092 ;; If the selected input has `advice' function, generate
1093 ;; a special event (quail-advice QUAIL-CURRENT-STR).
1095 (setq generated-events
1096 (cons (list 'quail-advice quail-current-str)
1098 ;; Push characters in quail-current-str one by one to
1099 ;; generated-events while interleaving it with a special
1100 ;; event (compose-last-chars LEN) at each composition
1103 (when (and val (= idx (nth 1 val)))
1104 (setq generated-events
1105 (cons (list 'compose-last-chars (- idx (car val)))
1107 (setq val (find-composition idx len quail-current-str)))
1109 (setq char (aref quail-current-str idx))
1110 (or enable-multibyte-characters
1111 (setq char (multibyte-char-to-unibyte char)))
1112 (setq generated-events (cons char generated-events)))
1113 (setq idx (1+ idx)))
1114 ;; Reorder generated-events.
1115 (setq generated-events (nreverse generated-events))))
1116 (if (and input-method-exit-on-first-char generated-events)
1117 (list (car generated-events))
1120 ;; Since KEY doesn't start any translation, just return it.
1123 (defun quail-start-conversion (key)
1124 "Start conversion of the typed character KEY by the current Quail package."
1125 ;; Check the possibility of translating KEY.
1126 ;; If KEY is nil, we can anyway start translation.
1127 (if (or (and (integerp key)
1128 (assq (if (quail-kbd-translate)
1129 (quail-keyboard-translate key) key)
1132 ;; Ok, we can start translation and conversion.
1133 (let* ((echo-keystrokes 0)
1135 (overriding-terminal-local-map (quail-conversion-keymap))
1136 (generated-events nil)
1137 (input-method-function nil)
1138 (modified-p (buffer-modified-p)))
1139 (setq quail-current-key ""
1140 quail-current-str ""
1143 quail-conversion-str "")
1145 (setq unread-command-events (cons key unread-command-events)))
1146 (while quail-converting
1147 (set-buffer-modified-p modified-p)
1148 (or quail-translating
1150 (setq quail-current-key ""
1151 quail-current-str ""
1152 quail-translating t)
1153 (quail-setup-overlays nil)))
1154 ;; Hide '... loaded' message.
1156 (let* ((keyseq (read-key-sequence
1157 (and input-method-use-echo-area
1158 (concat input-method-previous-message
1159 quail-conversion-str
1162 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1163 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1164 (let ((last-command-event (aref keyseq (1- (length keyseq))))
1165 (last-command this-command)
1169 (call-interactively cmd)
1170 (quail-error (message "%s" (cdr err)) (beep)))
1171 (or quail-translating
1173 (if quail-current-str
1174 (setq quail-conversion-str
1175 (concat quail-conversion-str
1176 (if (stringp quail-current-str)
1178 (char-to-string quail-current-str)))))
1179 (if (or input-method-exit-on-first-char
1180 (= (length quail-conversion-str) 0))
1181 (setq quail-converting nil)))))
1182 ;; KEYSEQ is not defined in the conversion keymap.
1183 ;; Let's return the event(s) to the caller.
1184 (setq unread-command-events
1185 (string-to-list (this-single-command-raw-keys)))
1186 (setq quail-converting nil))))
1187 (if (overlay-start quail-conv-overlay)
1188 (delete-region (overlay-start quail-conv-overlay)
1189 (overlay-end quail-conv-overlay)))
1190 (if (> (length quail-conversion-str) 0)
1191 (setq generated-events
1193 (if enable-multibyte-characters
1194 quail-conversion-str
1195 (string-make-unibyte quail-conversion-str)))))
1196 (if (and input-method-exit-on-first-char generated-events)
1197 (list (car generated-events))
1200 ;; Since KEY doesn't start any translation, just return it.
1203 (defun quail-terminate-translation ()
1204 "Terminate the translation of the current key."
1205 (setq quail-translating nil)
1206 (if (buffer-live-p quail-guidance-buf)
1208 (set-buffer quail-guidance-buf)
1211 (defun quail-select-current ()
1212 "Select the current text shown in Quail translation region."
1214 (quail-terminate-translation))
1216 ;; Update the current translation status according to CONTROL-FLAG.
1217 ;; If CONTROL-FLAG is integer value, it is the number of keys in the
1218 ;; head quail-current-key which can be translated. The remaining keys
1219 ;; are put back to unread-command-events to be handled again. If
1220 ;; CONTROL-FLAG is t, terminate the translation for the whole keys in
1221 ;; quail-current-key. If CONTROL-FLAG is nil, proceed the translation
1224 (defun quail-update-translation (control-flag)
1225 (let ((func (quail-update-translation-function)))
1227 (setq control-flag (funcall func control-flag))
1228 (if (numberp control-flag)
1229 (let ((len (length quail-current-key)))
1231 (setq control-flag t
1232 quail-current-str quail-current-key)
1233 (if input-method-exit-on-first-char
1234 (setq len control-flag)
1235 (while (> len control-flag)
1237 (setq unread-command-events
1238 (cons (aref quail-current-key len)
1239 unread-command-events))))
1240 (if quail-current-str
1241 (if input-method-exit-on-first-char
1242 (setq control-flag t))
1243 (setq quail-current-str
1244 (substring quail-current-key 0 len)))))
1245 (if quail-current-str
1246 (if (and input-method-exit-on-first-char
1248 (setq control-flag t))
1249 (setq quail-current-str quail-current-key)))))
1250 (or input-method-use-echo-area
1252 (quail-delete-region)
1253 (insert quail-current-str)))
1254 (let (quail-current-str)
1255 (quail-update-guidance))
1256 (or (stringp quail-current-str)
1257 (setq quail-current-str (char-to-string quail-current-str)))
1259 (quail-terminate-translation)))
1261 (defun quail-self-insert-command ()
1262 "Translate the typed character to by the current Quail map."
1264 (setq quail-current-key
1265 (concat quail-current-key (char-to-string last-command-event)))
1266 (or (catch 'quail-tag
1267 (quail-update-translation (quail-translate-key))
1269 ;; If someone throws for `quail-tag' by value nil, we exit from
1270 ;; translation mode.
1271 (setq quail-translating nil)))
1273 ;; Return the actual definition part of Quail map MAP.
1274 (defun quail-map-definition (map)
1275 (let ((def (car map)))
1276 (if (and (consp def) (not (vectorp (cdr def))))
1277 (setq def (car def)))
1282 ;; Return a string to be shown as the current translation of key
1283 ;; sequence of length LEN. DEF is a definition part of Quail map for
1285 (defun quail-get-current-str (len def)
1286 (or (and (consp def) (aref (cdr def) (car (car def))))
1289 (let ((str (quail-get-current-str
1291 (quail-map-definition (quail-lookup-key
1292 quail-current-key (1- len))))))
1294 (concat (if (stringp str) str (char-to-string str))
1295 (substring quail-current-key (1- len) len)))))))
1297 (defvar quail-guidance-translations-starting-column 20)
1299 ;; Update `quail-current-translations' to make RELATIVE-INDEX the
1300 ;; current translation.
1301 (defun quail-update-current-translations (&optional relative-index)
1302 (let* ((indices (car quail-current-translations))
1304 (start (nth 1 indices))
1305 (end (nth 2 indices)))
1306 ;; Validate the index number of current translation.
1308 (setcar indices (setq cur 0))
1309 (if (>= cur (length (cdr quail-current-translations)))
1311 (setq cur (1- (length (cdr quail-current-translations)))))))
1313 (if (or (null end) ; We have not yet calculated END.
1314 (< cur start) ; We moved to the previous block.
1315 (>= cur end)) ; We moved to the next block.
1316 (let ((len (length (cdr quail-current-translations)))
1317 (maxcol (- (window-width quail-guidance-win)
1318 quail-guidance-translations-starting-column))
1319 (block (nth 3 indices))
1320 col idx width trans num-items blocks)
1322 ;; We must calculate from the head.
1323 (setq start 0 block 0)
1324 (if end ; i.e. (>= cur end)
1326 (setq idx start col 0 end start num-items 0)
1327 ;; Loop until we hit the tail, or reach the block of CUR.
1328 (while (and (< idx len) (>= cur end))
1330 (setq start idx col 0 block (1+ block)))
1331 (setq trans (aref (cdr quail-current-translations) idx))
1332 (setq width (if (integerp trans) (char-width trans)
1333 (string-width trans)))
1334 (setq col (+ col width 3) num-items (1+ num-items))
1335 (if (and (> num-items 0)
1336 (or (>= col maxcol) (> num-items 10)))
1337 (setq end idx num-items 0)
1338 (setq idx (1+ idx))))
1339 (setcar (nthcdr 3 indices) block)
1342 ;; We hit the tail before reaching MAXCOL.
1344 (setcar (nthcdr 4 indices) block)))
1345 (setcar (cdr indices) start)
1346 (setcar (nthcdr 2 indices) end)))
1348 (if (>= (+ start relative-index) end)
1349 (setcar indices end)
1350 (setcar indices (+ start relative-index))))
1351 (setq quail-current-str
1352 (aref (cdr quail-current-translations) (car indices)))
1353 (or (stringp quail-current-str)
1354 (setq quail-current-str (char-to-string quail-current-str)))))
1356 (defun quail-translate-key ()
1357 "Translate the current key sequence according to the current Quail map.
1358 Return t if we can terminate the translation.
1359 Return nil if the current key sequence may be followed by more keys.
1360 Return number if we can't find any translation for the current key
1361 sequence. The number is the count of valid keys in the current
1362 sequence counting from the head."
1363 (let* ((len (length quail-current-key))
1364 (map (quail-lookup-key quail-current-key len))
1367 (let ((def (quail-map-definition map)))
1368 (setq quail-current-str (quail-get-current-str len def))
1369 ;; Return t only if we can terminate the current translation.
1371 ;; No alternative translations.
1372 (or (null (consp def)) (= (length (cdr def)) 1))
1373 ;; No translation for the longer key.
1375 ;; No shorter breaking point.
1376 (or (null (quail-maximum-shortest))
1378 (null (quail-lookup-key quail-current-key (1- len)))
1379 (null (quail-lookup-key
1380 (substring quail-current-key -2 -1) 1)))))
1382 ;; There's no translation for the current key sequence. Before
1383 ;; giving up, we must check two possibilities.
1385 (quail-maximum-shortest)
1387 (setq def (quail-map-definition
1388 (quail-lookup-key quail-current-key (- len 2))))
1389 (quail-lookup-key (substring quail-current-key -2) 2))
1390 ;; Now the sequence is "...ABCD", which can be split into
1391 ;; "...AB" and "CD..." to get valid translation.
1392 ;; At first, get translation of "...AB".
1393 (setq quail-current-str (quail-get-current-str (- len 2) def))
1394 ;; Then, return the length of "...AB".
1398 (quail-lookup-key (substring quail-current-key 0 -1))
1399 quail-current-translations
1400 (not (quail-deterministic))
1401 (setq ch (aref quail-current-key (1- len)))
1402 (>= ch ?0) (<= ch ?9))
1403 ;; A numeric key is entered to select a desirable translation.
1404 (setq quail-current-key (substring quail-current-key 0 -1))
1405 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1406 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1407 (quail-update-current-translations ch)
1408 ;; And, we can terminate the current translation.
1412 ;; No way to handle the last character in this context.
1415 (defun quail-next-translation ()
1416 "Select next translation in the current batch of candidates."
1418 (if quail-current-translations
1419 (let ((indices (car quail-current-translations)))
1420 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1421 ;; We are already at the tail.
1423 (setcar indices (1+ (car indices)))
1424 (quail-update-current-translations)
1425 (quail-update-translation nil)))
1426 (setq unread-command-events
1427 (cons last-command-event unread-command-events))
1428 (quail-terminate-translation)))
1430 (defun quail-prev-translation ()
1431 "Select previous translation in the current batch of candidates."
1433 (if quail-current-translations
1434 (let ((indices (car quail-current-translations)))
1435 (if (= (car indices) 0)
1436 ;; We are already at the head.
1438 (setcar indices (1- (car indices)))
1439 (quail-update-current-translations)
1440 (quail-update-translation nil)))
1441 (setq unread-command-events
1442 (cons last-command-event unread-command-events))
1443 (quail-terminate-translation)))
1445 (defun quail-next-translation-block ()
1446 "Select from the next block of translations."
1448 (if quail-current-translations
1449 (let* ((indices (car quail-current-translations))
1450 (offset (- (car indices) (nth 1 indices))))
1451 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1452 ;; We are already at the last block.
1454 (setcar indices (+ (nth 2 indices) offset))
1455 (quail-update-current-translations)
1456 (quail-update-translation nil)))
1457 (setq unread-command-events
1458 (cons last-command-event unread-command-events))
1459 (quail-terminate-translation)))
1461 (defun quail-prev-translation-block ()
1462 "Select the previous batch of 10 translation candidates."
1464 (if quail-current-translations
1465 (let* ((indices (car quail-current-translations))
1466 (offset (- (car indices) (nth 1 indices))))
1467 (if (= (nth 1 indices) 0)
1468 ;; We are already at the first block.
1470 (setcar indices (1- (nth 1 indices)))
1471 (quail-update-current-translations)
1472 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1474 (setcar indices (+ (nth 1 indices) offset))
1475 (quail-update-current-translations)))
1476 (quail-update-translation nil)))
1477 (setq unread-command-events
1478 (cons last-command-event unread-command-events))
1479 (quail-terminate-translation)))
1481 (defun quail-abort-translation ()
1482 "Abort translation and delete the current Quail key sequence."
1484 (quail-delete-region)
1485 (setq quail-current-str nil)
1486 (quail-terminate-translation))
1488 (defun quail-delete-last-char ()
1489 "Delete the last input character from the current Quail key sequence."
1491 (if (= (length quail-current-key) 1)
1492 (quail-abort-translation)
1493 (setq quail-current-key (substring quail-current-key 0 -1))
1494 (quail-delete-region)
1495 (quail-update-translation (quail-translate-key))))
1497 ;; For conversion mode.
1499 (defsubst quail-point-in-conversion-region ()
1500 "Return non-nil value if the point is in conversion region of Quail mode."
1502 (and (setq start (overlay-start quail-conv-overlay))
1503 (>= (setq pos (point)) start)
1504 (<= pos (overlay-end quail-conv-overlay)))))
1506 (defun quail-conversion-backward-char ()
1508 (if (<= (point) (overlay-start quail-conv-overlay))
1509 (quail-error "Beginning of conversion region"))
1510 (setq quail-translating nil)
1513 (defun quail-conversion-forward-char ()
1515 (if (>= (point) (overlay-end quail-conv-overlay))
1516 (quail-error "End of conversion region"))
1517 (setq quail-translating nil)
1520 (defun quail-conversion-beginning-of-region ()
1522 (setq quail-translating nil)
1523 (goto-char (overlay-start quail-conv-overlay)))
1525 (defun quail-conversion-end-of-region ()
1527 (setq quail-translating nil)
1528 (goto-char (overlay-end quail-conv-overlay)))
1530 (defun quail-conversion-delete-char ()
1532 (setq quail-translating nil)
1533 (if (>= (point) (overlay-end quail-conv-overlay))
1534 (quail-error "End of conversion region"))
1536 (let ((start (overlay-start quail-conv-overlay))
1537 (end (overlay-end quail-conv-overlay)))
1538 (setq quail-conversion-str (buffer-substring start end))
1540 (setq quail-converting nil))))
1542 (defun quail-conversion-delete-tail ()
1544 (if (>= (point) (overlay-end quail-conv-overlay))
1545 (quail-error "End of conversion region"))
1546 (delete-region (point) (overlay-end quail-conv-overlay))
1547 (let ((start (overlay-start quail-conv-overlay))
1548 (end (overlay-end quail-conv-overlay)))
1549 (setq quail-conversion-str (buffer-substring start end))
1551 (setq quail-converting nil))))
1553 (defun quail-conversion-backward-delete-char ()
1555 (if (> (length quail-current-key) 0)
1556 (quail-delete-last-char)
1557 (if (<= (point) (overlay-start quail-conv-overlay))
1558 (quail-error "Beginning of conversion region"))
1560 (let ((start (overlay-start quail-conv-overlay))
1561 (end (overlay-end quail-conv-overlay)))
1562 (setq quail-conversion-str (buffer-substring start end))
1564 (setq quail-converting nil)))))
1566 (defun quail-do-conversion (func &rest args)
1567 "Call FUNC to convert text in the current conversion region of Quail.
1568 Remaining args are for FUNC."
1569 (delete-overlay quail-overlay)
1572 (defun quail-no-conversion ()
1573 "Do no conversion of the current conversion region of Quail."
1575 (setq quail-converting nil))
1577 ;; Guidance, Completion, and Help buffer handlers.
1579 ;; Make a new one-line frame for Quail guidance buffer.
1580 (defun quail-make-guidance-frame (buf)
1581 (let* ((fparam (frame-parameters))
1582 (top (cdr (assq 'top fparam)))
1583 (border (cdr (assq 'border-width fparam)))
1584 (internal-border (cdr (assq 'internal-border-width fparam)))
1586 (frame-char-height) (* internal-border 2) (* border 2))))
1588 (setq newtop (+ top (frame-pixel-height))))
1589 (let* ((frame (make-frame (append '((user-position . t) (height . 1)
1590 (minibuffer) (menu-bar-lines . 0))
1591 (cons (cons 'top newtop) fparam))))
1592 (win (frame-first-window frame)))
1593 (set-window-buffer win buf)
1594 ;;(set-window-dedicated-p win t)
1597 ;; Setup Quail completion buffer.
1598 (defun quail-setup-completion-buf ()
1599 (unless (buffer-live-p quail-completion-buf)
1600 (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
1602 (set-buffer quail-completion-buf)
1603 (setq quail-overlay (make-overlay 1 1))
1604 (overlay-put quail-overlay 'face 'highlight))))
1606 ;; Return t iff the current Quail package requires showing guidance
1608 (defun quail-require-guidance-buf ()
1609 (and input-method-verbose-flag
1610 (if (eq input-method-verbose-flag 'default)
1611 (not (and (eq (selected-window) (minibuffer-window))
1613 (if (eq input-method-verbose-flag 'complex-only)
1614 (not (quail-simple))
1617 (defun quail-show-guidance-buf ()
1618 "Display a guidance buffer for Quail input method in some window.
1619 Create the buffer if it does not exist yet.
1620 The buffer is normally displayed at the echo area,
1621 but if the current buffer is a minibuffer, it is shown in
1622 the bottom-most ordinary window of the same frame,
1623 or in a newly created frame (if the selected frame has no other windows)."
1624 (when (quail-require-guidance-buf)
1625 ;; At first, setup a guidance buffer.
1626 (or (buffer-live-p quail-guidance-buf)
1627 (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*")))
1628 (let ((name (quail-name))
1629 (title (quail-title)))
1631 (set-buffer quail-guidance-buf)
1632 ;; To show the title of Quail package.
1633 (setq current-input-method name
1634 current-input-method-title title)
1636 (or (overlayp quail-overlay)
1638 (setq quail-overlay (make-overlay 1 1))
1639 (overlay-put quail-overlay 'face 'highlight)))
1640 (delete-overlay quail-overlay)
1641 (set-buffer-modified-p nil)))
1642 (bury-buffer quail-guidance-buf)
1644 ;; Assign the buffer " *Minibuf-N*" to all windows which are now
1645 ;; displaying quail-guidance-buf.
1646 (let ((win-list (get-buffer-window-list quail-guidance-buf t t)))
1648 (set-window-buffer (car win-list)
1649 (format " *Minibuf-%d*" (minibuffer-depth)))
1650 (setq win-list (cdr win-list))))
1652 ;; Then, display it in an appropriate window.
1653 (let ((win (minibuffer-window)))
1654 (if (or (eq (selected-window) win)
1655 input-method-use-echo-area)
1656 ;; Since we are in minibuffer, we can't use it for guidance.
1657 (if (eq win (frame-root-window))
1658 ;; Create a frame. It is sure that we are using some
1660 (quail-make-guidance-frame quail-guidance-buf)
1661 ;; Find the bottom window and split it if necessary.
1663 (setq win (window-at
1664 0 (1- (- (frame-height) (window-height win)))))
1665 (setq height (window-height win))
1666 ;; If WIN is tall enough, split it vertically and use
1669 (let ((window-min-height 2))
1670 ;; Here, `split-window' returns a lower window
1671 ;; which is what we wanted.
1672 (setq win (split-window win (- height 2)))))
1673 (set-window-buffer win quail-guidance-buf)
1674 ;;(set-window-dedicated-p win t)
1676 (set-window-buffer win quail-guidance-buf)
1677 (set-minibuffer-window win))
1678 (setq quail-guidance-win win)))
1680 ;; And, create a buffer for completion.
1681 (quail-setup-completion-buf)
1682 (bury-buffer quail-completion-buf))
1684 (defun quail-hide-guidance-buf ()
1685 "Hide the Quail guidance buffer."
1686 (if (buffer-live-p quail-guidance-buf)
1687 (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
1690 (setq win (car win-list) win-list (cdr win-list))
1691 (if (window-minibuffer-p win)
1692 ;; We are using echo area for the guidance buffer.
1693 ;; Vacate it to the deepest minibuffer.
1694 (set-window-buffer win
1695 (format " *Minibuf-%d*" (minibuffer-depth)))
1696 (if (eq win (frame-root-window (window-frame win)))
1698 ;; We are using a separate frame for guidance buffer.
1699 ;;(set-window-dedicated-p win nil)
1700 (delete-frame (window-frame win)))
1701 ;;(set-window-dedicated-p win nil)
1702 (delete-window win))))
1703 (setq quail-guidance-win nil))))
1705 (defun quail-update-guidance ()
1706 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1707 ;; Update guidance buffer.
1708 (if (quail-require-guidance-buf)
1709 (let ((guidance (quail-guidance)))
1710 (or (and (eq (selected-frame) (window-frame (minibuffer-window)))
1711 (eq (selected-frame) (window-frame quail-guidance-win)))
1712 (quail-show-guidance-buf))
1713 (cond ((or (eq guidance t)
1715 ;; Show the current possible translations.
1716 (quail-show-translations))
1718 ;; Show the current input keys.
1719 (let ((key quail-current-key))
1721 (set-buffer quail-guidance-buf)
1725 ;; Update completion buffer if displayed now. We highlight the
1726 ;; selected candidate string in *Completion* buffer if any.
1727 (let ((win (get-buffer-window quail-completion-buf))
1731 (setq str (if (stringp quail-current-str)
1733 (if (numberp quail-current-str)
1734 (char-to-string quail-current-str)))
1735 key quail-current-key)
1736 (set-buffer quail-completion-buf)
1737 (goto-char (point-min))
1738 (if (null (search-forward (concat " " key ":") nil t))
1739 (delete-overlay quail-overlay)
1741 (if (and str (search-forward (concat "." str) nil t))
1742 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
1743 (move-overlay quail-overlay (match-beginning 0) (point)))
1744 ;; Now POS points end of KEY and (point) points end of STR.
1745 (if (pos-visible-in-window-p (point) win)
1746 ;; STR is already visible.
1748 ;; We want to make both KEY and STR visible, but if the
1749 ;; window is too short, make at least STR visible.
1750 (setq pos (progn (point) (goto-char pos)))
1752 (set-window-start win (point))
1753 (if (not (pos-visible-in-window-p pos win))
1754 (set-window-start win pos))
1757 (defun quail-show-translations ()
1758 "Show the current possible translations."
1759 (let* ((key quail-current-key)
1760 (map (quail-lookup-key quail-current-key))
1761 (current-translations quail-current-translations))
1762 (if quail-current-translations
1763 (quail-update-current-translations))
1765 (set-buffer quail-guidance-buf)
1768 ;; Show the current key.
1769 (let ((guidance (quail-guidance)))
1770 (if (listp guidance)
1771 ;; We must show the specified PROMPTKEY instead of the
1772 ;; actual typed keys.
1777 (setq prompt-key (cdr (assoc (aref key i) guidance)))
1778 (insert (or prompt-key (aref key i)))
1782 ;; Show followable keys.
1783 (if (and (> (length key) 0) (cdr map))
1784 (let ((keys (mapcar (function (lambda (x) (car x)))
1786 (setq keys (sort keys '<))
1790 (setq keys (cdr keys)))
1793 ;; Show list of translations.
1794 (if (and current-translations
1795 (not (quail-deterministic)))
1796 (let* ((indices (car current-translations))
1798 (start (nth 1 indices))
1799 (end (nth 2 indices))
1801 (indent-to (- quail-guidance-translations-starting-column 7))
1802 (insert (format "(%02d/"(nth 3 indices))
1804 (format "%02d)" (nth 4 indices))
1807 (insert (format " %d." (if (= (- idx start) 9) 0
1808 (1+ (- idx start)))))
1809 (let ((pos (point)))
1810 (insert (aref (cdr current-translations) idx))
1812 (move-overlay quail-overlay pos (point))))
1813 (setq idx (1+ idx)))))
1816 (defvar quail-completion-max-depth 5
1817 "The maximum depth of Quail completion list.")
1819 (defun quail-completion ()
1820 "List all completions for the current key.
1821 All possible translations of the current key and whole possible longer keys
1822 are shown (at most to the depth specified `quail-completion-max-depth')."
1824 (quail-setup-completion-buf)
1825 (let ((win (get-buffer-window quail-completion-buf 'visible))
1826 (key quail-current-key)
1827 (map (quail-lookup-key quail-current-key))
1828 (require-update nil))
1830 (set-buffer quail-completion-buf)
1832 (equal key quail-current-key)
1833 (eq last-command 'quail-completion))
1834 ;; The window for Quail completion buffer has already been
1835 ;; shown. We just scroll it appropriately.
1836 (if (pos-visible-in-window-p (point-max) win)
1837 (set-window-start win (point-min))
1838 (let ((other-window-scroll-buffer quail-completion-buf))
1839 (scroll-other-window)))
1840 (setq quail-current-key key)
1842 (insert "Possible completion and corresponding translations are:\n")
1843 (quail-completion-1 key map 1)
1844 (goto-char (point-min))
1845 (display-buffer (current-buffer))
1846 (setq require-update t)))
1848 (quail-update-guidance)))
1849 (setq this-command 'quail-completion))
1851 ;; List all completions of KEY in MAP with indentation INDENT.
1852 (defun quail-completion-1 (key map indent)
1853 (let ((len (length key)))
1856 (if (and (symbolp map) (fboundp map))
1857 (setq map (funcall map key len)))
1859 (quail-completion-list-translations map key (+ indent len 1))
1861 (setq indent (+ indent 2))
1862 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
1864 (newkey (make-string (1+ len) 0))
1867 (setq l (funcall l)))
1868 ;; Set KEY in the first LEN characters of NEWKEY.
1870 (aset newkey i (aref key i))
1872 (while l ; L = ((CHAR . DEFN) ....) ;
1873 (aset newkey len (car (car l)))
1874 (quail-completion-1 newkey (cdr (car l)) indent)
1875 (setq l (cdr l)))))))
1877 ;; List all possible translations of KEY in Quail map MAP with
1878 ;; indentation INDENT.
1879 (defun quail-completion-list-translations (map key indent)
1880 (let (beg (translations
1881 (quail-get-translation (car map) key (length key))))
1882 (if (integerp translations)
1885 ;; Endow the character `translations' with `mouse-face' text
1886 ;; property to enable `mouse-2' completion.
1888 (insert translations)
1889 (put-text-property beg (point) 'mouse-face 'highlight)
1891 ;; We need only vector part.
1892 (setq translations (cdr translations))
1893 ;; Insert every 10 elements with indices in a line.
1894 (let ((len (length translations))
1898 (when (zerop (% i 10))
1902 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
1903 ;; We show the last digit of FROM while converting
1904 ;; 0,1,..,9 to 1,2,..,0.
1905 (insert (format " %d." (% (1+ i) 10)))
1907 (insert (aref translations i))
1908 ;; Passing the mouse over a character will highlight.
1909 (put-text-property beg (point) 'mouse-face 'highlight)
1913 ;; Choose a completion in *Quail Completions* buffer with mouse-2.
1915 (defun quail-mouse-choose-completion (event)
1916 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
1918 ;; This function is an exact copy of the mouse.el function
1919 ;; `mouse-choose-completion' except that we:
1920 ;; 1) add two lines from `choose-completion' in simple.el to give
1921 ;; the `mouse-2' click a little more leeway.
1922 ;; 2) don't bury *Quail Completions* buffer so comment a section, and
1923 ;; 3) delete/terminate the current quail selection here.
1924 ;; Give temporary modes such as isearch a chance to turn off.
1925 (run-hooks 'mouse-leave-buffer-hook)
1926 (let ((buffer (window-buffer))
1930 (set-buffer (window-buffer (posn-window (event-start event))))
1931 (if completion-reference-buffer
1932 (setq buffer completion-reference-buffer))
1933 (setq base-size completion-base-size)
1935 (goto-char (posn-point (event-start event)))
1937 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1938 (setq end (point) beg (1+ (point))))
1939 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
1940 (setq end (1- (point)) beg (point)))
1942 (quail-error "No completion here"))
1943 (setq beg (previous-single-property-change beg 'mouse-face))
1944 (setq end (or (next-single-property-change end 'mouse-face)
1946 (setq choice (buffer-substring beg end)))))
1947 ; (let ((owindow (selected-window)))
1948 ; (select-window (posn-window (event-start event)))
1949 ; (if (and (one-window-p t 'selected-frame)
1950 ; (window-dedicated-p (selected-window)))
1951 ; ;; This is a special buffer's frame
1952 ; (iconify-frame (selected-frame))
1953 ; (or (window-dedicated-p (selected-window))
1955 ; (select-window owindow))
1956 (quail-delete-region)
1957 (quail-choose-completion-string choice buffer base-size)
1958 (quail-terminate-translation)))
1960 ;; Modify the simple.el function `choose-completion-string', because
1961 ;; the simple.el function `choose-completion-delete-max-match' breaks
1962 ;; on Mule data, since the semantics of `forward-char' have changed.
1964 (defun quail-choose-completion-string (choice &optional buffer base-size)
1965 (let ((buffer (or buffer completion-reference-buffer)))
1966 ;; If BUFFER is a minibuffer, barf unless it's the currently
1967 ;; active minibuffer.
1968 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
1969 (or (not (active-minibuffer-window))
1971 (window-buffer (active-minibuffer-window))))))
1972 (quail-error "Minibuffer is not active for completion")
1973 ;; Store the completion in `quail-current-str', which will later
1974 ;; be converted to a character event list, then inserted into
1975 ;; the buffer where completion was requested.
1978 ; (delete-region (+ base-size (point-min)) (point))
1979 ; (choose-completion-delete-max-match choice))
1980 (setq quail-current-str choice)
1981 ;; Update point in the window that BUFFER is showing in.
1982 (let ((window (get-buffer-window buffer t)))
1983 (set-window-point window (point)))
1984 ;; If completing for the minibuffer, exit it with this choice.
1985 (and (not completion-no-auto-exit)
1986 (equal buffer (window-buffer (minibuffer-window)))
1987 minibuffer-completion-table
1988 ;; If this is reading a file name, and the file name chosen
1989 ;; is a directory, don't exit the minibuffer.
1990 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
1991 (file-directory-p (buffer-string)))
1992 (select-window (active-minibuffer-window))
1993 (exit-minibuffer))))))
1995 (defun quail-help (&optional package)
1996 "Show brief description of the current Quail package.
1997 Optional 2nd arg PACKAGE specifies the alternative Quail package to describe."
2000 (setq package quail-current-package))
2001 (let ((help-xref-mule-regexp help-xref-mule-regexp-template))
2002 (with-output-to-temp-buffer "*Help*"
2004 (set-buffer standard-output)
2005 (setq quail-current-package package)
2006 (insert "Quail input method (name:"
2008 ", mode line indicator:["
2010 "])\n\n---- Documentation ----\n"
2013 (if (quail-show-layout) (quail-show-kbd-layout))
2014 (quail-help-insert-keymap-description
2015 (quail-translation-keymap)
2016 (format "--- Key bindings%s ---\n"
2017 (if (quail-conversion-keymap)
2018 " (while translating)"
2020 (if (quail-conversion-keymap)
2021 (quail-help-insert-keymap-description
2022 (quail-conversion-keymap)
2023 "\n--- Key bindings (while converting) ---\n"))
2024 (setq quail-current-package nil)
2025 (help-setup-xref (list #'quail-help package)
2026 (interactive-p))))))
2028 (defun quail-help-insert-keymap-description (keymap &optional header)
2033 (insert (substitute-command-keys "\\{keymap}"))
2035 (while (search-forward "quail-other-command" nil 'move)
2036 (delete-region (line-beginning-position) (1+ (line-end-position))))))
2038 (defun quail-show-kbd-layout ()
2039 "Show keyboard layout with key tops of multilingual characters."
2040 (insert "--- Keyboard layout ---\n")
2041 (let ((blink-matching-paren nil)
2044 (while (< i quail-keyboard-layout-len)
2048 (indent-to (/ i 30)))
2051 (setq ch (aref quail-keyboard-layout i))
2052 (when (and (quail-kbd-translate)
2054 ;; This is the case that the current input method simulates
2055 ;; some keyboard layout (which means it requires keyboard
2056 ;; translation) and a key at location `i' exists on users
2057 ;; keyboard. We must translate that key by
2058 ;; `quail-keyboard-layout-standard'. But if if there's no
2059 ;; corresponding key in that standard layout, we must simulate
2060 ;; what is inserted if that key is pressed by setting CH a
2062 (setq ch (aref quail-keyboard-layout-standard i))
2064 (setq ch (- (aref quail-keyboard-layout i)))))
2066 (let ((last-command-event (- ch)))
2067 (self-insert-command 1))
2070 (let* ((map (cdr (assq ch (cdr (quail-map)))))
2071 (translation (and map (quail-get-translation
2072 (car map) (char-to-string ch) 1))))
2073 (if (integerp translation)
2074 (insert translation)
2075 (if (consp translation)
2076 (insert (aref (cdr translation) (car (car translation))))
2077 (let ((last-command-event ch))
2078 (self-insert-command 1)))))))
2082 (defun quail-translation-help ()
2083 "Show help message while translating in Quail input method."
2085 (if (not (eq this-command last-command))
2086 (let (state-msg keymap)
2087 (if (and quail-converting (= (length quail-current-key) 0))
2089 (format "Converting string %S by input method %S.\n"
2090 quail-conversion-str (quail-name))
2091 keymap (quail-conversion-keymap))
2093 (format "Translating key sequence %S by input method %S.\n"
2094 quail-current-key (quail-name))
2095 keymap (quail-translation-keymap)))
2096 (with-output-to-temp-buffer "*Help*"
2098 (set-buffer standard-output)
2100 (quail-help-insert-keymap-description
2102 "-----------------------\n")
2105 (save-selected-window
2106 (select-window (get-buffer-window "*Help*"))
2107 (if (eq this-command last-command)
2108 (if (< (window-end) (point-max))
2110 (if (> (window-start) (point-min))
2111 (set-window-start (selected-window) (point-min)))))
2113 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2114 "Type \\[quail-translation-help] to scroll up the help"
2115 (if (> (window-start) (point-min))
2116 "Type \\[quail-translation-help] to see the head of help"))))
2119 (message "%s" (substitute-command-keys scroll-help))
2122 (quail-update-guidance)
2125 ;; Quail map generator from state transition table.
2127 (defun quail-map-from-table (table)
2128 "Make quail map from state transition table TABLE.
2130 TABLE is an alist, the form is:
2131 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2133 STATE-n are symbols to denote state. STATE-0 is the initial state.
2135 TRANSITION-n-m are transition rules from STATE-n, and have the form
2136 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2137 RULES is a symbol whose value is an alist of keys \(string) vs the
2138 correponding characters or strings. The format of the symbol value of
2139 RULES is the same as arguments to `quail-define-rules'.
2141 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2142 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2143 transition is allowed, i.e. STATE-x may be STATE-n.
2145 If TRANSITION-n-m has the form RULES, the transition terminates
2146 when keys in RULES are input.
2148 The generated map can be set for the current Quail package by the
2149 function `quail-install-map' (which see)."
2150 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2152 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2153 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2154 ;; Set key sequence mapping rules in cdr part of each element.
2156 (quail-map-from-table-1 state-alist (car table))
2157 (setq table (cdr table)))
2159 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2160 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2161 ;; them with MAPPING-RULES of STATE-x to make elements of
2162 ;; STATE-ALIST valid Quail maps.
2163 (setq tail state-alist)
2165 (setq elt (car tail) tail (cdr tail))
2166 (quail-map-from-table-2 state-alist elt))
2168 ;; Return the Quail map for the initial state.
2171 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2172 ;; mapping rules in the element of STATE-ALIST that corresponds to
2173 ;; STATE according to TRANSITION ...
2174 (defun quail-map-from-table-1 (state-alist state-info)
2175 (let* ((state (car state-info))
2176 (map (assq state state-alist))
2177 (transitions (cdr state-info))
2180 (setq elt (car transitions) transitions (cdr transitions))
2181 (let (rules dst-state key trans)
2182 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2183 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2185 (setq rules (symbol-value (car elt))
2186 ;; Set (STATE-x) as branches for all keys in RULES.
2187 ;; It is replaced with actual branches for STATE-x
2188 ;; later in `quail-map-from-table-2'.
2189 dst-state (list (cdr elt)))
2190 (setq rules (symbol-value elt)))
2192 (setq key (car (car rules)) trans (cdr (car rules))
2195 (if (= (length trans) 1)
2196 (setq trans (aref trans 0))
2197 (setq trans (string-to-vector trans))))
2198 (set-nested-alist key trans map nil dst-state))))))
2200 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2202 ;; (STATE (CHAR NESTED-ALIST) ...)
2203 ;; NESTED-ALIST is a nested alist; the form is:
2204 ;; (TRANS (CHAR NESTED-ALIST) ...)
2206 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2207 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2208 ;; (cdr (assq STATE-x STATE-ALIST))
2210 (defun quail-map-from-table-2 (state-alist element)
2211 (let ((prev element)
2212 (tail (cdr element))
2215 (setq elt (car tail) prev tail tail (cdr tail))
2216 (quail-map-from-table-2 state-alist (cdr elt)))
2217 (setq elt (car tail))
2219 (quail-map-from-table-2 state-alist (cdr elt))
2220 (setcdr prev (cdr (assq elt state-alist))))))
2222 ;; Concatenate translations for all heading substrings of KEY in the
2223 ;; current Quail map. Here, `heading substring' means (substring KEY
2224 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2225 (defun quail-lookup-map-and-concat (key)
2226 (let* ((len (length key))
2227 (translation-list nil)
2230 (setq map (quail-lookup-key key len)
2233 (let* ((def (quail-map-definition map))
2234 (trans (if (consp def) (aref (cdr def) (car (car def)))
2236 (if (integerp trans)
2237 (setq trans (char-to-string trans)))
2238 (setq translation-list (cons trans translation-list)))))
2239 (apply 'concat translation-list)))
2242 (defvar quail-directory-name "quail"
2243 "Name of Quail directory which contains Quail packages.
2244 This is a sub-directory of LEIM directory.")
2247 (defun quail-update-leim-list-file (dirname &rest dirnames)
2248 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2249 DIRNAME is a directory containing Emacs input methods;
2250 normally, it should specify the `leim' subdirectory
2251 of the Emacs source tree.
2253 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2254 and update the file \"leim-list.el\" in DIRNAME.
2256 When called from a program, the remaining arguments are additional
2257 directory names to search for Quail packages under `quail' subdirectory
2259 (interactive "FDirectory of LEIM: ")
2260 (setq dirname (expand-file-name dirname))
2261 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2262 quail-dirs list-buf pkg-list pkg-buf pos)
2263 (if (not (file-writable-p leim-list))
2264 (error "Can't write to file \"%s\"" leim-list))
2265 (message "Updating %s ..." leim-list)
2266 (setq list-buf (find-file-noselect leim-list))
2268 ;; At first, clean up the file.
2270 (set-buffer list-buf)
2273 ;; Insert the correct header.
2274 (if (looking-at (regexp-quote leim-list-header))
2275 (goto-char (match-end 0))
2276 (insert leim-list-header))
2278 (if (not (re-search-forward leim-list-entry-regexp nil t))
2281 ;; Remove garbages after the header.
2282 (goto-char (match-beginning 0))
2284 (delete-region pos (point)))
2286 ;; Remove all entries for Quail.
2287 (while (re-search-forward leim-list-entry-regexp nil 'move)
2288 (goto-char (match-beginning 0))
2291 (let ((form (read list-buf)))
2292 (when (equal (nth 3 form) ''quail-use-package)
2293 (if (eolp) (forward-line 1))
2294 (delete-region pos (point))))
2296 ;; Delete the remaining contents because it seems that
2297 ;; this file is broken.
2298 (message "Garbage in %s deleted" leim-list)
2299 (delete-region pos (point-max)))))))
2301 ;; Search for `quail' subdirectory under each DIRNAMES.
2302 (setq dirnames (cons dirname dirnames))
2305 (setcar l (expand-file-name (car l)))
2306 (setq dirname (expand-file-name quail-directory-name (car l)))
2307 (if (file-readable-p dirname)
2308 (setq quail-dirs (cons dirname quail-dirs))
2309 (message "%s doesn't have `%s' subdirectory, just ignored"
2310 (car l) quail-directory-name)
2311 (setq quail-dirs (cons nil quail-dirs)))
2313 (setq quail-dirs (nreverse quail-dirs)))
2315 ;; Insert input method registering forms.
2317 (setq dirname (car quail-dirs))
2319 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2321 (message "Checking %s ..." (car pkg-list))
2323 (insert-file-contents (car pkg-list))
2324 (goto-char (point-min))
2325 (while (search-forward "(quail-define-package" nil t)
2326 (goto-char (match-beginning 0))
2328 (let ((form (read (current-buffer))))
2330 (set-buffer list-buf)
2332 (format "(register-input-method
2336 (nth 1 form) ; PACKAGE-NAME
2337 (nth 2 form) ; LANGUAGE
2338 'quail-use-package ; ACTIVATE-FUNC
2339 (nth 3 form) ; PACKAGE-TITLE
2340 (progn ; PACKAGE-DESCRIPTION (one line)
2341 (string-match ".*" (nth 5 form))
2342 (match-string 0 (nth 5 form)))
2343 (file-relative-name ; PACKAGE-FILENAME
2344 (file-name-sans-extension (car pkg-list))
2347 ;; Ignore the remaining contents of this file.
2348 (goto-char (point-max))
2349 (message "Some part of \"%s\" is broken" dirname)))))
2350 (setq pkg-list (cdr pkg-list)))
2351 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2353 ;; At last, write out LEIM list file.
2355 (set-buffer list-buf)
2356 (setq buffer-file-coding-system 'iso-2022-7bit)
2358 (kill-buffer list-buf)
2359 (message "Updating %s ... done" leim-list)))
2361 (defun quail-advice (args)
2362 "Advice users about the characters input by the current Quail package.
2363 The argument is a parameterized event of the form:
2364 (quail-advice STRING)
2365 where STRING is a string containing the input characters.
2366 If STRING has property `advice' and the value is a function,
2367 call it with one argument STRING."
2369 (let* ((string (nth 1 args))
2370 (func (get-text-property 0 'advice string)))
2371 (if (functionp func)
2372 (funcall func string))))
2374 (global-set-key [quail-advice] 'quail-advice)
2379 ;;; quail.el ends here