]> code.delx.au - gnu-emacs/blob - lisp/international/quail.el
(translation-table-for-input): New
[gnu-emacs] / lisp / international / quail.el
1 ;;; quail.el --- provides simple input method for multilingual text
2
3 ;; Copyright (C) 1995, 2000 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2001 Free Software Foundation, Inc.
6
7 ;; Author: Kenichi HANDA <handa@etl.go.jp>
8 ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
9 ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
10 ;; Keywords: mule, multilingual, input method
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; In Quail minor mode, you can input multilingual text easily. By
32 ;; defining a translation table (named Quail map) which maps ASCII key
33 ;; string to multilingual character or string, you can input any text
34 ;; from ASCII keyboard.
35 ;;
36 ;; We use words "translation" and "conversion" differently. The
37 ;; former is done by Quail package itself, the latter is the further
38 ;; process of converting a translated text to some more desirable
39 ;; text. For instance, Quail package for Japanese (`quail-jp')
40 ;; translates Roman text (transliteration of Japanese in Latin
41 ;; alphabets) to Hiragana text, which is then converted to
42 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
43 ;; CONVERSION-KEYS argument of the Quail package.
44
45 ;;; Code:
46
47 (require 'help-mode)
48
49 (defgroup quail nil
50 "Quail: multilingual input method."
51 :group 'leim)
52
53 ;; Buffer local variables
54
55 (defvar quail-current-package nil
56 "The current Quail package, which depends on the current input method.
57 See the documentation of `quail-package-alist' for the format.")
58 (make-variable-buffer-local 'quail-current-package)
59 (put 'quail-current-package 'permanent-local t)
60
61 ;; Quail uses the following two buffers to assist users.
62 ;; A buffer to show available key sequence or translation list.
63 (defvar quail-guidance-buf nil)
64 ;; A buffer to show completion list of the current key sequence.
65 (defvar quail-completion-buf nil)
66
67 ;; Each buffer in which Quail is activated should use different
68 ;; guidance buffers.
69 (make-variable-buffer-local 'quail-guidance-buf)
70 (put 'quail-guidance-buf 'permanent-local t)
71
72 ;; A main window showing Quail guidance buffer.
73 (defvar quail-guidance-win nil)
74 (make-variable-buffer-local 'quail-guidance-win)
75
76 (defvar quail-overlay nil
77 "Overlay which covers the current translation region of Quail.")
78 (make-variable-buffer-local 'quail-overlay)
79
80 (defvar quail-conv-overlay nil
81 "Overlay which covers the text to be converted in Quail mode.")
82 (make-variable-buffer-local 'quail-conv-overlay)
83
84 (defvar quail-current-key nil
85 "Current key for translation in Quail mode.")
86 (make-variable-buffer-local 'quail-current-key)
87
88 (defvar quail-current-str nil
89 "Currently selected translation of the current key.")
90 (make-variable-buffer-local 'quail-current-str)
91
92 (defvar quail-current-translations nil
93 "Cons of indices and vector of possible translations of the current key.
94 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
95 CURRENT is an index of the current translation,
96 START and END are indices of the start and end of the current block,
97 BLOCK is the current block index,
98 BLOCKS is a number of blocks of translation.")
99 (make-variable-buffer-local 'quail-current-translations)
100
101 (defvar quail-current-data nil
102 "Any Lisp object holding information of current translation status.
103 When a key sequence is mapped to TRANS and TRANS is a cons
104 of actual translation and some Lisp object to be referred
105 for translating the longer key sequence, this variable is set
106 to that Lisp object.")
107 (make-variable-buffer-local 'quail-current-data)
108
109 ;; Quail package handlers.
110
111 (defvar quail-package-alist nil
112 "List of Quail packages.
113 A Quail package is a list of these elements:
114 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
115 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
116 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
117 CONVERSION-KEYS, SIMPLE.
118
119 QUAIL-MAP is a data structure to map key strings to translations. For
120 the format, see the documentation of `quail-map-p'.
121
122 DECODE-MAP is an alist of translations and corresponding keys.
123
124 See the documentation of `quail-define-package' for the other elements.")
125
126 ;; Return various slots in the current quail-package.
127
128 (defsubst quail-name ()
129 "Return the name of the current Quail package."
130 (nth 0 quail-current-package))
131 ;;;###autoload
132 (defun quail-title ()
133 "Return the title of the current Quail package."
134 (let ((title (nth 1 quail-current-package)))
135 ;; TITLE may be a string or a list. If it is a list, each element
136 ;; is a string or the form (VAR STR1 STR2), and the interpretation
137 ;; of the list is the same as that of mode-line-format.
138 (if (stringp title)
139 title
140 (condition-case nil
141 (mapconcat
142 (lambda (x)
143 (cond ((stringp x) x)
144 ((and (listp x) (symbolp (car x)) (= (length x) 3))
145 (if (symbol-value (car x))
146 (nth 1 x) (nth 2 x)))
147 (t "")))
148 title "")
149 (error "")))))
150 (defsubst quail-map ()
151 "Return the translation map of the current Quail package."
152 (nth 2 quail-current-package))
153 (defsubst quail-guidance ()
154 "Return an object used for `guidance' feature of the current Quail package.
155 See also the documentation of `quail-define-package'."
156 (nth 3 quail-current-package))
157 (defsubst quail-docstring ()
158 "Return the documentation string of the current Quail package."
159 (nth 4 quail-current-package))
160 (defsubst quail-translation-keymap ()
161 "Return translation keymap in the current Quail package.
162 Translation keymap is a keymap used while translation region is active."
163 (nth 5 quail-current-package))
164 (defsubst quail-forget-last-selection ()
165 "Return `forget-last-selection' flag of the current Quail package.
166 See also the documentation of `quail-define-package'."
167 (nth 6 quail-current-package))
168 (defsubst quail-deterministic ()
169 "Return `deterministic' flag of the current Quail package.
170 See also the documentation of `quail-define-package'."
171 (nth 7 quail-current-package))
172 (defsubst quail-kbd-translate ()
173 "Return `kbd-translate' flag of the current Quail package.
174 See also the documentation of `quail-define-package'."
175 (nth 8 quail-current-package))
176 (defsubst quail-show-layout ()
177 "Return `show-layout' flag of the current Quail package.
178 See also the documentation of `quail-define-package'."
179 (nth 9 quail-current-package))
180 (defsubst quail-decode-map ()
181 "Return decode map of the current Quail package.
182 It is an alist of translations and corresponding keys."
183 (nth 10 quail-current-package))
184 (defsubst quail-maximum-shortest ()
185 "Return `maximum-shortest' flag of the current Quail package.
186 See also the documentation of `quail-define-package'."
187 (nth 11 quail-current-package))
188 (defsubst quail-overlay-plist ()
189 "Return property list of an overly used in the current Quail package."
190 (nth 12 quail-current-package))
191 (defsubst quail-update-translation-function ()
192 "Return a function for updating translation in the current Quail package."
193 (nth 13 quail-current-package))
194 (defsubst quail-conversion-keymap ()
195 "Return conversion keymap in the current Quail package.
196 Conversion keymap is a keymap used while conversion region is active
197 but translation region is not active."
198 (nth 14 quail-current-package))
199 (defsubst quail-simple ()
200 "Return t if the current Quail package is simple."
201 (nth 15 quail-current-package))
202
203 (defsubst quail-package (name)
204 "Return Quail package named NAME."
205 (assoc name quail-package-alist))
206
207 (defun quail-add-package (package)
208 "Add Quail package PACKAGE to `quail-package-alist'."
209 (let ((pac (quail-package (car package))))
210 (if pac
211 (setcdr pac (cdr package))
212 (setq quail-package-alist (cons package quail-package-alist)))))
213
214 (defun quail-select-package (name)
215 "Select Quail package named NAME as the current Quail package."
216 (let ((package (quail-package name)))
217 (if (null package)
218 (error "No Quail package `%s'" name))
219 (setq quail-current-package package)
220 (setq-default quail-current-package package)
221 name))
222
223 ;;;###autoload
224 (defun quail-use-package (package-name &rest libraries)
225 "Start using Quail package PACKAGE-NAME.
226 The remaining arguments are libraries to be loaded before using the package.
227
228 This activates input method defined by PACKAGE-NAME by running
229 `quail-activate', which see."
230 (let ((package (quail-package package-name)))
231 (if (null package)
232 ;; Perhaps we have not yet loaded necessary libraries.
233 (while libraries
234 (if (not (load (car libraries) t))
235 (progn
236 (with-output-to-temp-buffer "*Help*"
237 (princ "Quail package \"")
238 (princ package-name)
239 (princ "\" can't be activated\n because library \"")
240 (princ (car libraries))
241 (princ "\" is not in `load-path'.
242
243 The most common case is that you have not yet installed appropriate
244 libraries in LEIM (Libraries of Emacs Input Method) which is
245 distributed separately from Emacs.
246
247 LEIM is available from the same ftp directory as Emacs."))
248 (error "Can't use the Quail package `%s'" package-name))
249 (setq libraries (cdr libraries))))))
250 (quail-select-package package-name)
251 (setq current-input-method-title (quail-title))
252 (quail-activate)
253 ;; Hide all '... loaded' message.
254 (message nil))
255
256 (defvar quail-translation-keymap
257 (let ((map (make-keymap))
258 (i 0))
259 (while (< i ?\ )
260 (define-key map (char-to-string i) 'quail-other-command)
261 (setq i (1+ i)))
262 (while (< i 127)
263 (define-key map (char-to-string i) 'quail-self-insert-command)
264 (setq i (1+ i)))
265 (setq i 128)
266 (while (< i 256)
267 (define-key map (vector i) 'quail-self-insert-command)
268 (setq i (1+ i)))
269 (define-key map "\177" 'quail-delete-last-char)
270 (define-key map "\C-f" 'quail-next-translation)
271 (define-key map "\C-b" 'quail-prev-translation)
272 (define-key map "\C-n" 'quail-next-translation-block)
273 (define-key map "\C-p" 'quail-prev-translation-block)
274 (define-key map [right] 'quail-next-translation)
275 (define-key map [left] 'quail-prev-translation)
276 (define-key map [down] 'quail-next-translation-block)
277 (define-key map [up] 'quail-prev-translation-block)
278 (define-key map "\C-i" 'quail-completion)
279 (define-key map "\C-@" 'quail-select-current)
280 ;; Following simple.el, Enter key on numeric keypad selects the
281 ;; current translation just like `C-SPC', and `mouse-2' chooses
282 ;; any completion visible in the *Quail Completions* buffer.
283 (define-key map [kp-enter] 'quail-select-current)
284 (define-key map [mouse-2] 'quail-mouse-choose-completion)
285 (define-key map [down-mouse-2] nil)
286 (define-key map "\C-h" 'quail-translation-help)
287 (define-key map [?\C- ] 'quail-select-current)
288 (define-key map [tab] 'quail-completion)
289 (define-key map [delete] 'quail-delete-last-char)
290 (define-key map [backspace] 'quail-delete-last-char)
291 map)
292 "Keymap used processing translation in complex Quail modes.
293 Only a few especially complex input methods use this map;
294 most use `quail-simple-translation-keymap' instead.
295 This map is activated while translation region is active.")
296
297 (defvar quail-translation-docstring
298 "When you type keys, the echo area shows the possible characters
299 which correspond to that key sequence, each preceded by a digit. You
300 can select one of the characters shown by typing the corresponding
301 digit. Alternatively, you can use C-f and C-b to move through the
302 line to select the character you want, then type a letter to begin
303 entering another Chinese character or type a space or punctuation
304 character.
305
306 If there are more than ten possible characters for the given spelling,
307 the echo area shows ten characters at a time; you can use C-n to move
308 to the next group of ten, and C-p to move back to the previous group
309 of ten.")
310
311 ;; Categorize each Quail commands to make the output of quail-help
312 ;; concise. This is done by putting `quail-help' property. The value
313 ;; is:
314 ;; hide -- never show this command
315 ;; non-deterministic -- show only for non-deterministic input method
316 (let ((l '((quail-other-command . hide)
317 (quail-self-insert-command . hide)
318 (quail-delete-last-char . hide)
319 (quail-next-translation . non-deterministic)
320 (quail-prev-translation . non-deterministic)
321 (quail-next-translation-block . non-deterministic)
322 (quail-prev-translation-block . non-deterministic))))
323 (while l
324 (put (car (car l)) 'quail-help (cdr (car l)))
325 (setq l (cdr l))))
326
327 (defvar quail-simple-translation-keymap
328 (let ((map (make-keymap))
329 (i 0))
330 (while (< i ?\ )
331 (define-key map (char-to-string i) 'quail-other-command)
332 (setq i (1+ i)))
333 (while (< i 127)
334 (define-key map (char-to-string i) 'quail-self-insert-command)
335 (setq i (1+ i)))
336 (setq i 128)
337 (while (< i 256)
338 (define-key map (vector i) 'quail-self-insert-command)
339 (setq i (1+ i)))
340 (define-key map "\177" 'quail-delete-last-char)
341 (define-key map [delete] 'quail-delete-last-char)
342 (define-key map [backspace] 'quail-delete-last-char)
343 ;;(let ((meta-map (make-sparse-keymap)))
344 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
345 ;;(define-key map [escape] meta-map))
346 map)
347 "Keymap used while processing translation in simple Quail modes.
348 A few especially complex input methods use `quail-translation-keymap' instead.
349 This map is activated while translation region is active.")
350
351 (defvar quail-conversion-keymap
352 (let ((map (make-keymap))
353 (i ?\ ))
354 (while (< i 127)
355 (define-key map (char-to-string i) 'quail-self-insert-command)
356 (setq i (1+ i)))
357 (setq i 128)
358 (while (< i 256)
359 (define-key map (vector i) 'quail-self-insert-command)
360 (setq i (1+ i)))
361 (define-key map "\C-b" 'quail-conversion-backward-char)
362 (define-key map "\C-f" 'quail-conversion-forward-char)
363 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
364 (define-key map "\C-e" 'quail-conversion-end-of-region)
365 (define-key map "\C-d" 'quail-conversion-delete-char)
366 (define-key map "\C-k" 'quail-conversion-delete-tail)
367 (define-key map "\C-h" 'quail-translation-help)
368 (define-key map "\177" 'quail-conversion-backward-delete-char)
369 (define-key map [delete] 'quail-conversion-backward-delete-char)
370 (define-key map [backspace] 'quail-conversion-backward-delete-char)
371 map)
372 "Keymap used for processing conversion in Quail mode.
373 This map is activated while conversion region is active but translation
374 region is not active.")
375
376 ;; Just a dummy definition.
377 (defun quail-other-command ()
378 (interactive)
379 )
380
381 ;;;###autoload
382 (defun quail-define-package (name language title
383 &optional guidance docstring translation-keys
384 forget-last-selection deterministic
385 kbd-translate show-layout create-decode-map
386 maximum-shortest overlay-plist
387 update-translation-function
388 conversion-keys simple)
389 "Define NAME as a new Quail package for input LANGUAGE.
390 TITLE is a string to be displayed at mode-line to indicate this package.
391 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
392 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
393 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
394 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
395
396 GUIDANCE specifies how a guidance string is shown in echo area.
397 If it is t, list of all possible translations for the current key is shown
398 with the currently selected translation being highlighted.
399 If it is an alist, the element has the form (CHAR . STRING). Each character
400 in the current key is searched in the list and the corresponding string is
401 shown.
402 If it is nil, the current key is shown.
403
404 DOCSTRING is the documentation string of this package. The command
405 `describe-input-method' shows this string while replacing the form
406 \\=\\<VAR> in the string by the value of VAR. That value should be a
407 string. For instance, the form \\=\\<quail-translation-docstring> is
408 replaced by a description about how to select a translation from a
409 list of candidates.
410
411 TRANSLATION-KEYS specifies additional key bindings used while translation
412 region is active. It is an alist of single key character vs. corresponding
413 command to be called.
414
415 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
416 for the future to translate the same key. If this flag is nil, a
417 translation selected for a key is remembered so that it can be the
418 first candidate when the same key is entered later.
419
420 DETERMINISTIC non-nil means the first candidate of translation is
421 selected automatically without allowing users to select another
422 translation for a key. In this case, unselected translations are of
423 no use for an interactive use of Quail but can be used by some other
424 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
425 to t.
426
427 KBD-TRANSLATE non-nil means input characters are translated from a
428 user's keyboard layout to the standard keyboard layout. See the
429 documentation of `quail-keyboard-layout' and
430 `quail-keyboard-layout-standard' for more detail.
431
432 SHOW-LAYOUT non-nil means the `quail-help' command should show
433 the user's keyboard layout visually with translated characters.
434 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
435 this package defines no translations for single character keys.
436
437 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
438 map is an alist of translations and corresponding original keys.
439 Although this map is not used by Quail itself, it can be used by some
440 other programs. For instance, Vietnamese supporting needs this map to
441 convert Vietnamese text to VIQR format which uses only ASCII
442 characters to represent Vietnamese characters.
443
444 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
445 length of the shortest sequence. When we don't have a translation of
446 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
447 the key at \"..AB\" and start translation of \"CD..\". Hangul
448 packages, for instance, use this facility. If this flag is nil, we
449 break the key just at \"..ABC\" and start translation of \"D..\".
450
451 OVERLAY-PLIST if non-nil is a property list put on an overlay which
452 covers Quail translation region.
453
454 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
455 the current translation region according to a new translation data. By
456 default, a translated text or a user's key sequence (if no translation
457 for it) is inserted.
458
459 CONVERSION-KEYS specifies additional key bindings used while
460 conversion region is active. It is an alist of single key character
461 vs. corresponding command to be called.
462
463 If SIMPLE is non-nil, then we do not alter the meanings of
464 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
465 non-Quail commands."
466 (let (translation-keymap conversion-keymap)
467 (if deterministic (setq forget-last-selection t))
468 (if translation-keys
469 (progn
470 (setq translation-keymap (copy-keymap
471 (if simple quail-simple-translation-keymap
472 quail-translation-keymap)))
473 (while translation-keys
474 (define-key translation-keymap
475 (car (car translation-keys)) (cdr (car translation-keys)))
476 (setq translation-keys (cdr translation-keys))))
477 (setq translation-keymap
478 (if simple quail-simple-translation-keymap
479 quail-translation-keymap)))
480 (when conversion-keys
481 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
482 (while conversion-keys
483 (define-key conversion-keymap
484 (car (car conversion-keys)) (cdr (car conversion-keys)))
485 (setq conversion-keys (cdr conversion-keys))))
486 (quail-add-package
487 (list name title (list nil) guidance (or docstring "")
488 translation-keymap
489 forget-last-selection deterministic kbd-translate show-layout
490 (if create-decode-map (list 'decode-map) nil)
491 maximum-shortest overlay-plist update-translation-function
492 conversion-keymap simple))
493
494 ;; Update input-method-alist.
495 (let ((slot (assoc name input-method-alist))
496 (val (list language 'quail-use-package title docstring)))
497 (if slot (setcdr slot val)
498 (setq input-method-alist (cons (cons name val) input-method-alist)))))
499
500 (quail-select-package name))
501
502 ;; Quail minor mode handlers.
503
504 ;; Setup overlays used in Quail mode.
505 (defun quail-setup-overlays (conversion-mode)
506 (let ((pos (point)))
507 (if (overlayp quail-overlay)
508 (move-overlay quail-overlay pos pos)
509 (setq quail-overlay (make-overlay pos pos nil nil t))
510 (if input-method-highlight-flag
511 (overlay-put quail-overlay 'face 'underline))
512 (let ((l (quail-overlay-plist)))
513 (while l
514 (overlay-put quail-overlay (car l) (car (cdr l)))
515 (setq l (cdr (cdr l))))))
516 (if conversion-mode
517 (if (overlayp quail-conv-overlay)
518 (if (not (overlay-start quail-conv-overlay))
519 (move-overlay quail-conv-overlay pos pos))
520 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
521 (if input-method-highlight-flag
522 (overlay-put quail-conv-overlay 'face 'underline))))))
523
524 ;; Delete overlays used in Quail mode.
525 (defun quail-delete-overlays ()
526 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
527 (delete-overlay quail-overlay))
528 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
529 (delete-overlay quail-conv-overlay)))
530
531 ;; Kill Quail guidance buffer. Set in kill-buffer-hook.
532 (defun quail-kill-guidance-buf ()
533 (if (buffer-live-p quail-guidance-buf)
534 (kill-buffer quail-guidance-buf)))
535
536 (defun quail-inactivate ()
537 "Inactivate Quail input method.
538
539 This function runs the normal hook `quail-inactivate-hook'."
540 (interactive)
541 (quail-activate -1))
542
543 (defun quail-activate (&optional arg)
544 "Activate Quail input method.
545 With arg, activate Quail input method if and only if arg is positive.
546
547 This function runs `quail-activate-hook' if it activates the input
548 method, `quail-inactivate-hook' if it deactivates it.
549
550 While this input method is active, the variable
551 `input-method-function' is bound to the function `quail-input-method'."
552 (if (and arg
553 (< (prefix-numeric-value arg) 0))
554 ;; Let's inactivate Quail input method.
555 (unwind-protect
556 (progn
557 (quail-hide-guidance-buf)
558 (quail-delete-overlays)
559 (setq describe-current-input-method-function nil)
560 (run-hooks 'quail-inactivate-hook))
561 (kill-local-variable 'input-method-function))
562 ;; Let's activate Quail input method.
563 (if (null quail-current-package)
564 ;; Quail package is not yet selected. Select one now.
565 (let (name)
566 (if quail-package-alist
567 (setq name (car (car quail-package-alist)))
568 (error "No Quail package loaded"))
569 (quail-select-package name)))
570 (setq inactivate-current-input-method-function 'quail-inactivate)
571 (setq describe-current-input-method-function 'quail-help)
572 (quail-delete-overlays)
573 (quail-show-guidance-buf)
574 ;; If we are in minibuffer, turn off the current input method
575 ;; before exiting.
576 (if (eq (selected-window) (minibuffer-window))
577 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
578 (add-hook 'kill-buffer-hook 'quail-kill-guidance-buf nil t)
579 (run-hooks 'quail-activate-hook)
580 (make-local-variable 'input-method-function)
581 (setq input-method-function 'quail-input-method)))
582
583 (defun quail-exit-from-minibuffer ()
584 (inactivate-input-method)
585 (if (<= (minibuffer-depth) 1)
586 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
587
588 ;; Keyboard layout translation handlers.
589
590 ;; Some Quail packages provide localized keyboard simulation which
591 ;; requires a particular keyboard layout. In this case, what we need
592 ;; is locations of keys the user entered, not character codes
593 ;; generated by those keys. However, for the moment, there's no
594 ;; common way to get such information. So, we ask a user to give
595 ;; information of his own keyboard layout, then translate it to the
596 ;; standard layout which we defined so that all Quail packages depend
597 ;; just on it.
598
599 (defconst quail-keyboard-layout-standard
600 "\
601 \
602 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
603 qQwWeErRtTyYuUiIoOpP[{]} \
604 aAsSdDfFgGhHjJkKlL;:'\"\\| \
605 zZxXcCvVbBnNmM,<.>/? \
606 "
607 "Standard keyboard layout of printable characters Quail assumes.
608 See the documentation of `quail-keyboard-layout' for this format.
609 This layout is almost the same as that of VT100,
610 but the location of key \\ (backslash) is just right of key ' (single-quote),
611 not right of RETURN key.")
612
613 (defvar quail-keyboard-layout quail-keyboard-layout-standard
614 "A string which represents physical key layout of a particular keyboard.
615 We assume there are six rows and each row has 15 keys (columns),
616 the first row is above the `1' - `0' row,
617 the first column of the second row is left of key `1',
618 the first column of the third row is left of key `q',
619 the first column of the fourth row is left of key `a',
620 the first column of the fifth row is left of key `z',
621 the sixth row is below the `z' - `/' row.
622 Nth (N is even) and (N+1)th characters in the string are non-shifted
623 and shifted characters respectively at the same location.
624 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
625 The command `quail-set-keyboard-layout' usually sets this variable.")
626
627 (defconst quail-keyboard-layout-len 180)
628
629 ;; Here we provide several examples of famous keyboard layouts.
630
631 (defvar quail-keyboard-layout-alist
632 (list
633 (cons "standard" quail-keyboard-layout-standard)
634 '("sun-type3" . "\
635 \
636 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
637 qQwWeErRtTyYuUiIoOpP[{]} \
638 aAsSdDfFgGhHjJkKlL;:'\" \
639 zZxXcCvVbBnNmM,<.>/? \
640 ")
641 '("atari-german" . "\
642 \
643 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
644 qQwWeErRtTzZuUiIoOpP\374\334+* \
645 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
646 <>yYxXcCvVbBnNmM,;.:-_ \
647 ")
648
649 '("pc102-de" . "\
650 \
651 ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
652 qQwWeErRtTzZuUiIoOpP\374\334+* \
653 aAsSdDfFgGhHjJkKlL\366\326\344\304 \
654 <>yYxXcCvVbBnNmM,;.:-_ \
655 ")
656
657 '("jp106" . "\
658 \
659 1!2\"3#4$5%6&7'8(9)0~-=^~\\| \
660 qQwWeErRtTyYuUiIoOpP@`[{ \
661 aAsSdDfFgGhHjJkKlL;+:*]} \
662 zZxXcCvVbBnNmM,<.>/?\\_ \
663 ")
664 )
665 "Alist of keyboard names and corresponding layout strings.
666 See the documentation of `quail-keyboard-layout' for the format of
667 the layout string.")
668
669 ;; A non-standard keyboard layout may miss some key locations of the
670 ;; standard layout while having additional key locations not in the
671 ;; standard layout. This alist maps those additional key locations to
672 ;; the missing locations. The value is updated automatically by
673 ;; quail-set-keyboard-layout.
674 (defvar quail-keyboard-layout-substitution nil)
675
676 (defun quail-update-keyboard-layout (kbd-type)
677 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
678 (if (null layout)
679 ;; Here, we had better ask a user to define his own keyboard
680 ;; layout interactively.
681 (error "Unknown keyboard type `%s'" kbd-type))
682 (setq quail-keyboard-layout (cdr layout))
683 (let ((i quail-keyboard-layout-len)
684 subst-list missing-list)
685 ;; Sum up additional key locations not in the standard layout in
686 ;; subst-list, and missing key locations in missing-list.
687 (while (> i 0)
688 (setq i (1- i))
689 (if (= (aref quail-keyboard-layout i) ? )
690 (if (/= (aref quail-keyboard-layout-standard i) ? )
691 (setq missing-list (cons i missing-list)))
692 (if (= (aref quail-keyboard-layout-standard i) ? )
693 (setq subst-list (cons (cons i nil) subst-list)))))
694 (setq quail-keyboard-layout-substitution subst-list)
695 ;; If there are additional key locations, map them to missing
696 ;; key locations.
697 (while missing-list
698 (while (and subst-list (cdr (car subst-list)))
699 (setq subst-list (cdr subst-list)))
700 (if subst-list
701 (setcdr (car subst-list) (car missing-list)))
702 (setq missing-list (cdr missing-list))))))
703
704 (defcustom quail-keyboard-layout-type "standard"
705 "Type of keyboard layout used in Quail base input method.
706 Available types are listed in the variable `quail-keyboard-layout-alist'."
707 :group 'quail
708 :type 'string
709 :set #'(lambda (symbol value)
710 (quail-update-keyboard-layout value)
711 (set symbol value)))
712
713 ;;;###autoload
714 (defun quail-set-keyboard-layout (kbd-type)
715 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
716
717 Since some Quail packages depends on a physical layout of keys (not
718 characters generated by them), those are created by assuming the
719 standard layout defined in `quail-keyboard-layout-standard'. This
720 function tells Quail system the layout of your keyboard so that what
721 you type is correctly handled."
722 (interactive
723 (let* ((completion-ignore-case t)
724 (type (completing-read "Keyboard type: "
725 quail-keyboard-layout-alist)))
726 (list type)))
727 (quail-update-keyboard-layout kbd-type)
728 (setq quail-keyboard-layout-type kbd-type))
729
730 (defun quail-keyboard-translate (char)
731 "Translate CHAR to the one in the standard keyboard layout."
732 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
733 ;; All Quail packages are designed based on
734 ;; `quail-keyboard-layout-standard'.
735 char
736 (let ((i 0))
737 ;; Find the key location on the current keyboard layout.
738 (while (and (< i quail-keyboard-layout-len)
739 (/= char (aref quail-keyboard-layout i)))
740 (setq i (1+ i)))
741 (if (= i quail-keyboard-layout-len)
742 ;; CHAR is not in quail-keyboard-layout, which means that a
743 ;; user typed a key which generated a character code to be
744 ;; handled out of Quail. Just return CHAR and make
745 ;; quail-execute-non-quail-command handle it correctly.
746 char
747 (let ((ch (aref quail-keyboard-layout-standard i)))
748 (if (= ch ?\ )
749 ;; This location not available in the standard keyboard
750 ;; layout. Check if the location is used to substitute
751 ;; for the other location of the standard layout.
752 (if (setq i (cdr (assq i quail-keyboard-layout-substitution)))
753 (aref quail-keyboard-layout-standard i)
754 ;; Just return CHAR as well as above.
755 char)
756 ch))))))
757
758 (defun quail-keyseq-translate (keyseq)
759 (apply 'string
760 (mapcar (function (lambda (x) (quail-keyboard-translate x)))
761 keyseq)))
762
763 (defun quail-insert-kbd-layout (kbd-layout)
764 "Insert the visual keyboard layout table according to KBD-LAYOUT.
765 The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
766 (let (done-list layout i ch)
767 ;; At first, convert KBD-LAYOUT to the same size vector that
768 ;; contains translated character or string.
769 (setq layout (string-to-vector kbd-layout)
770 i 0)
771 (while (< i quail-keyboard-layout-len)
772 (setq ch (aref kbd-layout i))
773 (if (quail-kbd-translate)
774 (setq ch (quail-keyboard-translate ch)))
775 (let* ((map (cdr (assq ch (cdr (quail-map)))))
776 (translation (and map (quail-get-translation
777 (car map) (char-to-string ch) 1))))
778 (if translation
779 (progn
780 (if (consp translation)
781 (setq translation (aref (cdr translation) 0)))
782 (setq done-list (cons translation done-list)))
783 (setq translation ch))
784 (aset layout i translation))
785 (setq i (1+ i)))
786
787 (let ((pos (point))
788 (bar "|")
789 lower upper row)
790 ;; Make table without horizontal lines. Each column for a key
791 ;; has the form "| LU |" where L is for lower key and and U is
792 ;; for a upper key. If width of L (U) is greater than 1,
793 ;; preceding (following) space is not inserted.
794 (put-text-property 0 1 'face 'bold bar)
795 (setq i 0)
796 (while (< i quail-keyboard-layout-len)
797 (when (= (% i 30) 0)
798 (setq row (/ i 30))
799 (if (> row 1)
800 (insert-char 32 (+ row (/ (- row 2) 2)))))
801 (setq lower (aref layout i)
802 upper (aref layout (1+ i)))
803 (if (and (integerp lower) (>= lower 128) (< lower 256))
804 (setq lower (unibyte-char-to-multibyte lower)))
805 (if (and (integerp upper) (>= upper 128) (< upper 256))
806 (setq upper (unibyte-char-to-multibyte upper)))
807 (insert bar)
808 (if (= (if (stringp lower) (string-width lower) (char-width lower)) 1)
809 (insert " "))
810 (insert lower upper)
811 (if (= (if (stringp upper) (string-width upper) (char-width upper)) 1)
812 (insert " "))
813 (setq i (+ i 2))
814 (if (= (% i 30) 0)
815 (insert bar "\n")))
816 ;; Insert horizontal lines while deleting blank key columns at the
817 ;; beginning and end of each line.
818 (save-restriction
819 (narrow-to-region pos (point))
820 (goto-char pos)
821 ;;(while (looking-at "[| ]*$")
822 ;;(forward-line 1)
823 ;;(delete-region pos (point)))
824 (let ((from1 100) (to1 0) from2 to2)
825 (while (not (eobp))
826 (if (looking-at "[| ]*$")
827 ;; The entire row is blank.
828 (delete-region (point) (match-end 0))
829 ;; Delete blank key columns at the head.
830 (if (looking-at " *\\(| \\)+")
831 (subst-char-in-region (point) (match-end 0) ?| ? ))
832 ;; Delete blank key columns at the tail.
833 (if (re-search-forward "\\( |\\)+$" (line-end-position) t)
834 (delete-region (match-beginning 0) (point)))
835 (beginning-of-line))
836 ;; Calculate the start and end columns of a horizontal line.
837 (if (eolp)
838 (setq from2 from1 to2 to1)
839 (skip-chars-forward " ")
840 (setq from2 (current-column))
841 (end-of-line)
842 (setq to2 (current-column))
843 (if (< from2 from1)
844 (setq from1 from2))
845 (if (> to2 to1)
846 (setq to1 to2))
847 (beginning-of-line))
848 ;; If the previous or the current line has at least one key
849 ;; column, insert a horizontal line.
850 (when (> to1 0)
851 (insert-char 32 from1)
852 (setq pos (point))
853 (insert "+")
854 (insert-char ?- (- (- to1 from1) 2))
855 (insert "+")
856 (put-text-property pos (point) 'face 'bold)
857 (insert "\n"))
858 (setq from1 from2 to1 to2)
859 (forward-line 1)))
860 ;; Insert "space bar" box.
861 (forward-line -1)
862 (setq pos (point))
863 (insert
864 " +-----------------------------+
865 | space bar |
866 +-----------------------------+
867 ")
868 (put-text-property pos (point) 'face 'bold)
869 (insert ?\n)))
870
871 done-list))
872
873 ;;;###autoload
874 (defun quail-show-keyboard-layout (&optional keyboard-type)
875 "Show the physical layout of the keyboard type KEYBOARD-TYPE.
876
877 The variable `quail-keyboard-layout-type' holds the currently selected
878 keyboard type."
879 (interactive
880 (list (completing-read "Keyboard type (default, current choise): "
881 quail-keyboard-layout-alist
882 nil t)))
883 (or (and keyboard-type (> (length keyboard-type) 0))
884 (setq keyboard-type quail-keyboard-layout-type))
885 (let ((layout (assoc keyboard-type quail-keyboard-layout-alist)))
886 (or layout
887 (error "Unknown keyboard type: %s" keyboard-type))
888 (with-output-to-temp-buffer "*Help*"
889 (with-current-buffer standard-output
890 (insert "Keyboard layout (keyboard type: "
891 keyboard-type
892 ")\n")
893 (quail-insert-kbd-layout (cdr layout))))))
894
895 ;; Quail map
896
897 (defsubst quail-map-p (object)
898 "Return t if OBJECT is a Quail map.
899
900 A Quail map holds information how a particular key should be translated.
901 Its format is (TRANSLATION . ALIST).
902 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
903 In the latter case, each element of VECTOR is a candidate for the translation,
904 and INDEX points the currently selected translation.
905
906 ALIST is normally a list of elements that look like (CHAR . DEFN),
907 where DEFN is another Quail map for a longer key (CHAR added to the
908 current key). It may also be a symbol of a function which returns an
909 alist of the above format.
910
911 Just after a Quail package is read, TRANSLATION may be a string or a
912 vector. Then each element of the string or vector is a candidate for
913 the translation. These objects are transformed to cons cells in the
914 format \(INDEX . VECTOR), as described above."
915 (and (consp object)
916 (let ((translation (car object)))
917 (or (integerp translation) (null translation)
918 (vectorp translation) (stringp translation)
919 (symbolp translation)
920 (and (consp translation) (not (vectorp (cdr translation))))))
921 (let ((alist (cdr object)))
922 (or (and (listp alist) (consp (car alist)))
923 (symbolp alist)))))
924
925 ;;;###autoload
926 (defmacro quail-define-rules (&rest rules)
927 "Define translation rules of the current Quail package.
928 Each argument is a list of KEY and TRANSLATION.
929 KEY is a string meaning a sequence of keystrokes to be translated.
930 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
931 If it is a character, it is the sole translation of KEY.
932 If it is a string, each character is a candidate for the translation.
933 If it is a vector, each element (string or character) is a candidate
934 for the translation.
935 In these cases, a key specific Quail map is generated and assigned to KEY.
936
937 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
938 it is used to handle KEY.
939
940 The first argument may be an alist of annotations for the following
941 rules. Each element has the form (ANNOTATION . VALUE), where
942 ANNOTATION is a symbol indicating the annotation type. Currently
943 the following annotation types are supported.
944
945 append -- the value non-nil means that the following rules should
946 be appended to the rules of the current Quail package.
947
948 face -- the value is a face to use for displaying TRANSLATIONs in
949 candidate list.
950
951 advice -- the value is a function to call after one of RULES is
952 selected. The function is called with one argument, the
953 selected TRANSLATION string, after the TRANSLATION is
954 inserted.
955
956 no-decode-map --- the value non-nil means that decoding map is not
957 generated for the following translations."
958 (let ((l rules)
959 append no-decode-map props)
960 ;; If the first argument is an alist of annotations, handle them.
961 (if (consp (car (car l)))
962 (let ((annotations (car l)))
963 (setq append (assq 'append annotations))
964 (if append
965 (setq annotations (delete append annotations)
966 append (cdr append)))
967 (setq no-decode-map (assq 'no-decode-map annotations))
968 (if no-decode-map
969 (setq annotations (delete no-decode-map annotations)
970 no-decode-map (cdr no-decode-map)))
971 ;; Convert the remaining annoations to property list PROPS.
972 (while annotations
973 (setq props
974 (cons (car (car annotations))
975 (cons (cdr (car annotations))
976 props))
977 annotations (cdr annotations)))
978 (setq l (cdr l))))
979 ;; Process the remaining arguments one by one.
980 (if append
981 ;; There's no way to add new rules at compiling time.
982 `(let ((tail ',l)
983 (map (quail-map))
984 (decode-map (and (quail-decode-map) (not ,no-decode-map)))
985 (properties ',props)
986 key trans)
987 (while tail
988 (setq key (car (car tail)) trans (car (cdr (car tail)))
989 tail (cdr tail))
990 (quail-defrule-internal key trans map t decode-map properties)))
991 ;; We can build up quail map and decode map at compiling time.
992 (let ((map (list nil))
993 (decode-map (if (not no-decode-map) (list 'decode-map)))
994 key trans)
995 (while l
996 (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
997 (quail-defrule-internal key trans map t decode-map props))
998 `(if (not (quail-decode-map))
999 (quail-install-map ',map)
1000 (quail-install-map ',map)
1001 (quail-install-decode-map ',decode-map))))))
1002
1003 ;;;###autoload
1004 (defun quail-install-map (map &optional name)
1005 "Install the Quail map MAP in the current Quail package.
1006
1007 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1008 which to install MAP.
1009
1010 The installed map can be referred by the function `quail-map'."
1011 (if (null quail-current-package)
1012 (error "No current Quail package"))
1013 (if (null (quail-map-p map))
1014 (error "Invalid Quail map `%s'" map))
1015 (setcar (cdr (cdr quail-current-package)) map))
1016
1017 ;;;###autoload
1018 (defun quail-install-decode-map (decode-map &optional name)
1019 "Install the Quail decode map DECODE-MAP in the current Quail package.
1020
1021 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1022 which to install MAP.
1023
1024 The installed decode map can be referred by the function `quail-decode-map'."
1025 (if (null quail-current-package)
1026 (error "No current Quail package"))
1027 (if (not (and (consp decode-map) (eq (car decode-map) 'decode-map)))
1028 (error "Invalid Quail decode map `%s'" decode-map))
1029 (setcar (nthcdr 10 quail-current-package) decode-map))
1030
1031 ;;;###autoload
1032 (defun quail-defrule (key translation &optional name append)
1033 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
1034 KEY is a string meaning a sequence of keystrokes to be translated.
1035 TRANSLATION is a character, a string, a vector, a Quail map,
1036 a function, or a cons.
1037 It it is a character, it is the sole translation of KEY.
1038 If it is a string, each character is a candidate for the translation.
1039 If it is a vector, each element (string or character) is a candidate
1040 for the translation.
1041 If it is a cons, the car is one of the above and the cdr is a function
1042 to call when translating KEY (the return value is assigned to the
1043 variable `quail-current-data'). If the cdr part is not a function,
1044 the value itself is assigned to `quail-current-data'.
1045 In these cases, a key specific Quail map is generated and assigned to KEY.
1046
1047 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
1048 it is used to handle KEY.
1049
1050 Optional 3rd argument NAME, if specified, says which Quail package
1051 to define this translation rule in. The default is to define it in the
1052 current Quail package.
1053
1054 Optional 4th argument APPEND, if non-nil, appends TRANSLATION
1055 to the current translations for KEY instead of replacing them."
1056 (if name
1057 (let ((package (quail-package name)))
1058 (if (null package)
1059 (error "No Quail package `%s'" name))
1060 (setq quail-current-package package)))
1061 (quail-defrule-internal key translation (quail-map) append))
1062
1063 ;;;###autoload
1064 (defun quail-defrule-internal (key trans map &optional append decode-map props)
1065 "Define KEY as TRANS in a Quail map MAP.
1066
1067 If Optional 4th arg APPEND is non-nil, TRANS is appended to the
1068 current translations for KEY instead of replacing them.
1069
1070 Optional 5th arg DECODE-MAP is a Quail decode map.
1071
1072 Optional 6th arg PROPS is a property list annotating TRANS. See the
1073 function `quail-define-rules' for the detail."
1074 (if (null (stringp key))
1075 "Invalid Quail key `%s'" key)
1076 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
1077 (consp trans)
1078 (symbolp trans)
1079 (quail-map-p trans)))
1080 (error "Invalid Quail translation `%s'" trans))
1081 (if (null (quail-map-p map))
1082 (error "Invalid Quail map `%s'" map))
1083 (let ((len (length key))
1084 (idx 0)
1085 ch entry)
1086 ;; Make a map for registering TRANS if necessary.
1087 (while (< idx len)
1088 (if (null (consp map))
1089 ;; We come here, for example, when we try to define a rule
1090 ;; for "ABC" but a rule for "AB" is already defined as a
1091 ;; symbol.
1092 (error "Quail key %s is too long" key))
1093 (setq ch (aref key idx)
1094 entry (assq ch (cdr map)))
1095 (if (null entry)
1096 (progn
1097 (setq entry (cons ch (list nil)))
1098 (setcdr map (cons entry (cdr map)))))
1099 (setq map (cdr entry))
1100 (setq idx (1+ idx)))
1101 (if (symbolp trans)
1102 (if (cdr map)
1103 ;; We come here, for example, when we try to define a rule
1104 ;; for "AB" as a symbol but a rule for "ABC" is already
1105 ;; defined.
1106 (error "Quail key %s is too short" key)
1107 (setcdr entry trans))
1108 (if (quail-map-p trans)
1109 (if (not (listp (cdr map)))
1110 ;; We come here, for example, when we try to define a rule
1111 ;; for "AB" as a symbol but a rule for "ABC" is already
1112 ;; defined.
1113 (error "Quail key %s is too short" key)
1114 (if (not (listp (cdr trans)))
1115 (if (cdr map)
1116 ;; We come here, for example, when we try to
1117 ;; define a rule for "AB" as a symbol but a rule
1118 ;; for "ABC" is already defined.
1119 (error "Quail key %s is too short" key)
1120 (setcdr entry trans))
1121 (setcdr entry (append trans (cdr map)))))
1122 ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS
1123 ;; to a vector of strings, add PROPS to each string and record
1124 ;; this rule in DECODE-MAP.
1125 (when (and (or props decode-map)
1126 (not (consp trans)) (not (symbolp trans)))
1127 (if (integerp trans)
1128 (setq trans (vector trans))
1129 (if (stringp trans)
1130 (setq trans (string-to-vector trans))))
1131 (let ((len (length trans))
1132 elt)
1133 (while (> len 0)
1134 (setq len (1- len))
1135 (setq elt (aref trans len))
1136 (if (integerp elt)
1137 (setq elt (char-to-string elt)))
1138 (aset trans len elt)
1139 (if props
1140 (add-text-properties 0 (length elt) props elt))
1141 (if decode-map
1142 (setcdr decode-map
1143 (cons (cons elt key) (cdr decode-map)))))))
1144 (if (and (car map) append)
1145 (let ((prev (quail-get-translation (car map) key len)))
1146 (if (integerp prev)
1147 (setq prev (vector prev))
1148 (setq prev (cdr prev)))
1149 (if (integerp trans)
1150 (setq trans (vector trans))
1151 (if (stringp trans)
1152 (setq trans (string-to-vector trans))))
1153 (setq trans
1154 (cons (list 0 0 0 0 nil)
1155 (vconcat prev trans)))))
1156 (setcar map trans)))))
1157
1158 (defun quail-get-translation (def key len)
1159 "Return the translation specified as DEF for KEY of length LEN.
1160 The translation is either a character or a cons of the form (INDEX . VECTOR),
1161 where VECTOR is a vector of candidates (character or string) for
1162 the translation, and INDEX points into VECTOR to specify the currently
1163 selected translation."
1164 (if (and def (symbolp def))
1165 (if (functionp def)
1166 ;; DEF is a symbol of a function which returns valid translation.
1167 (setq def (funcall def key len))
1168 (setq def nil)))
1169 (if (and (consp def) (not (vectorp (cdr def))))
1170 (setq def (car def)))
1171
1172 (cond
1173 ((or (integerp def) (consp def))
1174 def)
1175
1176 ((null def)
1177 ;; No translation.
1178 nil)
1179
1180 ((stringp def)
1181 ;; If the length is 1, we don't need vector but a single candidate
1182 ;; as the translation.
1183 (if (= (length def) 1)
1184 (aref def 0)
1185 ;; Each character in DEF is a candidate of translation. Reform
1186 ;; it as (INDICES . VECTOR).
1187 (cons (list 0 0 0 0 nil) (string-to-vector def))))
1188
1189 ((vectorp def)
1190 ;; If the length is 1, and the length of element string is 1, we
1191 ;; don't need vector but a single candidate as the translation.
1192 (if (and (= (length def) 1)
1193 (= (length (aref def 0)) 1))
1194 (aref (aref def 0) 0)
1195 ;; Each element (string or character) in DEF is a candidate of
1196 ;; translation. Reform it as (INDICES . VECTOR).
1197 (cons (list 0 0 0 0 nil) def)))
1198
1199 (t
1200 (error "Invalid object in Quail map: %s" def))))
1201
1202 (defun quail-lookup-key (key &optional len)
1203 "Lookup KEY of length LEN in the current Quail map and return the definition.
1204 The returned value is a Quail map specific to KEY."
1205 (or len
1206 (setq len (length key)))
1207 (let ((idx 0)
1208 (map (quail-map))
1209 (kbd-translate (quail-kbd-translate))
1210 slot ch translation def)
1211 (while (and map (< idx len))
1212 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
1213 (aref key idx)))
1214 (setq idx (1+ idx))
1215 (if (and (cdr map) (symbolp (cdr map)))
1216 (setcdr map (funcall (cdr map) key idx)))
1217 (setq slot (assq ch (cdr map)))
1218 (if (and (cdr slot) (symbolp (cdr slot)))
1219 (setcdr slot (funcall (cdr slot) key idx)))
1220 (setq map (cdr slot)))
1221 (setq def (car map))
1222 (setq quail-current-translations nil)
1223 (if (and map (setq translation (quail-get-translation def key len)))
1224 (progn
1225 (if (and (consp def) (not (vectorp (cdr def))))
1226 (progn
1227 (if (not (equal (car def) translation))
1228 ;; We must reflect TRANSLATION to car part of DEF.
1229 (setcar def translation))
1230 (setq quail-current-data
1231 (if (functionp (cdr def))
1232 (funcall (cdr def))
1233 (cdr def))))
1234 (if (not (equal def translation))
1235 ;; We must reflect TRANSLATION to car part of MAP.
1236 (setcar map translation)))
1237 (if (and (consp translation) (vectorp (cdr translation)))
1238 (progn
1239 (setq quail-current-translations translation)
1240 (if (quail-forget-last-selection)
1241 (setcar (car quail-current-translations) 0))))))
1242 ;; We may have to reform cdr part of MAP.
1243 (if (and (cdr map) (functionp (cdr map)))
1244 (setcdr map (funcall (cdr map) key len)))
1245 map))
1246
1247 (put 'quail-error 'error-conditions '(quail-error error))
1248 (defun quail-error (&rest args)
1249 (signal 'quail-error (apply 'format args)))
1250
1251
1252 (defun quail-input-string-to-events (str)
1253 "Convert input string STR to a list of events.
1254 Do so while interleaving with the following special events:
1255 \(compose-last-chars LEN COMPONENTS)
1256 \(quail-advice INPUT-STRING)"
1257 (let* ((events (string-to-list str))
1258 (len (length str))
1259 (idx len)
1260 composition from to)
1261 (while (and (> idx 0)
1262 (setq composition (find-composition idx 0 str t)))
1263 (setq from (car composition) to (nth 1 composition))
1264 (setcdr (nthcdr (1- to) events)
1265 (cons (list 'compose-last-chars (- to from)
1266 (and (not (nth 3 composition)) (nth 2 composition)))
1267 (nthcdr to events)))
1268 (setq idx (1- from)))
1269 (if (or (get-text-property 0 'advice str)
1270 (next-single-property-change 0 'advice str))
1271 (setq events
1272 (nconc events (list (list 'quail-advice str)))))
1273 events))
1274
1275 (defvar quail-translating nil)
1276 (defvar quail-converting nil)
1277 (defvar quail-conversion-str nil)
1278
1279 (defun quail-input-method (key)
1280 (if (or buffer-read-only
1281 overriding-terminal-local-map
1282 overriding-local-map)
1283 (list key)
1284 (quail-setup-overlays (quail-conversion-keymap))
1285 (let ((modified-p (buffer-modified-p))
1286 (buffer-undo-list t))
1287 (or (and quail-guidance-win
1288 (window-live-p quail-guidance-win)
1289 (eq (window-buffer quail-guidance-win) quail-guidance-buf)
1290 (not input-method-use-echo-area))
1291 (quail-show-guidance-buf))
1292 (unwind-protect
1293 (let ((input-string (if (quail-conversion-keymap)
1294 (quail-start-conversion key)
1295 (quail-start-translation key))))
1296 (when (and (stringp input-string)
1297 (> (length input-string) 0))
1298 (if input-method-exit-on-first-char
1299 (list (aref input-string 0))
1300 (quail-input-string-to-events input-string))))
1301 (quail-delete-overlays)
1302 (if (buffer-live-p quail-guidance-buf)
1303 (with-current-buffer quail-guidance-buf
1304 (erase-buffer)))
1305 (quail-hide-guidance-buf)
1306 (set-buffer-modified-p modified-p)
1307 ;; Run this hook only when the current input method doesn't require
1308 ;; conversion. When conversion is required, the conversion function
1309 ;; should run this hook at a proper timing.
1310 (unless (quail-conversion-keymap)
1311 (run-hooks 'input-method-after-insert-chunk-hook))))))
1312
1313 (defun quail-overlay-region-events (overlay)
1314 (let ((start (overlay-start overlay))
1315 (end (overlay-end overlay)))
1316 (if (< start end)
1317 (prog1
1318 (string-to-list (buffer-substring start end))
1319 (delete-region start end)))))
1320
1321 (defsubst quail-delete-region ()
1322 "Delete the text in the current translation region of Quail."
1323 (if (overlay-start quail-overlay)
1324 (delete-region (overlay-start quail-overlay)
1325 (overlay-end quail-overlay))))
1326
1327 (defun quail-start-translation (key)
1328 "Start translation of the typed character KEY by the current Quail package.
1329 Return the input string."
1330 ;; Check the possibility of translating KEY.
1331 ;; If KEY is nil, we can anyway start translation.
1332 (if (or (and (integerp key)
1333 (assq (if (quail-kbd-translate)
1334 (quail-keyboard-translate key) key)
1335 (cdr (quail-map))))
1336 (null key))
1337 ;; OK, we can start translation.
1338 (let* ((echo-keystrokes 0)
1339 (help-char nil)
1340 (overriding-terminal-local-map (quail-translation-keymap))
1341 (generated-events nil)
1342 (input-method-function nil)
1343 (modified-p (buffer-modified-p)))
1344 (setq quail-current-key ""
1345 quail-current-str ""
1346 quail-translating t)
1347 (if key
1348 (setq unread-command-events (cons key unread-command-events)))
1349 (while quail-translating
1350 (set-buffer-modified-p modified-p)
1351 (let* ((keyseq (read-key-sequence
1352 (and input-method-use-echo-area
1353 (concat input-method-previous-message
1354 quail-current-str))
1355 nil nil t))
1356 (cmd (lookup-key (quail-translation-keymap) keyseq)))
1357 (if (if key
1358 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
1359 (eq cmd 'quail-self-insert-command))
1360 (let ((last-command-event (aref keyseq (1- (length keyseq))))
1361 (last-command this-command)
1362 (this-command cmd))
1363 (setq key t)
1364 (condition-case err
1365 (call-interactively cmd)
1366 (quail-error (message "%s" (cdr err)) (beep))))
1367 ;; KEYSEQ is not defined in the translation keymap.
1368 ;; Let's return the event(s) to the caller.
1369 (setq unread-command-events
1370 (string-to-list (this-single-command-raw-keys)))
1371 (setq quail-translating nil))))
1372 (quail-delete-region)
1373 quail-current-str)
1374
1375 ;; Since KEY doesn't start any translation, just return it.
1376 ;; But translate KEY if necessary.
1377 (if (quail-kbd-translate)
1378 (setq key (quail-keyboard-translate key)))
1379 (char-to-string key)))
1380
1381 (defun quail-start-conversion (key)
1382 "Start conversion of the typed character KEY by the current Quail package.
1383 Return the input string."
1384 ;; Check the possibility of translating KEY.
1385 ;; If KEY is nil, we can anyway start translation.
1386 (if (or (and (integerp key)
1387 (assq (if (quail-kbd-translate)
1388 (quail-keyboard-translate key) key)
1389 (cdr (quail-map))))
1390 (null key))
1391 ;; Ok, we can start translation and conversion.
1392 (let* ((echo-keystrokes 0)
1393 (help-char nil)
1394 (overriding-terminal-local-map (quail-conversion-keymap))
1395 (generated-events nil)
1396 (input-method-function nil)
1397 (modified-p (buffer-modified-p)))
1398 (setq quail-current-key ""
1399 quail-current-str ""
1400 quail-translating t
1401 quail-converting t
1402 quail-conversion-str "")
1403 (if key
1404 (setq unread-command-events (cons key unread-command-events)))
1405 (while quail-converting
1406 (set-buffer-modified-p modified-p)
1407 (or quail-translating
1408 (progn
1409 (setq quail-current-key ""
1410 quail-current-str ""
1411 quail-translating t)
1412 (quail-setup-overlays nil)))
1413 ;; Hide '... loaded' message.
1414 (message nil)
1415 (let* ((keyseq (read-key-sequence
1416 (and input-method-use-echo-area
1417 (concat input-method-previous-message
1418 quail-conversion-str
1419 quail-current-str))
1420 nil nil t))
1421 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1422 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1423 (let ((last-command-event (aref keyseq (1- (length keyseq))))
1424 (last-command this-command)
1425 (this-command cmd))
1426 (setq key t)
1427 (condition-case err
1428 (call-interactively cmd)
1429 (quail-error (message "%s" (cdr err)) (beep)))
1430 (or quail-translating
1431 (progn
1432 (if quail-current-str
1433 (setq quail-conversion-str
1434 (concat quail-conversion-str
1435 (if (stringp quail-current-str)
1436 quail-current-str
1437 (char-to-string quail-current-str)))))
1438 (if (or input-method-exit-on-first-char
1439 (= (length quail-conversion-str) 0))
1440 (setq quail-converting nil)))))
1441 ;; KEYSEQ is not defined in the conversion keymap.
1442 ;; Let's return the event(s) to the caller.
1443 (setq unread-command-events
1444 (string-to-list (this-single-command-raw-keys)))
1445 (setq quail-converting nil))))
1446 (setq quail-translating nil)
1447 (if (overlay-start quail-conv-overlay)
1448 (delete-region (overlay-start quail-conv-overlay)
1449 (overlay-end quail-conv-overlay)))
1450 (if (> (length quail-conversion-str) 0)
1451 quail-conversion-str))
1452
1453 ;; Since KEY doesn't start any translation, just return it.
1454 ;; But translate KEY if necessary.
1455 (if (quail-kbd-translate)
1456 (setq key (quail-keyboard-translate key)))
1457 (char-to-string key)))
1458
1459 (defun quail-terminate-translation ()
1460 "Terminate the translation of the current key."
1461 (setq quail-translating nil)
1462 (if (buffer-live-p quail-guidance-buf)
1463 (with-current-buffer quail-guidance-buf
1464 (erase-buffer))))
1465
1466 (defun quail-select-current ()
1467 "Accept the currently selected translation."
1468 (interactive)
1469 (quail-terminate-translation))
1470
1471 (defun quail-update-translation (control-flag)
1472 "Update the current translation status according to CONTROL-FLAG.
1473 If CONTROL-FLAG is integer value, it is the number of keys in the
1474 head `quail-current-key' which can be translated. The remaining keys
1475 are put back to `unread-command-events' to be handled again. If
1476 CONTROL-FLAG is t, terminate the translation for the whole keys in
1477 `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation
1478 with more keys."
1479 (let ((func (quail-update-translation-function)))
1480 (if func
1481 (setq control-flag (funcall func control-flag))
1482 (cond ((numberp control-flag)
1483 (let ((len (length quail-current-key)))
1484 (if (= control-flag 0)
1485 (setq quail-current-str
1486 (if (quail-kbd-translate)
1487 (quail-keyseq-translate quail-current-key)
1488 quail-current-key)))
1489 (or input-method-exit-on-first-char
1490 (while (> len control-flag)
1491 (setq len (1- len))
1492 (setq unread-command-events
1493 (cons (aref quail-current-key len)
1494 unread-command-events))))))
1495 ((null control-flag)
1496 (unless quail-current-str
1497 (setq quail-current-str
1498 (if (quail-kbd-translate)
1499 (quail-keyseq-translate quail-current-key)
1500 quail-current-key))
1501 (if (and input-method-exit-on-first-char
1502 (quail-simple))
1503 (setq control-flag t)))))))
1504 (or input-method-use-echo-area
1505 (progn
1506 (quail-delete-region)
1507 (insert quail-current-str)))
1508 (let (quail-current-str)
1509 (quail-update-guidance))
1510 (or (stringp quail-current-str)
1511 (setq quail-current-str (char-to-string quail-current-str)))
1512 (if control-flag
1513 (quail-terminate-translation)))
1514
1515 (defun quail-self-insert-command ()
1516 "Translate the typed key by the current Quail map, and insert."
1517 (interactive "*")
1518 (setq quail-current-key
1519 (concat quail-current-key (char-to-string last-command-event)))
1520 (or (catch 'quail-tag
1521 (quail-update-translation (quail-translate-key))
1522 t)
1523 ;; If someone throws for `quail-tag' by value nil, we exit from
1524 ;; translation mode.
1525 (setq quail-translating nil)))
1526
1527 (defun quail-map-definition (map)
1528 "Return the actual definition part of Quail map MAP."
1529 (let ((def (car map)))
1530 (if (and (consp def) (not (vectorp (cdr def))))
1531 (setq def (car def)))
1532 (if (eq def t)
1533 (setq def nil))
1534 def))
1535
1536 (defun quail-get-current-str (len def)
1537 "Return string to be shown as current translation of key sequence.
1538 LEN is the length of the sequence. DEF is a definition part of the
1539 Quail map for the sequence."
1540 (or (and (consp def) (aref (cdr def) (car (car def))))
1541 def
1542 (and (> len 1)
1543 (let ((str (quail-get-current-str
1544 (1- len)
1545 (quail-map-definition (quail-lookup-key
1546 quail-current-key (1- len))))))
1547 (if str
1548 (concat (if (stringp str) str (char-to-string str))
1549 (substring quail-current-key (1- len) len)))))))
1550
1551 (defvar quail-guidance-translations-starting-column 20)
1552
1553 (defun quail-update-current-translations (&optional relative-index)
1554 "Update `quail-current-translations'.
1555 Make RELATIVE-INDEX the current translation."
1556 (let* ((indices (car quail-current-translations))
1557 (cur (car indices))
1558 (start (nth 1 indices))
1559 (end (nth 2 indices)))
1560 ;; Validate the index number of current translation.
1561 (if (< cur 0)
1562 (setcar indices (setq cur 0))
1563 (if (>= cur (length (cdr quail-current-translations)))
1564 (setcar indices
1565 (setq cur (1- (length (cdr quail-current-translations)))))))
1566
1567 (if (or (null end) ; We have not yet calculated END.
1568 (< cur start) ; We moved to the previous block.
1569 (>= cur end)) ; We moved to the next block.
1570 (let ((len (length (cdr quail-current-translations)))
1571 (maxcol (- (window-width quail-guidance-win)
1572 quail-guidance-translations-starting-column))
1573 (block (nth 3 indices))
1574 col idx width trans num-items blocks)
1575 (if (< cur start)
1576 ;; We must calculate from the head.
1577 (setq start 0 block 0)
1578 (if end ; i.e. (>= cur end)
1579 (setq start end)))
1580 (setq idx start col 0 end start num-items 0)
1581 ;; Loop until we hit the tail, or reach the block of CUR.
1582 (while (and (< idx len) (>= cur end))
1583 (if (= num-items 0)
1584 (setq start idx col 0 block (1+ block)))
1585 (setq trans (aref (cdr quail-current-translations) idx))
1586 (setq width (if (integerp trans) (char-width trans)
1587 (string-width trans)))
1588 (setq col (+ col width 3) num-items (1+ num-items))
1589 (if (and (> num-items 0)
1590 (or (>= col maxcol) (> num-items 10)))
1591 (setq end idx num-items 0)
1592 (setq idx (1+ idx))))
1593 (setcar (nthcdr 3 indices) block)
1594 (if (>= idx len)
1595 (progn
1596 ;; We hit the tail before reaching MAXCOL.
1597 (setq end idx)
1598 (setcar (nthcdr 4 indices) block)))
1599 (setcar (cdr indices) start)
1600 (setcar (nthcdr 2 indices) end)))
1601 (if relative-index
1602 (if (>= (+ start relative-index) end)
1603 (setcar indices (1- end))
1604 (setcar indices (+ start relative-index))))
1605 (setq quail-current-str
1606 (aref (cdr quail-current-translations) (car indices)))
1607 (or (stringp quail-current-str)
1608 (setq quail-current-str (char-to-string quail-current-str)))))
1609
1610 (defun quail-translate-key ()
1611 "Translate the current key sequence according to the current Quail map.
1612 Return t if we can terminate the translation.
1613 Return nil if the current key sequence may be followed by more keys.
1614 Return number if we can't find any translation for the current key
1615 sequence. The number is the count of valid keys in the current
1616 sequence counting from the head."
1617 (let* ((len (length quail-current-key))
1618 (map (quail-lookup-key quail-current-key len))
1619 def ch)
1620 (if map
1621 (let ((def (quail-map-definition map)))
1622 (setq quail-current-str (quail-get-current-str len def))
1623 ;; Return t only if we can terminate the current translation.
1624 (and
1625 ;; No alternative translations.
1626 (or (null (consp def)) (= (length (cdr def)) 1))
1627 ;; No translation for the longer key.
1628 (null (cdr map))
1629 ;; No shorter breaking point.
1630 (or (null (quail-maximum-shortest))
1631 (< len 3)
1632 (null (quail-lookup-key quail-current-key (1- len)))
1633 (null (quail-lookup-key
1634 (substring quail-current-key -2 -1) 1)))))
1635
1636 ;; There's no translation for the current key sequence. Before
1637 ;; giving up, we must check two possibilities.
1638 (cond ((and
1639 (quail-maximum-shortest)
1640 (>= len 3)
1641 (setq def (quail-map-definition
1642 (quail-lookup-key quail-current-key (- len 2))))
1643 (quail-lookup-key (substring quail-current-key -2) 2))
1644 ;; Now the sequence is "...ABCD", which can be split into
1645 ;; "...AB" and "CD..." to get valid translation.
1646 ;; At first, get translation of "...AB".
1647 (setq quail-current-str (quail-get-current-str (- len 2) def))
1648 ;; Then, return the length of "...AB".
1649 (- len 2))
1650
1651 ((and (> len 0)
1652 (quail-lookup-key (substring quail-current-key 0 -1))
1653 quail-current-translations
1654 (not (quail-deterministic))
1655 (setq ch (aref quail-current-key (1- len)))
1656 (>= ch ?0) (<= ch ?9))
1657 ;; A numeric key is entered to select a desirable translation.
1658 (setq quail-current-key (substring quail-current-key 0 -1))
1659 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1660 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1661 (quail-update-current-translations ch)
1662 ;; And, we can terminate the current translation.
1663 t)
1664
1665 (t
1666 ;; No way to handle the last character in this context.
1667 (1- len))))))
1668
1669 (defun quail-next-translation ()
1670 "Select next translation in the current batch of candidates."
1671 (interactive)
1672 (if quail-current-translations
1673 (let ((indices (car quail-current-translations)))
1674 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1675 ;; We are already at the tail.
1676 (beep)
1677 (setcar indices (1+ (car indices)))
1678 (quail-update-current-translations)
1679 (quail-update-translation nil)))
1680 (setq unread-command-events
1681 (cons last-command-event unread-command-events))
1682 (quail-terminate-translation)))
1683
1684 (defun quail-prev-translation ()
1685 "Select previous translation in the current batch of candidates."
1686 (interactive)
1687 (if quail-current-translations
1688 (let ((indices (car quail-current-translations)))
1689 (if (= (car indices) 0)
1690 ;; We are already at the head.
1691 (beep)
1692 (setcar indices (1- (car indices)))
1693 (quail-update-current-translations)
1694 (quail-update-translation nil)))
1695 (setq unread-command-events
1696 (cons last-command-event unread-command-events))
1697 (quail-terminate-translation)))
1698
1699 (defun quail-next-translation-block ()
1700 "Select from the next block of translations."
1701 (interactive)
1702 (if quail-current-translations
1703 (let* ((indices (car quail-current-translations))
1704 (offset (- (car indices) (nth 1 indices))))
1705 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1706 ;; We are already at the last block.
1707 (beep)
1708 (setcar indices (+ (nth 2 indices) offset))
1709 (quail-update-current-translations)
1710 (quail-update-translation nil)))
1711 (setq unread-command-events
1712 (cons last-command-event unread-command-events))
1713 (quail-terminate-translation)))
1714
1715 (defun quail-prev-translation-block ()
1716 "Select the previous batch of 10 translation candidates."
1717 (interactive)
1718 (if quail-current-translations
1719 (let* ((indices (car quail-current-translations))
1720 (offset (- (car indices) (nth 1 indices))))
1721 (if (= (nth 1 indices) 0)
1722 ;; We are already at the first block.
1723 (beep)
1724 (setcar indices (1- (nth 1 indices)))
1725 (quail-update-current-translations)
1726 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1727 (progn
1728 (setcar indices (+ (nth 1 indices) offset))
1729 (quail-update-current-translations)))
1730 (quail-update-translation nil)))
1731 (setq unread-command-events
1732 (cons last-command-event unread-command-events))
1733 (quail-terminate-translation)))
1734
1735 (defun quail-abort-translation ()
1736 "Abort translation and delete the current Quail key sequence."
1737 (interactive)
1738 (quail-delete-region)
1739 (setq quail-current-str nil)
1740 (quail-terminate-translation))
1741
1742 (defun quail-delete-last-char ()
1743 "Delete the last input character from the current Quail key sequence."
1744 (interactive)
1745 (if (= (length quail-current-key) 1)
1746 (quail-abort-translation)
1747 (setq quail-current-key (substring quail-current-key 0 -1))
1748 (quail-delete-region)
1749 (quail-update-translation (quail-translate-key))))
1750
1751 ;; For conversion mode.
1752
1753 (defsubst quail-point-in-conversion-region ()
1754 "Return non-nil value if the point is in conversion region of Quail mode."
1755 (let (start pos)
1756 (and (setq start (overlay-start quail-conv-overlay))
1757 (>= (setq pos (point)) start)
1758 (<= pos (overlay-end quail-conv-overlay)))))
1759
1760 (defun quail-conversion-backward-char ()
1761 (interactive)
1762 (if (<= (point) (overlay-start quail-conv-overlay))
1763 (quail-error "Beginning of conversion region"))
1764 (setq quail-translating nil)
1765 (forward-char -1))
1766
1767 (defun quail-conversion-forward-char ()
1768 (interactive)
1769 (if (>= (point) (overlay-end quail-conv-overlay))
1770 (quail-error "End of conversion region"))
1771 (setq quail-translating nil)
1772 (forward-char 1))
1773
1774 (defun quail-conversion-beginning-of-region ()
1775 (interactive)
1776 (setq quail-translating nil)
1777 (goto-char (overlay-start quail-conv-overlay)))
1778
1779 (defun quail-conversion-end-of-region ()
1780 (interactive)
1781 (setq quail-translating nil)
1782 (goto-char (overlay-end quail-conv-overlay)))
1783
1784 (defun quail-conversion-delete-char ()
1785 (interactive)
1786 (setq quail-translating nil)
1787 (if (>= (point) (overlay-end quail-conv-overlay))
1788 (quail-error "End of conversion region"))
1789 (delete-char 1)
1790 (let ((start (overlay-start quail-conv-overlay))
1791 (end (overlay-end quail-conv-overlay)))
1792 (setq quail-conversion-str (buffer-substring start end))
1793 (if (= start end)
1794 (setq quail-converting nil))))
1795
1796 (defun quail-conversion-delete-tail ()
1797 (interactive)
1798 (if (>= (point) (overlay-end quail-conv-overlay))
1799 (quail-error "End of conversion region"))
1800 (delete-region (point) (overlay-end quail-conv-overlay))
1801 (let ((start (overlay-start quail-conv-overlay))
1802 (end (overlay-end quail-conv-overlay)))
1803 (setq quail-conversion-str (buffer-substring start end))
1804 (if (= start end)
1805 (setq quail-converting nil))))
1806
1807 (defun quail-conversion-backward-delete-char ()
1808 (interactive)
1809 (if (> (length quail-current-key) 0)
1810 (quail-delete-last-char)
1811 (if (<= (point) (overlay-start quail-conv-overlay))
1812 (quail-error "Beginning of conversion region"))
1813 (delete-char -1)
1814 (let ((start (overlay-start quail-conv-overlay))
1815 (end (overlay-end quail-conv-overlay)))
1816 (setq quail-conversion-str (buffer-substring start end))
1817 (if (= start end)
1818 (setq quail-converting nil)))))
1819
1820 (defun quail-do-conversion (func &rest args)
1821 "Call FUNC to convert text in the current conversion region of Quail.
1822 Remaining args are for FUNC."
1823 (delete-overlay quail-overlay)
1824 (apply func args))
1825
1826 (defun quail-no-conversion ()
1827 "Do no conversion of the current conversion region of Quail."
1828 (interactive)
1829 (setq quail-converting nil))
1830
1831 ;; Guidance, Completion, and Help buffer handlers.
1832
1833 (defun quail-make-guidance-frame (buf)
1834 "Make a new one-line frame for Quail guidance buffer."
1835 (let* ((fparam (frame-parameters))
1836 (top (cdr (assq 'top fparam)))
1837 (border (cdr (assq 'border-width fparam)))
1838 (internal-border (cdr (assq 'internal-border-width fparam)))
1839 (newtop (- top
1840 (frame-char-height) (* internal-border 2) (* border 2))))
1841 (if (< newtop 0)
1842 (setq newtop (+ top (frame-pixel-height))))
1843 (let* ((frame (make-frame (append '((user-position . t) (height . 1)
1844 (minibuffer) (menu-bar-lines . 0))
1845 (cons (cons 'top newtop) fparam))))
1846 (win (frame-first-window frame)))
1847 (set-window-buffer win buf)
1848 ;;(set-window-dedicated-p win t)
1849 )))
1850
1851 (defun quail-setup-completion-buf ()
1852 "Setup Quail completion buffer."
1853 (unless (buffer-live-p quail-completion-buf)
1854 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1855 (setq quail-completion-buf (get-buffer-create "*Quail Completions*")))
1856 (with-current-buffer quail-completion-buf
1857 (setq quail-overlay (make-overlay 1 1))
1858 (overlay-put quail-overlay 'face 'highlight))))
1859
1860 (defun quail-require-guidance-buf ()
1861 "Return t iff the current Quail package requires showing guidance buffer."
1862 (and input-method-verbose-flag
1863 (if (eq input-method-verbose-flag 'default)
1864 (not (and (eq (selected-window) (minibuffer-window))
1865 (quail-simple)))
1866 (if (eq input-method-verbose-flag 'complex-only)
1867 (not (quail-simple))
1868 t))))
1869
1870 (defun quail-show-guidance-buf ()
1871 "Display a guidance buffer for Quail input method in some window.
1872 Create the buffer if it does not exist yet.
1873 The buffer is normally displayed at the echo area,
1874 but if the current buffer is a minibuffer, it is shown in
1875 the bottom-most ordinary window of the same frame,
1876 or in a newly created frame (if the selected frame has no other windows)."
1877 (when (quail-require-guidance-buf)
1878 ;; At first, setup a guidance buffer.
1879 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1880 (or (buffer-live-p quail-guidance-buf)
1881 (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*"))))
1882 (let ((name (quail-name))
1883 (title (quail-title)))
1884 (with-current-buffer quail-guidance-buf
1885 ;; To show the title of Quail package.
1886 (setq current-input-method name
1887 current-input-method-title title)
1888 (erase-buffer)
1889 (or (overlayp quail-overlay)
1890 (progn
1891 (setq quail-overlay (make-overlay 1 1))
1892 (overlay-put quail-overlay 'face 'highlight)))
1893 (delete-overlay quail-overlay)
1894 (set-buffer-modified-p nil)))
1895 (bury-buffer quail-guidance-buf)
1896
1897 ;; Assign the buffer " *Minibuf-N*" to all windows which are now
1898 ;; displaying quail-guidance-buf.
1899 (let ((win-list (get-buffer-window-list quail-guidance-buf t t)))
1900 (while win-list
1901 (set-window-buffer (car win-list)
1902 (format " *Minibuf-%d*" (minibuffer-depth)))
1903 (setq win-list (cdr win-list))))
1904
1905 ;; Then, display it in an appropriate window.
1906 (let ((win (minibuffer-window)))
1907 (if (or (eq (selected-window) win)
1908 input-method-use-echo-area)
1909 ;; Since we are in minibuffer, we can't use it for guidance.
1910 (if (eq win (frame-root-window))
1911 ;; Create a frame. It is sure that we are using some
1912 ;; window system.
1913 (quail-make-guidance-frame quail-guidance-buf)
1914 ;; Find the bottom window and split it if necessary.
1915 (setq win (window-at
1916 0 (1- (- (frame-height) (window-height win)))))
1917 (let ((height (window-height win))
1918 (window-min-height 2))
1919 ;; If WIN is tall enough, split it vertically and use
1920 ;; the lower one.
1921 (when (>= height 4)
1922 ;; Here, `split-window' returns a lower window
1923 ;; which is what we wanted.
1924 (setq win (split-window win (- height 2))))
1925 (set-window-buffer win quail-guidance-buf)
1926 (with-current-buffer quail-guidance-buf
1927 (fit-window-to-buffer win nil (window-height win)))))
1928 (set-window-buffer win quail-guidance-buf)
1929 (set-minibuffer-window win))
1930 (setq quail-guidance-win win)))
1931
1932 ;; And, create a buffer for completion.
1933 (quail-setup-completion-buf)
1934 (bury-buffer quail-completion-buf))
1935
1936 (defun quail-hide-guidance-buf ()
1937 "Hide the Quail guidance buffer."
1938 (if (buffer-live-p quail-guidance-buf)
1939 (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
1940 win)
1941 (while win-list
1942 (setq win (car win-list) win-list (cdr win-list))
1943 (if (window-minibuffer-p win)
1944 ;; We are using echo area for the guidance buffer.
1945 ;; Vacate it to the deepest minibuffer.
1946 (set-window-buffer win
1947 (format " *Minibuf-%d*" (minibuffer-depth)))
1948 (if (eq win (frame-root-window (window-frame win)))
1949 (progn
1950 ;; We are using a separate frame for guidance buffer.
1951 ;;(set-window-dedicated-p win nil)
1952 (delete-frame (window-frame win)))
1953 ;;(set-window-dedicated-p win nil)
1954 (delete-window win))))
1955 (setq quail-guidance-win nil))))
1956
1957 (defun quail-update-guidance ()
1958 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1959 ;; Update guidance buffer.
1960 (if (quail-require-guidance-buf)
1961 (let ((guidance (quail-guidance)))
1962 (unless (and (eq (selected-frame) (window-frame (minibuffer-window)))
1963 (eq (selected-frame) (window-frame quail-guidance-win)))
1964 ;; The guidance window is not shown in this frame, show it.
1965 (quail-show-guidance-buf))
1966 (cond ((or (eq guidance t)
1967 (consp guidance))
1968 ;; Show the current possible translations.
1969 (quail-show-translations))
1970 ((null guidance)
1971 ;; Show the current input keys.
1972 (let ((key quail-current-key))
1973 (if (quail-kbd-translate)
1974 (setq key (quail-keyseq-translate key)))
1975 (with-current-buffer quail-guidance-buf
1976 (erase-buffer)
1977 (insert key)))))
1978 ;; Make sure the height of the guidance window is OK --
1979 ;; sometimes, if the minibuffer window expands due to user
1980 ;; input (for instance if the newly inserted character is in a
1981 ;; different font), it will cause the guidance window to be
1982 ;; only partially visible. We force a redisplay first because
1983 ;; this automatic expansion doesn't happen until then, and we
1984 ;; want to see the window sizes after the expansion.
1985 (sit-for 0)
1986 (fit-window-to-buffer quail-guidance-win nil
1987 (window-height quail-guidance-win))))
1988
1989 ;; Update completion buffer if displayed now. We highlight the
1990 ;; selected candidate string in *Completion* buffer if any.
1991 (let ((win (get-buffer-window quail-completion-buf))
1992 key str pos)
1993 (if win
1994 (save-excursion
1995 (setq str (if (stringp quail-current-str)
1996 quail-current-str
1997 (if (numberp quail-current-str)
1998 (char-to-string quail-current-str)))
1999 key quail-current-key)
2000 (set-buffer quail-completion-buf)
2001 (goto-char (point-min))
2002 (if (null (search-forward (concat " " key ":") nil t))
2003 (delete-overlay quail-overlay)
2004 (setq pos (point))
2005 (if (and str (search-forward (concat "." str) nil t))
2006 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
2007 (move-overlay quail-overlay (match-beginning 0) (point)))
2008 ;; Now POS points end of KEY and (point) points end of STR.
2009 (if (pos-visible-in-window-p (point) win)
2010 ;; STR is already visible.
2011 nil
2012 ;; We want to make both KEY and STR visible, but if the
2013 ;; window is too short, make at least STR visible.
2014 (setq pos (progn (point) (goto-char pos)))
2015 (beginning-of-line)
2016 (set-window-start win (point))
2017 (if (not (pos-visible-in-window-p pos win))
2018 (set-window-start win pos))
2019 ))))))
2020
2021 (defun quail-show-translations ()
2022 "Show the current possible translations."
2023 (let* ((key quail-current-key)
2024 (map (quail-lookup-key quail-current-key))
2025 (current-translations quail-current-translations))
2026 (if quail-current-translations
2027 (quail-update-current-translations))
2028 (with-current-buffer quail-guidance-buf
2029 (erase-buffer)
2030
2031 ;; Show the current key.
2032 (let ((guidance (quail-guidance)))
2033 (if (listp guidance)
2034 ;; We must show the specified PROMPTKEY instead of the
2035 ;; actual typed keys.
2036 (let ((i 0)
2037 (len (length key))
2038 prompt-key)
2039 (while (< i len)
2040 (setq prompt-key (cdr (assoc (aref key i) guidance)))
2041 (insert (or prompt-key (aref key i)))
2042 (setq i (1+ i))))
2043 (insert key)))
2044
2045 ;; Show followable keys.
2046 (if (and (> (length key) 0) (cdr map))
2047 (let ((keys (mapcar (function (lambda (x) (car x)))
2048 (cdr map))))
2049 (setq keys (sort keys '<))
2050 (insert "[")
2051 (while keys
2052 (insert (car keys))
2053 (setq keys (cdr keys)))
2054 (insert "]")))
2055
2056 ;; Show list of translations.
2057 (if (and current-translations
2058 (not (quail-deterministic)))
2059 (let* ((indices (car current-translations))
2060 (cur (car indices))
2061 (start (nth 1 indices))
2062 (end (nth 2 indices))
2063 (idx start))
2064 (indent-to (- quail-guidance-translations-starting-column 7))
2065 (insert (format "(%02d/"(nth 3 indices))
2066 (if (nth 4 indices)
2067 (format "%02d)" (nth 4 indices))
2068 "??)"))
2069 (while (< idx end)
2070 (insert (format " %d." (if (= (- idx start) 9) 0
2071 (1+ (- idx start)))))
2072 (let ((pos (point)))
2073 (insert (aref (cdr current-translations) idx))
2074 (if (= idx cur)
2075 (move-overlay quail-overlay pos (point))))
2076 (setq idx (1+ idx)))))
2077 )))
2078
2079 (defvar quail-completion-max-depth 5
2080 "The maximum depth of Quail completion list.")
2081
2082 (defun quail-completion ()
2083 "List all completions for the current key.
2084 All possible translations of the current key and whole possible longer keys
2085 are shown (at most to the depth specified `quail-completion-max-depth')."
2086 (interactive)
2087 (quail-setup-completion-buf)
2088 (let ((win (get-buffer-window quail-completion-buf 'visible))
2089 (key quail-current-key)
2090 (map (quail-lookup-key quail-current-key))
2091 (require-update nil))
2092 (with-current-buffer quail-completion-buf
2093 (if (and win
2094 (equal key quail-current-key)
2095 (eq last-command 'quail-completion))
2096 ;; The window for Quail completion buffer has already been
2097 ;; shown. We just scroll it appropriately.
2098 (if (pos-visible-in-window-p (point-max) win)
2099 (set-window-start win (point-min))
2100 (let ((other-window-scroll-buffer quail-completion-buf))
2101 (scroll-other-window)))
2102 (setq quail-current-key key)
2103 (erase-buffer)
2104 (insert "Possible completion and corresponding translations are:\n")
2105 (quail-completion-1 key map 1)
2106 (goto-char (point-min))
2107 (display-buffer (current-buffer))
2108 (setq require-update t)))
2109 (if require-update
2110 (quail-update-guidance)))
2111 (setq this-command 'quail-completion))
2112
2113 (defun quail-completion-1 (key map indent)
2114 "List all completions of KEY in MAP with indentation INDENT."
2115 (let ((len (length key)))
2116 (indent-to indent)
2117 (insert key ":")
2118 (if (and (symbolp map) (fboundp map))
2119 (setq map (funcall map key len)))
2120 (if (car map)
2121 (quail-completion-list-translations map key (+ indent len 1))
2122 (insert " -\n"))
2123 (setq indent (+ indent 2))
2124 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
2125 (let ((l (cdr map))
2126 (newkey (make-string (1+ len) 0))
2127 (i 0))
2128 (if (functionp l)
2129 (setq l (funcall l)))
2130 ;; Set KEY in the first LEN characters of NEWKEY.
2131 (while (< i len)
2132 (aset newkey i (aref key i))
2133 (setq i (1+ i)))
2134 (setq l (reverse l))
2135 (while l ; L = ((CHAR . DEFN) ....) ;
2136 (aset newkey len (car (car l)))
2137 (quail-completion-1 newkey (cdr (car l)) indent)
2138 (setq l (cdr l)))))))
2139
2140 (defun quail-completion-list-translations (map key indent)
2141 "List all possible translations of KEY in Quail MAP with indentation INDENT."
2142 (let (beg (translations
2143 (quail-get-translation (car map) key (length key))))
2144 (if (integerp translations)
2145 (progn
2146 (insert "(1/1) 1.")
2147 ;; Endow the character `translations' with `mouse-face' text
2148 ;; property to enable `mouse-2' completion.
2149 (setq beg (point))
2150 (insert translations)
2151 (put-text-property beg (point) 'mouse-face 'highlight)
2152 (insert "\n"))
2153 ;; We need only vector part.
2154 (setq translations (cdr translations))
2155 ;; Insert every 10 elements with indices in a line.
2156 (let ((len (length translations))
2157 (i 0)
2158 num)
2159 (while (< i len)
2160 (when (zerop (% i 10))
2161 (when (>= i 10)
2162 (insert "\n")
2163 (indent-to indent))
2164 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
2165 ;; We show the last digit of FROM while converting
2166 ;; 0,1,..,9 to 1,2,..,0.
2167 (insert (format " %d." (% (1+ i) 10)))
2168 (setq beg (point))
2169 (insert (aref translations i))
2170 ;; Passing the mouse over a character will highlight.
2171 (put-text-property beg (point) 'mouse-face 'highlight)
2172 (setq i (1+ i)))
2173 (insert "\n")))))
2174
2175 ;; Choose a completion in *Quail Completions* buffer with mouse-2.
2176
2177 (defun quail-mouse-choose-completion (event)
2178 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
2179 (interactive "e")
2180 ;; This function is an exact copy of the mouse.el function
2181 ;; `mouse-choose-completion' except that we:
2182 ;; 1) add two lines from `choose-completion' in simple.el to give
2183 ;; the `mouse-2' click a little more leeway.
2184 ;; 2) don't bury *Quail Completions* buffer so comment a section, and
2185 ;; 3) delete/terminate the current quail selection here.
2186 ;; Give temporary modes such as isearch a chance to turn off.
2187 (run-hooks 'mouse-leave-buffer-hook)
2188 (let ((buffer (window-buffer))
2189 choice
2190 base-size)
2191 (with-current-buffer (window-buffer (posn-window (event-start event)))
2192 (if completion-reference-buffer
2193 (setq buffer completion-reference-buffer))
2194 (setq base-size completion-base-size)
2195 (save-excursion
2196 (goto-char (posn-point (event-start event)))
2197 (let (beg end)
2198 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2199 (setq end (point) beg (1+ (point))))
2200 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
2201 (setq end (1- (point)) beg (point)))
2202 (if (null beg)
2203 (quail-error "No completion here"))
2204 (setq beg (previous-single-property-change beg 'mouse-face))
2205 (setq end (or (next-single-property-change end 'mouse-face)
2206 (point-max)))
2207 (setq choice (buffer-substring beg end)))))
2208 ; (let ((owindow (selected-window)))
2209 ; (select-window (posn-window (event-start event)))
2210 ; (if (and (one-window-p t 'selected-frame)
2211 ; (window-dedicated-p (selected-window)))
2212 ; ;; This is a special buffer's frame
2213 ; (iconify-frame (selected-frame))
2214 ; (or (window-dedicated-p (selected-window))
2215 ; (bury-buffer)))
2216 ; (select-window owindow))
2217 (quail-delete-region)
2218 (quail-choose-completion-string choice buffer base-size)
2219 (quail-terminate-translation)))
2220
2221 ;; Modify the simple.el function `choose-completion-string', because
2222 ;; the simple.el function `choose-completion-delete-max-match' breaks
2223 ;; on Mule data, since the semantics of `forward-char' have changed.
2224
2225 (defun quail-choose-completion-string (choice &optional buffer base-size)
2226 (let ((buffer (or buffer completion-reference-buffer)))
2227 ;; If BUFFER is a minibuffer, barf unless it's the currently
2228 ;; active minibuffer.
2229 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
2230 (or (not (active-minibuffer-window))
2231 (not (equal buffer
2232 (window-buffer (active-minibuffer-window))))))
2233 (quail-error "Minibuffer is not active for completion")
2234 ;; Store the completion in `quail-current-str', which will later
2235 ;; be converted to a character event list, then inserted into
2236 ;; the buffer where completion was requested.
2237 (set-buffer buffer)
2238 ; (if base-size
2239 ; (delete-region (+ base-size (point-min)) (point))
2240 ; (choose-completion-delete-max-match choice))
2241 (setq quail-current-str choice)
2242 ;; Update point in the window that BUFFER is showing in.
2243 (let ((window (get-buffer-window buffer t)))
2244 (set-window-point window (point)))
2245 ;; If completing for the minibuffer, exit it with this choice.
2246 (and (not completion-no-auto-exit)
2247 (equal buffer (window-buffer (minibuffer-window)))
2248 minibuffer-completion-table
2249 ;; If this is reading a file name, and the file name chosen
2250 ;; is a directory, don't exit the minibuffer.
2251 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
2252 (file-directory-p (buffer-string)))
2253 (select-window (active-minibuffer-window))
2254 (exit-minibuffer))))))
2255
2256 (defun quail-build-decode-map (map-list key decode-map num
2257 &optional maxnum ignores)
2258 "Build a decoding map.
2259 Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
2260 vs the corresponding translations defined in the Quail map
2261 specified by the first element MAP-LIST. Each pair has the form
2262 \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
2263 \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
2264 is a key sequence to reach MAP.
2265 Optional 5th arg MAXNUM limits the number of accumulated pairs.
2266 Optional 6th arg IGNORES is a list of translations to ignore."
2267 (let* ((map (car map-list))
2268 (translation (quail-get-translation (car map) key (length key)))
2269 elt)
2270 (cond ((integerp translation)
2271 ;; Accept only non-ASCII chars not listed in IGNORES.
2272 (when (and (> translation 255) (not (memq translation ignores)))
2273 (setcdr decode-map
2274 (cons (cons key translation) (cdr decode-map)))
2275 (setq num (1+ num))))
2276 ((consp translation)
2277 (setq translation (cdr translation))
2278 (let ((multibyte nil))
2279 (mapc (function (lambda (x)
2280 ;; Accept only non-ASCII chars not
2281 ;; listed in IGNORES.
2282 (if (and (if (integerp x) (> x 255)
2283 (> (string-bytes x) (length x)))
2284 (not (member x ignores)))
2285 (setq multibyte t))))
2286 translation)
2287 (when multibyte
2288 (setcdr decode-map
2289 (cons (cons key translation) (cdr decode-map)))
2290 (setq num (+ num (length translation)))))))
2291 (if (and maxnum (> num maxnum))
2292 (- num)
2293 (setq map (cdr map))
2294 ;; Recursively check the deeper map.
2295 (while (and map (>= num 0))
2296 (setq elt (car map) map (cdr map))
2297 (when (and (integerp (car elt)) (consp (cdr elt))
2298 (not (memq (cdr elt) map-list)))
2299 (setq num (quail-build-decode-map (cons (cdr elt) map-list)
2300 (format "%s%c" key (car elt))
2301 decode-map num maxnum ignores))))
2302 num)))
2303
2304 (defun quail-insert-decode-map (decode-map)
2305 "Insert pairs of key sequences vs the corresponding translations.
2306 These are stored in DECODE-MAP using the concise format. DECODE-MAP
2307 should be made by `quail-build-decode-map' (which see)."
2308 (setq decode-map
2309 (sort (cdr decode-map)
2310 (function (lambda (x y)
2311 (setq x (car x) y (car y))
2312 (or (> (length x) (length y))
2313 (and (= (length x) (length y))
2314 (not (string< x y))))))))
2315 (let ((frame-width (frame-width (window-frame (get-buffer-window
2316 (current-buffer) 'visible))))
2317 (single-key-width 3)
2318 (single-trans-width 4)
2319 (multiple-key-width 3)
2320 (single-list nil)
2321 (multiple-list nil)
2322 elt trans width pos cols rows col row str col-width)
2323 ;; Divide the elements of decoding map into single ones (i.e. the
2324 ;; one that has single translation) and multibyte ones (i.e. the
2325 ;; one that has multiple translations).
2326 (while decode-map
2327 (setq elt (car decode-map) decode-map (cdr decode-map)
2328 trans (cdr elt))
2329 (if (and (vectorp trans) (= (length trans) 1))
2330 (setq trans (aref trans 0)))
2331 (if (vectorp trans)
2332 (setq multiple-list (cons elt multiple-list))
2333 (setq single-list (cons (cons (car elt) trans) single-list)
2334 width (if (stringp trans) (string-width trans)
2335 (char-width trans)))
2336 (if (> width single-trans-width)
2337 (setq single-trans-width width)))
2338 (setq width (length (car elt)))
2339 (if (> width single-key-width)
2340 (setq single-key-width width))
2341 (if (> width multiple-key-width)
2342 (setq multiple-key-width width)))
2343 (when single-list
2344 (setq col-width (+ single-key-width 1 single-trans-width 1)
2345 cols (/ frame-width col-width)
2346 rows (/ (length single-list) cols))
2347 (if (> (% (length single-list) cols) 0)
2348 (setq rows (1+ rows)))
2349 (insert "key")
2350 (indent-to (1+ single-key-width))
2351 (insert "char")
2352 (indent-to (1+ col-width))
2353 (insert "[type a key sequence to insert the corresponding character]\n")
2354 (setq pos (point))
2355 (insert-char ?\n (+ rows 2))
2356 (goto-char pos)
2357 (setq col (- col-width) row 0)
2358 (while single-list
2359 (setq elt (car single-list) single-list (cdr single-list))
2360 (when (= (% row rows) 0)
2361 (goto-char pos)
2362 (setq col (+ col col-width))
2363 (move-to-column col t)
2364 (insert-char ?- single-key-width)
2365 (insert ? )
2366 (insert-char ?- single-trans-width)
2367 (forward-line 1))
2368 (move-to-column col t)
2369 (insert (car elt))
2370 (indent-to (+ col single-key-width 1))
2371 (insert (cdr elt))
2372 (forward-line 1)
2373 (setq row (1+ row)))
2374 (goto-char (point-max)))
2375
2376 (when multiple-list
2377 (insert "key")
2378 (indent-to (1+ multiple-key-width))
2379 (insert "character(s) [type a key (sequence) and select one from the list]\n")
2380 (insert-char ?- multiple-key-width)
2381 (insert " ------------\n")
2382 (while multiple-list
2383 (setq elt (car multiple-list) multiple-list (cdr multiple-list))
2384 (insert (car elt))
2385 (indent-to multiple-key-width)
2386 (if (vectorp (cdr elt))
2387 (mapc (function
2388 (lambda (x)
2389 (let ((width (if (integerp x) (char-width x)
2390 (string-width x))))
2391 (when (> (+ (current-column) 1 width) frame-width)
2392 (insert "\n")
2393 (indent-to multiple-key-width))
2394 (insert " " x))))
2395 (cdr elt))
2396 (insert " " (cdr elt)))
2397 (insert ?\n))
2398 (insert ?\n))))
2399
2400 (define-button-type 'quail-keyboard-layout-button
2401 :supertype 'help-xref
2402 'help-function '(lambda (layout)
2403 (help-setup-xref `(quail-keyboard-layout-button ,layout) nil)
2404 (quail-show-keyboard-layout layout))
2405 'help-echo (purecopy "mouse-2, RET: show keyboard layout"))
2406
2407 (define-button-type 'quail-keyboard-customize-button
2408 :supertype 'help-customize-variable
2409 'help-echo (purecopy "mouse-2, RET: customize keyboard layout"))
2410
2411 (defun quail-help (&optional package)
2412 "Show brief description of the current Quail package.
2413 Optional arg PACKAGE specifies the name of alternative Quail
2414 package to describe."
2415 (interactive)
2416 (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
2417 (default-enable-multibyte-characters enable-multibyte-characters)
2418 (package-def
2419 (if package
2420 (assoc package quail-package-alist)
2421 quail-current-package)))
2422 ;; At first, make sure that the help buffer has window.
2423 (help-setup-xref (list #'quail-help package) (interactive-p))
2424 (with-output-to-temp-buffer (help-buffer)
2425 (with-current-buffer standard-output
2426 (setq quail-current-package package-def)))
2427 ;; Then, insert text in the help buffer while paying attention to
2428 ;; the width of the frame in which the buffer displayed.
2429 (with-current-buffer (help-buffer)
2430 (setq buffer-read-only nil)
2431 (insert "Input method: " (quail-name)
2432 " (mode line indicator:"
2433 (quail-title)
2434 ")\n\n")
2435 (save-restriction
2436 (narrow-to-region (point) (point))
2437 (insert (quail-docstring))
2438 (goto-char (point-min))
2439 (with-syntax-table emacs-lisp-mode-syntax-table
2440 (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
2441 (let ((sym (intern-soft
2442 (buffer-substring (+ (match-beginning 0) 2)
2443 (1- (point))))))
2444 (if (and (boundp sym)
2445 (stringp (symbol-value sym)))
2446 (replace-match (symbol-value sym) t t)))))
2447 (goto-char (point-max)))
2448 (or (bolp)
2449 (insert "\n"))
2450 (insert "\n")
2451
2452 (let ((done-list nil))
2453 ;; Show keyboard layout if the current package requests it..
2454 (when (quail-show-layout)
2455 (insert "
2456 KEYBOARD LAYOUT
2457 ---------------
2458 This input method works by translating individual input characters.
2459 Assuming that your actual keyboard has the `")
2460 (help-insert-xref-button
2461 quail-keyboard-layout-type
2462 'quail-keyboard-layout-button
2463 quail-keyboard-layout-type)
2464 (insert "' layout,
2465 translation results in the following \"virtual\" keyboard layout:
2466 ")
2467 (setq done-list
2468 (quail-insert-kbd-layout quail-keyboard-layout))
2469 (insert "If your keyboard has a different layout, rearranged from
2470 `")
2471 (help-insert-xref-button
2472 "standard"
2473 'quail-keyboard-layout-button "standard")
2474 (insert "', the \"virtual\" keyboard you get with this input method
2475 will be rearranged in the same way.
2476
2477 You can set the variable `quail-keyboard-layout-type' to specify
2478 the physical layout of your keyboard; the tables shown in
2479 documentation of input methods including this one are based on the
2480 physical keyboard layout as specified with that variable.
2481 ")
2482 (help-insert-xref-button
2483 "[customize keyboard layout]"
2484 'quail-keyboard-customize-button 'quail-keyboard-layout-type)
2485 (insert "\n"))
2486
2487 ;; Show key sequences.
2488 (let ((decode-map (list 'decode-map))
2489 elt pos num)
2490 (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
2491 0 512 done-list))
2492 (when (> num 0)
2493 (insert "
2494 KEY SEQUENCE
2495 -----------
2496 ")
2497 (if (quail-show-layout)
2498 (insert "You can also input more characters")
2499 (insert "You can input characters"))
2500 (insert " by the following key sequences:\n")
2501 (quail-insert-decode-map decode-map))))
2502
2503 (quail-help-insert-keymap-description
2504 (quail-translation-keymap)
2505 "\
2506 KEY BINDINGS FOR TRANSLATION
2507 ----------------------------\n")
2508 (insert ?\n)
2509 (if (quail-conversion-keymap)
2510 (quail-help-insert-keymap-description
2511 (quail-conversion-keymap)
2512 "\
2513 KEY BINDINGS FOR CONVERSION
2514 ---------------------------\n"))
2515 (setq quail-current-package nil)
2516 ;; Resize the help window again, now that it has all its contents.
2517 (save-selected-window
2518 (select-window (get-buffer-window (current-buffer)))
2519 (run-hooks 'temp-buffer-show-hook)))))
2520
2521 (defun quail-help-insert-keymap-description (keymap &optional header)
2522 (let (pos1 pos2 eol)
2523 (setq pos1 (point))
2524 (if header
2525 (insert header))
2526 (save-excursion
2527 (insert (substitute-command-keys "\\{keymap}")))
2528 ;; Skip headers "key bindings", etc.
2529 (forward-line 3)
2530 (setq pos2 (point))
2531 (with-syntax-table emacs-lisp-mode-syntax-table
2532 (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
2533 (let ((sym (intern-soft (buffer-substring (match-beginning 0)
2534 (point)))))
2535 (if (and sym (fboundp sym)
2536 (or (eq (get sym 'quail-help) 'hide)
2537 (and (quail-deterministic)
2538 (eq (get sym 'quail-help) 'non-deterministic))))
2539 (delete-region (line-beginning-position)
2540 (1+ (line-end-position)))))))
2541 (goto-char pos2)
2542 (while (not (eobp))
2543 (if (looking-at "[ \t]*$")
2544 (delete-region (point) (1+ (line-end-position)))
2545 (forward-line 1)))
2546 (goto-char pos2)
2547 (if (eobp)
2548 (delete-region pos1 (point)))
2549 (goto-char (point-max))))
2550
2551 (defun quail-translation-help ()
2552 "Show help message while translating in Quail input method."
2553 (interactive)
2554 (if (not (eq this-command last-command))
2555 (let (state-msg keymap)
2556 (if (and quail-converting (= (length quail-current-key) 0))
2557 (setq state-msg
2558 (format "Converting string %S by input method %S.\n"
2559 quail-conversion-str (quail-name))
2560 keymap (quail-conversion-keymap))
2561 (setq state-msg
2562 (format "Translating key sequence %S by input method %S.\n"
2563 quail-current-key (quail-name))
2564 keymap (quail-translation-keymap)))
2565 (with-output-to-temp-buffer "*Help*"
2566 (with-current-buffer standard-output
2567 (insert state-msg)
2568 (quail-help-insert-keymap-description
2569 keymap
2570 "-----------------------\n")
2571 ;; Isn't this redundant ? -stef
2572 (help-mode)))))
2573 (let (scroll-help)
2574 (save-selected-window
2575 (select-window (get-buffer-window "*Help*"))
2576 (if (eq this-command last-command)
2577 (if (< (window-end) (point-max))
2578 (scroll-up)
2579 (if (> (window-start) (point-min))
2580 (set-window-start (selected-window) (point-min)))))
2581 (setq scroll-help
2582 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2583 "Type \\[quail-translation-help] to scroll up the help"
2584 (if (> (window-start) (point-min))
2585 "Type \\[quail-translation-help] to see the head of help"))))
2586 (if scroll-help
2587 (progn
2588 (message "%s" (substitute-command-keys scroll-help))
2589 (sit-for 1)
2590 (message nil)
2591 (quail-update-guidance)
2592 ))))
2593 \f
2594 ;; Quail map generator from state transition table.
2595
2596 (defun quail-map-from-table (table)
2597 "Make quail map from state transition table TABLE.
2598
2599 TABLE is an alist, the form is:
2600 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2601
2602 STATE-n are symbols to denote state. STATE-0 is the initial state.
2603
2604 TRANSITION-n-m are transition rules from STATE-n, and have the form
2605 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2606 RULES is a symbol whose value is an alist of keys \(string) vs the
2607 correponding characters or strings. The format of the symbol value of
2608 RULES is the same as arguments to `quail-define-rules'.
2609
2610 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2611 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2612 transition is allowed, i.e. STATE-x may be STATE-n.
2613
2614 If TRANSITION-n-m has the form RULES, the transition terminates
2615 when keys in RULES are input.
2616
2617 The generated map can be set for the current Quail package by the
2618 function `quail-install-map' (which see)."
2619 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2620 tail elt)
2621 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2622 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2623 ;; Set key sequence mapping rules in cdr part of each element.
2624 (while table
2625 (quail-map-from-table-1 state-alist (car table))
2626 (setq table (cdr table)))
2627
2628 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2629 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2630 ;; them with MAPPING-RULES of STATE-x to make elements of
2631 ;; STATE-ALIST valid Quail maps.
2632 (setq tail state-alist)
2633 (while tail
2634 (setq elt (car tail) tail (cdr tail))
2635 (quail-map-from-table-2 state-alist elt))
2636
2637 ;; Return the Quail map for the initial state.
2638 (car state-alist)))
2639
2640 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2641 ;; mapping rules in the element of STATE-ALIST that corresponds to
2642 ;; STATE according to TRANSITION ...
2643 (defun quail-map-from-table-1 (state-alist state-info)
2644 (let* ((state (car state-info))
2645 (map (assq state state-alist))
2646 (transitions (cdr state-info))
2647 elt)
2648 (while transitions
2649 (setq elt (car transitions) transitions (cdr transitions))
2650 (let (rules dst-state key trans)
2651 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2652 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2653 (if (consp elt)
2654 (setq rules (symbol-value (car elt))
2655 ;; Set (STATE-x) as branches for all keys in RULES.
2656 ;; It is replaced with actual branches for STATE-x
2657 ;; later in `quail-map-from-table-2'.
2658 dst-state (list (cdr elt)))
2659 (setq rules (symbol-value elt)))
2660 (while rules
2661 (setq key (car (car rules)) trans (cdr (car rules))
2662 rules (cdr rules))
2663 (if (stringp trans)
2664 (if (= (length trans) 1)
2665 (setq trans (aref trans 0))
2666 (setq trans (string-to-vector trans))))
2667 (set-nested-alist key trans map nil dst-state))))))
2668
2669 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2670 ;; the form is:
2671 ;; (STATE (CHAR NESTED-ALIST) ...)
2672 ;; NESTED-ALIST is a nested alist; the form is:
2673 ;; (TRANS (CHAR NESTED-ALIST) ...)
2674 ;; or
2675 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2676 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2677 ;; (cdr (assq STATE-x STATE-ALIST))
2678
2679 (defun quail-map-from-table-2 (state-alist element)
2680 (let ((prev element)
2681 (tail (cdr element))
2682 elt)
2683 (while (cdr tail)
2684 (setq elt (car tail) prev tail tail (cdr tail))
2685 (quail-map-from-table-2 state-alist (cdr elt)))
2686 (setq elt (car tail))
2687 (if (consp elt)
2688 (quail-map-from-table-2 state-alist (cdr elt))
2689 (setcdr prev (cdr (assq elt state-alist))))))
2690
2691 ;; Concatenate translations for all heading substrings of KEY in the
2692 ;; current Quail map. Here, `heading substring' means (substring KEY
2693 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2694 (defun quail-lookup-map-and-concat (key)
2695 (let* ((len (length key))
2696 (translation-list nil)
2697 map)
2698 (while (> len 0)
2699 (setq map (quail-lookup-key key len)
2700 len (1- len))
2701 (if map
2702 (let* ((def (quail-map-definition map))
2703 (trans (if (consp def) (aref (cdr def) (car (car def)))
2704 def)))
2705 (if (integerp trans)
2706 (setq trans (char-to-string trans)))
2707 (setq translation-list (cons trans translation-list)))))
2708 (apply 'concat translation-list)))
2709
2710 \f
2711 (defvar quail-directory-name "quail"
2712 "Name of Quail directory which contains Quail packages.
2713 This is a sub-directory of LEIM directory.")
2714
2715 ;;;###autoload
2716 (defun quail-update-leim-list-file (dirname &rest dirnames)
2717 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2718 DIRNAME is a directory containing Emacs input methods;
2719 normally, it should specify the `leim' subdirectory
2720 of the Emacs source tree.
2721
2722 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2723 and update the file \"leim-list.el\" in DIRNAME.
2724
2725 When called from a program, the remaining arguments are additional
2726 directory names to search for Quail packages under `quail' subdirectory
2727 of each directory."
2728 (interactive "FDirectory of LEIM: ")
2729 (setq dirname (expand-file-name dirname))
2730 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2731 quail-dirs list-buf pkg-list pkg-buf pos)
2732 (if (not (file-writable-p leim-list))
2733 (error "Can't write to file \"%s\"" leim-list))
2734 (message "Updating %s ..." leim-list)
2735 (setq list-buf (find-file-noselect leim-list))
2736
2737 ;; At first, clean up the file.
2738 (with-current-buffer list-buf
2739 (goto-char 1)
2740
2741 ;; Insert the correct header.
2742 (if (looking-at (regexp-quote leim-list-header))
2743 (goto-char (match-end 0))
2744 (insert leim-list-header))
2745 (setq pos (point))
2746 (if (not (re-search-forward leim-list-entry-regexp nil t))
2747 nil
2748
2749 ;; Remove garbages after the header.
2750 (goto-char (match-beginning 0))
2751 (if (< pos (point))
2752 (delete-region pos (point)))
2753
2754 ;; Remove all entries for Quail.
2755 (while (re-search-forward leim-list-entry-regexp nil 'move)
2756 (goto-char (match-beginning 0))
2757 (setq pos (point))
2758 (condition-case nil
2759 (let ((form (read list-buf)))
2760 (when (equal (nth 3 form) ''quail-use-package)
2761 (if (eolp) (forward-line 1))
2762 (delete-region pos (point))))
2763 (error
2764 ;; Delete the remaining contents because it seems that
2765 ;; this file is broken.
2766 (message "Garbage in %s deleted" leim-list)
2767 (delete-region pos (point-max)))))))
2768
2769 ;; Search for `quail' subdirectory under each DIRNAMES.
2770 (setq dirnames (cons dirname dirnames))
2771 (let ((l dirnames))
2772 (while l
2773 (setcar l (expand-file-name (car l)))
2774 (setq dirname (expand-file-name quail-directory-name (car l)))
2775 (if (file-readable-p dirname)
2776 (setq quail-dirs (cons dirname quail-dirs))
2777 (message "%s doesn't have `%s' subdirectory, just ignored"
2778 (car l) quail-directory-name)
2779 (setq quail-dirs (cons nil quail-dirs)))
2780 (setq l (cdr l)))
2781 (setq quail-dirs (nreverse quail-dirs)))
2782
2783 ;; Insert input method registering forms.
2784 (while quail-dirs
2785 (setq dirname (car quail-dirs))
2786 (when dirname
2787 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2788 (while pkg-list
2789 (message "Checking %s ..." (car pkg-list))
2790 (with-temp-buffer
2791 (insert-file-contents (car pkg-list))
2792 (goto-char (point-min))
2793 (while (search-forward "(quail-define-package" nil t)
2794 (goto-char (match-beginning 0))
2795 (condition-case nil
2796 (let ((form (read (current-buffer))))
2797 (with-current-buffer list-buf
2798 (insert
2799 (format "(register-input-method
2800 %S %S '%s
2801 %S %S
2802 %S)\n"
2803 (nth 1 form) ; PACKAGE-NAME
2804 (nth 2 form) ; LANGUAGE
2805 'quail-use-package ; ACTIVATE-FUNC
2806 (nth 3 form) ; PACKAGE-TITLE
2807 (progn ; PACKAGE-DESCRIPTION (one line)
2808 (string-match ".*" (nth 5 form))
2809 (match-string 0 (nth 5 form)))
2810 (file-relative-name ; PACKAGE-FILENAME
2811 (file-name-sans-extension (car pkg-list))
2812 (car dirnames))))))
2813 (error
2814 ;; Ignore the remaining contents of this file.
2815 (goto-char (point-max))
2816 (message "Some part of \"%s\" is broken" (car pkg-list))))))
2817 (setq pkg-list (cdr pkg-list)))
2818 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2819
2820 ;; At last, write out LEIM list file.
2821 (with-current-buffer list-buf
2822 (setq buffer-file-coding-system 'iso-2022-7bit)
2823 (save-buffer 0))
2824 (kill-buffer list-buf)
2825 (message "Updating %s ... done" leim-list)))
2826 \f
2827 (defun quail-advice (args)
2828 "Advise users about the characters input by the current Quail package.
2829 The argument is a parameterized event of the form:
2830 (quail-advice STRING)
2831 where STRING is a string containing the input characters.
2832 If STRING has property `advice' and the value is a function,
2833 call it with one argument STRING."
2834 (interactive "e")
2835 (let* ((string (nth 1 args))
2836 (func (get-text-property 0 'advice string)))
2837 (if (functionp func)
2838 (funcall func string))))
2839
2840 (global-set-key [quail-advice] 'quail-advice)
2841
2842 ;;
2843 (provide 'quail)
2844
2845 ;;; quail.el ends here