*** empty log message ***
[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 If STR has `advice' text property, append the following special event:
1273 \(quail-advice STR)"
1274 (let ((events (mapcar
1275 (lambda (c)
1276 ;; This gives us the chance to unify on input
1277 ;; (e.g. using ucs-tables.el).
1278 (or (and translation-table-for-input
1279 (aref translation-table-for-input c))
1280 c))
1281 str)))
1282 (if (or (get-text-property 0 'advice str)
1283 (next-single-property-change 0 'advice str))
1284 (setq events
1285 (nconc events (list (list 'quail-advice str)))))
1286 events))
1287
1288 (defvar quail-translating nil)
1289 (defvar quail-converting nil)
1290 (defvar quail-conversion-str nil)
1291
1292 (defun quail-input-method (key)
1293 (if (or buffer-read-only
1294 overriding-terminal-local-map
1295 overriding-local-map)
1296 (list key)
1297 (quail-setup-overlays (quail-conversion-keymap))
1298 (let ((modified-p (buffer-modified-p))
1299 (buffer-undo-list t))
1300 (unwind-protect
1301 (let ((input-string (if (quail-conversion-keymap)
1302 (quail-start-conversion key)
1303 (quail-start-translation key))))
1304 (setq quail-guidance-str "")
1305 (when (and (stringp input-string)
1306 (> (length input-string) 0))
1307 (if input-method-exit-on-first-char
1308 (list (aref input-string 0))
1309 (quail-input-string-to-events input-string))))
1310 (quail-delete-overlays)
1311 (set-buffer-modified-p modified-p)
1312 ;; Run this hook only when the current input method doesn't require
1313 ;; conversion. When conversion is required, the conversion function
1314 ;; should run this hook at a proper timing.
1315 (unless (quail-conversion-keymap)
1316 (run-hooks 'input-method-after-insert-chunk-hook))))))
1317
1318 (defun quail-overlay-region-events (overlay)
1319 (let ((start (overlay-start overlay))
1320 (end (overlay-end overlay)))
1321 (if (< start end)
1322 (prog1
1323 (string-to-list (buffer-substring start end))
1324 (delete-region start end)))))
1325
1326 (defsubst quail-delete-region ()
1327 "Delete the text in the current translation region of Quail."
1328 (if (overlay-start quail-overlay)
1329 (delete-region (overlay-start quail-overlay)
1330 (overlay-end quail-overlay))))
1331
1332 (defun quail-start-translation (key)
1333 "Start translation of the typed character KEY by the current Quail package.
1334 Return the input string."
1335 ;; Check the possibility of translating KEY.
1336 ;; If KEY is nil, we can anyway start translation.
1337 (if (or (and (integerp key)
1338 (assq (if (quail-kbd-translate)
1339 (quail-keyboard-translate key) key)
1340 (cdr (quail-map))))
1341 (null key))
1342 ;; OK, we can start translation.
1343 (let* ((echo-keystrokes 0)
1344 (help-char nil)
1345 (overriding-terminal-local-map (quail-translation-keymap))
1346 (generated-events nil)
1347 (input-method-function nil)
1348 (modified-p (buffer-modified-p))
1349 last-command-event last-command this-command)
1350 (setq quail-current-key ""
1351 quail-current-str ""
1352 quail-translating t)
1353 (if key
1354 (setq unread-command-events (cons key unread-command-events)))
1355 (while quail-translating
1356 (set-buffer-modified-p modified-p)
1357 (quail-show-guidance)
1358 (let* ((keyseq (read-key-sequence
1359 (and input-method-use-echo-area
1360 (concat input-method-previous-message
1361 quail-current-str))
1362 nil nil t))
1363 (cmd (lookup-key (quail-translation-keymap) keyseq)))
1364 (if (if key
1365 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
1366 (eq cmd 'quail-self-insert-command))
1367 (progn
1368 (setq last-command-event (aref keyseq (1- (length keyseq)))
1369 last-command this-command
1370 this-command cmd)
1371 (setq key t)
1372 (condition-case err
1373 (call-interactively cmd)
1374 (quail-error (message "%s" (cdr err)) (beep))))
1375 ;; KEYSEQ is not defined in the translation keymap.
1376 ;; Let's return the event(s) to the caller.
1377 (setq unread-command-events
1378 (string-to-list (this-single-command-raw-keys)))
1379 (setq quail-translating nil))))
1380 (quail-delete-region)
1381 quail-current-str)
1382
1383 ;; Since KEY doesn't start any translation, just return it.
1384 ;; But translate KEY if necessary.
1385 (if (quail-kbd-translate)
1386 (setq key (quail-keyboard-translate key)))
1387 (char-to-string key)))
1388
1389 (defun quail-start-conversion (key)
1390 "Start conversion of the typed character KEY by the current Quail package.
1391 Return the input string."
1392 ;; Check the possibility of translating KEY.
1393 ;; If KEY is nil, we can anyway start translation.
1394 (if (or (and (integerp key)
1395 (assq (if (quail-kbd-translate)
1396 (quail-keyboard-translate key) key)
1397 (cdr (quail-map))))
1398 (null key))
1399 ;; Ok, we can start translation and conversion.
1400 (let* ((echo-keystrokes 0)
1401 (help-char nil)
1402 (overriding-terminal-local-map (quail-conversion-keymap))
1403 (generated-events nil)
1404 (input-method-function nil)
1405 (modified-p (buffer-modified-p))
1406 last-command-event last-command this-command)
1407 (setq quail-current-key ""
1408 quail-current-str ""
1409 quail-translating t
1410 quail-converting t
1411 quail-conversion-str "")
1412 (if key
1413 (setq unread-command-events (cons key unread-command-events)))
1414 (while quail-converting
1415 (set-buffer-modified-p modified-p)
1416 (or quail-translating
1417 (progn
1418 (setq quail-current-key ""
1419 quail-current-str ""
1420 quail-translating t)
1421 (quail-setup-overlays nil)))
1422 (quail-show-guidance)
1423 (let* ((keyseq (read-key-sequence
1424 (and input-method-use-echo-area
1425 (concat input-method-previous-message
1426 quail-conversion-str
1427 quail-current-str))
1428 nil nil t))
1429 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1430 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1431 (progn
1432 (setq last-command-event (aref keyseq (1- (length keyseq)))
1433 last-command this-command
1434 this-command cmd)
1435 (setq key t)
1436 (condition-case err
1437 (call-interactively cmd)
1438 (quail-error (message "%s" (cdr err)) (beep)))
1439 (or quail-translating
1440 (progn
1441 (if quail-current-str
1442 (setq quail-conversion-str
1443 (concat quail-conversion-str
1444 (if (stringp quail-current-str)
1445 quail-current-str
1446 (char-to-string quail-current-str)))))
1447 (if (or input-method-exit-on-first-char
1448 (= (length quail-conversion-str) 0))
1449 (setq quail-converting nil)))))
1450 ;; KEYSEQ is not defined in the conversion keymap.
1451 ;; Let's return the event(s) to the caller.
1452 (setq unread-command-events
1453 (string-to-list (this-single-command-raw-keys)))
1454 (setq quail-converting nil))))
1455 (setq quail-translating nil)
1456 (if (overlay-start quail-conv-overlay)
1457 (delete-region (overlay-start quail-conv-overlay)
1458 (overlay-end quail-conv-overlay)))
1459 (if (> (length quail-conversion-str) 0)
1460 quail-conversion-str))
1461
1462 ;; Since KEY doesn't start any translation, just return it.
1463 ;; But translate KEY if necessary.
1464 (if (quail-kbd-translate)
1465 (setq key (quail-keyboard-translate key)))
1466 (char-to-string key)))
1467
1468 (defun quail-terminate-translation ()
1469 "Terminate the translation of the current key."
1470 (setq quail-translating nil)
1471 (setq quail-guidance-str " "))
1472
1473 (defun quail-select-current ()
1474 "Accept the currently selected translation."
1475 (interactive)
1476 (quail-terminate-translation))
1477
1478 (defun quail-update-translation (control-flag)
1479 "Update the current translation status according to CONTROL-FLAG.
1480 If CONTROL-FLAG is integer value, it is the number of keys in the
1481 head `quail-current-key' which can be translated. The remaining keys
1482 are put back to `unread-command-events' to be handled again. If
1483 CONTROL-FLAG is t, terminate the translation for the whole keys in
1484 `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation
1485 with more keys."
1486 (let ((func (quail-update-translation-function)))
1487 (if func
1488 (setq control-flag (funcall func control-flag))
1489 (cond ((numberp control-flag)
1490 (let ((len (length quail-current-key)))
1491 (if (= control-flag 0)
1492 (setq quail-current-str
1493 (if (quail-kbd-translate)
1494 (quail-keyseq-translate quail-current-key)
1495 quail-current-key)))
1496 (or input-method-exit-on-first-char
1497 (while (> len control-flag)
1498 (setq len (1- len))
1499 (setq unread-command-events
1500 (cons (aref quail-current-key len)
1501 unread-command-events))))))
1502 ((null control-flag)
1503 (unless quail-current-str
1504 (setq quail-current-str
1505 (if (quail-kbd-translate)
1506 (quail-keyseq-translate quail-current-key)
1507 quail-current-key))
1508 (if (and input-method-exit-on-first-char
1509 (quail-simple))
1510 (setq control-flag t)))))))
1511 (or input-method-use-echo-area
1512 (let (pos)
1513 (quail-delete-region)
1514 (setq pos (point))
1515 (insert quail-current-str)
1516 (move-overlay quail-overlay pos (point))
1517 (if (overlayp quail-conv-overlay)
1518 (if (not (overlay-start quail-conv-overlay))
1519 (move-overlay quail-conv-overlay pos (point))
1520 (if (< (overlay-end quail-conv-overlay) (point))
1521 (move-overlay quail-conv-overlay
1522 (overlay-start quail-conv-overlay)
1523 (point)))))))
1524 (let (quail-current-str)
1525 (quail-update-guidance))
1526 (or (stringp quail-current-str)
1527 (setq quail-current-str (char-to-string quail-current-str)))
1528 (if control-flag
1529 (quail-terminate-translation)))
1530
1531 (defun quail-self-insert-command ()
1532 "Translate the typed key by the current Quail map, and insert."
1533 (interactive "*")
1534 (setq quail-current-key
1535 (concat quail-current-key (char-to-string last-command-event)))
1536 (or (catch 'quail-tag
1537 (quail-update-translation (quail-translate-key))
1538 t)
1539 ;; If someone throws for `quail-tag' by value nil, we exit from
1540 ;; translation mode.
1541 (setq quail-translating nil)))
1542
1543 (defun quail-map-definition (map)
1544 "Return the actual definition part of Quail map MAP."
1545 (let ((def (car map)))
1546 (if (and (consp def) (not (vectorp (cdr def))))
1547 (setq def (car def)))
1548 (if (eq def t)
1549 (setq def nil))
1550 def))
1551
1552 (defun quail-get-current-str (len def)
1553 "Return string to be shown as current translation of key sequence.
1554 LEN is the length of the sequence. DEF is a definition part of the
1555 Quail map for the sequence."
1556 (or (and (consp def) (aref (cdr def) (car (car def))))
1557 def
1558 (and (> len 1)
1559 (let ((str (quail-get-current-str
1560 (1- len)
1561 (quail-map-definition (quail-lookup-key
1562 quail-current-key (1- len))))))
1563 (if str
1564 (concat (if (stringp str) str (char-to-string str))
1565 (substring quail-current-key (1- len) len)))))))
1566
1567 (defvar quail-guidance-translations-starting-column 20)
1568
1569 (defun quail-update-current-translations (&optional relative-index)
1570 "Update `quail-current-translations'.
1571 Make RELATIVE-INDEX the current translation."
1572 (let* ((indices (car quail-current-translations))
1573 (cur (car indices))
1574 (start (nth 1 indices))
1575 (end (nth 2 indices)))
1576 ;; Validate the index number of current translation.
1577 (if (< cur 0)
1578 (setcar indices (setq cur 0))
1579 (if (>= cur (length (cdr quail-current-translations)))
1580 (setcar indices
1581 (setq cur (1- (length (cdr quail-current-translations)))))))
1582
1583 (if (or (null end) ; We have not yet calculated END.
1584 (< cur start) ; We moved to the previous block.
1585 (>= cur end)) ; We moved to the next block.
1586 (let ((len (length (cdr quail-current-translations)))
1587 (maxcol (- (window-width)
1588 quail-guidance-translations-starting-column))
1589 (block (nth 3 indices))
1590 col idx width trans num-items blocks)
1591 (if (< cur start)
1592 ;; We must calculate from the head.
1593 (setq start 0 block 0)
1594 (if end ; i.e. (>= cur end)
1595 (setq start end)))
1596 (setq idx start col 0 end start num-items 0)
1597 ;; Loop until we hit the tail, or reach the block of CUR.
1598 (while (and (< idx len) (>= cur end))
1599 (if (= num-items 0)
1600 (setq start idx col 0 block (1+ block)))
1601 (setq trans (aref (cdr quail-current-translations) idx))
1602 (setq width (if (integerp trans) (char-width trans)
1603 (string-width trans)))
1604 (setq col (+ col width 3) num-items (1+ num-items))
1605 (if (and (> num-items 0)
1606 (or (>= col maxcol) (> num-items 10)))
1607 (setq end idx num-items 0)
1608 (setq idx (1+ idx))))
1609 (setcar (nthcdr 3 indices) block)
1610 (if (>= idx len)
1611 (progn
1612 ;; We hit the tail before reaching MAXCOL.
1613 (setq end idx)
1614 (setcar (nthcdr 4 indices) block)))
1615 (setcar (cdr indices) start)
1616 (setcar (nthcdr 2 indices) end)))
1617 (if relative-index
1618 (if (>= (+ start relative-index) end)
1619 (setcar indices (1- end))
1620 (setcar indices (+ start relative-index))))
1621 (setq quail-current-str
1622 (aref (cdr quail-current-translations) (car indices)))
1623 (or (stringp quail-current-str)
1624 (setq quail-current-str (char-to-string quail-current-str)))))
1625
1626 (defun quail-translate-key ()
1627 "Translate the current key sequence according to the current Quail map.
1628 Return t if we can terminate the translation.
1629 Return nil if the current key sequence may be followed by more keys.
1630 Return number if we can't find any translation for the current key
1631 sequence. The number is the count of valid keys in the current
1632 sequence counting from the head."
1633 (let* ((len (length quail-current-key))
1634 (map (quail-lookup-key quail-current-key len))
1635 def ch)
1636 (if map
1637 (let ((def (quail-map-definition map)))
1638 (setq quail-current-str (quail-get-current-str len def))
1639 ;; Return t only if we can terminate the current translation.
1640 (and
1641 ;; No alternative translations.
1642 (or (null (consp def)) (= (length (cdr def)) 1))
1643 ;; No translation for the longer key.
1644 (null (cdr map))
1645 ;; No shorter breaking point.
1646 (or (null (quail-maximum-shortest))
1647 (< len 3)
1648 (null (quail-lookup-key quail-current-key (1- len)))
1649 (null (quail-lookup-key
1650 (substring quail-current-key -2 -1) 1)))))
1651
1652 ;; There's no translation for the current key sequence. Before
1653 ;; giving up, we must check two possibilities.
1654 (cond ((and
1655 (quail-maximum-shortest)
1656 (>= len 3)
1657 (setq def (quail-map-definition
1658 (quail-lookup-key quail-current-key (- len 2))))
1659 (quail-lookup-key (substring quail-current-key -2) 2))
1660 ;; Now the sequence is "...ABCD", which can be split into
1661 ;; "...AB" and "CD..." to get valid translation.
1662 ;; At first, get translation of "...AB".
1663 (setq quail-current-str (quail-get-current-str (- len 2) def))
1664 ;; Then, return the length of "...AB".
1665 (- len 2))
1666
1667 ((and (> len 0)
1668 (quail-lookup-key (substring quail-current-key 0 -1))
1669 quail-current-translations
1670 (not (quail-deterministic))
1671 (setq ch (aref quail-current-key (1- len)))
1672 (>= ch ?0) (<= ch ?9))
1673 ;; A numeric key is entered to select a desirable translation.
1674 (setq quail-current-key (substring quail-current-key 0 -1))
1675 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1676 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1677 (quail-update-current-translations ch)
1678 ;; And, we can terminate the current translation.
1679 t)
1680
1681 (t
1682 ;; No way to handle the last character in this context.
1683 (setq def (quail-map-definition
1684 (quail-lookup-key quail-current-key (1- len))))
1685 (if def (setq quail-current-str
1686 (quail-get-current-str (1- len) def)))
1687 (1- len))))))
1688
1689 (defun quail-next-translation ()
1690 "Select next translation in the current batch of candidates."
1691 (interactive)
1692 (if quail-current-translations
1693 (let ((indices (car quail-current-translations)))
1694 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1695 ;; We are already at the tail.
1696 (beep)
1697 (setcar indices (1+ (car indices)))
1698 (quail-update-current-translations)
1699 (quail-update-translation nil)))
1700 (setq unread-command-events
1701 (cons last-command-event unread-command-events))
1702 (quail-terminate-translation)))
1703
1704 (defun quail-prev-translation ()
1705 "Select previous translation in the current batch of candidates."
1706 (interactive)
1707 (if quail-current-translations
1708 (let ((indices (car quail-current-translations)))
1709 (if (= (car indices) 0)
1710 ;; We are already at the head.
1711 (beep)
1712 (setcar indices (1- (car indices)))
1713 (quail-update-current-translations)
1714 (quail-update-translation nil)))
1715 (setq unread-command-events
1716 (cons last-command-event unread-command-events))
1717 (quail-terminate-translation)))
1718
1719 (defun quail-next-translation-block ()
1720 "Select from the next block of translations."
1721 (interactive)
1722 (if quail-current-translations
1723 (let* ((indices (car quail-current-translations))
1724 (offset (- (car indices) (nth 1 indices))))
1725 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1726 ;; We are already at the last block.
1727 (beep)
1728 (setcar indices (+ (nth 2 indices) offset))
1729 (quail-update-current-translations)
1730 (quail-update-translation nil)))
1731 (setq unread-command-events
1732 (cons last-command-event unread-command-events))
1733 (quail-terminate-translation)))
1734
1735 (defun quail-prev-translation-block ()
1736 "Select the previous batch of 10 translation candidates."
1737 (interactive)
1738 (if quail-current-translations
1739 (let* ((indices (car quail-current-translations))
1740 (offset (- (car indices) (nth 1 indices))))
1741 (if (= (nth 1 indices) 0)
1742 ;; We are already at the first block.
1743 (beep)
1744 (setcar indices (1- (nth 1 indices)))
1745 (quail-update-current-translations)
1746 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1747 (progn
1748 (setcar indices (+ (nth 1 indices) offset))
1749 (quail-update-current-translations)))
1750 (quail-update-translation nil)))
1751 (setq unread-command-events
1752 (cons last-command-event unread-command-events))
1753 (quail-terminate-translation)))
1754
1755 (defun quail-abort-translation ()
1756 "Abort translation and delete the current Quail key sequence."
1757 (interactive)
1758 (quail-delete-region)
1759 (setq quail-current-str nil)
1760 (quail-terminate-translation))
1761
1762 (defun quail-delete-last-char ()
1763 "Delete the last input character from the current Quail key sequence."
1764 (interactive)
1765 (if (= (length quail-current-key) 1)
1766 (quail-abort-translation)
1767 (setq quail-current-key (substring quail-current-key 0 -1))
1768 (quail-delete-region)
1769 (quail-update-translation (quail-translate-key))))
1770
1771 ;; For conversion mode.
1772
1773 (defsubst quail-point-in-conversion-region ()
1774 "Return non-nil value if the point is in conversion region of Quail mode."
1775 (let (start pos)
1776 (and (setq start (overlay-start quail-conv-overlay))
1777 (>= (setq pos (point)) start)
1778 (<= pos (overlay-end quail-conv-overlay)))))
1779
1780 (defun quail-conversion-backward-char ()
1781 (interactive)
1782 (if (<= (point) (overlay-start quail-conv-overlay))
1783 (quail-error "Beginning of conversion region"))
1784 (setq quail-translating nil)
1785 (forward-char -1))
1786
1787 (defun quail-conversion-forward-char ()
1788 (interactive)
1789 (if (>= (point) (overlay-end quail-conv-overlay))
1790 (quail-error "End of conversion region"))
1791 (setq quail-translating nil)
1792 (forward-char 1))
1793
1794 (defun quail-conversion-beginning-of-region ()
1795 (interactive)
1796 (setq quail-translating nil)
1797 (goto-char (overlay-start quail-conv-overlay)))
1798
1799 (defun quail-conversion-end-of-region ()
1800 (interactive)
1801 (setq quail-translating nil)
1802 (goto-char (overlay-end quail-conv-overlay)))
1803
1804 (defun quail-conversion-delete-char ()
1805 (interactive)
1806 (setq quail-translating nil)
1807 (if (>= (point) (overlay-end quail-conv-overlay))
1808 (quail-error "End of conversion region"))
1809 (delete-char 1)
1810 (let ((start (overlay-start quail-conv-overlay))
1811 (end (overlay-end quail-conv-overlay)))
1812 (setq quail-conversion-str (buffer-substring start end))
1813 (if (= start end)
1814 (setq quail-converting nil))))
1815
1816 (defun quail-conversion-delete-tail ()
1817 (interactive)
1818 (if (>= (point) (overlay-end quail-conv-overlay))
1819 (quail-error "End of conversion region"))
1820 (delete-region (point) (overlay-end quail-conv-overlay))
1821 (let ((start (overlay-start quail-conv-overlay))
1822 (end (overlay-end quail-conv-overlay)))
1823 (setq quail-conversion-str (buffer-substring start end))
1824 (if (= start end)
1825 (setq quail-converting nil))))
1826
1827 (defun quail-conversion-backward-delete-char ()
1828 (interactive)
1829 (if (> (length quail-current-key) 0)
1830 (quail-delete-last-char)
1831 (if (<= (point) (overlay-start quail-conv-overlay))
1832 (quail-error "Beginning of conversion region"))
1833 (delete-char -1)
1834 (let ((start (overlay-start quail-conv-overlay))
1835 (end (overlay-end quail-conv-overlay)))
1836 (setq quail-conversion-str (buffer-substring start end))
1837 (if (= start end)
1838 (setq quail-converting nil)))))
1839
1840 (defun quail-do-conversion (func &rest args)
1841 "Call FUNC to convert text in the current conversion region of Quail.
1842 Remaining args are for FUNC."
1843 (delete-overlay quail-overlay)
1844 (apply func args))
1845
1846 (defun quail-no-conversion ()
1847 "Do no conversion of the current conversion region of Quail."
1848 (interactive)
1849 (setq quail-converting nil))
1850
1851 ;; Guidance, Completion, and Help buffer handlers.
1852
1853 (defun quail-make-guidance-frame ()
1854 "Make a new one-line frame for Quail guidance."
1855 (let* ((fparam (frame-parameters))
1856 (top (cdr (assq 'top fparam)))
1857 (border (cdr (assq 'border-width fparam)))
1858 (internal-border (cdr (assq 'internal-border-width fparam)))
1859 (newtop (- top
1860 (frame-char-height) (* internal-border 2) (* border 2))))
1861 (if (< newtop 0)
1862 (setq newtop (+ top (frame-pixel-height) internal-border border)))
1863 (make-frame (append '((user-position . t) (height . 1)
1864 (minibuffer)
1865 (menu-bar-lines . 0) (tool-bar-lines . 0))
1866 (cons (cons 'top newtop) fparam)))))
1867
1868 (defun quail-setup-completion-buf ()
1869 "Setup Quail completion buffer."
1870 (unless (buffer-live-p quail-completion-buf)
1871 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1872 (setq quail-completion-buf (get-buffer-create "*Quail Completions*")))
1873 (with-current-buffer quail-completion-buf
1874 (setq quail-overlay (make-overlay 1 1))
1875 (overlay-put quail-overlay 'face 'highlight))))
1876
1877 (defun quail-require-guidance-buf ()
1878 "Return t iff the current Quail package requires showing guidance buffer."
1879 (and input-method-verbose-flag
1880 (if (eq input-method-verbose-flag 'default)
1881 (not (and (eq (selected-window) (minibuffer-window))
1882 (quail-simple)))
1883 (if (eq input-method-verbose-flag 'complex-only)
1884 (not (quail-simple))
1885 t))))
1886
1887
1888 ;; Quail specific version of minibuffer-message. It displays STRING
1889 ;; with timeout 1000000 seconds instead of two seconds.
1890
1891 (defun quail-minibuffer-message (string)
1892 (message nil)
1893 (let ((point-max (point-max))
1894 (inhibit-quit t))
1895 (save-excursion
1896 (goto-char point-max)
1897 (insert string))
1898 (sit-for 1000000)
1899 (delete-region point-max (point-max))
1900 (when quit-flag
1901 (setq quit-flag nil
1902 unread-command-events '(7)))))
1903
1904 (defun quail-show-guidance ()
1905 "Display a guidance for Quail input method in some window.
1906 The guidance is normally displayed at the echo area,
1907 or in a newly created frame (if the current buffer is a
1908 minibuffer and the selected frame has no other windows)."
1909 ;; At first, setup a buffer for completion.
1910 (quail-setup-completion-buf)
1911 (bury-buffer quail-completion-buf)
1912
1913 ;; Then, show the guidance.
1914 (when (and (quail-require-guidance-buf)
1915 (null unread-command-events)
1916 (null unread-post-input-method-events))
1917 (if (or (eq (selected-window) (minibuffer-window))
1918 input-method-use-echo-area)
1919 (if (eq (minibuffer-window) (frame-root-window))
1920 ;; Use another frame. It is sure that we are using some
1921 ;; window system.
1922 (let ((guidance quail-guidance-str))
1923 (or (frame-live-p quail-guidance-frame)
1924 (setq quail-guidance-frame
1925 (quail-make-guidance-frame)))
1926 (or (buffer-live-p quail-guidance-buf)
1927 (setq quail-guidance-buf
1928 (get-buffer-create " *Quail-guidance*")))
1929 (save-excursion
1930 (set-buffer quail-guidance-buf)
1931 (erase-buffer)
1932 (setq cursor-type nil)
1933 (insert guidance))
1934 (set-window-buffer (frame-root-window quail-guidance-frame)
1935 quail-guidance-buf)
1936 (quail-minibuffer-message
1937 (format " [%s]" current-input-method-title)))
1938 ;; Show the guidance in the next line of the currrent
1939 ;; minibuffer.
1940 (quail-minibuffer-message
1941 (format " [%s]\n%s"
1942 current-input-method-title quail-guidance-str)))
1943 ;; Show the guidance in echo area without logging.
1944 (let ((message-log-max nil))
1945 (message "%s" quail-guidance-str)))))
1946
1947 (defun quail-hide-guidance ()
1948 "Hide the Quail guidance."
1949 (when (and (quail-require-guidance-buf)
1950 (or (eq (selected-window) (minibuffer-window))
1951 input-method-use-echo-area)
1952 (eq (minibuffer-window) (frame-root-window)))
1953 ;; We are using another frame for the guidance.
1954 (if (frame-live-p quail-guidance-frame)
1955 (delete-frame quail-guidance-frame))
1956 (if (buffer-live-p quail-guidance-buf)
1957 (kill-buffer quail-guidance-buf))))
1958
1959 (defun quail-update-guidance ()
1960 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1961 ;; Update the guidance string.
1962 (when (quail-require-guidance-buf)
1963 (let ((guidance (quail-guidance)))
1964 (cond ((or (eq guidance t)
1965 (consp guidance))
1966 ;; Show the current possible translations.
1967 (setq quail-guidance-str
1968 (quail-get-translations)))
1969 ((null guidance)
1970 ;; Show the current input keys.
1971 (let ((key quail-current-key))
1972 (if (quail-kbd-translate)
1973 (setq key (quail-keyseq-translate key)))
1974 (setq quail-guidance-str (if (stringp key) key (string key)))))
1975 (t
1976 (setq quail-guidance-str " ")))))
1977
1978 ;; Update completion buffer if displayed now. We highlight the
1979 ;; selected candidate string in *Completion* buffer if any.
1980 (let ((win (get-buffer-window quail-completion-buf))
1981 key str pos)
1982 (if win
1983 (save-excursion
1984 (setq str (if (stringp quail-current-str)
1985 quail-current-str
1986 (if (numberp quail-current-str)
1987 (char-to-string quail-current-str)))
1988 key quail-current-key)
1989 (set-buffer quail-completion-buf)
1990 (goto-char (point-min))
1991 (if (null (search-forward (concat " " key ":") nil t))
1992 (delete-overlay quail-overlay)
1993 (setq pos (point))
1994 (if (and str (search-forward (concat "." str) nil t))
1995 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
1996 (move-overlay quail-overlay (match-beginning 0) (point)))
1997 ;; Now POS points end of KEY and (point) points end of STR.
1998 (if (pos-visible-in-window-p (point) win)
1999 ;; STR is already visible.
2000 nil
2001 ;; We want to make both KEY and STR visible, but if the
2002 ;; window is too short, make at least STR visible.
2003 (setq pos (progn (point) (goto-char pos)))
2004 (beginning-of-line)
2005 (set-window-start win (point))
2006 (if (not (pos-visible-in-window-p pos win))
2007 (set-window-start win pos))
2008 ))))))
2009
2010 (defun quail-get-translations ()
2011 "Return a string containing the current possible translations."
2012 (let ((map (quail-lookup-key quail-current-key))
2013 (str (copy-sequence quail-current-key)))
2014 (if quail-current-translations
2015 (quail-update-current-translations))
2016
2017 ;; Show the current key.
2018 (let ((guidance (quail-guidance)))
2019 (if (listp guidance)
2020 ;; We must replace thetyped key with the specified PROMPTKEY.
2021 (dotimes (i (length str))
2022 (let ((prompt-key (cdr (assoc (aref str i) guidance))))
2023 (if prompt-key
2024 (aset str i (aref prompt-key 0)))))))
2025
2026 ;; Show followable keys.
2027 (if (and (> (length quail-current-key) 0) (cdr map))
2028 (setq str
2029 (format "%s[%s]"
2030 str
2031 (concat (sort (mapcar (function (lambda (x) (car x)))
2032 (cdr map))
2033 '<)))))
2034 ;; Show list of translations.
2035 (if (and quail-current-translations
2036 (not (quail-deterministic)))
2037 (let* ((indices (car quail-current-translations))
2038 (cur (car indices))
2039 (start (nth 1 indices))
2040 (end (nth 2 indices))
2041 (idx start))
2042 (if (< (string-width str)
2043 (- quail-guidance-translations-starting-column 7))
2044 (setq str
2045 (concat str
2046 (make-string
2047 (- quail-guidance-translations-starting-column
2048 7 (string-width str))
2049 32))))
2050 (setq str (format "%s(%02d/%s)"
2051 str (nth 3 indices)
2052 (if (nth 4 indices)
2053 (format "%02d" (nth 4 indices))
2054 "??")))
2055 (while (< idx end)
2056 (let ((len (length str))
2057 (trans (aref (cdr quail-current-translations) idx)))
2058 (or (stringp trans)
2059 (setq trans (string trans)))
2060 (setq str (format "%s %d.%s"
2061 str
2062 (if (= (- idx start) 9) 0
2063 (1+ (- idx start)))
2064 trans))
2065 (if (= idx cur)
2066 (put-text-property (+ len 3) (length str)
2067 'face 'highlight str))
2068 (setq idx (1+ idx))))))
2069
2070 str))
2071
2072 (defvar quail-completion-max-depth 5
2073 "The maximum depth of Quail completion list.")
2074
2075 (defun quail-completion ()
2076 "List all completions for the current key.
2077 All possible translations of the current key and whole possible longer keys
2078 are shown (at most to the depth specified `quail-completion-max-depth')."
2079 (interactive)
2080 (quail-setup-completion-buf)
2081 (let ((win (get-buffer-window quail-completion-buf 'visible))
2082 (key quail-current-key)
2083 (map (quail-lookup-key quail-current-key))
2084 (require-update nil))
2085 (with-current-buffer quail-completion-buf
2086 (if (and win
2087 (equal key quail-current-key)
2088 (eq last-command 'quail-completion))
2089 ;; The window for Quail completion buffer has already been
2090 ;; shown. We just scroll it appropriately.
2091 (if (pos-visible-in-window-p (point-max) win)
2092 (set-window-start win (point-min))
2093 (let ((other-window-scroll-buffer quail-completion-buf)
2094 ;; This nil binding is necessary to surely scroll
2095 ;; quail-completion-buf.
2096 (minibuffer-scroll-window nil))
2097 (scroll-other-window)))
2098 (setq quail-current-key key)
2099 (erase-buffer)
2100 (insert "Possible completion and corresponding translations are:\n")
2101 (quail-completion-1 key map 1)
2102 (goto-char (point-min))
2103 (display-buffer (current-buffer))
2104 (setq require-update t)))
2105 (if require-update
2106 (quail-update-guidance)))
2107 (setq this-command 'quail-completion))
2108
2109 (defun quail-completion-1 (key map indent)
2110 "List all completions of KEY in MAP with indentation INDENT."
2111 (let ((len (length key)))
2112 (indent-to indent)
2113 (insert key ":")
2114 (if (and (symbolp map) (fboundp map))
2115 (setq map (funcall map key len)))
2116 (if (car map)
2117 (quail-completion-list-translations map key (+ indent len 1))
2118 (insert " -\n"))
2119 (setq indent (+ indent 2))
2120 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
2121 (let ((l (cdr map))
2122 (newkey (make-string (1+ len) 0))
2123 (i 0))
2124 (if (functionp l)
2125 (setq l (funcall l)))
2126 ;; Set KEY in the first LEN characters of NEWKEY.
2127 (while (< i len)
2128 (aset newkey i (aref key i))
2129 (setq i (1+ i)))
2130 (setq l (reverse l))
2131 (while l ; L = ((CHAR . DEFN) ....) ;
2132 (aset newkey len (car (car l)))
2133 (quail-completion-1 newkey (cdr (car l)) indent)
2134 (setq l (cdr l)))))))
2135
2136 (defun quail-completion-list-translations (map key indent)
2137 "List all possible translations of KEY in Quail MAP with indentation INDENT."
2138 (let (beg (translations
2139 (quail-get-translation (car map) key (length key))))
2140 (if (integerp translations)
2141 (progn
2142 (insert "(1/1) 1.")
2143 ;; Endow the character `translations' with `mouse-face' text
2144 ;; property to enable `mouse-2' completion.
2145 (setq beg (point))
2146 (insert translations)
2147 (put-text-property beg (point) 'mouse-face 'highlight)
2148 (insert "\n"))
2149 ;; We need only vector part.
2150 (setq translations (cdr translations))
2151 ;; Insert every 10 elements with indices in a line.
2152 (let ((len (length translations))
2153 (i 0)
2154 num)
2155 (while (< i len)
2156 (when (zerop (% i 10))
2157 (when (>= i 10)
2158 (insert "\n")
2159 (indent-to indent))
2160 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
2161 ;; We show the last digit of FROM while converting
2162 ;; 0,1,..,9 to 1,2,..,0.
2163 (insert (format " %d." (% (1+ i) 10)))
2164 (setq beg (point))
2165 (insert (aref translations i))
2166 ;; Passing the mouse over a character will highlight.
2167 (put-text-property beg (point) 'mouse-face 'highlight)
2168 (setq i (1+ i)))
2169 (insert "\n")))))
2170
2171 (defun quail-mouse-choose-completion (event)
2172 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
2173 (interactive "e")
2174 ;; This function is an exact copy of the mouse.el function
2175 ;; `mouse-choose-completion' except that we:
2176 ;; 1) add two lines from `choose-completion' in simple.el to give
2177 ;; the `mouse-2' click a little more leeway.
2178 ;; 2) don't bury *Quail Completions* buffer, so comment a section, and
2179 ;; 3) delete/terminate the current quail selection here.
2180 ;; Give temporary modes such as isearch a chance to turn off.
2181 (run-hooks 'mouse-leave-buffer-hook)
2182 (let ((buffer (window-buffer))
2183 choice
2184 base-size)
2185 (with-current-buffer (window-buffer (posn-window (event-start event)))
2186 (if completion-reference-buffer
2187 (setq buffer completion-reference-buffer))
2188 (setq base-size completion-base-size)
2189 (save-excursion
2190 (goto-char (posn-point (event-start event)))
2191 (let (beg end)
2192 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2193 (setq end (point) beg (1+ (point))))
2194 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
2195 (setq end (1- (point)) beg (point)))
2196 (if (null beg)
2197 (quail-error "No completion here"))
2198 (setq beg (previous-single-property-change beg 'mouse-face))
2199 (setq end (or (next-single-property-change end 'mouse-face)
2200 (point-max)))
2201 (setq choice (buffer-substring beg end)))))
2202 ; (let ((owindow (selected-window)))
2203 ; (select-window (posn-window (event-start event)))
2204 ; (if (and (one-window-p t 'selected-frame)
2205 ; (window-dedicated-p (selected-window)))
2206 ; ;; This is a special buffer's frame
2207 ; (iconify-frame (selected-frame))
2208 ; (or (window-dedicated-p (selected-window))
2209 ; (bury-buffer)))
2210 ; (select-window owindow))
2211 (quail-delete-region)
2212 (quail-choose-completion-string choice buffer base-size)
2213 (quail-terminate-translation)))
2214
2215 ;; BASE-SIZE here is for compatibility with an (unused) arg of a
2216 ;; previous implementation.
2217 (defun quail-choose-completion-string (choice &optional buffer base-size)
2218 (setq quail-current-str choice)
2219 (choose-completion-string choice buffer))
2220
2221 (defun quail-build-decode-map (map-list key decode-map num
2222 &optional maxnum ignores)
2223 "Build a decoding map.
2224 Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
2225 vs the corresponding translations defined in the Quail map
2226 specified by the first element MAP-LIST. Each pair has the form
2227 \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
2228 \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
2229 is a key sequence to reach MAP.
2230 Optional 5th arg MAXNUM limits the number of accumulated pairs.
2231 Optional 6th arg IGNORES is a list of translations to ignore."
2232 (let* ((map (car map-list))
2233 (translation (quail-get-translation (car map) key (length key)))
2234 elt)
2235 (cond ((integerp translation)
2236 ;; Accept only non-ASCII chars not listed in IGNORES.
2237 (when (and (> translation 255) (not (memq translation ignores)))
2238 (setcdr decode-map
2239 (cons (cons key translation) (cdr decode-map)))
2240 (setq num (1+ num))))
2241 ((consp translation)
2242 (setq translation (cdr translation))
2243 (let ((multibyte nil))
2244 (mapc (function (lambda (x)
2245 ;; Accept only non-ASCII chars not
2246 ;; listed in IGNORES.
2247 (if (and (if (integerp x) (> x 255)
2248 (> (string-bytes x) (length x)))
2249 (not (member x ignores)))
2250 (setq multibyte t))))
2251 translation)
2252 (when multibyte
2253 (setcdr decode-map
2254 (cons (cons key translation) (cdr decode-map)))
2255 (setq num (+ num (length translation)))))))
2256 (if (and maxnum (> num maxnum))
2257 (- num)
2258 (setq map (cdr map))
2259 ;; Recursively check the deeper map.
2260 (while (and map (>= num 0))
2261 (setq elt (car map) map (cdr map))
2262 (when (and (integerp (car elt)) (consp (cdr elt))
2263 (not (memq (cdr elt) map-list)))
2264 (setq num (quail-build-decode-map (cons (cdr elt) map-list)
2265 (format "%s%c" key (car elt))
2266 decode-map num maxnum ignores))))
2267 num)))
2268
2269 (defun quail-insert-decode-map (decode-map)
2270 "Insert pairs of key sequences vs the corresponding translations.
2271 These are stored in DECODE-MAP using the concise format. DECODE-MAP
2272 should be made by `quail-build-decode-map' (which see)."
2273 (setq decode-map
2274 (sort (cdr decode-map)
2275 (function (lambda (x y)
2276 (setq x (car x) y (car y))
2277 (or (> (length x) (length y))
2278 (and (= (length x) (length y))
2279 (not (string< x y))))))))
2280 (let ((frame-width (frame-width (window-frame (get-buffer-window
2281 (current-buffer) 'visible))))
2282 (single-key-width 3)
2283 (single-trans-width 4)
2284 (multiple-key-width 3)
2285 (single-list nil)
2286 (multiple-list nil)
2287 elt trans width pos cols rows col row str col-width)
2288 ;; Divide the elements of decoding map into single ones (i.e. the
2289 ;; one that has single translation) and multibyte ones (i.e. the
2290 ;; one that has multiple translations).
2291 (while decode-map
2292 (setq elt (car decode-map) decode-map (cdr decode-map)
2293 trans (cdr elt))
2294 (if (and (vectorp trans) (= (length trans) 1))
2295 (setq trans (aref trans 0)))
2296 (if (vectorp trans)
2297 (setq multiple-list (cons elt multiple-list))
2298 (setq single-list (cons (cons (car elt) trans) single-list)
2299 width (if (stringp trans) (string-width trans)
2300 (char-width trans)))
2301 (if (> width single-trans-width)
2302 (setq single-trans-width width)))
2303 (setq width (length (car elt)))
2304 (if (> width single-key-width)
2305 (setq single-key-width width))
2306 (if (> width multiple-key-width)
2307 (setq multiple-key-width width)))
2308 (when single-list
2309 (setq col-width (+ single-key-width 1 single-trans-width 1)
2310 cols (/ frame-width col-width)
2311 rows (/ (length single-list) cols))
2312 (if (> (% (length single-list) cols) 0)
2313 (setq rows (1+ rows)))
2314 (insert "key")
2315 (indent-to (1+ single-key-width))
2316 (insert "char")
2317 (indent-to (1+ col-width))
2318 (insert "[type a key sequence to insert the corresponding character]\n")
2319 (setq pos (point))
2320 (insert-char ?\n (+ rows 2))
2321 (goto-char pos)
2322 (setq col (- col-width) row 0)
2323 (while single-list
2324 (setq elt (car single-list) single-list (cdr single-list))
2325 (when (= (% row rows) 0)
2326 (goto-char pos)
2327 (setq col (+ col col-width))
2328 (move-to-column col t)
2329 (insert-char ?- single-key-width)
2330 (insert ? )
2331 (insert-char ?- single-trans-width)
2332 (forward-line 1))
2333 (move-to-column col t)
2334 (insert (car elt))
2335 (indent-to (+ col single-key-width 1))
2336 (insert (cdr elt))
2337 (forward-line 1)
2338 (setq row (1+ row)))
2339 (goto-char (point-max)))
2340
2341 (when multiple-list
2342 (insert "key")
2343 (indent-to (1+ multiple-key-width))
2344 (insert "character(s) [type a key (sequence) and select one from the list]\n")
2345 (insert-char ?- multiple-key-width)
2346 (insert " ------------\n")
2347 (while multiple-list
2348 (setq elt (car multiple-list) multiple-list (cdr multiple-list))
2349 (insert (car elt))
2350 (indent-to multiple-key-width)
2351 (if (vectorp (cdr elt))
2352 (mapc (function
2353 (lambda (x)
2354 (let ((width (if (integerp x) (char-width x)
2355 (string-width x))))
2356 (when (> (+ (current-column) 1 width) frame-width)
2357 (insert "\n")
2358 (indent-to multiple-key-width))
2359 (insert " " x))))
2360 (cdr elt))
2361 (insert " " (cdr elt)))
2362 (insert ?\n))
2363 (insert ?\n))))
2364
2365 (define-button-type 'quail-keyboard-layout-button
2366 :supertype 'help-xref
2367 'help-function '(lambda (layout)
2368 (help-setup-xref `(quail-keyboard-layout-button ,layout) nil)
2369 (quail-show-keyboard-layout layout))
2370 'help-echo (purecopy "mouse-2, RET: show keyboard layout"))
2371
2372 (define-button-type 'quail-keyboard-customize-button
2373 :supertype 'help-customize-variable
2374 'help-echo (purecopy "mouse-2, RET: customize keyboard layout"))
2375
2376 (defun quail-help (&optional package)
2377 "Show brief description of the current Quail package.
2378 Optional arg PACKAGE specifies the name of alternative Quail
2379 package to describe."
2380 (interactive)
2381 (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
2382 (default-enable-multibyte-characters enable-multibyte-characters)
2383 (package-def
2384 (if package
2385 (assoc package quail-package-alist)
2386 quail-current-package)))
2387 ;; At first, make sure that the help buffer has window.
2388 (let ((temp-buffer-show-hook nil))
2389 (with-output-to-temp-buffer (help-buffer)
2390 (with-current-buffer standard-output
2391 (setq quail-current-package package-def))))
2392 ;; Then, insert text in the help buffer while paying attention to
2393 ;; the width of the frame in which the buffer displayed.
2394 (with-current-buffer (help-buffer)
2395 (setq buffer-read-only nil)
2396 (insert "Input method: " (quail-name)
2397 " (mode line indicator:"
2398 (quail-title)
2399 ")\n\n")
2400 (save-restriction
2401 (narrow-to-region (point) (point))
2402 (insert (quail-docstring))
2403 (goto-char (point-min))
2404 (with-syntax-table emacs-lisp-mode-syntax-table
2405 (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
2406 (let ((sym (intern-soft
2407 (buffer-substring (+ (match-beginning 0) 2)
2408 (1- (point))))))
2409 (if (and (boundp sym)
2410 (stringp (symbol-value sym)))
2411 (replace-match (symbol-value sym) t t)))))
2412 (goto-char (point-max)))
2413 (or (bolp)
2414 (insert "\n"))
2415 (insert "\n")
2416
2417 (let ((done-list nil))
2418 ;; Show keyboard layout if the current package requests it..
2419 (when (quail-show-layout)
2420 (insert "
2421 KEYBOARD LAYOUT
2422 ---------------
2423 This input method works by translating individual input characters.
2424 Assuming that your actual keyboard has the `")
2425 (help-insert-xref-button
2426 quail-keyboard-layout-type
2427 'quail-keyboard-layout-button
2428 quail-keyboard-layout-type)
2429 (insert "' layout,
2430 translation results in the following \"virtual\" keyboard layout:
2431 ")
2432 (setq done-list
2433 (quail-insert-kbd-layout quail-keyboard-layout))
2434 (insert "If your keyboard has a different layout, rearranged from
2435 `")
2436 (help-insert-xref-button
2437 "standard"
2438 'quail-keyboard-layout-button "standard")
2439 (insert "', the \"virtual\" keyboard you get with this input method
2440 will be rearranged in the same way.
2441
2442 You can set the variable `quail-keyboard-layout-type' to specify
2443 the physical layout of your keyboard; the tables shown in
2444 documentation of input methods including this one are based on the
2445 physical keyboard layout as specified with that variable.
2446 ")
2447 (help-insert-xref-button
2448 "[customize keyboard layout]"
2449 'quail-keyboard-customize-button 'quail-keyboard-layout-type)
2450 (insert "\n"))
2451
2452 ;; Show key sequences.
2453 (let ((decode-map (list 'decode-map))
2454 elt pos num)
2455 (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
2456 0 512 done-list))
2457 (when (> num 0)
2458 (insert "
2459 KEY SEQUENCE
2460 ------------
2461 ")
2462 (if (quail-show-layout)
2463 (insert "You can also input more characters")
2464 (insert "You can input characters"))
2465 (insert " by the following key sequences:\n")
2466 (quail-insert-decode-map decode-map))))
2467
2468 (quail-help-insert-keymap-description
2469 (quail-translation-keymap)
2470 "\
2471 KEY BINDINGS FOR TRANSLATION
2472 ----------------------------\n")
2473 (insert ?\n)
2474 (if (quail-conversion-keymap)
2475 (quail-help-insert-keymap-description
2476 (quail-conversion-keymap)
2477 "\
2478 KEY BINDINGS FOR CONVERSION
2479 ---------------------------\n"))
2480 (setq quail-current-package nil)
2481 ;; Resize the help window again, now that it has all its contents.
2482 (save-selected-window
2483 (select-window (get-buffer-window (current-buffer) t))
2484 (run-hooks 'temp-buffer-show-hook)))))
2485
2486 (defun quail-help-insert-keymap-description (keymap &optional header)
2487 (let (pos1 pos2 eol)
2488 (setq pos1 (point))
2489 (if header
2490 (insert header))
2491 (save-excursion
2492 (insert (substitute-command-keys "\\{keymap}")))
2493 ;; Skip headers "key bindings", etc.
2494 (forward-line 3)
2495 (setq pos2 (point))
2496 (with-syntax-table emacs-lisp-mode-syntax-table
2497 (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
2498 (let ((sym (intern-soft (buffer-substring (match-beginning 0)
2499 (point)))))
2500 (if (and sym (fboundp sym)
2501 (or (eq (get sym 'quail-help) 'hide)
2502 (and (quail-deterministic)
2503 (eq (get sym 'quail-help) 'non-deterministic))))
2504 (delete-region (line-beginning-position)
2505 (1+ (line-end-position)))))))
2506 (goto-char pos2)
2507 (while (not (eobp))
2508 (if (looking-at "[ \t]*$")
2509 (delete-region (point) (1+ (line-end-position)))
2510 (forward-line 1)))
2511 (goto-char pos2)
2512 (if (eobp)
2513 (delete-region pos1 (point)))
2514 (goto-char (point-max))))
2515
2516 (defun quail-translation-help ()
2517 "Show help message while translating in Quail input method."
2518 (interactive)
2519 (if (not (eq this-command last-command))
2520 (let (state-msg keymap)
2521 (if (and quail-converting (= (length quail-current-key) 0))
2522 (setq state-msg
2523 (format "Converting string %S by input method %S.\n"
2524 quail-conversion-str (quail-name))
2525 keymap (quail-conversion-keymap))
2526 (setq state-msg
2527 (format "Translating key sequence %S by input method %S.\n"
2528 quail-current-key (quail-name))
2529 keymap (quail-translation-keymap)))
2530 (with-output-to-temp-buffer "*Help*"
2531 (with-current-buffer standard-output
2532 (insert state-msg)
2533 (quail-help-insert-keymap-description
2534 keymap
2535 "-----------------------\n")
2536 ;; Isn't this redundant ? -stef
2537 (help-mode)))))
2538 (let (scroll-help)
2539 (save-selected-window
2540 (select-window (get-buffer-window "*Help*"))
2541 (if (eq this-command last-command)
2542 (if (< (window-end) (point-max))
2543 (scroll-up)
2544 (if (> (window-start) (point-min))
2545 (set-window-start (selected-window) (point-min)))))
2546 (setq scroll-help
2547 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2548 "Type \\[quail-translation-help] to scroll up the help"
2549 (if (> (window-start) (point-min))
2550 "Type \\[quail-translation-help] to see the head of help"))))
2551 (if scroll-help
2552 (progn
2553 (message "%s" (substitute-command-keys scroll-help))
2554 (sit-for 1)
2555 (message nil)
2556 (quail-update-guidance)
2557 ))))
2558 \f
2559 ;; Quail map generator from state transition table.
2560
2561 (defun quail-map-from-table (table)
2562 "Make quail map from state transition table TABLE.
2563
2564 TABLE is an alist, the form is:
2565 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2566
2567 STATE-n are symbols to denote state. STATE-0 is the initial state.
2568
2569 TRANSITION-n-m are transition rules from STATE-n, and have the form
2570 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2571 RULES is a symbol whose value is an alist of keys \(string) vs the
2572 correponding characters or strings. The format of the symbol value of
2573 RULES is the same as arguments to `quail-define-rules'.
2574
2575 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2576 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2577 transition is allowed, i.e. STATE-x may be STATE-n.
2578
2579 If TRANSITION-n-m has the form RULES, the transition terminates
2580 when keys in RULES are input.
2581
2582 The generated map can be set for the current Quail package by the
2583 function `quail-install-map' (which see)."
2584 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2585 tail elt)
2586 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2587 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2588 ;; Set key sequence mapping rules in cdr part of each element.
2589 (while table
2590 (quail-map-from-table-1 state-alist (car table))
2591 (setq table (cdr table)))
2592
2593 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2594 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2595 ;; them with MAPPING-RULES of STATE-x to make elements of
2596 ;; STATE-ALIST valid Quail maps.
2597 (setq tail state-alist)
2598 (while tail
2599 (setq elt (car tail) tail (cdr tail))
2600 (quail-map-from-table-2 state-alist elt))
2601
2602 ;; Return the Quail map for the initial state.
2603 (car state-alist)))
2604
2605 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2606 ;; mapping rules in the element of STATE-ALIST that corresponds to
2607 ;; STATE according to TRANSITION ...
2608 (defun quail-map-from-table-1 (state-alist state-info)
2609 (let* ((state (car state-info))
2610 (map (assq state state-alist))
2611 (transitions (cdr state-info))
2612 elt)
2613 (while transitions
2614 (setq elt (car transitions) transitions (cdr transitions))
2615 (let (rules dst-state key trans)
2616 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2617 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2618 (if (consp elt)
2619 (setq rules (symbol-value (car elt))
2620 ;; Set (STATE-x) as branches for all keys in RULES.
2621 ;; It is replaced with actual branches for STATE-x
2622 ;; later in `quail-map-from-table-2'.
2623 dst-state (list (cdr elt)))
2624 (setq rules (symbol-value elt)))
2625 (while rules
2626 (setq key (car (car rules)) trans (cdr (car rules))
2627 rules (cdr rules))
2628 (if (stringp trans)
2629 (if (= (length trans) 1)
2630 (setq trans (aref trans 0))
2631 (setq trans (string-to-vector trans))))
2632 (set-nested-alist key trans map nil dst-state))))))
2633
2634 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2635 ;; the form is:
2636 ;; (STATE (CHAR NESTED-ALIST) ...)
2637 ;; NESTED-ALIST is a nested alist; the form is:
2638 ;; (TRANS (CHAR NESTED-ALIST) ...)
2639 ;; or
2640 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2641 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2642 ;; (cdr (assq STATE-x STATE-ALIST))
2643
2644 (defun quail-map-from-table-2 (state-alist element)
2645 (let ((prev element)
2646 (tail (cdr element))
2647 elt)
2648 (while (cdr tail)
2649 (setq elt (car tail) prev tail tail (cdr tail))
2650 (quail-map-from-table-2 state-alist (cdr elt)))
2651 (setq elt (car tail))
2652 (if (consp elt)
2653 (quail-map-from-table-2 state-alist (cdr elt))
2654 (setcdr prev (cdr (assq elt state-alist))))))
2655
2656 ;; Concatenate translations for all heading substrings of KEY in the
2657 ;; current Quail map. Here, `heading substring' means (substring KEY
2658 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2659 (defun quail-lookup-map-and-concat (key)
2660 (let* ((len (length key))
2661 (translation-list nil)
2662 map)
2663 (while (> len 0)
2664 (setq map (quail-lookup-key key len)
2665 len (1- len))
2666 (if map
2667 (let* ((def (quail-map-definition map))
2668 (trans (if (consp def) (aref (cdr def) (car (car def)))
2669 def)))
2670 (if (integerp trans)
2671 (setq trans (char-to-string trans)))
2672 (setq translation-list (cons trans translation-list)))))
2673 (apply 'concat translation-list)))
2674
2675 \f
2676 (defvar quail-directory-name "quail"
2677 "Name of Quail directory which contains Quail packages.
2678 This is a sub-directory of LEIM directory.")
2679
2680 ;;;###autoload
2681 (defun quail-update-leim-list-file (dirname &rest dirnames)
2682 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2683 DIRNAME is a directory containing Emacs input methods;
2684 normally, it should specify the `leim' subdirectory
2685 of the Emacs source tree.
2686
2687 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2688 and update the file \"leim-list.el\" in DIRNAME.
2689
2690 When called from a program, the remaining arguments are additional
2691 directory names to search for Quail packages under `quail' subdirectory
2692 of each directory."
2693 (interactive "FDirectory of LEIM: ")
2694 (setq dirname (expand-file-name dirname))
2695 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2696 quail-dirs list-buf pkg-list pkg-buf pos)
2697 (if (not (file-writable-p leim-list))
2698 (error "Can't write to file \"%s\"" leim-list))
2699 (message "Updating %s ..." leim-list)
2700 (setq list-buf (find-file-noselect leim-list))
2701
2702 ;; At first, clean up the file.
2703 (with-current-buffer list-buf
2704 (goto-char 1)
2705
2706 ;; Insert the correct header.
2707 (if (looking-at (regexp-quote leim-list-header))
2708 (goto-char (match-end 0))
2709 (insert leim-list-header))
2710 (setq pos (point))
2711 (if (not (re-search-forward leim-list-entry-regexp nil t))
2712 nil
2713
2714 ;; Remove garbages after the header.
2715 (goto-char (match-beginning 0))
2716 (if (< pos (point))
2717 (delete-region pos (point)))
2718
2719 ;; Remove all entries for Quail.
2720 (while (re-search-forward leim-list-entry-regexp nil 'move)
2721 (goto-char (match-beginning 0))
2722 (setq pos (point))
2723 (condition-case nil
2724 (let ((form (read list-buf)))
2725 (when (equal (nth 3 form) ''quail-use-package)
2726 (if (eolp) (forward-line 1))
2727 (delete-region pos (point))))
2728 (error
2729 ;; Delete the remaining contents because it seems that
2730 ;; this file is broken.
2731 (message "Garbage in %s deleted" leim-list)
2732 (delete-region pos (point-max)))))))
2733
2734 ;; Search for `quail' subdirectory under each DIRNAMES.
2735 (setq dirnames (cons dirname dirnames))
2736 (let ((l dirnames))
2737 (while l
2738 (setcar l (expand-file-name (car l)))
2739 (setq dirname (expand-file-name quail-directory-name (car l)))
2740 (if (file-readable-p dirname)
2741 (setq quail-dirs (cons dirname quail-dirs))
2742 (message "%s doesn't have `%s' subdirectory, just ignored"
2743 (car l) quail-directory-name)
2744 (setq quail-dirs (cons nil quail-dirs)))
2745 (setq l (cdr l)))
2746 (setq quail-dirs (nreverse quail-dirs)))
2747
2748 ;; Insert input method registering forms.
2749 (while quail-dirs
2750 (setq dirname (car quail-dirs))
2751 (when dirname
2752 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2753 (while pkg-list
2754 (message "Checking %s ..." (car pkg-list))
2755 (with-temp-buffer
2756 (insert-file-contents (car pkg-list))
2757 (goto-char (point-min))
2758 ;; Don't get fooled by commented-out code.
2759 (while (re-search-forward "^[ \t]*(quail-define-package" nil t)
2760 (goto-char (match-beginning 0))
2761 (condition-case nil
2762 (let ((form (read (current-buffer))))
2763 (with-current-buffer list-buf
2764 (insert
2765 (format "(register-input-method
2766 %S %S '%s
2767 %S %S
2768 %S)\n"
2769 (nth 1 form) ; PACKAGE-NAME
2770 (nth 2 form) ; LANGUAGE
2771 'quail-use-package ; ACTIVATE-FUNC
2772 (nth 3 form) ; PACKAGE-TITLE
2773 (progn ; PACKAGE-DESCRIPTION (one line)
2774 (string-match ".*" (nth 5 form))
2775 (match-string 0 (nth 5 form)))
2776 (file-relative-name ; PACKAGE-FILENAME
2777 (file-name-sans-extension (car pkg-list))
2778 (car dirnames))))))
2779 (error
2780 ;; Ignore the remaining contents of this file.
2781 (goto-char (point-max))
2782 (message "Some part of \"%s\" is broken" (car pkg-list))))))
2783 (setq pkg-list (cdr pkg-list)))
2784 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2785
2786 ;; At last, write out LEIM list file.
2787 (with-current-buffer list-buf
2788 (let ((coding-system-for-write 'iso-2022-7bit))
2789 (save-buffer 0)))
2790 (kill-buffer list-buf)
2791 (message "Updating %s ... done" leim-list)))
2792 \f
2793 (defun quail-advice (args)
2794 "Advise users about the characters input by the current Quail package.
2795 The argument is a parameterized event of the form:
2796 (quail-advice STRING)
2797 where STRING is a string containing the input characters.
2798 If STRING has property `advice' and the value is a function,
2799 call it with one argument STRING."
2800 (interactive "e")
2801 (let* ((string (nth 1 args))
2802 (func (get-text-property 0 'advice string)))
2803 (if (functionp func)
2804 (funcall func string))))
2805
2806 (global-set-key [quail-advice] 'quail-advice)
2807
2808 ;;
2809 (provide 'quail)
2810
2811 ;;; quail.el ends here