]> code.delx.au - gnu-emacs/blob - lisp/international/quail.el
(quail-start-conversion): Don't include
[gnu-emacs] / lisp / international / quail.el
1 ;;; quail.el --- Provides simple input method for multilingual text
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
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, which depends on the current input method.
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 ;; Each buffer in which Quail is activated should use different
63 ;; guidance buffers.
64 (make-variable-buffer-local 'quail-guidance-buf)
65 (put 'quail-guidance-buf 'permanent-local t)
66
67 ;; A main window showing Quail guidance buffer.
68 (defvar quail-guidance-win nil)
69 (make-variable-buffer-local 'quail-guidance-win)
70
71 (defvar quail-overlay nil
72 "Overlay which covers the current translation region of Quail.")
73 (make-variable-buffer-local 'quail-overlay)
74
75 (defvar quail-conv-overlay nil
76 "Overlay which covers the text to be converted in Quail mode.")
77 (make-variable-buffer-local 'quail-conv-overlay)
78
79 (defvar quail-current-key nil
80 "Current key for translation in Quail mode.")
81 (make-variable-buffer-local 'quail-current-key)
82
83 (defvar quail-current-str nil
84 "Currently selected translation of the current key.")
85 (make-variable-buffer-local 'quail-current-str)
86
87 (defvar quail-current-translations nil
88 "Cons of indices and vector of possible translations of the current key.
89 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
90 CURRENT is an index of the current translation,
91 START and END are indices of the start and end of the current block,
92 BLOCK is the current block index,
93 BLOCKS is a number of blocks of translation.")
94 (make-variable-buffer-local 'quail-current-translations)
95
96 (defvar quail-current-data nil
97 "Any Lisp object holding information of current translation status.
98 When a key sequence is mapped to TRANS and TRANS is a cons
99 of actual translation and some Lisp object to be referred
100 for translating the longer key sequence, this variable is set
101 to that Lisp object.")
102 (make-variable-buffer-local 'quail-current-data)
103
104 ;; Quail package handlers.
105
106 (defvar quail-package-alist nil
107 "List of Quail packages.
108 A Quail package is a list of these elements:
109 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
110 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
111 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
112 CONVERSION-KEYS, SIMPLE.
113
114 QUAIL-MAP is a data structure to map key strings to translations. For
115 the format, see the documentation of `quail-map-p'.
116
117 DECODE-MAP is an alist of translations and corresponding keys.
118
119 See the documentation of `quail-define-package' for the other elements.")
120
121 ;; Return various slots in the current quail-package.
122
123 (defsubst quail-name ()
124 "Return the name of the current Quail package."
125 (nth 0 quail-current-package))
126 (defsubst quail-title ()
127 "Return the title of the current Quail package."
128 (nth 1 quail-current-package))
129 (defsubst quail-map ()
130 "Return the translation map of the current Quail package."
131 (nth 2 quail-current-package))
132 (defsubst quail-guidance ()
133 "Return an object used for `guidance' feature of the current Quail package.
134 See also the documentation of `quail-define-package'."
135 (nth 3 quail-current-package))
136 (defsubst quail-docstring ()
137 "Return the documentation string of the current Quail package."
138 (nth 4 quail-current-package))
139 (defsubst quail-translation-keymap ()
140 "Return translation keymap in the current Quail package.
141 Translation keymap is a keymap used while translation region is active."
142 (nth 5 quail-current-package))
143 (defsubst quail-forget-last-selection ()
144 "Return `forget-last-selection' flag of the current Quail package.
145 See also the documentation of `quail-define-package'."
146 (nth 6 quail-current-package))
147 (defsubst quail-deterministic ()
148 "Return `deterministic' flag of the current Quail package.
149 See also the documentation of `quail-define-package'."
150 (nth 7 quail-current-package))
151 (defsubst quail-kbd-translate ()
152 "Return `kbd-translate' flag of the current Quail package.
153 See also the documentation of `quail-define-package'."
154 (nth 8 quail-current-package))
155 (defsubst quail-show-layout ()
156 "Return `show-layout' flag of the current Quail package.
157 See also the documentation of `quail-define-package'."
158 (nth 9 quail-current-package))
159 (defsubst quail-decode-map ()
160 "Return decode map of the current Quail package.
161 It is an alist of translations and corresponding keys."
162 (nth 10 quail-current-package))
163 (defsubst quail-maximum-shortest ()
164 "Return `maximum-shortest' flag of the current Quail package.
165 See also the documentation of `quail-define-package'."
166 (nth 11 quail-current-package))
167 (defsubst quail-overlay-plist ()
168 "Return property list of an overly used in the current Quail package."
169 (nth 12 quail-current-package))
170 (defsubst quail-update-translation-function ()
171 "Return a function for updating translation in the current Quail package."
172 (nth 13 quail-current-package))
173 (defsubst quail-conversion-keymap ()
174 "Return conversion keymap in the current Quail package.
175 Conversion keymap is a keymap used while conversion region is active
176 but translation region is not active."
177 (nth 14 quail-current-package))
178 (defsubst quail-simple ()
179 "Return t if the current Quail package is simple."
180 (nth 15 quail-current-package))
181
182 (defsubst quail-package (name)
183 "Return Quail package named NAME."
184 (assoc name quail-package-alist))
185
186 (defun quail-add-package (package)
187 "Add Quail package PACKAGE to `quail-package-alist'."
188 (let ((pac (quail-package (car package))))
189 (if pac
190 (setcdr pac (cdr package))
191 (setq quail-package-alist (cons package quail-package-alist)))))
192
193 (defun quail-select-package (name)
194 "Select Quail package named NAME as the current Quail package."
195 (let ((package (quail-package name)))
196 (if (null package)
197 (error "No Quail package `%s'" name))
198 (setq quail-current-package package)
199 (setq-default quail-current-package package)
200 name))
201
202 ;;;###autoload
203 (defun quail-use-package (package-name &rest libraries)
204 "Start using Quail package PACKAGE-NAME.
205 The remaining arguments are libraries to be loaded before using the package."
206 (let ((package (quail-package package-name)))
207 (if (null package)
208 ;; Perhaps we have not yet loaded necessary libraries.
209 (while libraries
210 (if (not (load (car libraries) t))
211 (progn
212 (with-output-to-temp-buffer "*Help*"
213 (princ "Quail package \"")
214 (princ package-name)
215 (princ "\" can't be activated\n because library \"")
216 (princ (car libraries))
217 (princ "\" is not in `load-path'.
218
219 The most common case is that you have not yet installed appropriate
220 libraries in LEIM (Libraries of Emacs Input Method) which is
221 distributed separately from Emacs.
222
223 LEIM is available from the same ftp directory as Emacs."))
224 (error "Can't use the Quail package `%s'" package-name))
225 (setq libraries (cdr libraries))))))
226 (quail-select-package package-name)
227 (setq current-input-method-title (quail-title))
228 (quail-activate))
229
230 (defvar quail-translation-keymap
231 (let ((map (make-keymap))
232 (i 0))
233 (while (< i ?\ )
234 (define-key map (char-to-string i) 'quail-other-command)
235 (setq i (1+ i)))
236 (while (< i 127)
237 (define-key map (char-to-string i) 'quail-self-insert-command)
238 (setq i (1+ i)))
239 (setq i 128)
240 (while (< i 256)
241 (define-key map (vector i) 'quail-self-insert-command)
242 (setq i (1+ i)))
243 (define-key map "\177" 'quail-delete-last-char)
244 (define-key map "\C-f" 'quail-next-translation)
245 (define-key map "\C-b" 'quail-prev-translation)
246 (define-key map "\C-n" 'quail-next-translation-block)
247 (define-key map "\C-p" 'quail-prev-translation-block)
248 (define-key map [right] 'quail-next-translation)
249 (define-key map [left] 'quail-prev-translation)
250 (define-key map [down] 'quail-next-translation-block)
251 (define-key map [up] 'quail-prev-translation-block)
252 (define-key map "\C-i" 'quail-completion)
253 (define-key map "\C-@" 'quail-select-current)
254 ;; Following simple.el, Enter key on numeric keypad selects the
255 ;; current translation just like `C-SPC', and `mouse-2' chooses
256 ;; any completion visible in the *Quail Completions* buffer.
257 (define-key map [kp-enter] 'quail-select-current)
258 (define-key map [mouse-2] 'quail-mouse-choose-completion)
259 (define-key map [down-mouse-2] nil)
260 (define-key map "\C-h" 'quail-translation-help)
261 (define-key map [?\C- ] 'quail-select-current)
262 (define-key map [tab] 'quail-completion)
263 (define-key map [delete] 'quail-delete-last-char)
264 (define-key map [backspace] 'quail-delete-last-char)
265 map)
266 "Keymap used processing translation in complex Quail modes.
267 Only a few especially complex input methods use this map;
268 most use `quail-simple-translation-keymap' instead.
269 This map is activated while translation region is active.")
270
271 (defvar quail-simple-translation-keymap
272 (let ((map (make-keymap))
273 (i 0))
274 (while (< i ?\ )
275 (define-key map (char-to-string i) 'quail-other-command)
276 (setq i (1+ i)))
277 (while (< i 127)
278 (define-key map (char-to-string i) 'quail-self-insert-command)
279 (setq i (1+ i)))
280 (setq i 128)
281 (while (< i 256)
282 (define-key map (vector i) 'quail-self-insert-command)
283 (setq i (1+ i)))
284 (define-key map "\177" 'quail-delete-last-char)
285 (define-key map [delete] 'quail-delete-last-char)
286 (define-key map [backspace] 'quail-delete-last-char)
287 ;;(let ((meta-map (make-sparse-keymap)))
288 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
289 ;;(define-key map [escape] meta-map))
290 map)
291 "Keymap used while processing translation in simple Quail modes.
292 A few especially complex input methods use `quail-translation-keymap' instead.
293 This map is activated while translation region is active.")
294
295 (defvar quail-conversion-keymap
296 (let ((map (make-keymap))
297 (i ?\ ))
298 (while (< i 127)
299 (define-key map (char-to-string i) 'quail-self-insert-command)
300 (setq i (1+ i)))
301 (setq i 128)
302 (while (< i 256)
303 (define-key map (vector i) 'quail-self-insert-command)
304 (setq i (1+ i)))
305 (define-key map "\C-b" 'quail-conversion-backward-char)
306 (define-key map "\C-f" 'quail-conversion-forward-char)
307 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
308 (define-key map "\C-e" 'quail-conversion-end-of-region)
309 (define-key map "\C-d" 'quail-conversion-delete-char)
310 (define-key map "\C-k" 'quail-conversion-delete-tail)
311 (define-key map "\C-h" 'quail-translation-help)
312 (define-key map "\177" 'quail-conversion-backward-delete-char)
313 (define-key map [delete] 'quail-conversion-backward-delete-char)
314 (define-key map [backspace] 'quail-conversion-backward-delete-char)
315 map)
316 "Keymap used for processing conversion in Quail mode.
317 This map is activated while conversion region is active but translation
318 region is not active.")
319
320 ;;;###autoload
321 (defun quail-define-package (name language title
322 &optional guidance docstring translation-keys
323 forget-last-selection deterministic
324 kbd-translate show-layout create-decode-map
325 maximum-shortest overlay-plist
326 update-translation-function
327 conversion-keys simple)
328 "Define NAME as a new Quail package for input LANGUAGE.
329 TITLE is a string to be displayed at mode-line to indicate this package.
330 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
331 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
332 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
333 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
334
335 GUIDANCE specifies how a guidance string is shown in echo area.
336 If it is t, list of all possible translations for the current key is shown
337 with the currently selected translation being highlighted.
338 If it is an alist, the element has the form (CHAR . STRING). Each character
339 in the current key is searched in the list and the corresponding string is
340 shown.
341 If it is nil, the current key is shown.
342
343 DOCSTRING is the documentation string of this package.
344
345 TRANSLATION-KEYS specifies additional key bindings used while translation
346 region is active. It is an alist of single key character vs. corresponding
347 command to be called.
348
349 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
350 for the future to translate the same key. If this flag is nil, a
351 translation selected for a key is remembered so that it can be the
352 first candidate when the same key is entered later.
353
354 DETERMINISTIC non-nil means the first candidate of translation is
355 selected automatically without allowing users to select another
356 translation for a key. In this case, unselected translations are of
357 no use for an interactive use of Quail but can be used by some other
358 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
359 to t.
360
361 KBD-TRANSLATE non-nil means input characters are translated from a
362 user's keyboard layout to the standard keyboard layout. See the
363 documentation of `quail-keyboard-layout' and
364 `quail-keyboard-layout-standard' for more detail.
365
366 SHOW-LAYOUT non-nil means the `quail-help' command should show
367 the user's keyboard layout visually with translated characters.
368 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
369 this package defines no translations for single character keys.
370
371 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
372 map is an alist of translations and corresponding original keys.
373 Although this map is not used by Quail itself, it can be used by some
374 other programs. For instance, Vietnamese supporting needs this map to
375 convert Vietnamese text to VIQR format which uses only ASCII
376 characters to represent Vietnamese characters.
377
378 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
379 length of the shortest sequence. When we don't have a translation of
380 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
381 the key at \"..AB\" and start translation of \"CD..\". Hangul
382 packages, for instance, use this facility. If this flag is nil, we
383 break the key just at \"..ABC\" and start translation of \"D..\".
384
385 OVERLAY-PLIST if non-nil is a property list put on an overlay which
386 covers Quail translation region.
387
388 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
389 the current translation region according to a new translation data. By
390 default, a translated text or a user's key sequence (if no translation
391 for it) is inserted.
392
393 CONVERSION-KEYS specifies additional key bindings used while
394 conversion region is active. It is an alist of single key character
395 vs. corresponding command to be called.
396
397 If SIMPLE is non-nil, then we do not alter the meanings of
398 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
399 non-Quail commands."
400 (let (translation-keymap conversion-keymap)
401 (if deterministic (setq forget-last-selection t))
402 (if translation-keys
403 (progn
404 (setq translation-keymap (copy-keymap
405 (if simple quail-simple-translation-keymap
406 quail-translation-keymap)))
407 (while translation-keys
408 (define-key translation-keymap
409 (car (car translation-keys)) (cdr (car translation-keys)))
410 (setq translation-keys (cdr translation-keys))))
411 (setq translation-keymap
412 (if simple quail-simple-translation-keymap
413 quail-translation-keymap)))
414 (when conversion-keys
415 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
416 (while conversion-keys
417 (define-key conversion-keymap
418 (car (car conversion-keys)) (cdr (car conversion-keys)))
419 (setq conversion-keys (cdr conversion-keys))))
420 (quail-add-package
421 (list name title (list nil) guidance (or docstring "")
422 translation-keymap
423 forget-last-selection deterministic kbd-translate show-layout
424 (if create-decode-map (list 'decode-map) nil)
425 maximum-shortest overlay-plist update-translation-function
426 conversion-keymap simple))
427
428 ;; Update input-method-alist.
429 (let ((slot (assoc name input-method-alist))
430 (val (list language 'quail-use-package title docstring)))
431 (if slot (setcdr slot val)
432 (setq input-method-alist (cons (cons name val) input-method-alist)))))
433
434 (quail-select-package name))
435
436 ;; Quail minor mode handlers.
437
438 ;; Setup overlays used in Quail mode.
439 (defun quail-setup-overlays (conversion-mode)
440 (let ((pos (point)))
441 (if (overlayp quail-overlay)
442 (move-overlay quail-overlay pos pos)
443 (setq quail-overlay (make-overlay pos pos nil nil t))
444 (if input-method-highlight-flag
445 (overlay-put quail-overlay 'face 'underline))
446 (let ((l (quail-overlay-plist)))
447 (while l
448 (overlay-put quail-overlay (car l) (car (cdr l)))
449 (setq l (cdr (cdr l))))))
450 (if conversion-mode
451 (if (overlayp quail-conv-overlay)
452 (if (not (overlay-start quail-conv-overlay))
453 (move-overlay quail-conv-overlay pos pos))
454 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
455 (if input-method-highlight-flag
456 (overlay-put quail-conv-overlay 'face 'underline))))))
457
458 ;; Delete overlays used in Quail mode.
459 (defun quail-delete-overlays ()
460 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
461 (delete-overlay quail-overlay))
462 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
463 (delete-overlay quail-conv-overlay)))
464
465 ;; Kill Quail guidance buffer. Set in kill-buffer-hook.
466 (defun quail-kill-guidance-buf ()
467 (if (buffer-live-p quail-guidance-buf)
468 (kill-buffer quail-guidance-buf)))
469
470 (defun quail-inactivate ()
471 "Inactivate Quail input method."
472 (interactive)
473 (quail-activate -1))
474
475 (defun quail-activate (&optional arg)
476 "Activate Quail input method.
477 With arg, activate Quail input method if and only if arg is positive.
478
479 While this input method is active, the variable
480 `input-method-function' is bound to the function `quail-input-method'."
481 (if (and arg
482 (< (prefix-numeric-value arg) 0))
483 ;; Let's inactivate Quail input method.
484 (unwind-protect
485 (progn
486 (quail-hide-guidance-buf)
487 (quail-delete-overlays)
488 (setq describe-current-input-method-function nil)
489 (run-hooks 'quail-inactivate-hook))
490 (kill-local-variable 'input-method-function))
491 ;; Let's active Quail input method.
492 (if (null quail-current-package)
493 ;; Quail package is not yet selected. Select one now.
494 (let (name)
495 (if quail-package-alist
496 (setq name (car (car quail-package-alist)))
497 (error "No Quail package loaded"))
498 (quail-select-package name)))
499 (setq inactivate-current-input-method-function 'quail-inactivate)
500 (setq describe-current-input-method-function 'quail-help)
501 (quail-delete-overlays)
502 (quail-show-guidance-buf)
503 ;; If we are in minibuffer, turn off the current input method
504 ;; before exiting.
505 (if (eq (selected-window) (minibuffer-window))
506 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
507 (make-local-hook 'kill-buffer-hook)
508 (add-hook 'kill-buffer-hook 'quail-kill-guidance-buf nil t)
509 (run-hooks 'quail-activate-hook)
510 (make-local-variable 'input-method-function)
511 (setq input-method-function 'quail-input-method)))
512
513 (defun quail-exit-from-minibuffer ()
514 (inactivate-input-method)
515 (if (<= (minibuffer-depth) 1)
516 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
517
518 ;; Keyboard layout translation handlers.
519
520 ;; Some Quail packages provide localized keyboard simulation which
521 ;; requires a particular keyboard layout. In this case, what we need
522 ;; is locations of keys the user entered, not character codes
523 ;; generated by those keys. However, for the moment, there's no
524 ;; common way to get such information. So, we ask a user to give
525 ;; information of his own keyboard layout, then translate it to the
526 ;; standard layout which we defined so that all Quail packages depend
527 ;; just on it.
528
529 (defconst quail-keyboard-layout-standard
530 "\
531 \
532 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
533 qQwWeErRtTyYuUiIoOpP[{]} \
534 aAsSdDfFgGhHjJkKlL;:'\"\\| \
535 zZxXcCvVbBnNmM,<.>/? \
536 "
537 "Standard keyboard layout of printable characters Quail assumes.
538 See the documentation of `quail-keyboard-layout' for this format.
539 This layout is almost the same as that of VT100,
540 but the location of key \\ (backslash) is just right of key ' (single-quote),
541 not right of RETURN key.")
542
543 (defvar quail-keyboard-layout quail-keyboard-layout-standard
544 "A string which represents physical key layout of a particular keyboard.
545 We assume there are six rows and each row has 15 keys (columns),
546 the first row is above the `1' - `0' row,
547 the first column of the second row is left of key `1',
548 the first column of the third row is left of key `q',
549 the first column of the fourth row is left of key `a',
550 the first column of the fifth row is left of key `z',
551 the sixth row is below the `z' - `/' row.
552 Nth (N is even) and (N+1)th characters in the string are non-shifted
553 and shifted characters respectively at the same location.
554 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
555 The command `quail-set-keyboard-layout' usually sets this variable.")
556
557 (defconst quail-keyboard-layout-len 180)
558
559 ;; Here we provide several examples of famous keyboard layouts.
560
561 (defvar quail-keyboard-layout-alist
562 (list
563 '("sun-type3" . "\
564 \
565 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
566 qQwWeErRtTyYuUiIoOpP[{]} \
567 aAsSdDfFgGhHjJkKlL;:'\" \
568 zZxXcCvVbBnNmM,<.>/? \
569 ")
570 '("atari-german" . "\
571 \
572 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
573 qQwWeErRtTzZuUiIoOpP\374\334+* \
574 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
575 <>yYxXcCvVbBnNmM,;.:-_ \
576 ")
577
578 '("pc102-de" . "\
579 \
580 ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
581 qQwWeErRtTzZuUiIoOpP\374\334+* \
582 aAsSdDfFgGhHjJkKlL\366\326\344\304 \
583 <>yYxXcCvVbBnNmM,;.:-_ \
584 ")
585
586 (cons "standard" quail-keyboard-layout-standard))
587 "Alist of keyboard names and corresponding layout strings.
588 See the documentation of `quail-keyboard-layout' for the format of
589 the layout string.")
590
591 ;;;###autoload
592 (defun quail-set-keyboard-layout (kbd-type)
593 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
594
595 Since some Quail packages depends on a physical layout of keys (not
596 characters generated by them), those are created by assuming the
597 standard layout defined in `quail-keyboard-layout-standard'. This
598 function tells Quail system the layout of your keyboard so that what
599 you type is correctly handled."
600 (interactive
601 (let* ((completion-ignore-case t)
602 (type (completing-read "Keyboard type: "
603 quail-keyboard-layout-alist)))
604 (list type)))
605 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
606 (if (null layout)
607 ;; Here, we had better ask a user to define his own keyboard
608 ;; layout interactively.
609 (error "Unknown keyboard type `%s'" kbd-type))
610 (setq quail-keyboard-layout (cdr layout))))
611
612 (defun quail-keyboard-translate (ch)
613 "Translate CHAR according to `quail-keyboard-layout' and return the result."
614 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
615 ;; All Quail packages are designed based on
616 ;; `quail-keyboard-layout-standard'.
617 ch
618 (let ((i 0))
619 (while (and (< i quail-keyboard-layout-len)
620 (/= ch (aref quail-keyboard-layout i)))
621 (setq i (1+ i)))
622 (if (= i quail-keyboard-layout-len)
623 ;; CH is not in quail-keyboard-layout, which means that a
624 ;; user typed a key which generated a character code to be
625 ;; handled out of Quail. Just return CH and make
626 ;; quail-execute-non-quail-command handle it correctly.
627 ch
628 (let ((char (aref quail-keyboard-layout-standard i)))
629 (if (= char ?\ )
630 ;; A user typed a key at the location not converted by
631 ;; quail-keyboard-layout-standard. Just return CH as
632 ;; well as above.
633 ch
634 char))))))
635
636 ;; Quail map
637
638 (defsubst quail-map-p (object)
639 "Return t if OBJECT is a Quail map.
640
641 A Quail map holds information how a particular key should be translated.
642 Its format is (TRANSLATION . ALIST).
643 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
644 In the latter case, each element of VECTOR is a candidate for the translation,
645 and INDEX points the currently selected translation.
646
647 ALIST is normally a list of elements that look like (CHAR . DEFN),
648 where DEFN is another Quail map for a longer key (CHAR added to the
649 current key). It may also be a symbol of a function which returns an
650 alist of the above format.
651
652 Just after a Quail package is read, TRANSLATION may be a string or a
653 vector. Then each element of the string or vector is a candidate for
654 the translation. These objects are transformed to cons cells in the
655 format \(INDEX . VECTOR), as described above."
656 (and (consp object)
657 (let ((translation (car object)))
658 (or (integerp translation) (null translation)
659 (vectorp translation) (stringp translation)
660 (symbolp translation)
661 (and (consp translation) (not (vectorp (cdr translation))))))
662 (let ((alist (cdr object)))
663 (or (and (listp alist) (consp (car alist)))
664 (symbolp alist)))))
665
666 ;;;###autoload
667 (defmacro quail-define-rules (&rest rules)
668 "Define translation rules of the current Quail package.
669 Each argument is a list of KEY and TRANSLATION.
670 KEY is a string meaning a sequence of keystrokes to be translated.
671 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
672 If it is a character, it is the sole translation of KEY.
673 If it is a string, each character is a candidate for the translation.
674 If it is a vector, each element (string or character) is a candidate
675 for the translation.
676 In these cases, a key specific Quail map is generated and assigned to KEY.
677
678 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
679 it is used to handle KEY."
680 `(quail-install-map
681 ',(let ((l rules)
682 (map (list nil)))
683 (while l
684 (quail-defrule-internal (car (car l)) (car (cdr (car l))) map t)
685 (setq l (cdr l)))
686 map)))
687
688 ;;;###autoload
689 (defun quail-install-map (map &optional name)
690 "Install the Quail map MAP in the current Quail package.
691
692 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
693 which to install MAP.
694
695 The installed map can be referred by the function `quail-map'."
696 (if (null quail-current-package)
697 (error "No current Quail package"))
698 (if (null (quail-map-p map))
699 (error "Invalid Quail map `%s'" map))
700 (setcar (cdr (cdr quail-current-package)) map))
701
702 ;;;###autoload
703 (defun quail-defrule (key translation &optional name append)
704 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
705 KEY is a string meaning a sequence of keystrokes to be translated.
706 TRANSLATION is a character, a string, a vector, a Quail map,
707 a function, or a cons.
708 It it is a character, it is the sole translation of KEY.
709 If it is a string, each character is a candidate for the translation.
710 If it is a vector, each element (string or character) is a candidate
711 for the translation.
712 If it is a cons, the car is one of the above and the cdr is a function
713 to call when translating KEY (the return value is assigned to the
714 variable `quail-current-data'). If the cdr part is not a function,
715 the value itself is assigned to `quail-current-data'.
716 In these cases, a key specific Quail map is generated and assigned to KEY.
717
718 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
719 it is used to handle KEY.
720
721 Optional 3rd argument NAME, if specified, says which Quail package
722 to define this translation rule in. The default is to define it in the
723 current Quail package.
724
725 Optional 4th argument APPEND, if non-nil, appends TRANSLATION
726 to the current translations for KEY instead of replacing them."
727 (if name
728 (let ((package (quail-package name)))
729 (if (null package)
730 (error "No Quail package `%s'" name))
731 (setq quail-current-package package)))
732 (quail-defrule-internal key translation (quail-map) append))
733
734 ;;;###autoload
735 (defun quail-defrule-internal (key trans map &optional append)
736 "Define KEY as TRANS in a Quail map MAP."
737 (if (null (stringp key))
738 "Invalid Quail key `%s'" key)
739 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
740 (consp trans)
741 (symbolp trans)
742 (quail-map-p trans)))
743 (error "Invalid Quail translation `%s'" trans))
744 (if (null (quail-map-p map))
745 (error "Invalid Quail map `%s'" map))
746 (let ((len (length key))
747 (idx 0)
748 ch entry)
749 ;; Make a map for registering TRANS if necessary.
750 (while (< idx len)
751 (if (null (consp map))
752 ;; We come here, for example, when we try to define a rule
753 ;; for "ABC" but a rule for "AB" is already defined as a
754 ;; symbol.
755 (error "Quail key %s is too long" key))
756 (setq ch (aref key idx)
757 entry (assq ch (cdr map)))
758 (if (null entry)
759 (progn
760 (setq entry (cons ch (list nil)))
761 (setcdr map (cons entry (cdr map)))))
762 (setq map (cdr entry))
763 (setq idx (1+ idx)))
764 (if (symbolp trans)
765 (if (cdr map)
766 ;; We come here, for example, when we try to define a rule
767 ;; for "AB" as a symbol but a rule for "ABC" is already
768 ;; defined.
769 (error "Quail key %s is too short" key)
770 (setcdr entry trans))
771 (if (quail-map-p trans)
772 (if (not (listp (cdr map)))
773 ;; We come here, for example, when we try to define a rule
774 ;; for "AB" as a symbol but a rule for "ABC" is already
775 ;; defined.
776 (error "Quail key %s is too short" key)
777 (if (not (listp (cdr trans)))
778 (if (cdr map)
779 ;; We come here, for example, when we try to
780 ;; define a rule for "AB" as a symbol but a rule
781 ;; for "ABC" is already defined.
782 (error "Quail key %s is too short" key)
783 (setcdr entry trans))
784 (setcdr entry (append trans (cdr map)))))
785 (if (and (car map) append)
786 (let ((prev (quail-get-translation (car map) key len)))
787 (if (integerp prev)
788 (setq prev (vector prev))
789 (setq prev (cdr prev)))
790 (if (integerp trans)
791 (setq trans (vector trans))
792 (if (stringp trans)
793 (setq trans (string-to-vector trans))))
794 (setq trans
795 (cons (list 0 0 0 0 nil)
796 (vconcat prev trans)))))
797 (setcar map trans)))))
798
799 (defun quail-get-translation (def key len)
800 "Return the translation specified as DEF for KEY of length LEN.
801 The translation is either a character or a cons of the form (INDEX . VECTOR),
802 where VECTOR is a vector of candidates (character or string) for
803 the translation, and INDEX points into VECTOR to specify the currently
804 selected translation."
805 (if (and def (symbolp def))
806 (if (functionp def)
807 ;; DEF is a symbol of a function which returns valid translation.
808 (setq def (funcall def key len))
809 (setq def nil)))
810 (if (and (consp def) (not (vectorp (cdr def))))
811 (setq def (car def)))
812
813 (cond
814 ((or (integerp def) (consp def))
815 def)
816
817 ((null def)
818 ;; No translation.
819 nil)
820
821 ((stringp def)
822 ;; Each character in DEF is a candidate of translation. Reform
823 ;; it as (INDICES . VECTOR).
824 (setq def (string-to-vector def))
825 ;; But if the length is 1, we don't need vector but a single
826 ;; candidate as the translation.
827 (if (= (length def) 1)
828 (aref def 0)
829 (cons (list 0 0 0 0 nil) def)))
830
831 ((vectorp def)
832 ;; Each element (string or character) in DEF is a candidate of
833 ;; translation. Reform it as (INDICES . VECTOR).
834 (cons (list 0 0 0 0 nil) def))
835
836 (t
837 (error "Invalid object in Quail map: %s" def))))
838
839 (defun quail-lookup-key (key &optional len)
840 "Lookup KEY of length LEN in the current Quail map and return the definition.
841 The returned value is a Quail map specific to KEY."
842 (or len
843 (setq len (length key)))
844 (let ((idx 0)
845 (map (quail-map))
846 (kbd-translate (quail-kbd-translate))
847 slot ch translation def)
848 (while (and map (< idx len))
849 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
850 (aref key idx)))
851 (setq idx (1+ idx))
852 (if (and (cdr map) (symbolp (cdr map)))
853 (setcdr map (funcall (cdr map) key idx)))
854 (setq slot (assq ch (cdr map)))
855 (if (and (cdr slot) (symbolp (cdr slot)))
856 (setcdr slot (funcall (cdr slot) key idx)))
857 (setq map (cdr slot)))
858 (setq def (car map))
859 (setq quail-current-translations nil)
860 (if (and map (setq translation (quail-get-translation def key len)))
861 (progn
862 (if (and (consp def) (not (vectorp (cdr def))))
863 (progn
864 (if (not (equal (car def) translation))
865 ;; We must reflect TRANSLATION to car part of DEF.
866 (setcar def translation))
867 (setq quail-current-data
868 (if (functionp (cdr def))
869 (funcall (cdr def))
870 (cdr def))))
871 (if (not (equal def translation))
872 ;; We must reflect TRANSLATION to car part of MAP.
873 (setcar map translation)))
874 (if (and (consp translation) (vectorp (cdr translation)))
875 (progn
876 (setq quail-current-translations translation)
877 (if (quail-forget-last-selection)
878 (setcar (car quail-current-translations) 0))))))
879 ;; We may have to reform cdr part of MAP.
880 (if (and (cdr map) (functionp (cdr map)))
881 (setcdr map (funcall (cdr map) key len)))
882 map))
883
884 (put 'quail-error 'error-conditions '(quail-error error))
885 (defun quail-error (&rest args)
886 (signal 'quail-error (apply 'format args)))
887
888 (defvar quail-translating nil)
889 (defvar quail-converting nil)
890 (defvar quail-conversion-str nil)
891
892 (defun quail-input-method (key)
893 (if (or buffer-read-only
894 overriding-terminal-local-map
895 overriding-local-map)
896 (list key)
897 (quail-setup-overlays (quail-conversion-keymap))
898 (let ((modified-p (buffer-modified-p))
899 (buffer-undo-list t))
900 (or (and quail-guidance-win
901 (window-live-p quail-guidance-win)
902 (eq (window-buffer quail-guidance-win) quail-guidance-buf)
903 (not input-method-use-echo-area))
904 (quail-show-guidance-buf))
905 (unwind-protect
906 (if (quail-conversion-keymap)
907 (quail-start-conversion key)
908 (quail-start-translation key))
909 (quail-delete-overlays)
910 (if (buffer-live-p quail-guidance-buf)
911 (save-excursion
912 (set-buffer quail-guidance-buf)
913 (erase-buffer)))
914 (if input-method-use-echo-area
915 (quail-hide-guidance-buf))
916 (set-buffer-modified-p modified-p)
917 ;; Run this hook only when the current input method doesn't require
918 ;; conversion. When conversion is required, the conversion function
919 ;; should run this hook at a proper timing.
920 (unless (quail-conversion-keymap)
921 (run-hooks 'input-method-after-insert-chunk-hook))))))
922
923 (defun quail-overlay-region-events (overlay)
924 (let ((start (overlay-start overlay))
925 (end (overlay-end overlay)))
926 (if (< start end)
927 (prog1
928 (string-to-list (buffer-substring start end))
929 (delete-region start end)))))
930
931 (defsubst quail-delete-region ()
932 "Delete the text in the current translation region of Quail."
933 (if (overlay-start quail-overlay)
934 (delete-region (overlay-start quail-overlay)
935 (overlay-end quail-overlay))))
936
937 (defun quail-start-translation (key)
938 "Start translation of the typed character KEY by the current Quail package."
939 ;; Check the possibility of translating KEY.
940 ;; If KEY is nil, we can anyway start translation.
941 (if (or (and (integerp key)
942 (assq (if (quail-kbd-translate)
943 (quail-keyboard-translate key) key)
944 (cdr (quail-map))))
945 (null key))
946 ;; OK, we can start translation.
947 (let* ((echo-keystrokes 0)
948 (help-char nil)
949 (overriding-terminal-local-map (quail-translation-keymap))
950 (generated-events nil)
951 (input-method-function nil)
952 (modified-p (buffer-modified-p)))
953 (setq quail-current-key ""
954 quail-current-str ""
955 quail-translating t)
956 (if key
957 (setq unread-command-events (cons key unread-command-events)))
958 (while quail-translating
959 (set-buffer-modified-p modified-p)
960 (let* ((keyseq (read-key-sequence
961 (and input-method-use-echo-area
962 (concat input-method-previous-message
963 quail-current-str))
964 nil nil t))
965 (cmd (lookup-key (quail-translation-keymap) keyseq)))
966 (if (if key
967 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
968 (eq cmd 'quail-self-insert-command))
969 (progn
970 (setq key t)
971 (setq last-command-event (aref keyseq (1- (length keyseq)))
972 last-command this-command
973 this-command cmd)
974 (condition-case err
975 (call-interactively cmd)
976 (quail-error (message "%s" (cdr err)) (beep))))
977 ;; KEYSEQ is not defined in the translation keymap.
978 ;; Let's return the event(s) to the caller.
979 (setq unread-command-events
980 (string-to-list (this-single-command-raw-keys)))
981 (setq quail-translating nil))))
982 (quail-delete-region)
983 (if (and quail-current-str (> (length quail-current-str) 0))
984 (let* ((len (length quail-current-str))
985 (idx 0)
986 (val (find-composition 0 len quail-current-str))
987 char)
988 ;; Push characters in quail-current-str one by one to
989 ;; generated-events while interleaving it with a special
990 ;; event (compose-last-chars LEN) at each composition
991 ;; end.
992 (while (<= idx len)
993 (when (and val (= idx (nth 1 val)))
994 (setq generated-events
995 (cons (list 'compose-last-chars (- idx (car val)))
996 generated-events))
997 (setq val (find-composition idx len quail-current-str)))
998 (when (< idx len)
999 (setq char (aref quail-current-str idx))
1000 (or enable-multibyte-characters
1001 (setq char (multibyte-char-to-unibyte char)))
1002 (setq generated-events (cons char generated-events)))
1003 (setq idx (1+ idx)))
1004 ;; Reorder generated-events.
1005 (setq generated-events (nreverse generated-events))))
1006 (if (and input-method-exit-on-first-char generated-events)
1007 (list (car generated-events))
1008 generated-events))
1009
1010 ;; Since KEY doesn't start any translation, just return it.
1011 (list key)))
1012
1013 (defun quail-start-conversion (key)
1014 "Start conversion of the typed character KEY by the current Quail package."
1015 ;; Check the possibility of translating KEY.
1016 ;; If KEY is nil, we can anyway start translation.
1017 (if (or (and (integerp key)
1018 (assq (if (quail-kbd-translate)
1019 (quail-keyboard-translate key) key)
1020 (cdr (quail-map))))
1021 (null key))
1022 ;; Ok, we can start translation and conversion.
1023 (let* ((echo-keystrokes 0)
1024 (help-char nil)
1025 (overriding-terminal-local-map (quail-conversion-keymap))
1026 (generated-events nil)
1027 (input-method-function nil)
1028 (modified-p (buffer-modified-p)))
1029 (setq quail-current-key ""
1030 quail-current-str ""
1031 quail-translating t
1032 quail-converting t
1033 quail-conversion-str "")
1034 (if key
1035 (setq unread-command-events (cons key unread-command-events)))
1036 (while quail-converting
1037 (set-buffer-modified-p modified-p)
1038 (or quail-translating
1039 (progn
1040 (setq quail-current-key ""
1041 quail-current-str ""
1042 quail-translating t)
1043 (quail-setup-overlays nil)))
1044 (let* ((keyseq (read-key-sequence
1045 (and input-method-use-echo-area
1046 (concat input-method-previous-message
1047 quail-conversion-str
1048 quail-current-str))
1049 nil nil t))
1050 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1051 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1052 (progn
1053 (setq key t)
1054 (setq last-command-event (aref keyseq (1- (length keyseq)))
1055 last-command this-command
1056 this-command cmd)
1057 (condition-case err
1058 (call-interactively cmd)
1059 (quail-error (message "%s" (cdr err)) (beep)))
1060 (or quail-translating
1061 (progn
1062 (if quail-current-str
1063 (setq quail-conversion-str
1064 (concat quail-conversion-str
1065 (if (stringp quail-current-str)
1066 quail-current-str
1067 (char-to-string quail-current-str)))))
1068 (if (or input-method-exit-on-first-char
1069 (= (length quail-conversion-str) 0))
1070 (setq quail-converting nil)))))
1071 ;; KEYSEQ is not defined in the conversion keymap.
1072 ;; Let's return the event(s) to the caller.
1073 (setq unread-command-events
1074 (string-to-list (this-single-command-raw-keys)))
1075 (setq quail-converting nil))))
1076 (if (overlay-start quail-conv-overlay)
1077 (delete-region (overlay-start quail-conv-overlay)
1078 (overlay-end quail-conv-overlay)))
1079 (if (> (length quail-conversion-str) 0)
1080 (setq generated-events
1081 (string-to-list
1082 (if enable-multibyte-characters
1083 quail-conversion-str
1084 (string-make-unibyte quail-conversion-str)))))
1085 (if (and input-method-exit-on-first-char generated-events)
1086 (list (car generated-events))
1087 generated-events))
1088
1089 ;; Since KEY doesn't start any translation, just return it.
1090 (list key)))
1091
1092 (defun quail-terminate-translation ()
1093 "Terminate the translation of the current key."
1094 (setq quail-translating nil)
1095 (if (buffer-live-p quail-guidance-buf)
1096 (save-excursion
1097 (set-buffer quail-guidance-buf)
1098 (erase-buffer))))
1099
1100 (defun quail-select-current ()
1101 "Select the current text shown in Quail translation region."
1102 (interactive)
1103 (quail-terminate-translation))
1104
1105 ;; Update the current translation status according to CONTROL-FLAG.
1106 ;; If CONTROL-FLAG is integer value, it is the number of keys in the
1107 ;; head quail-current-key which can be translated. The remaining keys
1108 ;; are put back to unread-command-events to be handled again. If
1109 ;; CONTROL-FLAG is t, terminate the translation for the whole keys in
1110 ;; quail-current-key. If CONTROL-FLAG is nil, proceed the translation
1111 ;; with more keys.
1112
1113 (defun quail-update-translation (control-flag)
1114 (let ((func (quail-update-translation-function)))
1115 (if func
1116 (setq control-flag (funcall func control-flag))
1117 (if (numberp control-flag)
1118 (let ((len (length quail-current-key)))
1119 (if (= len 1)
1120 (setq control-flag t
1121 quail-current-str quail-current-key)
1122 (if input-method-exit-on-first-char
1123 (setq len control-flag)
1124 (while (> len control-flag)
1125 (setq len (1- len))
1126 (setq unread-command-events
1127 (cons (aref quail-current-key len)
1128 unread-command-events))))
1129 (if quail-current-str
1130 (if input-method-exit-on-first-char
1131 (setq control-flag t))
1132 (setq quail-current-str
1133 (substring quail-current-key 0 len)))))
1134 (if quail-current-str
1135 (if (and input-method-exit-on-first-char
1136 (quail-simple))
1137 (setq control-flag t))
1138 (setq quail-current-str quail-current-key)))))
1139 (or input-method-use-echo-area
1140 (progn
1141 (quail-delete-region)
1142 (insert quail-current-str)))
1143 (let (quail-current-str)
1144 (quail-update-guidance))
1145 (or (stringp quail-current-str)
1146 (setq quail-current-str (char-to-string quail-current-str)))
1147 (if control-flag
1148 (quail-terminate-translation)))
1149
1150 (defun quail-self-insert-command ()
1151 "Add the typed character to the key for translation."
1152 (interactive "*")
1153 (setq quail-current-key
1154 (concat quail-current-key (char-to-string last-command-event)))
1155 (or (catch 'quail-tag
1156 (quail-update-translation (quail-translate-key))
1157 t)
1158 ;; If someone throws for `quail-tag' by value nil, we exit from
1159 ;; translation mode.
1160 (setq quail-translating nil)))
1161
1162 ;; Return the actual definition part of Quail map MAP.
1163 (defun quail-map-definition (map)
1164 (let ((def (car map)))
1165 (if (and (consp def) (not (vectorp (cdr def))))
1166 (setq def (car def)))
1167 (if (eq def t)
1168 (setq def nil))
1169 def))
1170
1171 ;; Return a string to be shown as the current translation of key
1172 ;; sequence of length LEN. DEF is a definition part of Quail map for
1173 ;; the sequence.
1174 (defun quail-get-current-str (len def)
1175 (or (and (consp def) (aref (cdr def) (car (car def))))
1176 def
1177 (and (> len 1)
1178 (let ((str (quail-get-current-str
1179 (1- len)
1180 (quail-map-definition (quail-lookup-key
1181 quail-current-key (1- len))))))
1182 (if str
1183 (concat (if (stringp str) str (char-to-string str))
1184 (substring quail-current-key (1- len) len)))))))
1185
1186 (defvar quail-guidance-translations-starting-column 20)
1187
1188 ;; Update `quail-current-translations' to make RELATIVE-INDEX the
1189 ;; current translation.
1190 (defun quail-update-current-translations (&optional relative-index)
1191 (let* ((indices (car quail-current-translations))
1192 (cur (car indices))
1193 (start (nth 1 indices))
1194 (end (nth 2 indices)))
1195 ;; Validate the index number of current translation.
1196 (if (< cur 0)
1197 (setcar indices (setq cur 0))
1198 (if (>= cur (length (cdr quail-current-translations)))
1199 (setcar indices
1200 (setq cur (1- (length (cdr quail-current-translations)))))))
1201
1202 (if (or (null end) ; We have not yet calculated END.
1203 (< cur start) ; We moved to the previous block.
1204 (>= cur end)) ; We moved to the next block.
1205 (let ((len (length (cdr quail-current-translations)))
1206 (maxcol (- (window-width quail-guidance-win)
1207 quail-guidance-translations-starting-column))
1208 (block (nth 3 indices))
1209 col idx width trans num-items blocks)
1210 (if (< cur start)
1211 ;; We must calculate from the head.
1212 (setq start 0 block 0)
1213 (if end ; i.e. (>= cur end)
1214 (setq start end)))
1215 (setq idx start col 0 end start num-items 0)
1216 ;; Loop until we hit the tail, or reach the block of CUR.
1217 (while (and (< idx len) (>= cur end))
1218 (if (= num-items 0)
1219 (setq start idx col 0 block (1+ block)))
1220 (setq trans (aref (cdr quail-current-translations) idx))
1221 (setq width (if (integerp trans) (char-width trans)
1222 (string-width trans)))
1223 (setq col (+ col width 3) num-items (1+ num-items))
1224 (if (and (> num-items 0)
1225 (or (>= col maxcol) (> num-items 10)))
1226 (setq end idx num-items 0)
1227 (setq idx (1+ idx))))
1228 (setcar (nthcdr 3 indices) block)
1229 (if (>= idx len)
1230 (progn
1231 ;; We hit the tail before reaching MAXCOL.
1232 (setq end idx)
1233 (setcar (nthcdr 4 indices) block)))
1234 (setcar (cdr indices) start)
1235 (setcar (nthcdr 2 indices) end)))
1236 (if relative-index
1237 (if (>= (+ start relative-index) end)
1238 (setcar indices end)
1239 (setcar indices (+ start relative-index))))
1240 (setq quail-current-str
1241 (aref (cdr quail-current-translations) (car indices)))
1242 (or (stringp quail-current-str)
1243 (setq quail-current-str (char-to-string quail-current-str)))))
1244
1245 (defun quail-translate-key ()
1246 "Translate the current key sequence according to the current Quail map.
1247 Return t if we can terminate the translation.
1248 Return nil if the current key sequence may be followed by more keys.
1249 Return number if we can't find any translation for the current key
1250 sequence. The number is the count of valid keys in the current
1251 sequence counting from the head."
1252 (let* ((len (length quail-current-key))
1253 (map (quail-lookup-key quail-current-key len))
1254 def ch)
1255 (if map
1256 (let ((def (quail-map-definition map)))
1257 (setq quail-current-str (quail-get-current-str len def))
1258 ;; Return t only if we can terminate the current translation.
1259 (and
1260 ;; No alternative translations.
1261 (or (null (consp def)) (= (length (cdr def)) 1))
1262 ;; No translation for the longer key.
1263 (null (cdr map))
1264 ;; No shorter breaking point.
1265 (or (null (quail-maximum-shortest))
1266 (< len 3)
1267 (null (quail-lookup-key quail-current-key (1- len)))
1268 (null (quail-lookup-key
1269 (substring quail-current-key -2 -1) 1)))))
1270
1271 ;; There's no translation for the current key sequence. Before
1272 ;; giving up, we must check two possibilities.
1273 (cond ((and
1274 (quail-maximum-shortest)
1275 (>= len 4)
1276 (setq def (quail-map-definition
1277 (quail-lookup-key quail-current-key (- len 2))))
1278 (quail-lookup-key (substring quail-current-key -2) 2))
1279 ;; Now the sequence is "...ABCD", which can be split into
1280 ;; "...AB" and "CD..." to get valid translation.
1281 ;; At first, get translation of "...AB".
1282 (setq quail-current-str (quail-get-current-str (- len 2) def))
1283 ;; Then, return the length of "...AB".
1284 (- len 2))
1285
1286 ((and (> len 0)
1287 (quail-lookup-key (substring quail-current-key 0 -1))
1288 quail-current-translations
1289 (not (quail-deterministic))
1290 (setq ch (aref quail-current-key (1- len)))
1291 (>= ch ?0) (<= ch ?9))
1292 ;; A numeric key is entered to select a desirable translation.
1293 (setq quail-current-key (substring quail-current-key 0 -1))
1294 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1295 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1296 (quail-update-current-translations ch)
1297 ;; And, we can terminate the current translation.
1298 t)
1299
1300 (t
1301 ;; No way to handle the last character in this context.
1302 (1- len))))))
1303
1304 (defun quail-next-translation ()
1305 "Select next translation in the current batch of candidates."
1306 (interactive)
1307 (if quail-current-translations
1308 (let ((indices (car quail-current-translations)))
1309 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1310 ;; We are already at the tail.
1311 (beep)
1312 (setcar indices (1+ (car indices)))
1313 (quail-update-current-translations)
1314 (quail-update-translation nil)))
1315 (setq unread-command-events
1316 (cons last-command-event unread-command-events))
1317 (quail-terminate-translation)))
1318
1319 (defun quail-prev-translation ()
1320 "Select previous translation in the current batch of candidates."
1321 (interactive)
1322 (if quail-current-translations
1323 (let ((indices (car quail-current-translations)))
1324 (if (= (car indices) 0)
1325 ;; We are already at the head.
1326 (beep)
1327 (setcar indices (1- (car indices)))
1328 (quail-update-current-translations)
1329 (quail-update-translation nil)))
1330 (setq unread-command-events
1331 (cons last-command-event unread-command-events))
1332 (quail-terminate-translation)))
1333
1334 (defun quail-next-translation-block ()
1335 "Select from the next block of translations."
1336 (interactive)
1337 (if quail-current-translations
1338 (let* ((indices (car quail-current-translations))
1339 (offset (- (car indices) (nth 1 indices))))
1340 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1341 ;; We are already at the last block.
1342 (beep)
1343 (setcar indices (+ (nth 2 indices) offset))
1344 (quail-update-current-translations)
1345 (quail-update-translation nil)))
1346 (setq unread-command-events
1347 (cons last-command-event unread-command-events))
1348 (quail-terminate-translation)))
1349
1350 (defun quail-prev-translation-block ()
1351 "Select the previous batch of 10 translation candidates."
1352 (interactive)
1353 (if quail-current-translations
1354 (let* ((indices (car quail-current-translations))
1355 (offset (- (car indices) (nth 1 indices))))
1356 (if (= (nth 1 indices) 0)
1357 ;; We are already at the first block.
1358 (beep)
1359 (setcar indices (1- (nth 1 indices)))
1360 (quail-update-current-translations)
1361 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1362 (progn
1363 (setcar indices (+ (nth 1 indices) offset))
1364 (quail-update-current-translations)))
1365 (quail-update-translation nil)))
1366 (setq unread-command-events
1367 (cons last-command-event unread-command-events))
1368 (quail-terminate-translation)))
1369
1370 (defun quail-abort-translation ()
1371 "Abort translation and delete the current Quail key sequence."
1372 (interactive)
1373 (quail-delete-region)
1374 (setq quail-current-str nil)
1375 (quail-terminate-translation))
1376
1377 (defun quail-delete-last-char ()
1378 "Delete the last input character from the current Quail key sequence."
1379 (interactive)
1380 (if (= (length quail-current-key) 1)
1381 (quail-abort-translation)
1382 (setq quail-current-key (substring quail-current-key 0 -1))
1383 (quail-delete-region)
1384 (quail-update-translation (quail-translate-key))))
1385
1386 ;; For conversion mode.
1387
1388 (defsubst quail-point-in-conversion-region ()
1389 "Return non-nil value if the point is in conversion region of Quail mode."
1390 (let (start pos)
1391 (and (setq start (overlay-start quail-conv-overlay))
1392 (>= (setq pos (point)) start)
1393 (<= pos (overlay-end quail-conv-overlay)))))
1394
1395 (defun quail-conversion-backward-char ()
1396 (interactive)
1397 (if (<= (point) (overlay-start quail-conv-overlay))
1398 (quail-error "Beginning of conversion region"))
1399 (setq quail-translating nil)
1400 (forward-char -1))
1401
1402 (defun quail-conversion-forward-char ()
1403 (interactive)
1404 (if (>= (point) (overlay-end quail-conv-overlay))
1405 (quail-error "End of conversion region"))
1406 (setq quail-translating nil)
1407 (forward-char 1))
1408
1409 (defun quail-conversion-beginning-of-region ()
1410 (interactive)
1411 (setq quail-translating nil)
1412 (goto-char (overlay-start quail-conv-overlay)))
1413
1414 (defun quail-conversion-end-of-region ()
1415 (interactive)
1416 (setq quail-translating nil)
1417 (goto-char (overlay-end quail-conv-overlay)))
1418
1419 (defun quail-conversion-delete-char ()
1420 (interactive)
1421 (setq quail-translating nil)
1422 (if (>= (point) (overlay-end quail-conv-overlay))
1423 (quail-error "End of conversion region"))
1424 (delete-char 1)
1425 (let ((start (overlay-start quail-conv-overlay))
1426 (end (overlay-end quail-conv-overlay)))
1427 (setq quail-conversion-str (buffer-substring start end))
1428 (if (= start end)
1429 (setq quail-converting nil))))
1430
1431 (defun quail-conversion-delete-tail ()
1432 (interactive)
1433 (if (>= (point) (overlay-end quail-conv-overlay))
1434 (quail-error "End of conversion region"))
1435 (delete-region (point) (overlay-end quail-conv-overlay))
1436 (let ((start (overlay-start quail-conv-overlay))
1437 (end (overlay-end quail-conv-overlay)))
1438 (setq quail-conversion-str (buffer-substring start end))
1439 (if (= start end)
1440 (setq quail-converting nil))))
1441
1442 (defun quail-conversion-backward-delete-char ()
1443 (interactive)
1444 (if (> (length quail-current-key) 0)
1445 (quail-delete-last-char)
1446 (if (<= (point) (overlay-start quail-conv-overlay))
1447 (quail-error "Beginning of conversion region"))
1448 (delete-char -1)
1449 (let ((start (overlay-start quail-conv-overlay))
1450 (end (overlay-end quail-conv-overlay)))
1451 (setq quail-conversion-str (buffer-substring start end))
1452 (if (= start end)
1453 (setq quail-converting nil)))))
1454
1455 (defun quail-do-conversion (func &rest args)
1456 "Call FUNC to convert text in the current conversion region of Quail.
1457 Remaining args are for FUNC."
1458 (delete-overlay quail-overlay)
1459 (apply func args))
1460
1461 (defun quail-no-conversion ()
1462 "Do no conversion of the current conversion region of Quail."
1463 (interactive)
1464 (setq quail-converting nil))
1465
1466 ;; Guidance, Completion, and Help buffer handlers.
1467
1468 ;; Make a new one-line frame for Quail guidance buffer.
1469 (defun quail-make-guidance-frame (buf)
1470 (let* ((fparam (frame-parameters))
1471 (top (cdr (assq 'top fparam)))
1472 (border (cdr (assq 'border-width fparam)))
1473 (internal-border (cdr (assq 'internal-border-width fparam)))
1474 (newtop (- top
1475 (frame-char-height) (* internal-border 2) (* border 2))))
1476 (if (< newtop 0)
1477 (setq newtop (+ top (frame-pixel-height))))
1478 (let* ((frame (make-frame (append '((user-position . t) (height . 1)
1479 (minibuffer) (menu-bar-lines . 0))
1480 (cons (cons 'top newtop) fparam))))
1481 (win (frame-first-window frame)))
1482 (set-window-buffer win buf)
1483 ;;(set-window-dedicated-p win t)
1484 )))
1485
1486 ;; Setup Quail completion buffer.
1487 (defun quail-setup-completion-buf ()
1488 (unless (buffer-live-p quail-completion-buf)
1489 (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
1490 (save-excursion
1491 (set-buffer quail-completion-buf)
1492 (setq quail-overlay (make-overlay 1 1))
1493 (overlay-put quail-overlay 'face 'highlight))))
1494
1495 ;; Return t iff the current Quail package requires showing guidance
1496 ;; buffer.
1497 (defun quail-require-guidance-buf ()
1498 (and input-method-verbose-flag
1499 (if (eq input-method-verbose-flag 'default)
1500 (not (and (eq (selected-window) (minibuffer-window))
1501 (quail-simple)))
1502 (if (eq input-method-verbose-flag 'complex-only)
1503 (not (quail-simple))
1504 t))))
1505
1506 (defun quail-show-guidance-buf ()
1507 "Display a guidance buffer for Quail input method in some window.
1508 Create the buffer if it does not exist yet.
1509 The buffer is normally displayed at the echo area,
1510 but if the current buffer is a minibuffer, it is shown in
1511 the bottom-most ordinary window of the same frame,
1512 or in a newly created frame (if the selected frame has no other windows)."
1513 (when (quail-require-guidance-buf)
1514 ;; At first, setup a guidance buffer.
1515 (or (buffer-live-p quail-guidance-buf)
1516 (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*")))
1517 (let ((name (quail-name))
1518 (title (quail-title)))
1519 (save-excursion
1520 (set-buffer quail-guidance-buf)
1521 ;; To show the title of Quail package.
1522 (setq current-input-method name
1523 current-input-method-title title)
1524 (erase-buffer)
1525 (or (overlayp quail-overlay)
1526 (progn
1527 (setq quail-overlay (make-overlay 1 1))
1528 (overlay-put quail-overlay 'face 'highlight)))
1529 (delete-overlay quail-overlay)
1530 (set-buffer-modified-p nil)))
1531 (bury-buffer quail-guidance-buf)
1532
1533 ;; Assign the buffer " *Minibuf-N*" to all windows which are now
1534 ;; displaying quail-guidance-buf.
1535 (let ((win-list (get-buffer-window-list quail-guidance-buf t t)))
1536 (while win-list
1537 (set-window-buffer (car win-list)
1538 (format " *Minibuf-%d*" (minibuffer-depth)))
1539 (setq win-list (cdr win-list))))
1540
1541 ;; Then, display it in an appropriate window.
1542 (let ((win (minibuffer-window)))
1543 (if (or (eq (selected-window) win)
1544 input-method-use-echo-area)
1545 ;; Since we are in minibuffer, we can't use it for guidance.
1546 (if (eq win (frame-root-window))
1547 ;; Create a frame. It is sure that we are using some
1548 ;; window system.
1549 (quail-make-guidance-frame quail-guidance-buf)
1550 ;; Find the bottom window and split it if necessary.
1551 (let (height)
1552 (setq win (window-at 0 (- (frame-height) 2)))
1553 (setq height (window-height win))
1554 ;; If WIN is tall enough, split it vertically and use
1555 ;; the lower one.
1556 (if (>= height 4)
1557 (let ((window-min-height 2))
1558 ;; Here, `split-window' returns a lower window
1559 ;; which is what we wanted.
1560 (setq win (split-window win (- height 2)))))
1561 (set-window-buffer win quail-guidance-buf)
1562 ;;(set-window-dedicated-p win t)
1563 ))
1564 (set-window-buffer win quail-guidance-buf)
1565 (set-minibuffer-window win))
1566 (setq quail-guidance-win win)))
1567
1568 ;; And, create a buffer for completion.
1569 (quail-setup-completion-buf)
1570 (bury-buffer quail-completion-buf))
1571
1572 (defun quail-hide-guidance-buf ()
1573 "Hide the Quail guidance buffer."
1574 (if (buffer-live-p quail-guidance-buf)
1575 (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
1576 win)
1577 (while win-list
1578 (setq win (car win-list) win-list (cdr win-list))
1579 (if (window-minibuffer-p win)
1580 ;; We are using echo area for the guidance buffer.
1581 ;; Vacate it to the deepest minibuffer.
1582 (set-window-buffer win
1583 (format " *Minibuf-%d*" (minibuffer-depth)))
1584 (if (eq win (frame-root-window (window-frame win)))
1585 (progn
1586 ;; We are using a separate frame for guidance buffer.
1587 ;;(set-window-dedicated-p win nil)
1588 (delete-frame (window-frame win)))
1589 ;;(set-window-dedicated-p win nil)
1590 (delete-window win))))
1591 (setq quail-guidance-win nil))))
1592
1593 (defun quail-update-guidance ()
1594 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1595 ;; Update guidance buffer.
1596 (if (quail-require-guidance-buf)
1597 (let ((guidance (quail-guidance)))
1598 (or (and (eq (selected-frame) (window-frame (minibuffer-window)))
1599 (eq (selected-frame) (window-frame quail-guidance-win)))
1600 (quail-show-guidance-buf))
1601 (cond ((or (eq guidance t)
1602 (consp guidance))
1603 ;; Show the current possible translations.
1604 (quail-show-translations))
1605 ((null guidance)
1606 ;; Show the current input keys.
1607 (let ((key quail-current-key))
1608 (save-excursion
1609 (set-buffer quail-guidance-buf)
1610 (erase-buffer)
1611 (insert key)))))))
1612
1613 ;; Update completion buffer if displayed now. We highlight the
1614 ;; selected candidate string in *Completion* buffer if any.
1615 (let ((win (get-buffer-window quail-completion-buf))
1616 key str pos)
1617 (if win
1618 (save-excursion
1619 (setq str (if (stringp quail-current-str)
1620 quail-current-str
1621 (if (numberp quail-current-str)
1622 (char-to-string quail-current-str)))
1623 key quail-current-key)
1624 (set-buffer quail-completion-buf)
1625 (goto-char (point-min))
1626 (if (null (search-forward (concat " " key ":") nil t))
1627 (delete-overlay quail-overlay)
1628 (setq pos (point))
1629 (if (and str (search-forward (concat "." str) nil t))
1630 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
1631 (move-overlay quail-overlay (match-beginning 0) (point)))
1632 ;; Now POS points end of KEY and (point) points end of STR.
1633 (if (pos-visible-in-window-p (point) win)
1634 ;; STR is already visible.
1635 nil
1636 ;; We want to make both KEY and STR visible, but if the
1637 ;; window is too short, make at least STR visible.
1638 (setq pos (progn (point) (goto-char pos)))
1639 (beginning-of-line)
1640 (set-window-start win (point))
1641 (if (not (pos-visible-in-window-p pos win))
1642 (set-window-start win pos))
1643 ))))))
1644
1645 (defun quail-show-translations ()
1646 "Show the current possible translations."
1647 (let* ((key quail-current-key)
1648 (map (quail-lookup-key quail-current-key))
1649 (current-translations quail-current-translations))
1650 (if quail-current-translations
1651 (quail-update-current-translations))
1652 (save-excursion
1653 (set-buffer quail-guidance-buf)
1654 (erase-buffer)
1655
1656 ;; Show the current key.
1657 (let ((guidance (quail-guidance)))
1658 (if (listp guidance)
1659 ;; We must show the specified PROMPTKEY instead of the
1660 ;; actual typed keys.
1661 (let ((i 0)
1662 (len (length key))
1663 prompt-key)
1664 (while (< i len)
1665 (setq prompt-key (cdr (assoc (aref key i) guidance)))
1666 (insert (or prompt-key (aref key i)))
1667 (setq i (1+ i))))
1668 (insert key)))
1669
1670 ;; Show followable keys.
1671 (if (and (> (length key) 0) (cdr map))
1672 (let ((keys (mapcar (function (lambda (x) (car x)))
1673 (cdr map))))
1674 (setq keys (sort keys '<))
1675 (insert "[")
1676 (while keys
1677 (insert (car keys))
1678 (setq keys (cdr keys)))
1679 (insert "]")))
1680
1681 ;; Show list of translations.
1682 (if (and current-translations
1683 (not (quail-deterministic)))
1684 (let* ((indices (car current-translations))
1685 (cur (car indices))
1686 (start (nth 1 indices))
1687 (end (nth 2 indices))
1688 (idx start))
1689 (indent-to (- quail-guidance-translations-starting-column 7))
1690 (insert (format "(%02d/"(nth 3 indices))
1691 (if (nth 4 indices)
1692 (format "%02d)" (nth 4 indices))
1693 "??)"))
1694 (while (< idx end)
1695 (insert (format " %d." (if (= (- idx start) 9) 0
1696 (1+ (- idx start)))))
1697 (let ((pos (point)))
1698 (insert (aref (cdr current-translations) idx))
1699 (if (= idx cur)
1700 (move-overlay quail-overlay pos (point))))
1701 (setq idx (1+ idx)))))
1702 )))
1703
1704 (defvar quail-completion-max-depth 5
1705 "The maximum depth of Quail completion list.")
1706
1707 (defun quail-completion ()
1708 "List all completions for the current key.
1709 All possible translations of the current key and whole possible longer keys
1710 are shown (at most to the depth specified `quail-completion-max-depth')."
1711 (interactive)
1712 (quail-setup-completion-buf)
1713 (let ((win (get-buffer-window quail-completion-buf 'visible))
1714 (key quail-current-key)
1715 (map (quail-lookup-key quail-current-key))
1716 (require-update nil))
1717 (save-excursion
1718 (set-buffer quail-completion-buf)
1719 (if (and win
1720 (equal key quail-current-key)
1721 (eq last-command 'quail-completion))
1722 ;; The window for Quail completion buffer has already been
1723 ;; shown. We just scroll it appropriately.
1724 (if (pos-visible-in-window-p (point-max) win)
1725 (set-window-start win (point-min))
1726 (let ((other-window-scroll-buffer quail-completion-buf))
1727 (scroll-other-window)))
1728 (setq quail-current-key key)
1729 (erase-buffer)
1730 (insert "Possible completion and corresponding translations are:\n")
1731 (quail-completion-1 key map 1)
1732 (goto-char (point-min))
1733 (display-buffer (current-buffer))
1734 (setq require-update t)))
1735 (if require-update
1736 (quail-update-guidance)))
1737 (setq this-command 'quail-completion))
1738
1739 ;; List all completions of KEY in MAP with indentation INDENT.
1740 (defun quail-completion-1 (key map indent)
1741 (let ((len (length key)))
1742 (indent-to indent)
1743 (insert key ":")
1744 (if (and (symbolp map) (fboundp map))
1745 (setq map (funcall map key len)))
1746 (if (car map)
1747 (quail-completion-list-translations map key (+ indent len 1))
1748 (insert " -\n"))
1749 (setq indent (+ indent 2))
1750 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
1751 (let ((l (cdr map))
1752 (newkey (make-string (1+ len) 0))
1753 (i 0))
1754 (if (functionp l)
1755 (setq l (funcall l)))
1756 ;; Set KEY in the first LEN characters of NEWKEY.
1757 (while (< i len)
1758 (aset newkey i (aref key i))
1759 (setq i (1+ i)))
1760 (while l ; L = ((CHAR . DEFN) ....) ;
1761 (aset newkey len (car (car l)))
1762 (quail-completion-1 newkey (cdr (car l)) indent)
1763 (setq l (cdr l)))))))
1764
1765 ;; List all possible translations of KEY in Quail map MAP with
1766 ;; indentation INDENT.
1767 (defun quail-completion-list-translations (map key indent)
1768 (let (beg (translations
1769 (quail-get-translation (car map) key (length key))))
1770 (if (integerp translations)
1771 (progn
1772 (insert "(1/1) 1.")
1773 ;; Endow the character `translations' with `mouse-face' text
1774 ;; property to enable `mouse-2' completion.
1775 (setq beg (point))
1776 (insert translations)
1777 (put-text-property beg (point) 'mouse-face 'highlight)
1778 (insert "\n"))
1779 ;; We need only vector part.
1780 (setq translations (cdr translations))
1781 ;; Insert every 10 elements with indices in a line.
1782 (let ((len (length translations))
1783 (i 0)
1784 num)
1785 (while (< i len)
1786 (when (zerop (% i 10))
1787 (when (>= i 10)
1788 (insert "\n")
1789 (indent-to indent))
1790 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
1791 ;; We show the last digit of FROM while converting
1792 ;; 0,1,..,9 to 1,2,..,0.
1793 (insert (format " %d." (% (1+ i) 10)))
1794 (setq beg (point))
1795 (insert (aref translations i))
1796 ;; Passing the mouse over a character will highlight.
1797 (put-text-property beg (point) 'mouse-face 'highlight)
1798 (setq i (1+ i)))
1799 (insert "\n")))))
1800
1801 ;; Choose a completion in *Quail Completions* buffer with mouse-2.
1802
1803 (defun quail-mouse-choose-completion (event)
1804 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
1805 (interactive "e")
1806 ;; This function is an exact copy of the mouse.el function
1807 ;; `mouse-choose-completion' except that we:
1808 ;; 1) add two lines from `choose-completion' in simple.el to give
1809 ;; the `mouse-2' click a little more leeway.
1810 ;; 2) don't bury *Quail Completions* buffer so comment a section, and
1811 ;; 3) delete/terminate the current quail selection here.
1812 ;; Give temporary modes such as isearch a chance to turn off.
1813 (run-hooks 'mouse-leave-buffer-hook)
1814 (let ((buffer (window-buffer))
1815 choice
1816 base-size)
1817 (save-excursion
1818 (set-buffer (window-buffer (posn-window (event-start event))))
1819 (if completion-reference-buffer
1820 (setq buffer completion-reference-buffer))
1821 (setq base-size completion-base-size)
1822 (save-excursion
1823 (goto-char (posn-point (event-start event)))
1824 (let (beg end)
1825 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1826 (setq end (point) beg (1+ (point))))
1827 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
1828 (setq end (1- (point)) beg (point)))
1829 (if (null beg)
1830 (quail-error "No completion here"))
1831 (setq beg (previous-single-property-change beg 'mouse-face))
1832 (setq end (or (next-single-property-change end 'mouse-face)
1833 (point-max)))
1834 (setq choice (buffer-substring beg end)))))
1835 ; (let ((owindow (selected-window)))
1836 ; (select-window (posn-window (event-start event)))
1837 ; (if (and (one-window-p t 'selected-frame)
1838 ; (window-dedicated-p (selected-window)))
1839 ; ;; This is a special buffer's frame
1840 ; (iconify-frame (selected-frame))
1841 ; (or (window-dedicated-p (selected-window))
1842 ; (bury-buffer)))
1843 ; (select-window owindow))
1844 (quail-delete-region)
1845 (quail-choose-completion-string choice buffer base-size)
1846 (quail-terminate-translation)))
1847
1848 ;; Modify the simple.el function `choose-completion-string', because
1849 ;; the simple.el function `choose-completion-delete-max-match' breaks
1850 ;; on Mule data, since the semantics of `forward-char' have changed.
1851
1852 (defun quail-choose-completion-string (choice &optional buffer base-size)
1853 (let ((buffer (or buffer completion-reference-buffer)))
1854 ;; If BUFFER is a minibuffer, barf unless it's the currently
1855 ;; active minibuffer.
1856 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
1857 (or (not (active-minibuffer-window))
1858 (not (equal buffer
1859 (window-buffer (active-minibuffer-window))))))
1860 (quail-error "Minibuffer is not active for completion")
1861 ;; Store the completion in `quail-current-str', which will later
1862 ;; be converted to a character event list, then inserted into
1863 ;; the buffer where completion was requested.
1864 (set-buffer buffer)
1865 ; (if base-size
1866 ; (delete-region (+ base-size (point-min)) (point))
1867 ; (choose-completion-delete-max-match choice))
1868 (setq quail-current-str choice)
1869 ;; Update point in the window that BUFFER is showing in.
1870 (let ((window (get-buffer-window buffer t)))
1871 (set-window-point window (point)))
1872 ;; If completing for the minibuffer, exit it with this choice.
1873 (and (not completion-no-auto-exit)
1874 (equal buffer (window-buffer (minibuffer-window)))
1875 minibuffer-completion-table
1876 ;; If this is reading a file name, and the file name chosen
1877 ;; is a directory, don't exit the minibuffer.
1878 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
1879 (file-directory-p (buffer-string)))
1880 (select-window (active-minibuffer-window))
1881 (exit-minibuffer))))))
1882
1883 (defun quail-help ()
1884 "Show brief description of the current Quail package."
1885 (interactive)
1886 (let ((package quail-current-package))
1887 (with-output-to-temp-buffer "*Quail-Help*"
1888 (save-excursion
1889 (set-buffer standard-output)
1890 (let ((quail-current-package package))
1891 (insert "Quail input method (name:"
1892 (quail-name)
1893 ", mode line indicator:["
1894 (quail-title)
1895 "])\n---- Documentation ----\n"
1896 (quail-docstring))
1897 (newline)
1898 (if (quail-show-layout) (quail-show-kbd-layout))
1899 (quail-help-insert-keymap-description
1900 (quail-translation-keymap)
1901 "--- Key bindings (while translating) ---
1902 key binding
1903 --- -------\n")
1904 (if (quail-conversion-keymap)
1905 (quail-help-insert-keymap-description
1906 (quail-conversion-keymap)
1907 "--- Key bindings (while converting) ---
1908 key binding
1909 --- -------\n"))
1910 (help-mode))))))
1911
1912 (defun quail-help-insert-keymap-description (keymap &optional header)
1913 (let (from to)
1914 (if header
1915 (insert header))
1916 (save-excursion
1917 (save-window-excursion
1918 (let ((overriding-terminal-local-map keymap))
1919 (describe-bindings))
1920 (set-buffer "*Help*")
1921 (goto-char (point-min))
1922 (forward-line 4)
1923 (setq from (point))
1924 (search-forward "Global Bindings:" nil 'move)
1925 (beginning-of-line)
1926 (setq to (point))))
1927 (insert-buffer-substring "*Help*" from to)))
1928
1929 (defun quail-show-kbd-layout ()
1930 "Show keyboard layout with key tops of multilingual characters."
1931 (insert "--- Keyboard layout ---\n")
1932 (let ((blink-matching-paren nil)
1933 (i 0)
1934 ch)
1935 (while (< i quail-keyboard-layout-len)
1936 (if (= (% i 30) 0)
1937 (progn
1938 (newline)
1939 (indent-to (/ i 30)))
1940 (if (= (% i 2) 0)
1941 (insert " ")))
1942 (setq ch (aref quail-keyboard-layout i))
1943 (when (and (quail-kbd-translate)
1944 (/= ch ?\ ))
1945 ;; This is the case that the current input method simulates
1946 ;; some keyboard layout (which means it requires keyboard
1947 ;; translation) and a key at location `i' exists on users
1948 ;; keyboard. We must translate that key by
1949 ;; `quail-keyboard-layout-standard'. But if if there's no
1950 ;; corresponding key in that standard layout, we must simulate
1951 ;; what is inserted if that key is pressed by setting CH a
1952 ;; minus value.
1953 (setq ch (aref quail-keyboard-layout-standard i))
1954 (if (= ch ?\ )
1955 (setq ch (- (aref quail-keyboard-layout i)))))
1956 (if (< ch 0)
1957 (let ((last-command-event (- ch)))
1958 (self-insert-command 1))
1959 (if (= ch ?\ )
1960 (insert ch)
1961 (let* ((map (cdr (assq ch (cdr (quail-map)))))
1962 (translation (and map (quail-get-translation
1963 (car map) (char-to-string ch) 1))))
1964 (if (integerp translation)
1965 (insert translation)
1966 (if (consp translation)
1967 (insert (aref (cdr translation) (car (car translation))))
1968 (let ((last-command-event ch))
1969 (self-insert-command 1)))))))
1970 (setq i (1+ i))))
1971 (newline))
1972
1973 (defun quail-translation-help ()
1974 "Show help message while translating in Quail input method."
1975 (interactive)
1976 (if (not (eq this-command last-command))
1977 (let (state-msg keymap)
1978 (if (and quail-converting (= (length quail-current-key) 0))
1979 (setq state-msg
1980 (format "Converting string %S by input method %S.\n"
1981 quail-conversion-str (quail-name))
1982 keymap (quail-conversion-keymap))
1983 (setq state-msg
1984 (format "Translating key sequence %S by input method %S.\n"
1985 quail-current-key (quail-name))
1986 keymap (quail-translation-keymap)))
1987 (with-output-to-temp-buffer "*Quail-Help*"
1988 (save-excursion
1989 (set-buffer standard-output)
1990 (insert state-msg)
1991 (quail-help-insert-keymap-description
1992 keymap
1993 "-----------------------
1994 key binding
1995 --- -------\n")
1996 (help-mode)))))
1997 (let (scroll-help)
1998 (save-selected-window
1999 (select-window (get-buffer-window "*Quail-Help*"))
2000 (if (eq this-command last-command)
2001 (if (< (window-end) (point-max))
2002 (scroll-up)
2003 (if (> (window-start) (point-min))
2004 (set-window-start (selected-window) (point-min)))))
2005 (setq scroll-help
2006 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2007 "Type \\[quail-translation-help] to scroll up the help"
2008 (if (> (window-start) (point-min))
2009 "Type \\[quail-translation-help] to see the head of help"))))
2010 (if scroll-help
2011 (progn
2012 (message "%s" (substitute-command-keys scroll-help))
2013 (sit-for 1)
2014 (message nil)
2015 (quail-update-guidance)
2016 ))))
2017 \f
2018 ;; Quail map generator from state transition table.
2019
2020 (defun quail-map-from-table (table)
2021 "Make quail map from state transition table TABLE.
2022
2023 TABLE is an alist, the form is:
2024 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2025
2026 STATE-n are symbols to denote state. STATE-0 is the initial state.
2027
2028 TRANSITION-n-m are transition rules from STATE-n, and have the form
2029 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2030 RULES is a symbol whose value is an alist of keys \(string) vs the
2031 correponding characters or strings. The format of the symbol value of
2032 RULES is the same as arguments to `quail-define-rules'.
2033
2034 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2035 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2036 transition is allowed, i.e. STATE-x may be STATE-n.
2037
2038 If TRANSITION-n-m has the form RULES, the transition terminates
2039 when keys in RULES are input.
2040
2041 The generated map can be set for the current Quail package by the
2042 function `quail-install-map' (which see)."
2043 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2044 tail elt)
2045 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2046 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2047 ;; Set key sequence mapping rules in cdr part of each element.
2048 (while table
2049 (quail-map-from-table-1 state-alist (car table))
2050 (setq table (cdr table)))
2051
2052 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2053 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2054 ;; them with MAPPING-RULES of STATE-x to make elements of
2055 ;; STATE-ALIST valid Quail maps.
2056 (setq tail state-alist)
2057 (while tail
2058 (setq elt (car tail) tail (cdr tail))
2059 (quail-map-from-table-2 state-alist elt))
2060
2061 ;; Return the Quail map for the initial state.
2062 (car state-alist)))
2063
2064 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2065 ;; mapping rules in the element of STATE-ALIST that corresponds to
2066 ;; STATE according to TRANSITION ...
2067 (defun quail-map-from-table-1 (state-alist state-info)
2068 (let* ((state (car state-info))
2069 (map (assq state state-alist))
2070 (transitions (cdr state-info))
2071 elt)
2072 (while transitions
2073 (setq elt (car transitions) transitions (cdr transitions))
2074 (let (rules dst-state key trans)
2075 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2076 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2077 (if (consp elt)
2078 (setq rules (symbol-value (car elt))
2079 ;; Set (STATE-x) as branches for all keys in RULES.
2080 ;; It is replaced with actual branches for STATE-x
2081 ;; later in `quail-map-from-table-2'.
2082 dst-state (list (cdr elt)))
2083 (setq rules (symbol-value elt)))
2084 (while rules
2085 (setq key (car (car rules)) trans (cdr (car rules))
2086 rules (cdr rules))
2087 (if (stringp trans)
2088 (if (= (length trans) 1)
2089 (setq trans (aref trans 0))
2090 (setq trans (string-to-vector trans))))
2091 (set-nested-alist key trans map nil dst-state))))))
2092
2093 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2094 ;; the form is:
2095 ;; (STATE (CHAR NESTED-ALIST) ...)
2096 ;; NESTED-ALIST is a nested alist; the form is:
2097 ;; (TRANS (CHAR NESTED-ALIST) ...)
2098 ;; or
2099 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2100 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2101 ;; (cdr (assq STATE-x STATE-ALIST))
2102
2103 (defun quail-map-from-table-2 (state-alist element)
2104 (let ((prev element)
2105 (tail (cdr element))
2106 elt)
2107 (while (cdr tail)
2108 (setq elt (car tail) prev tail tail (cdr tail))
2109 (quail-map-from-table-2 state-alist (cdr elt)))
2110 (setq elt (car tail))
2111 (if (consp elt)
2112 (quail-map-from-table-2 state-alist (cdr elt))
2113 (setcdr prev (cdr (assq elt state-alist))))))
2114
2115 ;; Concatenate translations for all heading substrings of KEY in the
2116 ;; current Quail map. Here, `heading substring' means (substring KEY
2117 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2118 (defun quail-lookup-map-and-concat (key)
2119 (let* ((len (length key))
2120 (translation-list nil)
2121 map)
2122 (while (> len 0)
2123 (setq map (quail-lookup-key key len)
2124 len (1- len))
2125 (if map
2126 (let* ((def (quail-map-definition map))
2127 (trans (if (consp def) (aref (cdr def) (car (car def)))
2128 def)))
2129 (if (integerp trans)
2130 (setq trans (char-to-string trans)))
2131 (setq translation-list (cons trans translation-list)))))
2132 (apply 'concat translation-list)))
2133
2134 \f
2135 (defvar quail-directory-name "quail"
2136 "Name of Quail directory which contains Quail packages.
2137 This is a sub-directory of LEIM directory.")
2138
2139 ;;;###autoload
2140 (defun quail-update-leim-list-file (dirname &rest dirnames)
2141 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2142 DIRNAME is a directory containing Emacs input methods;
2143 normally, it should specify the `leim' subdirectory
2144 of the Emacs source tree.
2145
2146 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2147 and update the file \"leim-list.el\" in DIRNAME.
2148
2149 When called from a program, the remaining arguments are additional
2150 directory names to search for Quail packages under `quail' subdirectory
2151 of each directory."
2152 (interactive "FDirectory of LEIM: ")
2153 (setq dirname (expand-file-name dirname))
2154 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2155 quail-dirs list-buf pkg-list pkg-buf pos)
2156 (if (not (file-writable-p leim-list))
2157 (error "Can't write to file \"%s\"" leim-list))
2158 (message "Updating %s ..." leim-list)
2159 (setq list-buf (find-file-noselect leim-list))
2160
2161 ;; At first, clean up the file.
2162 (save-excursion
2163 (set-buffer list-buf)
2164 (goto-char 1)
2165
2166 ;; Insert the correct header.
2167 (if (looking-at (regexp-quote leim-list-header))
2168 (goto-char (match-end 0))
2169 (insert leim-list-header))
2170 (setq pos (point))
2171 (if (not (re-search-forward leim-list-entry-regexp nil t))
2172 nil
2173
2174 ;; Remove garbages after the header.
2175 (goto-char (match-beginning 0))
2176 (if (< pos (point))
2177 (delete-region pos (point)))
2178
2179 ;; Remove all entries for Quail.
2180 (while (re-search-forward leim-list-entry-regexp nil 'move)
2181 (goto-char (match-beginning 0))
2182 (setq pos (point))
2183 (condition-case nil
2184 (let ((form (read list-buf)))
2185 (when (equal (nth 3 form) ''quail-use-package)
2186 (if (eolp) (forward-line 1))
2187 (delete-region pos (point))))
2188 (error
2189 ;; Delete the remaining contents because it seems that
2190 ;; this file is broken.
2191 (message "Garbage in %s deleted" leim-list)
2192 (delete-region pos (point-max)))))))
2193
2194 ;; Search for `quail' subdirectory under each DIRNAMES.
2195 (setq dirnames (cons dirname dirnames))
2196 (let ((l dirnames))
2197 (while l
2198 (setcar l (expand-file-name (car l)))
2199 (setq dirname (expand-file-name quail-directory-name (car l)))
2200 (if (file-readable-p dirname)
2201 (setq quail-dirs (cons dirname quail-dirs))
2202 (message "%s doesn't have `%s' subdirectory, just ignored"
2203 (car l) quail-directory-name)
2204 (setq quail-dirs (cons nil quail-dirs)))
2205 (setq l (cdr l)))
2206 (setq quail-dirs (nreverse quail-dirs)))
2207
2208 ;; Insert input method registering forms.
2209 (while quail-dirs
2210 (setq dirname (car quail-dirs))
2211 (when dirname
2212 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2213 (while pkg-list
2214 (message "Checking %s ..." (car pkg-list))
2215 (with-temp-buffer
2216 (insert-file-contents (car pkg-list))
2217 (goto-char (point-min))
2218 (while (search-forward "(quail-define-package" nil t)
2219 (goto-char (match-beginning 0))
2220 (condition-case nil
2221 (let ((form (read (current-buffer))))
2222 (save-excursion
2223 (set-buffer list-buf)
2224 (insert
2225 (format "(register-input-method
2226 %S %S '%s
2227 %S %S
2228 %S)\n"
2229 (nth 1 form) ; PACKAGE-NAME
2230 (nth 2 form) ; LANGUAGE
2231 'quail-use-package ; ACTIVATE-FUNC
2232 (nth 3 form) ; PACKAGE-TITLE
2233 (progn ; PACKAGE-DESCRIPTION (one line)
2234 (string-match ".*" (nth 5 form))
2235 (match-string 0 (nth 5 form)))
2236 (file-relative-name ; PACKAGE-FILENAME
2237 (file-name-sans-extension (car pkg-list))
2238 (car dirnames))))))
2239 (error
2240 ;; Ignore the remaining contents of this file.
2241 (goto-char (point-max))
2242 (message "Some part of \"%s\" is broken" dirname)))))
2243 (setq pkg-list (cdr pkg-list)))
2244 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2245
2246 ;; At last, write out LEIM list file.
2247 (save-excursion
2248 (set-buffer list-buf)
2249 (setq buffer-file-coding-system 'iso-2022-7bit)
2250 (save-buffer 0))
2251 (kill-buffer list-buf)
2252 (message "Updating %s ... done" leim-list)))
2253 ;;
2254 (provide 'quail)
2255
2256 ;;; quail.el ends here