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