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