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