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