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