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