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