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