comparison lisp/international/quail.el @ 17052:d0d7b244b1d0

Initial revision
author Karl Heuer <kwzh@gnu.org>
date Thu, 20 Feb 1997 07:02:49 +0000
parents
children 70194012fb3a
comparison
equal deleted inserted replaced
17051:fd0b17a79b07 17052:d0d7b244b1d0
1 ;;; quail.el -- provides simple input method for multilingual text
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
5
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
10
11 ;; This file is part of GNU Emacs.
12
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)
16 ;; any later version.
17
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.
22
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
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 ;;; Commentary:
28
29 ;; In Quail minor mode, you can input multilingual text easily. By
30 ;; defining a translation table (named Quail map) which maps ASCII key
31 ;; string to multilingual character or string, you can input any text
32 ;; from ASCII keyboard.
33 ;;
34 ;; We use words "translation" and "conversion" differently. The
35 ;; former is done by Quail package itself, the latter is the further
36 ;; process of converting a translated text to some more desirable
37 ;; text. For instance, Quail package for Japanese (`quail-jp')
38 ;; translates Roman text (transliteration of Japanese in Latin
39 ;; alphabets) to Hiragana text, which is then converted to
40 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
41 ;; CONVERSION-KEYS argument of the Quail package.
42
43 ;;; Code:
44
45 (require 'faces)
46
47 ;; Buffer local variables
48
49 (defvar quail-current-package nil
50 "The current Quail package to input multilingual text in Quail minor mode.
51 See the documentation of `quail-package-alist' for the format.")
52 (make-variable-buffer-local 'quail-current-package)
53 (put 'quail-current-package 'permanent-local t)
54
55 ;; Quail uses the following two buffers to assist users.
56 ;; A buffer to show available key sequence or translation list.
57 (defvar quail-guidance-buf nil)
58 ;; A buffer to show completion list of the current key sequence.
59 (defvar quail-completion-buf nil)
60
61 (defvar quail-mode nil
62 "Non-nil if in Quail minor mode.")
63 (make-variable-buffer-local 'quail-mode)
64 (put 'quail-mode 'permanent-local t)
65
66 (defvar quail-overlay nil
67 "Overlay which covers the current translation region of Quail.")
68 (make-variable-buffer-local 'quail-overlay)
69
70 (defvar quail-conv-overlay nil
71 "Overlay which covers the text to be converted in Quail mode.")
72 (make-variable-buffer-local 'quail-conv-overlay)
73
74 (defvar quail-current-key nil
75 "Current key for translation in Quail mode.")
76
77 (defvar quail-current-str nil
78 "Currently selected translation of the current key.")
79
80 (defvar quail-current-translations nil
81 "Cons of indices and vector of possible translations of the current key.")
82
83 ;; A flag to control conversion region. Normally nil, but if set to
84 ;; t, it means we must start the new conversion region if new key to
85 ;; be translated is input.
86 (defvar quail-reset-conversion-region nil)
87
88 ;; Quail package handlers.
89
90 (defvar quail-package-alist nil
91 "List of Quail packages.
92 A Quail package is a list of these elements:
93 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
94 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
95 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
96 CONVERSION-KEYS.
97
98 QUAIL-MAP is a data structure to map key strings to translations. For
99 the format, see the documentation of `quail-map-p'.
100
101 DECODE-MAP is an alist of translations and corresponding keys.
102
103 See the documentation of `quail-define-package' for the other elements.")
104
105 ;; Return various slots in the current quail-package.
106
107 (defsubst quail-name ()
108 "Return the name of the current Quail package."
109 (nth 0 quail-current-package))
110 (defsubst quail-title ()
111 "Return the title of the current Quail package."
112 (nth 1 quail-current-package))
113 (defsubst quail-map ()
114 "Return the translation map of the current Quail package."
115 (nth 2 quail-current-package))
116 (defsubst quail-guidance ()
117 "Return an object used for `guidance' feature of the current Quail package.
118 See also the documentation of `quail-define-package'."
119 (nth 3 quail-current-package))
120 (defsubst quail-docstring ()
121 "Return the documentation string of the current Quail package."
122 (nth 4 quail-current-package))
123 (defsubst quail-translation-keymap ()
124 "Return translation keymap in the current Quail package.
125 Translation keymap is a keymap used while translation region is active."
126 (nth 5 quail-current-package))
127 (defsubst quail-forget-last-selection ()
128 "Return `forget-last-selection' flag of the current Quail package.
129 See also the documentation of `quail-define-package'."
130 (nth 6 quail-current-package))
131 (defsubst quail-deterministic ()
132 "Return `deterministic' flag of the current Quail package.
133 See also the documentation of `quail-define-package'."
134 (nth 7 quail-current-package))
135 (defsubst quail-kbd-translate ()
136 "Return `kbd-translate' flag of the current Quail package.
137 See also the documentation of `quail-define-package'."
138 (nth 8 quail-current-package))
139 (defsubst quail-show-layout ()
140 "Return `show-layout' flag of the current Quail package.
141 See also the documentation of `quail-define-package'."
142 (nth 9 quail-current-package))
143 (defsubst quail-decode-map ()
144 "Return decode map of the current Quail package.
145 It is an alist of translations and corresponding keys."
146 (nth 10 quail-current-package))
147 (defsubst quail-maximum-shortest ()
148 "Return `maximum-shortest' flag of the current Quail package.
149 See also the documentation of `quail-define-package'."
150 (nth 11 quail-current-package))
151 (defsubst quail-overlay-plist ()
152 "Return property list of an overly used in the current Quail package."
153 (nth 12 quail-current-package))
154 (defsubst quail-update-translation-function ()
155 "Return a function for updating translation in the current Quail package."
156 (nth 13 quail-current-package))
157 (defsubst quail-conversion-keymap ()
158 "Return conversion keymap in the current Quail package.
159 Conversion keymap is a keymap used while conversion region is active
160 but translation region is not active."
161 (nth 14 quail-current-package))
162
163 (defsubst quail-package (name)
164 "Return Quail package named NAME."
165 (assoc name quail-package-alist))
166
167 (defun quail-add-package (package)
168 "Add Quail package PACKAGE to `quail-package-alist'."
169 (let ((pac (quail-package (car package))))
170 (if pac
171 (setcdr pac (cdr package))
172 (setq quail-package-alist (cons package quail-package-alist)))))
173
174 (defun quail-select-package (name)
175 "Select Quail package named NAME as the current Quail package."
176 (let ((package (quail-package name)))
177 (if (null package)
178 (error "No Quail package `%s'" name))
179 (setq quail-current-package package)
180 (setq-default quail-current-package package)
181 name))
182
183 ;;;###autoload
184 (defun quail-use-package (package-name &rest libraries)
185 "Start using Quail package PACKAGE-NAME.
186 The remaining arguments are libraries to be loaded before using the package."
187 (while libraries
188 (if (not (load (car libraries) t))
189 (progn
190 (with-output-to-temp-buffer "*Help*"
191 (princ "Quail package \"")
192 (princ package-name)
193 (princ "\" can't be activated\n because library \"")
194 (princ (car libraries))
195 (princ "\" is not in `load-path'.
196
197 The most common case is that you have not yet installed appropriate
198 libraries in LEIM (Libraries of Emacs Input Method) which is
199 distributed separately from Emacs.
200
201 Installation of LEIM for Quail is very simple, just copy Quail
202 packages (byte-compiled Emacs Lisp files) to somewhere in your
203 `load-path'.
204
205 LEIM is available from the same ftp directory as Emacs."))
206 (error ""))
207 (setq libraries (cdr libraries))))
208 (quail-select-package package-name)
209 (setq current-input-method-title (quail-title))
210 (quail-mode 1))
211
212 (defun quail-inactivate ()
213 "Turn off Quail input method."
214 (interactive)
215 (throw 'quail-tag t))
216
217 (or (assq 'quail-mode minor-mode-alist)
218 (setq minor-mode-alist
219 (cons '(quail-mode " Quail") minor-mode-alist)))
220
221 (defvar quail-mode-map
222 (let ((map (make-keymap))
223 (i ? ))
224 (while (< i 127)
225 (define-key map (char-to-string i) 'quail-start-translation)
226 (setq i (1+ i)))
227 map)
228 "Keymap for Quail mode.")
229
230 (or (assq 'quail-mode minor-mode-map-alist)
231 (setq minor-mode-map-alist
232 (cons (cons 'quail-mode quail-mode-map) minor-mode-map-alist)))
233
234 (defvar quail-translation-keymap
235 (let ((map (make-keymap))
236 (i 0))
237 (while (< i ?\ )
238 (define-key map (char-to-string i) 'quail-execute-non-quail-command)
239 (setq i (1+ i)))
240 (while (< i 127)
241 (define-key map (char-to-string i) 'quail-self-insert-command)
242 (setq i (1+ i)))
243 (define-key map "\177" 'quail-delete-last-char)
244 (define-key map "\C-\\" 'quail-inactivate)
245 (define-key map "\C-f" 'quail-next-translation)
246 (define-key map "\C-b" 'quail-prev-translation)
247 (define-key map "\C-n" 'quail-next-translation-block)
248 (define-key map "\C-p" 'quail-prev-translation-block)
249 (define-key map "\C-i" 'quail-completion)
250 (define-key map "\C-@" 'quail-select-current)
251 (define-key map "\C-c" 'quail-abort-translation)
252 (define-key map "\C-h" 'quail-translation-help)
253 (define-key map [tab] 'quail-completion)
254 (define-key map [delete] 'quail-delete-last-char)
255 (define-key map [backspace] 'quail-delete-last-char)
256 ;; At last, define default key binding.
257 (append map '((t . quail-execute-non-quail-command))))
258 "Keymap used processing translation in Quail mode.
259 This map is activated while translation region is active.")
260
261 (defvar quail-conversion-keymap
262 (let ((map (make-keymap))
263 (i 0))
264 (while (< i ?\ )
265 (define-key map (char-to-string i) 'quail-execute-non-quail-command)
266 (setq i (1+ i)))
267 (while (< i 127)
268 (define-key map (char-to-string i)
269 'quail-start-translation-in-conversion-mode)
270 (setq i (1+ i)))
271 (define-key map "\C-b" 'quail-conversion-backward-char)
272 (define-key map "\C-f" 'quail-conversion-forward-char)
273 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
274 (define-key map "\C-e" 'quail-conversion-end-of-region)
275 (define-key map "\C-d" 'quail-conversion-delete-char)
276 (define-key map "\C-h" 'quail-conversion-help)
277 (define-key map "\C-\\" 'quail-inactivate)
278 (define-key map "\177" 'quail-conversion-backward-delete-char)
279 (define-key map [delete] 'quail-conversion-backward-delete-char)
280 (define-key map [backspace] 'quail-conversion-backward-delete-char)
281 ;; At last, define default key binding.
282 (append map '((t . quail-execute-non-quail-command))))
283 "Keymap used for processing conversion in Quail mode.
284 This map is activated while convesion region is active but translation
285 region is not active.")
286
287 (defun quail-define-package (name language title
288 &optional guidance docstring translation-keys
289 forget-last-selection deterministic
290 kbd-translate show-layout create-decode-map
291 maximum-shortest overlay-plist
292 update-translation-function
293 conversion-keys)
294 "Define NAME as a new Quail package for input LANGUAGE.
295 TITLE is a string to be displayed at mode-line to indicate this package.
296 Optional arguments are GUIDANCE, DOCSTRING, TRANLSATION-KEYS,
297 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
298 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
299 UPDATE-TRANSLATION-FUNCTION, and CONVERSION-KEYS.
300
301 GUIDANCE specifies how a guidance string is shown in echo area.
302 If it is t, list of all possible translations for the current key is shown
303 with the currently selected translation being highlighted.
304 If it is an alist, the element has the form (CHAR . STRING). Each character
305 in the current key is searched in the list and the corresponding string is
306 shown.
307 If it is nil, the current key is shown.
308
309 DOCSTRING is the documentation string of this package.
310
311 TRANSLATION-KEYS specifies additional key bindings used while translation
312 region is active. It is an alist of single key character vs. corresponding
313 command to be called.
314
315 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
316 for the future to translate the same key. If this flag is nil, a
317 translation selected for a key is remembered so that it can be the
318 first candidate when the same key is entered later.
319
320 DETERMINISTIC non-nil means the first candidate of translation is
321 selected automatically without allowing users to select another
322 translation for a key. In this case, unselected translations are of
323 no use for an interactive use of Quail but can be used by some other
324 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
325 to t.
326
327 KBD-TRANSLATE non-nil means input characters are translated from a
328 user's keyboard layout to the standard keyboard layout. See the
329 documentation of `quail-keyboard-layout' and
330 `quail-keyboard-layout-standard' for more detail.
331
332 SHOW-LAYOUT non-nil means the `quail-help' command should show
333 the user's keyboard layout visually with translated characters.
334 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
335 this package defines no translations for single character keys.
336
337 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
338 map is an alist of translations and corresponding original keys.
339 Although this map is not used by Quail itself, it can be used by some
340 other programs. For instance, Vietnamese supporting needs this map to
341 convert Vietnamese text to VIQR format which uses only ASCII
342 characters to represent Vietnamese characters.
343
344 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
345 length of the shortest sequence. When we don't have a translation of
346 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
347 the key at \"..AB\" and start translation of \"CD..\". Hangul
348 packages, for instance, use this facility. If this flag is nil, we
349 break the key just at \"..ABC\" and start translation of \"D..\".
350
351 OVERLAY-PLIST if non-nil is a property list put on an overlay which
352 covers Quail translation region.
353
354 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
355 the current translation region accoding to a new translation data. By
356 default, a tranlated text or a user's key sequence (if no transltion
357 for it) is inserted.
358
359 CONVERSION-KEYS specifies additional key bindings used while
360 conversion region is active. It is an alist of single key character
361 vs. corresponding command to be called."
362 (let (translation-keymap conversion-keymap)
363 (if deterministic (setq forget-last-selection t))
364 (if translation-keys
365 (progn
366 (setq translation-keymap (copy-keymap quail-translation-keymap))
367 (while translation-keys
368 (define-key translation-keymap
369 (car (car translation-keys)) (cdr (car translation-keys)))
370 (setq translation-keys (cdr translation-keys))))
371 (setq translation-keymap quail-translation-keymap))
372 (if conversion-keys
373 (progn
374 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
375 (while conversion-keys
376 (define-key conversion-keymap
377 (car (car conversion-keys)) (cdr (car conversion-keys)))
378 (setq conversion-keys (cdr conversion-keys)))))
379 (quail-add-package
380 (list name title (list nil) guidance (or docstring "")
381 translation-keymap
382 forget-last-selection deterministic kbd-translate show-layout
383 (if create-decode-map (list 'decode-map) nil)
384 maximum-shortest overlay-plist update-translation-function
385 conversion-keymap)))
386 (register-input-method language (list name 'quail-use-package))
387 (quail-select-package name))
388
389 ;; Quail minor mode handlers.
390
391 ;; Setup overlays used in Quail mode.
392 (defun quail-setup-overlays ()
393 (let ((pos (point)))
394 (if (overlayp quail-overlay)
395 (move-overlay quail-overlay pos pos)
396 (setq quail-overlay (make-overlay pos pos nil nil t))
397 (overlay-put quail-overlay 'face 'underline)
398 (let ((l (quail-overlay-plist)))
399 (while l
400 (overlay-put quail-overlay (car l) (car (cdr l)))
401 (setq l (cdr (cdr l))))))
402 (if (overlayp quail-conv-overlay)
403 (move-overlay quail-conv-overlay pos pos)
404 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
405 (overlay-put quail-conv-overlay 'face 'underline)
406 ;;(overlay-put quail-conv-overlay 'modification-hooks
407 ;;'(quail-conv-overlay-modification-hook))
408 )))
409
410 ;; Delete overlays used in Quail mode.
411 (defun quail-delete-overlays ()
412 (if (overlayp quail-overlay)
413 (delete-overlay quail-overlay))
414 (if (overlayp quail-conv-overlay)
415 (delete-overlay quail-conv-overlay)))
416
417 ;; While translating and converting, we enter the recursive edit and
418 ;; exit it frequently, which results in frequent and annoying change
419 ;; of and annoying in mode line. To avoid it, we use a modified
420 ;; mode-line-format.
421 (defvar quail-mode-line-format nil)
422
423 ;; Return a modified mode-line-format which doesn't show the recursive
424 ;; editing level. But, we only pay attention to the top level
425 ;; elements of the current mode-line-format.
426 (defun quail-generate-mode-line-format ()
427 (if (listp mode-line-format)
428 (let ((new (copy-sequence mode-line-format))
429 l elt idx)
430 (setq l new)
431 (while l
432 (setq elt (car l))
433 (if (and (stringp elt)
434 (or (setq idx (string-match "%\\[" elt))
435 (setq idx (string-match "%\\]" elt))))
436 (setcar l (concat (substring elt 0 idx)
437 (substring elt (+ idx 2)))))
438 (setq l (cdr l)))
439 new)
440 mode-line-format))
441
442 (defun quail-mode (&optional arg)
443 "Toggle Quail minor mode.
444 With arg, turn Quail mode on if and only if arg is positive.
445 Try \\[describe-bindings] in Quail mode to see the available key binding.
446 The command \\[describe-input-method] describes the current Quail package."
447 (interactive "P")
448 (setq quail-mode
449 (if (null arg) (null quail-mode)
450 (> (prefix-numeric-value arg) 0)))
451 (if (null quail-mode)
452 ;; Let's turn off Quail mode.
453 (progn
454 (quail-hide-guidance-buf)
455 (quail-delete-overlays)
456 (setq describe-current-input-method-function nil)
457 (setq current-input-method nil)
458 (run-hooks 'quail-mode-exit-hook)
459 (run-hooks 'input-method-inactivate-hook))
460 ;; Let's turn on Quail mode.
461 (if (null quail-current-package)
462 ;; Quail package is not yet selected. Select one now.
463 (let (name)
464 (if quail-package-alist
465 (setq name (car (car quail-package-alist)))
466 (setq quail-mode nil)
467 (error "No Quail package loaded"))
468 (quail-select-package name)))
469 (setq inactivate-current-input-method-function 'quail-mode)
470 (setq describe-current-input-method-function 'quail-help)
471 (setq quail-mode-line-format (quail-generate-mode-line-format))
472 (quail-delete-overlays)
473 (quail-show-guidance-buf)
474 ;; If we are in minibuffer, turn off Quail mode before exiting.
475 (if (eq (selected-window) (minibuffer-window))
476 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
477 (make-local-hook 'post-command-hook)
478 (run-hooks 'quail-mode-hook)
479 (run-hooks 'input-method-activate-hook))
480 (force-mode-line-update))
481
482 (defun quail-exit-from-minibuffer ()
483 (if quail-mode (quail-mode -1))
484 (if (<= (minibuffer-depth) 1)
485 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
486
487 (defvar quail-saved-overriding-local-map nil)
488 (defvar quail-saved-current-buffer nil)
489
490 ;; Toggle `quail-mode'. This function is added to `post-command-hook'
491 ;; in Quail mode, to turn Quail mode temporarily off, or back on
492 ;; after one non-Quail command.
493 (defun quail-toggle-mode-temporarily ()
494 (if quail-mode
495 ;; We are going to handle following events out of Quail mode.
496 (setq quail-mode nil
497 quail-saved-overriding-local-map overriding-local-map
498 quail-saved-current-buffer (current-buffer)
499 overriding-local-map nil)
500 ;; We have just executed one non-Quail command. We don't need
501 ;; this hook any more.
502 (remove-hook 'post-command-hook 'quail-toggle-mode-temporarily t)
503 ;; If the command changed the current buffer, we should not go
504 ;; back to Quail mode.
505 (if (not (eq (current-buffer) quail-saved-current-buffer))
506 (throw 'quail-tag nil)
507 ;; Let's go back to Quail mode.
508 (setq quail-mode t)
509 (setq overriding-local-map quail-saved-overriding-local-map)
510 ;; If whole text in conversion area was deleted, exit from the
511 ;; recursive edit.
512 (let ((start (overlay-start quail-conv-overlay)))
513 (if (and start (= start (overlay-end quail-conv-overlay)))
514 (throw 'quail-tag nil)))
515 )))
516
517 (defun quail-execute-non-quail-command ()
518 "Execute one non-Quail command in Quail mode.
519 The current translation and conversion are terminated."
520 (interactive)
521 (setq unread-command-events (cons last-input-event unread-command-events))
522 (quail-delete-overlays)
523 (if (buffer-live-p quail-guidance-buf)
524 (save-excursion
525 (set-buffer quail-guidance-buf)
526 (erase-buffer)))
527 (throw 'quail-tag nil))
528
529 ;; Keyboard layout translation handlers.
530
531 ;; Some Quail packages provide localized keyboard simulation which
532 ;; requires a particular keyboard layout. In this case, what we need
533 ;; is locations of keys the user entered, not character codes
534 ;; generated by those keys. However, for the moment, there's no
535 ;; common way to get such information. So, we ask a user to give
536 ;; information of his own keyboard layout, then translate it to the
537 ;; standard layout which we defined so that all Quail packages depend
538 ;; just on it.
539
540 (defconst quail-keyboard-layout-standard
541 "\
542 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
543 qQwWeErRtTyYuUiIoOpP[{]} \
544 aAsSdDfFgGhHjJkKlL;:'\"\\| \
545 zZxXcCvVbBnNmM,<.>/? "
546 "Standard keyboard layout of printable characters Quail assumes.
547 See the documentation of `quail-keyboard-layout' for this format.
548 This layout is almost the same as that of VT100,
549 but the location of key \\ (backslash) is just right of key ' (single-quote),
550 not right of RETURN key.")
551
552 (defvar quail-keyboard-layout quail-keyboard-layout-standard
553 "A string which represents physical key layout of a particular keyboard.
554 We assume there are four rows and each row has 15 keys (columns),
555 the first column of the first row is left of key '1',
556 the first column of the second row is left of key `q',
557 the first column of the third row is left of key `a',
558 the first column of the fourth row is left of key `z'.
559 Nth (N is even) and (N+1)th characters in the string are non-shifted
560 and shifted characters respectively at the same location.
561 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).")
562
563 (defconst quail-keyboard-layout-len 120)
564
565 ;; Here we provide several examples of famous keyboard layouts.
566
567 (defvar quail-keyboard-layout-alist
568 (list
569 '("sun-type3" . "\
570 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
571 qQwWeErRtTyYuUiIoOpP[{]} \
572 aAsSdDfFgGhHjJkKlL;:'\" \
573 zZxXcCvVbBnNmM,<.>/? ")
574 (cons "standard" quail-keyboard-layout-standard))
575 "Alist of keyboard names and corresponding layout strings.
576 See the documentation of `quail-keyboard-layout' for the format of
577 the layout string.")
578
579 (defun quail-set-keyboard-layout (kbd-type)
580 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
581
582 Since some Quail packages depends on a physical layout of keys (not
583 characters generated by them), those are created by assuming the
584 standard layout defined in `quail-keyboard-layout-standard'. This
585 function tells Quail system the layout of your keyboard so that what
586 you type is correctly handled."
587 (interactive
588 (let* ((completing-ignore-case t)
589 (type (completing-read "Keyboard type: "
590 quail-keyboard-layout-alist)))
591 (list type)))
592 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
593 (if (null layout)
594 ;; Here, we had better ask a user to define his own keyboard
595 ;; layout interactively.
596 (error "Unknown keyboard type `%s'" kbd-type))
597 (setq quail-keyboard-layout (cdr layout))))
598
599 (defun quail-keyboard-translate (ch)
600 "Translate CHAR according to `quail-keyboard-layout' and return the result."
601 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
602 ch
603 (let ((i 0))
604 (while (and (< i quail-keyboard-layout-len)
605 (/= ch (aref quail-keyboard-layout i)))
606 (setq i (1+ i)))
607 (if (= i quail-keyboard-layout-len)
608 (error "Character `%c' not found in your keyboard layout" ch))
609 (aref quail-keyboard-layout-standard i))))
610
611 ;; Quail map
612
613 (defsubst quail-map-p (object)
614 "Return t if OBJECT is a Quail map.
615
616 A Quail map holds information how a particular key should be translated.
617 Its format is (TRANSLATION . ALIST).
618 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
619 In the latter case, each element of VECTOR is a candidate for the translation,
620 and INDEX points the currently selected translation.
621
622 ALIST is normally a list of elements that look like (CHAR . DEFN),
623 where DEFN is another Quail map for a longer key (CHAR added to the
624 current key). It may also be a symbol of a function which returns an
625 alist of the above format.
626
627 Just after a Quail package is read, TRANSLATION may be a string or a
628 vector. Then each element of the string or vector is a candidate for
629 the translation. These objects are transformed to cons cells in the
630 format \(INDEX . VECTOR), as described above."
631 (and (consp object)
632 (let ((translation (car object)))
633 (or (integerp translation) (consp translation) (null translation)
634 (vectorp translation) (stringp translation)
635 (symbolp translation)))
636 (let ((alist (cdr object)))
637 (or (listp alist) (symbolp alist)))))
638
639 (defmacro quail-define-rules (&rest rules)
640 "Define translation rules of the current Quail package.
641 Each argument is a list of KEY and TRANSLATION.
642 KEY is a string meaning a sequence of keystrokes to be translated.
643 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
644 It it is a character, it is the sole translation of KEY.
645 If it is a string, each character is a candidate for the translation.
646 If it is a vector, each element (string or character) is a candidate
647 for the translation.
648 In these cases, a key specific Quail map is generated and assigned to KEY.
649
650 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
651 it is used to handle KEY."
652 `(quail-install-map
653 ',(let ((l rules)
654 (map (list nil)))
655 (while l
656 (quail-defrule-internal (car (car l)) (car (cdr (car l))) map)
657 (setq l (cdr l)))
658 map)))
659
660 (defun quail-install-map (map)
661 "Install the Quail map MAP in the current Quail package.
662 The installed map can be referred by the function `quail-map'."
663 (if (null quail-current-package)
664 (error "No current Quail package"))
665 (if (null (quail-map-p map))
666 (error "Invalid Quail map `%s'" map))
667 (setcar (cdr (cdr quail-current-package)) map))
668
669 (defun quail-defrule (key translation &optional name)
670 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
671 KEY is a string meaning a sequence of keystrokes to be translated.
672 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
673 It it is a character, it is the sole translation of KEY.
674 If it is a string, each character is a candidate for the translation.
675 If it is a vector, each element (string or character) is a candidate
676 for the translation.
677 In these cases, a key specific Quail map is generated and assigned to KEY.
678
679 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
680 it is used to handle KEY.
681 Optional argument NAME, if specified, says which Quail package
682 to define this translation rule in. The default is to define it in the
683 current Quail package."
684 (if name
685 (let ((package (quail-package name)))
686 (if (null package)
687 (error "No Quail package `%s'" name))
688 (setq quail-current-package package)))
689 (quail-defrule-internal key translation (quail-map)))
690
691 ;; Define KEY as TRANS in a Quail map MAP.
692 (defun quail-defrule-internal (key trans map)
693 (if (null (stringp key))
694 "Invalid Quail key `%s'" key)
695 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
696 (symbolp trans)
697 (quail-map-p trans)))
698 (error "Invalid Quail translation `%s'" trans))
699 (if (null (quail-map-p map))
700 (error "Invalid Quail map `%s'" map))
701 (let ((len (length key))
702 (idx 0)
703 ch entry)
704 (while (< idx len)
705 (if (null (consp map))
706 ;; We come here, for example, when we try to define a rule
707 ;; for "ABC" but a rule for "AB" is already defined as a
708 ;; symbol.
709 (error "Quail key %s is too long" key))
710 (setq ch (aref key idx)
711 entry (assq ch (cdr map)))
712 (if (null entry)
713 (progn
714 (setq entry (cons ch (list nil)))
715 (setcdr map (cons entry (cdr map)))))
716 (setq map (cdr entry))
717 (setq idx (1+ idx)))
718 (if (symbolp trans)
719 (if (cdr map)
720 ;; We come here, for example, when we try to define a rule
721 ;; for "AB" as a symbol but a rule for "ABC" is already
722 ;; defined.
723 (error "Quail key %s is too short" key)
724 (setcdr entry trans))
725 (if (quail-map-p trans)
726 (if (not (listp (cdr map)))
727 ;; We come here, for example, when we try to define a rule
728 ;; for "AB" as a symbol but a rule for "ABC" is already
729 ;; defined.
730 (error "Quail key %s is too short" key)
731 (if (not (listp (cdr trans)))
732 (if (cdr map)
733 ;; We come here, for example, when we try to
734 ;; define a rule for "AB" as a symbol but a rule
735 ;; for "ABC" is already defined.
736 (error "Quail key %s is too short" key)
737 (setcdr entry trans))
738 (setcdr entry (append trans (cdr map)))))
739 (setcar map trans)))))
740
741 (defun quail-get-translation (map key len)
742 "Return the translation specified in Quail map MAP for KEY of length LEN.
743 The translation is either a character or a cons of the form (INDEX . VECTOR),
744 where VECTOR is a vector of candidates (character or string) for
745 the translation, and INDEX points into VECTOR to specify the currently
746 selected translation."
747 (let ((def (car map)))
748 (if (and def (symbolp def))
749 ;; DEF is a symbol of a function which returns valid translation.
750 (setq def (funcall def key len)))
751 (cond
752 ((or (integerp def) (consp def))
753 def)
754
755 ((null def)
756 ;; No translation.
757 nil)
758
759 ((stringp def)
760 ;; Each character in DEF is a candidate of translation. Reform
761 ;; it as (INDEX . VECTOR).
762 (setq def (string-to-vector def))
763 ;; But if the length is 1, we don't need vector but a single
764 ;; character as the translation.
765 (if (= (length def) 1)
766 (aref def 0)
767 (cons 0 def)))
768
769 ((vectorp def)
770 ;; Each element (string or character) in DEF is a candidate of
771 ;; translation. Reform it as (INDEX . VECTOR).
772 (cons 0 def))
773
774 (t
775 (error "Invalid object in Quail map: %s" def)))))
776
777 (defun quail-lookup-key (key len)
778 "Lookup KEY of length LEN in the current Quail map and return the definition.
779 The returned value is a Quail map specific to KEY."
780 (let ((idx 0)
781 (map (quail-map))
782 (kbd-translate (quail-kbd-translate))
783 slot ch translation)
784 (while (and map (< idx len))
785 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
786 (aref key idx)))
787 (setq idx (1+ idx))
788 (if (and (cdr map) (symbolp (cdr map)))
789 (setcdr map (funcall (cdr map) key idx)))
790 (setq slot (assq ch (cdr map)))
791 (if (and (cdr slot) (symbolp (cdr slot)))
792 (setcdr slot (funcall (cdr slot) key idx)))
793 (setq map (cdr slot)))
794 (if (and map (setq translation (quail-get-translation map key len)))
795 (progn
796 ;; We may have to reform car part of MAP.
797 (if (not (equal (car map) translation))
798 (setcar map translation))
799 (if (consp translation)
800 (progn
801 (setq quail-current-translations translation)
802 (if (quail-forget-last-selection)
803 (setcar quail-current-translations 0))))
804 ;; We may have to reform cdr part of MAP.
805 (if (and (cdr map) (symbolp (cdr map)))
806 (progn
807 (setcdr map (funcall (cdr map) key len))))
808 ))
809 map))
810
811 (defun quail-conv-overlay-modification-hook (overlay after &rest ignore)
812 (if (and after
813 (= (overlay-start overlay) (overlay-end overlay)))
814 ;; Whole text in conversion area was deleted. Let's exit from
815 ;; the recursive edit.
816 (throw 'exit nil)))
817
818 (defvar quail-suppress-conversion nil
819 "If non-nil, suppress converting facility of the current Quail package.")
820
821 ;; If set to non-nil, exit conversion mode before starting new translation.
822 (defvar quail-exit-conversion-mode nil)
823
824 (defun quail-start-translation ()
825 "Start translating the typed character in Quail mode."
826 (interactive "*")
827 (setq unread-command-events
828 (cons last-command-event unread-command-events))
829 ;; Check the possibility of translating the last key.
830 (if (assq last-command-event (cdr (quail-map)))
831 ;; Ok, we can start translation.
832 (let ((mode-line-format quail-mode-line-format))
833 (quail-setup-overlays)
834 (if (catch 'quail-tag
835 (if (and (not quail-suppress-conversion)
836 (quail-conversion-keymap))
837 ;; We must start translation in conversion mode.
838 (let ((overriding-local-map (quail-conversion-keymap)))
839 (setq quail-exit-conversion-mode nil)
840 (recursive-edit)
841 (if (and auto-fill-function
842 (> (current-column) (current-fill-column)))
843 (run-hooks 'auto-fill-function)))
844 (let ((overriding-local-map (quail-translation-keymap)))
845 (setq quail-current-key "")
846 (recursive-edit)))
847 (if (prog1 (< (overlay-start quail-conv-overlay)
848 (overlay-end quail-conv-overlay))
849 (delete-overlay quail-conv-overlay))
850 (run-hooks 'input-method-after-insert-chunk-hook))
851 nil)
852 ;; Someone has thrown a tag with value t, which means
853 ;; we should turn Quail mode off.
854 (quail-mode -1)))
855 ;; Since the typed character doesn't start any translation, handle
856 ;; it out of Quail mode. We come back to Quail mode later because
857 ;; function `quail-toggle-mode-temporarily' is in
858 ;; `post-command-hook'.
859 (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
860
861 (defsubst quail-point-in-conversion-region ()
862 "Return non-nil value if the point is in conversion region of Quail mode."
863 (let (start pos)
864 (and (setq start (overlay-start quail-conv-overlay))
865 (>= (setq pos (point)) start)
866 (<= pos (overlay-end quail-conv-overlay)))))
867
868 (defun quail-start-translation-in-conversion-mode ()
869 "Start translating the typed character in conversion mode of Quail mode."
870 (interactive "*")
871 (setq unread-command-events
872 (cons last-command-event unread-command-events))
873 (if (or quail-exit-conversion-mode
874 (not (quail-point-in-conversion-region)))
875 (progn
876 ;; We must start translation with new conversion region.
877 (setq quail-exit-conversion-mode nil)
878 (throw 'exit nil)))
879 ;; Check the possibility of translating the last key.
880 (if (assq last-command-event (cdr (quail-map)))
881 ;; Ok, we can start translation.
882 (let ((overriding-local-map (quail-translation-keymap)))
883 (setq quail-current-key "")
884 (move-overlay quail-overlay (point) (point))
885 (recursive-edit))
886 ;; Since the typed character doesn't start any translation, handle
887 ;; it out of Quail mode. We come back to Quail mode later because
888 ;; function `quail-toggle-mode-temporarily' is in
889 ;; `post-command-hook'.
890 (add-hook 'post-command-hook 'quail-toggle-mode-temporarily nil t)))
891
892 (defun quail-terminate-translation ()
893 "Terminate the translation of the current key."
894 (let ((start (overlay-start quail-overlay)))
895 (if (and start
896 (< start (overlay-end quail-overlay)))
897 ;; Here we simulate self-insert-command.
898 (let (last-command-char)
899 (goto-char start)
900 ;; The first one might want to expand an abbrev.
901 (setq last-command-char (following-char))
902 (delete-char 1)
903 (self-insert-command 1)
904 (if (< (point) (overlay-end quail-overlay))
905 (if overwrite-mode
906 (while (< (point) (overlay-end quail-overlay))
907 (setq last-command-char (following-char))
908 (delete-char 1)
909 (self-insert-command 1))
910 ;; The last one might still want to auto-fill.
911 (goto-char (overlay-end quail-overlay))
912 (let ((last-command-char (preceding-char)))
913 (delete-char -1)
914 (self-insert-command 1)))))))
915 (delete-overlay quail-overlay)
916 (if (buffer-live-p quail-guidance-buf)
917 (save-excursion
918 (set-buffer quail-guidance-buf)
919 (erase-buffer)))
920 (throw 'exit nil))
921
922 (defsubst quail-delete-region ()
923 "Delete the text in the current translation region of Quail."
924 (delete-region (overlay-start quail-overlay) (overlay-end quail-overlay)))
925
926 (defun quail-select-current ()
927 "Select the current text shown in Quail translation region."
928 (interactive)
929 (quail-terminate-translation))
930
931 ;; Update the current translation status according to CONTROL-FLAG.
932 ;; If CONTROL-FLAG is integer value, it is the number of keys in the
933 ;; head quail-current-key which can be translated. The remaining keys
934 ;; are put back to unread-command-events to be handled again.
935 ;; If CONTROL-FLAG is t, terminate the translation for the whole keys
936 ;; in quail-current-key.
937 ;; If CONTROL-FLAG is nil, proceed the translation with more keys.
938
939 (defun quail-update-translation (control-flag)
940 (quail-delete-region)
941 (let ((func (quail-update-translation-function)))
942 (if func
943 (funcall func control-flag)
944 (if (numberp control-flag)
945 (let ((len (length quail-current-key)))
946 (while (> len control-flag)
947 (setq len (1- len))
948 (setq unread-command-events
949 (cons (aref quail-current-key len)
950 unread-command-events)))
951 (insert (or quail-current-str
952 (substring quail-current-key 0 len))))
953 (insert (or quail-current-str quail-current-key)))))
954 (quail-update-guidance)
955 (if control-flag
956 (quail-terminate-translation)))
957
958 (defun quail-self-insert-command ()
959 "Add the typed character to the key for translation."
960 (interactive "*")
961 (setq quail-current-key
962 (concat quail-current-key (char-to-string last-command-event)))
963 (quail-update-translation (quail-translate-key)))
964
965 (defun quail-translate-key ()
966 "Translate the current key sequence according to the current Quail map.
967 Return t if we can terminate the translation.
968 Return nil if the current key sequence may be followed by more keys.
969 Return number if we can't find any translation for the current key
970 sequence. The number is the count of valid keys in the current
971 sequence counting from the head."
972 (let* ((len (length quail-current-key))
973 (map (quail-lookup-key quail-current-key len))
974 def ch)
975 (if map
976 (let ((def (car map)))
977 (setq quail-current-str
978 (if (consp def) (aref (cdr def) (car def)) def))
979 ;; Return t only if we can terminate the current translation.
980 (and
981 ;; No alternative translations.
982 (or (null (consp def)) (= (length (cdr def)) 1))
983 ;; No translation for the longer key.
984 (null (cdr map))
985 ;; No shorter breaking point.
986 (or (null (quail-maximum-shortest))
987 (< len 3)
988 (null (quail-lookup-key quail-current-key (1- len)))
989 (null (quail-lookup-key
990 (substring quail-current-key -2 -1) 1)))))
991
992 ;; There's no translation for the current key sequence. Before
993 ;; giving up, we must check two possibilities.
994 (cond ((and
995 (quail-maximum-shortest)
996 (>= len 4)
997 (setq def (car (quail-lookup-key quail-current-key (- len 2))))
998 (quail-lookup-key (substring quail-current-key -2) 2))
999 ;; Now the sequence is "...ABCD", which can be split into
1000 ;; "...AB" and "CD..." to get valid translation.
1001 ;; At first, get translation of "...AB".
1002 (setq quail-current-str
1003 (if (consp def) (aref (cdr def) (car def)) def))
1004 ;; Then, return the length of "...AB".
1005 (- len 2))
1006
1007 ((and quail-current-translations
1008 (not (quail-deterministic))
1009 (setq ch (aref quail-current-key (1- len)))
1010 (>= ch ?0) (<= ch ?9))
1011 ;; A numeric key is entered to select a desirable translation.
1012 (setq quail-current-key (substring quail-current-key 0 -1))
1013 (quail-select-translation
1014 (+ (* (/ (car quail-current-translations) 10) 10)
1015 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1016 (if (= ch ?0) 9 (- ch ?1))))
1017 ;; And, we can terminate the current translation.
1018 t)
1019
1020 (t
1021 ;; No way to handle the last character in this context.
1022 (1- len))))))
1023
1024 (defun quail-next-translation ()
1025 "Select next translation in the current batch of candidates."
1026 (interactive)
1027 (if quail-current-translations
1028 (progn
1029 (quail-select-translation (1+ (car quail-current-translations)))
1030 (quail-update-translation nil))
1031 (beep)))
1032
1033 (defun quail-prev-translation ()
1034 "Select previous translation in the current batch of candidates."
1035 (interactive)
1036 (if quail-current-translations
1037 (progn
1038 (quail-select-translation (1- (car quail-current-translations)))
1039 (quail-update-translation nil))
1040 (beep)))
1041
1042 (defun quail-next-translation-block ()
1043 "Select the next batch of 10 translation candidates."
1044 (interactive)
1045 (if quail-current-translations
1046 (let ((limit (1- (length (cdr quail-current-translations))))
1047 (n (car quail-current-translations)))
1048 (if (< (/ n 10) (/ limit 10))
1049 (progn
1050 (quail-select-translation (min (+ n 10) limit))
1051 (quail-update-translation nil))
1052 ;; We are already at the last block.
1053 (beep)))
1054 (beep)))
1055
1056 (defun quail-prev-translation-block ()
1057 "Select the previous batch of 10 translation candidates."
1058 (interactive)
1059 (if (and quail-current-translations
1060 (>= (car quail-current-translations) 10))
1061 (progn
1062 (quail-select-translation (- (car quail-current-translations) 10))
1063 (quail-update-translation nil))
1064 (beep)))
1065
1066 (defun quail-select-translation (n)
1067 "Select Nth translation in the current batch of translation candidates."
1068 (if (or (< n 0) (>= n (length (cdr quail-current-translations))))
1069 (beep)
1070 (setcar quail-current-translations n)
1071 (setq quail-current-str (aref (cdr quail-current-translations) n))))
1072
1073 (defun quail-abort-translation ()
1074 "Abort translation and delete the current Quail key sequence."
1075 (interactive)
1076 (quail-delete-region)
1077 (quail-terminate-translation))
1078
1079 (defun quail-delete-last-char ()
1080 "Delete the last input character from the current Quail key sequence."
1081 (interactive)
1082 (if (= (length quail-current-key) 1)
1083 (quail-abort-translation)
1084 (setq quail-current-key (substring quail-current-key 0 -1))
1085 (quail-update-translation (quail-translate-key))))
1086
1087 ;; For conversion mode.
1088
1089 (defun quail-conversion-backward-char ()
1090 (interactive)
1091 (if (<= (point) (overlay-start quail-conv-overlay))
1092 (error "Beginning of conversion region"))
1093 (forward-char -1))
1094
1095 (defun quail-conversion-forward-char ()
1096 (interactive)
1097 (if (>= (point) (overlay-end quail-conv-overlay))
1098 (error "End of conversion region"))
1099 (forward-char 1))
1100
1101 (defun quail-conversion-beginning-of-region ()
1102 (interactive)
1103 (goto-char (overlay-start quail-conv-overlay)))
1104
1105 (defun quail-conversion-end-of-region ()
1106 (interactive)
1107 (goto-char (overlay-end quail-conv-overlay)))
1108
1109 (defun quail-conversion-delete-char ()
1110 (interactive)
1111 (if (>= (point) (overlay-end quail-conv-overlay))
1112 (error "End of conversion region"))
1113 (delete-char 1)
1114 (if (= (overlay-start quail-conv-overlay)
1115 (overlay-end quail-conv-overlay))
1116 (throw 'quail-tag nil)))
1117
1118 (defun quail-conversion-backward-delete-char ()
1119 (interactive)
1120 (if (<= (point) (overlay-start quail-conv-overlay))
1121 (error "Beginning of conversion region"))
1122 (delete-char -1)
1123 (if (= (overlay-start quail-conv-overlay)
1124 (overlay-end quail-conv-overlay))
1125 (throw 'quail-tag nil)))
1126
1127 (defun quail-do-conversion (func &rest args)
1128 "Call FUNC to convert text in the current conversion region of Quail.
1129 Remaining args are for FUNC."
1130 (delete-overlay quail-overlay)
1131 (apply func args))
1132
1133 (defun quail-no-conversion ()
1134 "Do no conversion of the current conversion region of Quail."
1135 (interactive)
1136 (throw 'exit nil))
1137
1138 ;; Guidance, Completion, and Help buffer handlers.
1139
1140 (defun quail-show-guidance-buf ()
1141 "Display a Quail guidance buffer in some window.
1142 Create the buffer if it does not exist yet.
1143 The window is normally shown in a minibuffer,
1144 but if the selected window is a minibuffer, it is shown in
1145 the bottommost ordinary window."
1146
1147 (if (or (null input-method-tersely-flag)
1148 (not (eq (selected-window) (minibuffer-window))))
1149 (progn
1150 ;; At first, setup a guidance buffer.
1151 (or (buffer-live-p quail-guidance-buf)
1152 (setq quail-guidance-buf
1153 (get-buffer-create " *Quail-guidance*")))
1154 (save-excursion
1155 (let ((title (quail-title)))
1156 (set-buffer quail-guidance-buf)
1157 ;; Show the title of Quail package in the left of mode-line.
1158 (setq current-input-method nil)
1159 (setq current-input-method-title title)
1160 (setq mode-line-format (cons '("[" current-input-method-title "]")
1161 default-mode-line-format))
1162 (erase-buffer)
1163 (or (overlayp quail-overlay)
1164 (progn
1165 (setq quail-overlay (make-overlay 1 1))
1166 (overlay-put quail-overlay 'face 'highlight)))
1167 (delete-overlay quail-overlay)
1168 (set-buffer-modified-p nil)))
1169 (bury-buffer quail-guidance-buf)
1170
1171 ;; Then, display it in an appropriate window.
1172 (if (not (get-buffer-window quail-guidance-buf))
1173 ;; Guidance buffer is not yet shown in any window.
1174 (let ((win (minibuffer-window)))
1175 (if (eq (selected-window) win)
1176 ;; Since we are in minibuffer, we can't use it for guidance.
1177 ;; Let's find the bottom window.
1178 (let (height)
1179 (setq win (window-at 0 (- (frame-height) 2)))
1180 (setq height (window-height win))
1181 ;; If WIN is too tall, split it vertically and use
1182 ;; the lower one.
1183 (if (>= height 4)
1184 (let ((window-min-height 2))
1185 ;; Here, `split-window' returns a lower window
1186 ;; which is what we wanted.
1187 (setq win (split-window win (- height 2)))))
1188 (set-window-buffer win quail-guidance-buf)
1189 (set-window-dedicated-p win t))
1190 (set-window-buffer win quail-guidance-buf))))))
1191
1192 ;; And, create a buffer for completion.
1193 (or (buffer-live-p quail-completion-buf)
1194 (progn
1195 (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
1196 (save-excursion
1197 (set-buffer quail-completion-buf)
1198 (setq quail-overlay (make-overlay 1 1))
1199 (overlay-put quail-overlay 'face 'highlight))))
1200 (bury-buffer quail-completion-buf))
1201
1202 (defun quail-hide-guidance-buf ()
1203 "Hide the Quail guidance buffer."
1204 (let* ((win (minibuffer-window))
1205 (buf (window-buffer win)))
1206 (if (eq buf quail-guidance-buf)
1207 ;; Quail guidance buffer is at echo area. Vacate it to the
1208 ;; deepest minibuffer.
1209 (set-window-buffer win (format " *Minibuf-%d*" (minibuffer-depth)))
1210 ;; Delete the window for guidance buffer.
1211 (if (or (null input-method-tersely-flag)
1212 (not (eq (selected-window) (minibuffer-window))))
1213 (progn
1214 (setq win (get-buffer-window quail-guidance-buf))
1215 (set-window-dedicated-p win nil)
1216 (delete-window win))))))
1217
1218 (defun quail-update-guidance ()
1219 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1220 ;; Update guidance buffer.
1221 (if (or (null input-method-tersely-flag)
1222 (not (eq (selected-window) (minibuffer-window))))
1223 (let ((guidance (quail-guidance)))
1224 (cond ((eq guidance t)
1225 ;; Show the current possible translations.
1226 (quail-show-translations))
1227 ((null guidance)
1228 ;; Show the current input keys.
1229 (let ((key quail-current-key))
1230 (save-excursion
1231 (set-buffer quail-guidance-buf)
1232 (erase-buffer)
1233 (insert key))))
1234 ((listp guidance)
1235 ;; Show alternative characters specified in this alist.
1236 (let* ((key quail-current-key)
1237 (len (length key))
1238 (i 0)
1239 ch alternative)
1240 (save-excursion
1241 (set-buffer quail-guidance-buf)
1242 (erase-buffer)
1243 (while (< i len)
1244 (setq ch (aref key i))
1245 (setq alternative (cdr (assoc ch guidance)))
1246 (insert (or alternative ch))
1247 (setq i (1+ i)))))))))
1248
1249 ;; Update completion buffer if displayed now. We highlight the
1250 ;; selected candidate string in *Completion* buffer if any.
1251 (let ((win (get-buffer-window quail-completion-buf))
1252 key str pos)
1253 (if win
1254 (save-excursion
1255 (setq str (if (stringp quail-current-str)
1256 quail-current-str
1257 (if (numberp quail-current-str)
1258 (char-to-string quail-current-str)))
1259 key quail-current-key)
1260 (set-buffer quail-completion-buf)
1261 (goto-char (point-min))
1262 (if (null (search-forward (concat " " key ":") nil t))
1263 (delete-overlay quail-overlay)
1264 (setq pos (point))
1265 (if (and str (search-forward (concat "." str) nil t))
1266 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
1267 (move-overlay quail-overlay (match-beginning 0) (point)))
1268 ;; Now POS points end of KEY and (point) points end of STR.
1269 (if (pos-visible-in-window-p (point) win)
1270 ;; STR is already visible.
1271 nil
1272 ;; We want to make both KEY and STR visible, but if the
1273 ;; window is too short, make at least STR visible.
1274 (setq pos (progn (point) (goto-char pos)))
1275 (beginning-of-line)
1276 (set-window-start win (point))
1277 (if (not (pos-visible-in-window-p pos win))
1278 (set-window-start win pos))
1279 ))))))
1280
1281 (defun quail-show-translations ()
1282 "Show the current possible translations."
1283 (let ((key quail-current-key)
1284 (map (quail-lookup-key quail-current-key (length quail-current-key))))
1285 (save-excursion
1286 (set-buffer quail-guidance-buf)
1287 (erase-buffer)
1288
1289 ;; Show the current key.
1290 (insert key)
1291
1292 ;; Show possible following keys.
1293 (if (cdr map)
1294 (let ((l (cdr map)))
1295 (insert "[")
1296 (while l
1297 (insert (car (car l)))
1298 (setq l (cdr l)))
1299 (insert "]")))
1300
1301 ;; Show list of translations.
1302 (if (consp (car map))
1303 (let* ((idx (car (car map)))
1304 (translations (cdr (car map)))
1305 (from (* (/ idx 10) 10))
1306 (to (min (+ from 10) (length translations))))
1307 (indent-to 10)
1308 (insert (format "(%d/%d)"
1309 (1+ (/ from 10))
1310 (1+ (/ (length translations) 10))))
1311 (while (< from to)
1312 ;; We show the last digit of FROM, but by changing
1313 ;; 0,1,..,9 to 1,2,..,0.
1314 (insert (format " %d."
1315 (if (= (% from 10) 9) 0 (1+ (% from 10)))))
1316 (let ((pos (point)))
1317 (insert (aref translations from))
1318 (if (= idx from)
1319 (move-overlay quail-overlay pos (point))))
1320 (setq from (1+ from)))))
1321 )))
1322
1323 (defun quail-completion ()
1324 "List all completions for the current key.
1325 All possible translations of the current key and whole possible longer keys
1326 are shown."
1327 (interactive)
1328 (let ((key quail-current-key)
1329 (map (quail-lookup-key quail-current-key (length quail-current-key))))
1330 (save-excursion
1331 (set-buffer quail-completion-buf)
1332 (erase-buffer)
1333 (insert "Possible completion and corresponding translations are:\n")
1334 (quail-completion-1 key map 1)
1335 (goto-char (point-min))
1336 (display-buffer (current-buffer)))
1337 (quail-update-guidance)))
1338
1339 ;; List all completions of KEY in MAP with indentation INDENT.
1340 (defun quail-completion-1 (key map indent)
1341 (let ((len (length key)))
1342 (indent-to indent)
1343 (insert key ":")
1344 (if (and (symbolp map) (fboundp map))
1345 (setq map (funcall map key len)))
1346 (if (car map)
1347 (quail-completion-list-translations map key (+ indent len 1))
1348 (insert " -\n"))
1349 (setq indent (+ indent 2))
1350 (if (cdr map)
1351 (let ((l (cdr map))
1352 (newkey (make-string (1+ len) 0))
1353 (i 0))
1354 ;; Set KEY in the first LEN characters of NEWKEY.
1355 (while (< i len)
1356 (aset newkey i (aref key i))
1357 (setq i (1+ i)))
1358 (while l ; L = ((CHAR . DEFN) ....) ;
1359 (aset newkey len (car (car l)))
1360 (quail-completion-1 newkey (cdr (car l)) indent)
1361 (setq l (cdr l)))))))
1362
1363 ;; List all possible translations of KEY in Quail map MAP with
1364 ;; indentation INDENT."
1365 (defun quail-completion-list-translations (map key indent)
1366 (let ((translations
1367 (quail-get-translation map key (length key))))
1368 (if (integerp translations)
1369 (insert "(1/1) 1." translations "\n")
1370 ;; We need only vector part.
1371 (setq translations (cdr translations))
1372 ;; Insert every 10 elements with indices in a line.
1373 (let ((len (length translations))
1374 (i 0)
1375 (first t)
1376 num)
1377 (while (< i len)
1378 (if first
1379 (progn
1380 (insert "(1/1)")
1381 (setq first nil))
1382 (if (= (% i 10) 0)
1383 (progn
1384 (newline)
1385 (indent-to indent)
1386 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))))
1387 ;; We show the last digit of FROM while converting
1388 ;; 0,1,..,9 to 1,2,..,0.
1389 (insert (format " %d." (if (= (% i 10) 9) 0 (1+ (% i 10)))))
1390 (insert (aref translations i))
1391 (setq i (1+ i)))
1392 (newline)))))
1393
1394 (defun quail-help ()
1395 "Show brief description of the current Quail package."
1396 (interactive)
1397 (let ((package quail-current-package)
1398 (buf (get-buffer-create "*Quail-help*")))
1399 (save-excursion
1400 (set-buffer buf)
1401 (erase-buffer)
1402 (setq quail-current-package package)
1403 (insert "Quail input method (name:"
1404 (quail-name)
1405 ", mode line indicator:["
1406 (quail-title)
1407 "])\n---- Documentation ----\n"
1408 (quail-docstring))
1409 (newline)
1410 (if (quail-show-layout) (quail-show-kbd-layout))
1411 (insert )
1412 (quail-help-insert-keymap-description
1413 quail-mode-map
1414 "---- Key bindings (before starting translation) ----
1415 key binding
1416 --- -------\n")
1417 (quail-help-insert-keymap-description
1418 (quail-translation-keymap)
1419 "--- Key bindings (while translating) ---
1420 key binding
1421 --- -------\n")
1422 (if (quail-conversion-keymap)
1423 (quail-help-insert-keymap-description
1424 (quail-conversion-keymap)
1425 "--- Key bindings (while converting) ---
1426 key binding
1427 --- -------\n"))
1428 (goto-char (point-min))
1429 (set-buffer-modified-p nil)
1430 (help-mode))
1431 (display-buffer buf)))
1432
1433 (defun quail-help-insert-keymap-description (keymap &optional header)
1434 (let (from to)
1435 (if header
1436 (insert header))
1437 (save-excursion
1438 (save-window-excursion
1439 (let ((overriding-local-map keymap))
1440 (describe-bindings))
1441 (set-buffer "*Help*")
1442 (goto-char (point-min))
1443 (forward-line 4)
1444 (setq from (point))
1445 (search-forward "Global Bindings:" nil 'move)
1446 (beginning-of-line)
1447 (setq to (point))))
1448 (insert-buffer-substring "*Help*" from to)))
1449
1450 (defun quail-show-kbd-layout ()
1451 "Show keyboard layout with key tops of multilingual characters."
1452 (insert "--- Keyboard layout ---\n")
1453 (let* ((i 0) ch)
1454 (while (< i quail-keyboard-layout-len)
1455 (if (= (% i 30) 0)
1456 (progn
1457 (newline)
1458 (indent-to (/ i 30)))
1459 (if (= (% i 2) 0)
1460 (insert " ")))
1461 (setq ch (aref quail-keyboard-layout i))
1462 (if (= ch ?\ )
1463 (insert ch)
1464 (let ((map (cdr (assq ch (cdr (quail-map))))))
1465 (if map
1466 (let ((translation
1467 (quail-get-translation map (char-to-string ch) 1)))
1468 (if (integerp translation)
1469 (insert translation)
1470 (insert (aref (cdr translation) (car translation)))))
1471 (insert ch))))
1472 (setq i (1+ i))))
1473 (newline))
1474
1475 (defun quail-translation-help ()
1476 "Show help message while translating in Quail mode."
1477 (interactive)
1478 (let ((package quail-current-package)
1479 (current-key quail-current-key)
1480 (buf (get-buffer-create "*Quail-Help*")))
1481 (save-excursion
1482 (set-buffer buf)
1483 (erase-buffer)
1484 (setq quail-current-package package)
1485 (insert
1486 (format "You are translating the key sequence \"%s\" in Quail mode.\n"
1487 quail-current-key))
1488 (quail-help-insert-keymap-description
1489 (quail-translation-keymap)
1490 "-----------------------
1491 key binding
1492 --- -------\n")
1493 (goto-char (point-min))
1494 (set-buffer-modified-p nil))
1495 (display-buffer buf)))
1496
1497 (defun quail-conversion-help ()
1498 "Show help message while converting in Quail mode."
1499 (interactive)
1500 (let ((package quail-current-package)
1501 (str (buffer-substring (overlay-start quail-conv-overlay)
1502 (overlay-end quail-conv-overlay)))
1503 (buf (get-buffer-create "*Quail-Help*")))
1504 (save-excursion
1505 (set-buffer buf)
1506 (erase-buffer)
1507 (setq quail-current-package package)
1508 (insert
1509 (format "You are converting the string \"%s\" in Quail mode.\n" str))
1510 (quail-help-insert-keymap-description
1511 (quail-conversion-keymap)
1512 "-----------------------
1513 key binding
1514 --- -------\n")
1515 (goto-char (point-min))
1516 (set-buffer-modified-p nil))
1517 (display-buffer buf)))
1518
1519 ;;
1520 (provide 'quail)
1521
1522 ;;; quail.el ends here