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