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