1 ;;; quail.el --- provides simple input method for multilingual text
3 ;; Copyright (C) 1995, 2000 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
7 ;; Author: Kenichi HANDA <handa@etl.go.jp>
8 ;; Naoto TAKAHASHI <ntakahas@etl.go.jp>
9 ;; Maintainer: Kenichi HANDA <handa@etl.go.jp>
10 ;; Keywords: mule, multilingual, input method, i18n
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; In Quail minor mode, you can input multilingual text easily. By
32 ;; defining a translation table (named Quail map) which maps ASCII key
33 ;; string to multilingual character or string, you can input any text
34 ;; from ASCII keyboard.
36 ;; We use words "translation" and "conversion" differently. The
37 ;; former is done by Quail package itself, the latter is the further
38 ;; process of converting a translated text to some more desirable
39 ;; text. For instance, Quail package for Japanese (`quail-jp')
40 ;; translates Roman text (transliteration of Japanese in Latin
41 ;; alphabets) to Hiragana text, which is then converted to
42 ;; Kanji-and-Kana mixed text or Katakana text by commands specified in
43 ;; CONVERSION-KEYS argument of the Quail package.
45 ;; [There was an input method for Mule 2.3 called `Tamago' from the
46 ;; Japanese `TAkusan MAtasete GOmenasai', or `Sorry for having you
47 ;; wait so long'; this couldn't be included in Emacs 20. `Tamago' is
48 ;; Japanese for `egg' (implicitly a hen's egg). Handa-san made a
49 ;; smaller and simpler system; the smaller quail egg is also eaten in
50 ;; Japan. Maybe others will be egged on to write more sorts of input
58 "Quail: multilingual input method."
61 ;; Buffer local variables
63 (defvar quail-current-package nil
64 "The current Quail package, which depends on the current input method.
65 See the documentation of `quail-package-alist' for the format.")
66 (make-variable-buffer-local 'quail-current-package
)
67 (put 'quail-current-package
'permanent-local t
)
69 ;; Quail uses the following variables to assist users.
70 ;; A string containing available key sequences or translation list.
71 (defvar quail-guidance-str nil
)
72 ;; A buffer to show completion list of the current key sequence.
73 (defvar quail-completion-buf nil
)
74 ;; We may display the guidance string in a buffer on a one-line frame.
75 (defvar quail-guidance-buf nil
)
76 (defvar quail-guidance-frame nil
)
78 ;; Each buffer in which Quail is activated should use different
80 (make-variable-buffer-local 'quail-guidance-str
)
81 (put 'quail-guidance-str
'permanent-local t
)
83 (defvar quail-overlay nil
84 "Overlay which covers the current translation region of Quail.")
85 (make-variable-buffer-local 'quail-overlay
)
87 (defvar quail-conv-overlay nil
88 "Overlay which covers the text to be converted in Quail mode.")
89 (make-variable-buffer-local 'quail-conv-overlay
)
91 (defvar quail-current-key nil
92 "Current key for translation in Quail mode.")
93 (make-variable-buffer-local 'quail-current-key
)
95 (defvar quail-current-str nil
96 "Currently selected translation of the current key.")
97 (make-variable-buffer-local 'quail-current-str
)
99 (defvar quail-current-translations nil
100 "Cons of indices and vector of possible translations of the current key.
101 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
102 CURRENT is an index of the current translation,
103 START and END are indices of the start and end of the current block,
104 BLOCK is the current block index,
105 BLOCKS is a number of blocks of translation.")
106 (make-variable-buffer-local 'quail-current-translations
)
108 (defvar quail-current-data nil
109 "Any Lisp object holding information of current translation status.
110 When a key sequence is mapped to TRANS and TRANS is a cons
111 of actual translation and some Lisp object to be referred
112 for translating the longer key sequence, this variable is set
113 to that Lisp object.")
114 (make-variable-buffer-local 'quail-current-data
)
116 ;; Quail package handlers.
118 (defvar quail-package-alist nil
119 "List of Quail packages.
120 A Quail package is a list of these elements:
121 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
122 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
123 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
124 CONVERSION-KEYS, SIMPLE.
126 QUAIL-MAP is a data structure to map key strings to translations. For
127 the format, see the documentation of `quail-map-p'.
129 DECODE-MAP is an alist of translations and corresponding keys.
131 See the documentation of `quail-define-package' for the other elements.")
133 ;; Return various slots in the current quail-package.
135 (defsubst quail-name
()
136 "Return the name of the current Quail package."
137 (nth 0 quail-current-package
))
139 (defun quail-title ()
140 "Return the title of the current Quail package."
141 (let ((title (nth 1 quail-current-package
)))
142 ;; TITLE may be a string or a list. If it is a list, each element
143 ;; is a string or the form (VAR STR1 STR2), and the interpretation
144 ;; of the list is the same as that of mode-line-format.
150 (cond ((stringp x
) x
)
151 ((and (listp x
) (symbolp (car x
)) (= (length x
) 3))
152 (if (symbol-value (car x
))
153 (nth 1 x
) (nth 2 x
)))
157 (defsubst quail-map
()
158 "Return the translation map of the current Quail package."
159 (nth 2 quail-current-package
))
160 (defsubst quail-guidance
()
161 "Return an object used for `guidance' feature of the current Quail package.
162 See also the documentation of `quail-define-package'."
163 (nth 3 quail-current-package
))
164 (defsubst quail-docstring
()
165 "Return the documentation string of the current Quail package."
166 (nth 4 quail-current-package
))
167 (defsubst quail-translation-keymap
()
168 "Return translation keymap in the current Quail package.
169 Translation keymap is a keymap used while translation region is active."
170 (nth 5 quail-current-package
))
171 (defsubst quail-forget-last-selection
()
172 "Return `forget-last-selection' flag of the current Quail package.
173 See also the documentation of `quail-define-package'."
174 (nth 6 quail-current-package
))
175 (defsubst quail-deterministic
()
176 "Return `deterministic' flag of the current Quail package.
177 See also the documentation of `quail-define-package'."
178 (nth 7 quail-current-package
))
179 (defsubst quail-kbd-translate
()
180 "Return `kbd-translate' flag of the current Quail package.
181 See also the documentation of `quail-define-package'."
182 (nth 8 quail-current-package
))
183 (defsubst quail-show-layout
()
184 "Return `show-layout' flag of the current Quail package.
185 See also the documentation of `quail-define-package'."
186 (nth 9 quail-current-package
))
187 (defsubst quail-decode-map
()
188 "Return decode map of the current Quail package.
189 It is an alist of translations and corresponding keys."
190 (nth 10 quail-current-package
))
191 (defsubst quail-maximum-shortest
()
192 "Return `maximum-shortest' flag of the current Quail package.
193 See also the documentation of `quail-define-package'."
194 (nth 11 quail-current-package
))
195 (defsubst quail-overlay-plist
()
196 "Return property list of an overly used in the current Quail package."
197 (nth 12 quail-current-package
))
198 (defsubst quail-update-translation-function
()
199 "Return a function for updating translation in the current Quail package."
200 (nth 13 quail-current-package
))
201 (defsubst quail-conversion-keymap
()
202 "Return conversion keymap in the current Quail package.
203 Conversion keymap is a keymap used while conversion region is active
204 but translation region is not active."
205 (nth 14 quail-current-package
))
206 (defsubst quail-simple
()
207 "Return t if the current Quail package is simple."
208 (nth 15 quail-current-package
))
210 (defsubst quail-package
(name)
211 "Return Quail package named NAME."
212 (assoc name quail-package-alist
))
214 (defun quail-add-package (package)
215 "Add Quail package PACKAGE to `quail-package-alist'."
216 (let ((pac (quail-package (car package
))))
218 (setcdr pac
(cdr package
))
219 (setq quail-package-alist
(cons package quail-package-alist
)))))
221 (defun quail-select-package (name)
222 "Select Quail package named NAME as the current Quail package."
223 (let ((package (quail-package name
)))
225 (error "No Quail package `%s'" name
))
226 (setq quail-current-package package
)
227 (setq-default quail-current-package package
)
231 (defun quail-use-package (package-name &rest libraries
)
232 "Start using Quail package PACKAGE-NAME.
233 The remaining arguments are libraries to be loaded before using the package.
235 This activates input method defined by PACKAGE-NAME by running
236 `quail-activate', which see."
237 (let ((package (quail-package package-name
)))
239 ;; Perhaps we have not yet loaded necessary libraries.
241 (if (not (load (car libraries
) t
))
243 (with-output-to-temp-buffer "*Help*"
244 (princ "Quail package \"")
246 (princ "\" can't be activated\n because library \"")
247 (princ (car libraries
))
248 (princ "\" is not in `load-path'.
250 The most common case is that you have not yet installed appropriate
251 libraries in LEIM (Libraries of Emacs Input Method) which is
252 distributed separately from Emacs.
254 LEIM is available from the same ftp directory as Emacs."))
255 (error "Can't use the Quail package `%s'" package-name
))
256 (setq libraries
(cdr libraries
))))))
257 (quail-select-package package-name
)
258 (setq current-input-method-title
(quail-title))
260 ;; Hide all '... loaded' message.
263 (defvar quail-translation-keymap
264 (let ((map (make-keymap))
267 (define-key map
(char-to-string i
) 'quail-other-command
)
270 (define-key map
(char-to-string i
) 'quail-self-insert-command
)
274 (define-key map
(vector i
) 'quail-self-insert-command
)
276 (define-key map
"\177" 'quail-delete-last-char
)
277 (define-key map
"\C-f" 'quail-next-translation
)
278 (define-key map
"\C-b" 'quail-prev-translation
)
279 (define-key map
"\C-n" 'quail-next-translation-block
)
280 (define-key map
"\C-p" 'quail-prev-translation-block
)
281 (define-key map
[right] 'quail-next-translation)
282 (define-key map [left] 'quail-prev-translation)
283 (define-key map [down] 'quail-next-translation-block)
284 (define-key map [up] 'quail-prev-translation-block)
285 (define-key map "\C-i" 'quail-completion)
286 (define-key map "\C-@" 'quail-select-current)
287 ;; Following simple.el, Enter key on numeric keypad selects the
288 ;; current translation just like `C-SPC', and `mouse-2' chooses
289 ;; any completion visible in the *Quail Completions* buffer.
290 (define-key map [kp-enter] 'quail-select-current)
291 (define-key map [mouse-2] 'quail-mouse-choose-completion)
292 (define-key map [down-mouse-2] nil)
293 (define-key map "\C-h" 'quail-translation-help)
294 (define-key map [?\C- ] 'quail-select-current)
295 (define-key map [tab] 'quail-completion)
296 (define-key map [delete] 'quail-delete-last-char)
297 (define-key map [backspace] 'quail-delete-last-char)
299 "Keymap used processing translation in complex Quail modes.
300 Only a few especially complex input methods use this map;
301 most use `quail-simple-translation-keymap' instead.
302 This map is activated while translation region is active.")
304 (defvar quail-translation-docstring
305 "When you type keys, the echo area shows the possible characters
306 which correspond to that key sequence, each preceded by a digit. You
307 can select one of the characters shown by typing the corresponding
308 digit. Alternatively, you can use C-f and C-b to move through the
309 line to select the character you want, then type a letter to begin
310 entering another Chinese character or type a space or punctuation
313 If there are more than ten possible characters for the given spelling,
314 the echo area shows ten characters at a time; you can use C-n to move
315 to the next group of ten, and C-p to move back to the previous group
318 ;; Categorize each Quail commands to make the output of quail-help
319 ;; concise. This is done by putting `quail-help' property. The value
321 ;; hide -- never show this command
322 ;; non-deterministic -- show only for non-deterministic input method
323 (let ((l '((quail-other-command . hide)
324 (quail-self-insert-command . hide)
325 (quail-delete-last-char . hide)
326 (quail-next-translation . non-deterministic)
327 (quail-prev-translation . non-deterministic)
328 (quail-next-translation-block . non-deterministic)
329 (quail-prev-translation-block . non-deterministic))))
331 (put (car (car l)) 'quail-help (cdr (car l)))
334 (defvar quail-simple-translation-keymap
335 (let ((map (make-keymap))
338 (define-key map (char-to-string i) 'quail-other-command)
341 (define-key map (char-to-string i) 'quail-self-insert-command)
345 (define-key map (vector i) 'quail-self-insert-command)
347 (define-key map "\177" 'quail-delete-last-char)
348 (define-key map [delete] 'quail-delete-last-char)
349 (define-key map [backspace] 'quail-delete-last-char)
350 ;;(let ((meta-map (make-sparse-keymap)))
351 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
352 ;;(define-key map [escape] meta-map))
354 "Keymap used while processing translation in simple Quail modes.
355 A few especially complex input methods use `quail-translation-keymap' instead.
356 This map is activated while translation region is active.")
358 (defvar quail-conversion-keymap
359 (let ((map (make-keymap))
362 (define-key map (char-to-string i) 'quail-self-insert-command)
366 (define-key map (vector i) 'quail-self-insert-command)
368 (define-key map "\C-b" 'quail-conversion-backward-char)
369 (define-key map "\C-f" 'quail-conversion-forward-char)
370 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
371 (define-key map "\C-e" 'quail-conversion-end-of-region)
372 (define-key map "\C-d" 'quail-conversion-delete-char)
373 (define-key map "\C-k" 'quail-conversion-delete-tail)
374 (define-key map "\C-h" 'quail-translation-help)
375 (define-key map "\177" 'quail-conversion-backward-delete-char)
376 (define-key map [delete] 'quail-conversion-backward-delete-char)
377 (define-key map [backspace] 'quail-conversion-backward-delete-char)
379 "Keymap used for processing conversion in Quail mode.
380 This map is activated while conversion region is active but translation
381 region is not active.")
383 ;; Just a dummy definition.
384 (defun quail-other-command ()
389 (defun quail-define-package (name language title
390 &optional guidance docstring translation-keys
391 forget-last-selection deterministic
392 kbd-translate show-layout create-decode-map
393 maximum-shortest overlay-plist
394 update-translation-function
395 conversion-keys simple)
396 "Define NAME as a new Quail package for input LANGUAGE.
397 TITLE is a string to be displayed at mode-line to indicate this package.
398 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
399 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
400 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
401 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
403 GUIDANCE specifies how a guidance string is shown in echo area.
404 If it is t, list of all possible translations for the current key is shown
405 with the currently selected translation being highlighted.
406 If it is an alist, the element has the form (CHAR . STRING). Each character
407 in the current key is searched in the list and the corresponding string is
409 If it is nil, the current key is shown.
411 DOCSTRING is the documentation string of this package. The command
412 `describe-input-method' shows this string while replacing the form
413 \\=\\<VAR> in the string by the value of VAR. That value should be a
414 string. For instance, the form \\=\\<quail-translation-docstring> is
415 replaced by a description about how to select a translation from a
418 TRANSLATION-KEYS specifies additional key bindings used while translation
419 region is active. It is an alist of single key character vs. corresponding
420 command to be called.
422 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
423 for the future to translate the same key. If this flag is nil, a
424 translation selected for a key is remembered so that it can be the
425 first candidate when the same key is entered later.
427 DETERMINISTIC non-nil means the first candidate of translation is
428 selected automatically without allowing users to select another
429 translation for a key. In this case, unselected translations are of
430 no use for an interactive use of Quail but can be used by some other
431 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
434 KBD-TRANSLATE non-nil means input characters are translated from a
435 user's keyboard layout to the standard keyboard layout. See the
436 documentation of `quail-keyboard-layout' and
437 `quail-keyboard-layout-standard' for more detail.
439 SHOW-LAYOUT non-nil means the `quail-help' command should show
440 the user's keyboard layout visually with translated characters.
441 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
442 this package defines no translations for single character keys.
444 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
445 map is an alist of translations and corresponding original keys.
446 Although this map is not used by Quail itself, it can be used by some
447 other programs. For instance, Vietnamese supporting needs this map to
448 convert Vietnamese text to VIQR format which uses only ASCII
449 characters to represent Vietnamese characters.
451 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
452 length of the shortest sequence. When we don't have a translation of
453 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
454 the key at \"..AB\" and start translation of \"CD..\". Hangul
455 packages, for instance, use this facility. If this flag is nil, we
456 break the key just at \"..ABC\" and start translation of \"D..\".
458 OVERLAY-PLIST if non-nil is a property list put on an overlay which
459 covers Quail translation region.
461 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
462 the current translation region according to a new translation data. By
463 default, a translated text or a user's key sequence (if no translation
466 CONVERSION-KEYS specifies additional key bindings used while
467 conversion region is active. It is an alist of single key character
468 vs. corresponding command to be called.
470 If SIMPLE is non-nil, then we do not alter the meanings of
471 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
473 (let (translation-keymap conversion-keymap)
474 (if deterministic (setq forget-last-selection t))
477 (setq translation-keymap (copy-keymap
478 (if simple quail-simple-translation-keymap
479 quail-translation-keymap)))
480 (while translation-keys
481 (define-key translation-keymap
482 (car (car translation-keys)) (cdr (car translation-keys)))
483 (setq translation-keys (cdr translation-keys))))
484 (setq translation-keymap
485 (if simple quail-simple-translation-keymap
486 quail-translation-keymap)))
487 (when conversion-keys
488 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
489 (while conversion-keys
490 (define-key conversion-keymap
491 (car (car conversion-keys)) (cdr (car conversion-keys)))
492 (setq conversion-keys (cdr conversion-keys))))
494 (list name title (list nil) guidance (or docstring "")
496 forget-last-selection deterministic kbd-translate show-layout
497 (if create-decode-map (list 'decode-map) nil)
498 maximum-shortest overlay-plist update-translation-function
499 conversion-keymap simple))
501 ;; Update input-method-alist.
502 (let ((slot (assoc name input-method-alist))
503 (val (list language 'quail-use-package title docstring)))
504 (if slot (setcdr slot val)
505 (setq input-method-alist (cons (cons name val) input-method-alist)))))
507 (quail-select-package name))
509 ;; Quail minor mode handlers.
511 ;; Setup overlays used in Quail mode.
512 (defun quail-setup-overlays (conversion-mode)
514 (if (overlayp quail-overlay)
515 (move-overlay quail-overlay pos pos)
516 (setq quail-overlay (make-overlay pos pos))
517 (if input-method-highlight-flag
518 (overlay-put quail-overlay 'face 'underline))
519 (let ((l (quail-overlay-plist)))
521 (overlay-put quail-overlay (car l) (car (cdr l)))
522 (setq l (cdr (cdr l))))))
524 (if (overlayp quail-conv-overlay)
525 (if (not (overlay-start quail-conv-overlay))
526 (move-overlay quail-conv-overlay pos pos))
527 (setq quail-conv-overlay (make-overlay pos pos))
528 (if input-method-highlight-flag
529 (overlay-put quail-conv-overlay 'face 'underline))))))
531 ;; Delete overlays used in Quail mode.
532 (defun quail-delete-overlays ()
533 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
534 (delete-overlay quail-overlay))
535 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
536 (delete-overlay quail-conv-overlay)))
538 (defun quail-inactivate ()
539 "Inactivate Quail input method.
541 This function runs the normal hook `quail-inactivate-hook'."
545 (defun quail-activate (&optional arg)
546 "Activate Quail input method.
547 With arg, activate Quail input method if and only if arg is positive.
549 This function runs `quail-activate-hook' if it activates the input
550 method, `quail-inactivate-hook' if it deactivates it.
552 While this input method is active, the variable
553 `input-method-function' is bound to the function `quail-input-method'."
555 (< (prefix-numeric-value arg) 0))
556 ;; Let's inactivate Quail input method.
559 (quail-delete-overlays)
560 (setq describe-current-input-method-function nil)
561 (quail-hide-guidance)
562 (remove-hook 'post-command-hook 'quail-show-guidance t)
563 (run-hooks 'quail-inactivate-hook))
564 (kill-local-variable 'input-method-function))
565 ;; Let's activate Quail input method.
566 (if (null quail-current-package)
567 ;; Quail package is not yet selected. Select one now.
569 (if quail-package-alist
570 (setq name (car (car quail-package-alist)))
571 (error "No Quail package loaded"))
572 (quail-select-package name)))
573 (setq inactivate-current-input-method-function 'quail-inactivate)
574 (setq describe-current-input-method-function 'quail-help)
575 (quail-delete-overlays)
576 (setq quail-guidance-str "")
577 (quail-show-guidance)
578 ;; If we are in minibuffer, turn off the current input method
580 (when (eq (selected-window) (minibuffer-window))
581 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)
582 (add-hook 'post-command-hook 'quail-show-guidance nil t))
583 (run-hooks 'quail-activate-hook)
584 (make-local-variable 'input-method-function)
585 (setq input-method-function 'quail-input-method)))
587 (defun quail-exit-from-minibuffer ()
588 (inactivate-input-method)
589 (if (<= (minibuffer-depth) 1)
590 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
592 ;; Keyboard layout translation handlers.
594 ;; Some Quail packages provide localized keyboard simulation which
595 ;; requires a particular keyboard layout. In this case, what we need
596 ;; is locations of keys the user entered, not character codes
597 ;; generated by those keys. However, for the moment, there's no
598 ;; common way to get such information. So, we ask a user to give
599 ;; information of his own keyboard layout, then translate it to the
600 ;; standard layout which we defined so that all Quail packages depend
603 (defconst quail-keyboard-layout-standard
606 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
607 qQwWeErRtTyYuUiIoOpP[{]} \
608 aAsSdDfFgGhHjJkKlL;:'\"\\| \
609 zZxXcCvVbBnNmM,<.>/? \
611 "Standard keyboard layout of printable characters Quail assumes.
612 See the documentation of `quail-keyboard-layout' for this format.
613 This layout is almost the same as that of VT100,
614 but the location of key \\ (backslash) is just right of key ' (single-quote),
615 not right of RETURN key.")
617 (defconst quail-keyboard-layout-len 180)
619 ;; Here we provide several examples of famous keyboard layouts.
620 ;; This is a candidate for a language environment-dependent setting.
621 (defvar quail-keyboard-layout-alist
623 (cons "standard" quail-keyboard-layout-standard)
626 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
627 qQwWeErRtTyYuUiIoOpP[{]} \
628 aAsSdDfFgGhHjJkKlL;:'\" \
629 zZxXcCvVbBnNmM,<.>/? \
631 '("atari-german" . "\
633 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
634 qQwWeErRtTzZuUiIoOpP\374\334+* \
635 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
636 <>yYxXcCvVbBnNmM,;.:-_ \
641 ^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
642 qQwWeErRtTzZuUiIoOpP\374\334+* \
643 aAsSdDfFgGhHjJkKlL\366\326\344\304 \
644 <>yYxXcCvVbBnNmM,;.:-_ \
649 1!2\"3#4$5%6&7'8(9)0~-=^~\\| \
650 qQwWeErRtTyYuUiIoOpP@`[{ \
651 aAsSdDfFgGhHjJkKlL;+:*]} \
652 zZxXcCvVbBnNmM,<.>/?\\_ \
656 `\2541!2\"3\2434$5%6^7&8*9(0)-_=+ \
657 qQwWeErRtTyYuUiIoOpP[{]} \
658 aAsSdDfFgGhHjJkKlL;:'@#~ \
659 \\|zZxXcCvVbBnNmM,<.>/? \
662 "Alist of keyboard names and corresponding layout strings.
663 See the documentation of `quail-keyboard-layout' for the format of
666 (defcustom quail-keyboard-layout quail-keyboard-layout-standard
667 "A string which represents physical key layout of a particular keyboard.
668 We assume there are six rows and each row has 15 keys (columns),
669 the first row is above the `1' - `0' row,
670 the first column of the second row is left of key `1',
671 the first column of the third row is left of key `q',
672 the first column of the fourth row is left of key `a',
673 the first column of the fifth row is left of key `z',
674 the sixth row is below the `z' - `/' row.
675 Nth (N is even) and (N+1)th characters in the string are non-shifted
676 and shifted characters respectively at the same location.
677 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
678 The command `quail-set-keyboard-layout' usually sets this variable."
681 ,@(mapcar (lambda (pair)
682 (list 'const :tag (car pair) (cdr pair)))
683 quail-keyboard-layout-alist)
684 (string :tag "Other")))
686 ;; A non-standard keyboard layout may miss some key locations of the
687 ;; standard layout while having additional key locations not in the
688 ;; standard layout. This alist maps those additional key locations to
689 ;; the missing locations. The value is updated automatically by
690 ;; quail-set-keyboard-layout.
691 (defvar quail-keyboard-layout-substitution nil)
693 (defun quail-update-keyboard-layout (kbd-type)
694 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
696 ;; Here, we had better ask a user to define his own keyboard
697 ;; layout interactively.
698 (error "Unknown keyboard type `%s'" kbd-type))
699 (setq quail-keyboard-layout (cdr layout))
700 (let ((i quail-keyboard-layout-len)
701 subst-list missing-list)
702 ;; Sum up additional key locations not in the standard layout in
703 ;; subst-list, and missing key locations in missing-list.
706 (if (= (aref quail-keyboard-layout i) ? )
707 (if (/= (aref quail-keyboard-layout-standard i) ? )
708 (setq missing-list (cons i missing-list)))
709 (if (= (aref quail-keyboard-layout-standard i) ? )
710 (setq subst-list (cons (cons i nil) subst-list)))))
711 (setq quail-keyboard-layout-substitution subst-list)
712 ;; If there are additional key locations, map them to missing
715 (while (and subst-list (cdr (car subst-list)))
716 (setq subst-list (cdr subst-list)))
718 (setcdr (car subst-list) (car missing-list)))
719 (setq missing-list (cdr missing-list))))))
721 (defcustom quail-keyboard-layout-type "standard"
722 "Type of keyboard layout used in Quail base input method.
723 Available types are listed in the variable `quail-keyboard-layout-alist'."
725 :type (cons 'choice (mapcar (lambda (elt)
726 (list 'const (car elt)))
727 quail-keyboard-layout-alist))
728 :set #'(lambda (symbol value)
729 (quail-update-keyboard-layout value)
733 (defun quail-set-keyboard-layout (kbd-type)
734 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
736 Since some Quail packages depends on a physical layout of keys (not
737 characters generated by them), those are created by assuming the
738 standard layout defined in `quail-keyboard-layout-standard'. This
739 function tells Quail system the layout of your keyboard so that what
740 you type is correctly handled."
742 (let* ((completion-ignore-case t)
743 (type (completing-read "Keyboard type: "
744 quail-keyboard-layout-alist)))
746 (quail-update-keyboard-layout kbd-type)
747 (setq quail-keyboard-layout-type kbd-type))
749 (defun quail-keyboard-translate (char)
750 "Translate CHAR to the one in the standard keyboard layout."
751 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
752 ;; All Quail packages are designed based on
753 ;; `quail-keyboard-layout-standard'.
756 ;; Find the key location on the current keyboard layout.
757 (while (and (< i quail-keyboard-layout-len)
758 (/= char (aref quail-keyboard-layout i)))
760 (if (= i quail-keyboard-layout-len)
761 ;; CHAR is not in quail-keyboard-layout, which means that a
762 ;; user typed a key which generated a character code to be
763 ;; handled out of Quail. Just return CHAR and make
764 ;; quail-execute-non-quail-command handle it correctly.
766 (let ((ch (aref quail-keyboard-layout-standard i)))
768 ;; This location not available in the standard keyboard
769 ;; layout. Check if the location is used to substitute
770 ;; for the other location of the standard layout.
771 (if (setq i (cdr (assq i quail-keyboard-layout-substitution)))
772 (aref quail-keyboard-layout-standard i)
773 ;; Just return CHAR as well as above.
777 (defun quail-keyseq-translate (keyseq)
779 (mapcar (function (lambda (x) (quail-keyboard-translate x)))
782 (defun quail-insert-kbd-layout (kbd-layout)
783 "Insert the visual keyboard layout table according to KBD-LAYOUT.
784 The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
785 (let (done-list layout i ch)
786 ;; At first, convert KBD-LAYOUT to the same size vector that
787 ;; contains translated character or string.
788 (setq layout (string-to-vector kbd-layout)
790 (while (< i quail-keyboard-layout-len)
791 (setq ch (aref kbd-layout i))
792 (if (quail-kbd-translate)
793 (setq ch (quail-keyboard-translate ch)))
794 (let* ((map (cdr (assq ch (cdr (quail-map)))))
795 (translation (and map (quail-get-translation
796 (car map) (char-to-string ch) 1))))
799 (if (consp translation)
800 (setq translation (aref (cdr translation) 0)))
801 (setq done-list (cons translation done-list)))
802 (setq translation ch))
803 (aset layout i translation))
809 ;; Make table without horizontal lines. Each column for a key
810 ;; has the form "| LU |" where L is for lower key and and U is
811 ;; for a upper key. If width of L (U) is greater than 1,
812 ;; preceding (following) space is not inserted.
813 (put-text-property 0 1 'face 'bold bar)
815 (while (< i quail-keyboard-layout-len)
819 (insert-char 32 (+ row (/ (- row 2) 2)))))
820 (setq lower (aref layout i)
821 upper (aref layout (1+ i)))
822 (if (and (integerp lower) (>= lower 128) (< lower 256))
823 (setq lower (unibyte-char-to-multibyte lower)))
824 (if (and (integerp upper) (>= upper 128) (< upper 256))
825 (setq upper (unibyte-char-to-multibyte upper)))
827 (if (= (if (stringp lower) (string-width lower) (char-width lower)) 1)
830 (if (= (if (stringp upper) (string-width upper) (char-width upper)) 1)
835 ;; Insert horizontal lines while deleting blank key columns at the
836 ;; beginning and end of each line.
838 (narrow-to-region pos (point))
840 ;;(while (looking-at "[| ]*$")
842 ;;(delete-region pos (point)))
843 (let ((from1 100) (to1 0) from2 to2)
845 (if (looking-at "[| ]*$")
846 ;; The entire row is blank.
847 (delete-region (point) (match-end 0))
848 ;; Delete blank key columns at the head.
849 (if (looking-at " *\\(| \\)+")
850 (subst-char-in-region (point) (match-end 0) ?| ? ))
851 ;; Delete blank key columns at the tail.
852 (if (re-search-forward "\\( |\\)+$" (line-end-position) t)
853 (delete-region (match-beginning 0) (point)))
855 ;; Calculate the start and end columns of a horizontal line.
857 (setq from2 from1 to2 to1)
858 (skip-chars-forward " ")
859 (setq from2 (current-column))
861 (setq to2 (current-column))
867 ;; If the previous or the current line has at least one key
868 ;; column, insert a horizontal line.
870 (insert-char 32 from1)
873 (insert-char ?- (- (- to1 from1) 2))
875 (put-text-property pos (point) 'face 'bold)
877 (setq from1 from2 to1 to2)
879 ;; Insert "space bar" box.
883 " +-----------------------------+
885 +-----------------------------+
887 (put-text-property pos (point) 'face 'bold)
893 (defun quail-show-keyboard-layout (&optional keyboard-type)
894 "Show the physical layout of the keyboard type KEYBOARD-TYPE.
896 The variable `quail-keyboard-layout-type' holds the currently selected
899 (list (completing-read "Keyboard type (default, current choice): "
900 quail-keyboard-layout-alist
902 (or (and keyboard-type (> (length keyboard-type) 0))
903 (setq keyboard-type quail-keyboard-layout-type))
904 (let ((layout (assoc keyboard-type quail-keyboard-layout-alist)))
906 (error "Unknown keyboard type: %s" keyboard-type))
907 (with-output-to-temp-buffer "*Help*"
908 (with-current-buffer standard-output
909 (insert "Keyboard layout (keyboard type: "
912 (quail-insert-kbd-layout (cdr layout))))))
916 (defsubst quail-map-p (object)
917 "Return t if OBJECT is a Quail map.
919 A Quail map holds information how a particular key should be translated.
920 Its format is (TRANSLATION . ALIST).
921 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
922 In the latter case, each element of VECTOR is a candidate for the translation,
923 and INDEX points the currently selected translation.
925 ALIST is normally a list of elements that look like (CHAR . DEFN),
926 where DEFN is another Quail map for a longer key (CHAR added to the
927 current key). It may also be a symbol of a function which returns an
928 alist of the above format.
930 Just after a Quail package is read, TRANSLATION may be a string or a
931 vector. Then each element of the string or vector is a candidate for
932 the translation. These objects are transformed to cons cells in the
933 format \(INDEX . VECTOR), as described above."
935 (let ((translation (car object)))
936 (or (integerp translation) (null translation)
937 (vectorp translation) (stringp translation)
938 (symbolp translation)
939 (and (consp translation) (not (vectorp (cdr translation))))))
940 (let ((alist (cdr object)))
941 (or (and (listp alist) (consp (car alist)))
945 (defmacro quail-define-rules (&rest rules)
946 "Define translation rules of the current Quail package.
947 Each argument is a list of KEY and TRANSLATION.
948 KEY is a string meaning a sequence of keystrokes to be translated.
949 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
950 If it is a character, it is the sole translation of KEY.
951 If it is a string, each character is a candidate for the translation.
952 If it is a vector, each element (string or character) is a candidate
954 In these cases, a key specific Quail map is generated and assigned to KEY.
956 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
957 it is used to handle KEY.
959 The first argument may be an alist of annotations for the following
960 rules. Each element has the form (ANNOTATION . VALUE), where
961 ANNOTATION is a symbol indicating the annotation type. Currently
962 the following annotation types are supported.
964 append -- the value non-nil means that the following rules should
965 be appended to the rules of the current Quail package.
967 face -- the value is a face to use for displaying TRANSLATIONs in
970 advice -- the value is a function to call after one of RULES is
971 selected. The function is called with one argument, the
972 selected TRANSLATION string, after the TRANSLATION is
975 no-decode-map --- the value non-nil means that decoding map is not
976 generated for the following translations."
978 append no-decode-map props)
979 ;; If the first argument is an alist of annotations, handle them.
980 (if (consp (car (car l)))
981 (let ((annotations (car l)))
982 (setq append (assq 'append annotations))
984 (setq annotations (delete append annotations)
985 append (cdr append)))
986 (setq no-decode-map (assq 'no-decode-map annotations))
988 (setq annotations (delete no-decode-map annotations)
989 no-decode-map (cdr no-decode-map)))
990 ;; Convert the remaining annoations to property list PROPS.
993 (cons (car (car annotations))
994 (cons (cdr (car annotations))
996 annotations (cdr annotations)))
998 ;; Process the remaining arguments one by one.
1000 ;; There's no way to add new rules at compiling time.
1003 (decode-map (and (quail-decode-map) (not ,no-decode-map)))
1004 (properties ',props)
1007 (setq key (car (car tail)) trans (car (cdr (car tail)))
1009 (quail-defrule-internal key trans map t decode-map properties)))
1010 ;; We can build up quail map and decode map at compiling time.
1011 (let ((map (list nil))
1012 (decode-map (if (not no-decode-map) (list 'decode-map)))
1015 (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
1016 (quail-defrule-internal key trans map t decode-map props))
1017 `(if (not (quail-decode-map))
1018 (quail-install-map ',map)
1019 (quail-install-map ',map)
1020 (quail-install-decode-map ',decode-map))))))
1023 (defun quail-install-map (map &optional name)
1024 "Install the Quail map MAP in the current Quail package.
1026 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1027 which to install MAP.
1029 The installed map can be referred by the function `quail-map'."
1030 (if (null quail-current-package)
1031 (error "No current Quail package"))
1032 (if (null (quail-map-p map))
1033 (error "Invalid Quail map `%s'" map))
1034 (setcar (cdr (cdr quail-current-package)) map))
1037 (defun quail-install-decode-map (decode-map &optional name)
1038 "Install the Quail decode map DECODE-MAP in the current Quail package.
1040 Optional 2nd arg NAME, if non-nil, is a name of Quail package for
1041 which to install MAP.
1043 The installed decode map can be referred by the function `quail-decode-map'."
1044 (if (null quail-current-package)
1045 (error "No current Quail package"))
1046 (if (not (and (consp decode-map) (eq (car decode-map) 'decode-map)))
1047 (error "Invalid Quail decode map `%s'" decode-map))
1048 (setcar (nthcdr 10 quail-current-package) decode-map))
1051 (defun quail-defrule (key translation &optional name append)
1052 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
1053 KEY is a string meaning a sequence of keystrokes to be translated.
1054 TRANSLATION is a character, a string, a vector, a Quail map,
1055 a function, or a cons.
1056 It it is a character, it is the sole translation of KEY.
1057 If it is a string, each character is a candidate for the translation.
1058 If it is a vector, each element (string or character) is a candidate
1059 for the translation.
1060 If it is a cons, the car is one of the above and the cdr is a function
1061 to call when translating KEY (the return value is assigned to the
1062 variable `quail-current-data'). If the cdr part is not a function,
1063 the value itself is assigned to `quail-current-data'.
1064 In these cases, a key specific Quail map is generated and assigned to KEY.
1066 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
1067 it is used to handle KEY.
1069 Optional 3rd argument NAME, if specified, says which Quail package
1070 to define this translation rule in. The default is to define it in the
1071 current Quail package.
1073 Optional 4th argument APPEND, if non-nil, appends TRANSLATION
1074 to the current translations for KEY instead of replacing them."
1076 (let ((package (quail-package name)))
1078 (error "No Quail package `%s'" name))
1079 (setq quail-current-package package)))
1080 (quail-defrule-internal key translation (quail-map) append))
1083 (defun quail-defrule-internal (key trans map &optional append decode-map props)
1084 "Define KEY as TRANS in a Quail map MAP.
1086 If Optional 4th arg APPEND is non-nil, TRANS is appended to the
1087 current translations for KEY instead of replacing them.
1089 Optional 5th arg DECODE-MAP is a Quail decode map.
1091 Optional 6th arg PROPS is a property list annotating TRANS. See the
1092 function `quail-define-rules' for the detail."
1093 (if (null (stringp key))
1094 "Invalid Quail key `%s'" key)
1095 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
1098 (quail-map-p trans)))
1099 (error "Invalid Quail translation `%s'" trans))
1100 (if (null (quail-map-p map))
1101 (error "Invalid Quail map `%s'" map))
1102 (let ((len (length key))
1105 ;; Make a map for registering TRANS if necessary.
1107 (if (null (consp map))
1108 ;; We come here, for example, when we try to define a rule
1109 ;; for "ABC" but a rule for "AB" is already defined as a
1111 (error "Quail key %s is too long" key))
1112 (setq ch (aref key idx)
1113 entry (assq ch (cdr map)))
1116 (setq entry (cons ch (list nil)))
1117 (setcdr map (cons entry (cdr map)))))
1118 (setq map (cdr entry))
1119 (setq idx (1+ idx)))
1122 ;; We come here, for example, when we try to define a rule
1123 ;; for "AB" as a symbol but a rule for "ABC" is already
1125 (error "Quail key %s is too short" key)
1126 (setcdr entry trans))
1127 (if (quail-map-p trans)
1128 (if (not (listp (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
1132 (error "Quail key %s is too short" key)
1133 (if (not (listp (cdr trans)))
1135 ;; We come here, for example, when we try to
1136 ;; define a rule for "AB" as a symbol but a rule
1137 ;; for "ABC" is already defined.
1138 (error "Quail key %s is too short" key)
1139 (setcdr entry trans))
1140 (setcdr entry (append trans (cdr map)))))
1141 ;; If PROPS is non-nil or DECODE-MAP is non-nil, convert TRANS
1142 ;; to a vector of strings, add PROPS to each string and record
1143 ;; this rule in DECODE-MAP.
1144 (when (and (or props decode-map)
1145 (not (consp trans)) (not (symbolp trans)))
1146 (if (integerp trans)
1147 (setq trans (vector trans))
1149 (setq trans (string-to-vector trans))))
1150 (let ((len (length trans))
1154 (setq elt (aref trans len))
1156 (setq elt (char-to-string elt)))
1157 (aset trans len elt)
1159 (add-text-properties 0 (length elt) props elt))
1162 (cons (cons elt key) (cdr decode-map)))))))
1163 (if (and (car map) append)
1164 (let ((prev (quail-get-translation (car map) key len)))
1166 (setq prev (vector prev))
1167 (setq prev (cdr prev)))
1168 (if (integerp trans)
1169 (setq trans (vector trans))
1171 (setq trans (string-to-vector trans))))
1173 (cons (list 0 0 0 0 nil)
1174 (vconcat prev trans)))))
1175 (setcar map trans)))))
1177 (defun quail-get-translation (def key len)
1178 "Return the translation specified as DEF for KEY of length LEN.
1179 The translation is either a character or a cons of the form (INDEX . VECTOR),
1180 where VECTOR is a vector of candidates (character or string) for
1181 the translation, and INDEX points into VECTOR to specify the currently
1182 selected translation."
1183 (if (and def (symbolp def))
1185 ;; DEF is a symbol of a function which returns valid translation.
1186 (setq def (funcall def key len))
1188 (if (and (consp def) (not (vectorp (cdr def))))
1189 (setq def (car def)))
1192 ((or (integerp def) (consp def))
1200 ;; If the length is 1, we don't need vector but a single candidate
1201 ;; as the translation.
1202 (if (= (length def) 1)
1204 ;; Each character in DEF is a candidate of translation. Reform
1205 ;; it as (INDICES . VECTOR).
1206 (cons (list 0 0 0 0 nil) (string-to-vector def))))
1209 ;; If the length is 1, and the length of element string is 1, we
1210 ;; don't need vector but a single candidate as the translation.
1211 (if (and (= (length def) 1)
1212 (= (length (aref def 0)) 1))
1213 (aref (aref def 0) 0)
1214 ;; Each element (string or character) in DEF is a candidate of
1215 ;; translation. Reform it as (INDICES . VECTOR).
1216 (cons (list 0 0 0 0 nil) def)))
1219 (error "Invalid object in Quail map: %s" def))))
1221 (defun quail-lookup-key (key &optional len)
1222 "Lookup KEY of length LEN in the current Quail map and return the definition.
1223 The returned value is a Quail map specific to KEY."
1225 (setq len (length key)))
1228 (kbd-translate (quail-kbd-translate))
1229 slot ch translation def)
1230 (while (and map (< idx len))
1231 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
1234 (if (and (cdr map) (symbolp (cdr map)))
1235 (setcdr map (funcall (cdr map) key idx)))
1236 (setq slot (assq ch (cdr map)))
1237 (if (and (cdr slot) (symbolp (cdr slot)))
1238 (setcdr slot (funcall (cdr slot) key idx)))
1239 (setq map (cdr slot)))
1240 (setq def (car map))
1241 (setq quail-current-translations nil)
1242 (if (and map (setq translation (quail-get-translation def key len)))
1244 (if (and (consp def) (not (vectorp (cdr def))))
1246 (if (not (equal (car def) translation))
1247 ;; We must reflect TRANSLATION to car part of DEF.
1248 (setcar def translation))
1249 (setq quail-current-data
1250 (if (functionp (cdr def))
1253 (if (not (equal def translation))
1254 ;; We must reflect TRANSLATION to car part of MAP.
1255 (setcar map translation)))
1256 (if (and (consp translation) (vectorp (cdr translation)))
1258 (setq quail-current-translations translation)
1259 (if (quail-forget-last-selection)
1260 (setcar (car quail-current-translations) 0))))))
1261 ;; We may have to reform cdr part of MAP.
1262 (if (and (cdr map) (functionp (cdr map)))
1263 (setcdr map (funcall (cdr map) key len)))
1266 (put 'quail-error 'error-conditions '(quail-error error))
1267 (defun quail-error (&rest args)
1268 (signal 'quail-error (apply 'format args)))
1270 (defun quail-input-string-to-events (str)
1271 "Convert input string STR to a list of events.
1272 Do so while interleaving with the following special events:
1273 \(compose-last-chars LEN COMPONENTS)
1274 \(quail-advice INPUT-STRING)"
1275 (let* ((events (mapcar
1277 ;; This gives us the chance to unify on input
1278 ;; (e.g. using ucs-tables.el).
1279 (or (and translation-table-for-input
1280 (aref translation-table-for-input c))
1285 composition from to)
1286 (while (and (> idx 0)
1287 (setq composition (find-composition idx 0 str t)))
1288 (setq from (car composition) to (nth 1 composition))
1289 (setcdr (nthcdr (1- to) events)
1290 (cons (list 'compose-last-chars (- to from)
1291 (and (not (nth 3 composition)) (nth 2 composition)))
1292 (nthcdr to events)))
1293 (setq idx (1- from)))
1294 (if (or (get-text-property 0 'advice str)
1295 (next-single-property-change 0 'advice str))
1297 (nconc events (list (list 'quail-advice str)))))
1300 (defvar quail-translating nil)
1301 (defvar quail-converting nil)
1302 (defvar quail-conversion-str nil)
1304 (defun quail-input-method (key)
1305 (if (or buffer-read-only
1306 overriding-terminal-local-map
1307 overriding-local-map)
1309 (quail-setup-overlays (quail-conversion-keymap))
1310 (let ((modified-p (buffer-modified-p))
1311 (buffer-undo-list t))
1313 (let ((input-string (if (quail-conversion-keymap)
1314 (quail-start-conversion key)
1315 (quail-start-translation key))))
1316 (setq quail-guidance-str "")
1317 (when (and (stringp input-string)
1318 (> (length input-string) 0))
1319 (if input-method-exit-on-first-char
1320 (list (aref input-string 0))
1321 (quail-input-string-to-events input-string))))
1322 (quail-delete-overlays)
1323 (set-buffer-modified-p modified-p)
1324 ;; Run this hook only when the current input method doesn't require
1325 ;; conversion. When conversion is required, the conversion function
1326 ;; should run this hook at a proper timing.
1327 (unless (quail-conversion-keymap)
1328 (run-hooks 'input-method-after-insert-chunk-hook))))))
1330 (defun quail-overlay-region-events (overlay)
1331 (let ((start (overlay-start overlay))
1332 (end (overlay-end overlay)))
1335 (string-to-list (buffer-substring start end))
1336 (delete-region start end)))))
1338 (defsubst quail-delete-region ()
1339 "Delete the text in the current translation region of Quail."
1340 (if (overlay-start quail-overlay)
1341 (delete-region (overlay-start quail-overlay)
1342 (overlay-end quail-overlay))))
1344 (defun quail-start-translation (key)
1345 "Start translation of the typed character KEY by the current Quail package.
1346 Return the input string."
1347 ;; Check the possibility of translating KEY.
1348 ;; If KEY is nil, we can anyway start translation.
1349 (if (or (and (integerp key)
1350 (assq (if (quail-kbd-translate)
1351 (quail-keyboard-translate key) key)
1354 ;; OK, we can start translation.
1355 (let* ((echo-keystrokes 0)
1357 (overriding-terminal-local-map (quail-translation-keymap))
1358 (generated-events nil)
1359 (input-method-function nil)
1360 (modified-p (buffer-modified-p))
1361 last-command-event last-command this-command)
1362 (setq quail-current-key ""
1363 quail-current-str ""
1364 quail-translating t)
1366 (setq unread-command-events (cons key unread-command-events)))
1367 (while quail-translating
1368 (set-buffer-modified-p modified-p)
1369 (quail-show-guidance)
1370 (let* ((keyseq (read-key-sequence
1371 (and input-method-use-echo-area
1372 (concat input-method-previous-message
1375 (cmd (lookup-key (quail-translation-keymap) keyseq)))
1377 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
1378 (eq cmd 'quail-self-insert-command))
1380 (setq last-command-event (aref keyseq (1- (length keyseq)))
1381 last-command this-command
1385 (call-interactively cmd)
1386 (quail-error (message "%s" (cdr err)) (beep))))
1387 ;; KEYSEQ is not defined in the translation keymap.
1388 ;; Let's return the event(s) to the caller.
1389 (setq unread-command-events
1390 (string-to-list (this-single-command-raw-keys)))
1391 (setq quail-translating nil))))
1392 (quail-delete-region)
1395 ;; Since KEY doesn't start any translation, just return it.
1396 ;; But translate KEY if necessary.
1397 (if (quail-kbd-translate)
1398 (setq key (quail-keyboard-translate key)))
1399 (char-to-string key)))
1401 (defun quail-start-conversion (key)
1402 "Start conversion of the typed character KEY by the current Quail package.
1403 Return the input string."
1404 ;; Check the possibility of translating KEY.
1405 ;; If KEY is nil, we can anyway start translation.
1406 (if (or (and (integerp key)
1407 (assq (if (quail-kbd-translate)
1408 (quail-keyboard-translate key) key)
1411 ;; Ok, we can start translation and conversion.
1412 (let* ((echo-keystrokes 0)
1414 (overriding-terminal-local-map (quail-conversion-keymap))
1415 (generated-events nil)
1416 (input-method-function nil)
1417 (modified-p (buffer-modified-p))
1418 last-command-event last-command this-command)
1419 (setq quail-current-key ""
1420 quail-current-str ""
1423 quail-conversion-str "")
1425 (setq unread-command-events (cons key unread-command-events)))
1426 (while quail-converting
1427 (set-buffer-modified-p modified-p)
1428 (or quail-translating
1430 (setq quail-current-key ""
1431 quail-current-str ""
1432 quail-translating t)
1433 (quail-setup-overlays nil)))
1434 (quail-show-guidance)
1435 (let* ((keyseq (read-key-sequence
1436 (and input-method-use-echo-area
1437 (concat input-method-previous-message
1438 quail-conversion-str
1441 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
1442 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
1444 (setq last-command-event (aref keyseq (1- (length keyseq)))
1445 last-command this-command
1449 (call-interactively cmd)
1450 (quail-error (message "%s" (cdr err)) (beep)))
1451 (or quail-translating
1453 (if quail-current-str
1454 (setq quail-conversion-str
1455 (concat quail-conversion-str
1456 (if (stringp quail-current-str)
1458 (char-to-string quail-current-str)))))
1459 (if (or input-method-exit-on-first-char
1460 (= (length quail-conversion-str) 0))
1461 (setq quail-converting nil)))))
1462 ;; KEYSEQ is not defined in the conversion keymap.
1463 ;; Let's return the event(s) to the caller.
1464 (setq unread-command-events
1465 (string-to-list (this-single-command-raw-keys)))
1466 (setq quail-converting nil))))
1467 (setq quail-translating nil)
1468 (if (overlay-start quail-conv-overlay)
1469 (delete-region (overlay-start quail-conv-overlay)
1470 (overlay-end quail-conv-overlay)))
1471 (if (> (length quail-conversion-str) 0)
1472 quail-conversion-str))
1474 ;; Since KEY doesn't start any translation, just return it.
1475 ;; But translate KEY if necessary.
1476 (if (quail-kbd-translate)
1477 (setq key (quail-keyboard-translate key)))
1478 (char-to-string key)))
1480 (defun quail-terminate-translation ()
1481 "Terminate the translation of the current key."
1482 (setq quail-translating nil)
1483 (setq quail-guidance-str " "))
1485 (defun quail-select-current ()
1486 "Accept the currently selected translation."
1488 (quail-terminate-translation))
1490 (defun quail-update-translation (control-flag)
1491 "Update the current translation status according to CONTROL-FLAG.
1492 If CONTROL-FLAG is integer value, it is the number of keys in the
1493 head `quail-current-key' which can be translated. The remaining keys
1494 are put back to `unread-command-events' to be handled again. If
1495 CONTROL-FLAG is t, terminate the translation for the whole keys in
1496 `quail-current-key'. If CONTROL-FLAG is nil, proceed the translation
1498 (let ((func (quail-update-translation-function)))
1500 (setq control-flag (funcall func control-flag))
1501 (cond ((numberp control-flag)
1502 (let ((len (length quail-current-key)))
1503 (if (= control-flag 0)
1504 (setq quail-current-str
1505 (if (quail-kbd-translate)
1506 (quail-keyseq-translate quail-current-key)
1507 quail-current-key)))
1508 (or input-method-exit-on-first-char
1509 (while (> len control-flag)
1511 (setq unread-command-events
1512 (cons (aref quail-current-key len)
1513 unread-command-events))))))
1514 ((null control-flag)
1515 (unless quail-current-str
1516 (setq quail-current-str
1517 (if (quail-kbd-translate)
1518 (quail-keyseq-translate quail-current-key)
1520 (if (and input-method-exit-on-first-char
1522 (setq control-flag t)))))))
1523 (or input-method-use-echo-area
1525 (quail-delete-region)
1527 (insert quail-current-str)
1528 (move-overlay quail-overlay pos (point))
1529 (if (overlayp quail-conv-overlay)
1530 (if (not (overlay-start quail-conv-overlay))
1531 (move-overlay quail-conv-overlay pos (point))
1532 (if (< (overlay-end quail-conv-overlay) (point))
1533 (move-overlay quail-conv-overlay
1534 (overlay-start quail-conv-overlay)
1536 (let (quail-current-str)
1537 (quail-update-guidance))
1538 (or (stringp quail-current-str)
1539 (setq quail-current-str (char-to-string quail-current-str)))
1541 (quail-terminate-translation)))
1543 (defun quail-self-insert-command ()
1544 "Translate the typed key by the current Quail map, and insert."
1546 (setq quail-current-key
1547 (concat quail-current-key (char-to-string last-command-event)))
1548 (or (catch 'quail-tag
1549 (quail-update-translation (quail-translate-key))
1551 ;; If someone throws for `quail-tag' by value nil, we exit from
1552 ;; translation mode.
1553 (setq quail-translating nil)))
1555 (defun quail-map-definition (map)
1556 "Return the actual definition part of Quail map MAP."
1557 (let ((def (car map)))
1558 (if (and (consp def) (not (vectorp (cdr def))))
1559 (setq def (car def)))
1564 (defun quail-get-current-str (len def)
1565 "Return string to be shown as current translation of key sequence.
1566 LEN is the length of the sequence. DEF is a definition part of the
1567 Quail map for the sequence."
1568 (or (and (consp def) (aref (cdr def) (car (car def))))
1571 (let ((str (quail-get-current-str
1573 (quail-map-definition (quail-lookup-key
1574 quail-current-key (1- len))))))
1576 (concat (if (stringp str) str (char-to-string str))
1577 (substring quail-current-key (1- len) len)))))))
1579 (defvar quail-guidance-translations-starting-column 20)
1581 (defun quail-update-current-translations (&optional relative-index)
1582 "Update `quail-current-translations'.
1583 Make RELATIVE-INDEX the current translation."
1584 (let* ((indices (car quail-current-translations))
1586 (start (nth 1 indices))
1587 (end (nth 2 indices)))
1588 ;; Validate the index number of current translation.
1590 (setcar indices (setq cur 0))
1591 (if (>= cur (length (cdr quail-current-translations)))
1593 (setq cur (1- (length (cdr quail-current-translations)))))))
1595 (if (or (null end) ; We have not yet calculated END.
1596 (< cur start) ; We moved to the previous block.
1597 (>= cur end)) ; We moved to the next block.
1598 (let ((len (length (cdr quail-current-translations)))
1599 (maxcol (- (window-width)
1600 quail-guidance-translations-starting-column))
1601 (block (nth 3 indices))
1602 col idx width trans num-items blocks)
1604 ;; We must calculate from the head.
1605 (setq start 0 block 0)
1606 (if end ; i.e. (>= cur end)
1608 (setq idx start col 0 end start num-items 0)
1609 ;; Loop until we hit the tail, or reach the block of CUR.
1610 (while (and (< idx len) (>= cur end))
1612 (setq start idx col 0 block (1+ block)))
1613 (setq trans (aref (cdr quail-current-translations) idx))
1614 (setq width (if (integerp trans) (char-width trans)
1615 (string-width trans)))
1616 (setq col (+ col width 3) num-items (1+ num-items))
1617 (if (and (> num-items 0)
1618 (or (>= col maxcol) (> num-items 10)))
1619 (setq end idx num-items 0)
1620 (setq idx (1+ idx))))
1621 (setcar (nthcdr 3 indices) block)
1624 ;; We hit the tail before reaching MAXCOL.
1626 (setcar (nthcdr 4 indices) block)))
1627 (setcar (cdr indices) start)
1628 (setcar (nthcdr 2 indices) end)))
1630 (if (>= (+ start relative-index) end)
1631 (setcar indices (1- end))
1632 (setcar indices (+ start relative-index))))
1633 (setq quail-current-str
1634 (aref (cdr quail-current-translations) (car indices)))
1635 (or (stringp quail-current-str)
1636 (setq quail-current-str (char-to-string quail-current-str)))))
1638 (defun quail-translate-key ()
1639 "Translate the current key sequence according to the current Quail map.
1640 Return t if we can terminate the translation.
1641 Return nil if the current key sequence may be followed by more keys.
1642 Return number if we can't find any translation for the current key
1643 sequence. The number is the count of valid keys in the current
1644 sequence counting from the head."
1645 (let* ((len (length quail-current-key))
1646 (map (quail-lookup-key quail-current-key len))
1649 (let ((def (quail-map-definition map)))
1650 (setq quail-current-str (quail-get-current-str len def))
1651 ;; Return t only if we can terminate the current translation.
1653 ;; No alternative translations.
1654 (or (null (consp def)) (= (length (cdr def)) 1))
1655 ;; No translation for the longer key.
1657 ;; No shorter breaking point.
1658 (or (null (quail-maximum-shortest))
1660 (null (quail-lookup-key quail-current-key (1- len)))
1661 (null (quail-lookup-key
1662 (substring quail-current-key -2 -1) 1)))))
1664 ;; There's no translation for the current key sequence. Before
1665 ;; giving up, we must check two possibilities.
1667 (quail-maximum-shortest)
1669 (setq def (quail-map-definition
1670 (quail-lookup-key quail-current-key (- len 2))))
1671 (quail-lookup-key (substring quail-current-key -2) 2))
1672 ;; Now the sequence is "...ABCD", which can be split into
1673 ;; "...AB" and "CD..." to get valid translation.
1674 ;; At first, get translation of "...AB".
1675 (setq quail-current-str (quail-get-current-str (- len 2) def))
1676 ;; Then, return the length of "...AB".
1680 (quail-lookup-key (substring quail-current-key 0 -1))
1681 quail-current-translations
1682 (not (quail-deterministic))
1683 (setq ch (aref quail-current-key (1- len)))
1684 (>= ch ?0) (<= ch ?9))
1685 ;; A numeric key is entered to select a desirable translation.
1686 (setq quail-current-key (substring quail-current-key 0 -1))
1687 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1688 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1689 (quail-update-current-translations ch)
1690 ;; And, we can terminate the current translation.
1694 ;; No way to handle the last character in this context.
1697 (defun quail-next-translation ()
1698 "Select next translation in the current batch of candidates."
1700 (if quail-current-translations
1701 (let ((indices (car quail-current-translations)))
1702 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1703 ;; We are already at the tail.
1705 (setcar indices (1+ (car indices)))
1706 (quail-update-current-translations)
1707 (quail-update-translation nil)))
1708 (setq unread-command-events
1709 (cons last-command-event unread-command-events))
1710 (quail-terminate-translation)))
1712 (defun quail-prev-translation ()
1713 "Select previous translation in the current batch of candidates."
1715 (if quail-current-translations
1716 (let ((indices (car quail-current-translations)))
1717 (if (= (car indices) 0)
1718 ;; We are already at the head.
1720 (setcar indices (1- (car indices)))
1721 (quail-update-current-translations)
1722 (quail-update-translation nil)))
1723 (setq unread-command-events
1724 (cons last-command-event unread-command-events))
1725 (quail-terminate-translation)))
1727 (defun quail-next-translation-block ()
1728 "Select from the next block of translations."
1730 (if quail-current-translations
1731 (let* ((indices (car quail-current-translations))
1732 (offset (- (car indices) (nth 1 indices))))
1733 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1734 ;; We are already at the last block.
1736 (setcar indices (+ (nth 2 indices) offset))
1737 (quail-update-current-translations)
1738 (quail-update-translation nil)))
1739 (setq unread-command-events
1740 (cons last-command-event unread-command-events))
1741 (quail-terminate-translation)))
1743 (defun quail-prev-translation-block ()
1744 "Select the previous batch of 10 translation candidates."
1746 (if quail-current-translations
1747 (let* ((indices (car quail-current-translations))
1748 (offset (- (car indices) (nth 1 indices))))
1749 (if (= (nth 1 indices) 0)
1750 ;; We are already at the first block.
1752 (setcar indices (1- (nth 1 indices)))
1753 (quail-update-current-translations)
1754 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1756 (setcar indices (+ (nth 1 indices) offset))
1757 (quail-update-current-translations)))
1758 (quail-update-translation nil)))
1759 (setq unread-command-events
1760 (cons last-command-event unread-command-events))
1761 (quail-terminate-translation)))
1763 (defun quail-abort-translation ()
1764 "Abort translation and delete the current Quail key sequence."
1766 (quail-delete-region)
1767 (setq quail-current-str nil)
1768 (quail-terminate-translation))
1770 (defun quail-delete-last-char ()
1771 "Delete the last input character from the current Quail key sequence."
1773 (if (= (length quail-current-key) 1)
1774 (quail-abort-translation)
1775 (setq quail-current-key (substring quail-current-key 0 -1))
1776 (quail-delete-region)
1777 (quail-update-translation (quail-translate-key))))
1779 ;; For conversion mode.
1781 (defsubst quail-point-in-conversion-region ()
1782 "Return non-nil value if the point is in conversion region of Quail mode."
1784 (and (setq start (overlay-start quail-conv-overlay))
1785 (>= (setq pos (point)) start)
1786 (<= pos (overlay-end quail-conv-overlay)))))
1788 (defun quail-conversion-backward-char ()
1790 (if (<= (point) (overlay-start quail-conv-overlay))
1791 (quail-error "Beginning of conversion region"))
1792 (setq quail-translating nil)
1795 (defun quail-conversion-forward-char ()
1797 (if (>= (point) (overlay-end quail-conv-overlay))
1798 (quail-error "End of conversion region"))
1799 (setq quail-translating nil)
1802 (defun quail-conversion-beginning-of-region ()
1804 (setq quail-translating nil)
1805 (goto-char (overlay-start quail-conv-overlay)))
1807 (defun quail-conversion-end-of-region ()
1809 (setq quail-translating nil)
1810 (goto-char (overlay-end quail-conv-overlay)))
1812 (defun quail-conversion-delete-char ()
1814 (setq quail-translating nil)
1815 (if (>= (point) (overlay-end quail-conv-overlay))
1816 (quail-error "End of conversion region"))
1818 (let ((start (overlay-start quail-conv-overlay))
1819 (end (overlay-end quail-conv-overlay)))
1820 (setq quail-conversion-str (buffer-substring start end))
1822 (setq quail-converting nil))))
1824 (defun quail-conversion-delete-tail ()
1826 (if (>= (point) (overlay-end quail-conv-overlay))
1827 (quail-error "End of conversion region"))
1828 (delete-region (point) (overlay-end quail-conv-overlay))
1829 (let ((start (overlay-start quail-conv-overlay))
1830 (end (overlay-end quail-conv-overlay)))
1831 (setq quail-conversion-str (buffer-substring start end))
1833 (setq quail-converting nil))))
1835 (defun quail-conversion-backward-delete-char ()
1837 (if (> (length quail-current-key) 0)
1838 (quail-delete-last-char)
1839 (if (<= (point) (overlay-start quail-conv-overlay))
1840 (quail-error "Beginning of conversion region"))
1842 (let ((start (overlay-start quail-conv-overlay))
1843 (end (overlay-end quail-conv-overlay)))
1844 (setq quail-conversion-str (buffer-substring start end))
1846 (setq quail-converting nil)))))
1848 (defun quail-do-conversion (func &rest args)
1849 "Call FUNC to convert text in the current conversion region of Quail.
1850 Remaining args are for FUNC."
1851 (delete-overlay quail-overlay)
1854 (defun quail-no-conversion ()
1855 "Do no conversion of the current conversion region of Quail."
1857 (setq quail-converting nil))
1859 ;; Guidance, Completion, and Help buffer handlers.
1861 (defun quail-make-guidance-frame ()
1862 "Make a new one-line frame for Quail guidance."
1863 (let* ((fparam (frame-parameters))
1864 (top (cdr (assq 'top fparam)))
1865 (border (cdr (assq 'border-width fparam)))
1866 (internal-border (cdr (assq 'internal-border-width fparam)))
1868 (frame-char-height) (* internal-border 2) (* border 2))))
1870 (setq newtop (+ top (frame-pixel-height) internal-border border)))
1871 (make-frame (append '((user-position . t) (height . 1)
1873 (menu-bar-lines . 0) (tool-bar-lines . 0))
1874 (cons (cons 'top newtop) fparam)))))
1876 (defun quail-setup-completion-buf ()
1877 "Setup Quail completion buffer."
1878 (unless (buffer-live-p quail-completion-buf)
1879 (let ((default-enable-multibyte-characters enable-multibyte-characters))
1880 (setq quail-completion-buf (get-buffer-create "*Quail Completions*")))
1881 (with-current-buffer quail-completion-buf
1882 (setq quail-overlay (make-overlay 1 1))
1883 (overlay-put quail-overlay 'face 'highlight))))
1885 (defun quail-require-guidance-buf ()
1886 "Return t iff the current Quail package requires showing guidance buffer."
1887 (and input-method-verbose-flag
1888 (if (eq input-method-verbose-flag 'default)
1889 (not (and (eq (selected-window) (minibuffer-window))
1891 (if (eq input-method-verbose-flag 'complex-only)
1892 (not (quail-simple))
1896 ;; Quail specific version of minibuffer-message. It displays STRING
1897 ;; with timeout 1000000 seconds instead of two seconds.
1899 (defun quail-minibuffer-message (string)
1901 (let ((point-max (point-max))
1904 (goto-char point-max)
1907 (delete-region point-max (point-max))
1910 unread-command-events '(7)))))
1912 (defun quail-show-guidance ()
1913 "Display a guidance for Quail input method in some window.
1914 The guidance is normally displayed at the echo area,
1915 or in a newly created frame (if the current buffer is a
1916 minibuffer and the selected frame has no other windows)."
1917 ;; At first, setup a buffer for completion.
1918 (quail-setup-completion-buf)
1919 (bury-buffer quail-completion-buf)
1921 ;; Then, show the guidance.
1922 (when (and (quail-require-guidance-buf)
1923 (null unread-command-events)
1924 (null unread-post-input-method-events))
1925 (if (or (eq (selected-window) (minibuffer-window))
1926 input-method-use-echo-area)
1927 (if (eq (minibuffer-window) (frame-root-window))
1928 ;; Use another frame. It is sure that we are using some
1930 (let ((guidance quail-guidance-str))
1931 (or (frame-live-p quail-guidance-frame)
1932 (setq quail-guidance-frame
1933 (quail-make-guidance-frame)))
1934 (or (buffer-live-p quail-guidance-buf)
1935 (setq quail-guidance-buf
1936 (get-buffer-create " *Quail-guidance*")))
1938 (set-buffer quail-guidance-buf)
1940 (setq cursor-type nil)
1942 (set-window-buffer (frame-root-window quail-guidance-frame)
1944 (quail-minibuffer-message
1945 (format " [%s]" current-input-method-title)))
1946 ;; Show the guidance in the next line of the currrent
1948 (quail-minibuffer-message
1950 current-input-method-title quail-guidance-str)))
1951 ;; Show the guidance in echo area without logging.
1952 (let ((message-log-max nil))
1953 (message "%s" quail-guidance-str)))))
1955 (defun quail-hide-guidance ()
1956 "Hide the Quail guidance."
1957 (when (and (quail-require-guidance-buf)
1958 (or (eq (selected-window) (minibuffer-window))
1959 input-method-use-echo-area)
1960 (eq (minibuffer-window) (frame-root-window)))
1961 ;; We are using another frame for the guidance.
1962 (if (frame-live-p quail-guidance-frame)
1963 (delete-frame quail-guidance-frame))
1964 (if (buffer-live-p quail-guidance-buf)
1965 (kill-buffer quail-guidance-buf))))
1967 (defun quail-update-guidance ()
1968 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1969 ;; Update the guidance string.
1970 (when (quail-require-guidance-buf)
1971 (let ((guidance (quail-guidance)))
1972 (cond ((or (eq guidance t)
1974 ;; Show the current possible translations.
1975 (setq quail-guidance-str
1976 (quail-get-translations)))
1978 ;; Show the current input keys.
1979 (let ((key quail-current-key))
1980 (if (quail-kbd-translate)
1981 (setq key (quail-keyseq-translate key)))
1982 (setq quail-guidance-str (if (stringp key) key (string key)))))
1984 (setq quail-guidance-str " ")))))
1986 ;; Update completion buffer if displayed now. We highlight the
1987 ;; selected candidate string in *Completion* buffer if any.
1988 (let ((win (get-buffer-window quail-completion-buf))
1992 (setq str (if (stringp quail-current-str)
1994 (if (numberp quail-current-str)
1995 (char-to-string quail-current-str)))
1996 key quail-current-key)
1997 (set-buffer quail-completion-buf)
1998 (goto-char (point-min))
1999 (if (null (search-forward (concat " " key ":") nil t))
2000 (delete-overlay quail-overlay)
2002 (if (and str (search-forward (concat "." str) nil t))
2003 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
2004 (move-overlay quail-overlay (match-beginning 0) (point)))
2005 ;; Now POS points end of KEY and (point) points end of STR.
2006 (if (pos-visible-in-window-p (point) win)
2007 ;; STR is already visible.
2009 ;; We want to make both KEY and STR visible, but if the
2010 ;; window is too short, make at least STR visible.
2011 (setq pos (progn (point) (goto-char pos)))
2013 (set-window-start win (point))
2014 (if (not (pos-visible-in-window-p pos win))
2015 (set-window-start win pos))
2018 (defun quail-get-translations ()
2019 "Return a string containing the current possible translations."
2020 (let ((map (quail-lookup-key quail-current-key))
2021 (str (copy-sequence quail-current-key)))
2022 (if quail-current-translations
2023 (quail-update-current-translations))
2025 ;; Show the current key.
2026 (let ((guidance (quail-guidance)))
2027 (if (listp guidance)
2028 ;; We must replace thetyped key with the specified PROMPTKEY.
2029 (dotimes (i (length str))
2030 (let ((prompt-key (cdr (assoc (aref str i) guidance))))
2032 (aset str i (aref prompt-key 0)))))))
2034 ;; Show followable keys.
2035 (if (and (> (length quail-current-key) 0) (cdr map))
2039 (concat (sort (mapcar (function (lambda (x) (car x)))
2042 ;; Show list of translations.
2043 (if (and quail-current-translations
2044 (not (quail-deterministic)))
2045 (let* ((indices (car quail-current-translations))
2047 (start (nth 1 indices))
2048 (end (nth 2 indices))
2050 (if (< (string-width str)
2051 (- quail-guidance-translations-starting-column 7))
2055 (- quail-guidance-translations-starting-column
2056 7 (string-width str))
2058 (setq str (format "%s(%02d/%s)"
2061 (format "%02d" (nth 4 indices))
2064 (let ((len (length str))
2065 (trans (aref (cdr quail-current-translations) idx)))
2067 (setq trans (string trans)))
2068 (setq str (format "%s %d.%s"
2070 (if (= (- idx start) 9) 0
2074 (put-text-property (+ len 3) (length str)
2075 'face 'highlight str))
2076 (setq idx (1+ idx))))))
2080 (defvar quail-completion-max-depth 5
2081 "The maximum depth of Quail completion list.")
2083 (defun quail-completion ()
2084 "List all completions for the current key.
2085 All possible translations of the current key and whole possible longer keys
2086 are shown (at most to the depth specified `quail-completion-max-depth')."
2088 (quail-setup-completion-buf)
2089 (let ((win (get-buffer-window quail-completion-buf 'visible))
2090 (key quail-current-key)
2091 (map (quail-lookup-key quail-current-key))
2092 (require-update nil))
2093 (with-current-buffer quail-completion-buf
2095 (equal key quail-current-key)
2096 (eq last-command 'quail-completion))
2097 ;; The window for Quail completion buffer has already been
2098 ;; shown. We just scroll it appropriately.
2099 (if (pos-visible-in-window-p (point-max) win)
2100 (set-window-start win (point-min))
2101 (let ((other-window-scroll-buffer quail-completion-buf)
2102 ;; This nil binding is necessary to surely scroll
2103 ;; quail-completion-buf.
2104 (minibuffer-scroll-window nil))
2105 (scroll-other-window)))
2106 (setq quail-current-key key)
2108 (insert "Possible completion and corresponding translations are:\n")
2109 (quail-completion-1 key map 1)
2110 (goto-char (point-min))
2111 (display-buffer (current-buffer))
2112 (setq require-update t)))
2114 (quail-update-guidance)))
2115 (setq this-command 'quail-completion))
2117 (defun quail-completion-1 (key map indent)
2118 "List all completions of KEY in MAP with indentation INDENT."
2119 (let ((len (length key)))
2122 (if (and (symbolp map) (fboundp map))
2123 (setq map (funcall map key len)))
2125 (quail-completion-list-translations map key (+ indent len 1))
2127 (setq indent (+ indent 2))
2128 (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
2130 (newkey (make-string (1+ len) 0))
2133 (setq l (funcall l)))
2134 ;; Set KEY in the first LEN characters of NEWKEY.
2136 (aset newkey i (aref key i))
2138 (setq l (reverse l))
2139 (while l ; L = ((CHAR . DEFN) ....) ;
2140 (aset newkey len (car (car l)))
2141 (quail-completion-1 newkey (cdr (car l)) indent)
2142 (setq l (cdr l)))))))
2144 (defun quail-completion-list-translations (map key indent)
2145 "List all possible translations of KEY in Quail MAP with indentation INDENT."
2146 (let (beg (translations
2147 (quail-get-translation (car map) key (length key))))
2148 (if (integerp translations)
2151 ;; Endow the character `translations' with `mouse-face' text
2152 ;; property to enable `mouse-2' completion.
2154 (insert translations)
2155 (put-text-property beg (point) 'mouse-face 'highlight)
2157 ;; We need only vector part.
2158 (setq translations (cdr translations))
2159 ;; Insert every 10 elements with indices in a line.
2160 (let ((len (length translations))
2164 (when (zerop (% i 10))
2168 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
2169 ;; We show the last digit of FROM while converting
2170 ;; 0,1,..,9 to 1,2,..,0.
2171 (insert (format " %d." (% (1+ i) 10)))
2173 (insert (aref translations i))
2174 ;; Passing the mouse over a character will highlight.
2175 (put-text-property beg (point) 'mouse-face 'highlight)
2179 (defun quail-mouse-choose-completion (event)
2180 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
2182 ;; This function is an exact copy of the mouse.el function
2183 ;; `mouse-choose-completion' except that we:
2184 ;; 1) add two lines from `choose-completion' in simple.el to give
2185 ;; the `mouse-2' click a little more leeway.
2186 ;; 2) don't bury *Quail Completions* buffer, so comment a section, and
2187 ;; 3) delete/terminate the current quail selection here.
2188 ;; Give temporary modes such as isearch a chance to turn off.
2189 (run-hooks 'mouse-leave-buffer-hook)
2190 (let ((buffer (window-buffer))
2193 (with-current-buffer (window-buffer (posn-window (event-start event)))
2194 (if completion-reference-buffer
2195 (setq buffer completion-reference-buffer))
2196 (setq base-size completion-base-size)
2198 (goto-char (posn-point (event-start event)))
2200 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2201 (setq end (point) beg (1+ (point))))
2202 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
2203 (setq end (1- (point)) beg (point)))
2205 (quail-error "No completion here"))
2206 (setq beg (previous-single-property-change beg 'mouse-face))
2207 (setq end (or (next-single-property-change end 'mouse-face)
2209 (setq choice (buffer-substring beg end)))))
2210 ; (let ((owindow (selected-window)))
2211 ; (select-window (posn-window (event-start event)))
2212 ; (if (and (one-window-p t 'selected-frame)
2213 ; (window-dedicated-p (selected-window)))
2214 ; ;; This is a special buffer's frame
2215 ; (iconify-frame (selected-frame))
2216 ; (or (window-dedicated-p (selected-window))
2218 ; (select-window owindow))
2219 (quail-delete-region)
2220 (quail-choose-completion-string choice buffer base-size)
2221 (quail-terminate-translation)))
2223 ;; BASE-SIZE here is for compatibility with an (unused) arg of a
2224 ;; previous implementation.
2225 (defun quail-choose-completion-string (choice &optional buffer base-size)
2226 (setq quail-current-str choice)
2227 (choose-completion-string choice buffer))
2229 (defun quail-build-decode-map (map-list key decode-map num
2230 &optional maxnum ignores)
2231 "Build a decoding map.
2232 Accumulate in the cdr part of DECODE-MAP all pairs of key sequences
2233 vs the corresponding translations defined in the Quail map
2234 specified by the first element MAP-LIST. Each pair has the form
2235 \(KEYSEQ . TRANSLATION). DECODE-MAP should have the form
2236 \(decode-map . ALIST), where ALIST is an alist of length NUM. KEY
2237 is a key sequence to reach MAP.
2238 Optional 5th arg MAXNUM limits the number of accumulated pairs.
2239 Optional 6th arg IGNORES is a list of translations to ignore."
2240 (let* ((map (car map-list))
2241 (translation (quail-get-translation (car map) key (length key)))
2243 (cond ((integerp translation)
2244 ;; Accept only non-ASCII chars not listed in IGNORES.
2245 (when (and (> translation 255) (not (memq translation ignores)))
2247 (cons (cons key translation) (cdr decode-map)))
2248 (setq num (1+ num))))
2249 ((consp translation)
2250 (setq translation (cdr translation))
2251 (let ((multibyte nil))
2252 (mapc (function (lambda (x)
2253 ;; Accept only non-ASCII chars not
2254 ;; listed in IGNORES.
2255 (if (and (if (integerp x) (> x 255)
2256 (> (string-bytes x) (length x)))
2257 (not (member x ignores)))
2258 (setq multibyte t))))
2262 (cons (cons key translation) (cdr decode-map)))
2263 (setq num (+ num (length translation)))))))
2264 (if (and maxnum (> num maxnum))
2266 (setq map (cdr map))
2267 ;; Recursively check the deeper map.
2268 (while (and map (>= num 0))
2269 (setq elt (car map) map (cdr map))
2270 (when (and (integerp (car elt)) (consp (cdr elt))
2271 (not (memq (cdr elt) map-list)))
2272 (setq num (quail-build-decode-map (cons (cdr elt) map-list)
2273 (format "%s%c" key (car elt))
2274 decode-map num maxnum ignores))))
2277 (defun quail-insert-decode-map (decode-map)
2278 "Insert pairs of key sequences vs the corresponding translations.
2279 These are stored in DECODE-MAP using the concise format. DECODE-MAP
2280 should be made by `quail-build-decode-map' (which see)."
2282 (sort (cdr decode-map)
2283 (function (lambda (x y)
2284 (setq x (car x) y (car y))
2285 (or (> (length x) (length y))
2286 (and (= (length x) (length y))
2287 (not (string< x y))))))))
2288 (let ((frame-width (frame-width (window-frame (get-buffer-window
2289 (current-buffer) 'visible))))
2290 (single-key-width 3)
2291 (single-trans-width 4)
2292 (multiple-key-width 3)
2295 elt trans width pos cols rows col row str col-width)
2296 ;; Divide the elements of decoding map into single ones (i.e. the
2297 ;; one that has single translation) and multibyte ones (i.e. the
2298 ;; one that has multiple translations).
2300 (setq elt (car decode-map) decode-map (cdr decode-map)
2302 (if (and (vectorp trans) (= (length trans) 1))
2303 (setq trans (aref trans 0)))
2305 (setq multiple-list (cons elt multiple-list))
2306 (setq single-list (cons (cons (car elt) trans) single-list)
2307 width (if (stringp trans) (string-width trans)
2308 (char-width trans)))
2309 (if (> width single-trans-width)
2310 (setq single-trans-width width)))
2311 (setq width (length (car elt)))
2312 (if (> width single-key-width)
2313 (setq single-key-width width))
2314 (if (> width multiple-key-width)
2315 (setq multiple-key-width width)))
2317 (setq col-width (+ single-key-width 1 single-trans-width 1)
2318 cols (/ frame-width col-width)
2319 rows (/ (length single-list) cols))
2320 (if (> (% (length single-list) cols) 0)
2321 (setq rows (1+ rows)))
2323 (indent-to (1+ single-key-width))
2325 (indent-to (1+ col-width))
2326 (insert "[type a key sequence to insert the corresponding character]\n")
2328 (insert-char ?\n (+ rows 2))
2330 (setq col (- col-width) row 0)
2332 (setq elt (car single-list) single-list (cdr single-list))
2333 (when (= (% row rows) 0)
2335 (setq col (+ col col-width))
2336 (move-to-column col t)
2337 (insert-char ?- single-key-width)
2339 (insert-char ?- single-trans-width)
2341 (move-to-column col t)
2343 (indent-to (+ col single-key-width 1))
2346 (setq row (1+ row)))
2347 (goto-char (point-max)))
2351 (indent-to (1+ multiple-key-width))
2352 (insert "character(s) [type a key (sequence) and select one from the list]\n")
2353 (insert-char ?- multiple-key-width)
2354 (insert " ------------\n")
2355 (while multiple-list
2356 (setq elt (car multiple-list) multiple-list (cdr multiple-list))
2358 (indent-to multiple-key-width)
2359 (if (vectorp (cdr elt))
2362 (let ((width (if (integerp x) (char-width x)
2364 (when (> (+ (current-column) 1 width) frame-width)
2366 (indent-to multiple-key-width))
2369 (insert " " (cdr elt)))
2373 (define-button-type 'quail-keyboard-layout-button
2374 :supertype 'help-xref
2375 'help-function '(lambda (layout)
2376 (help-setup-xref `(quail-keyboard-layout-button ,layout) nil)
2377 (quail-show-keyboard-layout layout))
2378 'help-echo (purecopy "mouse-2, RET: show keyboard layout"))
2380 (define-button-type 'quail-keyboard-customize-button
2381 :supertype 'help-customize-variable
2382 'help-echo (purecopy "mouse-2, RET: customize keyboard layout"))
2384 (defun quail-help (&optional package)
2385 "Show brief description of the current Quail package.
2386 Optional arg PACKAGE specifies the name of alternative Quail
2387 package to describe."
2389 (let ((help-xref-mule-regexp help-xref-mule-regexp-template)
2390 (default-enable-multibyte-characters enable-multibyte-characters)
2393 (assoc package quail-package-alist)
2394 quail-current-package)))
2395 ;; At first, make sure that the help buffer has window.
2396 (let ((temp-buffer-show-hook nil))
2397 (with-output-to-temp-buffer (help-buffer)
2398 (with-current-buffer standard-output
2399 (setq quail-current-package package-def))))
2400 ;; Then, insert text in the help buffer while paying attention to
2401 ;; the width of the frame in which the buffer displayed.
2402 (with-current-buffer (help-buffer)
2403 (setq buffer-read-only nil)
2404 (insert "Input method: " (quail-name)
2405 " (mode line indicator:"
2409 (narrow-to-region (point) (point))
2410 (insert (quail-docstring))
2411 (goto-char (point-min))
2412 (with-syntax-table emacs-lisp-mode-syntax-table
2413 (while (re-search-forward "\\\\<\\sw\\(\\sw\\|\\s_\\)+>" nil t)
2414 (let ((sym (intern-soft
2415 (buffer-substring (+ (match-beginning 0) 2)
2417 (if (and (boundp sym)
2418 (stringp (symbol-value sym)))
2419 (replace-match (symbol-value sym) t t)))))
2420 (goto-char (point-max)))
2425 (let ((done-list nil))
2426 ;; Show keyboard layout if the current package requests it..
2427 (when (quail-show-layout)
2431 This input method works by translating individual input characters.
2432 Assuming that your actual keyboard has the `")
2433 (help-insert-xref-button
2434 quail-keyboard-layout-type
2435 'quail-keyboard-layout-button
2436 quail-keyboard-layout-type)
2438 translation results in the following \"virtual\" keyboard layout:
2441 (quail-insert-kbd-layout quail-keyboard-layout))
2442 (insert "If your keyboard has a different layout, rearranged from
2444 (help-insert-xref-button
2446 'quail-keyboard-layout-button "standard")
2447 (insert "', the \"virtual\" keyboard you get with this input method
2448 will be rearranged in the same way.
2450 You can set the variable `quail-keyboard-layout-type' to specify
2451 the physical layout of your keyboard; the tables shown in
2452 documentation of input methods including this one are based on the
2453 physical keyboard layout as specified with that variable.
2455 (help-insert-xref-button
2456 "[customize keyboard layout]"
2457 'quail-keyboard-customize-button 'quail-keyboard-layout-type)
2460 ;; Show key sequences.
2461 (let ((decode-map (list 'decode-map))
2463 (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
2470 (if (quail-show-layout)
2471 (insert "You can also input more characters")
2472 (insert "You can input characters"))
2473 (insert " by the following key sequences:\n")
2474 (quail-insert-decode-map decode-map))))
2476 (quail-help-insert-keymap-description
2477 (quail-translation-keymap)
2479 KEY BINDINGS FOR TRANSLATION
2480 ----------------------------\n")
2482 (if (quail-conversion-keymap)
2483 (quail-help-insert-keymap-description
2484 (quail-conversion-keymap)
2486 KEY BINDINGS FOR CONVERSION
2487 ---------------------------\n"))
2488 (setq quail-current-package nil)
2489 ;; Resize the help window again, now that it has all its contents.
2490 (save-selected-window
2491 (select-window (get-buffer-window (current-buffer) t))
2492 (run-hooks 'temp-buffer-show-hook)))))
2494 (defun quail-help-insert-keymap-description (keymap &optional header)
2495 (let (pos1 pos2 eol)
2500 (insert (substitute-command-keys "\\{keymap}")))
2501 ;; Skip headers "key bindings", etc.
2504 (with-syntax-table emacs-lisp-mode-syntax-table
2505 (while (re-search-forward "\\sw\\(\\sw\\|\\s_\\)+" nil t)
2506 (let ((sym (intern-soft (buffer-substring (match-beginning 0)
2508 (if (and sym (fboundp sym)
2509 (or (eq (get sym 'quail-help) 'hide)
2510 (and (quail-deterministic)
2511 (eq (get sym 'quail-help) 'non-deterministic))))
2512 (delete-region (line-beginning-position)
2513 (1+ (line-end-position)))))))
2516 (if (looking-at "[ \t]*$")
2517 (delete-region (point) (1+ (line-end-position)))
2521 (delete-region pos1 (point)))
2522 (goto-char (point-max))))
2524 (defun quail-translation-help ()
2525 "Show help message while translating in Quail input method."
2527 (if (not (eq this-command last-command))
2528 (let (state-msg keymap)
2529 (if (and quail-converting (= (length quail-current-key) 0))
2531 (format "Converting string %S by input method %S.\n"
2532 quail-conversion-str (quail-name))
2533 keymap (quail-conversion-keymap))
2535 (format "Translating key sequence %S by input method %S.\n"
2536 quail-current-key (quail-name))
2537 keymap (quail-translation-keymap)))
2538 (with-output-to-temp-buffer "*Help*"
2539 (with-current-buffer standard-output
2541 (quail-help-insert-keymap-description
2543 "-----------------------\n")
2544 ;; Isn't this redundant ? -stef
2547 (save-selected-window
2548 (select-window (get-buffer-window "*Help*"))
2549 (if (eq this-command last-command)
2550 (if (< (window-end) (point-max))
2552 (if (> (window-start) (point-min))
2553 (set-window-start (selected-window) (point-min)))))
2555 (if (< (window-end (selected-window) 'up-to-date) (point-max))
2556 "Type \\[quail-translation-help] to scroll up the help"
2557 (if (> (window-start) (point-min))
2558 "Type \\[quail-translation-help] to see the head of help"))))
2561 (message "%s" (substitute-command-keys scroll-help))
2564 (quail-update-guidance)
2567 ;; Quail map generator from state transition table.
2569 (defun quail-map-from-table (table)
2570 "Make quail map from state transition table TABLE.
2572 TABLE is an alist, the form is:
2573 ((STATE-0 TRANSITION-0-1 TRANSITION-0-2 ...) (STATE-1 ...) ...)
2575 STATE-n are symbols to denote state. STATE-0 is the initial state.
2577 TRANSITION-n-m are transition rules from STATE-n, and have the form
2578 \(RULES . STATE-x) or RULES, where STATE-x is one of STATE-n above,
2579 RULES is a symbol whose value is an alist of keys \(string) vs the
2580 correponding characters or strings. The format of the symbol value of
2581 RULES is the same as arguments to `quail-define-rules'.
2583 If TRANSITION-n-m has the form (RULES . STATE-x), it means that
2584 STATE-n transits to STATE-x when keys in RULES are input. Recursive
2585 transition is allowed, i.e. STATE-x may be STATE-n.
2587 If TRANSITION-n-m has the form RULES, the transition terminates
2588 when keys in RULES are input.
2590 The generated map can be set for the current Quail package by the
2591 function `quail-install-map' (which see)."
2592 (let ((state-alist (mapcar (lambda (x) (list (car x))) table))
2594 ;; STATE-ALIST is an alist of states vs the correponding sub Quail
2595 ;; map. It is now initialized to ((STATE-0) (STATE-1) ...).
2596 ;; Set key sequence mapping rules in cdr part of each element.
2598 (quail-map-from-table-1 state-alist (car table))
2599 (setq table (cdr table)))
2601 ;; Now STATE-ALIST has the form ((STATE-0 MAPPING-RULES) ...).
2602 ;; Elements of MAPPING-RULES may have the form (STATE-x). Replace
2603 ;; them with MAPPING-RULES of STATE-x to make elements of
2604 ;; STATE-ALIST valid Quail maps.
2605 (setq tail state-alist)
2607 (setq elt (car tail) tail (cdr tail))
2608 (quail-map-from-table-2 state-alist elt))
2610 ;; Return the Quail map for the initial state.
2613 ;; STATE-INFO has the form (STATE TRANSITION ...). Set key sequence
2614 ;; mapping rules in the element of STATE-ALIST that corresponds to
2615 ;; STATE according to TRANSITION ...
2616 (defun quail-map-from-table-1 (state-alist state-info)
2617 (let* ((state (car state-info))
2618 (map (assq state state-alist))
2619 (transitions (cdr state-info))
2622 (setq elt (car transitions) transitions (cdr transitions))
2623 (let (rules dst-state key trans)
2624 ;; ELT has the form (RULES-SYMBOL . STATE-x) or RULES-SYMBOL.
2625 ;; STATE-x is one of car parts of STATE-ALIST's elements.
2627 (setq rules (symbol-value (car elt))
2628 ;; Set (STATE-x) as branches for all keys in RULES.
2629 ;; It is replaced with actual branches for STATE-x
2630 ;; later in `quail-map-from-table-2'.
2631 dst-state (list (cdr elt)))
2632 (setq rules (symbol-value elt)))
2634 (setq key (car (car rules)) trans (cdr (car rules))
2637 (if (= (length trans) 1)
2638 (setq trans (aref trans 0))
2639 (setq trans (string-to-vector trans))))
2640 (set-nested-alist key trans map nil dst-state))))))
2642 ;; ELEMENT is one element of STATE-ALIST. ELEMENT is a nested alist;
2644 ;; (STATE (CHAR NESTED-ALIST) ...)
2645 ;; NESTED-ALIST is a nested alist; the form is:
2646 ;; (TRANS (CHAR NESTED-ALIST) ...)
2648 ;; (TRANS (CHAR NESTED-ALIST) ... . (STATE-x))
2649 ;; Here, the task is to replace all occurrences of (STATE-x) with:
2650 ;; (cdr (assq STATE-x STATE-ALIST))
2652 (defun quail-map-from-table-2 (state-alist element)
2653 (let ((prev element)
2654 (tail (cdr element))
2657 (setq elt (car tail) prev tail tail (cdr tail))
2658 (quail-map-from-table-2 state-alist (cdr elt)))
2659 (setq elt (car tail))
2661 (quail-map-from-table-2 state-alist (cdr elt))
2662 (setcdr prev (cdr (assq elt state-alist))))))
2664 ;; Concatenate translations for all heading substrings of KEY in the
2665 ;; current Quail map. Here, `heading substring' means (substring KEY
2666 ;; 0 LEN), where LEN is 1, 2, ... (length KEY).
2667 (defun quail-lookup-map-and-concat (key)
2668 (let* ((len (length key))
2669 (translation-list nil)
2672 (setq map (quail-lookup-key key len)
2675 (let* ((def (quail-map-definition map))
2676 (trans (if (consp def) (aref (cdr def) (car (car def)))
2678 (if (integerp trans)
2679 (setq trans (char-to-string trans)))
2680 (setq translation-list (cons trans translation-list)))))
2681 (apply 'concat translation-list)))
2684 (defvar quail-directory-name "quail"
2685 "Name of Quail directory which contains Quail packages.
2686 This is a sub-directory of LEIM directory.")
2689 (defun quail-update-leim-list-file (dirname &rest dirnames)
2690 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
2691 DIRNAME is a directory containing Emacs input methods;
2692 normally, it should specify the `leim' subdirectory
2693 of the Emacs source tree.
2695 It searches for Quail packages under `quail' subdirectory of DIRNAME,
2696 and update the file \"leim-list.el\" in DIRNAME.
2698 When called from a program, the remaining arguments are additional
2699 directory names to search for Quail packages under `quail' subdirectory
2701 (interactive "FDirectory of LEIM: ")
2702 (setq dirname (expand-file-name dirname))
2703 (let ((leim-list (expand-file-name leim-list-file-name dirname))
2704 quail-dirs list-buf pkg-list pkg-buf pos)
2705 (if (not (file-writable-p leim-list))
2706 (error "Can't write to file \"%s\"" leim-list))
2707 (message "Updating %s ..." leim-list)
2708 (setq list-buf (find-file-noselect leim-list))
2710 ;; At first, clean up the file.
2711 (with-current-buffer list-buf
2714 ;; Insert the correct header.
2715 (if (looking-at (regexp-quote leim-list-header))
2716 (goto-char (match-end 0))
2717 (insert leim-list-header))
2719 (if (not (re-search-forward leim-list-entry-regexp nil t))
2722 ;; Remove garbages after the header.
2723 (goto-char (match-beginning 0))
2725 (delete-region pos (point)))
2727 ;; Remove all entries for Quail.
2728 (while (re-search-forward leim-list-entry-regexp nil 'move)
2729 (goto-char (match-beginning 0))
2732 (let ((form (read list-buf)))
2733 (when (equal (nth 3 form) ''quail-use-package)
2734 (if (eolp) (forward-line 1))
2735 (delete-region pos (point))))
2737 ;; Delete the remaining contents because it seems that
2738 ;; this file is broken.
2739 (message "Garbage in %s deleted" leim-list)
2740 (delete-region pos (point-max)))))))
2742 ;; Search for `quail' subdirectory under each DIRNAMES.
2743 (setq dirnames (cons dirname dirnames))
2746 (setcar l (expand-file-name (car l)))
2747 (setq dirname (expand-file-name quail-directory-name (car l)))
2748 (if (file-readable-p dirname)
2749 (setq quail-dirs (cons dirname quail-dirs))
2750 (message "%s doesn't have `%s' subdirectory, just ignored"
2751 (car l) quail-directory-name)
2752 (setq quail-dirs (cons nil quail-dirs)))
2754 (setq quail-dirs (nreverse quail-dirs)))
2756 ;; Insert input method registering forms.
2758 (setq dirname (car quail-dirs))
2760 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2762 (message "Checking %s ..." (car pkg-list))
2764 (insert-file-contents (car pkg-list))
2765 (goto-char (point-min))
2766 ;; Don't get fooled by commented-out code.
2767 (while (re-search-forward "^[ \t]*(quail-define-package" nil t)
2768 (goto-char (match-beginning 0))
2770 (let ((form (read (current-buffer))))
2771 (with-current-buffer list-buf
2773 (format "(register-input-method
2777 (nth 1 form) ; PACKAGE-NAME
2778 (nth 2 form) ; LANGUAGE
2779 'quail-use-package ; ACTIVATE-FUNC
2780 (nth 3 form) ; PACKAGE-TITLE
2781 (progn ; PACKAGE-DESCRIPTION (one line)
2782 (string-match ".*" (nth 5 form))
2783 (match-string 0 (nth 5 form)))
2784 (file-relative-name ; PACKAGE-FILENAME
2785 (file-name-sans-extension (car pkg-list))
2788 ;; Ignore the remaining contents of this file.
2789 (goto-char (point-max))
2790 (message "Some part of \"%s\" is broken" (car pkg-list))))))
2791 (setq pkg-list (cdr pkg-list)))
2792 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2794 ;; At last, write out LEIM list file.
2795 (with-current-buffer list-buf
2796 (setq buffer-file-coding-system 'iso-2022-7bit)
2798 (kill-buffer list-buf)
2799 (message "Updating %s ... done" leim-list)))
2801 (defun quail-advice (args)
2802 "Advise users about the characters input by the current Quail package.
2803 The argument is a parameterized event of the form:
2804 (quail-advice STRING)
2805 where STRING is a string containing the input characters.
2806 If STRING has property `advice' and the value is a function,
2807 call it with one argument STRING."
2809 (let* ((string (nth 1 args))
2810 (func (get-text-property 0 'advice string)))
2811 (if (functionp func)
2812 (funcall func string))))
2814 (global-set-key [quail-advice] 'quail-advice)
2819 ;;; quail.el ends here