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