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