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