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