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