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