]> code.delx.au - gnu-emacs/blob - lisp/international/quail.el
Doc fixes.
[gnu-emacs] / 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 (require 'faces)
47
48 ;; Buffer local variables
49
50 (defvar quail-current-package nil
51 "The current Quail package, which depends on the current input method.
52 See the documentation of `quail-package-alist' for the format.")
53 (make-variable-buffer-local 'quail-current-package)
54 (put 'quail-current-package 'permanent-local t)
55
56 ;; Quail uses the following two buffers to assist users.
57 ;; A buffer to show available key sequence or translation list.
58 (defvar quail-guidance-buf nil)
59 ;; A buffer to show completion list of the current key sequence.
60 (defvar quail-completion-buf nil)
61
62 ;; Each buffer in which Quail is activated should use different
63 ;; guidance buffers.
64 (make-variable-buffer-local 'quail-guidance-buf)
65 (put 'quail-guidance-buf 'permanent-local t)
66
67 ;; A main window showing Quail guidance buffer.
68 (defvar quail-guidance-win nil)
69 (make-variable-buffer-local 'quail-guidance-win)
70
71 (defvar quail-overlay nil
72 "Overlay which covers the current translation region of Quail.")
73 (make-variable-buffer-local 'quail-overlay)
74
75 (defvar quail-conv-overlay nil
76 "Overlay which covers the text to be converted in Quail mode.")
77 (make-variable-buffer-local 'quail-conv-overlay)
78
79 (defvar quail-current-key nil
80 "Current key for translation in Quail mode.")
81 (make-variable-buffer-local 'quail-current-key)
82
83 (defvar quail-current-str nil
84 "Currently selected translation of the current key.")
85 (make-variable-buffer-local 'quail-current-str)
86
87 (defvar quail-current-translations nil
88 "Cons of indices and vector of possible translations of the current key.
89 Indices is a list of (CURRENT START END BLOCK BLOCKS), where
90 CURRENT is an index of the current translation,
91 START and END are indices of the start and end of the current block,
92 BLOCK is the current block index,
93 BLOCKS is a number of blocks of translation.")
94 (make-variable-buffer-local 'quail-current-translations)
95
96 (defvar quail-current-data nil
97 "Any Lisp object holding information of current translation status.
98 When a key sequence is mapped to TRANS and TRANS is a cons
99 of actual translation and some Lisp object to be referred
100 for translating the longer key sequence, this variable is set
101 to that Lisp object.")
102 (make-variable-buffer-local 'quail-current-data)
103
104 ;; Quail package handlers.
105
106 (defvar quail-package-alist nil
107 "List of Quail packages.
108 A Quail package is a list of these elements:
109 NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
110 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
111 DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
112 CONVERSION-KEYS, SIMPLE.
113
114 QUAIL-MAP is a data structure to map key strings to translations. For
115 the format, see the documentation of `quail-map-p'.
116
117 DECODE-MAP is an alist of translations and corresponding keys.
118
119 See the documentation of `quail-define-package' for the other elements.")
120
121 ;; Return various slots in the current quail-package.
122
123 (defsubst quail-name ()
124 "Return the name of the current Quail package."
125 (nth 0 quail-current-package))
126 (defsubst quail-title ()
127 "Return the title of the current Quail package."
128 (nth 1 quail-current-package))
129 (defsubst quail-map ()
130 "Return the translation map of the current Quail package."
131 (nth 2 quail-current-package))
132 (defsubst quail-guidance ()
133 "Return an object used for `guidance' feature of the current Quail package.
134 See also the documentation of `quail-define-package'."
135 (nth 3 quail-current-package))
136 (defsubst quail-docstring ()
137 "Return the documentation string of the current Quail package."
138 (nth 4 quail-current-package))
139 (defsubst quail-translation-keymap ()
140 "Return translation keymap in the current Quail package.
141 Translation keymap is a keymap used while translation region is active."
142 (nth 5 quail-current-package))
143 (defsubst quail-forget-last-selection ()
144 "Return `forget-last-selection' flag of the current Quail package.
145 See also the documentation of `quail-define-package'."
146 (nth 6 quail-current-package))
147 (defsubst quail-deterministic ()
148 "Return `deterministic' flag of the current Quail package.
149 See also the documentation of `quail-define-package'."
150 (nth 7 quail-current-package))
151 (defsubst quail-kbd-translate ()
152 "Return `kbd-translate' flag of the current Quail package.
153 See also the documentation of `quail-define-package'."
154 (nth 8 quail-current-package))
155 (defsubst quail-show-layout ()
156 "Return `show-layout' flag of the current Quail package.
157 See also the documentation of `quail-define-package'."
158 (nth 9 quail-current-package))
159 (defsubst quail-decode-map ()
160 "Return decode map of the current Quail package.
161 It is an alist of translations and corresponding keys."
162 (nth 10 quail-current-package))
163 (defsubst quail-maximum-shortest ()
164 "Return `maximum-shortest' flag of the current Quail package.
165 See also the documentation of `quail-define-package'."
166 (nth 11 quail-current-package))
167 (defsubst quail-overlay-plist ()
168 "Return property list of an overly used in the current Quail package."
169 (nth 12 quail-current-package))
170 (defsubst quail-update-translation-function ()
171 "Return a function for updating translation in the current Quail package."
172 (nth 13 quail-current-package))
173 (defsubst quail-conversion-keymap ()
174 "Return conversion keymap in the current Quail package.
175 Conversion keymap is a keymap used while conversion region is active
176 but translation region is not active."
177 (nth 14 quail-current-package))
178 (defsubst quail-simple ()
179 "Return t if the current Quail package is simple."
180 (nth 15 quail-current-package))
181
182 (defsubst quail-package (name)
183 "Return Quail package named NAME."
184 (assoc name quail-package-alist))
185
186 (defun quail-add-package (package)
187 "Add Quail package PACKAGE to `quail-package-alist'."
188 (let ((pac (quail-package (car package))))
189 (if pac
190 (setcdr pac (cdr package))
191 (setq quail-package-alist (cons package quail-package-alist)))))
192
193 (defun quail-select-package (name)
194 "Select Quail package named NAME as the current Quail package."
195 (let ((package (quail-package name)))
196 (if (null package)
197 (error "No Quail package `%s'" name))
198 (setq quail-current-package package)
199 (setq-default quail-current-package package)
200 name))
201
202 ;;;###autoload
203 (defun quail-use-package (package-name &rest libraries)
204 "Start using Quail package PACKAGE-NAME.
205 The remaining arguments are libraries to be loaded before using the package."
206 (let ((package (quail-package package-name)))
207 (if (null package)
208 ;; Perhaps we have not yet loaded necessary libraries.
209 (while libraries
210 (if (not (load (car libraries) t))
211 (progn
212 (with-output-to-temp-buffer "*Help*"
213 (princ "Quail package \"")
214 (princ package-name)
215 (princ "\" can't be activated\n because library \"")
216 (princ (car libraries))
217 (princ "\" is not in `load-path'.
218
219 The most common case is that you have not yet installed appropriate
220 libraries in LEIM (Libraries of Emacs Input Method) which is
221 distributed separately from Emacs.
222
223 LEIM is available from the same ftp directory as Emacs."))
224 (error "Can't use the Quail package `%s'" package-name))
225 (setq libraries (cdr libraries))))))
226 (quail-select-package package-name)
227 (setq current-input-method-title (quail-title))
228 (quail-activate))
229
230 (defvar quail-translation-keymap
231 (let ((map (make-keymap))
232 (i 0))
233 (while (< i ?\ )
234 (define-key map (char-to-string i) 'quail-other-command)
235 (setq i (1+ i)))
236 (while (< i 127)
237 (define-key map (char-to-string i) 'quail-self-insert-command)
238 (setq i (1+ i)))
239 (setq i 128)
240 (while (< i 256)
241 (define-key map (vector i) 'quail-self-insert-command)
242 (setq i (1+ i)))
243 (define-key map "\177" 'quail-delete-last-char)
244 (define-key map "\C-f" 'quail-next-translation)
245 (define-key map "\C-b" 'quail-prev-translation)
246 (define-key map "\C-n" 'quail-next-translation-block)
247 (define-key map "\C-p" 'quail-prev-translation-block)
248 (define-key map [right] 'quail-next-translation)
249 (define-key map [left] 'quail-prev-translation)
250 (define-key map [down] 'quail-next-translation-block)
251 (define-key map [up] 'quail-prev-translation-block)
252 (define-key map "\C-i" 'quail-completion)
253 (define-key map "\C-@" 'quail-select-current)
254 ;; Following simple.el, Enter key on numeric keypad selects the
255 ;; current translation just like `C-SPC', and `mouse-2' chooses
256 ;; any completion visible in the *Quail Completions* buffer.
257 (define-key map [kp-enter] 'quail-select-current)
258 (define-key map [mouse-2] 'quail-mouse-choose-completion)
259 (define-key map [down-mouse-2] nil)
260 (define-key map "\C-h" 'quail-translation-help)
261 (define-key map [?\C- ] 'quail-select-current)
262 (define-key map [tab] 'quail-completion)
263 (define-key map [delete] 'quail-delete-last-char)
264 (define-key map [backspace] 'quail-delete-last-char)
265 map)
266 "Keymap used processing translation in complex Quail modes.
267 Only a few especially complex input methods use this map;
268 most use `quail-simple-translation-keymap' instead.
269 This map is activated while translation region is active.")
270
271 (defvar quail-simple-translation-keymap
272 (let ((map (make-keymap))
273 (i 0))
274 (while (< i ?\ )
275 (define-key map (char-to-string i) 'quail-other-command)
276 (setq i (1+ i)))
277 (while (< i 127)
278 (define-key map (char-to-string i) 'quail-self-insert-command)
279 (setq i (1+ i)))
280 (define-key map "\177" 'quail-delete-last-char)
281 (define-key map [delete] 'quail-delete-last-char)
282 (define-key map [backspace] 'quail-delete-last-char)
283 ;;(let ((meta-map (make-sparse-keymap)))
284 ;;(define-key map (char-to-string meta-prefix-char) meta-map)
285 ;;(define-key map [escape] meta-map))
286 map)
287 "Keymap used while processing translation in simple Quail modes.
288 A few especially complex input methods use `quail-translation-keymap' instead.
289 This map is activated while translation region is active.")
290
291 (defvar quail-conversion-keymap
292 (let ((map (make-keymap))
293 (i ?\ ))
294 (while (< i 127)
295 (define-key map (char-to-string i) 'quail-self-insert-command)
296 (setq i (1+ i)))
297 (setq i 128)
298 (while (< i 256)
299 (define-key map (vector i) 'quail-self-insert-command)
300 (setq i (1+ i)))
301 (define-key map "\C-b" 'quail-conversion-backward-char)
302 (define-key map "\C-f" 'quail-conversion-forward-char)
303 (define-key map "\C-a" 'quail-conversion-beginning-of-region)
304 (define-key map "\C-e" 'quail-conversion-end-of-region)
305 (define-key map "\C-d" 'quail-conversion-delete-char)
306 (define-key map "\C-k" 'quail-conversion-delete-tail)
307 (define-key map "\C-h" 'quail-translation-help)
308 (define-key map "\177" 'quail-conversion-backward-delete-char)
309 (define-key map [delete] 'quail-conversion-backward-delete-char)
310 (define-key map [backspace] 'quail-conversion-backward-delete-char)
311 map)
312 "Keymap used for processing conversion in Quail mode.
313 This map is activated while conversion region is active but translation
314 region is not active.")
315
316 ;;;###autoload
317 (defun quail-define-package (name language title
318 &optional guidance docstring translation-keys
319 forget-last-selection deterministic
320 kbd-translate show-layout create-decode-map
321 maximum-shortest overlay-plist
322 update-translation-function
323 conversion-keys simple)
324 "Define NAME as a new Quail package for input LANGUAGE.
325 TITLE is a string to be displayed at mode-line to indicate this package.
326 Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
327 FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
328 CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
329 UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
330
331 GUIDANCE specifies how a guidance string is shown in echo area.
332 If it is t, list of all possible translations for the current key is shown
333 with the currently selected translation being highlighted.
334 If it is an alist, the element has the form (CHAR . STRING). Each character
335 in the current key is searched in the list and the corresponding string is
336 shown.
337 If it is nil, the current key is shown.
338
339 DOCSTRING is the documentation string of this package.
340
341 TRANSLATION-KEYS specifies additional key bindings used while translation
342 region is active. It is an alist of single key character vs. corresponding
343 command to be called.
344
345 FORGET-LAST-SELECTION non-nil means a selected translation is not kept
346 for the future to translate the same key. If this flag is nil, a
347 translation selected for a key is remembered so that it can be the
348 first candidate when the same key is entered later.
349
350 DETERMINISTIC non-nil means the first candidate of translation is
351 selected automatically without allowing users to select another
352 translation for a key. In this case, unselected translations are of
353 no use for an interactive use of Quail but can be used by some other
354 programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
355 to t.
356
357 KBD-TRANSLATE non-nil means input characters are translated from a
358 user's keyboard layout to the standard keyboard layout. See the
359 documentation of `quail-keyboard-layout' and
360 `quail-keyboard-layout-standard' for more detail.
361
362 SHOW-LAYOUT non-nil means the `quail-help' command should show
363 the user's keyboard layout visually with translated characters.
364 If KBD-TRANSLATE is set, it is desirable to set also this flag unless
365 this package defines no translations for single character keys.
366
367 CREATE-DECODE-MAP non-nil means decode map is also created. A decode
368 map is an alist of translations and corresponding original keys.
369 Although this map is not used by Quail itself, it can be used by some
370 other programs. For instance, Vietnamese supporting needs this map to
371 convert Vietnamese text to VIQR format which uses only ASCII
372 characters to represent Vietnamese characters.
373
374 MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
375 length of the shortest sequence. When we don't have a translation of
376 key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
377 the key at \"..AB\" and start translation of \"CD..\". Hangul
378 packages, for instance, use this facility. If this flag is nil, we
379 break the key just at \"..ABC\" and start translation of \"D..\".
380
381 OVERLAY-PLIST if non-nil is a property list put on an overlay which
382 covers Quail translation region.
383
384 UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
385 the current translation region according to a new translation data. By
386 default, a translated text or a user's key sequence (if no translation
387 for it) is inserted.
388
389 CONVERSION-KEYS specifies additional key bindings used while
390 conversion region is active. It is an alist of single key character
391 vs. corresponding command to be called.
392
393 If SIMPLE is non-nil, then we do not alter the meanings of
394 commands such as C-f, C-b, C-n, C-p and TAB; they are treated as
395 non-Quail commands."
396 (let (translation-keymap conversion-keymap)
397 (if deterministic (setq forget-last-selection t))
398 (if translation-keys
399 (progn
400 (setq translation-keymap (copy-keymap
401 (if simple quail-simple-translation-keymap
402 quail-translation-keymap)))
403 (while translation-keys
404 (define-key translation-keymap
405 (car (car translation-keys)) (cdr (car translation-keys)))
406 (setq translation-keys (cdr translation-keys))))
407 (setq translation-keymap
408 (if simple quail-simple-translation-keymap
409 quail-translation-keymap)))
410 (when conversion-keys
411 (setq conversion-keymap (copy-keymap quail-conversion-keymap))
412 (while conversion-keys
413 (define-key conversion-keymap
414 (car (car conversion-keys)) (cdr (car conversion-keys)))
415 (setq conversion-keys (cdr conversion-keys))))
416 (quail-add-package
417 (list name title (list nil) guidance (or docstring "")
418 translation-keymap
419 forget-last-selection deterministic kbd-translate show-layout
420 (if create-decode-map (list 'decode-map) nil)
421 maximum-shortest overlay-plist update-translation-function
422 conversion-keymap simple))
423
424 ;; Update input-method-alist.
425 (let ((slot (assoc name input-method-alist))
426 (val (list language 'quail-use-package title docstring)))
427 (if slot (setcdr slot val)
428 (setq input-method-alist (cons (cons name val) input-method-alist)))))
429
430 (quail-select-package name))
431
432 ;; Quail minor mode handlers.
433
434 ;; Setup overlays used in Quail mode.
435 (defun quail-setup-overlays (conversion-mode)
436 (let ((pos (point)))
437 (if (overlayp quail-overlay)
438 (move-overlay quail-overlay pos pos)
439 (setq quail-overlay (make-overlay pos pos nil nil t))
440 (if input-method-highlight-flag
441 (overlay-put quail-overlay 'face 'underline))
442 (let ((l (quail-overlay-plist)))
443 (while l
444 (overlay-put quail-overlay (car l) (car (cdr l)))
445 (setq l (cdr (cdr l))))))
446 (if conversion-mode
447 (if (overlayp quail-conv-overlay)
448 (if (not (overlay-start quail-conv-overlay))
449 (move-overlay quail-conv-overlay pos pos))
450 (setq quail-conv-overlay (make-overlay pos pos nil nil t))
451 (if input-method-highlight-flag
452 (overlay-put quail-conv-overlay 'face 'underline))))))
453
454 ;; Delete overlays used in Quail mode.
455 (defun quail-delete-overlays ()
456 (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
457 (delete-overlay quail-overlay))
458 (if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
459 (delete-overlay quail-conv-overlay)))
460
461 ;; Kill Quail guidance buffer. Set in kill-buffer-hook.
462 (defun quail-kill-guidance-buf ()
463 (if (buffer-live-p quail-guidance-buf)
464 (kill-buffer quail-guidance-buf)))
465
466 (defun quail-inactivate ()
467 "Inactivate Quail input method."
468 (interactive)
469 (quail-activate -1))
470
471 (defun quail-activate (&optional arg)
472 "Activate Quail input method.
473 With arg, activate Quail input method if and only if arg is positive.
474
475 While this input method is active, the variable
476 `input-method-function' is bound to the function `quail-input-method'."
477 (if (and arg
478 (< (prefix-numeric-value arg) 0))
479 ;; Let's inactivate Quail input method.
480 (unwind-protect
481 (progn
482 (quail-hide-guidance-buf)
483 (quail-delete-overlays)
484 (setq describe-current-input-method-function nil)
485 (run-hooks 'quail-inactivate-hook))
486 (kill-local-variable 'input-method-function))
487 ;; Let's active Quail input method.
488 (if (null quail-current-package)
489 ;; Quail package is not yet selected. Select one now.
490 (let (name)
491 (if quail-package-alist
492 (setq name (car (car quail-package-alist)))
493 (error "No Quail package loaded"))
494 (quail-select-package name)))
495 (setq inactivate-current-input-method-function 'quail-inactivate)
496 (setq describe-current-input-method-function 'quail-help)
497 (quail-delete-overlays)
498 (quail-show-guidance-buf)
499 ;; If we are in minibuffer, turn off the current input method
500 ;; before exiting.
501 (if (eq (selected-window) (minibuffer-window))
502 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
503 (make-local-hook 'kill-buffer-hook)
504 (add-hook 'kill-buffer-hook 'quail-kill-guidance-buf nil t)
505 (run-hooks 'quail-activate-hook)
506 (make-local-variable 'input-method-function)
507 (setq input-method-function 'quail-input-method)))
508
509 (defun quail-exit-from-minibuffer ()
510 (inactivate-input-method)
511 (if (<= (minibuffer-depth) 1)
512 (remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
513
514 ;; Keyboard layout translation handlers.
515
516 ;; Some Quail packages provide localized keyboard simulation which
517 ;; requires a particular keyboard layout. In this case, what we need
518 ;; is locations of keys the user entered, not character codes
519 ;; generated by those keys. However, for the moment, there's no
520 ;; common way to get such information. So, we ask a user to give
521 ;; information of his own keyboard layout, then translate it to the
522 ;; standard layout which we defined so that all Quail packages depend
523 ;; just on it.
524
525 (defconst quail-keyboard-layout-standard
526 "\
527 \
528 1!2@3#4$5%6^7&8*9(0)-_=+`~ \
529 qQwWeErRtTyYuUiIoOpP[{]} \
530 aAsSdDfFgGhHjJkKlL;:'\"\\| \
531 zZxXcCvVbBnNmM,<.>/? \
532 "
533 "Standard keyboard layout of printable characters Quail assumes.
534 See the documentation of `quail-keyboard-layout' for this format.
535 This layout is almost the same as that of VT100,
536 but the location of key \\ (backslash) is just right of key ' (single-quote),
537 not right of RETURN key.")
538
539 (defvar quail-keyboard-layout quail-keyboard-layout-standard
540 "A string which represents physical key layout of a particular keyboard.
541 We assume there are six rows and each row has 15 keys (columns),
542 the first row is above the `1' - `0' row,
543 the first column of the second row is left of key `1',
544 the first column of the third row is left of key `q',
545 the first column of the fourth row is left of key `a',
546 the first column of the fifth row is left of key `z',
547 the sixth row is below the `z' - `/' row.
548 Nth (N is even) and (N+1)th characters in the string are non-shifted
549 and shifted characters respectively at the same location.
550 The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
551 The command `quail-set-keyboard-layout' usually sets this variable.")
552
553 (defconst quail-keyboard-layout-len 180)
554
555 ;; Here we provide several examples of famous keyboard layouts.
556
557 (defvar quail-keyboard-layout-alist
558 (list
559 '("sun-type3" . "\
560 \
561 1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
562 qQwWeErRtTyYuUiIoOpP[{]} \
563 aAsSdDfFgGhHjJkKlL;:'\" \
564 zZxXcCvVbBnNmM,<.>/? \
565 ")
566 '("atari-german" . "\
567 \
568 1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
569 qQwWeErRtTzZuUiIoOpP\374\334+* \
570 aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
571 <>yYxXcCvVbBnNmM,;.:-_ \
572 ")
573 (cons "standard" quail-keyboard-layout-standard))
574 "Alist of keyboard names and corresponding layout strings.
575 See the documentation of `quail-keyboard-layout' for the format of
576 the layout string.")
577
578 ;;;###autoload
579 (defun quail-set-keyboard-layout (kbd-type)
580 "Set the current keyboard layout to the same as keyboard KBD-TYPE.
581
582 Since some Quail packages depends on a physical layout of keys (not
583 characters generated by them), those are created by assuming the
584 standard layout defined in `quail-keyboard-layout-standard'. This
585 function tells Quail system the layout of your keyboard so that what
586 you type is correctly handled."
587 (interactive
588 (let* ((completing-ignore-case t)
589 (type (completing-read "Keyboard type: "
590 quail-keyboard-layout-alist)))
591 (list type)))
592 (let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
593 (if (null layout)
594 ;; Here, we had better ask a user to define his own keyboard
595 ;; layout interactively.
596 (error "Unknown keyboard type `%s'" kbd-type))
597 (setq quail-keyboard-layout (cdr layout))))
598
599 (defun quail-keyboard-translate (ch)
600 "Translate CHAR according to `quail-keyboard-layout' and return the result."
601 (if (eq quail-keyboard-layout quail-keyboard-layout-standard)
602 ;; All Quail packages are designed based on
603 ;; `quail-keyboard-layout-standard'.
604 ch
605 (let ((i 0))
606 (while (and (< i quail-keyboard-layout-len)
607 (/= ch (aref quail-keyboard-layout i)))
608 (setq i (1+ i)))
609 (if (= i quail-keyboard-layout-len)
610 ;; CH is not in quail-keyboard-layout, which means that a
611 ;; user typed a key which generated a character code to be
612 ;; handled out of Quail. Just return CH and make
613 ;; quail-execute-non-quail-command handle it correctly.
614 ch
615 (let ((char (aref quail-keyboard-layout-standard i)))
616 (if (= char ?\ )
617 ;; A user typed a key at the location not converted by
618 ;; quail-keyboard-layout-standard. Just return CH as
619 ;; well as above.
620 ch
621 char))))))
622
623 ;; Quail map
624
625 (defsubst quail-map-p (object)
626 "Return t if OBJECT is a Quail map.
627
628 A Quail map holds information how a particular key should be translated.
629 Its format is (TRANSLATION . ALIST).
630 TRANSLATION is either a character, or a cons (INDEX . VECTOR).
631 In the latter case, each element of VECTOR is a candidate for the translation,
632 and INDEX points the currently selected translation.
633
634 ALIST is normally a list of elements that look like (CHAR . DEFN),
635 where DEFN is another Quail map for a longer key (CHAR added to the
636 current key). It may also be a symbol of a function which returns an
637 alist of the above format.
638
639 Just after a Quail package is read, TRANSLATION may be a string or a
640 vector. Then each element of the string or vector is a candidate for
641 the translation. These objects are transformed to cons cells in the
642 format \(INDEX . VECTOR), as described above."
643 (and (consp object)
644 (let ((translation (car object)))
645 (or (integerp translation) (null translation)
646 (vectorp translation) (stringp translation)
647 (symbolp translation)
648 (and (consp translation) (not (vectorp (cdr translation))))))
649 (let ((alist (cdr object)))
650 (or (and (listp alist) (consp (car alist)))
651 (symbolp alist)))))
652
653 ;;;###autoload
654 (defmacro quail-define-rules (&rest rules)
655 "Define translation rules of the current Quail package.
656 Each argument is a list of KEY and TRANSLATION.
657 KEY is a string meaning a sequence of keystrokes to be translated.
658 TRANSLATION is a character, a string, a vector, a Quail map, or a function.
659 It it is a character, it is the sole translation of KEY.
660 If it is a string, each character is a candidate for the translation.
661 If it is a vector, each element (string or character) is a candidate
662 for the translation.
663 In these cases, a key specific Quail map is generated and assigned to KEY.
664
665 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
666 it is used to handle KEY."
667 `(quail-install-map
668 ',(let ((l rules)
669 (map (list nil)))
670 (while l
671 (quail-defrule-internal (car (car l)) (car (cdr (car l))) map t)
672 (setq l (cdr l)))
673 map)))
674
675 ;;;###autoload
676 (defun quail-install-map (map)
677 "Install the Quail map MAP in the current Quail package.
678 The installed map can be referred by the function `quail-map'."
679 (if (null quail-current-package)
680 (error "No current Quail package"))
681 (if (null (quail-map-p map))
682 (error "Invalid Quail map `%s'" map))
683 (setcar (cdr (cdr quail-current-package)) map))
684
685 ;;;###autoload
686 (defun quail-defrule (key translation &optional name)
687 "Add one translation rule, KEY to TRANSLATION, in the current Quail package.
688 KEY is a string meaning a sequence of keystrokes to be translated.
689 TRANSLATION is a character, a string, a vector, a Quail map,
690 a function, or a cons.
691 It it is a character, it is the sole translation of KEY.
692 If it is a string, each character is a candidate for the translation.
693 If it is a vector, each element (string or character) is a candidate
694 for the translation.
695 If it is a cons, the car is one of the above and the cdr is a function
696 to call when translating KEY (the return value is assigned to the
697 variable `quail-current-data'). If the cdr part is not a function,
698 the value itself is assigned to `quail-current-data'.
699 In these cases, a key specific Quail map is generated and assigned to KEY.
700
701 If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
702 it is used to handle KEY.
703 Optional argument NAME, if specified, says which Quail package
704 to define this translation rule in. The default is to define it in the
705 current Quail package."
706 (if name
707 (let ((package (quail-package name)))
708 (if (null package)
709 (error "No Quail package `%s'" name))
710 (setq quail-current-package package)))
711 (quail-defrule-internal key translation (quail-map)))
712
713 ;;;###autoload
714 (defun quail-defrule-internal (key trans map &optional append)
715 "Define KEY as TRANS in a Quail map MAP."
716 (if (null (stringp key))
717 "Invalid Quail key `%s'" key)
718 (if (not (or (numberp trans) (stringp trans) (vectorp trans)
719 (consp trans)
720 (symbolp trans)
721 (quail-map-p trans)))
722 (error "Invalid Quail translation `%s'" trans))
723 (if (null (quail-map-p map))
724 (error "Invalid Quail map `%s'" map))
725 (let ((len (length key))
726 (idx 0)
727 ch entry)
728 ;; Make a map for registering TRANS if necessary.
729 (while (< idx len)
730 (if (null (consp map))
731 ;; We come here, for example, when we try to define a rule
732 ;; for "ABC" but a rule for "AB" is already defined as a
733 ;; symbol.
734 (error "Quail key %s is too long" key))
735 (setq ch (aref key idx)
736 entry (assq ch (cdr map)))
737 (if (null entry)
738 (progn
739 (setq entry (cons ch (list nil)))
740 (setcdr map (cons entry (cdr map)))))
741 (setq map (cdr entry))
742 (setq idx (1+ idx)))
743 (if (symbolp trans)
744 (if (cdr map)
745 ;; We come here, for example, when we try to define a rule
746 ;; for "AB" as a symbol but a rule for "ABC" is already
747 ;; defined.
748 (error "Quail key %s is too short" key)
749 (setcdr entry trans))
750 (if (quail-map-p trans)
751 (if (not (listp (cdr map)))
752 ;; We come here, for example, when we try to define a rule
753 ;; for "AB" as a symbol but a rule for "ABC" is already
754 ;; defined.
755 (error "Quail key %s is too short" key)
756 (if (not (listp (cdr trans)))
757 (if (cdr map)
758 ;; We come here, for example, when we try to
759 ;; define a rule for "AB" as a symbol but a rule
760 ;; for "ABC" is already defined.
761 (error "Quail key %s is too short" key)
762 (setcdr entry trans))
763 (setcdr entry (append trans (cdr map)))))
764 (if (and append (stringp (car map)) (stringp trans))
765 (setcar map (concat (car map) trans))
766 (setcar map trans))))))
767
768 (defun quail-get-translation (def key len)
769 "Return the translation specified as DEF for KEY of length LEN.
770 The translation is either a character or a cons of the form (INDEX . VECTOR),
771 where VECTOR is a vector of candidates (character or string) for
772 the translation, and INDEX points into VECTOR to specify the currently
773 selected translation."
774 (if (and def (symbolp def))
775 ;; DEF is a symbol of a function which returns valid translation.
776 (setq def (funcall def key len)))
777 (if (and (consp def) (not (vectorp (cdr def))))
778 (setq def (car def)))
779
780 (cond
781 ((or (integerp def) (consp def))
782 def)
783
784 ((null def)
785 ;; No translation.
786 nil)
787
788 ((stringp def)
789 ;; Each character in DEF is a candidate of translation. Reform
790 ;; it as (INDICES . VECTOR).
791 (setq def (string-to-vector def))
792 ;; But if the length is 1, we don't need vector but a single
793 ;; candidate as the translation.
794 (if (= (length def) 1)
795 (aref def 0)
796 (cons (list 0 0 0 0 nil) def)))
797
798 ((vectorp def)
799 ;; Each element (string or character) in DEF is a candidate of
800 ;; translation. Reform it as (INDICES . VECTOR).
801 (cons (list 0 0 0 0 nil) def))
802
803 (t
804 (error "Invalid object in Quail map: %s" def))))
805
806 (defun quail-lookup-key (key &optional len)
807 "Lookup KEY of length LEN in the current Quail map and return the definition.
808 The returned value is a Quail map specific to KEY."
809 (or len
810 (setq len (length key)))
811 (let ((idx 0)
812 (map (quail-map))
813 (kbd-translate (quail-kbd-translate))
814 slot ch translation def)
815 (while (and map (< idx len))
816 (setq ch (if kbd-translate (quail-keyboard-translate (aref key idx))
817 (aref key idx)))
818 (setq idx (1+ idx))
819 (if (and (cdr map) (symbolp (cdr map)))
820 (setcdr map (funcall (cdr map) key idx)))
821 (setq slot (assq ch (cdr map)))
822 (if (and (cdr slot) (symbolp (cdr slot)))
823 (setcdr slot (funcall (cdr slot) key idx)))
824 (setq map (cdr slot)))
825 (setq def (car map))
826 (setq quail-current-translations nil)
827 (if (and map (setq translation (quail-get-translation def key len)))
828 (progn
829 (if (and (consp def) (not (vectorp (cdr def))))
830 (progn
831 (if (not (equal (car def) translation))
832 ;; We must reflect TRANSLATION to car part of DEF.
833 (setcar def translation))
834 (setq quail-current-data
835 (if (functionp (cdr def))
836 (funcall (cdr def))
837 (cdr def))))
838 (if (not (equal def translation))
839 ;; We must reflect TRANSLATION to car part of MAP.
840 (setcar map translation)))
841 (if (and (consp translation) (vectorp (cdr translation)))
842 (progn
843 (setq quail-current-translations translation)
844 (if (quail-forget-last-selection)
845 (setcar (car quail-current-translations) 0))))
846 ;; We may have to reform cdr part of MAP.
847 (if (and (cdr map) (symbolp (cdr map)))
848 (progn
849 (setcdr map (funcall (cdr map) key len))))
850 ))
851 map))
852
853 (put 'quail-error 'error-conditions '(quail-error error))
854 (defun quail-error (&rest args)
855 (signal 'quail-error (apply 'format args)))
856
857 (defvar quail-translating nil)
858 (defvar quail-converting nil)
859 (defvar quail-conversion-str nil)
860
861 (defun quail-input-method (key)
862 (if (or buffer-read-only
863 overriding-terminal-local-map
864 overriding-local-map)
865 (list key)
866 (quail-setup-overlays (quail-conversion-keymap))
867 (let ((modified-p (buffer-modified-p))
868 (buffer-undo-list t))
869 (or (and quail-guidance-win
870 (window-live-p quail-guidance-win)
871 (eq (window-buffer quail-guidance-win) quail-guidance-buf)
872 (not input-method-use-echo-area))
873 (quail-show-guidance-buf))
874 (unwind-protect
875 (if (quail-conversion-keymap)
876 (quail-start-conversion key)
877 (quail-start-translation key))
878 (quail-delete-overlays)
879 (if (buffer-live-p quail-guidance-buf)
880 (save-excursion
881 (set-buffer quail-guidance-buf)
882 (erase-buffer)))
883 (if input-method-use-echo-area
884 (quail-hide-guidance-buf))
885 (set-buffer-modified-p modified-p)
886 ;; Run this hook only when the current input method doesn't require
887 ;; conversion. When conversion is required, the conversion function
888 ;; should run this hook at a proper timing.
889 (unless (quail-conversion-keymap)
890 (run-hooks 'input-method-after-insert-chunk-hook))))))
891
892 (defun quail-overlay-region-events (overlay)
893 (let ((start (overlay-start overlay))
894 (end (overlay-end overlay)))
895 (if (< start end)
896 (prog1
897 (string-to-list (buffer-substring start end))
898 (delete-region start end)))))
899
900 (defsubst quail-delete-region ()
901 "Delete the text in the current translation region of Quail."
902 (if (overlay-start quail-overlay)
903 (delete-region (overlay-start quail-overlay)
904 (overlay-end quail-overlay))))
905
906 (defun quail-start-translation (key)
907 "Start translation of the typed character KEY by the current Quail package."
908 ;; Check the possibility of translating KEY.
909 ;; If KEY is nil, we can anyway start translation.
910 (if (or (and (integerp key)
911 (assq (if (quail-kbd-translate)
912 (quail-keyboard-translate key) key)
913 (cdr (quail-map))))
914 (null key))
915 ;; OK, we can start translation.
916 (let* ((echo-keystrokes 0)
917 (help-char nil)
918 (overriding-terminal-local-map (quail-translation-keymap))
919 (generated-events nil)
920 (input-method-function nil))
921 (setq quail-current-key ""
922 quail-current-str ""
923 quail-translating t)
924 (if key
925 (setq unread-command-events (cons key unread-command-events)))
926 (while quail-translating
927 (let* ((keyseq (read-key-sequence
928 (and input-method-use-echo-area
929 (concat input-method-previous-message
930 quail-current-str))))
931 (cmd (lookup-key (quail-translation-keymap) keyseq)))
932 (if (if key
933 (and (commandp cmd) (not (eq cmd 'quail-other-command)))
934 (eq cmd 'quail-self-insert-command))
935 (progn
936 (setq key t)
937 (setq last-command-event (aref keyseq (1- (length keyseq)))
938 last-command this-command
939 this-command cmd)
940 (condition-case err
941 (call-interactively cmd)
942 (quail-error (message "%s" (cdr err)) (beep))))
943 ;; KEYSEQ is not defined in the translation keymap.
944 ;; Let's return the event(s) to the caller.
945 (setq generated-events
946 (string-to-list (this-single-command-raw-keys)))
947 (setq quail-translating nil))))
948 (quail-delete-region)
949 (if (and quail-current-str (> (length quail-current-str) 0))
950 (setq generated-events
951 (append (string-to-list
952 (if enable-multibyte-characters
953 quail-current-str
954 (string-make-unibyte quail-current-str)))
955 generated-events)))
956 (if (and input-method-exit-on-first-char generated-events)
957 (list (car generated-events))
958 generated-events))
959
960 ;; Since KEY doesn't start any translation, just return it.
961 (list key)))
962
963 (defun quail-start-conversion (key)
964 "Start conversion of the typed character KEY by the current Quail package."
965 ;; Check the possibility of translating KEY.
966 ;; If KEY is nil, we can anyway start translation.
967 (if (or (and (integerp key)
968 (assq (if (quail-kbd-translate)
969 (quail-keyboard-translate key) key)
970 (cdr (quail-map))))
971 (null key))
972 ;; Ok, we can start translation and conversion.
973 (let* ((echo-keystrokes 0)
974 (help-char nil)
975 (overriding-terminal-local-map (quail-conversion-keymap))
976 (generated-events nil)
977 (input-method-function nil))
978 (setq quail-current-key ""
979 quail-current-str ""
980 quail-translating t
981 quail-converting t
982 quail-conversion-str "")
983 (if key
984 (setq unread-command-events (cons key unread-command-events)))
985 (while quail-converting
986 (or quail-translating
987 (progn
988 (setq quail-current-key ""
989 quail-current-str ""
990 quail-translating t)
991 (quail-setup-overlays nil)))
992 (let* ((keyseq (read-key-sequence
993 (and input-method-use-echo-area
994 (concat input-method-previous-message
995 quail-conversion-str
996 quail-current-str))))
997 (cmd (lookup-key (quail-conversion-keymap) keyseq)))
998 (if (if key (commandp cmd) (eq cmd 'quail-self-insert-command))
999 (progn
1000 (setq key t)
1001 (setq last-command-event (aref keyseq (1- (length keyseq)))
1002 last-command this-command
1003 this-command cmd)
1004 (condition-case err
1005 (call-interactively cmd)
1006 (quail-error (message "%s" (cdr err)) (beep)))
1007 (or quail-translating
1008 (progn
1009 (if quail-current-str
1010 (setq quail-conversion-str
1011 (concat quail-conversion-str
1012 (if (stringp quail-current-str)
1013 quail-current-str
1014 (char-to-string quail-current-str)))))
1015 (if input-method-exit-on-first-char
1016 (setq quail-converting nil)))))
1017 ;; KEYSEQ is not defined in the conversion keymap.
1018 ;; Let's return the event(s) to the caller.
1019 (setq generated-events
1020 (string-to-list (this-single-command-raw-keys)))
1021 (setq quail-converting nil))))
1022 (if (overlay-start quail-conv-overlay)
1023 (delete-region (overlay-start quail-conv-overlay)
1024 (overlay-end quail-conv-overlay)))
1025 (if (> (length quail-conversion-str) 0)
1026 (setq generated-events
1027 (append (string-to-list
1028 (if enable-multibyte-characters
1029 quail-conversion-str
1030 (string-make-unibyte quail-conversion-str)))
1031 generated-events)))
1032 (if (and input-method-exit-on-first-char generated-events)
1033 (list (car generated-events))
1034 generated-events))
1035
1036 ;; Since KEY doesn't start any translation, just return it.
1037 (list key)))
1038
1039 (defun quail-terminate-translation ()
1040 "Terminate the translation of the current key."
1041 (setq quail-translating nil)
1042 (if (buffer-live-p quail-guidance-buf)
1043 (save-excursion
1044 (set-buffer quail-guidance-buf)
1045 (erase-buffer))))
1046
1047 (defun quail-select-current ()
1048 "Select the current text shown in Quail translation region."
1049 (interactive)
1050 (quail-terminate-translation))
1051
1052 ;; Update the current translation status according to CONTROL-FLAG.
1053 ;; If CONTROL-FLAG is integer value, it is the number of keys in the
1054 ;; head quail-current-key which can be translated. The remaining keys
1055 ;; are put back to unread-command-events to be handled again. If
1056 ;; CONTROL-FLAG is t, terminate the translation for the whole keys in
1057 ;; quail-current-key. If CONTROL-FLAG is nil, proceed the translation
1058 ;; with more keys.
1059
1060 (defun quail-update-translation (control-flag)
1061 (let ((func (quail-update-translation-function)))
1062 (if func
1063 (setq control-flag (funcall func control-flag))
1064 (if (numberp control-flag)
1065 (let ((len (length quail-current-key)))
1066 (if (= len 1)
1067 (setq control-flag t
1068 quail-current-str quail-current-key)
1069 (while (> len control-flag)
1070 (setq len (1- len))
1071 (setq unread-command-events
1072 (cons (aref quail-current-key len)
1073 unread-command-events)))
1074 (if quail-current-str
1075 (if input-method-exit-on-first-char
1076 (setq control-flag t))
1077 (setq quail-current-str
1078 (substring quail-current-key 0 len)))))
1079 (if quail-current-str
1080 (if input-method-exit-on-first-char
1081 (setq control-flag t))
1082 (setq quail-current-str quail-current-key))))
1083 (if (not input-method-use-echo-area)
1084 (progn
1085 (quail-delete-region)
1086 (insert quail-current-str))))
1087 (quail-update-guidance)
1088 (or (stringp quail-current-str)
1089 (setq quail-current-str (char-to-string quail-current-str)))
1090 (if control-flag
1091 (quail-terminate-translation)))
1092
1093 (defun quail-self-insert-command ()
1094 "Add the typed character to the key for translation."
1095 (interactive "*")
1096 (setq quail-current-key
1097 (concat quail-current-key (char-to-string last-command-event)))
1098 (or (catch 'quail-tag
1099 (quail-update-translation (quail-translate-key))
1100 t)
1101 ;; If someone throws for `quail-tag' by value nil, we exit from
1102 ;; translation mode.
1103 (setq quail-translating nil)))
1104
1105 ;; Return the actual definition part of Quail map MAP.
1106 (defun quail-map-definition (map)
1107 (let ((def (car map)))
1108 (if (and (consp def) (not (vectorp (cdr def))))
1109 (setq def (car def)))
1110 def))
1111
1112 ;; Return a string to be shown as the current translation of key
1113 ;; sequence of length LEN. DEF is a definition part of Quail map for
1114 ;; the sequence.
1115 (defun quail-get-current-str (len def)
1116 (or (and (consp def) (aref (cdr def) (car (car def))))
1117 def
1118 (and (> len 1)
1119 (let ((str (quail-get-current-str
1120 (1- len)
1121 (quail-map-definition (quail-lookup-key
1122 quail-current-key (1- len))))))
1123 (if str
1124 (concat (if (stringp str) str (char-to-string str))
1125 (substring quail-current-key (1- len) len)))))))
1126
1127 (defvar quail-guidance-translations-starting-column 20)
1128
1129 ;; Update `quail-current-translations' to make RELATIVE-INDEX the
1130 ;; current translation.
1131 (defun quail-update-current-translations (&optional relative-index)
1132 (let* ((indices (car quail-current-translations))
1133 (cur (car indices))
1134 (start (nth 1 indices))
1135 (end (nth 2 indices)))
1136 ;; Validate the index number of current translation.
1137 (if (< cur 0)
1138 (setcar indices (setq cur 0))
1139 (if (>= cur (length (cdr quail-current-translations)))
1140 (setcar indices
1141 (setq cur (1- (length (cdr quail-current-translations)))))))
1142
1143 (if (or (null end) ; We have not yet calculated END.
1144 (< cur start) ; We moved to the previous block.
1145 (>= cur end)) ; We moved to the next block.
1146 (let ((len (length (cdr quail-current-translations)))
1147 (maxcol (- (window-width quail-guidance-win)
1148 quail-guidance-translations-starting-column))
1149 (block (nth 3 indices))
1150 col idx width trans num-items blocks)
1151 (if (< cur start)
1152 ;; We must calculate from the head.
1153 (setq start 0 block 0)
1154 (if end ; i.e. (>= cur end)
1155 (setq start end)))
1156 (setq idx start col 0 end start num-items 0)
1157 ;; Loop until we hit the tail, or reach the block of CUR.
1158 (while (and (< idx len) (>= cur end))
1159 (if (= num-items 0)
1160 (setq start idx col 0 block (1+ block)))
1161 (setq trans (aref (cdr quail-current-translations) idx))
1162 (setq width (if (integerp trans) (char-width trans)
1163 (string-width trans)))
1164 (setq col (+ col width 3) num-items (1+ num-items))
1165 (if (and (> num-items 0)
1166 (or (>= col maxcol) (> num-items 10)))
1167 (setq end idx num-items 0)
1168 (setq idx (1+ idx))))
1169 (setcar (nthcdr 3 indices) block)
1170 (if (>= idx len)
1171 (progn
1172 ;; We hit the tail before reaching MAXCOL.
1173 (setq end idx)
1174 (setcar (nthcdr 4 indices) block)))
1175 (setcar (cdr indices) start)
1176 (setcar (nthcdr 2 indices) end)))
1177 (if relative-index
1178 (if (>= (+ start relative-index) end)
1179 (setcar indices end)
1180 (setcar indices (+ start relative-index))))
1181 (setq quail-current-str
1182 (aref (cdr quail-current-translations) (car indices)))
1183 (or (stringp quail-current-str)
1184 (setq quail-current-str (char-to-string quail-current-str)))))
1185
1186 (defun quail-translate-key ()
1187 "Translate the current key sequence according to the current Quail map.
1188 Return t if we can terminate the translation.
1189 Return nil if the current key sequence may be followed by more keys.
1190 Return number if we can't find any translation for the current key
1191 sequence. The number is the count of valid keys in the current
1192 sequence counting from the head."
1193 (let* ((len (length quail-current-key))
1194 (map (quail-lookup-key quail-current-key len))
1195 def ch)
1196 (if map
1197 (let ((def (quail-map-definition map)))
1198 (setq quail-current-str (quail-get-current-str len def))
1199 ;; Return t only if we can terminate the current translation.
1200 (and
1201 ;; No alternative translations.
1202 (or (null (consp def)) (= (length (cdr def)) 1))
1203 ;; No translation for the longer key.
1204 (null (cdr map))
1205 ;; No shorter breaking point.
1206 (or (null (quail-maximum-shortest))
1207 (< len 3)
1208 (null (quail-lookup-key quail-current-key (1- len)))
1209 (null (quail-lookup-key
1210 (substring quail-current-key -2 -1) 1)))))
1211
1212 ;; There's no translation for the current key sequence. Before
1213 ;; giving up, we must check two possibilities.
1214 (cond ((and
1215 (quail-maximum-shortest)
1216 (>= len 4)
1217 (setq def (quail-map-definition
1218 (quail-lookup-key quail-current-key (- len 2))))
1219 (quail-lookup-key (substring quail-current-key -2) 2))
1220 ;; Now the sequence is "...ABCD", which can be split into
1221 ;; "...AB" and "CD..." to get valid translation.
1222 ;; At first, get translation of "...AB".
1223 (setq quail-current-str (quail-get-current-str (- len 2) def))
1224 ;; Then, return the length of "...AB".
1225 (- len 2))
1226
1227 ((and (> len 0)
1228 (quail-lookup-key (substring quail-current-key 0 -1))
1229 quail-current-translations
1230 (not (quail-deterministic))
1231 (setq ch (aref quail-current-key (1- len)))
1232 (>= ch ?0) (<= ch ?9))
1233 ;; A numeric key is entered to select a desirable translation.
1234 (setq quail-current-key (substring quail-current-key 0 -1))
1235 ;; We treat key 1,2..,9,0 as specifying 0,1,..8,9.
1236 (setq ch (if (= ch ?0) 9 (- ch ?1)))
1237 (quail-update-current-translations ch)
1238 ;; And, we can terminate the current translation.
1239 t)
1240
1241 (t
1242 ;; No way to handle the last character in this context.
1243 (1- len))))))
1244
1245 (defun quail-next-translation ()
1246 "Select next translation in the current batch of candidates."
1247 (interactive)
1248 (if quail-current-translations
1249 (let ((indices (car quail-current-translations)))
1250 (if (= (1+ (car indices)) (length (cdr quail-current-translations)))
1251 ;; We are already at the tail.
1252 (beep)
1253 (setcar indices (1+ (car indices)))
1254 (quail-update-current-translations)
1255 (quail-update-translation nil)))
1256 (setq unread-command-events
1257 (cons last-command-event unread-command-events))
1258 (quail-terminate-translation)))
1259
1260 (defun quail-prev-translation ()
1261 "Select previous translation in the current batch of candidates."
1262 (interactive)
1263 (if quail-current-translations
1264 (let ((indices (car quail-current-translations)))
1265 (if (= (car indices) 0)
1266 ;; We are already at the head.
1267 (beep)
1268 (setcar indices (1- (car indices)))
1269 (quail-update-current-translations)
1270 (quail-update-translation nil)))
1271 (setq unread-command-events
1272 (cons last-command-event unread-command-events))
1273 (quail-terminate-translation)))
1274
1275 (defun quail-next-translation-block ()
1276 "Select from the next block of translations."
1277 (interactive)
1278 (if quail-current-translations
1279 (let* ((indices (car quail-current-translations))
1280 (offset (- (car indices) (nth 1 indices))))
1281 (if (>= (nth 2 indices) (length (cdr quail-current-translations)))
1282 ;; We are already at the last block.
1283 (beep)
1284 (setcar indices (+ (nth 2 indices) offset))
1285 (quail-update-current-translations)
1286 (quail-update-translation nil)))
1287 (setq unread-command-events
1288 (cons last-command-event unread-command-events))
1289 (quail-terminate-translation)))
1290
1291 (defun quail-prev-translation-block ()
1292 "Select the previous batch of 10 translation candidates."
1293 (interactive)
1294 (if quail-current-translations
1295 (let* ((indices (car quail-current-translations))
1296 (offset (- (car indices) (nth 1 indices))))
1297 (if (= (nth 1 indices) 0)
1298 ;; We are already at the first block.
1299 (beep)
1300 (setcar indices (1- (nth 1 indices)))
1301 (quail-update-current-translations)
1302 (if (< (+ (nth 1 indices) offset) (nth 2 indices))
1303 (progn
1304 (setcar indices (+ (nth 1 indices) offset))
1305 (quail-update-current-translations)))
1306 (quail-update-translation nil)))
1307 (setq unread-command-events
1308 (cons last-command-event unread-command-events))
1309 (quail-terminate-translation)))
1310
1311 (defun quail-abort-translation ()
1312 "Abort translation and delete the current Quail key sequence."
1313 (interactive)
1314 (quail-delete-region)
1315 (setq quail-current-str nil)
1316 (quail-terminate-translation))
1317
1318 (defun quail-delete-last-char ()
1319 "Delete the last input character from the current Quail key sequence."
1320 (interactive)
1321 (if (= (length quail-current-key) 1)
1322 (quail-abort-translation)
1323 (setq quail-current-key (substring quail-current-key 0 -1))
1324 (quail-update-translation (quail-translate-key))))
1325
1326 ;; For conversion mode.
1327
1328 (defun quail-conversion-backward-char ()
1329 (interactive)
1330 (if (<= (point) (overlay-start quail-conv-overlay))
1331 (quail-error "Beginning of conversion region"))
1332 (setq quail-translating nil)
1333 (forward-char -1))
1334
1335 (defun quail-conversion-forward-char ()
1336 (interactive)
1337 (if (>= (point) (overlay-end quail-conv-overlay))
1338 (quail-error "End of conversion region"))
1339 (setq quail-translating nil)
1340 (forward-char 1))
1341
1342 (defun quail-conversion-beginning-of-region ()
1343 (interactive)
1344 (setq quail-translating nil)
1345 (goto-char (overlay-start quail-conv-overlay)))
1346
1347 (defun quail-conversion-end-of-region ()
1348 (interactive)
1349 (setq quail-translating nil)
1350 (goto-char (overlay-end quail-conv-overlay)))
1351
1352 (defun quail-conversion-delete-char ()
1353 (interactive)
1354 (setq quail-translating nil)
1355 (if (>= (point) (overlay-end quail-conv-overlay))
1356 (quail-error "End of conversion region"))
1357 (delete-char 1)
1358 (let ((start (overlay-start quail-conv-overlay))
1359 (end (overlay-end quail-conv-overlay)))
1360 (setq quail-conversion-str (buffer-substring start end))
1361 (if (= start end)
1362 (setq quail-converting nil))))
1363
1364 (defun quail-conversion-delete-tail ()
1365 (interactive)
1366 (if (>= (point) (overlay-end quail-conv-overlay))
1367 (quail-error "End of conversion region"))
1368 (delete-region (point) (overlay-end quail-conv-overlay))
1369 (let ((start (overlay-start quail-conv-overlay))
1370 (end (overlay-end quail-conv-overlay)))
1371 (setq quail-conversion-str (buffer-substring start end))
1372 (if (= start end)
1373 (setq quail-converting nil))))
1374
1375 (defun quail-conversion-backward-delete-char ()
1376 (interactive)
1377 (if (<= (point) (overlay-start quail-conv-overlay))
1378 (quail-error "Beginning of conversion region"))
1379 (delete-char -1)
1380 (let ((start (overlay-start quail-conv-overlay))
1381 (end (overlay-end quail-conv-overlay)))
1382 (setq quail-conversion-str (buffer-substring start end))
1383 (if (= start end)
1384 (setq quail-converting nil))))
1385
1386 (defun quail-do-conversion (func &rest args)
1387 "Call FUNC to convert text in the current conversion region of Quail.
1388 Remaining args are for FUNC."
1389 (delete-overlay quail-overlay)
1390 (apply func args))
1391
1392 (defun quail-no-conversion ()
1393 "Do no conversion of the current conversion region of Quail."
1394 (interactive)
1395 (setq quail-converting nil))
1396
1397 ;; Guidance, Completion, and Help buffer handlers.
1398
1399 ;; Make a new one-line frame for Quail guidance buffer.
1400 (defun quail-make-guidance-frame (buf)
1401 (let* ((fparam (frame-parameters))
1402 (top (cdr (assq 'top fparam)))
1403 (border (cdr (assq 'border-width fparam)))
1404 (internal-border (cdr (assq 'internal-border-width fparam)))
1405 (newtop (- top
1406 (frame-char-height) (* internal-border 2) (* border 2))))
1407 (if (< newtop 0)
1408 (setq newtop (+ top (frame-pixel-height))))
1409 (let* ((frame (make-frame (append '((user-position . t) (height . 1)
1410 (minibuffer) (menu-bar-lines . 0))
1411 (cons (cons 'top newtop) fparam))))
1412 (win (frame-first-window frame)))
1413 (set-window-buffer win buf)
1414 ;;(set-window-dedicated-p win t)
1415 )))
1416
1417 ;; Setup Quail completion buffer.
1418 (defun quail-setup-completion-buf ()
1419 (unless (buffer-live-p quail-completion-buf)
1420 (setq quail-completion-buf (get-buffer-create "*Quail Completions*"))
1421 (save-excursion
1422 (set-buffer quail-completion-buf)
1423 (setq quail-overlay (make-overlay 1 1))
1424 (overlay-put quail-overlay 'face 'highlight))))
1425
1426 ;; Return t iff the current Quail package requires showing guidance
1427 ;; buffer.
1428 (defun quail-require-guidance-buf ()
1429 (and input-method-verbose-flag
1430 (if (eq input-method-verbose-flag 'default)
1431 (not (and (eq (selected-window) (minibuffer-window))
1432 (quail-simple)))
1433 (if (eq input-method-verbose-flag 'complex-only)
1434 (not (quail-simple))
1435 t))))
1436
1437 (defun quail-show-guidance-buf ()
1438 "Display a guidance buffer for Quail input method in some window.
1439 Create the buffer if it does not exist yet.
1440 The buffer is normally displayed at the echo area,
1441 but if the current buffer is a minibuffer, it is shown in
1442 the bottom-most ordinary window of the same frame,
1443 or in a newly created frame (if the selected frame has no other windows)."
1444 (when (quail-require-guidance-buf)
1445 ;; At first, setup a guidance buffer.
1446 (or (buffer-live-p quail-guidance-buf)
1447 (setq quail-guidance-buf (generate-new-buffer " *Quail-guidance*")))
1448 (let ((title (quail-title)))
1449 (save-excursion
1450 (set-buffer quail-guidance-buf)
1451 ;; To show the title of Quail package.
1452 (setq current-input-method t
1453 current-input-method-title title)
1454 (erase-buffer)
1455 (or (overlayp quail-overlay)
1456 (progn
1457 (setq quail-overlay (make-overlay 1 1))
1458 (overlay-put quail-overlay 'face 'highlight)))
1459 (delete-overlay quail-overlay)
1460 (set-buffer-modified-p nil)))
1461 (bury-buffer quail-guidance-buf)
1462
1463 ;; Then, display it in an appropriate window.
1464 (let ((win (minibuffer-window)))
1465 (if (or (eq (selected-window) win)
1466 input-method-use-echo-area)
1467 ;; Since we are in minibuffer, we can't use it for guidance.
1468 (if (eq win (frame-root-window))
1469 ;; Create a frame. It is sure that we are using some
1470 ;; window system.
1471 (quail-make-guidance-frame quail-guidance-buf)
1472 ;; Find the bottom window and split it if necessary.
1473 (let (height)
1474 (setq win (window-at 0 (- (frame-height) 2)))
1475 (setq height (window-height win))
1476 ;; If WIN is tall enough, split it vertically and use
1477 ;; the lower one.
1478 (if (>= height 4)
1479 (let ((window-min-height 2))
1480 ;; Here, `split-window' returns a lower window
1481 ;; which is what we wanted.
1482 (setq win (split-window win (- height 2)))))
1483 (set-window-buffer win quail-guidance-buf)
1484 ;;(set-window-dedicated-p win t)
1485 ))
1486 (set-window-buffer win quail-guidance-buf))
1487 (setq quail-guidance-win win)))
1488
1489 ;; And, create a buffer for completion.
1490 (quail-setup-completion-buf)
1491 (bury-buffer quail-completion-buf))
1492
1493 (defun quail-hide-guidance-buf ()
1494 "Hide the Quail guidance buffer."
1495 (if (buffer-live-p quail-guidance-buf)
1496 (let ((win-list (get-buffer-window-list quail-guidance-buf t t))
1497 win)
1498 (while win-list
1499 (setq win (car win-list) win-list (cdr win-list))
1500 (if (eq win (minibuffer-window))
1501 ;; We are using echo area for the guidance buffer.
1502 ;; Vacate it to the deepest minibuffer.
1503 (set-window-buffer win
1504 (format " *Minibuf-%d*" (minibuffer-depth)))
1505 (if (eq win (frame-root-window (window-frame win)))
1506 (progn
1507 ;; We are using a separate frame for guidance buffer.
1508 ;;(set-window-dedicated-p win nil)
1509 (delete-frame (window-frame win)))
1510 ;;(set-window-dedicated-p win nil)
1511 (delete-window win)))))))
1512
1513 (defun quail-update-guidance ()
1514 "Update the Quail guidance buffer and completion buffer (if displayed now)."
1515 ;; Update guidance buffer.
1516 (if (quail-require-guidance-buf)
1517 (let ((guidance (quail-guidance)))
1518 (cond ((or (eq guidance t)
1519 (listp guidance))
1520 ;; Show the current possible translations.
1521 (quail-show-translations))
1522 ((null guidance)
1523 ;; Show the current input keys.
1524 (let ((key quail-current-key))
1525 (save-excursion
1526 (set-buffer quail-guidance-buf)
1527 (erase-buffer)
1528 (insert key)))))))
1529
1530 ;; Update completion buffer if displayed now. We highlight the
1531 ;; selected candidate string in *Completion* buffer if any.
1532 (let ((win (get-buffer-window quail-completion-buf))
1533 key str pos)
1534 (if win
1535 (save-excursion
1536 (setq str (if (stringp quail-current-str)
1537 quail-current-str
1538 (if (numberp quail-current-str)
1539 (char-to-string quail-current-str)))
1540 key quail-current-key)
1541 (set-buffer quail-completion-buf)
1542 (goto-char (point-min))
1543 (if (null (search-forward (concat " " key ":") nil t))
1544 (delete-overlay quail-overlay)
1545 (setq pos (point))
1546 (if (and str (search-forward (concat "." str) nil t))
1547 (move-overlay quail-overlay (1+ (match-beginning 0)) (point))
1548 (move-overlay quail-overlay (match-beginning 0) (point)))
1549 ;; Now POS points end of KEY and (point) points end of STR.
1550 (if (pos-visible-in-window-p (point) win)
1551 ;; STR is already visible.
1552 nil
1553 ;; We want to make both KEY and STR visible, but if the
1554 ;; window is too short, make at least STR visible.
1555 (setq pos (progn (point) (goto-char pos)))
1556 (beginning-of-line)
1557 (set-window-start win (point))
1558 (if (not (pos-visible-in-window-p pos win))
1559 (set-window-start win pos))
1560 ))))))
1561
1562 (defun quail-show-translations ()
1563 "Show the current possible translations."
1564 (let* ((key quail-current-key)
1565 (map (quail-lookup-key quail-current-key))
1566 (current-translations quail-current-translations))
1567 (if quail-current-translations
1568 (quail-update-current-translations))
1569 (save-excursion
1570 (set-buffer quail-guidance-buf)
1571 (erase-buffer)
1572
1573 ;; Show the current key.
1574 (let ((guidance (quail-guidance)))
1575 (if (listp guidance)
1576 ;; We must show the specified PROMPTKEY instead of the
1577 ;; actual typed keys.
1578 (let ((i 0)
1579 (len (length key))
1580 prompt-key)
1581 (while (< i len)
1582 (setq prompt-key (cdr (assoc (aref key i) guidance)))
1583 (insert (or prompt-key (aref key i)))
1584 (setq i (1+ i))))
1585 (insert key)))
1586
1587 ;; Show followable keys.
1588 (if (and (> (length key) 0) (cdr map))
1589 (let ((keys (mapcar (function (lambda (x) (car x)))
1590 (cdr map))))
1591 (setq keys (sort keys '<))
1592 (insert "[")
1593 (while keys
1594 (insert (car keys))
1595 (setq keys (cdr keys)))
1596 (insert "]")))
1597
1598 ;; Show list of translations.
1599 (if current-translations
1600 (let* ((indices (car current-translations))
1601 (cur (car indices))
1602 (start (nth 1 indices))
1603 (end (nth 2 indices))
1604 (idx start))
1605 (indent-to (- quail-guidance-translations-starting-column 7))
1606 (insert (format "(%02d/"(nth 3 indices))
1607 (if (nth 4 indices)
1608 (format "%02d)" (nth 4 indices))
1609 "??)"))
1610 (while (< idx end)
1611 (insert (format " %d." (if (= (- idx start) 9) 0
1612 (1+ (- idx start)))))
1613 (let ((pos (point)))
1614 (insert (aref (cdr current-translations) idx))
1615 (if (= idx cur)
1616 (move-overlay quail-overlay pos (point))))
1617 (setq idx (1+ idx)))))
1618 )))
1619
1620 (defun quail-completion ()
1621 "List all completions for the current key.
1622 All possible translations of the current key and whole possible longer keys
1623 are shown."
1624 (interactive)
1625 (quail-setup-completion-buf)
1626 (let ((win (get-buffer-window quail-completion-buf 'visible))
1627 (key quail-current-key)
1628 (map (quail-lookup-key quail-current-key))
1629 (require-update nil))
1630 (save-excursion
1631 (set-buffer quail-completion-buf)
1632 (if (and win
1633 (equal key quail-current-key)
1634 (eq last-command 'quail-completion))
1635 ;; The window for Quail completion buffer has already been
1636 ;; shown. We just scroll it appropriately.
1637 (if (pos-visible-in-window-p (point-max) win)
1638 (set-window-start win (point-min))
1639 (let ((other-window-scroll-buffer quail-completion-buf))
1640 (scroll-other-window)))
1641 (setq quail-current-key key)
1642 (erase-buffer)
1643 (insert "Possible completion and corresponding translations are:\n")
1644 (quail-completion-1 key map 1)
1645 (goto-char (point-min))
1646 (display-buffer (current-buffer))
1647 (setq require-update t)))
1648 (if require-update
1649 (quail-update-guidance)))
1650 (setq this-command 'quail-completion))
1651
1652 ;; List all completions of KEY in MAP with indentation INDENT.
1653 (defun quail-completion-1 (key map indent)
1654 (let ((len (length key)))
1655 (indent-to indent)
1656 (insert key ":")
1657 (if (and (symbolp map) (fboundp map))
1658 (setq map (funcall map key len)))
1659 (if (car map)
1660 (quail-completion-list-translations map key (+ indent len 1))
1661 (insert " -\n"))
1662 (setq indent (+ indent 2))
1663 (if (cdr map)
1664 (let ((l (cdr map))
1665 (newkey (make-string (1+ len) 0))
1666 (i 0))
1667 ;; Set KEY in the first LEN characters of NEWKEY.
1668 (while (< i len)
1669 (aset newkey i (aref key i))
1670 (setq i (1+ i)))
1671 (while l ; L = ((CHAR . DEFN) ....) ;
1672 (aset newkey len (car (car l)))
1673 (quail-completion-1 newkey (cdr (car l)) indent)
1674 (setq l (cdr l)))))))
1675
1676 ;; List all possible translations of KEY in Quail map MAP with
1677 ;; indentation INDENT.
1678 (defun quail-completion-list-translations (map key indent)
1679 (let (beg (translations
1680 (quail-get-translation (car map) key (length key))))
1681 (if (integerp translations)
1682 (progn
1683 (insert "(1/1) 1.")
1684 ;; Endow the character `translations' with `mouse-face' text
1685 ;; property to enable `mouse-2' completion.
1686 (setq beg (point))
1687 (insert translations)
1688 (put-text-property beg (point) 'mouse-face 'highlight)
1689 (insert "\n"))
1690 ;; We need only vector part.
1691 (setq translations (cdr translations))
1692 ;; Insert every 10 elements with indices in a line.
1693 (let ((len (length translations))
1694 (i 0)
1695 num)
1696 (while (< i len)
1697 (when (zerop (% i 10))
1698 (when (>= i 10)
1699 (insert "\n")
1700 (indent-to indent))
1701 (insert (format "(%d/%d)" (1+ (/ i 10)) (1+ (/ len 10)))))
1702 ;; We show the last digit of FROM while converting
1703 ;; 0,1,..,9 to 1,2,..,0.
1704 (insert (format " %d." (% (1+ i) 10)))
1705 (setq beg (point))
1706 (insert (aref translations i))
1707 ;; Passing the mouse over a character will highlight.
1708 (put-text-property beg (point) 'mouse-face 'highlight)
1709 (setq i (1+ i)))
1710 (insert "\n")))))
1711
1712 ;; Choose a completion in *Quail Completions* buffer with mouse-2.
1713
1714 (defun quail-mouse-choose-completion (event)
1715 "Click on an alternative in the `*Quail Completions*' buffer to choose it."
1716 (interactive "e")
1717 ;; This function is an exact copy of the mouse.el function
1718 ;; `mouse-choose-completion' except that we:
1719 ;; 1) add two lines from `choose-completion' in simple.el to give
1720 ;; the `mouse-2' click a little more leeway.
1721 ;; 2) don't bury *Quail Completions* buffer so comment a section, and
1722 ;; 3) delete/terminate the current quail selection here.
1723 ;; Give temporary modes such as isearch a chance to turn off.
1724 (run-hooks 'mouse-leave-buffer-hook)
1725 (let ((buffer (window-buffer))
1726 choice
1727 base-size)
1728 (save-excursion
1729 (set-buffer (window-buffer (posn-window (event-start event))))
1730 (if completion-reference-buffer
1731 (setq buffer completion-reference-buffer))
1732 (setq base-size completion-base-size)
1733 (save-excursion
1734 (goto-char (posn-point (event-start event)))
1735 (let (beg end)
1736 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1737 (setq end (point) beg (1+ (point))))
1738 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
1739 (setq end (1- (point)) beg (point)))
1740 (if (null beg)
1741 (quail-error "No completion here"))
1742 (setq beg (previous-single-property-change beg 'mouse-face))
1743 (setq end (or (next-single-property-change end 'mouse-face)
1744 (point-max)))
1745 (setq choice (buffer-substring beg end)))))
1746 ; (let ((owindow (selected-window)))
1747 ; (select-window (posn-window (event-start event)))
1748 ; (if (and (one-window-p t 'selected-frame)
1749 ; (window-dedicated-p (selected-window)))
1750 ; ;; This is a special buffer's frame
1751 ; (iconify-frame (selected-frame))
1752 ; (or (window-dedicated-p (selected-window))
1753 ; (bury-buffer)))
1754 ; (select-window owindow))
1755 (quail-delete-region)
1756 (quail-choose-completion-string choice buffer base-size)
1757 (quail-terminate-translation)))
1758
1759 ;; Modify the simple.el function `choose-completion-string', because
1760 ;; the simple.el function `choose-completion-delete-max-match' breaks
1761 ;; on Mule data, since the semantics of `forward-char' have changed.
1762
1763 (defun quail-choose-completion-string (choice &optional buffer base-size)
1764 (let ((buffer (or buffer completion-reference-buffer)))
1765 ;; If BUFFER is a minibuffer, barf unless it's the currently
1766 ;; active minibuffer.
1767 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
1768 (or (not (active-minibuffer-window))
1769 (not (equal buffer
1770 (window-buffer (active-minibuffer-window))))))
1771 (quail-error "Minibuffer is not active for completion")
1772 ;; Store the completion in `quail-current-str', which will later
1773 ;; be converted to a character event list, then inserted into
1774 ;; the buffer where completion was requested.
1775 (set-buffer buffer)
1776 ; (if base-size
1777 ; (delete-region (+ base-size (point-min)) (point))
1778 ; (choose-completion-delete-max-match choice))
1779 (setq quail-current-str choice)
1780 ;; Update point in the window that BUFFER is showing in.
1781 (let ((window (get-buffer-window buffer t)))
1782 (set-window-point window (point)))
1783 ;; If completing for the minibuffer, exit it with this choice.
1784 (and (not completion-no-auto-exit)
1785 (equal buffer (window-buffer (minibuffer-window)))
1786 minibuffer-completion-table
1787 ;; If this is reading a file name, and the file name chosen
1788 ;; is a directory, don't exit the minibuffer.
1789 (if (and (eq minibuffer-completion-table 'read-file-name-internal)
1790 (file-directory-p (buffer-string)))
1791 (select-window (active-minibuffer-window))
1792 (exit-minibuffer))))))
1793
1794 (defun quail-help ()
1795 "Show brief description of the current Quail package."
1796 (interactive)
1797 (let ((package quail-current-package))
1798 (with-output-to-temp-buffer "*Quail-Help*"
1799 (save-excursion
1800 (set-buffer standard-output)
1801 (let ((quail-current-package package))
1802 (insert "Quail input method (name:"
1803 (quail-name)
1804 ", mode line indicator:["
1805 (quail-title)
1806 "])\n---- Documentation ----\n"
1807 (quail-docstring))
1808 (newline)
1809 (if (quail-show-layout) (quail-show-kbd-layout))
1810 (quail-help-insert-keymap-description
1811 (quail-translation-keymap)
1812 "--- Key bindings (while translating) ---
1813 key binding
1814 --- -------\n")
1815 (if (quail-conversion-keymap)
1816 (quail-help-insert-keymap-description
1817 (quail-conversion-keymap)
1818 "--- Key bindings (while converting) ---
1819 key binding
1820 --- -------\n"))
1821 (help-mode))))))
1822
1823 (defun quail-help-insert-keymap-description (keymap &optional header)
1824 (let (from to)
1825 (if header
1826 (insert header))
1827 (save-excursion
1828 (save-window-excursion
1829 (let ((overriding-terminal-local-map keymap))
1830 (describe-bindings))
1831 (set-buffer "*Help*")
1832 (goto-char (point-min))
1833 (forward-line 4)
1834 (setq from (point))
1835 (search-forward "Global Bindings:" nil 'move)
1836 (beginning-of-line)
1837 (setq to (point))))
1838 (insert-buffer-substring "*Help*" from to)))
1839
1840 (defun quail-show-kbd-layout ()
1841 "Show keyboard layout with key tops of multilingual characters."
1842 (insert "--- Keyboard layout ---\n")
1843 (let* ((i 0) ch)
1844 (while (< i quail-keyboard-layout-len)
1845 (if (= (% i 30) 0)
1846 (progn
1847 (newline)
1848 (indent-to (/ i 30)))
1849 (if (= (% i 2) 0)
1850 (insert " ")))
1851 (setq ch (aref quail-keyboard-layout i))
1852 (when (and (quail-kbd-translate)
1853 (/= ch ?\ ))
1854 ;; This is the case that the current input method simulates
1855 ;; some keyboard layout (which means it requires keyboard
1856 ;; translation) and a key at location `i' exists on users
1857 ;; keyboard. We must translate that key by
1858 ;; `quail-keyboard-layout-standard'. But if if there's no
1859 ;; corresponding key in that standard layout, we must simulate
1860 ;; what is inserted if that key is pressed by setting CH a
1861 ;; minus value.
1862 (setq ch (aref quail-keyboard-layout-standard i))
1863 (if (= ch ?\ )
1864 (setq ch (- (aref quail-keyboard-layout i)))))
1865 (if (< ch 0)
1866 (let ((last-command-event (- ch)))
1867 (self-insert-command 1))
1868 (if (= ch ?\ )
1869 (insert ch)
1870 (let* ((map (cdr (assq ch (cdr (quail-map)))))
1871 (translation (and map (quail-get-translation
1872 (car map) (char-to-string ch) 1))))
1873 (if (integerp translation)
1874 (insert translation)
1875 (if (consp translation)
1876 (insert (aref (cdr translation) (car (car translation))))
1877 (let ((last-command-event ch))
1878 (self-insert-command 1)))))))
1879 (setq i (1+ i))))
1880 (newline))
1881
1882 (defun quail-translation-help ()
1883 "Show help message while translating in Quail input method."
1884 (interactive)
1885 (if (not (eq this-command last-command))
1886 (let (state-msg keymap)
1887 (if (and quail-converting (= (length quail-current-key) 0))
1888 (setq state-msg
1889 (format "Converting string %S by input method %S.\n"
1890 quail-conversion-str (quail-name))
1891 keymap (quail-conversion-keymap))
1892 (setq state-msg
1893 (format "Translating key sequence %S by input method %S.\n"
1894 quail-current-key (quail-name))
1895 keymap (quail-translation-keymap)))
1896 (with-output-to-temp-buffer "*Quail-Help*"
1897 (save-excursion
1898 (set-buffer standard-output)
1899 (insert state-msg)
1900 (quail-help-insert-keymap-description
1901 keymap
1902 "-----------------------
1903 key binding
1904 --- -------\n")
1905 (help-mode)))))
1906 (let (scroll-help)
1907 (save-selected-window
1908 (select-window (get-buffer-window "*Quail-Help*"))
1909 (if (eq this-command last-command)
1910 (if (< (window-end) (point-max))
1911 (scroll-up)
1912 (if (> (window-start) (point-min))
1913 (set-window-start (selected-window) (point-min)))))
1914 (setq scroll-help
1915 (if (< (window-end (selected-window) 'up-to-date) (point-max))
1916 "Type \\[quail-translation-help] to scroll up the help"
1917 (if (> (window-start) (point-min))
1918 "Type \\[quail-translation-help] to see the head of help"))))
1919 (if scroll-help
1920 (progn
1921 (message "%s" (substitute-command-keys scroll-help))
1922 (sit-for 1)
1923 (message nil)
1924 (quail-update-guidance)
1925 ))))
1926
1927 \f
1928 (defvar quail-directory-name "quail"
1929 "Name of Quail directory which contains Quail packages.
1930 This is a sub-directory of LEIM directory.")
1931
1932 ;;;###autoload
1933 (defun quail-update-leim-list-file (dirname &rest dirnames)
1934 "Update entries for Quail packages in `LEIM' list file in directory DIRNAME.
1935 DIRNAME is a directory containing Emacs input methods;
1936 normally, it should specify the `leim' subdirectory
1937 of the Emacs source tree.
1938
1939 It searches for Quail packages under `quail' subdirectory of DIRNAME,
1940 and update the file \"leim-list.el\" in DIRNAME.
1941
1942 When called from a program, the remaining arguments are additional
1943 directory names to search for Quail packages under `quail' subdirectory
1944 of each directory."
1945 (interactive "FDirectory of LEIM: ")
1946 (setq dirname (expand-file-name dirname))
1947 (let ((leim-list (expand-file-name leim-list-file-name dirname))
1948 quail-dirs list-buf pkg-list pkg-buf pos)
1949 (if (not (file-writable-p leim-list))
1950 (error "Can't write to file \"%s\"" leim-list))
1951 (message "Updating %s ..." leim-list)
1952 (setq list-buf (find-file-noselect leim-list))
1953
1954 ;; At first, clean up the file.
1955 (save-excursion
1956 (set-buffer list-buf)
1957 (goto-char 1)
1958
1959 ;; Insert the correct header.
1960 (if (looking-at (regexp-quote leim-list-header))
1961 (goto-char (match-end 0))
1962 (insert leim-list-header))
1963 (setq pos (point))
1964 (if (not (re-search-forward leim-list-entry-regexp nil t))
1965 nil
1966
1967 ;; Remove garbages after the header.
1968 (goto-char (match-beginning 0))
1969 (if (< pos (point))
1970 (delete-region pos (point)))
1971
1972 ;; Remove all entries for Quail.
1973 (while (re-search-forward leim-list-entry-regexp nil 'move)
1974 (goto-char (match-beginning 0))
1975 (setq pos (point))
1976 (condition-case nil
1977 (let ((form (read list-buf)))
1978 (when (equal (nth 3 form) ''quail-use-package)
1979 (if (eolp) (forward-line 1))
1980 (delete-region pos (point))))
1981 (error
1982 ;; Delete the remaining contents because it seems that
1983 ;; this file is broken.
1984 (message "Garbage in %s deleted" leim-list)
1985 (delete-region pos (point-max)))))))
1986
1987 ;; Search for `quail' subdirectory under each DIRNAMES.
1988 (setq dirnames (cons dirname dirnames))
1989 (let ((l dirnames))
1990 (while l
1991 (setcar l (expand-file-name (car l)))
1992 (setq dirname (expand-file-name quail-directory-name (car l)))
1993 (if (file-readable-p dirname)
1994 (setq quail-dirs (cons dirname quail-dirs))
1995 (message "%s doesn't have `%s' subdirectory, just ignored"
1996 (car l) quail-directory-name)
1997 (setq quail-dirs (cons nil quail-dirs)))
1998 (setq l (cdr l)))
1999 (setq quail-dirs (nreverse quail-dirs)))
2000
2001 ;; Insert input method registering forms.
2002 (while quail-dirs
2003 (setq dirname (car quail-dirs))
2004 (when dirname
2005 (setq pkg-list (directory-files dirname 'full "\\.el$" 'nosort))
2006 (while pkg-list
2007 (message "Checking %s ..." (car pkg-list))
2008 (with-temp-buffer
2009 (insert-file-contents (car pkg-list))
2010 (goto-char (point-min))
2011 (while (search-forward "(quail-define-package" nil t)
2012 (goto-char (match-beginning 0))
2013 (condition-case nil
2014 (let ((form (read (current-buffer))))
2015 (save-excursion
2016 (set-buffer list-buf)
2017 (insert
2018 (format "(register-input-method
2019 %S %S '%s
2020 %S %S
2021 %S)\n"
2022 (nth 1 form) ; PACKAGE-NAME
2023 (nth 2 form) ; LANGUAGE
2024 'quail-use-package ; ACTIVATE-FUNC
2025 (nth 3 form) ; PACKAGE-TITLE
2026 (progn ; PACKAGE-DESCRIPTION (one line)
2027 (string-match ".*" (nth 5 form))
2028 (match-string 0 (nth 5 form)))
2029 (file-relative-name ; PACKAGE-FILENAME
2030 (file-name-sans-extension (car pkg-list))
2031 (car dirnames))))))
2032 (error
2033 ;; Ignore the remaining contents of this file.
2034 (goto-char (point-max))
2035 (message "Some part of \"%s\" is broken" dirname)))))
2036 (setq pkg-list (cdr pkg-list)))
2037 (setq quail-dirs (cdr quail-dirs) dirnames (cdr dirnames))))
2038
2039 ;; At last, write out LEIM list file.
2040 (save-excursion
2041 (set-buffer list-buf)
2042 (setq buffer-file-coding-system 'iso-2022-7bit)
2043 (save-buffer 0))
2044 (kill-buffer list-buf)
2045 (message "Updating %s ... done" leim-list)))
2046 ;;
2047 (provide 'quail)
2048
2049 ;;; quail.el ends here