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