1 ;;; quail.el --- provides simple input method for multilingual text
3 ;; Copyright (C) 1995, 2000 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2001 Free Software Foundation, Inc.
7 ;; Author: Kenichi HANDA <handa@etl.go.jp>
8 ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
9 ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
10 ;; Keywords: mule, multilingual, input method
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; In Quail minor mode, you can input multilingual text easily. By
32 ;; defining a translation table (named Quail map) which maps ASCII key
33 ;; string to multilingual character or string, you can input any text
34 ;; from ASCII keyboard.
36 ;; We use words "translation" and "conversion" differently. The
37 ;; former is done by Quail package itself, the latter is the further
38 ;; process of converting a translated text to some more desirable
39 ;; text. For instance, Quail package for Japanese (`quail-jp')
40 ;; translates Roman text (transliteration of Japanese in Latin
41 ;; alphabets) to Hiragana text, which is then converted to
42 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
43 ;; CONVERSION-KEYS argument of the Quail package.
48 "Quail: multilingual input method."
51 ;; Buffer local variables
53 (defvar quail-current-package nil
54 "The current Quail package, which depends on the current input method.
55 See the documentation of `quail-package-alist' for the format.")
56 (make-variable-buffer-local 'quail-current-package)
57 (put 'quail-current-package 'permanent-local t)
59 ;; Quail uses the following two buffers to assist users.
60 ;; A buffer to show available key sequence or translation list.
61 (defvar quail-guidance-buf nil)
62 ;; A buffer to show completion list of the current key sequence.
63 (defvar quail-completion-buf nil)
65 ;; Each buffer in which Quail is activated should use different
67 (make-variable-buffer-local 'quail-guidance-buf)
68 (put 'quail-guidance-buf 'permanent-local t)
70 ;; A main window showing Quail guidance buffer.
71 (defvar quail-guidance-win nil)
72 (make-variable-buffer-local 'quail-guidance-win)
74 (defvar quail-overlay nil
75 "Overlay which covers the current translation region of Quail.")
76 (make-variable-buffer-local 'quail-overlay)
78 (defvar quail-conv-overlay nil
79 "Overlay which covers the text to be converted in Quail mode.")
80 (make-variable-buffer-local 'quail-conv-overlay)
82 (defvar quail-current-key nil
83 "Current key for translation in Quail mode.")
84 (make-variable-buffer-local 'quail-current-key)
86 (defvar quail-current-str nil
87 "Currently selected translation of the current key.")
88 (make-variable-buffer-local 'quail-current-str)
90 (defvar quail-current-translations nil
91 "Cons of indices and vector of possible translations of the current key.
92 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
93 CURRENT is an index of the current translation,
94 START and END are indices of the start and end of the current block,
95 BLOCK is the current block index,
96 BLOCKS is a number of blocks of translation.")
97 (make-variable-buffer-local 'quail-current-translations)
99 (defvar quail-current-data nil
100 "Any Lisp object holding information of current translation status.
101 When a key sequence is mapped to TRANS and TRANS is a cons
102 of actual translation and some Lisp object to be referred
103 for translating the longer key sequence, this variable is set
104 to that Lisp object.")
105 (make-variable-buffer-local 'quail-current-data)
107 ;; Quail package handlers.
109 (defvar quail-package-alist nil
110 "List of Quail packages.
111 A Quail package is a list of these elements:
112 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
113 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
114 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
115 CONVERSION-KEYS, SIMPLE.
117 QUAIL-MAP is a data structure to map key strings to translations. For
118 the format, see the documentation of `quail-map-p'.
120 DECODE-MAP is an alist of translations and corresponding keys.
122 See the documentation of `quail-define-package' for the other elements.")
124 ;; Return various slots in the current quail-package.
126 (defsubst quail-name ()
127 "Return the name of the current Quail package."
128 (nth 0 quail-current-package))
130 (defun quail-title ()
131 "Return the title of the current Quail package."
132 (let ((title (nth 1 quail-current-package)))
133 ;; TITLE may be a string or a list. If it is a list, each element
134 ;; is a string or the form (VAR STR1 STR2), and the interpretation
135 ;; of the list is the same as that of mode-line-format.
141 (cond ((stringp x) x)
142 ((and (listp x) (symbolp (car x)) (= (length x) 3))
143 (if (symbol-value (car x))
144 (nth 1 x) (nth 2 x)))
148 (defsubst quail-map ()
149 "Return the translation map of the current Quail package."
150 (nth 2 quail-current-package))
151 (defsubst quail-guidance ()
152 "Return an object used for `guidance' feature of the current Quail package.
153 See also the documentation of `quail-define-package'."
154 (nth 3 quail-current-package))
155 (defsubst quail-docstring ()
156 "Return the documentation string of the current Quail package."
157 (nth 4 quail-current-package))
158 (defsubst quail-translation-keymap ()
159 "Return translation keymap in the current Quail package.
160 Translation keymap is a keymap used while translation region is active."
161 (nth 5 quail-current-package))
162 (defsubst quail-forget-last-selection ()
163 "Return `forget-last-selection' flag of the current Quail package.
164 See also the documentation of `quail-define-package'."
165 (nth 6 quail-current-package))
166 (defsubst quail-deterministic ()
167 "Return `deterministic' flag of the current Quail package.
168 See also the documentation of `quail-define-package'."
169 (nth 7 quail-current-package))
170 (defsubst quail-kbd-translate ()
171 "Return `kbd-translate' flag of the current Quail package.
172 See also the documentation of `quail-define-package'."
173 (nth 8 quail-current-package))
174 (defsubst quail-show-layout ()
175 "Return `show-layout' flag of the current Quail package.
176 See also the documentation of `quail-define-package'."
177 (nth 9 quail-current-package))
178 (defsubst quail-decode-map ()
179 "Return decode map of the current Quail package.
180 It is an alist of translations and corresponding keys."
181 (nth 10 quail-current-package))
182 (defsubst quail-maximum-shortest ()
183 "Return `maximum-shortest' flag of the current Quail package.
184 See also the documentation of `quail-define-package'."
185 (nth 11 quail-current-package))
186 (defsubst quail-overlay-plist ()
187 "Return property list of an overly used in the current Quail package."
188 (nth 12 quail-current-package))
189 (defsubst quail-update-translation-function ()
190 "Return a function for updating translation in the current Quail package."
191 (nth 13 quail-current-package))
192 (defsubst quail-conversion-keymap ()
193 "Return conversion keymap in the current Quail package.
194 Conversion keymap is a keymap used while conversion region is active
195 but translation region is not active."
196 (nth 14 quail-current-package))
197 (defsubst quail-simple ()
198 "Return t if the current Quail package is simple."
199 (nth 15 quail-current-package))
201 (defsubst quail-package (name)
202 "Return Quail package named NAME."
203 (assoc name quail-package-alist))
205 (defun quail-add-package (package)
206 "Add Quail package PACKAGE to `quail-package-alist'."
207 (let ((pac (quail-package (car package))))
209 (setcdr pac (cdr package))
210 (setq quail-package-alist (cons package quail-package-alist)))))
212 (defun quail-select-package (name)
213 "Select Quail package named NAME as the current Quail package."
214 (let ((package (quail-package name)))
216 (error "No Quail package `%s'" name))
217 (setq quail-current-package package)
218 (setq-default quail-current-package package)
222 (defun quail-use-package (package-name &rest libraries)
223 "Start using Quail package PACKAGE-NAME.
224 The remaining arguments are libraries to be loaded before using the package.
226 This activates input method defined by PACKAGE-NAME by running
227 `quail-activate', which see."
228 (let ((package (quail-package package-name)))
230 ;; Perhaps we have not yet loaded necessary libraries.
232 (if (not (load (car libraries) t))
234 (with-output-to-temp-buffer "*Help*"
235 (princ "Quail package \"")
237 (princ "\" can't be activated\n because library \"")
238 (princ (car libraries))
239 (princ "\" is not in `load-path'.
241 The most common case is that you have not yet installed appropriate
242 libraries in LEIM (Libraries of Emacs Input Method) which is
243 distributed separately from Emacs.
245 LEIM is available from the same ftp directory as Emacs."))
246 (error "Can't use the Quail package `%s'" package-name))
247 (setq libraries (cdr libraries))))))
248 (quail-select-package package-name)
249 (setq current-input-method-title (quail-title))
251 ;; Hide all '... loaded' message.
254 (defvar quail-translation-keymap
255 (let ((map (make-keymap))
258 (define-key map (char-to-string i) 'quail-other-command)
261 (define-key map (char-to-string i) 'quail-self-insert-command)
265 (define-key map (vector i) 'quail-self-insert-command)
267 (define-key map "\177" 'quail-delete-last-char)
268 (define-key map "\C-f" 'quail-next-translation)
269 (define-key map "\C-b" 'quail-prev-translation)
270 (define-key map "\C-n" 'quail-next-translation-block)
271 (define-key map "\C-p" 'quail-prev-translation-block)
272 (define-key map [right] 'quail-next-translation)
273 (define-key map [left] 'quail-prev-translation)
274 (define-key map [down] 'quail-next-translation-block)
275 (define-key map [up] 'quail-prev-translation-block)
276 (define-key map "\C-i" 'quail-completion)
277 (define-key map "\C-@" 'quail-select-current)
278 ;; Following simple.el, Enter key on numeric keypad selects the
279 ;; current translation just like `C-SPC', and `mouse-2' chooses
280 ;; any completion visible in the *Quail Completions* buffer.
281 (define-key map [kp-enter] 'quail-select-current)
282 (define-key map [mouse-2] 'quail-mouse-choose-completion)
283 (define-key map [down-mouse-2] nil)
284 (define-key map "\C-h" 'quail-translation-help)
285 (define-key map [?\C- ] 'quail-select-current)
286 (define-key map [tab] 'quail-completion)
287 (define-key map [delete] 'quail-delete-last-char)
288 (define-key map [backspace] 'quail-delete-last-char)
290 "Keymap used processing translation in complex Quail modes.
291 Only a few especially complex input methods use this map;
292 most use `quail-simple-translation-keymap' instead.
293 This map is activated while translation region is active.")
295 (defvar quail-translation-docstring
296 "When you type keys, the echo area shows the possible characters
297 which correspond to that key sequence, each preceded by a digit. You
298 can select one of the characters shown by typing the corresponding
299 digit. Alternatively, you can use C-f and C-b to move through the
300 line to select the character you want, then type a letter to begin
301 entering another Chinese character or type a space or punctuation
304 If there are more than ten possible characters for the given spelling,
305 the echo area shows ten characters at a time; you can use C-n to move
306 to the next group of ten, and C-p to move back to the previous group
309 ;; Categorize each Quail commands to make the output of quail-help
310 ;; concise. This is done by putting `quail-help' property. The value
312 ;; hide -- never show this command
313 ;; non-deterministic -- show only for non-deterministic input method
314 (let ((l '((quail-other-command . hide)
315 (quail-self-insert-command . hide)
316 (quail-delete-last-char . hide)
317 (quail-next-translation . non-deterministic)
318 (quail-prev-translation . non-deterministic)
319 (quail-next-translation-block . non-deterministic)
320 (quail-prev-translation-block . non-deterministic))))
322 (put (car (car l)) 'quail-help (cdr (car l)))
325 (defvar quail-simple-translation-keymap
326 (let ((map (make-keymap))
329 (define-key map (char-to-string i) 'quail-other-command)
332 (define-key map (char-to-string i) 'quail-self-insert-command)
336 (define-key map (vector i) 'quail-self-insert-command)
338 (define-key map "\177" 'quail-delete-last-char)
339 (define-key map [delete] 'quail-delete-last-char)
340 (define-key map [backspace] 'quail-delete-last-char)
341 ;;(let ((meta-map (make-sparse-keymap)))
342 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
343 ;;(define-key map [escape] meta-map))
345 "Keymap used while processing translation in simple Quail modes.
346 A few especially complex input methods use `quail-translation-keymap' instead.
347 This map is activated while translation region is active.")
349 (defvar quail-conversion-keymap
350 (let ((map (make-keymap))
353 (define-key map (char-to-string i) 'quail-self-insert-command)
357 (define-key map (vector i) 'quail-self-insert-command)
359 (define-key map "\C-b" 'quail-conversion-backward-char)
360 (define-key map "\C-f" 'quail-conversion-forward-char)
361 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
362 (define-key map "\C-e" 'quail-conversion-end-of-region)
363 (define-key map "\C-d" 'quail-conversion-delete-char)
364 (define-key map "\C-k" 'quail-conversion-delete-tail)
365 (define-key map "\C-h" 'quail-translation-help)
366 (define-key map "\177" 'quail-conversion-backward-delete-char)
367 (define-key map [delete] 'quail-conversion-backward-delete-char)
368 (define-key map [backspace] 'quail-conversion-backward-delete-char)
370 "Keymap used for processing conversion in Quail mode.
371 This map is activated while conversion region is active but translation
372 region is not active.")
374 ;; Just a dummy definition.
375 (defun quail-other-command ()
380 (defun quail-define-package (name language title
381 &optional guidance docstring translation-keys
382 forget-last-selection deterministic
383 kbd-translate show-layout create-decode-map
384 maximum-shortest overlay-plist
385 update-translation-function
386 conversion-keys simple)
387 "Define NAME as a new Quail package for input LANGUAGE.
388 TITLE is a string to be displayed at mode-line to indicate this package.
389 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
390 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
391 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
392 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
394 GUIDANCE specifies how a guidance string is shown in echo area.
395 If it is t, list of all possible translations for the current key is shown
396 with the currently selected translation being highlighted.
397 If it is an alist, the element has the form (CHAR . STRING). Each character
398 in the current key is searched in the list and the corresponding string is
400 If it is nil, the current key is shown.
402 DOCSTRING is the documentation string of this package. The command
403 `describe-input-method' shows this string while replacing the form
404 \\=\\<VAR> in the string by the value of VAR. That value should be a
405 string. For instance, the form \\=\\<quail-translation-docstring> is
406 replaced by a description about how to select a translation from a
409 TRANSLATION-KEYS specifies additional key bindings used while translation
410 region is active. It is an alist of single key character vs. corresponding
411 command to be called.
413 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
414 for the future to translate the same key. If this flag is nil, a
415 translation selected for a key is remembered so that it can be the
416 first candidate when the same key is entered later.
418 DETERMINISTIC non-nil means the first candidate of translation is
419 selected automatically without allowing users to select another
420 translation for a key. In this case, unselected translations are of
421 no use for an interactive use of Quail but can be used by some other
422 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
425 KBD-TRANSLATE non-nil means input characters are translated from a
426 user's keyboard layout to the standard keyboard layout. See the
427 documentation of `quail-keyboard-layout' and
428 `quail-keyboard-layout-standard' for more detail.
430 SHOW-LAYOUT non-nil means the `quail-help' command should show
431 the user's keyboard layout visually with translated characters.
432 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
433 this package defines no translations for single character keys.
435 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
436 map is an alist of translations and corresponding original keys.
437 Although this map is not used by Quail itself, it can be used by some
438 other programs. For instance, Vietnamese supporting needs this map to
439 convert Vietnamese text to VIQR format which uses only ASCII
440 characters to represent Vietnamese characters.
442 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
443 length of the shortest sequence. When we don't have a translation of
444 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
445 the key at \"..AB\" and start translation of \"CD..\". Hangul
446 packages, for instance, use this facility. If this flag is nil, we
447 break the key just at \"..ABC\" and start translation of \"D..\".
449 OVERLAY-PLIST if non-nil is a property list put on an overlay which
450 covers Quail translation region.
452 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
453 the current translation region according to a new translation data. By
454 default, a translated text or a user's key sequence (if no translation
457 CONVERSION-KEYS specifies additional key bindings used while
458 conversion region is active. It is an alist of single key character
459 vs. corresponding command to be called.
461 If SIMPLE is non-nil, then we do not alter the meanings of
462 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
464 (let (translation-keymap conversion-keymap)
465 (if deterministic (setq forget-last-selection t))
468 (setq translation-keymap (copy-keymap
469 (if simple quail-simple-translation-keymap
470 quail-translation-keymap)))
471 (while translation-keys
472 (define-key translation-keymap
473 (car (car translation-keys)) (cdr (car translation-keys)))
474 (setq translation-keys (cdr translation-keys))))
475 (setq translation-keymap
476 (if simple quail-simple-translation-keymap
477 quail-translation-keymap)))
478 (when conversion-keys
479 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
480 (while conversion-keys
481 (define-key conversion-keymap
482 (car (car conversion-keys)) (cdr (car conversion-keys)))
483 (setq conversion-keys (cdr conversion-keys))))
485 (list name title (list nil) guidance (or docstring "")
487 forget-last-selection deterministic kbd-translate show-layout
488 (if create-decode-map (list 'decode-map) nil)
489 maximum-shortest overlay-plist update-translation-function
490 conversion-keymap simple))
492 ;; Update input-method-alist.
493 (let ((slot (assoc name input-method-alist))
494 (val (list language 'quail-use-package title docstring)))
495 (if slot (setcdr slot val)
496 (setq input-method-alist (cons (cons name val) input-method-alist)))))
498 (quail-select-package name))
500 ;; Quail minor mode handlers.
502 ;; Setup overlays used in Quail mode.
503 (defun quail-setup-overlays (conversion-mode)
505 (if (overlayp quail-overlay)
506 (move-overlay quail-overlay pos pos)
507 (setq quail-overlay (make-overlay pos pos nil nil t))
508 (if input-method-highlight-flag
509 (overlay-put quail-overlay 'face 'underline))
510 (let ((l (quail-overlay-plist)))
512 (overlay-put quail-overlay (car l) (car (cdr l)))
513 (setq l (cdr (cdr l))))))
515 (if (overlayp quail-conv-overlay)
516 (if (not (overlay-start quail-conv-overlay))
517 (move-overlay quail-conv-overlay pos pos))
518 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
519 (if input-method-highlight-flag
520 (overlay-put quail-conv-overlay 'face 'underline))))))
522 ;; Delete overlays used in Quail mode.
523 (defun quail-delete-overlays ()
524 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
525 (delete-overlay quail-overlay))
526 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
527 (delete-overlay quail-conv-overlay)))
529 ;; Kill Quail guidance buffer. Set in kill-buffer-hook.
530 (defun quail-kill-guidance-buf ()
531 (if (buffer-live-p quail-guidance-buf)
532 (kill-buffer quail-guidance-buf)))
534 (defun quail-inactivate ()
535 "Inactivate Quail input method.
537 This function runs the normal hook `quail-inactivate-hook'."
541 (defun quail-activate (&optional arg)
542 "Activate Quail input method.
543 With arg, activate Quail input method if and only if arg is positive.
545 This function runs `quail-activate-hook' if it activates the input
546 method, `quail-inactivate-hook' if it deactivates it.
548 While this input method is active, the variable
549 `input-method-function' is bound to the function `quail-input-method'."
551 (< (prefix-numeric-value arg) 0))
552 ;; Let's inactivate Quail input method.
555 (quail-hide-guidance-buf)
556 (quail-delete-overlays)
557 (setq describe-current-input-method-function nil)
558 (run-hooks 'quail-inactivate-hook))
559 (kill-local-variable 'input-method-function))
560 ;; Let's activate Quail input method.
561 (if (null quail-current-package)
562 ;; Quail package is not yet selected. Select one now.
564 (if quail-package-alist
565 (setq name (car (car quail-package-alist)))
566 (error "No Quail package loaded"))
567 (quail-select-package name)))
568 (setq inactivate-current-input-method-function 'quail-inactivate)
569 (setq describe-current-input-method-function 'quail-help)
570 (quail-delete-overlays)
571 (quail-show-guidance-buf)
572 ;; If we are in minibuffer, turn off the current input method
574 (if (eq (selected-window) (minibuffer-window))
575 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
576 (add-hook 'kill-buffer-hook 'quail-kill-guidance-buf nil t)
577 (run-hooks 'quail-activate-hook)
578 (make-local-variable 'input-method-function)
579 (setq input-method-function 'quail-input-method)))
581 (defun quail-exit-from-minibuffer ()
582 (inactivate-input-method)
583 (if (<= (minibuffer-depth) 1)
584 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
586 ;; Keyboard layout translation handlers.
588 ;; Some Quail packages provide localized keyboard simulation which
589 ;; requires a particular keyboard layout. In this case, what we need
590 ;; is locations of keys the user entered, not character codes
591 ;; generated by those keys. However, for the moment, there's no
592 ;; common way to get such information. So, we ask a user to give
593 ;; information of his own keyboard layout, then translate it to the
594 ;; standard layout which we defined so that all Quail packages depend
597 (defconst quail-keyboard-layout-standard
600 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
601 qQwWeErRtTyYuUiIoOpP[{]} \
602 aAsSdDfFgGhHjJkKlL;:'\"\\| \
603 zZxXcCvVbBnNmM,<.>/? \
605 "Standard keyboard layout of printable characters Quail assumes.
606 See the documentation of `quail-keyboard-layout' for this format.
607 This layout is almost the same as that of VT100,
608 but the location of key \\ (backslash) is just right of key ' (single-quote),
609 not right of RETURN key.")
611 (defvar quail-keyboard-layout quail-keyboard-layout-standard
612 "A string which represents physical key layout of a particular keyboard.
613 We assume there are six rows and each row has 15 keys (columns),
614 the first row is above the `1' - `0' row,
615 the first column of the second row is left of key `1',
616 the first column of the third row is left of key `q',
617 the first column of the fourth row is left of key `a',
618 the first column of the fifth row is left of key `z',
619 the sixth row is below the `z' - `/' row.
620 Nth (N is even) and (N+1)th characters in the string are non-shifted
621 and shifted characters respectively at the same location.
622 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
623 The command `quail-set-keyboard-layout' usually sets this variable.")
625 (defconst quail-keyboard-layout-len 180)
627 ;; Here we provide several examples of famous keyboard layouts.
629 (defvar quail-keyboard-layout-alist
631 (cons "standard" quail-keyboard-layout-standard)
634 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
635 qQwWeErRtTyYuUiIoOpP[{]} \
636 aAsSdDfFgGhHjJkKlL;:'\" \
637 zZxXcCvVbBnNmM,<.>/? \
639 '("atari-german" . "\
641 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
642 qQwWeErRtTzZuUiIoOpP\374\334+* \
643 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
644 <>yYxXcCvVbBnNmM,;.:-_ \
649 ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
650 qQwWeErRtTzZuUiIoOpP\374\334+* \
651 aAsSdDfFgGhHjJkKlL\366\326\344\304 \
652 <>yYxXcCvVbBnNmM,;.:-_ \
657 1!2\"3#4$5%6&7'8(9)0~-=^~\\| \
658 qQwWeErRtTyYuUiIoOpP@`[{ \
659 aAsSdDfFgGhHjJkKlL;+:*]} \
660 zZxXcCvVbBnNmM,<.>/?\\_ \
663 "Alist of keyboard names and corresponding layout strings.
664 See the documentation of `quail-keyboard-layout' for the format of
667 ;; A non-standard keyboard layout may miss some key locations of the
668 ;; standard layout while having additional key locations not in the
669 ;; standard layout. This alist maps those additional key locations to
670 ;; the missing locations. The value is updated automatically by
671 ;; quail-set-keyboard-layout.
672 (defvar quail-keyboard-layout-substitution nil)
674 (defun quail-update-keyboard-layout (kbd-type)
675 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
677 ;; Here, we had better ask a user to define his own keyboard
678 ;; layout interactively.
679 (error "Unknown keyboard type `%s'" kbd-type))
680 (setq quail-keyboard-layout (cdr layout))
681 (let ((i quail-keyboard-layout-len)
682 subst-list missing-list)
683 ;; Sum up additional key locations not in the standard layout in
684 ;; subst-list, and missing key locations in missing-list.
687 (if (= (aref quail-keyboard-layout i) ? )
688 (if (/= (aref quail-keyboard-layout-standard i) ? )
689 (setq missing-list (cons i missing-list)))
690 (if (= (aref quail-keyboard-layout-standard i) ? )
691 (setq subst-list (cons (cons i nil) subst-list)))))
692 (setq quail-keyboard-layout-substitution subst-list)
693 ;; If there are additional key locations, map them to missing
696 (while (and subst-list (cdr (car subst-list)))
697 (setq subst-list (cdr subst-list)))
699 (setcdr (car subst-list) (car missing-list)))
700 (setq missing-list (cdr missing-list))))))
702 (defcustom quail-keyboard-layout-type "standard"
703 "Type of keyboard layout used in Quail base input method.
704 Available types are listed in the variable `quail-keyboard-layout-alist'."
707 :set #'(lambda (symbol value)
708 (quail-update-keyboard-layout value)
712 (defun quail-set-keyboard-layout (kbd-type)
713 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
715 Since some Quail packages depends on a physical layout of keys (not
716 characters generated by them), those are created by assuming the
717 standard layout defined in `quail-keyboard-layout-standard'. This
718 function tells Quail system the layout of your keyboard so that what
719 you type is correctly handled."
721 (let* ((completion-ignore-case t)
722 (type (completing-read "Keyboard type: "
723 quail-keyboard-layout-alist)))
725 (quail-update-keyboard-layout kbd-type)
726 (setq quail-keyboard-layout-type kbd-type))
728 (defun quail-keyboard-translate (char)
729 "Translate CHAR to the one in the standard keyboard layout."
730 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
731 ;; All Quail packages are designed based on
732 ;; `quail-keyboard-layout-standard'.
735 ;; Find the key location on the current keyboard layout.
736 (while (and (< i quail-keyboard-layout-len)
737 (/= char (aref quail-keyboard-layout i)))
739 (if (= i quail-keyboard-layout-len)
740 ;; CHAR is not in quail-keyboard-layout, which means that a
741 ;; user typed a key which generated a character code to be
742 ;; handled out of Quail. Just return CHAR and make
743 ;; quail-execute-non-quail-command handle it correctly.
745 (let ((ch (aref quail-keyboard-layout-standard i)))
747 ;; This location not available in the standard keyboard
748 ;; layout. Check if the location is used to substitute
749 ;; for the other location of the standard layout.
750 (if (setq i (cdr (assq i quail-keyboard-layout-substitution)))
751 (aref quail-keyboard-layout-standard i)
752 ;; Just return CHAR as well as above.
756 (defun quail-keyseq-translate (keyseq)
758 (mapcar (function (lambda (x) (quail-keyboard-translate x)))
761 (defun quail-insert-kbd-layout (kbd-layout)
762 "Insert the visual keyboard layout table according to KBD-LAYOUT.
763 The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
764 (let (done-list layout i ch)
765 ;; At first, convert KBD-LAYOUT to the same size vector that
766 ;; contains translated character or string.
767 (setq layout (string-to-vector kbd-layout)
769 (while (< i quail-keyboard-layout-len)
770 (setq ch (aref kbd-layout i))
771 (if (quail-kbd-translate)
772 (setq ch (quail-keyboard-translate ch)))
773 (let* ((map (cdr (assq ch (cdr (quail-map)))))
774 (translation (and map (quail-get-translation
775 (car map) (char-to-string ch) 1))))
778 (if (consp translation)
779 (setq translation (aref (cdr translation) 0)))
780 (setq done-list (cons translation done-list)))
781 (setq translation ch))
782 (aset layout i translation))
788 ;; Make table without horizontal lines. Each column for a key
789 ;; has the form "| LU |" where L is for lower key and and U is
790 ;; for a upper key. If width of L (U) is greater than 1,
791 ;; preceding (following) space is not inserted.
792 (put-text-property 0 1 'face 'bold bar)
794 (while (< i quail-keyboard-layout-len)
798 (insert-char 32 (+ row (/ (- row 2) 2)))))
799 (setq lower (aref layout i)
800 upper (aref layout (1+ i)))
801 (if (and (integerp lower) (>= lower 128) (< lower 256))
802 (setq lower (unibyte-char-to-multibyte lower)))
803 (if (and (integerp upper) (>= upper 128) (< upper 256))
804 (setq upper (unibyte-char-to-multibyte upper)))
806 (if (= (if (stringp lower) (string-width lower) (char-width lower)) 1)
809 (if (= (if (stringp upper) (string-width upper) (char-width upper)) 1)
814 ;; Insert horizontal lines while deleting blank key columns at the
815 ;; beginning and end of each line.
817 (narrow-to-region pos (point))
819 ;;(while (looking-at "[| ]*$")
821 ;;(delete-region pos (point)))
822 (let ((from1 100) (to1 0) from2 to2)
824 (if (looking-at "[| ]*$")
825 ;; The entire row is blank.
826 (delete-region (point) (match-end 0))
827 ;; Delete blank key columns at the head.
828 (if (looking-at " *\\(| \\)+")
829 (subst-char-in-region (point) (match-end 0) ?| ? ))
830 ;; Delete blank key columns at the tail.
831 (if (re-search-forward "\\( |\\)+$" (line-end-position) t)
832 (delete-region (match-beginning 0) (point)))
834 ;; Calculate the start and end columns of a horizontal line.
836 (setq from2 from1 to2 to1)
837 (skip-chars-forward " ")
838 (setq from2 (current-column))
840 (setq to2 (current-column))
846 ;; If the previous or the current line has at least one key
847 ;; column, insert a horizontal line.
849 (insert-char 32 from1)
852 (insert-char ?- (- (- to1 from1) 2))
854 (put-text-property pos (point) 'face 'bold)
856 (setq from1 from2 to1 to2)
858 ;; Insert "space bar" box.
862 " +-----------------------------+
864 +-----------------------------+
866 (put-text-property pos (point) 'face 'bold)
872 (defun quail-show-keyboard-layout (&optional keyboard-type)
873 "Show the physical layout of the keyboard type KEYBOARD-TYPE.
875 The variable `quail-keyboard-layout-type' holds the currently selected
878 (list (completing-read "Keyboard type (default, current choise): "
879 quail-keyboard-layout-alist
881 (or (and keyboard-type (> (length keyboard-type) 0))
882 (setq keyboard-type quail-keyboard-layout-type))
883 (let ((layout (assoc keyboard-type quail-keyboard-layout-alist)))
885 (error "Unknown keyboard type: %s" keyboard-type))
886 (with-output-to-temp-buffer "*Help*"
887 (with-current-buffer standard-output
888 (insert "Keyboard layout (keyboard type: "
891 (quail-insert-kbd-layout (cdr layout))))))
895 (defsubst quail-map-p (object)
896 "Return t if OBJECT is a Quail map.
898 A Quail map holds information how a particular key should be translated.
899 Its format is (TRANSLATION . ALIST).
900 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
901 In the latter case, each element of VECTOR is a candidate for the translation,
902 and INDEX points the currently selected translation.
904 ALIST is normally a list of elements that look like (CHAR . DEFN),
905 where DEFN is another Quail map for a longer key (CHAR added to the
906 current key). It may also be a symbol of a function which returns an
907 alist of the above format.
909 Just after a Quail package is read, TRANSLATION may be a string or a
910 vector. Then each element of the string or vector is a candidate for
911 the translation. These objects are transformed to cons cells in the
912 format \(INDEX . VECTOR), as described above."
914 (let ((translation (car object)))
915 (or (integerp translation) (null translation)
916 (vectorp translation) (stringp translation)
917 (symbolp translation)
918 (and (consp translation) (not (vectorp (cdr translation))))))
919 (let ((alist (cdr object)))
920 (or (and (listp alist) (consp (car alist)))
924 (defmacro quail-define-rules (&rest rules)
925 "Define translation rules of the current Quail package.
926 Each argument is a list of KEY and TRANSLATION.
927 KEY is a string meaning a sequence of keystrokes to be translated.
928 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
929 If it is a character, it is the sole translation of KEY.
930 If it is a string, each character is a candidate for the translation.
931 If it is a vector, each element (string or character) is a candidate
933 In these cases, a key specific Quail map is generated and assigned to KEY.
935 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
936 it is used to handle KEY.
938 The first argument may be an alist of annotations for the following
939 rules. Each element has the form (ANNOTATION . VALUE), where
940 ANNOTATION is a symbol indicating the annotation type. Currently
941 the following annotation types are supported.
943 append -- the value non-nil means that the following rules should
944 be appended to the rules of the current Quail package.
946 face -- the value is a face to use for displaying TRANSLATIONs in
949 advice -- the value is a function to call after one of RULES is
950 selected. The function is called with one argument, the
951 selected TRANSLATION string, after the TRANSLATION is
954 no-decode-map --- the value non-nil means that decoding map is not
955 generated for the following translations."
957 append no-decode-map props)
958 ;; If the first argument is an alist of annotations, handle them.
959 (if (consp (car (car l)))
960 (let ((annotations (car l)))
961 (setq append (assq 'append annotations))
963 (setq annotations (delete append annotations)
964 append (cdr append)))
965 (setq no-decode-map (assq 'no-decode-map annotations))
967 (setq annotations (delete no-decode-map annotations)
968 no-decode-map (cdr no-decode-map)))
969 ;; Convert the remaining annoations to property list PROPS.
972 (cons (car (car annotations))
973 (cons (cdr (car annotations))
975 annotations (cdr annotations)))
977 ;; Process the remaining arguments one by one.
979 ;; There's no way to add new rules at compiling time.
982 (decode-map (and (quail-decode-map) (not ,no-decode-map)))
986 (setq key (car (car tail)) trans (car (cdr (car tail)))
988 (quail-defrule-internal key trans map t decode-map properties)))
989 ;; We can build up quail map and decode map at compiling time.
990 (let ((map (list nil))
991 (decode-map (if (not no-decode-map) (list 'decode-map)))
994 (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
995 (quail-defrule-internal key trans map t decode-map props))
996 `(if (not (quail-decode-map))
997 (quail-install-map ',map)
998 (quail-install-map ',map)
999 (quail-install-decode-map ',decode-map))))))
1002 (defun quail-install-map (map &optional name)
1003 "Install the Quail map MAP in the current Quail package.
1005 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1006 which to install MAP.
1008 The installed map can be referred by the function `quail-map'."
1009 (if (null quail-current-package)
1010 (error "No current Quail package"))
1011 (if (null (quail-map-p map))
1012 (error "Invalid Quail map `%s'" map))
1013 (setcar (cdr (cdr quail-current-package)) map))
1016 (defun quail-install-decode-map (decode-map &optional name)
1017 "Install the Quail decode map DECODE-MAP in the current Quail package.
1019 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1020 which to install MAP.
1022 The installed decode map can be referred by the function `quail-decode-map'."
1023 (if (null quail-current-package)
1024 (error "No current Quail package"))
1025 (if (not (and (consp decode-map) (eq (car decode-map) 'decode-map)))
1026 (error "Invalid Quail decode map `%s'" decode-map))
1027 (setcar (nthcdr 10 quail-current-package) decode-map))
1030 (defun quail-defrule (key translation &optional name append)
1031 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
1032 KEY is a string meaning a sequence of keystrokes to be translated.
1033 TRANSLATION is a character, a string, a vector, a Quail map,
1034 a function, or a cons.
1035 It it is a character, it is the sole translation of KEY.
1036 If it is a string, each character is a candidate for the translation.
1037 If it is a vector, each element (string or character) is a candidate
1038 for the translation.
1039 If it is a cons, the car is one of the above and the cdr is a function
1040 to call when translating KEY (the return value is assigned to the
1041 variable `quail-current-data'). If the cdr part is not a function,
1042 the value itself is assigned to `quail-current-data'.
1043 In these cases, a key specific Quail map is generated and assigned to KEY.
1045 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
1046 it is used to handle KEY.
1048 Optional 3rd argument NAME, if specified, says which Quail package
1049 to define this translation rule in. The default is to define it in the
1050 current Quail package.
1052 Optional 4th argument APPEND, if non-nil, appends TRANSLATION
1053 to the current translations for KEY instead of replacing them."
1055 (let ((package (quail-package name)))
1057 (error "No Quail package `%s'" name))
1058 (setq quail-current-package package)))
1059 (quail-defrule-internal key translation (quail-map) append))
1062 (defun quail-defrule-internal (key trans map &optional append decode-map props)
1063 "Define KEY as TRANS in a Quail map MAP.
1065 If Optional 4th arg APPEND is non-nil, TRANS is appended to the
1066 current translations for KEY instead of replacing them.
1068 Optional 5th arg DECODE-MAP is a Quail decode map.
1070 Optional 6th arg PROPS is a property list annotating TRANS. See the
1071 function `quail-define-rules' for the detail."
1072 (if (null (stringp key))
1073 "Invalid Quail key `%s'" key)
1074 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
1077 (quail-map-p trans)))
1078 (error "Invalid Quail translation `%s'" trans))
1079 (if (null (quail-map-p map))
1080 (error "Invalid Quail map `%s'" map))
1081 (let ((len (length key))
1084 ;; Make a map for registering TRANS if necessary.
1086 (if (null (consp map))
1087 ;; We come here, for example, when we try to define a rule
1088 ;; for "ABC" but a rule for "AB" is already defined as a
1090 (error "Quail key %s is too long" key))
1091 (setq ch (aref key idx)
1092 entry (assq ch (cdr map)))
1095 (setq entry (cons ch (list nil)))
1096 (setcdr map (cons entry (cdr map)))))
1097 (setq map (cdr entry))
1098 (setq idx (1+ idx)))
1101 ;; We come here, for example, when we try to define a rule
1102 ;; for "AB" as a symbol but a rule for "ABC" is already
1104 (error "Quail key %s is too short" key)
1105 (setcdr entry trans))
1106 (if (quail-map-p trans)
1107 (if (not (listp (cdr map)))
1108 ;; We come here, for example, when we try to define a rule
1109 ;; for "AB" as a symbol but a rule for "ABC" is already
1111 (error "Quail key %s is too short" key)
1112 (if (not (listp (cdr trans)))
1114 ;; We come here, for example, when we try to
1115 ;; define a rule for "AB" as a symbol but a rule
1116 ;; for "ABC" is already defined.
1117 (error "Quail key %s is too short" key)
1118 (setcdr entry trans))
1119 (setcdr entry (append trans (cdr map)))))
1120 ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS
1121 ;; to a vector of strings, add PROPS to each string and record
1122 ;; this rule in DECODE-MAP.
1123 (when (and (or props decode-map)
1124 (not (consp trans)) (not (symbolp trans)))
1125 (if (integerp trans)
1126 (setq trans (vector trans))
1128 (setq trans (string-to-vector trans))))
1129 (let ((len (length trans))
1133 (setq elt (aref trans len))
1135 (setq elt (char-to-string elt)))
1136 (aset trans len elt)
1138 (add-text-properties 0 (length elt) props elt))
1141 (cons (cons elt key) (cdr decode-map)))))))
1142 (if (and (car map) append)
1143 (let ((prev (quail-get-translation (car map) key len)))
1145 (setq prev (vector prev))
1146 (setq prev (cdr prev)))
1147 (if (integerp trans)
1148 (setq trans (vector trans))
1150 (setq trans (string-to-vector trans))))
1152 (cons (list 0 0 0 0 nil)
1153 (vconcat prev trans)))))
1154 (setcar map trans)))))
1156 (defun quail-get-translation (def key len)
1157 "Return the translation specified as DEF for KEY of length LEN.
1158 The translation is either a character or a cons of the form (INDEX . VECTOR),
1159 where VECTOR is a vector of candidates (character or string) for
1160 the translation, and INDEX points into VECTOR to specify the currently
1161 selected translation."
1162 (if (and def (symbolp def))
1164 ;; DEF is a symbol of a function which returns valid translation.
1165 (setq def (funcall def key len))
1167 (if (and (consp def) (not (vectorp (cdr def))))
1168 (setq def (car def)))
1171 ((or (integerp def) (consp def))
1179 ;; If the length is 1, we don't need vector but a single candidate
1180 ;; as the translation.
1181 (if (= (length def) 1)
1183 ;; Each character in DEF is a candidate of translation. Reform
1184 ;; it as (INDICES . VECTOR).
1185 (cons (list 0 0 0 0 nil) (string-to-vector def))))
1188 ;; If the length is 1, and the length of element string is 1, we
1189 ;; don't need vector but a single candidate as the translation.
1190 (if (and (= (length def) 1)
1191 (= (length (aref def 0)) 1))
1192 (aref (aref def 0) 0)
1193 ;; Each element (string or character) in DEF is a candidate of
1194 ;; translation. Reform it as (INDICES . VECTOR).
1195 (cons (list 0 0 0 0 nil) def)))
1198 (error "Invalid object in Quail map: %s" def))))
1200 (defun quail-lookup-key (key &optional len)
1201 "Lookup KEY of length LEN in the current Quail map and return the definition.
1202 The returned value is a Quail map specific to KEY."
1204 (setq len (length key)))
1207 (kbd-translate (quail-kbd-translate))
1208 slot ch translation def)
1209 (while (and map (< idx len))
1210 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
1213 (if (and (cdr map) (symbolp (cdr map)))
1214 (setcdr map (funcall (cdr map) key idx)))
1215 (setq slot (assq ch (cdr map)))
1216 (if (and (cdr slot) (symbolp (cdr slot)))
1217 (setcdr slot (funcall (cdr slot) key idx)))
1218 (setq map (cdr slot)))
1219 (setq def (car map))
1220 (setq quail-current-translations nil)
1221 (if (and map (setq translation (quail-get-translation def key len)))
1223 (if (and (consp def) (not (vectorp (cdr def))))
1225 (if (not (equal (car def) translation))
1226 ;; We must reflect TRANSLATION to car part of DEF.
1227 (setcar def translation))
1228 (setq quail-current-data
1229 (if (functionp (cdr def))
1232 (if (not (equal def translation))
1233 ;; We must reflect TRANSLATION to car part of MAP.
1234 (setcar map translation)))
1235 (if (and (consp translation) (vectorp (cdr translation)))
1237 (setq quail-current-translations translation)
1238 (if (quail-forget-last-selection)
1239 (setcar (car quail-current-translations) 0))))))
1240 ;; We may have to reform cdr part of MAP.
1241 (if (and (cdr map) (functionp (cdr map)))
1242 (setcdr map (funcall (cdr map) key len)))
1245 (put 'quail-error 'error-conditions '(quail-error error))
1246 (defun quail-error (&rest args)
1247 (signal 'quail-error (apply 'format args)))
1250 (defun quail-input-string-to-events (str)
1251 "Convert input string STR to a list of events.
1252 Do so while interleaving with the following special events:
1253 \(compose-last-chars LEN COMPONENTS)
1254 \(quail-advice INPUT-STRING)"
1255 (let* ((events (string-to-list str))
1258 composition from to)
1259 (while (and (> idx 0)
1260 (setq composition (find-composition idx 0 str t)))
1261 (setq from (car composition) to (nth 1 composition))
1262 (setcdr (nthcdr (1- to) events)
1263 (cons (list 'compose-last-chars (- to from)
1264 (and (not (nth 3 composition)) (nth 2 composition)))
1265 (nthcdr to events)))
1266 (setq idx (1- from)))
1267 (if (or (get-text-property 0 'advice str)
1268 (next-single-property-change 0 'advice str))
1270 (nconc events (list (list 'quail-advice str)))))
1273 (defvar quail-translating nil)
1274 (defvar quail-converting nil)
1275 (defvar quail-conversion-str nil)
1277 (defun quail-input-method (key)
1278 (if (or buffer-read-only
1279 overriding-terminal-local-map
1280 overriding-local-map)
1282 (quail-setup-overlays (quail-conversion-keymap))
1283 (let ((modified-p (buffer-modified-p))
1284 (buffer-undo-list t))
1285 (or (and quail-guidance-win
1286 (window-live-p quail-guidance-win)
1287 (eq (window-buffer quail-guidance-win) quail-guidance-buf)
1288 (not input-method-use-echo-area))
1289 (quail-show-guidance-buf))
1291 (let ((input-string (if (quail-conversion-keymap)
1292 (quail-start-conversion key)
1293 (quail-start-translation key))))
1294 (when (and (stringp input-string)
1295 (> (length input-string) 0))
1296 (if input-method-exit-on-first-char
1297 (list (aref input-string 0))
1298 (quail-input-string-to-events input-string))))
1299 (quail-delete-overlays)
1300 (if (buffer-live-p quail-guidance-buf)
1301 (with-current-buffer quail-guidance-buf
1303 (quail-hide-guidance-buf)
1304 (set-buffer-modified-p modified-p)
1305 ;; Run this hook only when the current input method doesn't require
1306 ;; conversion. When conversion is required, the conversion function
1307 ;; should run this hook at a proper timing.
1308 (unless (quail-conversion-keymap)
1309 (run-hooks 'input-method-after-insert-chunk-hook))))))
1311 (defun quail-overlay-region-events (overlay)
1312 (let ((start (overlay-start overlay))
1313 (end (overlay-end overlay)))
1316 (string-to-list (buffer-substring start end))
1317 (delete-region start end)))))
1319 (defsubst quail-delete-region ()
1320 "Delete the text in the current translation region of Quail."
1321 (if (overlay-start quail-overlay)
1322 (delete-region (overlay-start quail-overlay)
1323 (overlay-end quail-overlay))))
1325 (defun quail-start-translation (key)
1326 "Start translation of the typed character KEY by the current Quail package.
1327 Return the input string."
1328 ;; Check the possibility of translating KEY.
1329 ;; If KEY is nil, we can anyway start translation.
1330 (if (or (and (integerp key)
1331 (assq (if (quail-kbd-translate)
1332 (quail-keyboard-translate key) key)
1335 ;; OK, we can start translation.
1336 (let* ((echo-keystrokes 0)
1338 (overriding-terminal-local-map (quail-translation-keymap))
1339 (generated-events nil)
1340 (input-method-function nil)
1341 (modified-p (buffer-modified-p)))
1342 (setq quail-current-key ""
1343 quail-current-str ""
1344 quail-translating t)
1346 (setq unread-command-events (cons key unread-command-events)))
1347 (while quail-translating
1348 (set-buffer-modified-p modified-p)
1349 (let* ((keyseq (read-key-sequence
1350 (and input-method-use-echo-area
1351 (concat input-method-previous-message
1354 (cmd (lookup-key (quail-translation-keymap) keyseq)))
1356 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
1357 (eq cmd 'quail-self-insert-command))
1358 (let ((last-command-event (aref keyseq (1- (length keyseq))))
1359 (last-command this-command)
1363 (call-interactively cmd)
1364 (quail-error (message "%s" (cdr err)) (beep))))
1365 ;; KEYSEQ is not defined in the translation keymap.
1366 ;; Let's return the event(s) to the caller.
1367 (setq unread-command-events
1368 (string-to-list (this-single-command-raw-keys)))
1369 (setq quail-translating nil))))
1370 (quail-delete-region)
1373 ;; Since KEY doesn't start any translation, just return it.
1374 ;; But translate KEY if necessary.
1375 (if (quail-kbd-translate)
1376 (setq key (quail-keyboard-translate key)))
1377 (char-to-string key)))
1379 (defun quail-start-conversion (key)
1380 "Start conversion of the typed character KEY by the current Quail package.
1381 Return the input string."
1382 ;; Check the possibility of translating KEY.
1383 ;; If KEY is nil, we can anyway start translation.
1384 (if (or (and (integerp key)
1385 (assq (if (quail-kbd-translate)
1386 (quail-keyboard-translate key) key)
1389 ;; Ok, we can start translation and conversion.
1390 (let* ((echo-keystrokes 0)
1392 (overriding-terminal-local-map (quail-conversion-keymap))
1393 (generated-events nil)
1394 (input-method-function nil)
1395 (modified-p (buffer-modified-p)))
1396 (setq quail-current-key ""
1397 quail-current-str ""
1400 quail-conversion-str "")
1402 (setq unread-command-events (cons key unread-command-events)))
1403 (while quail-converting
1404 (set-buffer-modified-p modified-p)
1405 (or quail-translating
1407 (setq quail-current-key ""
1408 quail-current-str ""
1409 quail-translating t)
1410 (quail-setup-overlays nil)))
1411 ;; Hide '... loaded' message.
1413 (let* ((keyseq (read-key-sequence
1414 (and input-method-use-echo-area
1415 (concat input-method-previous-message
1416 quail-conversion-str
1419 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1420 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1421 (let ((last-command-event (aref keyseq (1- (length keyseq))))
1422 (last-command this-command)
1426 (call-interactively cmd)
1427 (quail-error (message "%s" (cdr err)) (beep)))
1428 (or quail-translating
1430 (if quail-current-str
1431 (setq quail-conversion-str
1432 (concat quail-conversion-str
1433 (if (stringp quail-current-str)
1435 (char-to-string quail-current-str)))))
1436 (if (or input-method-exit-on-first-char
1437 (= (length quail-conversion-str) 0))
1438 (setq quail-converting nil)))))
1439 ;; KEYSEQ is not defined in the conversion keymap.
1440 ;; Let's return the event(s) to the caller.
1441 (setq unread-command-events
1442 (string-to-list (this-single-command-raw-keys)))
1443 (setq quail-converting nil))))
1444 (setq quail-translating nil)
1445 (if (overlay-start quail-conv-overlay)
1446 (delete-region (overlay-start quail-conv-overlay)
1447 (overlay-end quail-conv-overlay)))
1448 (if (> (length quail-conversion-str) 0)
1449 quail-conversion-str))
1451 ;; Since KEY doesn't start any translation, just return it.
1452 ;; But translate KEY if necessary.
1453 (if (quail-kbd-translate)
1454 (setq key (quail-keyboard-translate key)))
1455 (char-to-string key)))
1457 (defun quail-terminate-translation ()
1458 "Terminate the translation of the current key."
1459 (setq quail-translating nil)
1460 (if (buffer-live-p quail-guidance-buf)
1461 (with-current-buffer quail-guidance-buf
1464 (defun quail-select-current ()
1465 "Accept the currently selected translation."
1467 (quail-terminate-translation))
1469 (defun quail-update-translation (control-flag)
1470 "Update the current translation status according to CONTROL-FLAG.
1471 If CONTROL-FLAG is integer value, it is the number of keys in the
1472 head `quail-current-key' which can be translated. The remaining keys
1473 are put back to `unread-command-events' to be handled again. If
1474 CONTROL-FLAG is t, terminate the translation for the whole keys in
1475 `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation
1477 (let ((func (quail-update-translation-function)))
1479 (setq control-flag (funcall func control-flag))
1480 (cond ((numberp control-flag)
1481 (let ((len (length quail-current-key)))
1482 (if (= control-flag 0)
1483 (setq quail-current-str
1484 (if (quail-kbd-translate)
1485 (quail-keyseq-translate quail-current-key)
1486 quail-current-key)))
1487 (or input-method-exit-on-first-char
1488 (while (> len control-flag)
1490 (setq unread-command-events
1491 (cons (aref quail-current-key len)
1492 unread-command-events))))))
1493 ((null control-flag)
1494 (unless quail-current-str
1495 (setq quail-current-str
1496 (if (quail-kbd-translate)
1497 (quail-keyseq-translate quail-current-key)
1499 (if (and input-method-exit-on-first-char
1501 (setq control-flag t)))))))
1502 (or input-method-use-echo-area
1504 (quail-delete-region)
1505 (insert quail-current-str)))
1506 (let (quail-current-str)
1507 (quail-update-guidance))
1508 (or (stringp quail-current-str)
1509 (setq quail-current-str (char-to-string quail-current-str)))
1511 (quail-terminate-translation)))
1513 (defun quail-self-insert-command ()
1514 "Translate the typed key by the current Quail map, and insert."
1516 (setq quail-current-key
1517 (concat quail-current-key (char-to-string last-command-event)))
1518 (or (catch 'quail-tag
1519 (quail-update-translation (quail-translate-key))
1521 ;; If someone throws for `quail-tag' by value nil, we exit from
1522 ;; translation mode.
1523 (setq quail-translating nil)))
1525 (defun quail-map-definition (map)
1526 "Return the actual definition part of Quail map MAP."
1527 (let ((def (car map)))
1528 (if (and (consp def) (not (vectorp (cdr def))))
1529 (setq def (car def)))
1534 (defun quail-get-current-str (len def)
1535 "Return string to be shown as current translation of key sequence.
1536 LEN is the length of the sequence. DEF is a definition part of the
1537 Quail map for the sequence."
1538 (or (and (consp def) (aref (cdr def) (car (car def))))
1541 (let ((str (quail-get-current-str
1543 (quail-map-definition (quail-lookup-key
1544 quail-current-key (1- len))))))
1546 (concat (if (stringp str) str (char-to-string str))
1547 (substring quail-current-key (1- len) len)))))))
1549 (defvar quail-guidance-translations-starting-column 20)
1551 (defun quail-update-current-translations (&optional relative-index)
1552 "Update `quail-current-translations'.
1553 Make RELATIVE-INDEX the current translation."
1554 (let* ((indices (car quail-current-translations))
1556 (start (nth 1 indices))
1557 (end (nth 2 indices)))
1558 ;; Validate the index number of current translation.
1560 (setcar indices (setq cur 0))
1561 (if (>= cur (length (cdr quail-current-translations)))
1563 (setq cur (1- (length (cdr quail-current-translations)))))))
1565 (if (or (null end) ; We have not yet calculated END.
1566 (< cur start) ; We moved to the previous block.
1567 (>= cur end)) ; We moved to the next block.
1568 (let ((len (length (cdr quail-current-translations)))
1569 (maxcol (- (window-width quail-guidance-win)
1570 quail-guidance-translations-starting-column))
1571 (block (nth 3 indices))
1572 col idx width trans num-items blocks)
1574 ;; We must calculate from the head.
1575 (setq start 0 block 0)
1576 (if end ; i.e. (>= cur end)
1578 (setq idx start col 0 end start num-items 0)
1579 ;; Loop until we hit the tail, or reach the block of CUR.
1580 (while (and (< idx len) (>= cur end))
1582 (setq start idx col 0 block (1+ block)))
1583 (setq trans (aref (cdr quail-current-translations) idx))
1584 (setq width (if (integerp trans) (char-width trans)
1585 (string-width trans)))
1586 (setq col (+ col width 3) num-items (1+ num-items))
1587 (if (and (> num-items 0)
1588 (or (>= col maxcol) (> num-items 10)))
1589 (setq end idx num-items 0)
1590 (setq idx (1+ idx))))
1591 (setcar (nthcdr 3 indices) block)
1594 ;; We hit the tail before reaching MAXCOL.
1596 (setcar (nthcdr 4 indices) block)))
1597 (setcar (cdr indices) start)
1598 (setcar (nthcdr 2 indices) end)))
1600 (if (>= (+ start relative-index) end)
1601 (setcar indices (1- end))
1602 (setcar indices (+ start relative-index))))
1603 (setq quail-current-str
1604 (aref (cdr quail-current-translations) (car indices)))
1605 (or (stringp quail-current-str)
1606 (setq quail-current-str (char-to-string quail-current-str)))))
1608 (defun quail-translate-key ()
1609 "Translate the current key sequence according to the current Quail map.
1610 Return t if we can terminate the translation.
1611 Return nil if the current key sequence may be followed by more keys.
1612 Return number if we can't find any translation for the current key
1613 sequence. The number is the count of valid keys in the current
1614 sequence counting from the head."
1615 (let* ((len (length quail-current-key))
1616 (map (quail-lookup-key quail-current-key len))
1619 (let ((def (quail-map-definition map)))
1620 (setq quail-current-str (quail-get-current-str len def))
1621 ;; Return t only if we can terminate the current translation.
1623 ;; No alternative translations.
1624 (or (null (consp def)) (= (length (cdr def)) 1))
1625 ;; No translation for the longer key.
1627 ;; No shorter breaking point.
1628 (or (null (quail-maximum-shortest))
1630 (null (quail-lookup-key quail-current-key (1- len)))
1631 (null (quail-lookup-key
1632 (substring quail-current-key -2 -1) 1)))))
1634 ;; There's no translation for the current key sequence. Before
1635 ;; giving up, we must check two possibilities.
1637 (quail-maximum-shortest)
1639 (setq def (quail-map-definition
1640 (quail-lookup-key quail-current-key (- len 2))))
1641 (quail-lookup-key (substring quail-current-key -2) 2))
1642 ;; Now the sequence is "...ABCD", which can be split into
1643 ;; "...AB" and "CD..." to get valid translation.
1644 ;; At first, get translation of "...AB".
1645 (setq quail-current-str (quail-get-current-str (- len 2) def))
1646 ;; Then, return the length of "...AB".
1650 (quail-lookup-key (substring quail-current-key 0 -1))
1651 quail-current-translations
1652 (not (quail-deterministic))
1653 (setq ch (aref quail-current-key (1- len)))
1654 (>= ch ?0) (<= ch ?9))
1655 ;; A numeric key is entered to select a desirable translation.
1656 (setq quail-current-key (substring quail-current-key 0 -1))
1657 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1658 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1659 (quail-update-current-translations ch)
1660 ;; And, we can terminate the current translation.
1664 ;; No way to handle the last character in this context.
1667 (defun quail-next-translation ()
1668 "Select next translation in the current batch of candidates."
1670 (if quail-current-translations
1671 (let ((indices (car quail-current-translations)))
1672 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1673 ;; We are already at the tail.
1675 (setcar indices (1+ (car indices)))
1676 (quail-update-current-translations)
1677 (quail-update-translation nil)))
1678 (setq unread-command-events
1679 (cons last-command-event unread-command-events))
1680 (quail-terminate-translation)))
1682 (defun quail-prev-translation ()
1683 "Select previous translation in the current batch of candidates."
1685 (if quail-current-translations
1686 (let ((indices (car quail-current-translations)))
1687 (if (= (car indices) 0)
1688 ;; We are already at the head.
1690 (setcar indices (1- (car indices)))
1691 (quail-update-current-translations)
1692 (quail-update-translation nil)))
1693 (setq unread-command-events
1694 (cons last-command-event unread-command-events))
1695 (quail-terminate-translation)))
1697 (defun quail-next-translation-block ()
1698 "Select from the next block of translations."
1700 (if quail-current-translations
1701 (let* ((indices (car quail-current-translations))
1702 (offset (- (car indices) (nth 1 indices))))
1703 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1704 ;; We are already at the last block.
1706 (setcar indices (+ (nth 2 indices) offset))
1707 (quail-update-current-translations)
1708 (quail-update-translation nil)))
1709 (setq unread-command-events
1710 (cons last-command-event unread-command-events))
1711 (quail-terminate-translation)))
1713 (defun quail-prev-translation-block ()
1714 "Select the previous batch of 10 translation candidates."
1716 (if quail-current-translations
1717 (let* ((indices (car quail-current-translations))
1718 (offset (- (car indices) (nth 1 indices))))
1719 (if (= (nth 1 indices) 0)
1720 ;; We are already at the first block.
1722 (setcar indices (1- (nth 1 indices)))
1723 (quail-update-current-translations)
1724 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1726 (setcar indices (+ (nth 1 indices) offset))
1727 (quail-update-current-translations)))
1728 (quail-update-translation nil)))
1729 (setq unread-command-events
1730 (cons last-command-event unread-command-events))
1731 (quail-terminate-translation)))
1733 (defun quail-abort-translation ()
1734 "Abort translation and delete the current Quail key sequence."
1736 (quail-delete-region)
1737 (setq quail-current-str nil)
1738 (quail-terminate-translation))
1740 (defun quail-delete-last-char ()
1741 "Delete the last input character from the current Quail key sequence."
1743 (if (= (length quail-current-key) 1)
1744 (quail-abort-translation)
1745 (setq quail-current-key (substring quail-current-key 0 -1))
1746 (quail-delete-region)
1747 (quail-update-translation (quail-translate-key))))
1749 ;; For conversion mode.
1751 (defsubst quail-point-in-conversion-region ()
1752 "Return non-nil value if the point is in conversion region of Quail mode."
1754 (and (setq start (overlay-start quail-conv-overlay))
1755 (>= (setq pos (point)) start)
1756 (<= pos (overlay-end quail-conv-overlay)))))
1758 (defun quail-conversion-backward-char ()
1760 (if (<= (point) (overlay-start quail-conv-overlay))
1761 (quail-error "Beginning of conversion region"))
1762 (setq quail-translating nil)
1765 (defun quail-conversion-forward-char ()
1767 (if (>= (point) (overlay-end quail-conv-overlay))
1768 (quail-error "End of conversion region"))
1769 (setq quail-translating nil)
1772 (defun quail-conversion-beginning-of-region ()
1774 (setq quail-translating nil)
1775 (goto-char (overlay-start quail-conv-overlay)))
1777 (defun quail-conversion-end-of-region ()
1779 (setq quail-translating nil)
1780 (goto-char (overlay-end quail-conv-overlay)))
1782 (defun quail-conversion-delete-char ()
1784 (setq quail-translating nil)
1785 (if (>= (point) (overlay-end quail-conv-overlay))
1786 (quail-error "End of conversion region"))
1788 (let ((start (overlay-start quail-conv-overlay))
1789 (end (overlay-end quail-conv-overlay)))
1790 (setq quail-conversion-str (buffer-substring start end))
1792 (setq quail-converting nil))))
1794 (defun quail-conversion-delete-tail ()
1796 (if (>= (point) (overlay-end quail-conv-overlay))
1797 (quail-error "End of conversion region"))
1798 (delete-region (point) (overlay-end quail-conv-overlay))
1799 (let ((start (overlay-start quail-conv-overlay))
1800 (end (overlay-end quail-conv-overlay)))
1801 (setq quail-conversion-str (buffer-substring start end))
1803 (setq quail-converting nil))))
1805 (defun quail-conversion-backward-delete-char ()
1807 (if (> (length quail-current-key) 0)
1808 (quail-delete-last-char)
1809 (if (<= (point) (overlay-start quail-conv-overlay))
1810 (quail-error "Beginning of conversion region"))
1812 (let ((start (overlay-start quail-conv-overlay))
1813 (end (overlay-end quail-conv-overlay)))
1814 (setq quail-conversion-str (buffer-substring start end))
1816 (setq quail-converting nil)))))
1818 (defun quail-do-conversion (func &rest args)
1819 "Call FUNC to convert text in the current conversion region of Quail.
1820 Remaining args are for FUNC."
1821 (delete-overlay quail-overlay)
1824 (defun quail-no-conversion ()
1825 "Do no conversion of the current conversion region of Quail."
1827 (setq quail-converting nil))
1829 ;; Guidance, Completion, and Help buffer handlers.
1831 (defun quail-make-guidance-frame (buf)
1832 "Make a new one-line frame for Quail guidance buffer."
1833 (let* ((fparam (frame-parameters))
1834 (top (cdr (assq 'top fparam)))
1835 (border (cdr (assq 'border-width fparam)))
1836 (internal-border (cdr (assq 'internal-border-width fparam)))
1838 (frame-char-height) (* internal-border 2) (* border 2))))
1840 (setq newtop (+ top (frame-pixel-height))))
1841 (let* ((frame (make-frame (append '((user-position . t) (height . 1)
1842 (minibuffer) (menu-bar-lines . 0))
1843 (cons (cons 'top newtop) fparam))))
1844 (win (frame-first-window frame)))
1845 (set-window-buffer win buf)
1846 ;;(set-window-dedicated-p win t)
1849 (defun quail-setup-completion-buf ()
1850 "Setup Quail completion buffer."
1851 (unless (buffer-live-p quail-completion-buf)
1852 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1853 (setq quail-completion-buf (get-buffer-create "*Quail Completions*")))
1854 (with-current-buffer quail-completion-buf
1855 (setq quail-overlay (make-overlay 1 1))
1856 (overlay-put quail-overlay 'face 'highlight))))
1858 (defun quail-require-guidance-buf ()
1859 "Return t iff the current Quail package requires showing guidance buffer."
1860 (and input-method-verbose-flag
1861 (if (eq input-method-verbose-flag 'default)
1862 (not (and (eq (selected-window) (minibuffer-window))
1864 (if (eq input-method-verbose-flag 'complex-only)
1865 (not (quail-simple))
1868 (defun quail-show-guidance-buf ()
1869 "Display a guidance buffer for Quail input method in some window.
1870 Create the buffer if it does not exist yet.
1871 The buffer is normally displayed at the echo area,
1872 but if the current buffer is a minibuffer, it is shown in
1873 the bottom-most ordinary window of the same frame,
1874 or in a newly created frame (if the selected frame has no other windows)."
1875 (when (quail-require-guidance-buf)
1876 ;; At first, setup a guidance buffer.
1877 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1878 (or (buffer-live-p quail-guidance-buf)
1879 (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*"))))
1880 (let ((name (quail-name))
1881 (title (quail-title)))
1882 (with-current-buffer quail-guidance-buf
1883 ;; To show the title of Quail package.
1884 (setq current-input-method name
1885 current-input-method-title title)
1887 (or (overlayp quail-overlay)
1889 (setq quail-overlay (make-overlay 1 1))
1890 (overlay-put quail-overlay 'face 'highlight)))
1891 (delete-overlay quail-overlay)
1892 (set-buffer-modified-p nil)))
1893 (bury-buffer quail-guidance-buf)
1895 ;; Assign the buffer " *Minibuf-N*" to all windows which are now
1896 ;; displaying quail-guidance-buf.
1897 (let ((win-list (get-buffer-window-list quail-guidance-buf t t)))
1899 (set-window-buffer (car win-list)
1900 (format " *Minibuf-%d*" (minibuffer-depth)))
1901 (setq win-list (cdr win-list))))
1903 ;; Then, display it in an appropriate window.
1904 (let ((win (minibuffer-window)))
1905 (if (or (eq (selected-window) win)
1906 input-method-use-echo-area)
1907 ;; Since we are in minibuffer, we can't use it for guidance.
1908 (if (eq win (frame-root-window))
1909 ;; Create a frame. It is sure that we are using some
1911 (quail-make-guidance-frame quail-guidance-buf)
1912 ;; Find the bottom window and split it if necessary.
1913 (setq win (window-at
1914 0 (1- (- (frame-height) (window-height win)))))
1915 (let ((height (window-height win))
1916 (window-min-height 2))
1917 ;; If WIN is tall enough, split it vertically and use
1920 ;; Here, `split-window' returns a lower window
1921 ;; which is what we wanted.
1922 (setq win (split-window win (- height 2))))
1923 (set-window-buffer win quail-guidance-buf)
1924 (with-current-buffer quail-guidance-buf
1925 (fit-window-to-buffer win nil (window-height win)))))
1926 (set-window-buffer win quail-guidance-buf)
1927 (set-minibuffer-window win))
1928 (setq quail-guidance-win win)))
1930 ;; And, create a buffer for completion.
1931 (quail-setup-completion-buf)
1932 (bury-buffer quail-completion-buf))
1934 (defun quail-hide-guidance-buf ()
1935 "Hide the Quail guidance buffer."
1936 (if (buffer-live-p quail-guidance-buf)
1937 (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
1940 (setq win (car win-list) win-list (cdr win-list))
1941 (if (window-minibuffer-p win)
1942 ;; We are using echo area for the guidance buffer.
1943 ;; Vacate it to the deepest minibuffer.
1944 (set-window-buffer win
1945 (format " *Minibuf-%d*" (minibuffer-depth)))
1946 (if (eq win (frame-root-window (window-frame win)))
1948 ;; We are using a separate frame for guidance buffer.
1949 ;;(set-window-dedicated-p win nil)
1950 (delete-frame (window-frame win)))
1951 ;;(set-window-dedicated-p win nil)
1952 (delete-window win))))
1953 (setq quail-guidance-win nil))))
1955 (defun quail-update-guidance ()
1956 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1957 ;; Update guidance buffer.
1958 (if (quail-require-guidance-buf)
1959 (let ((guidance (quail-guidance)))
1960 (unless (and (eq (selected-frame) (window-frame (minibuffer-window)))
1961 (eq (selected-frame) (window-frame quail-guidance-win)))
1962 ;; The guidance window is not shown in this frame, show it.
1963 (quail-show-guidance-buf))
1964 (cond ((or (eq guidance t)
1966 ;; Show the current possible translations.
1967 (quail-show-translations))
1969 ;; Show the current input keys.
1970 (let ((key quail-current-key))
1971 (if (quail-kbd-translate)
1972 (setq key (quail-keyseq-translate key)))
1973 (with-current-buffer quail-guidance-buf
1976 ;; Make sure the height of the guidance window is OK --
1977 ;; sometimes, if the minibuffer window expands due to user
1978 ;; input (for instance if the newly inserted character is in a
1979 ;; different font), it will cause the guidance window to be
1980 ;; only partially visible. We force a redisplay first because
1981 ;; this automatic expansion doesn't happen until then, and we
1982 ;; want to see the window sizes after the expansion.
1984 (fit-window-to-buffer quail-guidance-win nil
1985 (window-height quail-guidance-win))))
1987 ;; Update completion buffer if displayed now. We highlight the
1988 ;; selected candidate string in *Completion* buffer if any.
1989 (let ((win (get-buffer-window quail-completion-buf))
1993 (setq str (if (stringp quail-current-str)
1995 (if (numberp quail-current-str)
1996 (char-to-string quail-current-str)))
1997 key quail-current-key)
1998 (set-buffer quail-completion-buf)
1999 (goto-char (point-min))
2000 (if (null (search-forward (concat " " key ":") nil t))
2001 (delete-overlay quail-overlay)
2003 (if (and str (search-forward (concat "." str) nil t))
2004 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
2005 (move-overlay quail-overlay (match-beginning 0) (point)))
2006 ;; Now POS points end of KEY and (point) points end of STR.
2007 (if (pos-visible-in-window-p (point) win)
2008 ;; STR is already visible.
2010 ;; We want to make both KEY and STR visible, but if the
2011 ;; window is too short, make at least STR visible.
2012 (setq pos (progn (point) (goto-char pos)))
2014 (set-window-start win (point))
2015 (if (not (pos-visible-in-window-p pos win))
2016 (set-window-start win pos))
2019 (defun quail-show-translations ()
2020 "Show the current possible translations."
2021 (let* ((key quail-current-key)
2022 (map (quail-lookup-key quail-current-key))
2023 (current-translations quail-current-translations))
2024 (if quail-current-translations
2025 (quail-update-current-translations))
2026 (with-current-buffer quail-guidance-buf
2029 ;; Show the current key.
2030 (let ((guidance (quail-guidance)))
2031 (if (listp guidance)
2032 ;; We must show the specified PROMPTKEY instead of the
2033 ;; actual typed keys.
2038 (setq prompt-key (cdr (assoc (aref key i) guidance)))
2039 (insert (or prompt-key (aref key i)))
2043 ;; Show followable keys.
2044 (if (and (> (length key) 0) (cdr map))
2045 (let ((keys (mapcar (function (lambda (x) (car x)))
2047 (setq keys (sort keys '<))
2051 (setq keys (cdr keys)))
2054 ;; Show list of translations.
2055 (if (and current-translations
2056 (not (quail-deterministic)))
2057 (let* ((indices (car current-translations))
2059 (start (nth 1 indices))
2060 (end (nth 2 indices))
2062 (indent-to (- quail-guidance-translations-starting-column 7))
2063 (insert (format "(%02d/"(nth 3 indices))
2065 (format "%02d)" (nth 4 indices))
2068 (insert (format " %d." (if (= (- idx start) 9) 0
2069 (1+ (- idx start)))))
2070 (let ((pos (point)))
2071 (insert (aref (cdr current-translations) idx))
2073 (move-overlay quail-overlay pos (point))))
2074 (setq idx (1+ idx)))))
2077 (defvar quail-completion-max-depth 5
2078 "The maximum depth of Quail completion list.")
2080 (defun quail-completion ()
2081 "List all completions for the current key.
2082 All possible translations of the current key and whole possible longer keys
2083 are shown (at most to the depth specified `quail-completion-max-depth')."
2085 (quail-setup-completion-buf)
2086 (let ((win (get-buffer-window quail-completion-buf 'visible))
2087 (key quail-current-key)
2088 (map (quail-lookup-key quail-current-key))
2089 (require-update nil))
2090 (with-current-buffer quail-completion-buf
2092 (equal key quail-current-key)
2093 (eq last-command 'quail-completion))
2094 ;; The window for Quail completion buffer has already been
2095 ;; shown. We just scroll it appropriately.
2096 (if (pos-visible-in-window-p (point-max) win)
2097 (set-window-start win (point-min))
2098 (let ((other-window-scroll-buffer quail-completion-buf))
2099 (scroll-other-window)))
2100 (setq quail-current-key key)
2102 (insert "Possible completion and corresponding translations are:\n")
2103 (quail-completion-1 key map 1)
2104 (goto-char (point-min))
2105 (display-buffer (current-buffer))
2106 (setq require-update t)))
2108 (quail-update-guidance)))
2109 (setq this-command 'quail-completion))
2111 (defun quail-completion-1 (key map indent)
2112 "List all completions of KEY in MAP with indentation INDENT."
2113 (let ((len (length key)))
2116 (if (and (symbolp map) (fboundp map))
2117 (setq map (funcall map key len)))
2119 (quail-completion-list-translations map key (+ indent len 1))
2121 (setq indent (+ indent 2))
2122 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
2124 (newkey (make-string (1+ len) 0))
2127 (setq l (funcall l)))
2128 ;; Set KEY in the first LEN characters of NEWKEY.
2130 (aset newkey i (aref key i))
2132 (setq l (reverse l))
2133 (while l ; L = ((CHAR . DEFN) ....) ;
2134 (aset newkey len (car (car l)))
2135 (quail-completion-1 newkey (cdr (car l)) indent)
2136 (setq l (cdr l)))))))
2138 (defun quail-completion-list-translations (map key indent)
2139 "List all possible translations of KEY in Quail MAP with indentation INDENT."
2140 (let (beg (translations
2141 (quail-get-translation (car map) key (length key))))
2142 (if (integerp translations)
2145 ;; Endow the character `translations' with `mouse-face' text
2146 ;; property to enable `mouse-2' completion.
2148 (insert translations)
2149 (put-text-property beg (point) 'mouse-face 'highlight)
2151 ;; We need only vector part.
2152 (setq translations (cdr translations))
2153 ;; Insert every 10 elements with indices in a line.
2154 (let ((len (length translations))
2158 (when (zerop (% i 10))
2162 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
2163 ;; We show the last digit of FROM while converting
2164 ;; 0,1,..,9 to 1,2,..,0.
2165 (insert (format " %d." (% (1+ i) 10)))
2167 (insert (aref translations i))
2168 ;; Passing the mouse over a character will highlight.
2169 (put-text-property beg (point) 'mouse-face 'highlight)
2173 ;; Choose a completion in *Quail Completions* buffer with mouse-2.
2175 (defun quail-mouse-choose-completion (event)
2176 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
2178 ;; This function is an exact copy of the mouse.el function
2179 ;; `mouse-choose-completion' except that we:
2180 ;; 1) add two lines from `choose-completion' in simple.el to give
2181 ;; the `mouse-2' click a little more leeway.
2182 ;; 2) don't bury *Quail Completions* buffer so comment a section, and
2183 ;; 3) delete/terminate the current quail selection here.
2184 ;; Give temporary modes such as isearch a chance to turn off.
2185 (run-hooks 'mouse-leave-buffer-hook)
2186 (let ((buffer (window-buffer))
2189 (with-current-buffer (window-buffer (posn-window (event-start event)))
2190 (if completion-reference-buffer
2191 (setq buffer completion-reference-buffer))
2192 (setq base-size completion-base-size)
2194 (goto-char (posn-point (event-start event)))
2196 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2197 (setq end (point) beg (1+ (point))))
2198 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
2199 (setq end (1- (point)) beg (point)))
2201 (quail-error "No completion here"))
2202 (setq beg (previous-single-property-change beg 'mouse-face))
2203 (setq end (or (next-single-property-change end 'mouse-face)
2205 (setq choice (buffer-substring beg end)))))
2206 ; (let ((owindow (selected-window)))
2207 ; (select-window (posn-window (event-start event)))
2208 ; (if (and (one-window-p t 'selected-frame)
2209 ; (window-dedicated-p (selected-window)))
2210 ; ;; This is a special buffer's frame
2211 ; (iconify-frame (selected-frame))
2212 ; (or (window-dedicated-p (selected-window))
2214 ; (select-window owindow))
2215 (quail-delete-region)
2216 (quail-choose-completion-string choice buffer base-size)
2217 (quail-terminate-translation)))
2219 ;; Modify the simple.el function `choose-completion-string', because
2220 ;; the simple.el function `choose-completion-delete-max-match' breaks
2221 ;; on Mule data, since the semantics of `forward-char' have changed.
2223 (defun quail-choose-completion-string (choice &optional buffer base-size)
2224 (let ((buffer (or buffer completion-reference-buffer)))
2225 ;; If BUFFER is a minibuffer, barf unless it's the currently
2226 ;; active minibuffer.
2227 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
2228 (or (not (active-minibuffer-window))
2230 (window-buffer (active-minibuffer-window))))))
2231 (quail-error "Minibuffer is not active for completion")
2232 ;; Store the completion in `quail-current-str', which will later
2233 ;; be converted to a character event list, then inserted into
2234 ;; the buffer where completion was requested.
2237 ; (delete-region (+ base-size (point-min)) (point))
2238 ; (choose-completion-delete-max-match choice))
2239 (setq quail-current-str choice)
2240 ;; Update point in the window that BUFFER is showing in.
2241 (let ((window (get-buffer-window buffer t)))
2242 (set-window-point window (point)))
2243 ;; If completing for the minibuffer, exit it with this choice.
2244 (and (not completion-no-auto-exit)
2245 (equal buffer (window-buffer (minibuffer-window)))
2246 minibuffer-completion-table
2247 ;; If this is reading a file name, and the file name chosen
2248 ;; is a directory, don't exit the minibuffer.
2249 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
2250 (file-directory-p (buffer-string)))
2251 (select-window (active-minibuffer-window))
2252 (exit-minibuffer))))))
2254 (defun quail-build-decode-map (map-list key decode-map num
2255 &optional maxnum ignores)
2256 "Build a decoding map.
2257 Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
2258 vs the corresponding translations defined in the Quail map
2259 specified by the first element MAP-LIST. Each pair has the form
2260 \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
2261 \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
2262 is a key sequence to reach MAP.
2263 Optional 5th arg MAXNUM limits the number of accumulated pairs.
2264 Optional 6th arg IGNORES is a list of translations to ignore."
2265 (let* ((map (car map-list))
2266 (translation (quail-get-translation (car map) key (length key)))
2268 (cond ((integerp translation)
2269 ;; Accept only non-ASCII chars not listed in IGNORES.
2270 (when (and (> translation 255) (not (memq translation ignores)))
2272 (cons (cons key translation) (cdr decode-map)))
2273 (setq num (1+ num))))
2274 ((consp translation)
2275 (setq translation (cdr translation))
2276 (let ((multibyte nil))
2277 (mapc (function (lambda (x)
2278 ;; Accept only non-ASCII chars not
2279 ;; listed in IGNORES.
2280 (if (and (if (integerp x) (> x 255)
2281 (> (string-bytes x) (length x)))
2282 (not (member x ignores)))
2283 (setq multibyte t))))
2287 (cons (cons key translation) (cdr decode-map)))
2288 (setq num (+ num (length translation)))))))
2289 (if (and maxnum (> num maxnum))
2291 (setq map (cdr map))
2292 ;; Recursively check the deeper map.
2293 (while (and map (>= num 0))
2294 (setq elt (car map) map (cdr map))
2295 (when (and (integerp (car elt)) (consp (cdr elt))
2296 (not (memq (cdr elt) map-list)))
2297 (setq num (quail-build-decode-map (cons (cdr elt) map-list)
2298 (format "%s%c" key (car elt))
2299 decode-map num maxnum ignores))))
2302 (defun quail-insert-decode-map (decode-map)
2303 "Insert pairs of key sequences vs the corresponding translations.
2304 These are stored in DECODE-MAP using the concise format. DECODE-MAP
2305 should be made by `quail-build-decode-map' (which see)."
2307 (sort (cdr decode-map)
2308 (function (lambda (x y)
2309 (setq x (car x) y (car y))
2310 (or (> (length x) (length y))
2311 (and (= (length x) (length y))
2312 (not (string< x y))))))))
2313 (let ((frame-width (frame-width (window-frame (get-buffer-window
2314 (current-buffer) 'visible))))
2315 (single-key-width 3)
2316 (single-trans-width 4)
2317 (multiple-key-width 3)
2320 elt trans width pos cols rows col row str col-width)
2321 ;; Divide the elements of decoding map into single ones (i.e. the
2322 ;; one that has single translation) and multibyte ones (i.e. the
2323 ;; one that has multiple translations).
2325 (setq elt (car decode-map) decode-map (cdr decode-map)
2327 (if (and (vectorp trans) (= (length trans) 1))
2328 (setq trans (aref trans 0)))
2330 (setq multiple-list (cons elt multiple-list))
2331 (setq single-list (cons (cons (car elt) trans) single-list)
2332 width (if (stringp trans) (string-width trans)
2333 (char-width trans)))
2334 (if (> width single-trans-width)
2335 (setq single-trans-width width)))
2336 (setq width (length (car elt)))
2337 (if (> width single-key-width)
2338 (setq single-key-width width))
2339 (if (> width multiple-key-width)
2340 (setq multiple-key-width width)))
2342 (setq col-width (+ single-key-width 1 single-trans-width 1)
2343 cols (/ frame-width col-width)
2344 rows (/ (length single-list) cols))
2345 (if (> (% (length single-list) cols) 0)
2346 (setq rows (1+ rows)))
2348 (indent-to (1+ single-key-width))
2350 (indent-to (1+ col-width))
2351 (insert "[type a key sequence to insert the corresponding character]\n")
2353 (insert-char ?\n (+ rows 2))
2355 (setq col (- col-width) row 0)
2357 (setq elt (car single-list) single-list (cdr single-list))
2358 (when (= (% row rows) 0)
2360 (setq col (+ col col-width))
2361 (move-to-column col t)
2362 (insert-char ?- single-key-width)
2364 (insert-char ?- single-trans-width)
2366 (move-to-column col t)
2368 (indent-to (+ col single-key-width 1))
2371 (setq row (1+ row)))
2372 (goto-char (point-max)))
2376 (indent-to (1+ multiple-key-width))
2377 (insert "character(s) [type a key (sequence) and select one from the list]\n")
2378 (insert-char ?- multiple-key-width)
2379 (insert " ------------\n")
2380 (while multiple-list
2381 (setq elt (car multiple-list) multiple-list (cdr multiple-list))
2383 (indent-to multiple-key-width)
2384 (if (vectorp (cdr elt))
2387 (let ((width (if (integerp x) (char-width x)
2389 (when (> (+ (current-column) 1 width) frame-width)
2391 (indent-to multiple-key-width))
2394 (insert " " (cdr elt)))
2398 (defun quail-help (&optional package)
2399 "Show brief description of the current Quail package.
2400 Optional arg PACKAGE specifies the name of alternative Quail
2401 package to describe."
2404 (setq package (assoc package quail-package-alist))
2405 (setq package quail-current-package))
2406 (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
2407 (default-enable-multibyte-characters enable-multibyte-characters))
2408 ;; At first, make sure that the help buffer has window.
2409 (help-setup-xref (list #'quail-help package) (interactive-p))
2410 (with-output-to-temp-buffer (help-buffer)
2411 (with-current-buffer standard-output
2412 (setq quail-current-package package)))
2413 ;; Then, insert text in the help buffer while paying attention to
2414 ;; the width of the frame in which the buffer displayed.
2415 (with-current-buffer (help-buffer)
2416 (setq buffer-read-only nil)
2417 (insert "Input method: " (quail-name)
2418 " (mode line indicator:"
2422 (narrow-to-region (point) (point))
2423 (insert (quail-docstring))
2424 (goto-char (point-min))
2425 (with-syntax-table emacs-lisp-mode-syntax-table
2426 (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
2427 (let ((sym (intern-soft
2428 (buffer-substring (+ (match-beginning 0) 2)
2430 (if (and (boundp sym)
2431 (stringp (symbol-value sym)))
2432 (replace-match (symbol-value sym) t t)))))
2433 (goto-char (point-max)))
2438 (let ((done-list nil))
2439 ;; Show keyboard layout if the current package requests it..
2440 (when (quail-show-layout)
2444 This input method works by translating individual input characters.
2445 Assuming that your actual keyboard has the `")
2446 (help-insert-xref-button
2447 quail-keyboard-layout-type
2448 #'quail-show-keyboard-layout quail-keyboard-layout-type
2449 "mouse-2, RET: show this layout")
2451 translation results in the following \"virtual\" keyboard layout:
2454 (quail-insert-kbd-layout quail-keyboard-layout))
2455 (insert "If your keyboard has a different layout, rearranged from
2457 (help-insert-xref-button
2459 #'quail-show-keyboard-layout "standard"
2460 "mouse-2, RET: show this layout")
2461 (insert "', the \"virtual\" keyboard you get with this input method
2462 will be rearranged in the same way.
2464 You can set the variable `quail-keyboard-layout-type' to specify
2465 the physical layout of your keyboard; the tables shown in
2466 documentation of input methods including this one are based on the
2467 physical keyboard layout as specified with that variable.
2469 (help-insert-xref-button
2470 "[customize keyboard layout]"
2471 #'customize-variable 'quail-keyboard-layout-type
2472 "mouse-2, RET: set keyboard layout type")
2475 ;; Show key sequences.
2476 (let ((decode-map (list 'decode-map))
2478 (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
2485 (if (quail-show-layout)
2486 (insert "You can also input more characters")
2487 (insert "You can input characters"))
2488 (insert " by the following key sequences:\n")
2489 (quail-insert-decode-map decode-map))))
2491 (quail-help-insert-keymap-description
2492 (quail-translation-keymap)
2494 KEY BINDINGS FOR TRANSLATION
2495 ----------------------------\n")
2497 (if (quail-conversion-keymap)
2498 (quail-help-insert-keymap-description
2499 (quail-conversion-keymap)
2501 KEY BINDINGS FOR CONVERSION
2502 ---------------------------\n"))
2503 (setq quail-current-package nil)
2504 ;; Resize the help window again, now that it has all its contents.
2505 (save-selected-window
2506 (select-window (get-buffer-window (current-buffer)))
2507 (run-hooks 'temp-buffer-show-hook)))))
2509 (defun quail-help-insert-keymap-description (keymap &optional header)
2510 (let (pos1 pos2 eol)
2515 (insert (substitute-command-keys "\\{keymap}")))
2516 ;; Skip headers "key bindings", etc.
2519 (with-syntax-table emacs-lisp-mode-syntax-table
2520 (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
2521 (let ((sym (intern-soft (buffer-substring (match-beginning 0)
2523 (if (and sym (fboundp sym)
2524 (or (eq (get sym 'quail-help) 'hide)
2525 (and (quail-deterministic)
2526 (eq (get sym 'quail-help) 'non-deterministic))))
2527 (delete-region (line-beginning-position)
2528 (1+ (line-end-position)))))))
2531 (if (looking-at "[ \t]*$")
2532 (delete-region (point) (1+ (line-end-position)))
2536 (delete-region pos1 (point)))
2537 (goto-char (point-max))))
2539 (defun quail-translation-help ()
2540 "Show help message while translating in Quail input method."
2542 (if (not (eq this-command last-command))
2543 (let (state-msg keymap)
2544 (if (and quail-converting (= (length quail-current-key) 0))
2546 (format "Converting string %S by input method %S.\n"
2547 quail-conversion-str (quail-name))
2548 keymap (quail-conversion-keymap))
2550 (format "Translating key sequence %S by input method %S.\n"
2551 quail-current-key (quail-name))
2552 keymap (quail-translation-keymap)))
2553 (with-output-to-temp-buffer "*Help*"
2554 (with-current-buffer standard-output
2556 (quail-help-insert-keymap-description
2558 "-----------------------\n")
2559 ;; Isn't this redundant ? -stef
2562 (save-selected-window
2563 (select-window (get-buffer-window "*Help*"))
2564 (if (eq this-command last-command)
2565 (if (< (window-end) (point-max))
2567 (if (> (window-start) (point-min))
2568 (set-window-start (selected-window) (point-min)))))
2570 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2571 "Type \\[quail-translation-help] to scroll up the help"
2572 (if (> (window-start) (point-min))
2573 "Type \\[quail-translation-help] to see the head of help"))))
2576 (message "%s" (substitute-command-keys scroll-help))
2579 (quail-update-guidance)
2582 ;; Quail map generator from state transition table.
2584 (defun quail-map-from-table (table)
2585 "Make quail map from state transition table TABLE.
2587 TABLE is an alist, the form is:
2588 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2590 STATE-n are symbols to denote state. STATE-0 is the initial state.
2592 TRANSITION-n-m are transition rules from STATE-n, and have the form
2593 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2594 RULES is a symbol whose value is an alist of keys \(string) vs the
2595 correponding characters or strings. The format of the symbol value of
2596 RULES is the same as arguments to `quail-define-rules'.
2598 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2599 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2600 transition is allowed, i.e. STATE-x may be STATE-n.
2602 If TRANSITION-n-m has the form RULES, the transition terminates
2603 when keys in RULES are input.
2605 The generated map can be set for the current Quail package by the
2606 function `quail-install-map' (which see)."
2607 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2609 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2610 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2611 ;; Set key sequence mapping rules in cdr part of each element.
2613 (quail-map-from-table-1 state-alist (car table))
2614 (setq table (cdr table)))
2616 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2617 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2618 ;; them with MAPPING-RULES of STATE-x to make elements of
2619 ;; STATE-ALIST valid Quail maps.
2620 (setq tail state-alist)
2622 (setq elt (car tail) tail (cdr tail))
2623 (quail-map-from-table-2 state-alist elt))
2625 ;; Return the Quail map for the initial state.
2628 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2629 ;; mapping rules in the element of STATE-ALIST that corresponds to
2630 ;; STATE according to TRANSITION ...
2631 (defun quail-map-from-table-1 (state-alist state-info)
2632 (let* ((state (car state-info))
2633 (map (assq state state-alist))
2634 (transitions (cdr state-info))
2637 (setq elt (car transitions) transitions (cdr transitions))
2638 (let (rules dst-state key trans)
2639 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2640 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2642 (setq rules (symbol-value (car elt))
2643 ;; Set (STATE-x) as branches for all keys in RULES.
2644 ;; It is replaced with actual branches for STATE-x
2645 ;; later in `quail-map-from-table-2'.
2646 dst-state (list (cdr elt)))
2647 (setq rules (symbol-value elt)))
2649 (setq key (car (car rules)) trans (cdr (car rules))
2652 (if (= (length trans) 1)
2653 (setq trans (aref trans 0))
2654 (setq trans (string-to-vector trans))))
2655 (set-nested-alist key trans map nil dst-state))))))
2657 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2659 ;; (STATE (CHAR NESTED-ALIST) ...)
2660 ;; NESTED-ALIST is a nested alist; the form is:
2661 ;; (TRANS (CHAR NESTED-ALIST) ...)
2663 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2664 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2665 ;; (cdr (assq STATE-x STATE-ALIST))
2667 (defun quail-map-from-table-2 (state-alist element)
2668 (let ((prev element)
2669 (tail (cdr element))
2672 (setq elt (car tail) prev tail tail (cdr tail))
2673 (quail-map-from-table-2 state-alist (cdr elt)))
2674 (setq elt (car tail))
2676 (quail-map-from-table-2 state-alist (cdr elt))
2677 (setcdr prev (cdr (assq elt state-alist))))))
2679 ;; Concatenate translations for all heading substrings of KEY in the
2680 ;; current Quail map. Here, `heading substring' means (substring KEY
2681 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2682 (defun quail-lookup-map-and-concat (key)
2683 (let* ((len (length key))
2684 (translation-list nil)
2687 (setq map (quail-lookup-key key len)
2690 (let* ((def (quail-map-definition map))
2691 (trans (if (consp def) (aref (cdr def) (car (car def)))
2693 (if (integerp trans)
2694 (setq trans (char-to-string trans)))
2695 (setq translation-list (cons trans translation-list)))))
2696 (apply 'concat translation-list)))
2699 (defvar quail-directory-name "quail"
2700 "Name of Quail directory which contains Quail packages.
2701 This is a sub-directory of LEIM directory.")
2704 (defun quail-update-leim-list-file (dirname &rest dirnames)
2705 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2706 DIRNAME is a directory containing Emacs input methods;
2707 normally, it should specify the `leim' subdirectory
2708 of the Emacs source tree.
2710 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2711 and update the file \"leim-list.el\" in DIRNAME.
2713 When called from a program, the remaining arguments are additional
2714 directory names to search for Quail packages under `quail' subdirectory
2716 (interactive "FDirectory of LEIM: ")
2717 (setq dirname (expand-file-name dirname))
2718 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2719 quail-dirs list-buf pkg-list pkg-buf pos)
2720 (if (not (file-writable-p leim-list))
2721 (error "Can't write to file \"%s\"" leim-list))
2722 (message "Updating %s ..." leim-list)
2723 (setq list-buf (find-file-noselect leim-list))
2725 ;; At first, clean up the file.
2726 (with-current-buffer list-buf
2729 ;; Insert the correct header.
2730 (if (looking-at (regexp-quote leim-list-header))
2731 (goto-char (match-end 0))
2732 (insert leim-list-header))
2734 (if (not (re-search-forward leim-list-entry-regexp nil t))
2737 ;; Remove garbages after the header.
2738 (goto-char (match-beginning 0))
2740 (delete-region pos (point)))
2742 ;; Remove all entries for Quail.
2743 (while (re-search-forward leim-list-entry-regexp nil 'move)
2744 (goto-char (match-beginning 0))
2747 (let ((form (read list-buf)))
2748 (when (equal (nth 3 form) ''quail-use-package)
2749 (if (eolp) (forward-line 1))
2750 (delete-region pos (point))))
2752 ;; Delete the remaining contents because it seems that
2753 ;; this file is broken.
2754 (message "Garbage in %s deleted" leim-list)
2755 (delete-region pos (point-max)))))))
2757 ;; Search for `quail' subdirectory under each DIRNAMES.
2758 (setq dirnames (cons dirname dirnames))
2761 (setcar l (expand-file-name (car l)))
2762 (setq dirname (expand-file-name quail-directory-name (car l)))
2763 (if (file-readable-p dirname)
2764 (setq quail-dirs (cons dirname quail-dirs))
2765 (message "%s doesn't have `%s' subdirectory, just ignored"
2766 (car l) quail-directory-name)
2767 (setq quail-dirs (cons nil quail-dirs)))
2769 (setq quail-dirs (nreverse quail-dirs)))
2771 ;; Insert input method registering forms.
2773 (setq dirname (car quail-dirs))
2775 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2777 (message "Checking %s ..." (car pkg-list))
2779 (insert-file-contents (car pkg-list))
2780 (goto-char (point-min))
2781 (while (search-forward "(quail-define-package" nil t)
2782 (goto-char (match-beginning 0))
2784 (let ((form (read (current-buffer))))
2785 (with-current-buffer list-buf
2787 (format "(register-input-method
2791 (nth 1 form) ; PACKAGE-NAME
2792 (nth 2 form) ; LANGUAGE
2793 'quail-use-package ; ACTIVATE-FUNC
2794 (nth 3 form) ; PACKAGE-TITLE
2795 (progn ; PACKAGE-DESCRIPTION (one line)
2796 (string-match ".*" (nth 5 form))
2797 (match-string 0 (nth 5 form)))
2798 (file-relative-name ; PACKAGE-FILENAME
2799 (file-name-sans-extension (car pkg-list))
2802 ;; Ignore the remaining contents of this file.
2803 (goto-char (point-max))
2804 (message "Some part of \"%s\" is broken" (car pkg-list))))))
2805 (setq pkg-list (cdr pkg-list)))
2806 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2808 ;; At last, write out LEIM list file.
2809 (with-current-buffer list-buf
2810 (setq buffer-file-coding-system 'iso-2022-7bit)
2812 (kill-buffer list-buf)
2813 (message "Updating %s ... done" leim-list)))
2815 (defun quail-advice (args)
2816 "Advise users about the characters input by the current Quail package.
2817 The argument is a parameterized event of the form:
2818 (quail-advice STRING)
2819 where STRING is a string containing the input characters.
2820 If STRING has property `advice' and the value is a function,
2821 call it with one argument STRING."
2823 (let* ((string (nth 1 args))
2824 (func (get-text-property 0 'advice string)))
2825 (if (functionp func)
2826 (funcall func string))))
2828 (global-set-key [quail-advice] 'quail-advice)
2833 ;;; quail.el ends here