Add arch taglines
[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))))
53e89326
KH
1697 (if def (setq quail-current-str
1698 (quail-get-current-str (1- len) def)))
4ed46869
KH
1699 (1- len))))))
1700
1701(defun quail-next-translation ()
1702 "Select next translation in the current batch of candidates."
1703 (interactive)
1704 (if quail-current-translations
7d842556
KH
1705 (let ((indices (car quail-current-translations)))
1706 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
5871092a 1707 ;; We are already at the tail.
7d842556
KH
1708 (beep)
1709 (setcar indices (1+ (car indices)))
1710 (quail-update-current-translations)
1711 (quail-update-translation nil)))
b58fc490
KH
1712 (setq unread-command-events
1713 (cons last-command-event unread-command-events))
1714 (quail-terminate-translation)))
4ed46869
KH
1715
1716(defun quail-prev-translation ()
1717 "Select previous translation in the current batch of candidates."
1718 (interactive)
1719 (if quail-current-translations
7d842556
KH
1720 (let ((indices (car quail-current-translations)))
1721 (if (= (car indices) 0)
1722 ;; We are already at the head.
1723 (beep)
1724 (setcar indices (1- (car indices)))
1725 (quail-update-current-translations)
1726 (quail-update-translation nil)))
b58fc490
KH
1727 (setq unread-command-events
1728 (cons last-command-event unread-command-events))
1729 (quail-terminate-translation)))
4ed46869
KH
1730
1731(defun quail-next-translation-block ()
7d842556 1732 "Select from the next block of translations."
4ed46869
KH
1733 (interactive)
1734 (if quail-current-translations
7d842556
KH
1735 (let* ((indices (car quail-current-translations))
1736 (offset (- (car indices) (nth 1 indices))))
1737 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1738 ;; We are already at the last block.
1739 (beep)
1740 (setcar indices (+ (nth 2 indices) offset))
1741 (quail-update-current-translations)
1742 (quail-update-translation nil)))
b58fc490 1743 (setq unread-command-events
d91eafdf 1744 (cons last-command-event unread-command-events))
b58fc490 1745 (quail-terminate-translation)))
4ed46869
KH
1746
1747(defun quail-prev-translation-block ()
1748 "Select the previous batch of 10 translation candidates."
1749 (interactive)
7d842556
KH
1750 (if quail-current-translations
1751 (let* ((indices (car quail-current-translations))
1752 (offset (- (car indices) (nth 1 indices))))
1753 (if (= (nth 1 indices) 0)
1754 ;; We are already at the first block.
1755 (beep)
1756 (setcar indices (1- (nth 1 indices)))
1757 (quail-update-current-translations)
1758 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1759 (progn
1760 (setcar indices (+ (nth 1 indices) offset))
1761 (quail-update-current-translations)))
1762 (quail-update-translation nil)))
b58fc490
KH
1763 (setq unread-command-events
1764 (cons last-command-event unread-command-events))
1765 (quail-terminate-translation)))
4ed46869 1766
4ed46869
KH
1767(defun quail-abort-translation ()
1768 "Abort translation and delete the current Quail key sequence."
1769 (interactive)
1770 (quail-delete-region)
d91eafdf 1771 (setq quail-current-str nil)
4ed46869
KH
1772 (quail-terminate-translation))
1773
1774(defun quail-delete-last-char ()
1775 "Delete the last input character from the current Quail key sequence."
1776 (interactive)
1777 (if (= (length quail-current-key) 1)
1778 (quail-abort-translation)
1779 (setq quail-current-key (substring quail-current-key 0 -1))
817e162f 1780 (quail-delete-region)
4ed46869
KH
1781 (quail-update-translation (quail-translate-key))))
1782
1783;; For conversion mode.
1784
407c6b94
KH
1785(defsubst quail-point-in-conversion-region ()
1786 "Return non-nil value if the point is in conversion region of Quail mode."
1787 (let (start pos)
1788 (and (setq start (overlay-start quail-conv-overlay))
1789 (>= (setq pos (point)) start)
1790 (<= pos (overlay-end quail-conv-overlay)))))
1791
4ed46869
KH
1792(defun quail-conversion-backward-char ()
1793 (interactive)
1794 (if (<= (point) (overlay-start quail-conv-overlay))
b58fc490
KH
1795 (quail-error "Beginning of conversion region"))
1796 (setq quail-translating nil)
4ed46869
KH
1797 (forward-char -1))
1798
1799(defun quail-conversion-forward-char ()
1800 (interactive)
1801 (if (>= (point) (overlay-end quail-conv-overlay))
b58fc490
KH
1802 (quail-error "End of conversion region"))
1803 (setq quail-translating nil)
4ed46869
KH
1804 (forward-char 1))
1805
1806(defun quail-conversion-beginning-of-region ()
1807 (interactive)
b45d8d64 1808 (setq quail-translating nil)
4ed46869
KH
1809 (goto-char (overlay-start quail-conv-overlay)))
1810
1811(defun quail-conversion-end-of-region ()
1812 (interactive)
b45d8d64 1813 (setq quail-translating nil)
4ed46869
KH
1814 (goto-char (overlay-end quail-conv-overlay)))
1815
1816(defun quail-conversion-delete-char ()
1817 (interactive)
b45d8d64 1818 (setq quail-translating nil)
4ed46869 1819 (if (>= (point) (overlay-end quail-conv-overlay))
b58fc490 1820 (quail-error "End of conversion region"))
4ed46869 1821 (delete-char 1)
d91eafdf
KH
1822 (let ((start (overlay-start quail-conv-overlay))
1823 (end (overlay-end quail-conv-overlay)))
1824 (setq quail-conversion-str (buffer-substring start end))
1825 (if (= start end)
1826 (setq quail-converting nil))))
4ed46869 1827
b45d8d64
KH
1828(defun quail-conversion-delete-tail ()
1829 (interactive)
1830 (if (>= (point) (overlay-end quail-conv-overlay))
1831 (quail-error "End of conversion region"))
1832 (delete-region (point) (overlay-end quail-conv-overlay))
d91eafdf
KH
1833 (let ((start (overlay-start quail-conv-overlay))
1834 (end (overlay-end quail-conv-overlay)))
1835 (setq quail-conversion-str (buffer-substring start end))
1836 (if (= start end)
1837 (setq quail-converting nil))))
b45d8d64 1838
4ed46869
KH
1839(defun quail-conversion-backward-delete-char ()
1840 (interactive)
407c6b94
KH
1841 (if (> (length quail-current-key) 0)
1842 (quail-delete-last-char)
1843 (if (<= (point) (overlay-start quail-conv-overlay))
1844 (quail-error "Beginning of conversion region"))
1845 (delete-char -1)
1846 (let ((start (overlay-start quail-conv-overlay))
1847 (end (overlay-end quail-conv-overlay)))
1848 (setq quail-conversion-str (buffer-substring start end))
1849 (if (= start end)
1850 (setq quail-converting nil)))))
4ed46869
KH
1851
1852(defun quail-do-conversion (func &rest args)
1853 "Call FUNC to convert text in the current conversion region of Quail.
1854Remaining args are for FUNC."
1855 (delete-overlay quail-overlay)
1856 (apply func args))
1857
1858(defun quail-no-conversion ()
1859 "Do no conversion of the current conversion region of Quail."
1860 (interactive)
b45d8d64 1861 (setq quail-converting nil))
4ed46869
KH
1862
1863;; Guidance, Completion, and Help buffer handlers.
1864
17388a62
KH
1865(defun quail-make-guidance-frame ()
1866 "Make a new one-line frame for Quail guidance."
7d842556
KH
1867 (let* ((fparam (frame-parameters))
1868 (top (cdr (assq 'top fparam)))
1869 (border (cdr (assq 'border-width fparam)))
1870 (internal-border (cdr (assq 'internal-border-width fparam)))
1871 (newtop (- top
1872 (frame-char-height) (* internal-border 2) (* border 2))))
1873 (if (< newtop 0)
17388a62
KH
1874 (setq newtop (+ top (frame-pixel-height) internal-border border)))
1875 (make-frame (append '((user-position . t) (height . 1)
1876 (minibuffer)
1877 (menu-bar-lines . 0) (tool-bar-lines . 0))
1878 (cons (cons 'top newtop) fparam)))))
7d842556 1879
05204016 1880(defun quail-setup-completion-buf ()
74ace46a 1881 "Setup Quail completion buffer."
05204016 1882 (unless (buffer-live-p quail-completion-buf)
83600a29
KH
1883 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1884 (setq quail-completion-buf (get-buffer-create "*Quail Completions*")))
24790d0c 1885 (with-current-buffer quail-completion-buf
05204016
KH
1886 (setq quail-overlay (make-overlay 1 1))
1887 (overlay-put quail-overlay 'face 'highlight))))
1888
b55ba027 1889(defun quail-require-guidance-buf ()
74ace46a 1890 "Return t iff the current Quail package requires showing guidance buffer."
b55ba027 1891 (and input-method-verbose-flag
9a0eac6e
KH
1892 (if (eq input-method-verbose-flag 'default)
1893 (not (and (eq (selected-window) (minibuffer-window))
1894 (quail-simple)))
1895 (if (eq input-method-verbose-flag 'complex-only)
1896 (not (quail-simple))
1897 t))))
b55ba027 1898
17388a62
KH
1899
1900;; Quail specific version of minibuffer-message. It displays STRING
1901;; with timeout 1000000 seconds instead of two seconds.
1902
1903(defun quail-minibuffer-message (string)
1904 (message nil)
1905 (let ((point-max (point-max))
1906 (inhibit-quit t))
1907 (save-excursion
1908 (goto-char point-max)
1909 (insert string))
1910 (sit-for 1000000)
1911 (delete-region point-max (point-max))
1912 (when quit-flag
1913 (setq quit-flag nil
1914 unread-command-events '(7)))))
1915
1916(defun quail-show-guidance ()
1917 "Display a guidance for Quail input method in some window.
1918The guidance is normally displayed at the echo area,
1919or in a newly created frame (if the current buffer is a
1920minibuffer and the selected frame has no other windows)."
1921 ;; At first, setup a buffer for completion.
05204016 1922 (quail-setup-completion-buf)
17388a62
KH
1923 (bury-buffer quail-completion-buf)
1924
1925 ;; Then, show the guidance.
1926 (when (and (quail-require-guidance-buf)
1927 (null unread-command-events)
1928 (null unread-post-input-method-events))
1929 (if (or (eq (selected-window) (minibuffer-window))
1930 input-method-use-echo-area)
1931 (if (eq (minibuffer-window) (frame-root-window))
1932 ;; Use another frame. It is sure that we are using some
1933 ;; window system.
1934 (let ((guidance quail-guidance-str))
1935 (or (frame-live-p quail-guidance-frame)
1936 (setq quail-guidance-frame
1937 (quail-make-guidance-frame)))
1938 (or (buffer-live-p quail-guidance-buf)
1939 (setq quail-guidance-buf
1940 (get-buffer-create " *Quail-guidance*")))
1941 (save-excursion
1942 (set-buffer quail-guidance-buf)
1943 (erase-buffer)
1944 (setq cursor-type nil)
1945 (insert guidance))
1946 (set-window-buffer (frame-root-window quail-guidance-frame)
1947 quail-guidance-buf)
1948 (quail-minibuffer-message
1949 (format " [%s]" current-input-method-title)))
1950 ;; Show the guidance in the next line of the currrent
1951 ;; minibuffer.
1952 (quail-minibuffer-message
1953 (format " [%s]\n%s"
1954 current-input-method-title quail-guidance-str)))
1955 ;; Show the guidance in echo area without logging.
1956 (let ((message-log-max nil))
1957 (message "%s" quail-guidance-str)))))
1958
1959(defun quail-hide-guidance ()
1960 "Hide the Quail guidance."
1961 (when (and (quail-require-guidance-buf)
1962 (or (eq (selected-window) (minibuffer-window))
1963 input-method-use-echo-area)
1964 (eq (minibuffer-window) (frame-root-window)))
1965 ;; We are using another frame for the guidance.
1966 (if (frame-live-p quail-guidance-frame)
1967 (delete-frame quail-guidance-frame))
1968 (if (buffer-live-p quail-guidance-buf)
1969 (kill-buffer quail-guidance-buf))))
4ed46869
KH
1970
1971(defun quail-update-guidance ()
1972 "Update the Quail guidance buffer and completion buffer (if displayed now)."
17388a62
KH
1973 ;; Update the guidance string.
1974 (when (quail-require-guidance-buf)
1975 (let ((guidance (quail-guidance)))
1976 (cond ((or (eq guidance t)
1977 (consp guidance))
1978 ;; Show the current possible translations.
1979 (setq quail-guidance-str
1980 (quail-get-translations)))
1981 ((null guidance)
1982 ;; Show the current input keys.
1983 (let ((key quail-current-key))
1984 (if (quail-kbd-translate)
1985 (setq key (quail-keyseq-translate key)))
1986 (setq quail-guidance-str (if (stringp key) key (string key)))))
1987 (t
1988 (setq quail-guidance-str " ")))))
4ed46869
KH
1989
1990 ;; Update completion buffer if displayed now. We highlight the
1991 ;; selected candidate string in *Completion* buffer if any.
1992 (let ((win (get-buffer-window quail-completion-buf))
1993 key str pos)
1994 (if win
1995 (save-excursion
1996 (setq str (if (stringp quail-current-str)
1997 quail-current-str
1998 (if (numberp quail-current-str)
1999 (char-to-string quail-current-str)))
2000 key quail-current-key)
2001 (set-buffer quail-completion-buf)
2002 (goto-char (point-min))
2003 (if (null (search-forward (concat " " key ":") nil t))
2004 (delete-overlay quail-overlay)
2005 (setq pos (point))
2006 (if (and str (search-forward (concat "." str) nil t))
17388a62
KH
2007 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
2008 (move-overlay quail-overlay (match-beginning 0) (point)))
4ed46869
KH
2009 ;; Now POS points end of KEY and (point) points end of STR.
2010 (if (pos-visible-in-window-p (point) win)
2011 ;; STR is already visible.
2012 nil
2013 ;; We want to make both KEY and STR visible, but if the
2014 ;; window is too short, make at least STR visible.
2015 (setq pos (progn (point) (goto-char pos)))
2016 (beginning-of-line)
2017 (set-window-start win (point))
2018 (if (not (pos-visible-in-window-p pos win))
2019 (set-window-start win pos))
2020 ))))))
2021
17388a62
KH
2022(defun quail-get-translations ()
2023 "Return a string containing the current possible translations."
2024 (let ((map (quail-lookup-key quail-current-key))
2025 (str (copy-sequence quail-current-key)))
7d842556
KH
2026 (if quail-current-translations
2027 (quail-update-current-translations))
4ed46869 2028
17388a62
KH
2029 ;; Show the current key.
2030 (let ((guidance (quail-guidance)))
2031 (if (listp guidance)
2032 ;; We must replace thetyped key with the specified PROMPTKEY.
2033 (dotimes (i (length str))
2034 (let ((prompt-key (cdr (assoc (aref str i) guidance))))
2035 (if prompt-key
2036 (aset str i (aref prompt-key 0)))))))
4ed46869 2037
17388a62
KH
2038 ;; Show followable keys.
2039 (if (and (> (length quail-current-key) 0) (cdr map))
2040 (setq str
2041 (format "%s[%s]"
2042 str
2043 (concat (sort (mapcar (function (lambda (x) (car x)))
2044 (cdr map))
2045 '<)))))
4ed46869 2046 ;; Show list of translations.
17388a62 2047 (if (and quail-current-translations
817e162f 2048 (not (quail-deterministic)))
17388a62 2049 (let* ((indices (car quail-current-translations))
7d842556
KH
2050 (cur (car indices))
2051 (start (nth 1 indices))
2052 (end (nth 2 indices))
2053 (idx start))
17388a62
KH
2054 (if (< (string-width str)
2055 (- quail-guidance-translations-starting-column 7))
2056 (setq str
2057 (concat str
2058 (make-string
2059 (- quail-guidance-translations-starting-column
2060 7 (string-width str))
2061 32))))
2062 (setq str (format "%s(%02d/%s)"
2063 str (nth 3 indices)
2064 (if (nth 4 indices)
2065 (format "%02d" (nth 4 indices))
2066 "??")))
7d842556 2067 (while (< idx end)
17388a62
KH
2068 (let ((len (length str))
2069 (trans (aref (cdr quail-current-translations) idx)))
2070 (or (stringp trans)
2071 (setq trans (string trans)))
2072 (setq str (format "%s %d.%s"
2073 str
2074 (if (= (- idx start) 9) 0
2075 (1+ (- idx start)))
2076 trans))
7d842556 2077 (if (= idx cur)
17388a62
KH
2078 (put-text-property (+ len 3) (length str)
2079 'face 'highlight str))
2080 (setq idx (1+ idx))))))
2081
2082 str))
4ed46869 2083
817e162f
KH
2084(defvar quail-completion-max-depth 5
2085 "The maximum depth of Quail completion list.")
2086
4ed46869
KH
2087(defun quail-completion ()
2088 "List all completions for the current key.
2089All possible translations of the current key and whole possible longer keys
817e162f 2090are shown (at most to the depth specified `quail-completion-max-depth')."
4ed46869 2091 (interactive)
05204016 2092 (quail-setup-completion-buf)
0548a7fd
KH
2093 (let ((win (get-buffer-window quail-completion-buf 'visible))
2094 (key quail-current-key)
2095 (map (quail-lookup-key quail-current-key))
2096 (require-update nil))
24790d0c 2097 (with-current-buffer quail-completion-buf
0548a7fd
KH
2098 (if (and win
2099 (equal key quail-current-key)
2100 (eq last-command 'quail-completion))
2101 ;; The window for Quail completion buffer has already been
2102 ;; shown. We just scroll it appropriately.
2103 (if (pos-visible-in-window-p (point-max) win)
2104 (set-window-start win (point-min))
d5ec6a2d
KH
2105 (let ((other-window-scroll-buffer quail-completion-buf)
2106 ;; This nil binding is necessary to surely scroll
2107 ;; quail-completion-buf.
2108 (minibuffer-scroll-window nil))
0548a7fd
KH
2109 (scroll-other-window)))
2110 (setq quail-current-key key)
2111 (erase-buffer)
2112 (insert "Possible completion and corresponding translations are:\n")
2113 (quail-completion-1 key map 1)
2114 (goto-char (point-min))
2115 (display-buffer (current-buffer))
2116 (setq require-update t)))
2117 (if require-update
2118 (quail-update-guidance)))
2119 (setq this-command 'quail-completion))
4ed46869 2120
4ed46869 2121(defun quail-completion-1 (key map indent)
74ace46a 2122"List all completions of KEY in MAP with indentation INDENT."
4ed46869
KH
2123 (let ((len (length key)))
2124 (indent-to indent)
2125 (insert key ":")
2126 (if (and (symbolp map) (fboundp map))
2127 (setq map (funcall map key len)))
2128 (if (car map)
2129 (quail-completion-list-translations map key (+ indent len 1))
2130 (insert " -\n"))
2131 (setq indent (+ indent 2))
817e162f 2132 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
4ed46869
KH
2133 (let ((l (cdr map))
2134 (newkey (make-string (1+ len) 0))
2135 (i 0))
817e162f
KH
2136 (if (functionp l)
2137 (setq l (funcall l)))
4ed46869
KH
2138 ;; Set KEY in the first LEN characters of NEWKEY.
2139 (while (< i len)
2140 (aset newkey i (aref key i))
2141 (setq i (1+ i)))
d5dc1e49 2142 (setq l (reverse l))
4ed46869
KH
2143 (while l ; L = ((CHAR . DEFN) ....) ;
2144 (aset newkey len (car (car l)))
2145 (quail-completion-1 newkey (cdr (car l)) indent)
2146 (setq l (cdr l)))))))
2147
4ed46869 2148(defun quail-completion-list-translations (map key indent)
74ace46a 2149 "List all possible translations of KEY in Quail MAP with indentation INDENT."
5611ce7c 2150 (let (beg (translations
ff913e92 2151 (quail-get-translation (car map) key (length key))))
4ed46869 2152 (if (integerp translations)
5611ce7c
KH
2153 (progn
2154 (insert "(1/1) 1.")
2155 ;; Endow the character `translations' with `mouse-face' text
2156 ;; property to enable `mouse-2' completion.
2157 (setq beg (point))
2158 (insert translations)
2159 (put-text-property beg (point) 'mouse-face 'highlight)
2160 (insert "\n"))
4ed46869
KH
2161 ;; We need only vector part.
2162 (setq translations (cdr translations))
2163 ;; Insert every 10 elements with indices in a line.
2164 (let ((len (length translations))
2165 (i 0)
4ed46869
KH
2166 num)
2167 (while (< i len)
8c14aa22
RS
2168 (when (zerop (% i 10))
2169 (when (>= i 10)
5611ce7c 2170 (insert "\n")
8c14aa22
RS
2171 (indent-to indent))
2172 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
4ed46869
KH
2173 ;; We show the last digit of FROM while converting
2174 ;; 0,1,..,9 to 1,2,..,0.
8c14aa22 2175 (insert (format " %d." (% (1+ i) 10)))
5611ce7c 2176 (setq beg (point))
4ed46869 2177 (insert (aref translations i))
5611ce7c
KH
2178 ;; Passing the mouse over a character will highlight.
2179 (put-text-property beg (point) 'mouse-face 'highlight)
4ed46869 2180 (setq i (1+ i)))
5611ce7c
KH
2181 (insert "\n")))))
2182
5611ce7c
KH
2183(defun quail-mouse-choose-completion (event)
2184 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
2185 (interactive "e")
2186 ;; This function is an exact copy of the mouse.el function
74ace46a 2187 ;; `mouse-choose-completion' except that we:
5611ce7c
KH
2188 ;; 1) add two lines from `choose-completion' in simple.el to give
2189 ;; the `mouse-2' click a little more leeway.
35fffde1 2190 ;; 2) don't bury *Quail Completions* buffer, so comment a section, and
5611ce7c
KH
2191 ;; 3) delete/terminate the current quail selection here.
2192 ;; Give temporary modes such as isearch a chance to turn off.
2193 (run-hooks 'mouse-leave-buffer-hook)
2194 (let ((buffer (window-buffer))
2195 choice
2196 base-size)
24790d0c 2197 (with-current-buffer (window-buffer (posn-window (event-start event)))
5611ce7c
KH
2198 (if completion-reference-buffer
2199 (setq buffer completion-reference-buffer))
2200 (setq base-size completion-base-size)
2201 (save-excursion
2202 (goto-char (posn-point (event-start event)))
2203 (let (beg end)
2204 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2205 (setq end (point) beg (1+ (point))))
2206 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
2207 (setq end (1- (point)) beg (point)))
2208 (if (null beg)
b58fc490 2209 (quail-error "No completion here"))
5611ce7c
KH
2210 (setq beg (previous-single-property-change beg 'mouse-face))
2211 (setq end (or (next-single-property-change end 'mouse-face)
2212 (point-max)))
2213 (setq choice (buffer-substring beg end)))))
2214; (let ((owindow (selected-window)))
2215; (select-window (posn-window (event-start event)))
2216; (if (and (one-window-p t 'selected-frame)
2217; (window-dedicated-p (selected-window)))
2218; ;; This is a special buffer's frame
2219; (iconify-frame (selected-frame))
2220; (or (window-dedicated-p (selected-window))
2221; (bury-buffer)))
2222; (select-window owindow))
2223 (quail-delete-region)
2224 (quail-choose-completion-string choice buffer base-size)
2225 (quail-terminate-translation)))
2226
35fffde1
DL
2227;; BASE-SIZE here is for compatibility with an (unused) arg of a
2228;; previous implementation.
5611ce7c 2229(defun quail-choose-completion-string (choice &optional buffer base-size)
35fffde1
DL
2230 (setq quail-current-str choice)
2231 (choose-completion-string choice buffer))
4ed46869 2232
362a8065
KH
2233(defun quail-build-decode-map (map-list key decode-map num
2234 &optional maxnum ignores)
74ace46a
DL
2235 "Build a decoding map.
2236Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
2237vs the corresponding translations defined in the Quail map
2238specified by the first element MAP-LIST. Each pair has the form
2239\(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
2240\(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
2241is a key sequence to reach MAP.
2242Optional 5th arg MAXNUM limits the number of accumulated pairs.
2243Optional 6th arg IGNORES is a list of translations to ignore."
362a8065
KH
2244 (let* ((map (car map-list))
2245 (translation (quail-get-translation (car map) key (length key)))
2246 elt)
95109387 2247 (cond ((integerp translation)
362a8065 2248 ;; Accept only non-ASCII chars not listed in IGNORES.
95109387
KH
2249 (when (and (> translation 255) (not (memq translation ignores)))
2250 (setcdr decode-map
2251 (cons (cons key translation) (cdr decode-map)))
2252 (setq num (1+ num))))
2253 ((consp translation)
2254 (setq translation (cdr translation))
2255 (let ((multibyte nil))
2256 (mapc (function (lambda (x)
362a8065
KH
2257 ;; Accept only non-ASCII chars not
2258 ;; listed in IGNORES.
95109387
KH
2259 (if (and (if (integerp x) (> x 255)
2260 (> (string-bytes x) (length x)))
2261 (not (member x ignores)))
2262 (setq multibyte t))))
2263 translation)
2264 (when multibyte
2265 (setcdr decode-map
2266 (cons (cons key translation) (cdr decode-map)))
2267 (setq num (+ num (length translation)))))))
2268 (if (and maxnum (> num maxnum))
2269 (- num)
2270 (setq map (cdr map))
362a8065 2271 ;; Recursively check the deeper map.
95109387
KH
2272 (while (and map (>= num 0))
2273 (setq elt (car map) map (cdr map))
362a8065
KH
2274 (when (and (integerp (car elt)) (consp (cdr elt))
2275 (not (memq (cdr elt) map-list)))
2276 (setq num (quail-build-decode-map (cons (cdr elt) map-list)
95109387
KH
2277 (format "%s%c" key (car elt))
2278 decode-map num maxnum ignores))))
2279 num)))
2280
2281(defun quail-insert-decode-map (decode-map)
74ace46a
DL
2282 "Insert pairs of key sequences vs the corresponding translations.
2283These are stored in DECODE-MAP using the concise format. DECODE-MAP
2284should be made by `quail-build-decode-map' (which see)."
95109387
KH
2285 (setq decode-map
2286 (sort (cdr decode-map)
2287 (function (lambda (x y)
2288 (setq x (car x) y (car y))
2289 (or (> (length x) (length y))
2290 (and (= (length x) (length y))
2291 (not (string< x y))))))))
58d2b986
KH
2292 (let ((frame-width (frame-width (window-frame (get-buffer-window
2293 (current-buffer) 'visible))))
362a8065
KH
2294 (single-key-width 3)
2295 (single-trans-width 4)
2296 (multiple-key-width 3)
2297 (single-list nil)
2298 (multiple-list nil)
95109387 2299 elt trans width pos cols rows col row str col-width)
362a8065
KH
2300 ;; Divide the elements of decoding map into single ones (i.e. the
2301 ;; one that has single translation) and multibyte ones (i.e. the
2302 ;; one that has multiple translations).
95109387
KH
2303 (while decode-map
2304 (setq elt (car decode-map) decode-map (cdr decode-map)
2305 trans (cdr elt))
2306 (if (and (vectorp trans) (= (length trans) 1))
2307 (setq trans (aref trans 0)))
2308 (if (vectorp trans)
362a8065
KH
2309 (setq multiple-list (cons elt multiple-list))
2310 (setq single-list (cons (cons (car elt) trans) single-list)
95109387
KH
2311 width (if (stringp trans) (string-width trans)
2312 (char-width trans)))
362a8065
KH
2313 (if (> width single-trans-width)
2314 (setq single-trans-width width)))
95109387 2315 (setq width (length (car elt)))
362a8065
KH
2316 (if (> width single-key-width)
2317 (setq single-key-width width))
2318 (if (> width multiple-key-width)
2319 (setq multiple-key-width width)))
2320 (when single-list
2321 (setq col-width (+ single-key-width 1 single-trans-width 1)
95109387 2322 cols (/ frame-width col-width)
362a8065
KH
2323 rows (/ (length single-list) cols))
2324 (if (> (% (length single-list) cols) 0)
95109387
KH
2325 (setq rows (1+ rows)))
2326 (insert "key")
362a8065 2327 (indent-to (1+ single-key-width))
95109387
KH
2328 (insert "char")
2329 (indent-to (1+ col-width))
2330 (insert "[type a key sequence to insert the corresponding character]\n")
2331 (setq pos (point))
2332 (insert-char ?\n (+ rows 2))
2333 (goto-char pos)
2334 (setq col (- col-width) row 0)
362a8065
KH
2335 (while single-list
2336 (setq elt (car single-list) single-list (cdr single-list))
95109387
KH
2337 (when (= (% row rows) 0)
2338 (goto-char pos)
2339 (setq col (+ col col-width))
2340 (move-to-column col t)
362a8065 2341 (insert-char ?- single-key-width)
95109387 2342 (insert ? )
362a8065 2343 (insert-char ?- single-trans-width)
95109387
KH
2344 (forward-line 1))
2345 (move-to-column col t)
2346 (insert (car elt))
362a8065 2347 (indent-to (+ col single-key-width 1))
95109387
KH
2348 (insert (cdr elt))
2349 (forward-line 1)
2350 (setq row (1+ row)))
2351 (goto-char (point-max)))
2352
362a8065 2353 (when multiple-list
95109387 2354 (insert "key")
362a8065 2355 (indent-to (1+ multiple-key-width))
95109387 2356 (insert "character(s) [type a key (sequence) and select one from the list]\n")
362a8065 2357 (insert-char ?- multiple-key-width)
95109387 2358 (insert " ------------\n")
362a8065
KH
2359 (while multiple-list
2360 (setq elt (car multiple-list) multiple-list (cdr multiple-list))
95109387 2361 (insert (car elt))
362a8065 2362 (indent-to multiple-key-width)
95109387
KH
2363 (if (vectorp (cdr elt))
2364 (mapc (function
2365 (lambda (x)
2366 (let ((width (if (integerp x) (char-width x)
2367 (string-width x))))
2368 (when (> (+ (current-column) 1 width) frame-width)
2369 (insert "\n")
362a8065 2370 (indent-to multiple-key-width))
95109387
KH
2371 (insert " " x))))
2372 (cdr elt))
2373 (insert " " (cdr elt)))
2374 (insert ?\n))
2375 (insert ?\n))))
2376
396ae608
RS
2377(define-button-type 'quail-keyboard-layout-button
2378 :supertype 'help-xref
a1506d29 2379 'help-function '(lambda (layout)
396ae608
RS
2380 (help-setup-xref `(quail-keyboard-layout-button ,layout) nil)
2381 (quail-show-keyboard-layout layout))
2382 'help-echo (purecopy "mouse-2, RET: show keyboard layout"))
2383
2384(define-button-type 'quail-keyboard-customize-button
2385 :supertype 'help-customize-variable
2386 'help-echo (purecopy "mouse-2, RET: customize keyboard layout"))
2387
6c7b13cf
KH
2388(defun quail-help (&optional package)
2389 "Show brief description of the current Quail package.
24790d0c 2390Optional arg PACKAGE specifies the name of alternative Quail
362a8065 2391package to describe."
4ed46869 2392 (interactive)
83600a29 2393 (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
396ae608
RS
2394 (default-enable-multibyte-characters enable-multibyte-characters)
2395 (package-def
2396 (if package
2397 (assoc package quail-package-alist)
2398 quail-current-package)))
58d2b986 2399 ;; At first, make sure that the help buffer has window.
1ce2659d
RS
2400 (let ((temp-buffer-show-hook nil))
2401 (with-output-to-temp-buffer (help-buffer)
2402 (with-current-buffer standard-output
2403 (setq quail-current-package package-def))))
58d2b986
KH
2404 ;; Then, insert text in the help buffer while paying attention to
2405 ;; the width of the frame in which the buffer displayed.
24790d0c 2406 (with-current-buffer (help-buffer)
09877d5d
MB
2407 (setq buffer-read-only nil)
2408 (insert "Input method: " (quail-name)
2409 " (mode line indicator:"
2410 (quail-title)
2411 ")\n\n")
2412 (save-restriction
2413 (narrow-to-region (point) (point))
2414 (insert (quail-docstring))
2415 (goto-char (point-min))
2416 (with-syntax-table emacs-lisp-mode-syntax-table
2417 (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
2418 (let ((sym (intern-soft
2419 (buffer-substring (+ (match-beginning 0) 2)
2420 (1- (point))))))
2421 (if (and (boundp sym)
2422 (stringp (symbol-value sym)))
2423 (replace-match (symbol-value sym) t t)))))
2424 (goto-char (point-max)))
2425 (or (bolp)
2426 (insert "\n"))
2427 (insert "\n")
2428
2429 (let ((done-list nil))
2430 ;; Show keyboard layout if the current package requests it..
2431 (when (quail-show-layout)
2432 (insert "
362a8065
KH
2433KEYBOARD LAYOUT
2434---------------
8179cccd
KH
2435This input method works by translating individual input characters.
2436Assuming that your actual keyboard has the `")
09877d5d
MB
2437 (help-insert-xref-button
2438 quail-keyboard-layout-type
396ae608
RS
2439 'quail-keyboard-layout-button
2440 quail-keyboard-layout-type)
09877d5d 2441 (insert "' layout,
8179cccd 2442translation results in the following \"virtual\" keyboard layout:
195e6740 2443")
09877d5d
MB
2444 (setq done-list
2445 (quail-insert-kbd-layout quail-keyboard-layout))
2446 (insert "If your keyboard has a different layout, rearranged from
8179cccd 2447`")
09877d5d
MB
2448 (help-insert-xref-button
2449 "standard"
396ae608 2450 'quail-keyboard-layout-button "standard")
09877d5d 2451 (insert "', the \"virtual\" keyboard you get with this input method
8179cccd
KH
2452will be rearranged in the same way.
2453
2454You can set the variable `quail-keyboard-layout-type' to specify
2455the physical layout of your keyboard; the tables shown in
2456documentation of input methods including this one are based on the
2457physical keyboard layout as specified with that variable.
2458")
09877d5d
MB
2459 (help-insert-xref-button
2460 "[customize keyboard layout]"
396ae608 2461 'quail-keyboard-customize-button 'quail-keyboard-layout-type)
09877d5d
MB
2462 (insert "\n"))
2463
2464 ;; Show key sequences.
2465 (let ((decode-map (list 'decode-map))
2466 elt pos num)
2467 (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
2468 0 512 done-list))
2469 (when (> num 0)
2470 (insert "
362a8065
KH
2471KEY SEQUENCE
2472-----------
2473")
09877d5d
MB
2474 (if (quail-show-layout)
2475 (insert "You can also input more characters")
2476 (insert "You can input characters"))
2477 (insert " by the following key sequences:\n")
2478 (quail-insert-decode-map decode-map))))
2479
2480 (quail-help-insert-keymap-description
2481 (quail-translation-keymap)
2482 "\
362a8065
KH
2483KEY BINDINGS FOR TRANSLATION
2484----------------------------\n")
09877d5d
MB
2485 (insert ?\n)
2486 (if (quail-conversion-keymap)
2487 (quail-help-insert-keymap-description
2488 (quail-conversion-keymap)
2489 "\
362a8065
KH
2490KEY BINDINGS FOR CONVERSION
2491---------------------------\n"))
74ace46a 2492 (setq quail-current-package nil)
09877d5d
MB
2493 ;; Resize the help window again, now that it has all its contents.
2494 (save-selected-window
2c72829f 2495 (select-window (get-buffer-window (current-buffer) t))
09877d5d 2496 (run-hooks 'temp-buffer-show-hook)))))
9a6428f8 2497
4ed46869 2498(defun quail-help-insert-keymap-description (keymap &optional header)
95109387
KH
2499 (let (pos1 pos2 eol)
2500 (setq pos1 (point))
4ed46869
KH
2501 (if header
2502 (insert header))
362a8065
KH
2503 (save-excursion
2504 (insert (substitute-command-keys "\\{keymap}")))
2505 ;; Skip headers "key bindings", etc.
95109387
KH
2506 (forward-line 3)
2507 (setq pos2 (point))
2508 (with-syntax-table emacs-lisp-mode-syntax-table
2509 (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
2510 (let ((sym (intern-soft (buffer-substring (match-beginning 0)
2511 (point)))))
2512 (if (and sym (fboundp sym)
362a8065
KH
2513 (or (eq (get sym 'quail-help) 'hide)
2514 (and (quail-deterministic)
2515 (eq (get sym 'quail-help) 'non-deterministic))))
95109387
KH
2516 (delete-region (line-beginning-position)
2517 (1+ (line-end-position)))))))
2518 (goto-char pos2)
2519 (while (not (eobp))
2520 (if (looking-at "[ \t]*$")
2521 (delete-region (point) (1+ (line-end-position)))
2522 (forward-line 1)))
2523 (goto-char pos2)
2524 (if (eobp)
2525 (delete-region pos1 (point)))
2526 (goto-char (point-max))))
4ed46869
KH
2527
2528(defun quail-translation-help ()
d91eafdf 2529 "Show help message while translating in Quail input method."
4ed46869 2530 (interactive)
d91eafdf
KH
2531 (if (not (eq this-command last-command))
2532 (let (state-msg keymap)
2533 (if (and quail-converting (= (length quail-current-key) 0))
2534 (setq state-msg
2535 (format "Converting string %S by input method %S.\n"
2536 quail-conversion-str (quail-name))
2537 keymap (quail-conversion-keymap))
2538 (setq state-msg
2539 (format "Translating key sequence %S by input method %S.\n"
2540 quail-current-key (quail-name))
2541 keymap (quail-translation-keymap)))
6c7b13cf 2542 (with-output-to-temp-buffer "*Help*"
24790d0c 2543 (with-current-buffer standard-output
d91eafdf
KH
2544 (insert state-msg)
2545 (quail-help-insert-keymap-description
2546 keymap
6c7b13cf 2547 "-----------------------\n")
24790d0c 2548 ;; Isn't this redundant ? -stef
d91eafdf
KH
2549 (help-mode)))))
2550 (let (scroll-help)
2551 (save-selected-window
6c7b13cf 2552 (select-window (get-buffer-window "*Help*"))
d91eafdf
KH
2553 (if (eq this-command last-command)
2554 (if (< (window-end) (point-max))
2555 (scroll-up)
2556 (if (> (window-start) (point-min))
2557 (set-window-start (selected-window) (point-min)))))
2558 (setq scroll-help
2559 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2560 "Type \\[quail-translation-help] to scroll up the help"
2561 (if (> (window-start) (point-min))
2562 "Type \\[quail-translation-help] to see the head of help"))))
2563 (if scroll-help
2564 (progn
2565 (message "%s" (substitute-command-keys scroll-help))
2566 (sit-for 1)
2567 (message nil)
2568 (quail-update-guidance)
2569 ))))
817e162f
KH
2570\f
2571;; Quail map generator from state transition table.
2572
2573(defun quail-map-from-table (table)
2574 "Make quail map from state transition table TABLE.
2575
2576TABLE is an alist, the form is:
2577 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2578
2579STATE-n are symbols to denote state. STATE-0 is the initial state.
2580
2581TRANSITION-n-m are transition rules from STATE-n, and have the form
2582\(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2583RULES is a symbol whose value is an alist of keys \(string) vs the
2584correponding characters or strings. The format of the symbol value of
2585RULES is the same as arguments to `quail-define-rules'.
2586
2587If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2588STATE-n transits to STATE-x when keys in RULES are input. Recursive
2589transition is allowed, i.e. STATE-x may be STATE-n.
2590
2591If TRANSITION-n-m has the form RULES, the transition terminates
2592when keys in RULES are input.
2593
2594The generated map can be set for the current Quail package by the
2595function `quail-install-map' (which see)."
2596 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2597 tail elt)
2598 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2599 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2600 ;; Set key sequence mapping rules in cdr part of each element.
2601 (while table
2602 (quail-map-from-table-1 state-alist (car table))
2603 (setq table (cdr table)))
2604
2605 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2606 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2607 ;; them with MAPPING-RULES of STATE-x to make elements of
2608 ;; STATE-ALIST valid Quail maps.
2609 (setq tail state-alist)
2610 (while tail
2611 (setq elt (car tail) tail (cdr tail))
2612 (quail-map-from-table-2 state-alist elt))
2613
2614 ;; Return the Quail map for the initial state.
2615 (car state-alist)))
2616
2617;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2618;; mapping rules in the element of STATE-ALIST that corresponds to
2619;; STATE according to TRANSITION ...
2620(defun quail-map-from-table-1 (state-alist state-info)
2621 (let* ((state (car state-info))
2622 (map (assq state state-alist))
2623 (transitions (cdr state-info))
2624 elt)
2625 (while transitions
2626 (setq elt (car transitions) transitions (cdr transitions))
2627 (let (rules dst-state key trans)
2628 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2629 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2630 (if (consp elt)
2631 (setq rules (symbol-value (car elt))
2632 ;; Set (STATE-x) as branches for all keys in RULES.
2633 ;; It is replaced with actual branches for STATE-x
2634 ;; later in `quail-map-from-table-2'.
2635 dst-state (list (cdr elt)))
2636 (setq rules (symbol-value elt)))
2637 (while rules
2638 (setq key (car (car rules)) trans (cdr (car rules))
2639 rules (cdr rules))
2640 (if (stringp trans)
2641 (if (= (length trans) 1)
2642 (setq trans (aref trans 0))
2643 (setq trans (string-to-vector trans))))
2644 (set-nested-alist key trans map nil dst-state))))))
2645
2646;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2647;; the form is:
2648;; (STATE (CHAR NESTED-ALIST) ...)
2649;; NESTED-ALIST is a nested alist; the form is:
2650;; (TRANS (CHAR NESTED-ALIST) ...)
2651;; or
2652;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2653;; Here, the task is to replace all occurrences of (STATE-x) with:
2654;; (cdr (assq STATE-x STATE-ALIST))
2655
2656(defun quail-map-from-table-2 (state-alist element)
2657 (let ((prev element)
2658 (tail (cdr element))
2659 elt)
2660 (while (cdr tail)
2661 (setq elt (car tail) prev tail tail (cdr tail))
2662 (quail-map-from-table-2 state-alist (cdr elt)))
2663 (setq elt (car tail))
2664 (if (consp elt)
2665 (quail-map-from-table-2 state-alist (cdr elt))
2666 (setcdr prev (cdr (assq elt state-alist))))))
2667
2668;; Concatenate translations for all heading substrings of KEY in the
2669;; current Quail map. Here, `heading substring' means (substring KEY
2670;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2671(defun quail-lookup-map-and-concat (key)
2672 (let* ((len (length key))
2673 (translation-list nil)
2674 map)
2675 (while (> len 0)
2676 (setq map (quail-lookup-key key len)
2677 len (1- len))
2678 (if map
2679 (let* ((def (quail-map-definition map))
2680 (trans (if (consp def) (aref (cdr def) (car (car def)))
2681 def)))
2682 (if (integerp trans)
2683 (setq trans (char-to-string trans)))
2684 (setq translation-list (cons trans translation-list)))))
2685 (apply 'concat translation-list)))
4ed46869 2686
ff913e92
KH
2687\f
2688(defvar quail-directory-name "quail"
cd30a521 2689 "Name of Quail directory which contains Quail packages.
ff913e92
KH
2690This is a sub-directory of LEIM directory.")
2691
2692;;;###autoload
70fd2661
KH
2693(defun quail-update-leim-list-file (dirname &rest dirnames)
2694 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2695DIRNAME is a directory containing Emacs input methods;
8cbe9074 2696normally, it should specify the `leim' subdirectory
70fd2661
KH
2697of the Emacs source tree.
2698
2699It searches for Quail packages under `quail' subdirectory of DIRNAME,
2700and update the file \"leim-list.el\" in DIRNAME.
ff913e92 2701
70fd2661
KH
2702When called from a program, the remaining arguments are additional
2703directory names to search for Quail packages under `quail' subdirectory
2704of each directory."
2705 (interactive "FDirectory of LEIM: ")
2706 (setq dirname (expand-file-name dirname))
2707 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2708 quail-dirs list-buf pkg-list pkg-buf pos)
2709 (if (not (file-writable-p leim-list))
2710 (error "Can't write to file \"%s\"" leim-list))
2711 (message "Updating %s ..." leim-list)
2712 (setq list-buf (find-file-noselect leim-list))
2713
2714 ;; At first, clean up the file.
24790d0c 2715 (with-current-buffer list-buf
70fd2661
KH
2716 (goto-char 1)
2717
2718 ;; Insert the correct header.
2719 (if (looking-at (regexp-quote leim-list-header))
2720 (goto-char (match-end 0))
2721 (insert leim-list-header))
2722 (setq pos (point))
2723 (if (not (re-search-forward leim-list-entry-regexp nil t))
2724 nil
2725
2726 ;; Remove garbages after the header.
2727 (goto-char (match-beginning 0))
2728 (if (< pos (point))
2729 (delete-region pos (point)))
2730
2731 ;; Remove all entries for Quail.
2732 (while (re-search-forward leim-list-entry-regexp nil 'move)
2733 (goto-char (match-beginning 0))
2734 (setq pos (point))
2735 (condition-case nil
2736 (let ((form (read list-buf)))
2737 (when (equal (nth 3 form) ''quail-use-package)
2738 (if (eolp) (forward-line 1))
2739 (delete-region pos (point))))
2740 (error
2741 ;; Delete the remaining contents because it seems that
2742 ;; this file is broken.
4be9beaf 2743 (message "Garbage in %s deleted" leim-list)
70fd2661
KH
2744 (delete-region pos (point-max)))))))
2745
cd30a521 2746 ;; Search for `quail' subdirectory under each DIRNAMES.
70fd2661
KH
2747 (setq dirnames (cons dirname dirnames))
2748 (let ((l dirnames))
2749 (while l
2750 (setcar l (expand-file-name (car l)))
2751 (setq dirname (expand-file-name quail-directory-name (car l)))
2752 (if (file-readable-p dirname)
2753 (setq quail-dirs (cons dirname quail-dirs))
4be9beaf 2754 (message "%s doesn't have `%s' subdirectory, just ignored"
70fd2661
KH
2755 (car l) quail-directory-name)
2756 (setq quail-dirs (cons nil quail-dirs)))
2757 (setq l (cdr l)))
2758 (setq quail-dirs (nreverse quail-dirs)))
2759
2760 ;; Insert input method registering forms.
2761 (while quail-dirs
2762 (setq dirname (car quail-dirs))
2763 (when dirname
2764 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2765 (while pkg-list
2766 (message "Checking %s ..." (car pkg-list))
2767 (with-temp-buffer
2768 (insert-file-contents (car pkg-list))
2769 (goto-char (point-min))
35fffde1
DL
2770 ;; Don't get fooled by commented-out code.
2771 (while (re-search-forward "^[ \t]*(quail-define-package" nil t)
ff913e92 2772 (goto-char (match-beginning 0))
70fd2661
KH
2773 (condition-case nil
2774 (let ((form (read (current-buffer))))
24790d0c 2775 (with-current-buffer list-buf
70fd2661
KH
2776 (insert
2777 (format "(register-input-method
ff913e92
KH
2778 %S %S '%s
2779 %S %S
8cbe9074 2780 %S)\n"
70fd2661
KH
2781 (nth 1 form) ; PACKAGE-NAME
2782 (nth 2 form) ; LANGUAGE
2783 'quail-use-package ; ACTIVATE-FUNC
2784 (nth 3 form) ; PACKAGE-TITLE
2785 (progn ; PACKAGE-DESCRIPTION (one line)
2786 (string-match ".*" (nth 5 form))
2787 (match-string 0 (nth 5 form)))
2788 (file-relative-name ; PACKAGE-FILENAME
2789 (file-name-sans-extension (car pkg-list))
2790 (car dirnames))))))
2791 (error
2792 ;; Ignore the remaining contents of this file.
2793 (goto-char (point-max))
186b3352 2794 (message "Some part of \"%s\" is broken" (car pkg-list))))))
70fd2661
KH
2795 (setq pkg-list (cdr pkg-list)))
2796 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2797
2798 ;; At last, write out LEIM list file.
24790d0c 2799 (with-current-buffer list-buf
70fd2661
KH
2800 (setq buffer-file-coding-system 'iso-2022-7bit)
2801 (save-buffer 0))
2802 (kill-buffer list-buf)
2803 (message "Updating %s ... done" leim-list)))
bb63aae5
KH
2804\f
2805(defun quail-advice (args)
74ace46a 2806 "Advise users about the characters input by the current Quail package.
bb63aae5
KH
2807The argument is a parameterized event of the form:
2808 (quail-advice STRING)
2809where STRING is a string containing the input characters.
2810If STRING has property `advice' and the value is a function,
2811call it with one argument STRING."
2812 (interactive "e")
2813 (let* ((string (nth 1 args))
2814 (func (get-text-property 0 'advice string)))
2815 (if (functionp func)
2816 (funcall func string))))
2817
2818(global-set-key [quail-advice] 'quail-advice)
2819
4ed46869
KH
2820;;
2821(provide 'quail)
2822
ab5796a9 2823;;; arch-tag: 46d7db54-5467-42c4-a2a9-53ca90a1e886
4ed46869 2824;;; quail.el ends here