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