1 ;;; quail.el --- Provides simple input method for multilingual text
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
6 ;; Author: Kenichi HANDA <handa@etl.go.jp>
7 ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
8 ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
9 ;; Keywords: mule, multilingual, input method
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; In Quail minor mode, you can input multilingual text easily. By
31 ;; defining a translation table (named Quail map) which maps ASCII key
32 ;; string to multilingual character or string, you can input any text
33 ;; from ASCII keyboard.
35 ;; We use words "translation" and "conversion" differently. The
36 ;; former is done by Quail package itself, the latter is the further
37 ;; process of converting a translated text to some more desirable
38 ;; text. For instance, Quail package for Japanese (`quail-jp')
39 ;; translates Roman text (transliteration of Japanese in Latin
40 ;; alphabets) to Hiragana text, which is then converted to
41 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
42 ;; CONVERSION-KEYS argument of the Quail package.
48 ;; Buffer local variables
50 (defvar quail-current-package nil
51 "The current Quail package to input multilingual text in Quail minor mode.
52 See the documentation of `quail-package-alist' for the format.")
53 (make-variable-buffer-local 'quail-current-package)
54 (put 'quail-current-package 'permanent-local t)
56 ;; Quail uses the following two buffers to assist users.
57 ;; A buffer to show available key sequence or translation list.
58 (defvar quail-guidance-buf nil)
59 ;; A buffer to show completion list of the current key sequence.
60 (defvar quail-completion-buf nil)
62 (defvar quail-mode nil
63 "Non-nil if in Quail minor mode.")
64 (make-variable-buffer-local 'quail-mode)
65 (put 'quail-mode 'permanent-local t)
67 (defvar quail-overlay nil
68 "Overlay which covers the current translation region of Quail.")
69 (make-variable-buffer-local 'quail-overlay)
71 (defvar quail-conv-overlay nil
72 "Overlay which covers the text to be converted in Quail mode.")
73 (make-variable-buffer-local 'quail-conv-overlay)
75 (defvar quail-current-key nil
76 "Current key for translation in Quail mode.")
78 (defvar quail-current-str nil
79 "Currently selected translation of the current key.")
81 (defvar quail-current-translations nil
82 "Cons of indices and vector of possible translations of the current key.")
84 ;; A flag to control conversion region. Normally nil, but if set to
85 ;; t, it means we must start the new conversion region if new key to
86 ;; be translated is input.
87 (defvar quail-reset-conversion-region nil)
89 ;; Quail package handlers.
91 (defvar quail-package-alist nil
92 "List of Quail packages.
93 A Quail package is a list of these elements:
94 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
95 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
96 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
99 QUAIL-MAP is a data structure to map key strings to translations. For
100 the format, see the documentation of `quail-map-p'.
102 DECODE-MAP is an alist of translations and corresponding keys.
104 See the documentation of `quail-define-package' for the other elements.")
106 ;; Return various slots in the current quail-package.
108 (defsubst quail-name ()
109 "Return the name of the current Quail package."
110 (nth 0 quail-current-package))
111 (defsubst quail-title ()
112 "Return the title of the current Quail package."
113 (nth 1 quail-current-package))
114 (defsubst quail-map ()
115 "Return the translation map of the current Quail package."
116 (nth 2 quail-current-package))
117 (defsubst quail-guidance ()
118 "Return an object used for `guidance' feature of the current Quail package.
119 See also the documentation of `quail-define-package'."
120 (nth 3 quail-current-package))
121 (defsubst quail-docstring ()
122 "Return the documentation string of the current Quail package."
123 (nth 4 quail-current-package))
124 (defsubst quail-translation-keymap ()
125 "Return translation keymap in the current Quail package.
126 Translation keymap is a keymap used while translation region is active."
127 (nth 5 quail-current-package))
128 (defsubst quail-forget-last-selection ()
129 "Return `forget-last-selection' flag of the current Quail package.
130 See also the documentation of `quail-define-package'."
131 (nth 6 quail-current-package))
132 (defsubst quail-deterministic ()
133 "Return `deterministic' flag of the current Quail package.
134 See also the documentation of `quail-define-package'."
135 (nth 7 quail-current-package))
136 (defsubst quail-kbd-translate ()
137 "Return `kbd-translate' flag of the current Quail package.
138 See also the documentation of `quail-define-package'."
139 (nth 8 quail-current-package))
140 (defsubst quail-show-layout ()
141 "Return `show-layout' flag of the current Quail package.
142 See also the documentation of `quail-define-package'."
143 (nth 9 quail-current-package))
144 (defsubst quail-decode-map ()
145 "Return decode map of the current Quail package.
146 It is an alist of translations and corresponding keys."
147 (nth 10 quail-current-package))
148 (defsubst quail-maximum-shortest ()
149 "Return `maximum-shortest' flag of the current Quail package.
150 See also the documentation of `quail-define-package'."
151 (nth 11 quail-current-package))
152 (defsubst quail-overlay-plist ()
153 "Return property list of an overly used in the current Quail package."
154 (nth 12 quail-current-package))
155 (defsubst quail-update-translation-function ()
156 "Return a function for updating translation in the current Quail package."
157 (nth 13 quail-current-package))
158 (defsubst quail-conversion-keymap ()
159 "Return conversion keymap in the current Quail package.
160 Conversion keymap is a keymap used while conversion region is active
161 but translation region is not active."
162 (nth 14 quail-current-package))
164 (defsubst quail-package (name)
165 "Return Quail package named NAME."
166 (assoc name quail-package-alist))
168 (defun quail-add-package (package)
169 "Add Quail package PACKAGE to `quail-package-alist'."
170 (let ((pac (quail-package (car package))))
172 (setcdr pac (cdr package))
173 (setq quail-package-alist (cons package quail-package-alist)))))
175 (defun quail-select-package (name)
176 "Select Quail package named NAME as the current Quail package."
177 (let ((package (quail-package name)))
179 (error "No Quail package `%s'" name))
180 (setq quail-current-package package)
181 (setq-default quail-current-package package)
185 (defun quail-use-package (package-name &rest libraries)
186 "Start using Quail package PACKAGE-NAME.
187 The remaining arguments are libraries to be loaded before using the package."
189 (if (not (load (car libraries) t))
191 (with-output-to-temp-buffer "*Help*"
192 (princ "Quail package \"")
194 (princ "\" can't be activated\n because library \"")
195 (princ (car libraries))
196 (princ "\" is not in `load-path'.
198 The most common case is that you have not yet installed appropriate
199 libraries in LEIM (Libraries of Emacs Input Method) which is
200 distributed separately from Emacs.
202 Installation of LEIM for Quail is very simple, just copy Quail
203 packages (byte-compiled Emacs Lisp files) to somewhere in your
206 LEIM is available from the same ftp directory as Emacs."))
207 (error "Can't use the Quail package `%s'" package-name))
208 (setq libraries (cdr libraries))))
209 (quail-select-package package-name)
210 (setq current-input-method-title (quail-title))
213 (defun quail-inactivate ()
214 "Turn off Quail input method."
216 (throw 'quail-tag t))
218 (or (assq 'quail-mode minor-mode-alist)
219 (setq minor-mode-alist
220 (cons '(quail-mode " Quail") minor-mode-alist)))
222 (defvar quail-mode-map
223 (let ((map (make-keymap))
226 (define-key map (char-to-string i) 'quail-start-translation)
229 "Keymap for Quail mode.")
231 (or (assq 'quail-mode minor-mode-map-alist)
232 (setq minor-mode-map-alist
233 (cons (cons 'quail-mode quail-mode-map) minor-mode-map-alist)))
235 ;; Since some Emacs Lisp programs (e.g. viper.el) make
236 ;; minor-mode-map-alist buffer-local, we must be sure to register
237 ;; quail-mode-map in default-value of minor-mode-map-alist.
238 (if (local-variable-p 'minor-mode-map-alist)
239 (let ((map (default-value 'minor-mode-map-alist)))
240 (or (assq 'quail-mode map)
241 (set-default 'minor-mode-map-alist (cons 'quail-mode map)))))
243 (defvar quail-translation-keymap
244 (let ((map (make-keymap))
247 (define-key map (char-to-string i) 'quail-execute-non-quail-command)
250 (define-key map (char-to-string i) 'quail-self-insert-command)
252 (define-key map "\177" 'quail-delete-last-char)
253 (define-key map "\C-\\" 'quail-inactivate)
254 (define-key map "\C-f" 'quail-next-translation)
255 (define-key map "\C-b" 'quail-prev-translation)
256 (define-key map "\C-n" 'quail-next-translation-block)
257 (define-key map "\C-p" 'quail-prev-translation-block)
258 (define-key map "\C-i" 'quail-completion)
259 (define-key map "\C-@" 'quail-select-current)
260 (define-key map "\C-c" 'quail-abort-translation)
261 (define-key map "\C-h" 'quail-translation-help)
262 (define-key map "\e" '(keymap (t . quail-execute-non-quail-command)))
263 (define-key map [tab] 'quail-completion)
264 (define-key map [delete] 'quail-delete-last-char)
265 (define-key map [backspace] 'quail-delete-last-char)
266 ;; At last, define default key binding.
267 (append map '((t . quail-execute-non-quail-command))))
268 "Keymap used processing translation in Quail mode.
269 This map is activated while translation region is active.")
271 (defvar quail-conversion-keymap
272 (let ((map (make-keymap))
275 (define-key map (char-to-string i) 'quail-execute-non-quail-command)
278 (define-key map (char-to-string i)
279 'quail-start-translation-in-conversion-mode)
281 (define-key map "\C-b" 'quail-conversion-backward-char)
282 (define-key map "\C-f" 'quail-conversion-forward-char)
283 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
284 (define-key map "\C-e" 'quail-conversion-end-of-region)
285 (define-key map "\C-d" 'quail-conversion-delete-char)
286 (define-key map "\C-h" 'quail-conversion-help)
287 (define-key map "\C-\\" 'quail-inactivate)
288 (define-key map "\e" '(keymap (t . quail-execute-non-quail-command)))
289 (define-key map "\177" 'quail-conversion-backward-delete-char)
290 (define-key map [delete] 'quail-conversion-backward-delete-char)
291 (define-key map [backspace] 'quail-conversion-backward-delete-char)
292 ;; At last, define default key binding.
293 (append map '((t . quail-execute-non-quail-command))))
294 "Keymap used for processing conversion in Quail mode.
295 This map is activated while convesion region is active but translation
296 region is not active.")
298 (defun quail-define-package (name language title
299 &optional guidance docstring translation-keys
300 forget-last-selection deterministic
301 kbd-translate show-layout create-decode-map
302 maximum-shortest overlay-plist
303 update-translation-function
305 "Define NAME as a new Quail package for input LANGUAGE.
306 TITLE is a string to be displayed at mode-line to indicate this package.
307 Optional arguments are GUIDANCE, DOCSTRING, TRANLSATION-KEYS,
308 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
309 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
310 UPDATE-TRANSLATION-FUNCTION, and CONVERSION-KEYS.
312 GUIDANCE specifies how a guidance string is shown in echo area.
313 If it is t, list of all possible translations for the current key is shown
314 with the currently selected translation being highlighted.
315 If it is an alist, the element has the form (CHAR . STRING). Each character
316 in the current key is searched in the list and the corresponding string is
318 If it is nil, the current key is shown.
320 DOCSTRING is the documentation string of this package.
322 TRANSLATION-KEYS specifies additional key bindings used while translation
323 region is active. It is an alist of single key character vs. corresponding
324 command to be called.
326 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
327 for the future to translate the same key. If this flag is nil, a
328 translation selected for a key is remembered so that it can be the
329 first candidate when the same key is entered later.
331 DETERMINISTIC non-nil means the first candidate of translation is
332 selected automatically without allowing users to select another
333 translation for a key. In this case, unselected translations are of
334 no use for an interactive use of Quail but can be used by some other
335 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
338 KBD-TRANSLATE non-nil means input characters are translated from a
339 user's keyboard layout to the standard keyboard layout. See the
340 documentation of `quail-keyboard-layout' and
341 `quail-keyboard-layout-standard' for more detail.
343 SHOW-LAYOUT non-nil means the `quail-help' command should show
344 the user's keyboard layout visually with translated characters.
345 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
346 this package defines no translations for single character keys.
348 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
349 map is an alist of translations and corresponding original keys.
350 Although this map is not used by Quail itself, it can be used by some
351 other programs. For instance, Vietnamese supporting needs this map to
352 convert Vietnamese text to VIQR format which uses only ASCII
353 characters to represent Vietnamese characters.
355 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
356 length of the shortest sequence. When we don't have a translation of
357 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
358 the key at \"..AB\" and start translation of \"CD..\". Hangul
359 packages, for instance, use this facility. If this flag is nil, we
360 break the key just at \"..ABC\" and start translation of \"D..\".
362 OVERLAY-PLIST if non-nil is a property list put on an overlay which
363 covers Quail translation region.
365 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
366 the current translation region accoding to a new translation data. By
367 default, a tranlated text or a user's key sequence (if no transltion
370 CONVERSION-KEYS specifies additional key bindings used while
371 conversion region is active. It is an alist of single key character
372 vs. corresponding command to be called."
373 (let (translation-keymap conversion-keymap)
374 (if deterministic (setq forget-last-selection t))
377 (setq translation-keymap (copy-keymap quail-translation-keymap))
378 (while translation-keys
379 (define-key translation-keymap
380 (car (car translation-keys)) (cdr (car translation-keys)))
381 (setq translation-keys (cdr translation-keys))))
382 (setq translation-keymap quail-translation-keymap))
385 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
386 (while conversion-keys
387 (define-key conversion-keymap
388 (car (car conversion-keys)) (cdr (car conversion-keys)))
389 (setq conversion-keys (cdr conversion-keys)))))
391 (list name title (list nil) guidance (or docstring "")
393 forget-last-selection deterministic kbd-translate show-layout
394 (if create-decode-map (list 'decode-map) nil)
395 maximum-shortest overlay-plist update-translation-function
397 (register-input-method language (list name 'quail-use-package))
398 (quail-select-package name))
400 ;; Quail minor mode handlers.
402 ;; Setup overlays used in Quail mode.
403 (defun quail-setup-overlays ()
405 (if (overlayp quail-overlay)
406 (move-overlay quail-overlay pos pos)
407 (setq quail-overlay (make-overlay pos pos nil nil t))
408 (overlay-put quail-overlay 'face 'underline)
409 (let ((l (quail-overlay-plist)))
411 (overlay-put quail-overlay (car l) (car (cdr l)))
412 (setq l (cdr (cdr l))))))
413 (if (overlayp quail-conv-overlay)
414 (move-overlay quail-conv-overlay pos pos)
415 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
416 (overlay-put quail-conv-overlay 'face 'underline)
417 ;;(overlay-put quail-conv-overlay 'modification-hooks
418 ;;'(quail-conv-overlay-modification-hook))
421 ;; Delete overlays used in Quail mode.
422 (defun quail-delete-overlays ()
423 (if (overlayp quail-overlay)
424 (delete-overlay quail-overlay))
425 (if (overlayp quail-conv-overlay)
426 (delete-overlay quail-conv-overlay)))
428 ;; While translating and converting, we enter the recursive edit and
429 ;; exit it frequently, which results in frequent and annoying change
430 ;; of and annoying in mode line. To avoid it, we use a modified
432 (defvar quail-mode-line-format nil)
434 ;; Return a modified mode-line-format which doesn't show the recursive
435 ;; editing level. But, we only pay attention to the top level
436 ;; elements of the current mode-line-format.
437 (defun quail-generate-mode-line-format ()
438 (if (listp mode-line-format)
439 (let ((new (copy-sequence mode-line-format))
444 (if (and (stringp elt)
445 (or (setq idx (string-match "%\\[" elt))
446 (setq idx (string-match "%\\]" elt))))
447 (setcar l (concat (substring elt 0 idx)
448 (substring elt (+ idx 2)))))
453 (defun quail-mode (&optional arg)
454 "Toggle Quail minor mode.
455 With arg, turn Quail mode on if and only if arg is positive.
456 Try \\[describe-bindings] in Quail mode to see the available key binding.
457 The command \\[describe-input-method] describes the current Quail package."
460 (if (null arg) (null quail-mode)
461 (> (prefix-numeric-value arg) 0)))
462 (if (null quail-mode)
463 ;; Let's turn off Quail mode.
465 (quail-hide-guidance-buf)
466 (quail-delete-overlays)
467 (setq describe-current-input-method-function nil)
468 (setq current-input-method nil)
469 (run-hooks 'quail-mode-exit-hook)
470 (run-hooks 'input-method-inactivate-hook))
471 ;; Let's turn on Quail mode.
472 ;; At first, be sure that quail-mode is at the first element of
473 ;; minor-mode-map-alist.
474 (or (eq (car minor-mode-map-alist) 'quail-mode)
475 (let ((l minor-mode-map-alist))
477 (if (eq (car (cdr l)) 'quail-mode)
479 (setcdr l (cdr (cdr l)))
482 (setq minor-mode-map-alist (cons 'quail-mode minor-mode-map-alist))))
483 (if (null quail-current-package)
484 ;; Quail package is not yet selected. Select one now.
486 (if quail-package-alist
487 (setq name (car (car quail-package-alist)))
488 (setq quail-mode nil)
489 (error "No Quail package loaded"))
490 (quail-select-package name)))
491 (setq inactivate-current-input-method-function 'quail-mode)
492 (setq describe-current-input-method-function 'quail-help)
493 (setq quail-mode-line-format (quail-generate-mode-line-format))
494 (quail-delete-overlays)
495 (quail-show-guidance-buf)
496 ;; If we are in minibuffer, turn off Quail mode before exiting.
497 (if (eq (selected-window) (minibuffer-window))
498 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
499 (make-local-hook 'post-command-hook)
500 (run-hooks 'quail-mode-hook)
501 (run-hooks 'input-method-activate-hook))
502 (force-mode-line-update))
504 (defun quail-exit-from-minibuffer ()
505 (if quail-mode (quail-mode -1))
506 (if (<= (minibuffer-depth) 1)
507 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
509 (defvar quail-saved-overriding-local-map nil)
510 (defvar quail-saved-current-buffer nil)
512 ;; Toggle `quail-mode'. This function is added to `post-command-hook'
513 ;; in Quail mode, to turn Quail mode temporarily off, or back on
514 ;; after one non-Quail command.
515 (defun quail-toggle-mode-temporarily ()
517 ;; We are going to handle following events out of Quail mode.
519 quail-saved-overriding-local-map overriding-local-map
520 quail-saved-current-buffer (current-buffer)
521 overriding-local-map nil)
522 ;; We have just executed one non-Quail command. We don't need
523 ;; this hook any more.
524 (remove-hook 'post-command-hook 'quail-toggle-mode-temporarily t)
525 ;; If the command changed the current buffer, we should not go
526 ;; back to Quail mode.
527 (if (not (eq (current-buffer) quail-saved-current-buffer))
528 (throw 'quail-tag nil)
529 ;; Let's go back to Quail mode.
531 (setq overriding-local-map quail-saved-overriding-local-map)
532 ;; If whole text in conversion area was deleted, exit from the
534 (let ((start (overlay-start quail-conv-overlay)))
535 (if (and start (= start (overlay-end quail-conv-overlay)))
536 (throw 'quail-tag nil)))
539 (defun quail-execute-non-quail-command ()
540 "Execute one non-Quail command in Quail mode.
541 The current translation and conversion are terminated."
543 (setq unread-command-events (cons last-input-event unread-command-events))
544 (quail-delete-overlays)
545 (if (buffer-live-p quail-guidance-buf)
547 (set-buffer quail-guidance-buf)
549 (throw 'quail-tag nil))
551 ;; Keyboard layout translation handlers.
553 ;; Some Quail packages provide localized keyboard simulation which
554 ;; requires a particular keyboard layout. In this case, what we need
555 ;; is locations of keys the user entered, not character codes
556 ;; generated by those keys. However, for the moment, there's no
557 ;; common way to get such information. So, we ask a user to give
558 ;; information of his own keyboard layout, then translate it to the
559 ;; standard layout which we defined so that all Quail packages depend
562 (defconst quail-keyboard-layout-standard
565 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
566 qQwWeErRtTyYuUiIoOpP[{]} \
567 aAsSdDfFgGhHjJkKlL;:'\"\\| \
568 zZxXcCvVbBnNmM,<.>/? \
570 "Standard keyboard layout of printable characters Quail assumes.
571 See the documentation of `quail-keyboard-layout' for this format.
572 This layout is almost the same as that of VT100,
573 but the location of key \\ (backslash) is just right of key ' (single-quote),
574 not right of RETURN key.")
576 (defvar quail-keyboard-layout quail-keyboard-layout-standard
577 "A string which represents physical key layout of a particular keyboard.
578 We assume there are six rows and each row has 15 keys (columns),
579 the first row is above the `1' - `0' row,
580 the first column of the second row is left of key `1',
581 the first column of the third row is left of key `q',
582 the first column of the fourth row is left of key `a',
583 the first column of the fifth row is left of key `z',
584 the sixth row is below the `z' - `/' row.
585 Nth (N is even) and (N+1)th characters in the string are non-shifted
586 and shifted characters respectively at the same location.
587 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).")
589 (defconst quail-keyboard-layout-len 180)
591 ;; Here we provide several examples of famous keyboard layouts.
593 (defvar quail-keyboard-layout-alist
597 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
598 qQwWeErRtTyYuUiIoOpP[{]} \
599 aAsSdDfFgGhHjJkKlL;:'\" \
600 zZxXcCvVbBnNmM,<.>/? \
602 (cons "standard" quail-keyboard-layout-standard))
603 "Alist of keyboard names and corresponding layout strings.
604 See the documentation of `quail-keyboard-layout' for the format of
607 (defun quail-set-keyboard-layout (kbd-type)
608 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
610 Since some Quail packages depends on a physical layout of keys (not
611 characters generated by them), those are created by assuming the
612 standard layout defined in `quail-keyboard-layout-standard'. This
613 function tells Quail system the layout of your keyboard so that what
614 you type is correctly handled."
616 (let* ((completing-ignore-case t)
617 (type (completing-read "Keyboard type: "
618 quail-keyboard-layout-alist)))
620 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
622 ;; Here, we had better ask a user to define his own keyboard
623 ;; layout interactively.
624 (error "Unknown keyboard type `%s'" kbd-type))
625 (setq quail-keyboard-layout (cdr layout))))
627 (defun quail-keyboard-translate (ch)
628 "Translate CHAR according to `quail-keyboard-layout' and return the result."
629 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
632 (while (and (< i quail-keyboard-layout-len)
633 (/= ch (aref quail-keyboard-layout i)))
635 (if (= i quail-keyboard-layout-len)
636 (error "Character `%c' not found in your keyboard layout" ch))
637 (aref quail-keyboard-layout-standard i))))
641 (defsubst quail-map-p (object)
642 "Return t if OBJECT is a Quail map.
644 A Quail map holds information how a particular key should be translated.
645 Its format is (TRANSLATION . ALIST).
646 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
647 In the latter case, each element of VECTOR is a candidate for the translation,
648 and INDEX points the currently selected translation.
650 ALIST is normally a list of elements that look like (CHAR . DEFN),
651 where DEFN is another Quail map for a longer key (CHAR added to the
652 current key). It may also be a symbol of a function which returns an
653 alist of the above format.
655 Just after a Quail package is read, TRANSLATION may be a string or a
656 vector. Then each element of the string or vector is a candidate for
657 the translation. These objects are transformed to cons cells in the
658 format \(INDEX . VECTOR), as described above."
660 (let ((translation (car object)))
661 (or (integerp translation) (consp translation) (null translation)
662 (vectorp translation) (stringp translation)
663 (symbolp translation)))
664 (let ((alist (cdr object)))
665 (or (listp alist) (symbolp alist)))))
667 (defmacro quail-define-rules (&rest rules)
668 "Define translation rules of the current Quail package.
669 Each argument is a list of KEY and TRANSLATION.
670 KEY is a string meaning a sequence of keystrokes to be translated.
671 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
672 It it is a character, it is the sole translation of KEY.
673 If it is a string, each character is a candidate for the translation.
674 If it is a vector, each element (string or character) is a candidate
676 In these cases, a key specific Quail map is generated and assigned to KEY.
678 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
679 it is used to handle KEY."
684 (quail-defrule-internal (car (car l)) (car (cdr (car l))) map)
688 (defun quail-install-map (map)
689 "Install the Quail map MAP in the current Quail package.
690 The installed map can be referred by the function `quail-map'."
691 (if (null quail-current-package)
692 (error "No current Quail package"))
693 (if (null (quail-map-p map))
694 (error "Invalid Quail map `%s'" map))
695 (setcar (cdr (cdr quail-current-package)) map))
697 (defun quail-defrule (key translation &optional name)
698 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
699 KEY is a string meaning a sequence of keystrokes to be translated.
700 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
701 It it is a character, it is the sole translation of KEY.
702 If it is a string, each character is a candidate for the translation.
703 If it is a vector, each element (string or character) is a candidate
705 In these cases, a key specific Quail map is generated and assigned to KEY.
707 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
708 it is used to handle KEY.
709 Optional argument NAME, if specified, says which Quail package
710 to define this translation rule in. The default is to define it in the
711 current Quail package."
713 (let ((package (quail-package name)))
715 (error "No Quail package `%s'" name))
716 (setq quail-current-package package)))
717 (quail-defrule-internal key translation (quail-map)))
719 ;; Define KEY as TRANS in a Quail map MAP.
720 (defun quail-defrule-internal (key trans map)
721 (if (null (stringp key))
722 "Invalid Quail key `%s'" key)
723 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
725 (quail-map-p trans)))
726 (error "Invalid Quail translation `%s'" trans))
727 (if (null (quail-map-p map))
728 (error "Invalid Quail map `%s'" map))
729 (let ((len (length key))
733 (if (null (consp map))
734 ;; We come here, for example, when we try to define a rule
735 ;; for "ABC" but a rule for "AB" is already defined as a
737 (error "Quail key %s is too long" key))
738 (setq ch (aref key idx)
739 entry (assq ch (cdr map)))
742 (setq entry (cons ch (list nil)))
743 (setcdr map (cons entry (cdr map)))))
744 (setq map (cdr entry))
748 ;; We come here, for example, when we try to define a rule
749 ;; for "AB" as a symbol but a rule for "ABC" is already
751 (error "Quail key %s is too short" key)
752 (setcdr entry trans))
753 (if (quail-map-p trans)
754 (if (not (listp (cdr map)))
755 ;; We come here, for example, when we try to define a rule
756 ;; for "AB" as a symbol but a rule for "ABC" is already
758 (error "Quail key %s is too short" key)
759 (if (not (listp (cdr trans)))
761 ;; We come here, for example, when we try to
762 ;; define a rule for "AB" as a symbol but a rule
763 ;; for "ABC" is already defined.
764 (error "Quail key %s is too short" key)
765 (setcdr entry trans))
766 (setcdr entry (append trans (cdr map)))))
767 (setcar map trans)))))
769 (defun quail-get-translation (map key len)
770 "Return the translation specified in Quail map MAP for KEY of length LEN.
771 The translation is either a character or a cons of the form (INDEX . VECTOR),
772 where VECTOR is a vector of candidates (character or string) for
773 the translation, and INDEX points into VECTOR to specify the currently
774 selected translation."
775 (let ((def (car map)))
776 (if (and def (symbolp def))
777 ;; DEF is a symbol of a function which returns valid translation.
778 (setq def (funcall def key len)))
780 ((or (integerp def) (consp def))
788 ;; Each character in DEF is a candidate of translation. Reform
789 ;; it as (INDEX . VECTOR).
790 (setq def (string-to-vector def))
791 ;; But if the length is 1, we don't need vector but a single
792 ;; character as the translation.
793 (if (= (length def) 1)
798 ;; Each element (string or character) in DEF is a candidate of
799 ;; translation. Reform it as (INDEX . VECTOR).
803 (error "Invalid object in Quail map: %s" def)))))
805 (defun quail-lookup-key (key len)
806 "Lookup KEY of length LEN in the current Quail map and return the definition.
807 The returned value is a Quail map specific to KEY."
810 (kbd-translate (quail-kbd-translate))
812 (while (and map (< idx len))
813 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
816 (if (and (cdr map) (symbolp (cdr map)))
817 (setcdr map (funcall (cdr map) key idx)))
818 (setq slot (assq ch (cdr map)))
819 (if (and (cdr slot) (symbolp (cdr slot)))
820 (setcdr slot (funcall (cdr slot) key idx)))
821 (setq map (cdr slot)))
822 (if (and map (setq translation (quail-get-translation map key len)))
824 ;; We may have to reform car part of MAP.
825 (if (not (equal (car map) translation))
826 (setcar map translation))
827 (if (consp translation)
829 (setq quail-current-translations translation)
830 (if (quail-forget-last-selection)
831 (setcar quail-current-translations 0))))
832 ;; We may have to reform cdr part of MAP.
833 (if (and (cdr map) (symbolp (cdr map)))
835 (setcdr map (funcall (cdr map) key len))))
839 (defun quail-conv-overlay-modification-hook (overlay after &rest ignore)
841 (= (overlay-start overlay) (overlay-end overlay)))
842 ;; Whole text in conversion area was deleted. Let's exit from
843 ;; the recursive edit.
846 (defvar quail-suppress-conversion nil
847 "If non-nil, suppress converting facility of the current Quail package.")
849 ;; If set to non-nil, exit conversion mode before starting new translation.
850 (defvar quail-exit-conversion-mode nil)
852 (defun quail-start-translation ()
853 "Start translating the typed character in Quail mode."
855 (setq unread-command-events
856 (cons last-command-event unread-command-events))
857 ;; Check the possibility of translating the last key.
858 (if (assq last-command-event (cdr (quail-map)))
859 ;; Ok, we can start translation.
860 (let ((mode-line-format quail-mode-line-format))
861 (quail-setup-overlays)
862 (if (catch 'quail-tag
863 (if (and (not quail-suppress-conversion)
864 (quail-conversion-keymap))
865 ;; We must start translation in conversion mode.
866 (let ((overriding-local-map (quail-conversion-keymap)))
867 (setq quail-exit-conversion-mode nil)
869 (if (and auto-fill-function
870 (> (current-column) (current-fill-column)))
871 (run-hooks 'auto-fill-function)))
872 (let ((overriding-local-map (quail-translation-keymap)))
873 (setq quail-current-key "")
875 (if (prog1 (< (overlay-start quail-conv-overlay)
876 (overlay-end quail-conv-overlay))
877 (delete-overlay quail-conv-overlay))
878 (run-hooks 'input-method-after-insert-chunk-hook))
880 ;; Someone has thrown a tag with value t, which means
881 ;; we should turn Quail mode off.
883 ;; Since the typed character doesn't start any translation, handle
884 ;; it out of Quail mode. We come back to Quail mode later because
885 ;; function `quail-toggle-mode-temporarily' is in
886 ;; `post-command-hook'.
887 (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
889 (defsubst quail-point-in-conversion-region ()
890 "Return non-nil value if the point is in conversion region of Quail mode."
892 (and (setq start (overlay-start quail-conv-overlay))
893 (>= (setq pos (point)) start)
894 (<= pos (overlay-end quail-conv-overlay)))))
896 (defun quail-start-translation-in-conversion-mode ()
897 "Start translating the typed character in conversion mode of Quail mode."
899 (setq unread-command-events
900 (cons last-command-event unread-command-events))
901 (if (or quail-exit-conversion-mode
902 (not (quail-point-in-conversion-region)))
904 ;; We must start translation with new conversion region.
905 (setq quail-exit-conversion-mode nil)
907 ;; Check the possibility of translating the last key.
908 (if (assq last-command-event (cdr (quail-map)))
909 ;; Ok, we can start translation.
910 (let ((overriding-local-map (quail-translation-keymap)))
911 (setq quail-current-key "")
912 (move-overlay quail-overlay (point) (point))
914 ;; Since the typed character doesn't start any translation, handle
915 ;; it out of Quail mode. We come back to Quail mode later because
916 ;; function `quail-toggle-mode-temporarily' is in
917 ;; `post-command-hook'.
918 (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
920 (defun quail-terminate-translation ()
921 "Terminate the translation of the current key."
922 (let ((start (overlay-start quail-overlay)))
924 (< start (overlay-end quail-overlay)))
925 ;; Here we simulate self-insert-command.
926 (let (last-command-char)
928 ;; The first one might want to expand an abbrev.
929 (setq last-command-char (following-char))
931 (self-insert-command 1)
932 (if (< (point) (overlay-end quail-overlay))
934 (while (< (point) (overlay-end quail-overlay))
935 (setq last-command-char (following-char))
937 (self-insert-command 1))
938 ;; The last one might still want to auto-fill.
939 (goto-char (overlay-end quail-overlay))
940 (let ((last-command-char (preceding-char)))
942 (self-insert-command 1)))))))
943 (delete-overlay quail-overlay)
944 (if (buffer-live-p quail-guidance-buf)
946 (set-buffer quail-guidance-buf)
950 (defsubst quail-delete-region ()
951 "Delete the text in the current translation region of Quail."
952 (delete-region (overlay-start quail-overlay) (overlay-end quail-overlay)))
954 (defun quail-select-current ()
955 "Select the current text shown in Quail translation region."
957 (quail-terminate-translation))
959 ;; Update the current translation status according to CONTROL-FLAG.
960 ;; If CONTROL-FLAG is integer value, it is the number of keys in the
961 ;; head quail-current-key which can be translated. The remaining keys
962 ;; are put back to unread-command-events to be handled again.
963 ;; If CONTROL-FLAG is t, terminate the translation for the whole keys
964 ;; in quail-current-key.
965 ;; If CONTROL-FLAG is nil, proceed the translation with more keys.
967 (defun quail-update-translation (control-flag)
968 (quail-delete-region)
969 (let ((func (quail-update-translation-function)))
971 (funcall func control-flag)
972 (if (numberp control-flag)
973 (let ((len (length quail-current-key)))
974 (while (> len control-flag)
976 (setq unread-command-events
977 (cons (aref quail-current-key len)
978 unread-command-events)))
979 (insert (or quail-current-str
980 (substring quail-current-key 0 len))))
981 (insert (or quail-current-str quail-current-key)))))
982 (quail-update-guidance)
984 (quail-terminate-translation)))
986 (defun quail-self-insert-command ()
987 "Add the typed character to the key for translation."
989 (setq quail-current-key
990 (concat quail-current-key (char-to-string last-command-event)))
991 (quail-update-translation (quail-translate-key)))
993 (defun quail-translate-key ()
994 "Translate the current key sequence according to the current Quail map.
995 Return t if we can terminate the translation.
996 Return nil if the current key sequence may be followed by more keys.
997 Return number if we can't find any translation for the current key
998 sequence. The number is the count of valid keys in the current
999 sequence counting from the head."
1000 (let* ((len (length quail-current-key))
1001 (map (quail-lookup-key quail-current-key len))
1004 (let ((def (car map)))
1005 (setq quail-current-str
1006 (if (consp def) (aref (cdr def) (car def)) def))
1007 ;; Return t only if we can terminate the current translation.
1009 ;; No alternative translations.
1010 (or (null (consp def)) (= (length (cdr def)) 1))
1011 ;; No translation for the longer key.
1013 ;; No shorter breaking point.
1014 (or (null (quail-maximum-shortest))
1016 (null (quail-lookup-key quail-current-key (1- len)))
1017 (null (quail-lookup-key
1018 (substring quail-current-key -2 -1) 1)))))
1020 ;; There's no translation for the current key sequence. Before
1021 ;; giving up, we must check two possibilities.
1023 (quail-maximum-shortest)
1025 (setq def (car (quail-lookup-key quail-current-key (- len 2))))
1026 (quail-lookup-key (substring quail-current-key -2) 2))
1027 ;; Now the sequence is "...ABCD", which can be split into
1028 ;; "...AB" and "CD..." to get valid translation.
1029 ;; At first, get translation of "...AB".
1030 (setq quail-current-str
1031 (if (consp def) (aref (cdr def) (car def)) def))
1032 ;; Then, return the length of "...AB".
1035 ((and quail-current-translations
1036 (not (quail-deterministic))
1037 (setq ch (aref quail-current-key (1- len)))
1038 (>= ch ?0) (<= ch ?9))
1039 ;; A numeric key is entered to select a desirable translation.
1040 (setq quail-current-key (substring quail-current-key 0 -1))
1041 (quail-select-translation
1042 (+ (* (/ (car quail-current-translations) 10) 10)
1043 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1044 (if (= ch ?0) 9 (- ch ?1))))
1045 ;; And, we can terminate the current translation.
1049 ;; No way to handle the last character in this context.
1052 (defun quail-next-translation ()
1053 "Select next translation in the current batch of candidates."
1055 (if quail-current-translations
1057 (quail-select-translation (1+ (car quail-current-translations)))
1058 (quail-update-translation nil))
1061 (defun quail-prev-translation ()
1062 "Select previous translation in the current batch of candidates."
1064 (if quail-current-translations
1066 (quail-select-translation (1- (car quail-current-translations)))
1067 (quail-update-translation nil))
1070 (defun quail-next-translation-block ()
1071 "Select the next batch of 10 translation candidates."
1073 (if quail-current-translations
1074 (let ((limit (1- (length (cdr quail-current-translations))))
1075 (n (car quail-current-translations)))
1076 (if (< (/ n 10) (/ limit 10))
1078 (quail-select-translation (min (+ n 10) limit))
1079 (quail-update-translation nil))
1080 ;; We are already at the last block.
1084 (defun quail-prev-translation-block ()
1085 "Select the previous batch of 10 translation candidates."
1087 (if (and quail-current-translations
1088 (>= (car quail-current-translations) 10))
1090 (quail-select-translation (- (car quail-current-translations) 10))
1091 (quail-update-translation nil))
1094 (defun quail-select-translation (n)
1095 "Select Nth translation in the current batch of translation candidates."
1096 (if (or (< n 0) (>= n (length (cdr quail-current-translations))))
1098 (setcar quail-current-translations n)
1099 (setq quail-current-str (aref (cdr quail-current-translations) n))))
1101 (defun quail-abort-translation ()
1102 "Abort translation and delete the current Quail key sequence."
1104 (quail-delete-region)
1105 (quail-terminate-translation))
1107 (defun quail-delete-last-char ()
1108 "Delete the last input character from the current Quail key sequence."
1110 (if (= (length quail-current-key) 1)
1111 (quail-abort-translation)
1112 (setq quail-current-key (substring quail-current-key 0 -1))
1113 (quail-update-translation (quail-translate-key))))
1115 ;; For conversion mode.
1117 (defun quail-conversion-backward-char ()
1119 (if (<= (point) (overlay-start quail-conv-overlay))
1120 (error "Beginning of conversion region"))
1123 (defun quail-conversion-forward-char ()
1125 (if (>= (point) (overlay-end quail-conv-overlay))
1126 (error "End of conversion region"))
1129 (defun quail-conversion-beginning-of-region ()
1131 (goto-char (overlay-start quail-conv-overlay)))
1133 (defun quail-conversion-end-of-region ()
1135 (goto-char (overlay-end quail-conv-overlay)))
1137 (defun quail-conversion-delete-char ()
1139 (if (>= (point) (overlay-end quail-conv-overlay))
1140 (error "End of conversion region"))
1142 (if (= (overlay-start quail-conv-overlay)
1143 (overlay-end quail-conv-overlay))
1144 (throw 'quail-tag nil)))
1146 (defun quail-conversion-backward-delete-char ()
1148 (if (<= (point) (overlay-start quail-conv-overlay))
1149 (error "Beginning of conversion region"))
1151 (if (= (overlay-start quail-conv-overlay)
1152 (overlay-end quail-conv-overlay))
1153 (throw 'quail-tag nil)))
1155 (defun quail-do-conversion (func &rest args)
1156 "Call FUNC to convert text in the current conversion region of Quail.
1157 Remaining args are for FUNC."
1158 (delete-overlay quail-overlay)
1161 (defun quail-no-conversion ()
1162 "Do no conversion of the current conversion region of Quail."
1166 ;; Guidance, Completion, and Help buffer handlers.
1168 (defun quail-show-guidance-buf ()
1169 "Display a Quail guidance buffer in some window.
1170 Create the buffer if it does not exist yet.
1171 The window is normally shown in a minibuffer,
1172 but if the selected window is a minibuffer, it is shown in
1173 the bottommost ordinary window."
1175 (if (or (null input-method-tersely-flag)
1176 (not (eq (selected-window) (minibuffer-window))))
1178 ;; At first, setup a guidance buffer.
1179 (or (buffer-live-p quail-guidance-buf)
1180 (setq quail-guidance-buf
1181 (get-buffer-create " *Quail-guidance*")))
1183 (let ((title (quail-title)))
1184 (set-buffer quail-guidance-buf)
1185 ;; Show the title of Quail package in the left of mode-line.
1186 (setq current-input-method nil)
1187 (setq current-input-method-title title)
1188 (setq mode-line-format (cons '("[" current-input-method-title "]")
1189 default-mode-line-format))
1191 (or (overlayp quail-overlay)
1193 (setq quail-overlay (make-overlay 1 1))
1194 (overlay-put quail-overlay 'face 'highlight)))
1195 (delete-overlay quail-overlay)
1196 (set-buffer-modified-p nil)))
1197 (bury-buffer quail-guidance-buf)
1199 ;; Then, display it in an appropriate window.
1200 (if (not (get-buffer-window quail-guidance-buf))
1201 ;; Guidance buffer is not yet shown in any window.
1202 (let ((win (minibuffer-window)))
1203 (if (eq (selected-window) win)
1204 ;; Since we are in minibuffer, we can't use it for guidance.
1205 ;; Let's find the bottom window.
1207 (setq win (window-at 0 (- (frame-height) 2)))
1208 (setq height (window-height win))
1209 ;; If WIN is too tall, split it vertically and use
1212 (let ((window-min-height 2))
1213 ;; Here, `split-window' returns a lower window
1214 ;; which is what we wanted.
1215 (setq win (split-window win (- height 2)))))
1216 (set-window-buffer win quail-guidance-buf)
1217 (set-window-dedicated-p win t))
1218 (set-window-buffer win quail-guidance-buf))))))
1220 ;; And, create a buffer for completion.
1221 (or (buffer-live-p quail-completion-buf)
1223 (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
1225 (set-buffer quail-completion-buf)
1226 (setq quail-overlay (make-overlay 1 1))
1227 (overlay-put quail-overlay 'face 'highlight))))
1228 (bury-buffer quail-completion-buf))
1230 (defun quail-hide-guidance-buf ()
1231 "Hide the Quail guidance buffer."
1232 (let* ((win (minibuffer-window))
1233 (buf (window-buffer win)))
1234 (if (eq buf quail-guidance-buf)
1235 ;; Quail guidance buffer is at echo area. Vacate it to the
1236 ;; deepest minibuffer.
1237 (set-window-buffer win (format " *Minibuf-%d*" (minibuffer-depth)))
1238 ;; Delete the window for guidance buffer.
1239 (if (or (null input-method-tersely-flag)
1240 (not (eq (selected-window) (minibuffer-window))))
1242 (setq win (get-buffer-window quail-guidance-buf))
1243 (set-window-dedicated-p win nil)
1244 (delete-window win))))))
1246 (defun quail-update-guidance ()
1247 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1248 ;; Update guidance buffer.
1249 (if (or (null input-method-tersely-flag)
1250 (not (eq (selected-window) (minibuffer-window))))
1251 (let ((guidance (quail-guidance)))
1252 (cond ((eq guidance t)
1253 ;; Show the current possible translations.
1254 (quail-show-translations))
1256 ;; Show the current input keys.
1257 (let ((key quail-current-key))
1259 (set-buffer quail-guidance-buf)
1263 ;; Show alternative characters specified in this alist.
1264 (let* ((key quail-current-key)
1269 (set-buffer quail-guidance-buf)
1272 (setq ch (aref key i))
1273 (setq alternative (cdr (assoc ch guidance)))
1274 (insert (or alternative ch))
1275 (setq i (1+ i)))))))))
1277 ;; Update completion buffer if displayed now. We highlight the
1278 ;; selected candidate string in *Completion* buffer if any.
1279 (let ((win (get-buffer-window quail-completion-buf))
1283 (setq str (if (stringp quail-current-str)
1285 (if (numberp quail-current-str)
1286 (char-to-string quail-current-str)))
1287 key quail-current-key)
1288 (set-buffer quail-completion-buf)
1289 (goto-char (point-min))
1290 (if (null (search-forward (concat " " key ":") nil t))
1291 (delete-overlay quail-overlay)
1293 (if (and str (search-forward (concat "." str) nil t))
1294 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
1295 (move-overlay quail-overlay (match-beginning 0) (point)))
1296 ;; Now POS points end of KEY and (point) points end of STR.
1297 (if (pos-visible-in-window-p (point) win)
1298 ;; STR is already visible.
1300 ;; We want to make both KEY and STR visible, but if the
1301 ;; window is too short, make at least STR visible.
1302 (setq pos (progn (point) (goto-char pos)))
1304 (set-window-start win (point))
1305 (if (not (pos-visible-in-window-p pos win))
1306 (set-window-start win pos))
1309 (defun quail-show-translations ()
1310 "Show the current possible translations."
1311 (let ((key quail-current-key)
1312 (map (quail-lookup-key quail-current-key (length quail-current-key))))
1314 (set-buffer quail-guidance-buf)
1317 ;; Show the current key.
1320 ;; Show possible following keys.
1322 (let ((l (cdr map)))
1325 (insert (car (car l)))
1329 ;; Show list of translations.
1330 (if (consp (car map))
1331 (let* ((idx (car (car map)))
1332 (translations (cdr (car map)))
1333 (from (* (/ idx 10) 10))
1334 (to (min (+ from 10) (length translations))))
1336 (insert (format "(%d/%d)"
1338 (1+ (/ (length translations) 10))))
1340 ;; We show the last digit of FROM, but by changing
1341 ;; 0,1,..,9 to 1,2,..,0.
1342 (insert (format " %d."
1343 (if (= (% from 10) 9) 0 (1+ (% from 10)))))
1344 (let ((pos (point)))
1345 (insert (aref translations from))
1347 (move-overlay quail-overlay pos (point))))
1348 (setq from (1+ from)))))
1351 (defun quail-completion ()
1352 "List all completions for the current key.
1353 All possible translations of the current key and whole possible longer keys
1356 (let ((key quail-current-key)
1357 (map (quail-lookup-key quail-current-key (length quail-current-key))))
1359 (set-buffer quail-completion-buf)
1361 (insert "Possible completion and corresponding translations are:\n")
1362 (quail-completion-1 key map 1)
1363 (goto-char (point-min))
1364 (display-buffer (current-buffer)))
1365 (quail-update-guidance)))
1367 ;; List all completions of KEY in MAP with indentation INDENT.
1368 (defun quail-completion-1 (key map indent)
1369 (let ((len (length key)))
1372 (if (and (symbolp map) (fboundp map))
1373 (setq map (funcall map key len)))
1375 (quail-completion-list-translations map key (+ indent len 1))
1377 (setq indent (+ indent 2))
1380 (newkey (make-string (1+ len) 0))
1382 ;; Set KEY in the first LEN characters of NEWKEY.
1384 (aset newkey i (aref key i))
1386 (while l ; L = ((CHAR . DEFN) ....) ;
1387 (aset newkey len (car (car l)))
1388 (quail-completion-1 newkey (cdr (car l)) indent)
1389 (setq l (cdr l)))))))
1391 ;; List all possible translations of KEY in Quail map MAP with
1392 ;; indentation INDENT.
1393 (defun quail-completion-list-translations (map key indent)
1395 (quail-get-translation map key (length key))))
1396 (if (integerp translations)
1397 (insert "(1/1) 1." translations "\n")
1398 ;; We need only vector part.
1399 (setq translations (cdr translations))
1400 ;; Insert every 10 elements with indices in a line.
1401 (let ((len (length translations))
1414 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))))
1415 ;; We show the last digit of FROM while converting
1416 ;; 0,1,..,9 to 1,2,..,0.
1417 (insert (format " %d." (if (= (% i 10) 9) 0 (1+ (% i 10)))))
1418 (insert (aref translations i))
1422 (defun quail-help ()
1423 "Show brief description of the current Quail package."
1425 (let ((package quail-current-package)
1426 (buf (get-buffer-create "*Quail-help*")))
1430 (setq quail-current-package package)
1431 (insert "Quail input method (name:"
1433 ", mode line indicator:["
1435 "])\n---- Documentation ----\n"
1438 (if (quail-show-layout) (quail-show-kbd-layout))
1440 (quail-help-insert-keymap-description
1442 "---- Key bindings (before starting translation) ----
1445 (quail-help-insert-keymap-description
1446 (quail-translation-keymap)
1447 "--- Key bindings (while translating) ---
1450 (if (quail-conversion-keymap)
1451 (quail-help-insert-keymap-description
1452 (quail-conversion-keymap)
1453 "--- Key bindings (while converting) ---
1456 (goto-char (point-min))
1457 (set-buffer-modified-p nil)
1459 (display-buffer buf)))
1461 (defun quail-help-insert-keymap-description (keymap &optional header)
1466 (save-window-excursion
1467 (let ((overriding-local-map keymap))
1468 (describe-bindings))
1469 (set-buffer "*Help*")
1470 (goto-char (point-min))
1473 (search-forward "Global Bindings:" nil 'move)
1476 (insert-buffer-substring "*Help*" from to)))
1478 (defun quail-show-kbd-layout ()
1479 "Show keyboard layout with key tops of multilingual characters."
1480 (insert "--- Keyboard layout ---\n")
1482 (while (< i quail-keyboard-layout-len)
1486 (indent-to (/ i 30)))
1489 (setq ch (aref quail-keyboard-layout i))
1492 (let* ((map (cdr (assq ch (cdr (quail-map)))))
1493 (translation (and map (quail-get-translation
1494 map (char-to-string ch) 1))))
1495 (if (integerp translation)
1496 (insert translation)
1497 (if (consp translation)
1498 (insert (aref (cdr translation) (car translation)))
1503 (defun quail-translation-help ()
1504 "Show help message while translating in Quail mode."
1506 (let ((package quail-current-package)
1507 (current-key quail-current-key)
1508 (buf (get-buffer-create "*Quail-Help*")))
1512 (setq quail-current-package package)
1514 (format "You are translating the key sequence \"%s\" in Quail mode.\n"
1516 (quail-help-insert-keymap-description
1517 (quail-translation-keymap)
1518 "-----------------------
1521 (goto-char (point-min))
1522 (set-buffer-modified-p nil))
1523 (display-buffer buf)))
1525 (defun quail-conversion-help ()
1526 "Show help message while converting in Quail mode."
1528 (let ((package quail-current-package)
1529 (str (buffer-substring (overlay-start quail-conv-overlay)
1530 (overlay-end quail-conv-overlay)))
1531 (buf (get-buffer-create "*Quail-Help*")))
1535 (setq quail-current-package package)
1537 (format "You are converting the string \"%s\" in Quail mode.\n" str))
1538 (quail-help-insert-keymap-description
1539 (quail-conversion-keymap)
1540 "-----------------------
1543 (goto-char (point-min))
1544 (set-buffer-modified-p nil))
1545 (display-buffer buf)))
1550 ;;; quail.el ends here