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