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