1 ;;; quail.el --- Provides simple input method for multilingual text
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Author: Kenichi HANDA <handa@etl.go.jp>
7 ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
8 ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
9 ;; Keywords: mule, multilingual, input method
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; In Quail minor mode, you can input multilingual text easily. By
31 ;; defining a translation table (named Quail map) which maps ASCII key
32 ;; string to multilingual character or string, you can input any text
33 ;; from ASCII keyboard.
35 ;; We use words "translation" and "conversion" differently. The
36 ;; former is done by Quail package itself, the latter is the further
37 ;; process of converting a translated text to some more desirable
38 ;; text. For instance, Quail package for Japanese (`quail-jp')
39 ;; translates Roman text (transliteration of Japanese in Latin
40 ;; alphabets) to Hiragana text, which is then converted to
41 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
42 ;; CONVERSION-KEYS argument of the Quail package.
48 ;; Buffer local variables
50 (defvar quail-current-package nil
51 "The current Quail package 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 ;; Each buffer in which Quail is activated should use different
64 (make-variable-buffer-local 'quail-guidance-buf)
66 ;; A main window showing Quail guidance buffer.
67 (defvar quail-guidance-win nil)
68 (make-variable-buffer-local 'quail-guidance-win)
70 (defvar quail-mode nil
71 "Non-nil if in Quail minor mode.")
72 (make-variable-buffer-local 'quail-mode)
73 (put 'quail-mode 'permanent-local t)
75 (defvar quail-overlay nil
76 "Overlay which covers the current translation region of Quail.")
77 (make-variable-buffer-local 'quail-overlay)
79 (defvar quail-conv-overlay nil
80 "Overlay which covers the text to be converted in Quail mode.")
81 (make-variable-buffer-local 'quail-conv-overlay)
83 (defvar quail-current-key nil
84 "Current key for translation in Quail mode.")
86 (defvar quail-current-str nil
87 "Currently selected translation of the current key.")
89 (defvar quail-current-translations nil
90 "Cons of indices and vector of possible translations of the current key.
91 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
92 CURRENT is an index of the current translation,
93 START and END are indices of the start and end of the current block,
94 BLOCK is the current block index,
95 BLOCKS is a number of blocks of translation.")
97 (defvar quail-current-data nil
98 "Any Lisp object holding information of current translation status.
99 When a key sequence is mapped to TRANS and TRANS is a cons
100 of actual translation and some Lisp object to be refered
101 for translating the longer key sequence, this variable is set
102 to that Lisp object.")
103 (make-variable-buffer-local 'quail-current-data)
105 ;; A flag to control conversion region. Normally nil, but if set to
106 ;; t, it means we must start the new conversion region if new key to
107 ;; be translated is input.
108 (defvar quail-reset-conversion-region nil)
110 ;; Quail package handlers.
112 (defvar quail-package-alist nil
113 "List of Quail packages.
114 A Quail package is a list of these elements:
115 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
116 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
117 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
120 QUAIL-MAP is a data structure to map key strings to translations. For
121 the format, see the documentation of `quail-map-p'.
123 DECODE-MAP is an alist of translations and corresponding keys.
125 See the documentation of `quail-define-package' for the other elements.")
127 ;; Return various slots in the current quail-package.
129 (defsubst quail-name ()
130 "Return the name of the current Quail package."
131 (nth 0 quail-current-package))
132 (defsubst quail-title ()
133 "Return the title of the current Quail package."
134 (nth 1 quail-current-package))
135 (defsubst quail-map ()
136 "Return the translation map of the current Quail package."
137 (nth 2 quail-current-package))
138 (defsubst quail-guidance ()
139 "Return an object used for `guidance' feature of the current Quail package.
140 See also the documentation of `quail-define-package'."
141 (nth 3 quail-current-package))
142 (defsubst quail-docstring ()
143 "Return the documentation string of the current Quail package."
144 (nth 4 quail-current-package))
145 (defsubst quail-translation-keymap ()
146 "Return translation keymap in the current Quail package.
147 Translation keymap is a keymap used while translation region is active."
148 (nth 5 quail-current-package))
149 (defsubst quail-forget-last-selection ()
150 "Return `forget-last-selection' flag of the current Quail package.
151 See also the documentation of `quail-define-package'."
152 (nth 6 quail-current-package))
153 (defsubst quail-deterministic ()
154 "Return `deterministic' flag of the current Quail package.
155 See also the documentation of `quail-define-package'."
156 (nth 7 quail-current-package))
157 (defsubst quail-kbd-translate ()
158 "Return `kbd-translate' flag of the current Quail package.
159 See also the documentation of `quail-define-package'."
160 (nth 8 quail-current-package))
161 (defsubst quail-show-layout ()
162 "Return `show-layout' flag of the current Quail package.
163 See also the documentation of `quail-define-package'."
164 (nth 9 quail-current-package))
165 (defsubst quail-decode-map ()
166 "Return decode map of the current Quail package.
167 It is an alist of translations and corresponding keys."
168 (nth 10 quail-current-package))
169 (defsubst quail-maximum-shortest ()
170 "Return `maximum-shortest' flag of the current Quail package.
171 See also the documentation of `quail-define-package'."
172 (nth 11 quail-current-package))
173 (defsubst quail-overlay-plist ()
174 "Return property list of an overly used in the current Quail package."
175 (nth 12 quail-current-package))
176 (defsubst quail-update-translation-function ()
177 "Return a function for updating translation in the current Quail package."
178 (nth 13 quail-current-package))
179 (defsubst quail-conversion-keymap ()
180 "Return conversion keymap in the current Quail package.
181 Conversion keymap is a keymap used while conversion region is active
182 but translation region is not active."
183 (nth 14 quail-current-package))
185 (defsubst quail-package (name)
186 "Return Quail package named NAME."
187 (assoc name quail-package-alist))
189 (defun quail-add-package (package)
190 "Add Quail package PACKAGE to `quail-package-alist'."
191 (let ((pac (quail-package (car package))))
193 (setcdr pac (cdr package))
194 (setq quail-package-alist (cons package quail-package-alist)))))
196 (defun quail-select-package (name)
197 "Select Quail package named NAME as the current Quail package."
198 (let ((package (quail-package name)))
200 (error "No Quail package `%s'" name))
201 (setq quail-current-package package)
202 (setq-default quail-current-package package)
206 (defun quail-use-package (package-name &rest libraries)
207 "Start using Quail package PACKAGE-NAME.
208 The remaining arguments are libraries to be loaded before using the package."
209 (let ((package (quail-package package-name)))
211 ;; Perhaps we have not yet loaded necessary libraries.
213 (if (not (load (car libraries) t))
215 (with-output-to-temp-buffer "*Help*"
216 (princ "Quail package \"")
218 (princ "\" can't be activated\n because library \"")
219 (princ (car libraries))
220 (princ "\" is not in `load-path'.
222 The most common case is that you have not yet installed appropriate
223 libraries in LEIM (Libraries of Emacs Input Method) which is
224 distributed separately from Emacs.
226 LEIM is available from the same ftp directory as Emacs."))
227 (error "Can't use the Quail package `%s'" package-name))
228 (setq libraries (cdr libraries))))))
229 (quail-select-package package-name)
230 (setq current-input-method-title (quail-title))
233 (defun quail-inactivate ()
234 "Turn off Quail input method."
236 (throw 'quail-tag t))
238 (or (assq 'quail-mode minor-mode-alist)
239 (setq minor-mode-alist
240 (cons '(quail-mode " Quail") minor-mode-alist)))
242 (defvar quail-mode-map
243 (let ((map (make-keymap))
246 (define-key map (char-to-string i) 'quail-start-translation)
249 "Keymap for Quail mode.")
251 (or (assq 'quail-mode minor-mode-map-alist)
252 (setq minor-mode-map-alist
253 (cons (cons 'quail-mode quail-mode-map) minor-mode-map-alist)))
255 ;; Since some Emacs Lisp programs (e.g. viper.el) make
256 ;; minor-mode-map-alist buffer-local, we must be sure to register
257 ;; quail-mode-map in default-value of minor-mode-map-alist.
258 (if (local-variable-p 'minor-mode-map-alist)
259 (let ((map (default-value 'minor-mode-map-alist)))
260 (or (assq 'quail-mode map)
261 (set-default 'minor-mode-map-alist (cons 'quail-mode map)))))
263 (defvar quail-translation-keymap
264 (let ((map (make-keymap))
267 (define-key map (char-to-string i) 'quail-execute-non-quail-command)
270 (define-key map (char-to-string i) 'quail-self-insert-command)
272 (define-key map "\177" 'quail-delete-last-char)
273 (define-key map "\C-\\" 'quail-inactivate)
274 (define-key map "\C-f" 'quail-next-translation)
275 (define-key map "\C-b" 'quail-prev-translation)
276 (define-key map "\C-n" 'quail-next-translation-block)
277 (define-key map "\C-p" 'quail-prev-translation-block)
278 (define-key map "\C-i" 'quail-completion)
279 (define-key map "\C-@" 'quail-select-current)
280 (define-key map "\C-c" 'quail-abort-translation)
281 (define-key map "\C-h" 'quail-translation-help)
282 (define-key map "\e" '(keymap (t . quail-execute-non-quail-command)))
283 (define-key map [tab] 'quail-completion)
284 (define-key map [delete] 'quail-delete-last-char)
285 (define-key map [backspace] 'quail-delete-last-char)
286 ;; At last, define default key binding.
287 (append map '((t . quail-execute-non-quail-command))))
288 "Keymap used processing translation in Quail mode.
289 This map is activated while translation region is active.")
291 (defvar quail-conversion-keymap
292 (let ((map (make-keymap))
295 (define-key map (char-to-string i) 'quail-execute-non-quail-command)
298 (define-key map (char-to-string i)
299 'quail-start-translation-in-conversion-mode)
301 (define-key map "\C-b" 'quail-conversion-backward-char)
302 (define-key map "\C-f" 'quail-conversion-forward-char)
303 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
304 (define-key map "\C-e" 'quail-conversion-end-of-region)
305 (define-key map "\C-d" 'quail-conversion-delete-char)
306 (define-key map "\C-h" 'quail-conversion-help)
307 (define-key map "\C-\\" 'quail-inactivate)
308 (define-key map "\e" '(keymap (t . quail-execute-non-quail-command)))
309 (define-key map "\177" 'quail-conversion-backward-delete-char)
310 (define-key map [delete] 'quail-conversion-backward-delete-char)
311 (define-key map [backspace] 'quail-conversion-backward-delete-char)
312 ;; At last, define default key binding.
313 (append map '((t . quail-execute-non-quail-command))))
314 "Keymap used for processing conversion in Quail mode.
315 This map is activated while convesion region is active but translation
316 region is not active.")
319 (defun quail-define-package (name language title
320 &optional guidance docstring translation-keys
321 forget-last-selection deterministic
322 kbd-translate show-layout create-decode-map
323 maximum-shortest overlay-plist
324 update-translation-function
326 "Define NAME as a new Quail package for input LANGUAGE.
327 TITLE is a string to be displayed at mode-line to indicate this package.
328 Optional arguments are GUIDANCE, DOCSTRING, TRANLSATION-KEYS,
329 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
330 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
331 UPDATE-TRANSLATION-FUNCTION, and CONVERSION-KEYS.
333 GUIDANCE specifies how a guidance string is shown in echo area.
334 If it is t, list of all possible translations for the current key is shown
335 with the currently selected translation being highlighted.
336 If it is an alist, the element has the form (CHAR . STRING). Each character
337 in the current key is searched in the list and the corresponding string is
339 If it is nil, the current key is shown.
341 DOCSTRING is the documentation string of this package.
343 TRANSLATION-KEYS specifies additional key bindings used while translation
344 region is active. It is an alist of single key character vs. corresponding
345 command to be called.
347 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
348 for the future to translate the same key. If this flag is nil, a
349 translation selected for a key is remembered so that it can be the
350 first candidate when the same key is entered later.
352 DETERMINISTIC non-nil means the first candidate of translation is
353 selected automatically without allowing users to select another
354 translation for a key. In this case, unselected translations are of
355 no use for an interactive use of Quail but can be used by some other
356 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
359 KBD-TRANSLATE non-nil means input characters are translated from a
360 user's keyboard layout to the standard keyboard layout. See the
361 documentation of `quail-keyboard-layout' and
362 `quail-keyboard-layout-standard' for more detail.
364 SHOW-LAYOUT non-nil means the `quail-help' command should show
365 the user's keyboard layout visually with translated characters.
366 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
367 this package defines no translations for single character keys.
369 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
370 map is an alist of translations and corresponding original keys.
371 Although this map is not used by Quail itself, it can be used by some
372 other programs. For instance, Vietnamese supporting needs this map to
373 convert Vietnamese text to VIQR format which uses only ASCII
374 characters to represent Vietnamese characters.
376 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
377 length of the shortest sequence. When we don't have a translation of
378 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
379 the key at \"..AB\" and start translation of \"CD..\". Hangul
380 packages, for instance, use this facility. If this flag is nil, we
381 break the key just at \"..ABC\" and start translation of \"D..\".
383 OVERLAY-PLIST if non-nil is a property list put on an overlay which
384 covers Quail translation region.
386 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
387 the current translation region accoding to a new translation data. By
388 default, a tranlated text or a user's key sequence (if no transltion
391 CONVERSION-KEYS specifies additional key bindings used while
392 conversion region is active. It is an alist of single key character
393 vs. corresponding command to be called."
394 (let (translation-keymap conversion-keymap)
395 (if deterministic (setq forget-last-selection t))
398 (setq translation-keymap (copy-keymap quail-translation-keymap))
399 (while translation-keys
400 (define-key translation-keymap
401 (car (car translation-keys)) (cdr (car translation-keys)))
402 (setq translation-keys (cdr translation-keys))))
403 (setq translation-keymap quail-translation-keymap))
406 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
407 (while conversion-keys
408 (define-key conversion-keymap
409 (car (car conversion-keys)) (cdr (car conversion-keys)))
410 (setq conversion-keys (cdr conversion-keys)))))
412 (list name title (list nil) guidance (or docstring "")
414 forget-last-selection deterministic kbd-translate show-layout
415 (if create-decode-map (list 'decode-map) nil)
416 maximum-shortest overlay-plist update-translation-function
419 ;; Update input-method-alist.
420 (let ((slot (assoc name input-method-alist))
421 (val (list language 'quail-use-package title docstring)))
422 (if slot (setcdr slot val)
423 (setq input-method-alist (cons (cons name val) input-method-alist)))))
425 (quail-select-package name))
427 ;; Quail minor mode handlers.
429 ;; Setup overlays used in Quail mode.
430 (defun quail-setup-overlays ()
432 (if (overlayp quail-overlay)
433 (move-overlay quail-overlay pos pos)
434 (setq quail-overlay (make-overlay pos pos nil nil t))
435 (overlay-put quail-overlay 'face 'underline)
436 (let ((l (quail-overlay-plist)))
438 (overlay-put quail-overlay (car l) (car (cdr l)))
439 (setq l (cdr (cdr l))))))
440 (if (overlayp quail-conv-overlay)
441 (move-overlay quail-conv-overlay pos pos)
442 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
443 (overlay-put quail-conv-overlay 'face 'underline)
444 ;;(overlay-put quail-conv-overlay 'modification-hooks
445 ;;'(quail-conv-overlay-modification-hook))
448 ;; Delete overlays used in Quail mode.
449 (defun quail-delete-overlays ()
450 (if (overlayp quail-overlay)
451 (delete-overlay quail-overlay))
452 (if (overlayp quail-conv-overlay)
453 (delete-overlay quail-conv-overlay)))
455 ;; While translating and converting, we enter and exit the recursive
456 ;; edit frequently, which results in frequent and annoying change of
457 ;; mode line. To avoid it, we use a modified mode-line-format.
458 (defvar quail-mode-line-format nil)
460 ;; Return a modified mode-line-format which doesn't show the recursive
461 ;; editing level. But, we only pay attention to the top level
462 ;; elements of the current mode-line-format.
463 (defun quail-generate-mode-line-format ()
464 (if (listp mode-line-format)
465 (let ((new (copy-sequence mode-line-format))
470 (if (and (stringp elt)
471 (or (setq idx (string-match "%\\[" elt))
472 (setq idx (string-match "%\\]" elt))))
473 (setcar l (concat (substring elt 0 idx)
474 (substring elt (+ idx 2)))))
479 (defun quail-mode (&optional arg)
480 "Toggle Quail minor mode.
481 With arg, turn Quail mode on if and only if arg is positive.
482 Try \\[describe-bindings] in Quail mode to see the available key binding.
483 The command \\[describe-input-method] describes the current Quail package."
486 (if (null arg) (null quail-mode)
487 (> (prefix-numeric-value arg) 0)))
488 (if (null quail-mode)
489 ;; Let's turn off Quail mode.
491 (quail-hide-guidance-buf)
492 (quail-delete-overlays)
493 (setq describe-current-input-method-function nil)
494 (setq current-input-method nil)
495 (run-hooks 'quail-mode-exit-hook)
496 (run-hooks 'input-method-inactivate-hook))
497 ;; Let's turn on Quail mode.
498 ;; At first, be sure that quail-mode is at the first element of
499 ;; minor-mode-map-alist.
500 (or (eq (car minor-mode-map-alist) 'quail-mode)
501 (let ((l minor-mode-map-alist))
503 (if (eq (car (cdr l)) 'quail-mode)
505 (setcdr l (cdr (cdr l)))
508 (setq minor-mode-map-alist (cons 'quail-mode minor-mode-map-alist))))
509 (if (null quail-current-package)
510 ;; Quail package is not yet selected. Select one now.
512 (if quail-package-alist
513 (setq name (car (car quail-package-alist)))
514 (setq quail-mode nil)
515 (error "No Quail package loaded"))
516 (quail-select-package name)))
517 (setq inactivate-current-input-method-function 'quail-mode)
518 (setq describe-current-input-method-function 'quail-help)
519 (setq quail-mode-line-format (quail-generate-mode-line-format))
520 (quail-delete-overlays)
521 (quail-show-guidance-buf)
522 ;; If we are in minibuffer, turn off Quail mode before exiting.
523 (if (eq (selected-window) (minibuffer-window))
524 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
525 (make-local-hook 'post-command-hook)
526 (run-hooks 'quail-mode-hook)
527 (run-hooks 'input-method-activate-hook))
528 (force-mode-line-update))
530 (defun quail-exit-from-minibuffer ()
531 (if quail-mode (quail-mode -1))
532 (if (<= (minibuffer-depth) 1)
533 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
535 (defvar quail-saved-overriding-local-map nil)
536 (defvar quail-saved-current-buffer nil)
538 ;; Toggle `quail-mode'. This function is added to `post-command-hook'
539 ;; in Quail mode, to turn Quail mode temporarily off, or back on
540 ;; after one non-Quail command.
541 (defun quail-toggle-mode-temporarily ()
543 ;; We are going to handle following events out of Quail mode.
545 quail-saved-overriding-local-map overriding-local-map
546 quail-saved-current-buffer (current-buffer)
547 overriding-local-map nil)
548 ;; We have just executed one non-Quail command. We don't need
549 ;; this hook any more.
550 (remove-hook 'post-command-hook 'quail-toggle-mode-temporarily t)
551 ;; If the command changed the current buffer, we should not go
552 ;; back to Quail mode.
553 (if (not (eq (current-buffer) quail-saved-current-buffer))
554 (throw 'quail-tag nil)
555 ;; Let's go back to Quail mode.
557 (setq overriding-local-map quail-saved-overriding-local-map)
558 ;; If whole text in conversion area was deleted, exit from the
560 (let ((start (overlay-start quail-conv-overlay)))
561 (if (and start (= start (overlay-end quail-conv-overlay)))
562 (throw 'quail-tag nil)))
565 (defun quail-execute-non-quail-command ()
566 "Execute one non-Quail command in Quail mode.
567 The current translation and conversion are terminated."
569 (setq unread-command-events (cons last-input-event unread-command-events))
570 (quail-delete-overlays)
571 (if (buffer-live-p quail-guidance-buf)
573 (set-buffer quail-guidance-buf)
575 (throw 'quail-tag nil))
577 ;; Keyboard layout translation handlers.
579 ;; Some Quail packages provide localized keyboard simulation which
580 ;; requires a particular keyboard layout. In this case, what we need
581 ;; is locations of keys the user entered, not character codes
582 ;; generated by those keys. However, for the moment, there's no
583 ;; common way to get such information. So, we ask a user to give
584 ;; information of his own keyboard layout, then translate it to the
585 ;; standard layout which we defined so that all Quail packages depend
588 (defconst quail-keyboard-layout-standard
591 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
592 qQwWeErRtTyYuUiIoOpP[{]} \
593 aAsSdDfFgGhHjJkKlL;:'\"\\| \
594 zZxXcCvVbBnNmM,<.>/? \
596 "Standard keyboard layout of printable characters Quail assumes.
597 See the documentation of `quail-keyboard-layout' for this format.
598 This layout is almost the same as that of VT100,
599 but the location of key \\ (backslash) is just right of key ' (single-quote),
600 not right of RETURN key.")
602 (defvar quail-keyboard-layout quail-keyboard-layout-standard
603 "A string which represents physical key layout of a particular keyboard.
604 We assume there are six rows and each row has 15 keys (columns),
605 the first row is above the `1' - `0' row,
606 the first column of the second row is left of key `1',
607 the first column of the third row is left of key `q',
608 the first column of the fourth row is left of key `a',
609 the first column of the fifth row is left of key `z',
610 the sixth row is below the `z' - `/' row.
611 Nth (N is even) and (N+1)th characters in the string are non-shifted
612 and shifted characters respectively at the same location.
613 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).")
615 (defconst quail-keyboard-layout-len 180)
617 ;; Here we provide several examples of famous keyboard layouts.
619 (defvar quail-keyboard-layout-alist
623 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
624 qQwWeErRtTyYuUiIoOpP[{]} \
625 aAsSdDfFgGhHjJkKlL;:'\" \
626 zZxXcCvVbBnNmM,<.>/? \
628 (cons "standard" quail-keyboard-layout-standard))
629 "Alist of keyboard names and corresponding layout strings.
630 See the documentation of `quail-keyboard-layout' for the format of
633 (defun quail-set-keyboard-layout (kbd-type)
634 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
636 Since some Quail packages depends on a physical layout of keys (not
637 characters generated by them), those are created by assuming the
638 standard layout defined in `quail-keyboard-layout-standard'. This
639 function tells Quail system the layout of your keyboard so that what
640 you type is correctly handled."
642 (let* ((completing-ignore-case t)
643 (type (completing-read "Keyboard type: "
644 quail-keyboard-layout-alist)))
646 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
648 ;; Here, we had better ask a user to define his own keyboard
649 ;; layout interactively.
650 (error "Unknown keyboard type `%s'" kbd-type))
651 (setq quail-keyboard-layout (cdr layout))))
653 (defun quail-keyboard-translate (ch)
654 "Translate CHAR according to `quail-keyboard-layout' and return the result."
655 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
658 (while (and (< i quail-keyboard-layout-len)
659 (/= ch (aref quail-keyboard-layout i)))
661 (if (= i quail-keyboard-layout-len)
662 (error "Character `%c' not found in your keyboard layout" ch))
663 (aref quail-keyboard-layout-standard i))))
667 (defsubst quail-map-p (object)
668 "Return t if OBJECT is a Quail map.
670 A Quail map holds information how a particular key should be translated.
671 Its format is (TRANSLATION . ALIST).
672 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
673 In the latter case, each element of VECTOR is a candidate for the translation,
674 and INDEX points the currently selected translation.
676 ALIST is normally a list of elements that look like (CHAR . DEFN),
677 where DEFN is another Quail map for a longer key (CHAR added to the
678 current key). It may also be a symbol of a function which returns an
679 alist of the above format.
681 Just after a Quail package is read, TRANSLATION may be a string or a
682 vector. Then each element of the string or vector is a candidate for
683 the translation. These objects are transformed to cons cells in the
684 format \(INDEX . VECTOR), as described above."
686 (let ((translation (car object)))
687 (or (integerp translation) (null translation)
688 (vectorp translation) (stringp translation)
689 (symbolp translation)
690 (and (consp translation) (not (vectorp (cdr translation))))))
691 (let ((alist (cdr object)))
692 (or (and (listp alist) (consp (car alist)))
696 (defmacro quail-define-rules (&rest rules)
697 "Define translation rules of the current Quail package.
698 Each argument is a list of KEY and TRANSLATION.
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."
713 (quail-defrule-internal (car (car l)) (car (cdr (car l))) map)
718 (defun quail-install-map (map)
719 "Install the Quail map MAP in the current Quail package.
720 The installed map can be referred by the function `quail-map'."
721 (if (null quail-current-package)
722 (error "No current Quail package"))
723 (if (null (quail-map-p map))
724 (error "Invalid Quail map `%s'" map))
725 (setcar (cdr (cdr quail-current-package)) map))
728 (defun quail-defrule (key translation &optional name)
729 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
730 KEY is a string meaning a sequence of keystrokes to be translated.
731 TRANSLATION is a character, a string, a vector, a Quail map,
732 a function, or a cons.
733 It it is a character, it is the sole translation of KEY.
734 If it is a string, each character is a candidate for the translation.
735 If it is a vector, each element (string or character) is a candidate
737 If it is a cons, the car is one of the above and the cdr is a function
738 to call when translating KEY (the return value is assigned to the
739 variable `quail-current-data'). If the cdr part is not a function,
740 the value itself is assigned to `quail-current-data'.
741 In these cases, a key specific Quail map is generated and assigned to KEY.
743 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
744 it is used to handle KEY.
745 Optional argument NAME, if specified, says which Quail package
746 to define this translation rule in. The default is to define it in the
747 current Quail package."
749 (let ((package (quail-package name)))
751 (error "No Quail package `%s'" name))
752 (setq quail-current-package package)))
753 (quail-defrule-internal key translation (quail-map)))
756 (defun quail-defrule-internal (key trans map)
757 "Define KEY as TRANS in a Quail map MAP."
758 (if (null (stringp key))
759 "Invalid Quail key `%s'" key)
760 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
763 (quail-map-p trans)))
764 (error "Invalid Quail translation `%s'" trans))
765 (if (null (quail-map-p map))
766 (error "Invalid Quail map `%s'" map))
767 (let ((len (length key))
770 ;; Make a map for registering TRANS if necessary.
772 (if (null (consp map))
773 ;; We come here, for example, when we try to define a rule
774 ;; for "ABC" but a rule for "AB" is already defined as a
776 (error "Quail key %s is too long" key))
777 (setq ch (aref key idx)
778 entry (assq ch (cdr map)))
781 (setq entry (cons ch (list nil)))
782 (setcdr map (cons entry (cdr map)))))
783 (setq map (cdr entry))
787 ;; We come here, for example, when we try to define a rule
788 ;; for "AB" as a symbol but a rule for "ABC" is already
790 (error "Quail key %s is too short" key)
791 (setcdr entry trans))
792 (if (quail-map-p trans)
793 (if (not (listp (cdr map)))
794 ;; We come here, for example, when we try to define a rule
795 ;; for "AB" as a symbol but a rule for "ABC" is already
797 (error "Quail key %s is too short" key)
798 (if (not (listp (cdr trans)))
800 ;; We come here, for example, when we try to
801 ;; define a rule for "AB" as a symbol but a rule
802 ;; for "ABC" is already defined.
803 (error "Quail key %s is too short" key)
804 (setcdr entry trans))
805 (setcdr entry (append trans (cdr map)))))
806 (setcar map trans)))))
808 (defun quail-get-translation (def key len)
809 "Return the translation specified as DEF for KEY of length LEN.
810 The translation is either a character or a cons of the form (INDEX . VECTOR),
811 where VECTOR is a vector of candidates (character or string) for
812 the translation, and INDEX points into VECTOR to specify the currently
813 selected translation."
814 (if (and def (symbolp def))
815 ;; DEF is a symbol of a function which returns valid translation.
816 (setq def (funcall def key len)))
817 (if (and (consp def) (not (vectorp (cdr def))))
818 (setq def (car def)))
821 ((or (integerp def) (consp def))
829 ;; Each character in DEF is a candidate of translation. Reform
830 ;; it as (INDICES . VECTOR).
831 (setq def (string-to-vector def))
832 ;; But if the length is 1, we don't need vector but a single
833 ;; candidate as the translation.
834 (if (= (length def) 1)
836 (cons (list 0 0 0 0 nil) def)))
839 ;; Each element (string or character) in DEF is a candidate of
840 ;; translation. Reform it as (INDICES . VECTOR).
841 (cons (list 0 0 0 0 nil) def))
844 (error "Invalid object in Quail map: %s" def))))
846 (defun quail-lookup-key (key &optional len)
847 "Lookup KEY of length LEN in the current Quail map and return the definition.
848 The returned value is a Quail map specific to KEY."
850 (setq len (length key)))
853 (kbd-translate (quail-kbd-translate))
854 slot ch translation def)
855 (while (and map (< idx len))
856 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
859 (if (and (cdr map) (symbolp (cdr map)))
860 (setcdr map (funcall (cdr map) key idx)))
861 (setq slot (assq ch (cdr map)))
862 (if (and (cdr slot) (symbolp (cdr slot)))
863 (setcdr slot (funcall (cdr slot) key idx)))
864 (setq map (cdr slot)))
866 (setq quail-current-translations nil)
867 (if (and map (setq translation (quail-get-translation def key len)))
869 (if (and (consp def) (not (vectorp (cdr def))))
871 (if (not (equal (car def) translation))
872 ;; We must reflect TRANSLATION to car part of DEF.
873 (setcar def translation))
874 (setq quail-current-data
875 (if (functionp (cdr def))
878 (if (not (equal def translation))
879 ;; We must reflect TRANSLATION to car part of MAP.
880 (setcar map translation)))
881 (if (and (consp translation) (vectorp (cdr translation)))
883 (setq quail-current-translations translation)
884 (if (quail-forget-last-selection)
885 (setcar (car quail-current-translations) 0))))
886 ;; We may have to reform cdr part of MAP.
887 (if (and (cdr map) (symbolp (cdr map)))
889 (setcdr map (funcall (cdr map) key len))))
893 (defun quail-conv-overlay-modification-hook (overlay after &rest ignore)
895 (= (overlay-start overlay) (overlay-end overlay)))
896 ;; Whole text in conversion area was deleted. Let's exit from
897 ;; the recursive edit.
900 (defvar quail-suppress-conversion nil
901 "If non-nil, suppress converting facility of the current Quail package.")
903 ;; If set to non-nil, exit conversion mode before starting new translation.
904 (defvar quail-exit-conversion-mode nil)
906 (defvar quail-prefix-arg nil)
908 (defun quail-start-translation (arg)
909 "Start translating the typed character in Quail mode."
911 (setq prefix-arg arg)
912 (setq quail-prefix-arg arg)
913 (setq unread-command-events
914 (cons last-command-event unread-command-events))
915 ;; Check the possibility of translating the last key.
916 (if (assq last-command-event (cdr (quail-map)))
917 ;; Ok, we can start translation.
918 (let ((mode-line-format quail-mode-line-format))
919 (quail-setup-overlays)
920 (if (catch 'quail-tag
921 (if (and (not quail-suppress-conversion)
922 (quail-conversion-keymap))
923 ;; We must start translation in conversion mode.
924 (let ((overriding-local-map (quail-conversion-keymap)))
925 (setq quail-exit-conversion-mode nil)
927 (if (and auto-fill-function
928 (> (current-column) (current-fill-column)))
929 (run-hooks 'auto-fill-function)))
930 (let ((overriding-local-map (quail-translation-keymap)))
931 (setq quail-current-key "")
933 (if (prog1 (< (overlay-start quail-conv-overlay)
934 (overlay-end quail-conv-overlay))
935 (delete-overlay quail-conv-overlay))
936 (run-hooks 'input-method-after-insert-chunk-hook))
938 ;; Someone has thrown a tag with value t, which means
939 ;; we should turn Quail mode off.
941 ;; Since the typed character doesn't start any translation, handle
942 ;; it out of Quail mode. We come back to Quail mode later because
943 ;; function `quail-toggle-mode-temporarily' is in
944 ;; `post-command-hook'.
945 (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
947 (defsubst quail-point-in-conversion-region ()
948 "Return non-nil value if the point is in conversion region of Quail mode."
950 (and (setq start (overlay-start quail-conv-overlay))
951 (>= (setq pos (point)) start)
952 (<= pos (overlay-end quail-conv-overlay)))))
954 (defun quail-start-translation-in-conversion-mode ()
955 "Start translating the typed character in conversion mode of Quail mode."
957 (setq unread-command-events
958 (cons last-command-event unread-command-events))
959 (if (or quail-exit-conversion-mode
960 (not (quail-point-in-conversion-region)))
962 ;; We must start translation with new conversion region.
963 (setq quail-exit-conversion-mode nil)
965 ;; Check the possibility of translating the last key.
966 (if (assq last-command-event (cdr (quail-map)))
967 ;; Ok, we can start translation.
968 (let ((overriding-local-map (quail-translation-keymap)))
969 (setq quail-current-key "")
970 (move-overlay quail-overlay (point) (point))
972 ;; Since the typed character doesn't start any translation, handle
973 ;; it out of Quail mode. We come back to Quail mode later because
974 ;; function `quail-toggle-mode-temporarily' is in
975 ;; `post-command-hook'.
976 (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
978 (defsubst quail-delete-region ()
979 "Delete the text in the current translation region of Quail."
980 (delete-region (overlay-start quail-overlay) (overlay-end quail-overlay)))
982 (defun quail-terminate-translation ()
983 "Terminate the translation of the current key."
984 (let ((start (overlay-start quail-overlay)))
986 (< start (overlay-end quail-overlay)))
987 ;; Here we simulate self-insert-command.
988 (let ((seq (string-to-sequence
989 (buffer-substring (overlay-start quail-overlay)
990 (overlay-end quail-overlay))
994 (quail-delete-region)
995 (setq last-command-char (car seq))
996 (self-insert-command (or quail-prefix-arg 1))
999 (setq last-command-char (car seq))
1000 (self-insert-command 1)
1001 (setq seq (cdr seq))))))
1002 (delete-overlay quail-overlay)
1003 (if (buffer-live-p quail-guidance-buf)
1005 (set-buffer quail-guidance-buf)
1009 (defun quail-select-current ()
1010 "Select the current text shown in Quail translation region."
1012 (quail-terminate-translation))
1014 ;; Update the current translation status according to CONTROL-FLAG.
1015 ;; If CONTROL-FLAG is integer value, it is the number of keys in the
1016 ;; head quail-current-key which can be translated. The remaining keys
1017 ;; are put back to unread-command-events to be handled again.
1018 ;; If CONTROL-FLAG is t, terminate the translation for the whole keys
1019 ;; in quail-current-key.
1020 ;; If CONTROL-FLAG is nil, proceed the translation with more keys.
1022 (defun quail-update-translation (control-flag)
1023 (quail-delete-region)
1024 (let ((func (quail-update-translation-function)))
1026 (funcall func control-flag)
1027 (if (numberp control-flag)
1028 (let ((len (length quail-current-key)))
1029 (while (> len control-flag)
1031 (setq unread-command-events
1032 (cons (aref quail-current-key len)
1033 unread-command-events)))
1034 (insert (or quail-current-str
1035 (substring quail-current-key 0 len))))
1036 (insert (or quail-current-str quail-current-key)))))
1037 (quail-update-guidance)
1039 (quail-terminate-translation)))
1041 (defun quail-self-insert-command ()
1042 "Add the typed character to the key for translation."
1044 (setq quail-current-key
1045 (concat quail-current-key (char-to-string last-command-event)))
1046 (quail-update-translation (quail-translate-key)))
1048 ;; Return the actual definition part of Quail map MAP.
1049 (defun quail-map-definition (map)
1050 (let ((def (car map)))
1051 (if (and (consp def) (not (vectorp (cdr def))))
1052 (setq def (car def)))
1055 ;; Return a string to be shown as the current translation of key
1056 ;; sequence of length LEN. DEF is a definition part of Quail map for
1058 (defun quail-get-current-str (len def)
1059 (or (and (consp def) (aref (cdr def) (car (car def))))
1062 (let ((str (quail-get-current-str
1064 (quail-map-definition (quail-lookup-key
1065 quail-current-key (1- len))))))
1067 (concat (if (stringp str) str (char-to-string str))
1068 (substring quail-current-key (1- len) len)))))))
1070 (defvar quail-guidance-translations-starting-column 20)
1072 ;; Update `quail-current-translations' to make RELATIVE-INDEX the
1073 ;; current translation.
1074 (defun quail-update-current-translations (&optional relative-index)
1075 (let* ((indices (car quail-current-translations))
1077 (start (nth 1 indices))
1078 (end (nth 2 indices)))
1079 ;; Validate the index number of current translation.
1081 (setcar indices (setq cur 0))
1082 (if (>= cur (length (cdr quail-current-translations)))
1084 (setq cur (1- (length (cdr quail-current-translations)))))))
1086 (if (or (null end) ; We have not yet calculated END.
1087 (< cur start) ; We moved to the previous block.
1088 (>= cur end)) ; We moved to the next block.
1089 (let ((len (length (cdr quail-current-translations)))
1090 (maxcol (- (window-width quail-guidance-win)
1091 quail-guidance-translations-starting-column))
1092 (block (nth 3 indices))
1093 col idx width trans num-items blocks)
1095 ;; We must calculate from the head.
1096 (setq start 0 block 0)
1097 (if end ; i.e. (>= cur end)
1099 (setq idx start col 0 end start num-items 0)
1100 ;; Loop until we hit the tail, or reach the block of CUR.
1101 (while (and (< idx len) (>= cur end))
1103 (setq start idx col 0 block (1+ block)))
1104 (setq trans (aref (cdr quail-current-translations) idx))
1105 (setq width (if (integerp trans) (char-width trans)
1106 (string-width trans)))
1107 (setq col (+ col width 3) num-items (1+ num-items))
1108 (if (and (> num-items 0)
1109 (or (>= col maxcol) (> num-items 10)))
1110 (setq end idx num-items 0)
1111 (setq idx (1+ idx))))
1112 (setcar (nthcdr 3 indices) block)
1115 ;; We hit the tail before reaching MAXCOL.
1117 (setcar (nthcdr 4 indices) block)))
1118 (setcar (cdr indices) start)
1119 (setcar (nthcdr 2 indices) end)))
1121 (if (>= (+ start relative-index) end)
1122 (setcar indices end)
1123 (setcar indices (+ start relative-index))))
1124 (setq quail-current-str
1125 (aref (cdr quail-current-translations) (car indices)))))
1127 (defun quail-translate-key ()
1128 "Translate the current key sequence according to the current Quail map.
1129 Return t if we can terminate the translation.
1130 Return nil if the current key sequence may be followed by more keys.
1131 Return number if we can't find any translation for the current key
1132 sequence. The number is the count of valid keys in the current
1133 sequence counting from the head."
1134 (let* ((len (length quail-current-key))
1135 (map (quail-lookup-key quail-current-key len))
1138 (let ((def (quail-map-definition map)))
1139 (setq quail-current-str (quail-get-current-str len def))
1140 ;; Return t only if we can terminate the current translation.
1142 ;; No alternative translations.
1143 (or (null (consp def)) (= (length (cdr def)) 1))
1144 ;; No translation for the longer key.
1146 ;; No shorter breaking point.
1147 (or (null (quail-maximum-shortest))
1149 (null (quail-lookup-key quail-current-key (1- len)))
1150 (null (quail-lookup-key
1151 (substring quail-current-key -2 -1) 1)))))
1153 ;; There's no translation for the current key sequence. Before
1154 ;; giving up, we must check two possibilities.
1156 (quail-maximum-shortest)
1158 (setq def (quail-map-definition
1159 (quail-lookup-key quail-current-key (- len 2))))
1160 (quail-lookup-key (substring quail-current-key -2) 2))
1161 ;; Now the sequence is "...ABCD", which can be split into
1162 ;; "...AB" and "CD..." to get valid translation.
1163 ;; At first, get translation of "...AB".
1164 (setq quail-current-str (quail-get-current-str (- len 2) def))
1165 ;; Then, return the length of "...AB".
1168 ((and quail-current-translations
1169 (not (quail-deterministic))
1170 (setq ch (aref quail-current-key (1- len)))
1171 (>= ch ?0) (<= ch ?9))
1172 ;; A numeric key is entered to select a desirable translation.
1173 (setq quail-current-key (substring quail-current-key 0 -1))
1174 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1175 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1176 (quail-update-current-translations ch)
1177 ;; And, we can terminate the current translation.
1181 ;; No way to handle the last character in this context.
1184 (defun quail-next-translation ()
1185 "Select next translation in the current batch of candidates."
1187 (if quail-current-translations
1188 (let ((indices (car quail-current-translations)))
1189 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1190 ;; We are alread at the tail.
1192 (setcar indices (1+ (car indices)))
1193 (quail-update-current-translations)
1194 (quail-update-translation nil)))
1197 (defun quail-prev-translation ()
1198 "Select previous translation in the current batch of candidates."
1200 (if quail-current-translations
1201 (let ((indices (car quail-current-translations)))
1202 (if (= (car indices) 0)
1203 ;; We are already at the head.
1205 (setcar indices (1- (car indices)))
1206 (quail-update-current-translations)
1207 (quail-update-translation nil)))
1210 (defun quail-next-translation-block ()
1211 "Select from the next block of translations."
1213 (if quail-current-translations
1214 (let* ((indices (car quail-current-translations))
1215 (offset (- (car indices) (nth 1 indices))))
1216 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1217 ;; We are already at the last block.
1219 (setcar indices (+ (nth 2 indices) offset))
1220 (quail-update-current-translations)
1221 (quail-update-translation nil)))
1224 (defun quail-prev-translation-block ()
1225 "Select the previous batch of 10 translation candidates."
1227 (if quail-current-translations
1228 (let* ((indices (car quail-current-translations))
1229 (offset (- (car indices) (nth 1 indices))))
1230 (if (= (nth 1 indices) 0)
1231 ;; We are already at the first block.
1233 (setcar indices (1- (nth 1 indices)))
1234 (quail-update-current-translations)
1235 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1237 (setcar indices (+ (nth 1 indices) offset))
1238 (quail-update-current-translations)))
1239 (quail-update-translation nil)))
1242 (defun quail-abort-translation ()
1243 "Abort translation and delete the current Quail key sequence."
1245 (quail-delete-region)
1246 (quail-terminate-translation))
1248 (defun quail-delete-last-char ()
1249 "Delete the last input character from the current Quail key sequence."
1251 (if (= (length quail-current-key) 1)
1252 (quail-abort-translation)
1253 (setq quail-current-key (substring quail-current-key 0 -1))
1254 (quail-update-translation (quail-translate-key))))
1256 ;; For conversion mode.
1258 (defun quail-conversion-backward-char ()
1260 (if (<= (point) (overlay-start quail-conv-overlay))
1261 (error "Beginning of conversion region"))
1264 (defun quail-conversion-forward-char ()
1266 (if (>= (point) (overlay-end quail-conv-overlay))
1267 (error "End of conversion region"))
1270 (defun quail-conversion-beginning-of-region ()
1272 (goto-char (overlay-start quail-conv-overlay)))
1274 (defun quail-conversion-end-of-region ()
1276 (goto-char (overlay-end quail-conv-overlay)))
1278 (defun quail-conversion-delete-char ()
1280 (if (>= (point) (overlay-end quail-conv-overlay))
1281 (error "End of conversion region"))
1283 (if (= (overlay-start quail-conv-overlay)
1284 (overlay-end quail-conv-overlay))
1285 (throw 'quail-tag nil)))
1287 (defun quail-conversion-backward-delete-char ()
1289 (if (<= (point) (overlay-start quail-conv-overlay))
1290 (error "Beginning of conversion region"))
1292 (if (= (overlay-start quail-conv-overlay)
1293 (overlay-end quail-conv-overlay))
1294 (throw 'quail-tag nil)))
1296 (defun quail-do-conversion (func &rest args)
1297 "Call FUNC to convert text in the current conversion region of Quail.
1298 Remaining args are for FUNC."
1299 (delete-overlay quail-overlay)
1302 (defun quail-no-conversion ()
1303 "Do no conversion of the current conversion region of Quail."
1307 ;; Guidance, Completion, and Help buffer handlers.
1309 ;; Make a new one-line frame for Quail guidance buffer.
1310 (defun quail-make-guidance-frame (buf)
1311 (let* ((fparam (frame-parameters))
1312 (top (cdr (assq 'top fparam)))
1313 (border (cdr (assq 'border-width fparam)))
1314 (internal-border (cdr (assq 'internal-border-width fparam)))
1316 (frame-char-height) (* internal-border 2) (* border 2))))
1318 (setq newtop (+ top (frame-pixel-height))))
1319 (let* ((frame (make-frame (append '((user-position . t) (height . 1)
1320 (minibuffer) (menu-bar-lines . 0))
1321 (cons (cons 'top newtop) fparam))))
1322 (win (frame-first-window frame)))
1323 (set-window-buffer win buf)
1324 (set-window-dedicated-p win t))))
1326 (defun quail-show-guidance-buf ()
1327 "Display a guidance buffer for Quail input method in some window.
1328 Create the buffer if it does not exist yet.
1329 The buffer is normally displayed at the echo area,
1330 but if the current buffer is a minibuffer, it is shown in
1331 the bottom-most ordinary window of the same frame,
1332 or in a newly created frame (if the selected frame has no other windows)."
1333 (if (and input-method-tersely-flag
1334 (eq (selected-window) (minibuffer-window)))
1335 ;; We don't need the guidance buffer.
1337 ;; At first, setup a guidance buffer.
1338 (or (buffer-live-p quail-guidance-buf)
1339 (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*")))
1340 (let ((title (quail-title)))
1342 (set-buffer quail-guidance-buf)
1343 ;; To show the title of Quail package.
1344 (setq current-input-method t
1345 current-input-method-title title)
1347 (or (overlayp quail-overlay)
1349 (setq quail-overlay (make-overlay 1 1))
1350 (overlay-put quail-overlay 'face 'highlight)))
1351 (delete-overlay quail-overlay)
1352 (set-buffer-modified-p nil)))
1353 (bury-buffer quail-guidance-buf)
1355 ;; Then, display it in an appropriate window.
1356 (let ((win (minibuffer-window)))
1357 (if (eq (selected-window) win)
1358 ;; Since we are in minibuffer, we can't use it for guidance.
1359 (if (eq win (frame-root-window))
1360 ;; Create a frame. It is sure that we are using some
1362 (quail-make-guidance-frame quail-guidance-buf)
1363 ;; Find the bottom window and split it if necessary.
1365 (setq win (window-at 0 (- (frame-height) 2)))
1366 (setq height (window-height win))
1367 ;; If WIN is tall enough, split it vertically and use
1370 (let ((window-min-height 2))
1371 ;; Here, `split-window' returns a lower window
1372 ;; which is what we wanted.
1373 (setq win (split-window win (- height 2)))))
1374 (set-window-buffer win quail-guidance-buf)
1375 (set-window-dedicated-p win t)))
1376 (set-window-buffer win quail-guidance-buf))
1377 (setq quail-guidance-win win)))
1379 ;; And, create a buffer for completion.
1380 (or (buffer-live-p quail-completion-buf)
1382 (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
1384 (set-buffer quail-completion-buf)
1385 (setq quail-overlay (make-overlay 1 1))
1386 (overlay-put quail-overlay 'face 'highlight))))
1387 (bury-buffer quail-completion-buf))
1389 (defun quail-hide-guidance-buf ()
1390 "Hide the Quail guidance buffer."
1391 (if (buffer-live-p quail-guidance-buf)
1392 (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
1395 (setq win (car win-list) win-list (cdr win-list))
1396 (if (eq win (minibuffer-window))
1397 ;; We are using echo area for the guidance buffer.
1398 ;; Vacate it to the deepest minibuffer.
1399 (set-window-buffer win
1400 (format " *Minibuf-%d*" (minibuffer-depth)))
1401 (if (eq win (frame-root-window (window-frame win)))
1403 ;; We are using a separate frame for guidance buffer.
1404 ;;(set-window-dedicated-p win nil)
1405 (delete-frame (window-frame win)))
1406 (set-window-dedicated-p win nil)
1407 (delete-window win)))))))
1409 (defun quail-update-guidance ()
1410 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1411 ;; Update guidance buffer.
1412 (if (or (null input-method-tersely-flag)
1413 (not (eq (selected-window) (minibuffer-window))))
1414 (let ((guidance (quail-guidance)))
1415 (cond ((eq guidance t)
1416 ;; Show the current possible translations.
1417 (quail-show-translations))
1419 ;; Show the current input keys.
1420 (let ((key quail-current-key))
1422 (set-buffer quail-guidance-buf)
1426 ;; Show alternative characters specified in this alist.
1427 (let* ((key quail-current-key)
1432 (set-buffer quail-guidance-buf)
1435 (setq ch (aref key i))
1436 (setq alternative (cdr (assoc ch guidance)))
1437 (insert (or alternative ch))
1438 (setq i (1+ i)))))))))
1440 ;; Update completion buffer if displayed now. We highlight the
1441 ;; selected candidate string in *Completion* buffer if any.
1442 (let ((win (get-buffer-window quail-completion-buf))
1446 (setq str (if (stringp quail-current-str)
1448 (if (numberp quail-current-str)
1449 (char-to-string quail-current-str)))
1450 key quail-current-key)
1451 (set-buffer quail-completion-buf)
1452 (goto-char (point-min))
1453 (if (null (search-forward (concat " " key ":") nil t))
1454 (delete-overlay quail-overlay)
1456 (if (and str (search-forward (concat "." str) nil t))
1457 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
1458 (move-overlay quail-overlay (match-beginning 0) (point)))
1459 ;; Now POS points end of KEY and (point) points end of STR.
1460 (if (pos-visible-in-window-p (point) win)
1461 ;; STR is already visible.
1463 ;; We want to make both KEY and STR visible, but if the
1464 ;; window is too short, make at least STR visible.
1465 (setq pos (progn (point) (goto-char pos)))
1467 (set-window-start win (point))
1468 (if (not (pos-visible-in-window-p pos win))
1469 (set-window-start win pos))
1472 (defun quail-show-translations ()
1473 "Show the current possible translations."
1474 (let* ((key quail-current-key)
1475 (map (quail-lookup-key quail-current-key)))
1476 (if quail-current-translations
1477 (quail-update-current-translations))
1479 (set-buffer quail-guidance-buf)
1482 ;; Show the current key.
1485 ;; Show followable keys.
1487 (let ((l (cdr map)))
1490 (insert (car (car l)))
1494 ;; Show list of translations.
1495 (if quail-current-translations
1496 (let* ((indices (car quail-current-translations))
1498 (start (nth 1 indices))
1499 (end (nth 2 indices))
1501 (indent-to (- quail-guidance-translations-starting-column 7))
1502 (insert (format "(%02d/"(nth 3 indices))
1504 (format "%02d)" (nth 4 indices))
1507 (insert (format " %d." (if (= (- idx start) 9) 0
1508 (1+ (- idx start)))))
1509 (let ((pos (point)))
1510 (insert (aref (cdr quail-current-translations) idx))
1512 (move-overlay quail-overlay pos (point))))
1513 (setq idx (1+ idx)))))
1516 (defun quail-completion ()
1517 "List all completions for the current key.
1518 All possible translations of the current key and whole possible longer keys
1521 (let ((key quail-current-key)
1522 (map (quail-lookup-key quail-current-key)))
1524 (set-buffer quail-completion-buf)
1526 (insert "Possible completion and corresponding translations are:\n")
1527 (quail-completion-1 key map 1)
1528 (goto-char (point-min))
1529 (display-buffer (current-buffer)))
1530 (quail-update-guidance)))
1532 ;; List all completions of KEY in MAP with indentation INDENT.
1533 (defun quail-completion-1 (key map indent)
1534 (let ((len (length key)))
1537 (if (and (symbolp map) (fboundp map))
1538 (setq map (funcall map key len)))
1540 (quail-completion-list-translations map key (+ indent len 1))
1542 (setq indent (+ indent 2))
1545 (newkey (make-string (1+ len) 0))
1547 ;; Set KEY in the first LEN characters of NEWKEY.
1549 (aset newkey i (aref key i))
1551 (while l ; L = ((CHAR . DEFN) ....) ;
1552 (aset newkey len (car (car l)))
1553 (quail-completion-1 newkey (cdr (car l)) indent)
1554 (setq l (cdr l)))))))
1556 ;; List all possible translations of KEY in Quail map MAP with
1557 ;; indentation INDENT.
1558 (defun quail-completion-list-translations (map key indent)
1560 (quail-get-translation (car map) key (length key))))
1561 (if (integerp translations)
1562 (insert "(1/1) 1." translations "\n")
1563 ;; We need only vector part.
1564 (setq translations (cdr translations))
1565 ;; Insert every 10 elements with indices in a line.
1566 (let ((len (length translations))
1579 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))))
1580 ;; We show the last digit of FROM while converting
1581 ;; 0,1,..,9 to 1,2,..,0.
1582 (insert (format " %d." (if (= (% i 10) 9) 0 (1+ (% i 10)))))
1583 (insert (aref translations i))
1587 (defun quail-help ()
1588 "Show brief description of the current Quail package."
1590 (let ((package quail-current-package))
1591 (with-output-to-temp-buffer "*Quail-Help*"
1593 (set-buffer standard-output)
1594 (let ((quail-current-package package))
1595 (insert "Quail input method (name:"
1597 ", mode line indicator:["
1599 "])\n---- Documentation ----\n"
1602 (if (quail-show-layout) (quail-show-kbd-layout))
1603 (quail-help-insert-keymap-description
1605 "---- Key bindings (before starting translation) ----
1608 (quail-help-insert-keymap-description
1609 (quail-translation-keymap)
1610 "--- Key bindings (while translating) ---
1613 (if (quail-conversion-keymap)
1614 (quail-help-insert-keymap-description
1615 (quail-conversion-keymap)
1616 "--- Key bindings (while converting) ---
1621 (defun quail-help-insert-keymap-description (keymap &optional header)
1626 (save-window-excursion
1627 (let ((overriding-local-map keymap))
1628 (describe-bindings))
1629 (set-buffer "*Help*")
1630 (goto-char (point-min))
1633 (search-forward "Global Bindings:" nil 'move)
1636 (insert-buffer-substring "*Help*" from to)))
1638 (defun quail-show-kbd-layout ()
1639 "Show keyboard layout with key tops of multilingual characters."
1640 (insert "--- Keyboard layout ---\n")
1642 (while (< i quail-keyboard-layout-len)
1646 (indent-to (/ i 30)))
1649 (setq ch (aref quail-keyboard-layout i))
1652 (let* ((map (cdr (assq ch (cdr (quail-map)))))
1653 (translation (and map (quail-get-translation
1654 (car map) (char-to-string ch) 1))))
1655 (if (integerp translation)
1656 (insert translation)
1657 (if (consp translation)
1658 (insert (aref (cdr translation) (car translation)))
1663 (defun quail-translation-help ()
1664 "Show help message while translating in Quail mode."
1666 (let ((package quail-current-package)
1667 (current-key quail-current-key))
1668 (with-output-to-temp-buffer "*Quail-Help*"
1670 (set-buffer standard-output)
1671 (let ((quail-current-package package))
1672 (princ "You are translating the key sequence ")
1673 (prin1 quail-current-key)
1674 (princ" in Quail mode.\n")
1675 (quail-help-insert-keymap-description
1676 (quail-translation-keymap)
1677 "-----------------------
1682 (defun quail-conversion-help ()
1683 "Show help message while converting in Quail mode."
1685 (let ((package quail-current-package)
1686 (str (buffer-substring (overlay-start quail-conv-overlay)
1687 (overlay-end quail-conv-overlay))))
1688 (with-output-to-temp-buffer "*Quail-Help*"
1690 (set-buffer standard-output)
1691 (let ((quail-current-package package))
1692 (princ "You are converting the string ")
1694 (princ " in Quail mode.\n")
1695 (quail-help-insert-keymap-description
1696 (quail-conversion-keymap)
1697 "-----------------------
1703 (defvar quail-directory-name "quail"
1704 "Name of Quail directory which cotains Quail packages.
1705 This is a sub-directory of LEIM directory.")
1708 (defun quail-update-leim-list-file (dirname)
1709 "Update entries for Quail packages in LEIM list file of directory DIRNAME.
1710 LEIM is a library of Emacs input method."
1711 (interactive "FDirectory of LEIM: ")
1712 (setq dirname (file-name-as-directory (expand-file-name dirname)))
1713 (let ((quail-dir (concat dirname quail-directory-name))
1714 (filename (concat dirname leim-list-file-name))
1715 list-buf pkg-list pkg-buf pos)
1716 (if (not (file-exists-p quail-dir))
1718 (if (not (file-readable-p quail-dir))
1719 (message "Can't write to file \"%s\"" filename)
1720 (if (not (file-writable-p filename))
1721 (message "Can't write to file \"%s\"" filename)
1722 (setq list-buf (find-file-noselect filename))
1723 (setq pkg-list (directory-files quail-dir 'full ".*\\.el$" 'nosort))
1724 (message "Updating %s ..." filename)
1726 ;; At first, clean up the file.
1728 (set-buffer list-buf)
1731 ;; Insert the correct header.
1732 (if (looking-at (regexp-quote leim-list-header))
1733 (goto-char (match-end 0))
1734 (insert leim-list-header))
1736 (if (not (re-search-forward leim-list-entry-regexp nil t))
1739 ;; Remove garbages after the header.
1740 (goto-char (match-beginning 0))
1742 (delete-region pos (point)))
1744 ;; Remove all entries for Quail.
1745 (while (re-search-forward leim-list-entry-regexp nil 'move)
1746 (goto-char (match-beginning 0))
1748 (let ((form (read list-buf)))
1749 (if (equal (nth 3 form) ''quail-use-package)
1751 (if (eolp) (forward-line 1))
1752 (delete-region pos (point))))))))
1754 ;; Insert entries for Quail.
1756 (message "Checking %s ..." (car pkg-list))
1757 (setq pkg-buf (find-file-noselect (car pkg-list)))
1759 (set-buffer pkg-buf)
1760 (while (search-forward "(quail-define-package" nil t)
1761 (goto-char (match-beginning 0))
1762 (let ((form (read (current-buffer))))
1764 (set-buffer list-buf)
1765 (insert (format "(register-input-method
1768 %S)\n" (nth 1 form) ; PACKAGE-NAME
1769 (nth 2 form) ; LANGUAGE
1770 'quail-use-package ; ACTIVATE-FUNC
1771 (nth 3 form) ; PACKAGE-TITLE
1772 (progn ; PACKAGE-DESCRIPTION (one line)
1773 (string-match ".*" (nth 5 form))
1774 (match-string 0 (nth 5 form)))
1775 (file-relative-name ; PACKAGE-FILENAME
1776 (file-name-sans-extension (car pkg-list)) dirname)
1778 (kill-buffer pkg-buf)
1779 (setq pkg-list (cdr pkg-list)))
1781 (set-buffer list-buf)
1782 (setq buffer-file-coding-system 'iso-2022-7bit)
1784 (kill-buffer list-buf)
1785 (message "Updating %s ... done" (buffer-file-name list-buf)))))))
1789 ;;; quail.el ends here