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