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