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