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, i18n
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.
45 ;; [There was an input method for Mule 2.3 called `Tamago' from the
46 ;; Japanese `TAkusan MAtasete GOmenasai', or `Sorry for having you
47 ;; wait so long'; this couldn't be included in Emacs 20. `Tamago' is
48 ;; Japanese for `egg' (implicitly a hen's egg). Handa-san made a
49 ;; smaller and simpler system; the smaller quail egg is also eaten in
50 ;; Japan. Maybe others will be egged on to write more sorts of input
58 "Quail: multilingual input method."
61 ;; Buffer local variables
63 (defvar quail-current-package nil
64 "The current Quail package, which depends on the current input method.
65 See the documentation of `quail-package-alist' for the format.")
66 (make-variable-buffer-local 'quail-current-package
)
67 (put 'quail-current-package
'permanent-local t
)
69 ;; Quail uses the following two buffers to assist users.
70 ;; A buffer to show available key sequence or translation list.
71 (defvar quail-guidance-buf nil
)
72 ;; A buffer to show completion list of the current key sequence.
73 (defvar quail-completion-buf nil
)
75 ;; Each buffer in which Quail is activated should use different
77 (make-variable-buffer-local 'quail-guidance-buf
)
78 (put 'quail-guidance-buf
'permanent-local t
)
80 ;; A main window showing Quail guidance buffer.
81 (defvar quail-guidance-win nil
)
82 (make-variable-buffer-local 'quail-guidance-win
)
84 (defvar quail-overlay nil
85 "Overlay which covers the current translation region of Quail.")
86 (make-variable-buffer-local 'quail-overlay
)
88 (defvar quail-conv-overlay nil
89 "Overlay which covers the text to be converted in Quail mode.")
90 (make-variable-buffer-local 'quail-conv-overlay
)
92 (defvar quail-current-key nil
93 "Current key for translation in Quail mode.")
94 (make-variable-buffer-local 'quail-current-key
)
96 (defvar quail-current-str nil
97 "Currently selected translation of the current key.")
98 (make-variable-buffer-local 'quail-current-str
)
100 (defvar quail-current-translations nil
101 "Cons of indices and vector of possible translations of the current key.
102 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
103 CURRENT is an index of the current translation,
104 START and END are indices of the start and end of the current block,
105 BLOCK is the current block index,
106 BLOCKS is a number of blocks of translation.")
107 (make-variable-buffer-local 'quail-current-translations
)
109 (defvar quail-current-data nil
110 "Any Lisp object holding information of current translation status.
111 When a key sequence is mapped to TRANS and TRANS is a cons
112 of actual translation and some Lisp object to be referred
113 for translating the longer key sequence, this variable is set
114 to that Lisp object.")
115 (make-variable-buffer-local 'quail-current-data
)
117 ;; Quail package handlers.
119 (defvar quail-package-alist nil
120 "List of Quail packages.
121 A Quail package is a list of these elements:
122 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
123 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
124 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
125 CONVERSION-KEYS, SIMPLE.
127 QUAIL-MAP is a data structure to map key strings to translations. For
128 the format, see the documentation of `quail-map-p'.
130 DECODE-MAP is an alist of translations and corresponding keys.
132 See the documentation of `quail-define-package' for the other elements.")
134 ;; Return various slots in the current quail-package.
136 (defsubst quail-name
()
137 "Return the name of the current Quail package."
138 (nth 0 quail-current-package
))
140 (defun quail-title ()
141 "Return the title of the current Quail package."
142 (let ((title (nth 1 quail-current-package
)))
143 ;; TITLE may be a string or a list. If it is a list, each element
144 ;; is a string or the form (VAR STR1 STR2), and the interpretation
145 ;; of the list is the same as that of mode-line-format.
151 (cond ((stringp x
) x
)
152 ((and (listp x
) (symbolp (car x
)) (= (length x
) 3))
153 (if (symbol-value (car x
))
154 (nth 1 x
) (nth 2 x
)))
158 (defsubst quail-map
()
159 "Return the translation map of the current Quail package."
160 (nth 2 quail-current-package
))
161 (defsubst quail-guidance
()
162 "Return an object used for `guidance' feature of the current Quail package.
163 See also the documentation of `quail-define-package'."
164 (nth 3 quail-current-package
))
165 (defsubst quail-docstring
()
166 "Return the documentation string of the current Quail package."
167 (nth 4 quail-current-package
))
168 (defsubst quail-translation-keymap
()
169 "Return translation keymap in the current Quail package.
170 Translation keymap is a keymap used while translation region is active."
171 (nth 5 quail-current-package
))
172 (defsubst quail-forget-last-selection
()
173 "Return `forget-last-selection' flag of the current Quail package.
174 See also the documentation of `quail-define-package'."
175 (nth 6 quail-current-package
))
176 (defsubst quail-deterministic
()
177 "Return `deterministic' flag of the current Quail package.
178 See also the documentation of `quail-define-package'."
179 (nth 7 quail-current-package
))
180 (defsubst quail-kbd-translate
()
181 "Return `kbd-translate' flag of the current Quail package.
182 See also the documentation of `quail-define-package'."
183 (nth 8 quail-current-package
))
184 (defsubst quail-show-layout
()
185 "Return `show-layout' flag of the current Quail package.
186 See also the documentation of `quail-define-package'."
187 (nth 9 quail-current-package
))
188 (defsubst quail-decode-map
()
189 "Return decode map of the current Quail package.
190 It is an alist of translations and corresponding keys."
191 (nth 10 quail-current-package
))
192 (defsubst quail-maximum-shortest
()
193 "Return `maximum-shortest' flag of the current Quail package.
194 See also the documentation of `quail-define-package'."
195 (nth 11 quail-current-package
))
196 (defsubst quail-overlay-plist
()
197 "Return property list of an overly used in the current Quail package."
198 (nth 12 quail-current-package
))
199 (defsubst quail-update-translation-function
()
200 "Return a function for updating translation in the current Quail package."
201 (nth 13 quail-current-package
))
202 (defsubst quail-conversion-keymap
()
203 "Return conversion keymap in the current Quail package.
204 Conversion keymap is a keymap used while conversion region is active
205 but translation region is not active."
206 (nth 14 quail-current-package
))
207 (defsubst quail-simple
()
208 "Return t if the current Quail package is simple."
209 (nth 15 quail-current-package
))
211 (defsubst quail-package
(name)
212 "Return Quail package named NAME."
213 (assoc name quail-package-alist
))
215 (defun quail-add-package (package)
216 "Add Quail package PACKAGE to `quail-package-alist'."
217 (let ((pac (quail-package (car package
))))
219 (setcdr pac
(cdr package
))
220 (setq quail-package-alist
(cons package quail-package-alist
)))))
222 (defun quail-select-package (name)
223 "Select Quail package named NAME as the current Quail package."
224 (let ((package (quail-package name
)))
226 (error "No Quail package `%s'" name
))
227 (setq quail-current-package package
)
228 (setq-default quail-current-package package
)
232 (defun quail-use-package (package-name &rest libraries
)
233 "Start using Quail package PACKAGE-NAME.
234 The remaining arguments are libraries to be loaded before using the package.
236 This activates input method defined by PACKAGE-NAME by running
237 `quail-activate', which see."
238 (let ((package (quail-package package-name
)))
240 ;; Perhaps we have not yet loaded necessary libraries.
242 (if (not (load (car libraries
) t
))
244 (with-output-to-temp-buffer "*Help*"
245 (princ "Quail package \"")
247 (princ "\" can't be activated\n because library \"")
248 (princ (car libraries
))
249 (princ "\" is not in `load-path'.
251 The most common case is that you have not yet installed appropriate
252 libraries in LEIM (Libraries of Emacs Input Method) which is
253 distributed separately from Emacs.
255 LEIM is available from the same ftp directory as Emacs."))
256 (error "Can't use the Quail package `%s'" package-name
))
257 (setq libraries
(cdr libraries
))))))
258 (quail-select-package package-name
)
259 (setq current-input-method-title
(quail-title))
261 ;; Hide all '... loaded' message.
264 (defvar quail-translation-keymap
265 (let ((map (make-keymap))
268 (define-key map
(char-to-string i
) 'quail-other-command
)
271 (define-key map
(char-to-string i
) 'quail-self-insert-command
)
275 (define-key map
(vector i
) 'quail-self-insert-command
)
277 (define-key map
"\177" 'quail-delete-last-char
)
278 (define-key map
"\C-f" 'quail-next-translation
)
279 (define-key map
"\C-b" 'quail-prev-translation
)
280 (define-key map
"\C-n" 'quail-next-translation-block
)
281 (define-key map
"\C-p" 'quail-prev-translation-block
)
282 (define-key map
[right] 'quail-next-translation)
283 (define-key map [left] 'quail-prev-translation)
284 (define-key map [down] 'quail-next-translation-block)
285 (define-key map [up] 'quail-prev-translation-block)
286 (define-key map "\C-i" 'quail-completion)
287 (define-key map "\C-@" 'quail-select-current)
288 ;; Following simple.el, Enter key on numeric keypad selects the
289 ;; current translation just like `C-SPC', and `mouse-2' chooses
290 ;; any completion visible in the *Quail Completions* buffer.
291 (define-key map [kp-enter] 'quail-select-current)
292 (define-key map [mouse-2] 'quail-mouse-choose-completion)
293 (define-key map [down-mouse-2] nil)
294 (define-key map "\C-h" 'quail-translation-help)
295 (define-key map [?\C- ] 'quail-select-current)
296 (define-key map [tab] 'quail-completion)
297 (define-key map [delete] 'quail-delete-last-char)
298 (define-key map [backspace] 'quail-delete-last-char)
300 "Keymap used processing translation in complex Quail modes.
301 Only a few especially complex input methods use this map;
302 most use `quail-simple-translation-keymap' instead.
303 This map is activated while translation region is active.")
305 (defvar quail-translation-docstring
306 "When you type keys, the echo area shows the possible characters
307 which correspond to that key sequence, each preceded by a digit. You
308 can select one of the characters shown by typing the corresponding
309 digit. Alternatively, you can use C-f and C-b to move through the
310 line to select the character you want, then type a letter to begin
311 entering another Chinese character or type a space or punctuation
314 If there are more than ten possible characters for the given spelling,
315 the echo area shows ten characters at a time; you can use C-n to move
316 to the next group of ten, and C-p to move back to the previous group
319 ;; Categorize each Quail commands to make the output of quail-help
320 ;; concise. This is done by putting `quail-help' property. The value
322 ;; hide -- never show this command
323 ;; non-deterministic -- show only for non-deterministic input method
324 (let ((l '((quail-other-command . hide)
325 (quail-self-insert-command . hide)
326 (quail-delete-last-char . hide)
327 (quail-next-translation . non-deterministic)
328 (quail-prev-translation . non-deterministic)
329 (quail-next-translation-block . non-deterministic)
330 (quail-prev-translation-block . non-deterministic))))
332 (put (car (car l)) 'quail-help (cdr (car l)))
335 (defvar quail-simple-translation-keymap
336 (let ((map (make-keymap))
339 (define-key map (char-to-string i) 'quail-other-command)
342 (define-key map (char-to-string i) 'quail-self-insert-command)
346 (define-key map (vector i) 'quail-self-insert-command)
348 (define-key map "\177" 'quail-delete-last-char)
349 (define-key map [delete] 'quail-delete-last-char)
350 (define-key map [backspace] 'quail-delete-last-char)
351 ;;(let ((meta-map (make-sparse-keymap)))
352 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
353 ;;(define-key map [escape] meta-map))
355 "Keymap used while processing translation in simple Quail modes.
356 A few especially complex input methods use `quail-translation-keymap' instead.
357 This map is activated while translation region is active.")
359 (defvar quail-conversion-keymap
360 (let ((map (make-keymap))
363 (define-key map (char-to-string i) 'quail-self-insert-command)
367 (define-key map (vector i) 'quail-self-insert-command)
369 (define-key map "\C-b" 'quail-conversion-backward-char)
370 (define-key map "\C-f" 'quail-conversion-forward-char)
371 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
372 (define-key map "\C-e" 'quail-conversion-end-of-region)
373 (define-key map "\C-d" 'quail-conversion-delete-char)
374 (define-key map "\C-k" 'quail-conversion-delete-tail)
375 (define-key map "\C-h" 'quail-translation-help)
376 (define-key map "\177" 'quail-conversion-backward-delete-char)
377 (define-key map [delete] 'quail-conversion-backward-delete-char)
378 (define-key map [backspace] 'quail-conversion-backward-delete-char)
380 "Keymap used for processing conversion in Quail mode.
381 This map is activated while conversion region is active but translation
382 region is not active.")
384 ;; Just a dummy definition.
385 (defun quail-other-command ()
390 (defun quail-define-package (name language title
391 &optional guidance docstring translation-keys
392 forget-last-selection deterministic
393 kbd-translate show-layout create-decode-map
394 maximum-shortest overlay-plist
395 update-translation-function
396 conversion-keys simple)
397 "Define NAME as a new Quail package for input LANGUAGE.
398 TITLE is a string to be displayed at mode-line to indicate this package.
399 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
400 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
401 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
402 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
404 GUIDANCE specifies how a guidance string is shown in echo area.
405 If it is t, list of all possible translations for the current key is shown
406 with the currently selected translation being highlighted.
407 If it is an alist, the element has the form (CHAR . STRING). Each character
408 in the current key is searched in the list and the corresponding string is
410 If it is nil, the current key is shown.
412 DOCSTRING is the documentation string of this package. The command
413 `describe-input-method' shows this string while replacing the form
414 \\=\\<VAR> in the string by the value of VAR. That value should be a
415 string. For instance, the form \\=\\<quail-translation-docstring> is
416 replaced by a description about how to select a translation from a
419 TRANSLATION-KEYS specifies additional key bindings used while translation
420 region is active. It is an alist of single key character vs. corresponding
421 command to be called.
423 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
424 for the future to translate the same key. If this flag is nil, a
425 translation selected for a key is remembered so that it can be the
426 first candidate when the same key is entered later.
428 DETERMINISTIC non-nil means the first candidate of translation is
429 selected automatically without allowing users to select another
430 translation for a key. In this case, unselected translations are of
431 no use for an interactive use of Quail but can be used by some other
432 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
435 KBD-TRANSLATE non-nil means input characters are translated from a
436 user's keyboard layout to the standard keyboard layout. See the
437 documentation of `quail-keyboard-layout' and
438 `quail-keyboard-layout-standard' for more detail.
440 SHOW-LAYOUT non-nil means the `quail-help' command should show
441 the user's keyboard layout visually with translated characters.
442 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
443 this package defines no translations for single character keys.
445 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
446 map is an alist of translations and corresponding original keys.
447 Although this map is not used by Quail itself, it can be used by some
448 other programs. For instance, Vietnamese supporting needs this map to
449 convert Vietnamese text to VIQR format which uses only ASCII
450 characters to represent Vietnamese characters.
452 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
453 length of the shortest sequence. When we don't have a translation of
454 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
455 the key at \"..AB\" and start translation of \"CD..\". Hangul
456 packages, for instance, use this facility. If this flag is nil, we
457 break the key just at \"..ABC\" and start translation of \"D..\".
459 OVERLAY-PLIST if non-nil is a property list put on an overlay which
460 covers Quail translation region.
462 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
463 the current translation region according to a new translation data. By
464 default, a translated text or a user's key sequence (if no translation
467 CONVERSION-KEYS specifies additional key bindings used while
468 conversion region is active. It is an alist of single key character
469 vs. corresponding command to be called.
471 If SIMPLE is non-nil, then we do not alter the meanings of
472 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
474 (let (translation-keymap conversion-keymap)
475 (if deterministic (setq forget-last-selection t))
478 (setq translation-keymap (copy-keymap
479 (if simple quail-simple-translation-keymap
480 quail-translation-keymap)))
481 (while translation-keys
482 (define-key translation-keymap
483 (car (car translation-keys)) (cdr (car translation-keys)))
484 (setq translation-keys (cdr translation-keys))))
485 (setq translation-keymap
486 (if simple quail-simple-translation-keymap
487 quail-translation-keymap)))
488 (when conversion-keys
489 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
490 (while conversion-keys
491 (define-key conversion-keymap
492 (car (car conversion-keys)) (cdr (car conversion-keys)))
493 (setq conversion-keys (cdr conversion-keys))))
495 (list name title (list nil) guidance (or docstring "")
497 forget-last-selection deterministic kbd-translate show-layout
498 (if create-decode-map (list 'decode-map) nil)
499 maximum-shortest overlay-plist update-translation-function
500 conversion-keymap simple))
502 ;; Update input-method-alist.
503 (let ((slot (assoc name input-method-alist))
504 (val (list language 'quail-use-package title docstring)))
505 (if slot (setcdr slot val)
506 (setq input-method-alist (cons (cons name val) input-method-alist)))))
508 (quail-select-package name))
510 ;; Quail minor mode handlers.
512 ;; Setup overlays used in Quail mode.
513 (defun quail-setup-overlays (conversion-mode)
515 (if (overlayp quail-overlay)
516 (move-overlay quail-overlay pos pos)
517 (setq quail-overlay (make-overlay pos pos nil nil t))
518 (if input-method-highlight-flag
519 (overlay-put quail-overlay 'face 'underline))
520 (let ((l (quail-overlay-plist)))
522 (overlay-put quail-overlay (car l) (car (cdr l)))
523 (setq l (cdr (cdr l))))))
525 (if (overlayp quail-conv-overlay)
526 (if (not (overlay-start quail-conv-overlay))
527 (move-overlay quail-conv-overlay pos pos))
528 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
529 (if input-method-highlight-flag
530 (overlay-put quail-conv-overlay 'face 'underline))))))
532 ;; Delete overlays used in Quail mode.
533 (defun quail-delete-overlays ()
534 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
535 (delete-overlay quail-overlay))
536 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
537 (delete-overlay quail-conv-overlay)))
539 ;; Kill Quail guidance buffer. Set in kill-buffer-hook.
540 (defun quail-kill-guidance-buf ()
541 (if (buffer-live-p quail-guidance-buf)
542 (kill-buffer quail-guidance-buf)))
544 (defun quail-inactivate ()
545 "Inactivate Quail input method.
547 This function runs the normal hook `quail-inactivate-hook'."
551 (defun quail-activate (&optional arg)
552 "Activate Quail input method.
553 With arg, activate Quail input method if and only if arg is positive.
555 This function runs `quail-activate-hook' if it activates the input
556 method, `quail-inactivate-hook' if it deactivates it.
558 While this input method is active, the variable
559 `input-method-function' is bound to the function `quail-input-method'."
561 (< (prefix-numeric-value arg) 0))
562 ;; Let's inactivate Quail input method.
565 (quail-hide-guidance-buf)
566 (quail-delete-overlays)
567 (setq describe-current-input-method-function nil)
568 (run-hooks 'quail-inactivate-hook))
569 (kill-local-variable 'input-method-function))
570 ;; Let's activate Quail input method.
571 (if (null quail-current-package)
572 ;; Quail package is not yet selected. Select one now.
574 (if quail-package-alist
575 (setq name (car (car quail-package-alist)))
576 (error "No Quail package loaded"))
577 (quail-select-package name)))
578 (setq inactivate-current-input-method-function 'quail-inactivate)
579 (setq describe-current-input-method-function 'quail-help)
580 (quail-delete-overlays)
581 (quail-show-guidance-buf)
582 ;; If we are in minibuffer, turn off the current input method
584 (if (eq (selected-window) (minibuffer-window))
585 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
586 (add-hook 'kill-buffer-hook 'quail-kill-guidance-buf nil t)
587 (run-hooks 'quail-activate-hook)
588 (make-local-variable 'input-method-function)
589 (setq input-method-function 'quail-input-method)))
591 (defun quail-exit-from-minibuffer ()
592 (inactivate-input-method)
593 (if (<= (minibuffer-depth) 1)
594 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
596 ;; Keyboard layout translation handlers.
598 ;; Some Quail packages provide localized keyboard simulation which
599 ;; requires a particular keyboard layout. In this case, what we need
600 ;; is locations of keys the user entered, not character codes
601 ;; generated by those keys. However, for the moment, there's no
602 ;; common way to get such information. So, we ask a user to give
603 ;; information of his own keyboard layout, then translate it to the
604 ;; standard layout which we defined so that all Quail packages depend
607 (defconst quail-keyboard-layout-standard
610 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
611 qQwWeErRtTyYuUiIoOpP[{]} \
612 aAsSdDfFgGhHjJkKlL;:'\"\\| \
613 zZxXcCvVbBnNmM,<.>/? \
615 "Standard keyboard layout of printable characters Quail assumes.
616 See the documentation of `quail-keyboard-layout' for this format.
617 This layout is almost the same as that of VT100,
618 but the location of key \\ (backslash) is just right of key ' (single-quote),
619 not right of RETURN key.")
621 (defvar quail-keyboard-layout quail-keyboard-layout-standard
622 "A string which represents physical key layout of a particular keyboard.
623 We assume there are six rows and each row has 15 keys (columns),
624 the first row is above the `1' - `0' row,
625 the first column of the second row is left of key `1',
626 the first column of the third row is left of key `q',
627 the first column of the fourth row is left of key `a',
628 the first column of the fifth row is left of key `z',
629 the sixth row is below the `z' - `/' row.
630 Nth (N is even) and (N+1)th characters in the string are non-shifted
631 and shifted characters respectively at the same location.
632 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
633 The command `quail-set-keyboard-layout' usually sets this variable.")
635 (defconst quail-keyboard-layout-len 180)
637 ;; Here we provide several examples of famous keyboard layouts.
638 ;; This is a candidate for a language environment-dependent setting.
639 (defvar quail-keyboard-layout-alist
641 (cons "standard" quail-keyboard-layout-standard)
644 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
645 qQwWeErRtTyYuUiIoOpP[{]} \
646 aAsSdDfFgGhHjJkKlL;:'\" \
647 zZxXcCvVbBnNmM,<.>/? \
649 '("atari-german" . "\
651 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
652 qQwWeErRtTzZuUiIoOpP\374\334+* \
653 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
654 <>yYxXcCvVbBnNmM,;.:-_ \
659 ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
660 qQwWeErRtTzZuUiIoOpP\374\334+* \
661 aAsSdDfFgGhHjJkKlL\366\326\344\304 \
662 <>yYxXcCvVbBnNmM,;.:-_ \
667 1!2\"3#4$5%6&7'8(9)0~-=^~\\| \
668 qQwWeErRtTyYuUiIoOpP@`[{ \
669 aAsSdDfFgGhHjJkKlL;+:*]} \
670 zZxXcCvVbBnNmM,<.>/?\\_ \
674 `\2541!2\3\243$5%6^7&8*9(0)-_=+ \
675 qQwWeErRtTyYuUiIoOpP[{]} \
676 aAsSdDfFgGhHjJkKlL;:'@#~ \
677 \\|zZxXcCvVbBnNmM,<.>/? \
680 "Alist of keyboard names and corresponding layout strings.
681 See the documentation of `quail-keyboard-layout' for the format of
684 ;; A non-standard keyboard layout may miss some key locations of the
685 ;; standard layout while having additional key locations not in the
686 ;; standard layout. This alist maps those additional key locations to
687 ;; the missing locations. The value is updated automatically by
688 ;; quail-set-keyboard-layout.
689 (defvar quail-keyboard-layout-substitution nil)
691 (defun quail-update-keyboard-layout (kbd-type)
692 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
694 ;; Here, we had better ask a user to define his own keyboard
695 ;; layout interactively.
696 (error "Unknown keyboard type `%s'" kbd-type))
697 (setq quail-keyboard-layout (cdr layout))
698 (let ((i quail-keyboard-layout-len)
699 subst-list missing-list)
700 ;; Sum up additional key locations not in the standard layout in
701 ;; subst-list, and missing key locations in missing-list.
704 (if (= (aref quail-keyboard-layout i) ? )
705 (if (/= (aref quail-keyboard-layout-standard i) ? )
706 (setq missing-list (cons i missing-list)))
707 (if (= (aref quail-keyboard-layout-standard i) ? )
708 (setq subst-list (cons (cons i nil) subst-list)))))
709 (setq quail-keyboard-layout-substitution subst-list)
710 ;; If there are additional key locations, map them to missing
713 (while (and subst-list (cdr (car subst-list)))
714 (setq subst-list (cdr subst-list)))
716 (setcdr (car subst-list) (car missing-list)))
717 (setq missing-list (cdr missing-list))))))
719 (defcustom quail-keyboard-layout-type "standard"
720 "Type of keyboard layout used in Quail base input method.
721 Available types are listed in the variable `quail-keyboard-layout-alist'."
723 :type (cons 'choice (mapcar (lambda (elt)
724 (list 'const (car elt)))
725 quail-keyboard-layout-alist))
726 :set #'(lambda (symbol value)
727 (quail-update-keyboard-layout value)
731 (defun quail-set-keyboard-layout (kbd-type)
732 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
734 Since some Quail packages depends on a physical layout of keys (not
735 characters generated by them), those are created by assuming the
736 standard layout defined in `quail-keyboard-layout-standard'. This
737 function tells Quail system the layout of your keyboard so that what
738 you type is correctly handled."
740 (let* ((completion-ignore-case t)
741 (type (completing-read "Keyboard type: "
742 quail-keyboard-layout-alist)))
744 (quail-update-keyboard-layout kbd-type)
745 (setq quail-keyboard-layout-type kbd-type))
747 (defun quail-keyboard-translate (char)
748 "Translate CHAR to the one in the standard keyboard layout."
749 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
750 ;; All Quail packages are designed based on
751 ;; `quail-keyboard-layout-standard'.
754 ;; Find the key location on the current keyboard layout.
755 (while (and (< i quail-keyboard-layout-len)
756 (/= char (aref quail-keyboard-layout i)))
758 (if (= i quail-keyboard-layout-len)
759 ;; CHAR is not in quail-keyboard-layout, which means that a
760 ;; user typed a key which generated a character code to be
761 ;; handled out of Quail. Just return CHAR and make
762 ;; quail-execute-non-quail-command handle it correctly.
764 (let ((ch (aref quail-keyboard-layout-standard i)))
766 ;; This location not available in the standard keyboard
767 ;; layout. Check if the location is used to substitute
768 ;; for the other location of the standard layout.
769 (if (setq i (cdr (assq i quail-keyboard-layout-substitution)))
770 (aref quail-keyboard-layout-standard i)
771 ;; Just return CHAR as well as above.
775 (defun quail-keyseq-translate (keyseq)
777 (mapcar (function (lambda (x) (quail-keyboard-translate x)))
780 (defun quail-insert-kbd-layout (kbd-layout)
781 "Insert the visual keyboard layout table according to KBD-LAYOUT.
782 The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
783 (let (done-list layout i ch)
784 ;; At first, convert KBD-LAYOUT to the same size vector that
785 ;; contains translated character or string.
786 (setq layout (string-to-vector kbd-layout)
788 (while (< i quail-keyboard-layout-len)
789 (setq ch (aref kbd-layout i))
790 (if (quail-kbd-translate)
791 (setq ch (quail-keyboard-translate ch)))
792 (let* ((map (cdr (assq ch (cdr (quail-map)))))
793 (translation (and map (quail-get-translation
794 (car map) (char-to-string ch) 1))))
797 (if (consp translation)
798 (setq translation (aref (cdr translation) 0)))
799 (setq done-list (cons translation done-list)))
800 (setq translation ch))
801 (aset layout i translation))
807 ;; Make table without horizontal lines. Each column for a key
808 ;; has the form "| LU |" where L is for lower key and and U is
809 ;; for a upper key. If width of L (U) is greater than 1,
810 ;; preceding (following) space is not inserted.
811 (put-text-property 0 1 'face 'bold bar)
813 (while (< i quail-keyboard-layout-len)
817 (insert-char 32 (+ row (/ (- row 2) 2)))))
818 (setq lower (aref layout i)
819 upper (aref layout (1+ i)))
820 (if (and (integerp lower) (>= lower 128) (< lower 256))
821 (setq lower (unibyte-char-to-multibyte lower)))
822 (if (and (integerp upper) (>= upper 128) (< upper 256))
823 (setq upper (unibyte-char-to-multibyte upper)))
825 (if (= (if (stringp lower) (string-width lower) (char-width lower)) 1)
828 (if (= (if (stringp upper) (string-width upper) (char-width upper)) 1)
833 ;; Insert horizontal lines while deleting blank key columns at the
834 ;; beginning and end of each line.
836 (narrow-to-region pos (point))
838 ;;(while (looking-at "[| ]*$")
840 ;;(delete-region pos (point)))
841 (let ((from1 100) (to1 0) from2 to2)
843 (if (looking-at "[| ]*$")
844 ;; The entire row is blank.
845 (delete-region (point) (match-end 0))
846 ;; Delete blank key columns at the head.
847 (if (looking-at " *\\(| \\)+")
848 (subst-char-in-region (point) (match-end 0) ?| ? ))
849 ;; Delete blank key columns at the tail.
850 (if (re-search-forward "\\( |\\)+$" (line-end-position) t)
851 (delete-region (match-beginning 0) (point)))
853 ;; Calculate the start and end columns of a horizontal line.
855 (setq from2 from1 to2 to1)
856 (skip-chars-forward " ")
857 (setq from2 (current-column))
859 (setq to2 (current-column))
865 ;; If the previous or the current line has at least one key
866 ;; column, insert a horizontal line.
868 (insert-char 32 from1)
871 (insert-char ?- (- (- to1 from1) 2))
873 (put-text-property pos (point) 'face 'bold)
875 (setq from1 from2 to1 to2)
877 ;; Insert "space bar" box.
881 " +-----------------------------+
883 +-----------------------------+
885 (put-text-property pos (point) 'face 'bold)
891 (defun quail-show-keyboard-layout (&optional keyboard-type)
892 "Show the physical layout of the keyboard type KEYBOARD-TYPE.
894 The variable `quail-keyboard-layout-type' holds the currently selected
897 (list (completing-read "Keyboard type (default, current choise): "
898 quail-keyboard-layout-alist
900 (or (and keyboard-type (> (length keyboard-type) 0))
901 (setq keyboard-type quail-keyboard-layout-type))
902 (let ((layout (assoc keyboard-type quail-keyboard-layout-alist)))
904 (error "Unknown keyboard type: %s" keyboard-type))
905 (with-output-to-temp-buffer "*Help*"
906 (with-current-buffer standard-output
907 (insert "Keyboard layout (keyboard type: "
910 (quail-insert-kbd-layout (cdr layout))))))
914 (defsubst quail-map-p (object)
915 "Return t if OBJECT is a Quail map.
917 A Quail map holds information how a particular key should be translated.
918 Its format is (TRANSLATION . ALIST).
919 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
920 In the latter case, each element of VECTOR is a candidate for the translation,
921 and INDEX points the currently selected translation.
923 ALIST is normally a list of elements that look like (CHAR . DEFN),
924 where DEFN is another Quail map for a longer key (CHAR added to the
925 current key). It may also be a symbol of a function which returns an
926 alist of the above format.
928 Just after a Quail package is read, TRANSLATION may be a string or a
929 vector. Then each element of the string or vector is a candidate for
930 the translation. These objects are transformed to cons cells in the
931 format \(INDEX . VECTOR), as described above."
933 (let ((translation (car object)))
934 (or (integerp translation) (null translation)
935 (vectorp translation) (stringp translation)
936 (symbolp translation)
937 (and (consp translation) (not (vectorp (cdr translation))))))
938 (let ((alist (cdr object)))
939 (or (and (listp alist) (consp (car alist)))
943 (defmacro quail-define-rules (&rest rules)
944 "Define translation rules of the current Quail package.
945 Each argument is a list of KEY and TRANSLATION.
946 KEY is a string meaning a sequence of keystrokes to be translated.
947 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
948 If it is a character, it is the sole translation of KEY.
949 If it is a string, each character is a candidate for the translation.
950 If it is a vector, each element (string or character) is a candidate
952 In these cases, a key specific Quail map is generated and assigned to KEY.
954 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
955 it is used to handle KEY.
957 The first argument may be an alist of annotations for the following
958 rules. Each element has the form (ANNOTATION . VALUE), where
959 ANNOTATION is a symbol indicating the annotation type. Currently
960 the following annotation types are supported.
962 append -- the value non-nil means that the following rules should
963 be appended to the rules of the current Quail package.
965 face -- the value is a face to use for displaying TRANSLATIONs in
968 advice -- the value is a function to call after one of RULES is
969 selected. The function is called with one argument, the
970 selected TRANSLATION string, after the TRANSLATION is
973 no-decode-map --- the value non-nil means that decoding map is not
974 generated for the following translations."
976 append no-decode-map props)
977 ;; If the first argument is an alist of annotations, handle them.
978 (if (consp (car (car l)))
979 (let ((annotations (car l)))
980 (setq append (assq 'append annotations))
982 (setq annotations (delete append annotations)
983 append (cdr append)))
984 (setq no-decode-map (assq 'no-decode-map annotations))
986 (setq annotations (delete no-decode-map annotations)
987 no-decode-map (cdr no-decode-map)))
988 ;; Convert the remaining annoations to property list PROPS.
991 (cons (car (car annotations))
992 (cons (cdr (car annotations))
994 annotations (cdr annotations)))
996 ;; Process the remaining arguments one by one.
998 ;; There's no way to add new rules at compiling time.
1001 (decode-map (and (quail-decode-map) (not ,no-decode-map)))
1002 (properties ',props)
1005 (setq key (car (car tail)) trans (car (cdr (car tail)))
1007 (quail-defrule-internal key trans map t decode-map properties)))
1008 ;; We can build up quail map and decode map at compiling time.
1009 (let ((map (list nil))
1010 (decode-map (if (not no-decode-map) (list 'decode-map)))
1013 (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
1014 (quail-defrule-internal key trans map t decode-map props))
1015 `(if (not (quail-decode-map))
1016 (quail-install-map ',map)
1017 (quail-install-map ',map)
1018 (quail-install-decode-map ',decode-map))))))
1021 (defun quail-install-map (map &optional name)
1022 "Install the Quail map MAP in the current Quail package.
1024 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1025 which to install MAP.
1027 The installed map can be referred by the function `quail-map'."
1028 (if (null quail-current-package)
1029 (error "No current Quail package"))
1030 (if (null (quail-map-p map))
1031 (error "Invalid Quail map `%s'" map))
1032 (setcar (cdr (cdr quail-current-package)) map))
1035 (defun quail-install-decode-map (decode-map &optional name)
1036 "Install the Quail decode map DECODE-MAP in the current Quail package.
1038 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1039 which to install MAP.
1041 The installed decode map can be referred by the function `quail-decode-map'."
1042 (if (null quail-current-package)
1043 (error "No current Quail package"))
1044 (if (not (and (consp decode-map) (eq (car decode-map) 'decode-map)))
1045 (error "Invalid Quail decode map `%s'" decode-map))
1046 (setcar (nthcdr 10 quail-current-package) decode-map))
1049 (defun quail-defrule (key translation &optional name append)
1050 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
1051 KEY is a string meaning a sequence of keystrokes to be translated.
1052 TRANSLATION is a character, a string, a vector, a Quail map,
1053 a function, or a cons.
1054 It it is a character, it is the sole translation of KEY.
1055 If it is a string, each character is a candidate for the translation.
1056 If it is a vector, each element (string or character) is a candidate
1057 for the translation.
1058 If it is a cons, the car is one of the above and the cdr is a function
1059 to call when translating KEY (the return value is assigned to the
1060 variable `quail-current-data'). If the cdr part is not a function,
1061 the value itself is assigned to `quail-current-data'.
1062 In these cases, a key specific Quail map is generated and assigned to KEY.
1064 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
1065 it is used to handle KEY.
1067 Optional 3rd argument NAME, if specified, says which Quail package
1068 to define this translation rule in. The default is to define it in the
1069 current Quail package.
1071 Optional 4th argument APPEND, if non-nil, appends TRANSLATION
1072 to the current translations for KEY instead of replacing them."
1074 (let ((package (quail-package name)))
1076 (error "No Quail package `%s'" name))
1077 (setq quail-current-package package)))
1078 (quail-defrule-internal key translation (quail-map) append))
1081 (defun quail-defrule-internal (key trans map &optional append decode-map props)
1082 "Define KEY as TRANS in a Quail map MAP.
1084 If Optional 4th arg APPEND is non-nil, TRANS is appended to the
1085 current translations for KEY instead of replacing them.
1087 Optional 5th arg DECODE-MAP is a Quail decode map.
1089 Optional 6th arg PROPS is a property list annotating TRANS. See the
1090 function `quail-define-rules' for the detail."
1091 (if (null (stringp key))
1092 "Invalid Quail key `%s'" key)
1093 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
1096 (quail-map-p trans)))
1097 (error "Invalid Quail translation `%s'" trans))
1098 (if (null (quail-map-p map))
1099 (error "Invalid Quail map `%s'" map))
1100 (let ((len (length key))
1103 ;; Make a map for registering TRANS if necessary.
1105 (if (null (consp map))
1106 ;; We come here, for example, when we try to define a rule
1107 ;; for "ABC" but a rule for "AB" is already defined as a
1109 (error "Quail key %s is too long" key))
1110 (setq ch (aref key idx)
1111 entry (assq ch (cdr map)))
1114 (setq entry (cons ch (list nil)))
1115 (setcdr map (cons entry (cdr map)))))
1116 (setq map (cdr entry))
1117 (setq idx (1+ idx)))
1120 ;; We come here, for example, when we try to define a rule
1121 ;; for "AB" as a symbol but a rule for "ABC" is already
1123 (error "Quail key %s is too short" key)
1124 (setcdr entry trans))
1125 (if (quail-map-p trans)
1126 (if (not (listp (cdr map)))
1127 ;; We come here, for example, when we try to define a rule
1128 ;; for "AB" as a symbol but a rule for "ABC" is already
1130 (error "Quail key %s is too short" key)
1131 (if (not (listp (cdr trans)))
1133 ;; We come here, for example, when we try to
1134 ;; define a rule for "AB" as a symbol but a rule
1135 ;; for "ABC" is already defined.
1136 (error "Quail key %s is too short" key)
1137 (setcdr entry trans))
1138 (setcdr entry (append trans (cdr map)))))
1139 ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS
1140 ;; to a vector of strings, add PROPS to each string and record
1141 ;; this rule in DECODE-MAP.
1142 (when (and (or props decode-map)
1143 (not (consp trans)) (not (symbolp trans)))
1144 (if (integerp trans)
1145 (setq trans (vector trans))
1147 (setq trans (string-to-vector trans))))
1148 (let ((len (length trans))
1152 (setq elt (aref trans len))
1154 (setq elt (char-to-string elt)))
1155 (aset trans len elt)
1157 (add-text-properties 0 (length elt) props elt))
1160 (cons (cons elt key) (cdr decode-map)))))))
1161 (if (and (car map) append)
1162 (let ((prev (quail-get-translation (car map) key len)))
1164 (setq prev (vector prev))
1165 (setq prev (cdr prev)))
1166 (if (integerp trans)
1167 (setq trans (vector trans))
1169 (setq trans (string-to-vector trans))))
1171 (cons (list 0 0 0 0 nil)
1172 (vconcat prev trans)))))
1173 (setcar map trans)))))
1175 (defun quail-get-translation (def key len)
1176 "Return the translation specified as DEF for KEY of length LEN.
1177 The translation is either a character or a cons of the form (INDEX . VECTOR),
1178 where VECTOR is a vector of candidates (character or string) for
1179 the translation, and INDEX points into VECTOR to specify the currently
1180 selected translation."
1181 (if (and def (symbolp def))
1183 ;; DEF is a symbol of a function which returns valid translation.
1184 (setq def (funcall def key len))
1186 (if (and (consp def) (not (vectorp (cdr def))))
1187 (setq def (car def)))
1190 ((or (integerp def) (consp def))
1198 ;; If the length is 1, we don't need vector but a single candidate
1199 ;; as the translation.
1200 (if (= (length def) 1)
1202 ;; Each character in DEF is a candidate of translation. Reform
1203 ;; it as (INDICES . VECTOR).
1204 (cons (list 0 0 0 0 nil) (string-to-vector def))))
1207 ;; If the length is 1, and the length of element string is 1, we
1208 ;; don't need vector but a single candidate as the translation.
1209 (if (and (= (length def) 1)
1210 (= (length (aref def 0)) 1))
1211 (aref (aref def 0) 0)
1212 ;; Each element (string or character) in DEF is a candidate of
1213 ;; translation. Reform it as (INDICES . VECTOR).
1214 (cons (list 0 0 0 0 nil) def)))
1217 (error "Invalid object in Quail map: %s" def))))
1219 (defun quail-lookup-key (key &optional len)
1220 "Lookup KEY of length LEN in the current Quail map and return the definition.
1221 The returned value is a Quail map specific to KEY."
1223 (setq len (length key)))
1226 (kbd-translate (quail-kbd-translate))
1227 slot ch translation def)
1228 (while (and map (< idx len))
1229 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
1232 (if (and (cdr map) (symbolp (cdr map)))
1233 (setcdr map (funcall (cdr map) key idx)))
1234 (setq slot (assq ch (cdr map)))
1235 (if (and (cdr slot) (symbolp (cdr slot)))
1236 (setcdr slot (funcall (cdr slot) key idx)))
1237 (setq map (cdr slot)))
1238 (setq def (car map))
1239 (setq quail-current-translations nil)
1240 (if (and map (setq translation (quail-get-translation def key len)))
1242 (if (and (consp def) (not (vectorp (cdr def))))
1244 (if (not (equal (car def) translation))
1245 ;; We must reflect TRANSLATION to car part of DEF.
1246 (setcar def translation))
1247 (setq quail-current-data
1248 (if (functionp (cdr def))
1251 (if (not (equal def translation))
1252 ;; We must reflect TRANSLATION to car part of MAP.
1253 (setcar map translation)))
1254 (if (and (consp translation) (vectorp (cdr translation)))
1256 (setq quail-current-translations translation)
1257 (if (quail-forget-last-selection)
1258 (setcar (car quail-current-translations) 0))))))
1259 ;; We may have to reform cdr part of MAP.
1260 (if (and (cdr map) (functionp (cdr map)))
1261 (setcdr map (funcall (cdr map) key len)))
1264 (put 'quail-error 'error-conditions '(quail-error error))
1265 (defun quail-error (&rest args)
1266 (signal 'quail-error (apply 'format args)))
1268 (defun quail-input-string-to-events (str)
1269 "Convert input string STR to a list of events.
1270 Do so while interleaving with the following special events:
1271 \(compose-last-chars LEN COMPONENTS)
1272 \(quail-advice INPUT-STRING)"
1273 (let* ((events (mapcar
1275 ;; This gives us the chance to unify on input
1276 ;; (e.g. using ucs-tables.el).
1277 (or (and translation-table-for-input
1278 (aref translation-table-for-input c))
1283 composition from to)
1284 (while (and (> idx 0)
1285 (setq composition (find-composition idx 0 str t)))
1286 (setq from (car composition) to (nth 1 composition))
1287 (setcdr (nthcdr (1- to) events)
1288 (cons (list 'compose-last-chars (- to from)
1289 (and (not (nth 3 composition)) (nth 2 composition)))
1290 (nthcdr to events)))
1291 (setq idx (1- from)))
1292 (if (or (get-text-property 0 'advice str)
1293 (next-single-property-change 0 'advice str))
1295 (nconc events (list (list 'quail-advice str)))))
1298 (defvar quail-translating nil)
1299 (defvar quail-converting nil)
1300 (defvar quail-conversion-str nil)
1302 (defun quail-input-method (key)
1303 (if (or buffer-read-only
1304 overriding-terminal-local-map
1305 overriding-local-map)
1307 (quail-setup-overlays (quail-conversion-keymap))
1308 (let ((modified-p (buffer-modified-p))
1309 (buffer-undo-list t))
1310 (or (and quail-guidance-win
1311 (window-live-p quail-guidance-win)
1312 (eq (window-buffer quail-guidance-win) quail-guidance-buf)
1313 (not input-method-use-echo-area))
1314 (quail-show-guidance-buf))
1316 (let ((input-string (if (quail-conversion-keymap)
1317 (quail-start-conversion key)
1318 (quail-start-translation key))))
1319 (when (and (stringp input-string)
1320 (> (length input-string) 0))
1321 (if input-method-exit-on-first-char
1322 (list (aref input-string 0))
1323 (quail-input-string-to-events input-string))))
1324 (quail-delete-overlays)
1325 (if (buffer-live-p quail-guidance-buf)
1326 (with-current-buffer quail-guidance-buf
1328 (quail-hide-guidance-buf)
1329 (set-buffer-modified-p modified-p)
1330 ;; Run this hook only when the current input method doesn't require
1331 ;; conversion. When conversion is required, the conversion function
1332 ;; should run this hook at a proper timing.
1333 (unless (quail-conversion-keymap)
1334 (run-hooks 'input-method-after-insert-chunk-hook))))))
1336 (defun quail-overlay-region-events (overlay)
1337 (let ((start (overlay-start overlay))
1338 (end (overlay-end overlay)))
1341 (string-to-list (buffer-substring start end))
1342 (delete-region start end)))))
1344 (defsubst quail-delete-region ()
1345 "Delete the text in the current translation region of Quail."
1346 (if (overlay-start quail-overlay)
1347 (delete-region (overlay-start quail-overlay)
1348 (overlay-end quail-overlay))))
1350 (defun quail-start-translation (key)
1351 "Start translation of the typed character KEY by the current Quail package.
1352 Return the input string."
1353 ;; Check the possibility of translating KEY.
1354 ;; If KEY is nil, we can anyway start translation.
1355 (if (or (and (integerp key)
1356 (assq (if (quail-kbd-translate)
1357 (quail-keyboard-translate key) key)
1360 ;; OK, we can start translation.
1361 (let* ((echo-keystrokes 0)
1363 (overriding-terminal-local-map (quail-translation-keymap))
1364 (generated-events nil)
1365 (input-method-function nil)
1366 (modified-p (buffer-modified-p))
1367 last-command-event last-command this-command)
1368 (setq quail-current-key ""
1369 quail-current-str ""
1370 quail-translating t)
1372 (setq unread-command-events (cons key unread-command-events)))
1373 (while quail-translating
1374 (set-buffer-modified-p modified-p)
1375 (let* ((keyseq (read-key-sequence
1376 (and input-method-use-echo-area
1377 (concat input-method-previous-message
1380 (cmd (lookup-key (quail-translation-keymap) keyseq)))
1382 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
1383 (eq cmd 'quail-self-insert-command))
1385 (setq last-command-event (aref keyseq (1- (length keyseq)))
1386 last-command this-command
1390 (call-interactively cmd)
1391 (quail-error (message "%s" (cdr err)) (beep))))
1392 ;; KEYSEQ is not defined in the translation keymap.
1393 ;; Let's return the event(s) to the caller.
1394 (setq unread-command-events
1395 (string-to-list (this-single-command-raw-keys)))
1396 (setq quail-translating nil))))
1397 (quail-delete-region)
1400 ;; Since KEY doesn't start any translation, just return it.
1401 ;; But translate KEY if necessary.
1402 (if (quail-kbd-translate)
1403 (setq key (quail-keyboard-translate key)))
1404 (char-to-string key)))
1406 (defun quail-start-conversion (key)
1407 "Start conversion of the typed character KEY by the current Quail package.
1408 Return the input string."
1409 ;; Check the possibility of translating KEY.
1410 ;; If KEY is nil, we can anyway start translation.
1411 (if (or (and (integerp key)
1412 (assq (if (quail-kbd-translate)
1413 (quail-keyboard-translate key) key)
1416 ;; Ok, we can start translation and conversion.
1417 (let* ((echo-keystrokes 0)
1419 (overriding-terminal-local-map (quail-conversion-keymap))
1420 (generated-events nil)
1421 (input-method-function nil)
1422 (modified-p (buffer-modified-p))
1423 last-command-event last-command this-command)
1424 (setq quail-current-key ""
1425 quail-current-str ""
1428 quail-conversion-str "")
1430 (setq unread-command-events (cons key unread-command-events)))
1431 (while quail-converting
1432 (set-buffer-modified-p modified-p)
1433 (or quail-translating
1435 (setq quail-current-key ""
1436 quail-current-str ""
1437 quail-translating t)
1438 (quail-setup-overlays nil)))
1439 ;; Hide '... loaded' message.
1441 (let* ((keyseq (read-key-sequence
1442 (and input-method-use-echo-area
1443 (concat input-method-previous-message
1444 quail-conversion-str
1447 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1448 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1450 (setq last-command-event (aref keyseq (1- (length keyseq)))
1451 last-command this-command
1455 (call-interactively cmd)
1456 (quail-error (message "%s" (cdr err)) (beep)))
1457 (or quail-translating
1459 (if quail-current-str
1460 (setq quail-conversion-str
1461 (concat quail-conversion-str
1462 (if (stringp quail-current-str)
1464 (char-to-string quail-current-str)))))
1465 (if (or input-method-exit-on-first-char
1466 (= (length quail-conversion-str) 0))
1467 (setq quail-converting nil)))))
1468 ;; KEYSEQ is not defined in the conversion keymap.
1469 ;; Let's return the event(s) to the caller.
1470 (setq unread-command-events
1471 (string-to-list (this-single-command-raw-keys)))
1472 (setq quail-converting nil))))
1473 (setq quail-translating nil)
1474 (if (overlay-start quail-conv-overlay)
1475 (delete-region (overlay-start quail-conv-overlay)
1476 (overlay-end quail-conv-overlay)))
1477 (if (> (length quail-conversion-str) 0)
1478 quail-conversion-str))
1480 ;; Since KEY doesn't start any translation, just return it.
1481 ;; But translate KEY if necessary.
1482 (if (quail-kbd-translate)
1483 (setq key (quail-keyboard-translate key)))
1484 (char-to-string key)))
1486 (defun quail-terminate-translation ()
1487 "Terminate the translation of the current key."
1488 (setq quail-translating nil)
1489 (if (buffer-live-p quail-guidance-buf)
1490 (with-current-buffer quail-guidance-buf
1493 (defun quail-select-current ()
1494 "Accept the currently selected translation."
1496 (quail-terminate-translation))
1498 (defun quail-update-translation (control-flag)
1499 "Update the current translation status according to CONTROL-FLAG.
1500 If CONTROL-FLAG is integer value, it is the number of keys in the
1501 head `quail-current-key' which can be translated. The remaining keys
1502 are put back to `unread-command-events' to be handled again. If
1503 CONTROL-FLAG is t, terminate the translation for the whole keys in
1504 `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation
1506 (let ((func (quail-update-translation-function)))
1508 (setq control-flag (funcall func control-flag))
1509 (cond ((numberp control-flag)
1510 (let ((len (length quail-current-key)))
1511 (if (= control-flag 0)
1512 (setq quail-current-str
1513 (if (quail-kbd-translate)
1514 (quail-keyseq-translate quail-current-key)
1515 quail-current-key)))
1516 (or input-method-exit-on-first-char
1517 (while (> len control-flag)
1519 (setq unread-command-events
1520 (cons (aref quail-current-key len)
1521 unread-command-events))))))
1522 ((null control-flag)
1523 (unless quail-current-str
1524 (setq quail-current-str
1525 (if (quail-kbd-translate)
1526 (quail-keyseq-translate quail-current-key)
1528 (if (and input-method-exit-on-first-char
1530 (setq control-flag t)))))))
1531 (or input-method-use-echo-area
1533 (quail-delete-region)
1534 (insert quail-current-str)))
1535 (let (quail-current-str)
1536 (quail-update-guidance))
1537 (or (stringp quail-current-str)
1538 (setq quail-current-str (char-to-string quail-current-str)))
1540 (quail-terminate-translation)))
1542 (defun quail-self-insert-command ()
1543 "Translate the typed key by the current Quail map, and insert."
1545 (setq quail-current-key
1546 (concat quail-current-key (char-to-string last-command-event)))
1547 (or (catch 'quail-tag
1548 (quail-update-translation (quail-translate-key))
1550 ;; If someone throws for `quail-tag' by value nil, we exit from
1551 ;; translation mode.
1552 (setq quail-translating nil)))
1554 (defun quail-map-definition (map)
1555 "Return the actual definition part of Quail map MAP."
1556 (let ((def (car map)))
1557 (if (and (consp def) (not (vectorp (cdr def))))
1558 (setq def (car def)))
1563 (defun quail-get-current-str (len def)
1564 "Return string to be shown as current translation of key sequence.
1565 LEN is the length of the sequence. DEF is a definition part of the
1566 Quail map for the sequence."
1567 (or (and (consp def) (aref (cdr def) (car (car def))))
1570 (let ((str (quail-get-current-str
1572 (quail-map-definition (quail-lookup-key
1573 quail-current-key (1- len))))))
1575 (concat (if (stringp str) str (char-to-string str))
1576 (substring quail-current-key (1- len) len)))))))
1578 (defvar quail-guidance-translations-starting-column 20)
1580 (defun quail-update-current-translations (&optional relative-index)
1581 "Update `quail-current-translations'.
1582 Make RELATIVE-INDEX the current translation."
1583 (let* ((indices (car quail-current-translations))
1585 (start (nth 1 indices))
1586 (end (nth 2 indices)))
1587 ;; Validate the index number of current translation.
1589 (setcar indices (setq cur 0))
1590 (if (>= cur (length (cdr quail-current-translations)))
1592 (setq cur (1- (length (cdr quail-current-translations)))))))
1594 (if (or (null end) ; We have not yet calculated END.
1595 (< cur start) ; We moved to the previous block.
1596 (>= cur end)) ; We moved to the next block.
1597 (let ((len (length (cdr quail-current-translations)))
1598 (maxcol (- (window-width quail-guidance-win)
1599 quail-guidance-translations-starting-column))
1600 (block (nth 3 indices))
1601 col idx width trans num-items blocks)
1603 ;; We must calculate from the head.
1604 (setq start 0 block 0)
1605 (if end ; i.e. (>= cur end)
1607 (setq idx start col 0 end start num-items 0)
1608 ;; Loop until we hit the tail, or reach the block of CUR.
1609 (while (and (< idx len) (>= cur end))
1611 (setq start idx col 0 block (1+ block)))
1612 (setq trans (aref (cdr quail-current-translations) idx))
1613 (setq width (if (integerp trans) (char-width trans)
1614 (string-width trans)))
1615 (setq col (+ col width 3) num-items (1+ num-items))
1616 (if (and (> num-items 0)
1617 (or (>= col maxcol) (> num-items 10)))
1618 (setq end idx num-items 0)
1619 (setq idx (1+ idx))))
1620 (setcar (nthcdr 3 indices) block)
1623 ;; We hit the tail before reaching MAXCOL.
1625 (setcar (nthcdr 4 indices) block)))
1626 (setcar (cdr indices) start)
1627 (setcar (nthcdr 2 indices) end)))
1629 (if (>= (+ start relative-index) end)
1630 (setcar indices (1- end))
1631 (setcar indices (+ start relative-index))))
1632 (setq quail-current-str
1633 (aref (cdr quail-current-translations) (car indices)))
1634 (or (stringp quail-current-str)
1635 (setq quail-current-str (char-to-string quail-current-str)))))
1637 (defun quail-translate-key ()
1638 "Translate the current key sequence according to the current Quail map.
1639 Return t if we can terminate the translation.
1640 Return nil if the current key sequence may be followed by more keys.
1641 Return number if we can't find any translation for the current key
1642 sequence. The number is the count of valid keys in the current
1643 sequence counting from the head."
1644 (let* ((len (length quail-current-key))
1645 (map (quail-lookup-key quail-current-key len))
1648 (let ((def (quail-map-definition map)))
1649 (setq quail-current-str (quail-get-current-str len def))
1650 ;; Return t only if we can terminate the current translation.
1652 ;; No alternative translations.
1653 (or (null (consp def)) (= (length (cdr def)) 1))
1654 ;; No translation for the longer key.
1656 ;; No shorter breaking point.
1657 (or (null (quail-maximum-shortest))
1659 (null (quail-lookup-key quail-current-key (1- len)))
1660 (null (quail-lookup-key
1661 (substring quail-current-key -2 -1) 1)))))
1663 ;; There's no translation for the current key sequence. Before
1664 ;; giving up, we must check two possibilities.
1666 (quail-maximum-shortest)
1668 (setq def (quail-map-definition
1669 (quail-lookup-key quail-current-key (- len 2))))
1670 (quail-lookup-key (substring quail-current-key -2) 2))
1671 ;; Now the sequence is "...ABCD", which can be split into
1672 ;; "...AB" and "CD..." to get valid translation.
1673 ;; At first, get translation of "...AB".
1674 (setq quail-current-str (quail-get-current-str (- len 2) def))
1675 ;; Then, return the length of "...AB".
1679 (quail-lookup-key (substring quail-current-key 0 -1))
1680 quail-current-translations
1681 (not (quail-deterministic))
1682 (setq ch (aref quail-current-key (1- len)))
1683 (>= ch ?0) (<= ch ?9))
1684 ;; A numeric key is entered to select a desirable translation.
1685 (setq quail-current-key (substring quail-current-key 0 -1))
1686 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1687 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1688 (quail-update-current-translations ch)
1689 ;; And, we can terminate the current translation.
1693 ;; No way to handle the last character in this context.
1696 (defun quail-next-translation ()
1697 "Select next translation in the current batch of candidates."
1699 (if quail-current-translations
1700 (let ((indices (car quail-current-translations)))
1701 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1702 ;; We are already at the tail.
1704 (setcar indices (1+ (car indices)))
1705 (quail-update-current-translations)
1706 (quail-update-translation nil)))
1707 (setq unread-command-events
1708 (cons last-command-event unread-command-events))
1709 (quail-terminate-translation)))
1711 (defun quail-prev-translation ()
1712 "Select previous translation in the current batch of candidates."
1714 (if quail-current-translations
1715 (let ((indices (car quail-current-translations)))
1716 (if (= (car indices) 0)
1717 ;; We are already at the head.
1719 (setcar indices (1- (car indices)))
1720 (quail-update-current-translations)
1721 (quail-update-translation nil)))
1722 (setq unread-command-events
1723 (cons last-command-event unread-command-events))
1724 (quail-terminate-translation)))
1726 (defun quail-next-translation-block ()
1727 "Select from the next block of translations."
1729 (if quail-current-translations
1730 (let* ((indices (car quail-current-translations))
1731 (offset (- (car indices) (nth 1 indices))))
1732 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1733 ;; We are already at the last block.
1735 (setcar indices (+ (nth 2 indices) offset))
1736 (quail-update-current-translations)
1737 (quail-update-translation nil)))
1738 (setq unread-command-events
1739 (cons last-command-event unread-command-events))
1740 (quail-terminate-translation)))
1742 (defun quail-prev-translation-block ()
1743 "Select the previous batch of 10 translation candidates."
1745 (if quail-current-translations
1746 (let* ((indices (car quail-current-translations))
1747 (offset (- (car indices) (nth 1 indices))))
1748 (if (= (nth 1 indices) 0)
1749 ;; We are already at the first block.
1751 (setcar indices (1- (nth 1 indices)))
1752 (quail-update-current-translations)
1753 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1755 (setcar indices (+ (nth 1 indices) offset))
1756 (quail-update-current-translations)))
1757 (quail-update-translation nil)))
1758 (setq unread-command-events
1759 (cons last-command-event unread-command-events))
1760 (quail-terminate-translation)))
1762 (defun quail-abort-translation ()
1763 "Abort translation and delete the current Quail key sequence."
1765 (quail-delete-region)
1766 (setq quail-current-str nil)
1767 (quail-terminate-translation))
1769 (defun quail-delete-last-char ()
1770 "Delete the last input character from the current Quail key sequence."
1772 (if (= (length quail-current-key) 1)
1773 (quail-abort-translation)
1774 (setq quail-current-key (substring quail-current-key 0 -1))
1775 (quail-delete-region)
1776 (quail-update-translation (quail-translate-key))))
1778 ;; For conversion mode.
1780 (defsubst quail-point-in-conversion-region ()
1781 "Return non-nil value if the point is in conversion region of Quail mode."
1783 (and (setq start (overlay-start quail-conv-overlay))
1784 (>= (setq pos (point)) start)
1785 (<= pos (overlay-end quail-conv-overlay)))))
1787 (defun quail-conversion-backward-char ()
1789 (if (<= (point) (overlay-start quail-conv-overlay))
1790 (quail-error "Beginning of conversion region"))
1791 (setq quail-translating nil)
1794 (defun quail-conversion-forward-char ()
1796 (if (>= (point) (overlay-end quail-conv-overlay))
1797 (quail-error "End of conversion region"))
1798 (setq quail-translating nil)
1801 (defun quail-conversion-beginning-of-region ()
1803 (setq quail-translating nil)
1804 (goto-char (overlay-start quail-conv-overlay)))
1806 (defun quail-conversion-end-of-region ()
1808 (setq quail-translating nil)
1809 (goto-char (overlay-end quail-conv-overlay)))
1811 (defun quail-conversion-delete-char ()
1813 (setq quail-translating nil)
1814 (if (>= (point) (overlay-end quail-conv-overlay))
1815 (quail-error "End of conversion region"))
1817 (let ((start (overlay-start quail-conv-overlay))
1818 (end (overlay-end quail-conv-overlay)))
1819 (setq quail-conversion-str (buffer-substring start end))
1821 (setq quail-converting nil))))
1823 (defun quail-conversion-delete-tail ()
1825 (if (>= (point) (overlay-end quail-conv-overlay))
1826 (quail-error "End of conversion region"))
1827 (delete-region (point) (overlay-end quail-conv-overlay))
1828 (let ((start (overlay-start quail-conv-overlay))
1829 (end (overlay-end quail-conv-overlay)))
1830 (setq quail-conversion-str (buffer-substring start end))
1832 (setq quail-converting nil))))
1834 (defun quail-conversion-backward-delete-char ()
1836 (if (> (length quail-current-key) 0)
1837 (quail-delete-last-char)
1838 (if (<= (point) (overlay-start quail-conv-overlay))
1839 (quail-error "Beginning of conversion region"))
1841 (let ((start (overlay-start quail-conv-overlay))
1842 (end (overlay-end quail-conv-overlay)))
1843 (setq quail-conversion-str (buffer-substring start end))
1845 (setq quail-converting nil)))))
1847 (defun quail-do-conversion (func &rest args)
1848 "Call FUNC to convert text in the current conversion region of Quail.
1849 Remaining args are for FUNC."
1850 (delete-overlay quail-overlay)
1853 (defun quail-no-conversion ()
1854 "Do no conversion of the current conversion region of Quail."
1856 (setq quail-converting nil))
1858 ;; Guidance, Completion, and Help buffer handlers.
1860 (defun quail-make-guidance-frame (buf)
1861 "Make a new one-line frame for Quail guidance buffer."
1862 (let* ((fparam (frame-parameters))
1863 (top (cdr (assq 'top fparam)))
1864 (border (cdr (assq 'border-width fparam)))
1865 (internal-border (cdr (assq 'internal-border-width fparam)))
1867 (frame-char-height) (* internal-border 2) (* border 2))))
1869 (setq newtop (+ top (frame-pixel-height))))
1870 (let* ((frame (make-frame (append '((user-position . t) (height . 1)
1871 (minibuffer) (menu-bar-lines . 0))
1872 (cons (cons 'top newtop) fparam))))
1873 (win (frame-first-window frame)))
1874 (set-window-buffer win buf)
1875 ;;(set-window-dedicated-p win t)
1878 (defun quail-setup-completion-buf ()
1879 "Setup Quail completion buffer."
1880 (unless (buffer-live-p quail-completion-buf)
1881 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1882 (setq quail-completion-buf (get-buffer-create "*Quail Completions*")))
1883 (with-current-buffer quail-completion-buf
1884 (setq quail-overlay (make-overlay 1 1))
1885 (overlay-put quail-overlay 'face 'highlight))))
1887 (defun quail-require-guidance-buf ()
1888 "Return t iff the current Quail package requires showing guidance buffer."
1889 (and input-method-verbose-flag
1890 (if (eq input-method-verbose-flag 'default)
1891 (not (and (eq (selected-window) (minibuffer-window))
1893 (if (eq input-method-verbose-flag 'complex-only)
1894 (not (quail-simple))
1897 (defun quail-show-guidance-buf ()
1898 "Display a guidance buffer for Quail input method in some window.
1899 Create the buffer if it does not exist yet.
1900 The buffer is normally displayed at the echo area,
1901 but if the current buffer is a minibuffer, it is shown in
1902 the bottom-most ordinary window of the same frame,
1903 or in a newly created frame (if the selected frame has no other windows)."
1904 (when (quail-require-guidance-buf)
1905 ;; At first, setup a guidance buffer.
1906 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1907 (or (buffer-live-p quail-guidance-buf)
1908 (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*"))))
1909 (let ((name (quail-name))
1910 (title (quail-title)))
1911 (with-current-buffer quail-guidance-buf
1912 ;; To show the title of Quail package.
1913 (setq current-input-method name
1914 current-input-method-title title)
1916 (or (overlayp quail-overlay)
1918 (setq quail-overlay (make-overlay 1 1))
1919 (overlay-put quail-overlay 'face 'highlight)))
1920 (delete-overlay quail-overlay)
1921 (set-buffer-modified-p nil)))
1922 (bury-buffer quail-guidance-buf)
1924 ;; Assign the buffer " *Minibuf-N*" to all windows which are now
1925 ;; displaying quail-guidance-buf.
1926 (let ((win-list (get-buffer-window-list quail-guidance-buf t t)))
1928 (set-window-buffer (car win-list)
1929 (format " *Minibuf-%d*" (minibuffer-depth)))
1930 (setq win-list (cdr win-list))))
1932 ;; Then, display it in an appropriate window.
1933 (let ((win (minibuffer-window)))
1934 (if (or (eq (selected-window) win)
1935 input-method-use-echo-area)
1936 ;; Since we are in minibuffer, we can't use it for guidance.
1937 (if (eq win (frame-root-window))
1938 ;; Create a frame. It is sure that we are using some
1940 (quail-make-guidance-frame quail-guidance-buf)
1941 ;; Find the bottom window and split it if necessary.
1942 (setq win (window-at
1943 0 (1- (- (frame-height) (window-height win)))))
1944 (let ((height (window-height win))
1945 (window-min-height 2))
1946 ;; If WIN is tall enough, split it vertically and use
1949 ;; Here, `split-window' returns a lower window
1950 ;; which is what we wanted.
1951 (setq win (split-window win (- height 2))))
1952 (set-window-buffer win quail-guidance-buf)
1953 (with-current-buffer quail-guidance-buf
1954 (fit-window-to-buffer win nil (window-height win)))))
1955 (set-window-buffer win quail-guidance-buf)
1956 (set-minibuffer-window win))
1957 (setq quail-guidance-win win)))
1959 ;; And, create a buffer for completion.
1960 (quail-setup-completion-buf)
1961 (bury-buffer quail-completion-buf))
1963 (defun quail-hide-guidance-buf ()
1964 "Hide the Quail guidance buffer."
1965 (if (buffer-live-p quail-guidance-buf)
1966 (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
1969 (setq win (car win-list) win-list (cdr win-list))
1970 (if (window-minibuffer-p win)
1971 ;; We are using echo area for the guidance buffer.
1972 ;; Vacate it to the deepest minibuffer.
1973 (set-window-buffer win
1974 (format " *Minibuf-%d*" (minibuffer-depth)))
1975 (if (eq win (frame-root-window (window-frame win)))
1977 ;; We are using a separate frame for guidance buffer.
1978 ;;(set-window-dedicated-p win nil)
1979 (delete-frame (window-frame win)))
1980 ;;(set-window-dedicated-p win nil)
1981 (delete-window win))))
1982 (setq quail-guidance-win nil))))
1984 (defun quail-update-guidance ()
1985 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1986 ;; Update guidance buffer.
1987 (if (quail-require-guidance-buf)
1988 (let ((guidance (quail-guidance)))
1989 (unless (and (eq (selected-frame) (window-frame (minibuffer-window)))
1990 (eq (selected-frame) (window-frame quail-guidance-win)))
1991 ;; The guidance window is not shown in this frame, show it.
1992 (quail-show-guidance-buf))
1993 (cond ((or (eq guidance t)
1995 ;; Show the current possible translations.
1996 (quail-show-translations))
1998 ;; Show the current input keys.
1999 (let ((key quail-current-key))
2000 (if (quail-kbd-translate)
2001 (setq key (quail-keyseq-translate key)))
2002 (with-current-buffer quail-guidance-buf
2005 ;; Make sure the height of the guidance window is OK --
2006 ;; sometimes, if the minibuffer window expands due to user
2007 ;; input (for instance if the newly inserted character is in a
2008 ;; different font), it will cause the guidance window to be
2009 ;; only partially visible. We force a redisplay first because
2010 ;; this automatic expansion doesn't happen until then, and we
2011 ;; want to see the window sizes after the expansion.
2013 (fit-window-to-buffer quail-guidance-win nil
2014 (window-height quail-guidance-win))))
2016 ;; Update completion buffer if displayed now. We highlight the
2017 ;; selected candidate string in *Completion* buffer if any.
2018 (let ((win (get-buffer-window quail-completion-buf))
2022 (setq str (if (stringp quail-current-str)
2024 (if (numberp quail-current-str)
2025 (char-to-string quail-current-str)))
2026 key quail-current-key)
2027 (set-buffer quail-completion-buf)
2028 (goto-char (point-min))
2029 (if (null (search-forward (concat " " key ":") nil t))
2030 (delete-overlay quail-overlay)
2032 (if (and str (search-forward (concat "." str) nil t))
2033 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
2034 (move-overlay quail-overlay (match-beginning 0) (point)))
2035 ;; Now POS points end of KEY and (point) points end of STR.
2036 (if (pos-visible-in-window-p (point) win)
2037 ;; STR is already visible.
2039 ;; We want to make both KEY and STR visible, but if the
2040 ;; window is too short, make at least STR visible.
2041 (setq pos (progn (point) (goto-char pos)))
2043 (set-window-start win (point))
2044 (if (not (pos-visible-in-window-p pos win))
2045 (set-window-start win pos))
2048 (defun quail-show-translations ()
2049 "Show the current possible translations."
2050 (let* ((key quail-current-key)
2051 (map (quail-lookup-key quail-current-key))
2052 (current-translations quail-current-translations))
2053 (if quail-current-translations
2054 (quail-update-current-translations))
2055 (with-current-buffer quail-guidance-buf
2058 ;; Show the current key.
2059 (let ((guidance (quail-guidance)))
2060 (if (listp guidance)
2061 ;; We must show the specified PROMPTKEY instead of the
2062 ;; actual typed keys.
2067 (setq prompt-key (cdr (assoc (aref key i) guidance)))
2068 (insert (or prompt-key (aref key i)))
2072 ;; Show followable keys.
2073 (if (and (> (length key) 0) (cdr map))
2074 (let ((keys (mapcar (function (lambda (x) (car x)))
2076 (setq keys (sort keys '<))
2080 (setq keys (cdr keys)))
2083 ;; Show list of translations.
2084 (if (and current-translations
2085 (not (quail-deterministic)))
2086 (let* ((indices (car current-translations))
2088 (start (nth 1 indices))
2089 (end (nth 2 indices))
2091 (indent-to (- quail-guidance-translations-starting-column 7))
2092 (insert (format "(%02d/"(nth 3 indices))
2094 (format "%02d)" (nth 4 indices))
2097 (insert (format " %d." (if (= (- idx start) 9) 0
2098 (1+ (- idx start)))))
2099 (let ((pos (point)))
2100 (insert (aref (cdr current-translations) idx))
2102 (move-overlay quail-overlay pos (point))))
2103 (setq idx (1+ idx)))))
2106 (defvar quail-completion-max-depth 5
2107 "The maximum depth of Quail completion list.")
2109 (defun quail-completion ()
2110 "List all completions for the current key.
2111 All possible translations of the current key and whole possible longer keys
2112 are shown (at most to the depth specified `quail-completion-max-depth')."
2114 (quail-setup-completion-buf)
2115 (let ((win (get-buffer-window quail-completion-buf 'visible))
2116 (key quail-current-key)
2117 (map (quail-lookup-key quail-current-key))
2118 (require-update nil))
2119 (with-current-buffer quail-completion-buf
2121 (equal key quail-current-key)
2122 (eq last-command 'quail-completion))
2123 ;; The window for Quail completion buffer has already been
2124 ;; shown. We just scroll it appropriately.
2125 (if (pos-visible-in-window-p (point-max) win)
2126 (set-window-start win (point-min))
2127 (let ((other-window-scroll-buffer quail-completion-buf))
2128 (scroll-other-window)))
2129 (setq quail-current-key key)
2131 (insert "Possible completion and corresponding translations are:\n")
2132 (quail-completion-1 key map 1)
2133 (goto-char (point-min))
2134 (display-buffer (current-buffer))
2135 (setq require-update t)))
2137 (quail-update-guidance)))
2138 (setq this-command 'quail-completion))
2140 (defun quail-completion-1 (key map indent)
2141 "List all completions of KEY in MAP with indentation INDENT."
2142 (let ((len (length key)))
2145 (if (and (symbolp map) (fboundp map))
2146 (setq map (funcall map key len)))
2148 (quail-completion-list-translations map key (+ indent len 1))
2150 (setq indent (+ indent 2))
2151 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
2153 (newkey (make-string (1+ len) 0))
2156 (setq l (funcall l)))
2157 ;; Set KEY in the first LEN characters of NEWKEY.
2159 (aset newkey i (aref key i))
2161 (setq l (reverse l))
2162 (while l ; L = ((CHAR . DEFN) ....) ;
2163 (aset newkey len (car (car l)))
2164 (quail-completion-1 newkey (cdr (car l)) indent)
2165 (setq l (cdr l)))))))
2167 (defun quail-completion-list-translations (map key indent)
2168 "List all possible translations of KEY in Quail MAP with indentation INDENT."
2169 (let (beg (translations
2170 (quail-get-translation (car map) key (length key))))
2171 (if (integerp translations)
2174 ;; Endow the character `translations' with `mouse-face' text
2175 ;; property to enable `mouse-2' completion.
2177 (insert translations)
2178 (put-text-property beg (point) 'mouse-face 'highlight)
2180 ;; We need only vector part.
2181 (setq translations (cdr translations))
2182 ;; Insert every 10 elements with indices in a line.
2183 (let ((len (length translations))
2187 (when (zerop (% i 10))
2191 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
2192 ;; We show the last digit of FROM while converting
2193 ;; 0,1,..,9 to 1,2,..,0.
2194 (insert (format " %d." (% (1+ i) 10)))
2196 (insert (aref translations i))
2197 ;; Passing the mouse over a character will highlight.
2198 (put-text-property beg (point) 'mouse-face 'highlight)
2202 (defun quail-mouse-choose-completion (event)
2203 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
2205 ;; This function is an exact copy of the mouse.el function
2206 ;; `mouse-choose-completion' except that we:
2207 ;; 1) add two lines from `choose-completion' in simple.el to give
2208 ;; the `mouse-2' click a little more leeway.
2209 ;; 2) don't bury *Quail Completions* buffer, so comment a section, and
2210 ;; 3) delete/terminate the current quail selection here.
2211 ;; Give temporary modes such as isearch a chance to turn off.
2212 (run-hooks 'mouse-leave-buffer-hook)
2213 (let ((buffer (window-buffer))
2216 (with-current-buffer (window-buffer (posn-window (event-start event)))
2217 (if completion-reference-buffer
2218 (setq buffer completion-reference-buffer))
2219 (setq base-size completion-base-size)
2221 (goto-char (posn-point (event-start event)))
2223 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2224 (setq end (point) beg (1+ (point))))
2225 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
2226 (setq end (1- (point)) beg (point)))
2228 (quail-error "No completion here"))
2229 (setq beg (previous-single-property-change beg 'mouse-face))
2230 (setq end (or (next-single-property-change end 'mouse-face)
2232 (setq choice (buffer-substring beg end)))))
2233 ; (let ((owindow (selected-window)))
2234 ; (select-window (posn-window (event-start event)))
2235 ; (if (and (one-window-p t 'selected-frame)
2236 ; (window-dedicated-p (selected-window)))
2237 ; ;; This is a special buffer's frame
2238 ; (iconify-frame (selected-frame))
2239 ; (or (window-dedicated-p (selected-window))
2241 ; (select-window owindow))
2242 (quail-delete-region)
2243 (quail-choose-completion-string choice buffer base-size)
2244 (quail-terminate-translation)))
2246 ;; BASE-SIZE here is for compatibility with an (unused) arg of a
2247 ;; previous implementation.
2248 (defun quail-choose-completion-string (choice &optional buffer base-size)
2249 (setq quail-current-str choice)
2250 (choose-completion-string choice buffer))
2252 (defun quail-build-decode-map (map-list key decode-map num
2253 &optional maxnum ignores)
2254 "Build a decoding map.
2255 Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
2256 vs the corresponding translations defined in the Quail map
2257 specified by the first element MAP-LIST. Each pair has the form
2258 \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
2259 \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
2260 is a key sequence to reach MAP.
2261 Optional 5th arg MAXNUM limits the number of accumulated pairs.
2262 Optional 6th arg IGNORES is a list of translations to ignore."
2263 (let* ((map (car map-list))
2264 (translation (quail-get-translation (car map) key (length key)))
2266 (cond ((integerp translation)
2267 ;; Accept only non-ASCII chars not listed in IGNORES.
2268 (when (and (> translation 255) (not (memq translation ignores)))
2270 (cons (cons key translation) (cdr decode-map)))
2271 (setq num (1+ num))))
2272 ((consp translation)
2273 (setq translation (cdr translation))
2274 (let ((multibyte nil))
2275 (mapc (function (lambda (x)
2276 ;; Accept only non-ASCII chars not
2277 ;; listed in IGNORES.
2278 (if (and (if (integerp x) (> x 255)
2279 (> (string-bytes x) (length x)))
2280 (not (member x ignores)))
2281 (setq multibyte t))))
2285 (cons (cons key translation) (cdr decode-map)))
2286 (setq num (+ num (length translation)))))))
2287 (if (and maxnum (> num maxnum))
2289 (setq map (cdr map))
2290 ;; Recursively check the deeper map.
2291 (while (and map (>= num 0))
2292 (setq elt (car map) map (cdr map))
2293 (when (and (integerp (car elt)) (consp (cdr elt))
2294 (not (memq (cdr elt) map-list)))
2295 (setq num (quail-build-decode-map (cons (cdr elt) map-list)
2296 (format "%s%c" key (car elt))
2297 decode-map num maxnum ignores))))
2300 (defun quail-insert-decode-map (decode-map)
2301 "Insert pairs of key sequences vs the corresponding translations.
2302 These are stored in DECODE-MAP using the concise format. DECODE-MAP
2303 should be made by `quail-build-decode-map' (which see)."
2305 (sort (cdr decode-map)
2306 (function (lambda (x y)
2307 (setq x (car x) y (car y))
2308 (or (> (length x) (length y))
2309 (and (= (length x) (length y))
2310 (not (string< x y))))))))
2311 (let ((frame-width (frame-width (window-frame (get-buffer-window
2312 (current-buffer) 'visible))))
2313 (single-key-width 3)
2314 (single-trans-width 4)
2315 (multiple-key-width 3)
2318 elt trans width pos cols rows col row str col-width)
2319 ;; Divide the elements of decoding map into single ones (i.e. the
2320 ;; one that has single translation) and multibyte ones (i.e. the
2321 ;; one that has multiple translations).
2323 (setq elt (car decode-map) decode-map (cdr decode-map)
2325 (if (and (vectorp trans) (= (length trans) 1))
2326 (setq trans (aref trans 0)))
2328 (setq multiple-list (cons elt multiple-list))
2329 (setq single-list (cons (cons (car elt) trans) single-list)
2330 width (if (stringp trans) (string-width trans)
2331 (char-width trans)))
2332 (if (> width single-trans-width)
2333 (setq single-trans-width width)))
2334 (setq width (length (car elt)))
2335 (if (> width single-key-width)
2336 (setq single-key-width width))
2337 (if (> width multiple-key-width)
2338 (setq multiple-key-width width)))
2340 (setq col-width (+ single-key-width 1 single-trans-width 1)
2341 cols (/ frame-width col-width)
2342 rows (/ (length single-list) cols))
2343 (if (> (% (length single-list) cols) 0)
2344 (setq rows (1+ rows)))
2346 (indent-to (1+ single-key-width))
2348 (indent-to (1+ col-width))
2349 (insert "[type a key sequence to insert the corresponding character]\n")
2351 (insert-char ?\n (+ rows 2))
2353 (setq col (- col-width) row 0)
2355 (setq elt (car single-list) single-list (cdr single-list))
2356 (when (= (% row rows) 0)
2358 (setq col (+ col col-width))
2359 (move-to-column col t)
2360 (insert-char ?- single-key-width)
2362 (insert-char ?- single-trans-width)
2364 (move-to-column col t)
2366 (indent-to (+ col single-key-width 1))
2369 (setq row (1+ row)))
2370 (goto-char (point-max)))
2374 (indent-to (1+ multiple-key-width))
2375 (insert "character(s) [type a key (sequence) and select one from the list]\n")
2376 (insert-char ?- multiple-key-width)
2377 (insert " ------------\n")
2378 (while multiple-list
2379 (setq elt (car multiple-list) multiple-list (cdr multiple-list))
2381 (indent-to multiple-key-width)
2382 (if (vectorp (cdr elt))
2385 (let ((width (if (integerp x) (char-width x)
2387 (when (> (+ (current-column) 1 width) frame-width)
2389 (indent-to multiple-key-width))
2392 (insert " " (cdr elt)))
2396 (define-button-type 'quail-keyboard-layout-button
2397 :supertype 'help-xref
2398 'help-function '(lambda (layout)
2399 (help-setup-xref `(quail-keyboard-layout-button ,layout) nil)
2400 (quail-show-keyboard-layout layout))
2401 'help-echo (purecopy "mouse-2, RET: show keyboard layout"))
2403 (define-button-type 'quail-keyboard-customize-button
2404 :supertype 'help-customize-variable
2405 'help-echo (purecopy "mouse-2, RET: customize keyboard layout"))
2407 (defun quail-help (&optional package)
2408 "Show brief description of the current Quail package.
2409 Optional arg PACKAGE specifies the name of alternative Quail
2410 package to describe."
2412 (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
2413 (default-enable-multibyte-characters enable-multibyte-characters)
2416 (assoc package quail-package-alist)
2417 quail-current-package)))
2418 ;; At first, make sure that the help buffer has window.
2419 (let ((temp-buffer-show-hook nil))
2420 (with-output-to-temp-buffer (help-buffer)
2421 (with-current-buffer standard-output
2422 (setq quail-current-package package-def))))
2423 ;; Then, insert text in the help buffer while paying attention to
2424 ;; the width of the frame in which the buffer displayed.
2425 (with-current-buffer (help-buffer)
2426 (setq buffer-read-only nil)
2427 (insert "Input method: " (quail-name)
2428 " (mode line indicator:"
2432 (narrow-to-region (point) (point))
2433 (insert (quail-docstring))
2434 (goto-char (point-min))
2435 (with-syntax-table emacs-lisp-mode-syntax-table
2436 (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
2437 (let ((sym (intern-soft
2438 (buffer-substring (+ (match-beginning 0) 2)
2440 (if (and (boundp sym)
2441 (stringp (symbol-value sym)))
2442 (replace-match (symbol-value sym) t t)))))
2443 (goto-char (point-max)))
2448 (let ((done-list nil))
2449 ;; Show keyboard layout if the current package requests it..
2450 (when (quail-show-layout)
2454 This input method works by translating individual input characters.
2455 Assuming that your actual keyboard has the `")
2456 (help-insert-xref-button
2457 quail-keyboard-layout-type
2458 'quail-keyboard-layout-button
2459 quail-keyboard-layout-type)
2461 translation results in the following \"virtual\" keyboard layout:
2464 (quail-insert-kbd-layout quail-keyboard-layout))
2465 (insert "If your keyboard has a different layout, rearranged from
2467 (help-insert-xref-button
2469 'quail-keyboard-layout-button "standard")
2470 (insert "', the \"virtual\" keyboard you get with this input method
2471 will be rearranged in the same way.
2473 You can set the variable `quail-keyboard-layout-type' to specify
2474 the physical layout of your keyboard; the tables shown in
2475 documentation of input methods including this one are based on the
2476 physical keyboard layout as specified with that variable.
2478 (help-insert-xref-button
2479 "[customize keyboard layout]"
2480 'quail-keyboard-customize-button 'quail-keyboard-layout-type)
2483 ;; Show key sequences.
2484 (let ((decode-map (list 'decode-map))
2486 (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
2493 (if (quail-show-layout)
2494 (insert "You can also input more characters")
2495 (insert "You can input characters"))
2496 (insert " by the following key sequences:\n")
2497 (quail-insert-decode-map decode-map))))
2499 (quail-help-insert-keymap-description
2500 (quail-translation-keymap)
2502 KEY BINDINGS FOR TRANSLATION
2503 ----------------------------\n")
2505 (if (quail-conversion-keymap)
2506 (quail-help-insert-keymap-description
2507 (quail-conversion-keymap)
2509 KEY BINDINGS FOR CONVERSION
2510 ---------------------------\n"))
2511 (setq quail-current-package nil)
2512 ;; Resize the help window again, now that it has all its contents.
2513 (save-selected-window
2514 (select-window (get-buffer-window (current-buffer) t))
2515 (run-hooks 'temp-buffer-show-hook)))))
2517 (defun quail-help-insert-keymap-description (keymap &optional header)
2518 (let (pos1 pos2 eol)
2523 (insert (substitute-command-keys "\\{keymap}")))
2524 ;; Skip headers "key bindings", etc.
2527 (with-syntax-table emacs-lisp-mode-syntax-table
2528 (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
2529 (let ((sym (intern-soft (buffer-substring (match-beginning 0)
2531 (if (and sym (fboundp sym)
2532 (or (eq (get sym 'quail-help) 'hide)
2533 (and (quail-deterministic)
2534 (eq (get sym 'quail-help) 'non-deterministic))))
2535 (delete-region (line-beginning-position)
2536 (1+ (line-end-position)))))))
2539 (if (looking-at "[ \t]*$")
2540 (delete-region (point) (1+ (line-end-position)))
2544 (delete-region pos1 (point)))
2545 (goto-char (point-max))))
2547 (defun quail-translation-help ()
2548 "Show help message while translating in Quail input method."
2550 (if (not (eq this-command last-command))
2551 (let (state-msg keymap)
2552 (if (and quail-converting (= (length quail-current-key) 0))
2554 (format "Converting string %S by input method %S.\n"
2555 quail-conversion-str (quail-name))
2556 keymap (quail-conversion-keymap))
2558 (format "Translating key sequence %S by input method %S.\n"
2559 quail-current-key (quail-name))
2560 keymap (quail-translation-keymap)))
2561 (with-output-to-temp-buffer "*Help*"
2562 (with-current-buffer standard-output
2564 (quail-help-insert-keymap-description
2566 "-----------------------\n")
2567 ;; Isn't this redundant ? -stef
2570 (save-selected-window
2571 (select-window (get-buffer-window "*Help*"))
2572 (if (eq this-command last-command)
2573 (if (< (window-end) (point-max))
2575 (if (> (window-start) (point-min))
2576 (set-window-start (selected-window) (point-min)))))
2578 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2579 "Type \\[quail-translation-help] to scroll up the help"
2580 (if (> (window-start) (point-min))
2581 "Type \\[quail-translation-help] to see the head of help"))))
2584 (message "%s" (substitute-command-keys scroll-help))
2587 (quail-update-guidance)
2590 ;; Quail map generator from state transition table.
2592 (defun quail-map-from-table (table)
2593 "Make quail map from state transition table TABLE.
2595 TABLE is an alist, the form is:
2596 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2598 STATE-n are symbols to denote state. STATE-0 is the initial state.
2600 TRANSITION-n-m are transition rules from STATE-n, and have the form
2601 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2602 RULES is a symbol whose value is an alist of keys \(string) vs the
2603 correponding characters or strings. The format of the symbol value of
2604 RULES is the same as arguments to `quail-define-rules'.
2606 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2607 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2608 transition is allowed, i.e. STATE-x may be STATE-n.
2610 If TRANSITION-n-m has the form RULES, the transition terminates
2611 when keys in RULES are input.
2613 The generated map can be set for the current Quail package by the
2614 function `quail-install-map' (which see)."
2615 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2617 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2618 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2619 ;; Set key sequence mapping rules in cdr part of each element.
2621 (quail-map-from-table-1 state-alist (car table))
2622 (setq table (cdr table)))
2624 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2625 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2626 ;; them with MAPPING-RULES of STATE-x to make elements of
2627 ;; STATE-ALIST valid Quail maps.
2628 (setq tail state-alist)
2630 (setq elt (car tail) tail (cdr tail))
2631 (quail-map-from-table-2 state-alist elt))
2633 ;; Return the Quail map for the initial state.
2636 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2637 ;; mapping rules in the element of STATE-ALIST that corresponds to
2638 ;; STATE according to TRANSITION ...
2639 (defun quail-map-from-table-1 (state-alist state-info)
2640 (let* ((state (car state-info))
2641 (map (assq state state-alist))
2642 (transitions (cdr state-info))
2645 (setq elt (car transitions) transitions (cdr transitions))
2646 (let (rules dst-state key trans)
2647 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2648 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2650 (setq rules (symbol-value (car elt))
2651 ;; Set (STATE-x) as branches for all keys in RULES.
2652 ;; It is replaced with actual branches for STATE-x
2653 ;; later in `quail-map-from-table-2'.
2654 dst-state (list (cdr elt)))
2655 (setq rules (symbol-value elt)))
2657 (setq key (car (car rules)) trans (cdr (car rules))
2660 (if (= (length trans) 1)
2661 (setq trans (aref trans 0))
2662 (setq trans (string-to-vector trans))))
2663 (set-nested-alist key trans map nil dst-state))))))
2665 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2667 ;; (STATE (CHAR NESTED-ALIST) ...)
2668 ;; NESTED-ALIST is a nested alist; the form is:
2669 ;; (TRANS (CHAR NESTED-ALIST) ...)
2671 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2672 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2673 ;; (cdr (assq STATE-x STATE-ALIST))
2675 (defun quail-map-from-table-2 (state-alist element)
2676 (let ((prev element)
2677 (tail (cdr element))
2680 (setq elt (car tail) prev tail tail (cdr tail))
2681 (quail-map-from-table-2 state-alist (cdr elt)))
2682 (setq elt (car tail))
2684 (quail-map-from-table-2 state-alist (cdr elt))
2685 (setcdr prev (cdr (assq elt state-alist))))))
2687 ;; Concatenate translations for all heading substrings of KEY in the
2688 ;; current Quail map. Here, `heading substring' means (substring KEY
2689 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2690 (defun quail-lookup-map-and-concat (key)
2691 (let* ((len (length key))
2692 (translation-list nil)
2695 (setq map (quail-lookup-key key len)
2698 (let* ((def (quail-map-definition map))
2699 (trans (if (consp def) (aref (cdr def) (car (car def)))
2701 (if (integerp trans)
2702 (setq trans (char-to-string trans)))
2703 (setq translation-list (cons trans translation-list)))))
2704 (apply 'concat translation-list)))
2707 (defvar quail-directory-name "quail"
2708 "Name of Quail directory which contains Quail packages.
2709 This is a sub-directory of LEIM directory.")
2712 (defun quail-update-leim-list-file (dirname &rest dirnames)
2713 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2714 DIRNAME is a directory containing Emacs input methods;
2715 normally, it should specify the `leim' subdirectory
2716 of the Emacs source tree.
2718 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2719 and update the file \"leim-list.el\" in DIRNAME.
2721 When called from a program, the remaining arguments are additional
2722 directory names to search for Quail packages under `quail' subdirectory
2724 (interactive "FDirectory of LEIM: ")
2725 (setq dirname (expand-file-name dirname))
2726 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2727 quail-dirs list-buf pkg-list pkg-buf pos)
2728 (if (not (file-writable-p leim-list))
2729 (error "Can't write to file \"%s\"" leim-list))
2730 (message "Updating %s ..." leim-list)
2731 (setq list-buf (find-file-noselect leim-list))
2733 ;; At first, clean up the file.
2734 (with-current-buffer list-buf
2737 ;; Insert the correct header.
2738 (if (looking-at (regexp-quote leim-list-header))
2739 (goto-char (match-end 0))
2740 (insert leim-list-header))
2742 (if (not (re-search-forward leim-list-entry-regexp nil t))
2745 ;; Remove garbages after the header.
2746 (goto-char (match-beginning 0))
2748 (delete-region pos (point)))
2750 ;; Remove all entries for Quail.
2751 (while (re-search-forward leim-list-entry-regexp nil 'move)
2752 (goto-char (match-beginning 0))
2755 (let ((form (read list-buf)))
2756 (when (equal (nth 3 form) ''quail-use-package)
2757 (if (eolp) (forward-line 1))
2758 (delete-region pos (point))))
2760 ;; Delete the remaining contents because it seems that
2761 ;; this file is broken.
2762 (message "Garbage in %s deleted" leim-list)
2763 (delete-region pos (point-max)))))))
2765 ;; Search for `quail' subdirectory under each DIRNAMES.
2766 (setq dirnames (cons dirname dirnames))
2769 (setcar l (expand-file-name (car l)))
2770 (setq dirname (expand-file-name quail-directory-name (car l)))
2771 (if (file-readable-p dirname)
2772 (setq quail-dirs (cons dirname quail-dirs))
2773 (message "%s doesn't have `%s' subdirectory, just ignored"
2774 (car l) quail-directory-name)
2775 (setq quail-dirs (cons nil quail-dirs)))
2777 (setq quail-dirs (nreverse quail-dirs)))
2779 ;; Insert input method registering forms.
2781 (setq dirname (car quail-dirs))
2783 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2785 (message "Checking %s ..." (car pkg-list))
2787 (insert-file-contents (car pkg-list))
2788 (goto-char (point-min))
2789 ;; Don't get fooled by commented-out code.
2790 (while (re-search-forward "^[ \t]*(quail-define-package" nil t)
2791 (goto-char (match-beginning 0))
2793 (let ((form (read (current-buffer))))
2794 (with-current-buffer list-buf
2796 (format "(register-input-method
2800 (nth 1 form) ; PACKAGE-NAME
2801 (nth 2 form) ; LANGUAGE
2802 'quail-use-package ; ACTIVATE-FUNC
2803 (nth 3 form) ; PACKAGE-TITLE
2804 (progn ; PACKAGE-DESCRIPTION (one line)
2805 (string-match ".*" (nth 5 form))
2806 (match-string 0 (nth 5 form)))
2807 (file-relative-name ; PACKAGE-FILENAME
2808 (file-name-sans-extension (car pkg-list))
2811 ;; Ignore the remaining contents of this file.
2812 (goto-char (point-max))
2813 (message "Some part of \"%s\" is broken" (car pkg-list))))))
2814 (setq pkg-list (cdr pkg-list)))
2815 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2817 ;; At last, write out LEIM list file.
2818 (with-current-buffer list-buf
2819 (setq buffer-file-coding-system 'iso-2022-7bit)
2821 (kill-buffer list-buf)
2822 (message "Updating %s ... done" leim-list)))
2824 (defun quail-advice (args)
2825 "Advise users about the characters input by the current Quail package.
2826 The argument is a parameterized event of the form:
2827 (quail-advice STRING)
2828 where STRING is a string containing the input characters.
2829 If STRING has property `advice' and the value is a function,
2830 call it with one argument STRING."
2832 (let* ((string (nth 1 args))
2833 (func (get-text-property 0 'advice string)))
2834 (if (functionp func)
2835 (funcall func string))))
2837 (global-set-key [quail-advice] 'quail-advice)
2842 ;;; quail.el ends here