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