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