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