mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathquail.el
3121 lines (2876 loc) · 118 KB
/
quail.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; quail.el --- provides simple input method for multilingual text -*- lexical-binding: t; -*-
;; Copyright (C) 1997-1998, 2000-2025 Free Software Foundation, Inc.
;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
;; National Institute of Advanced Industrial Science and Technology (AIST)
;; Registration Number H14PRO021
;; Author: Kenichi Handa <handa@gnu.org>
;; Naoto Takahashi <ntakahas@etl.go.jp>
;; Maintainer: emacs-devel@gnu.org
;; Keywords: mule, multilingual, input method, i18n
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; In Quail minor mode, you can input multilingual text easily. By
;; defining a translation table (named Quail map) which maps ASCII key
;; string to multilingual character or string, you can input any text
;; from ASCII keyboard.
;;
;; We use words "translation" and "conversion" differently. The
;; former is done by Quail package itself, the latter is the further
;; process of converting a translated text to some more desirable
;; text. For instance, Quail package for Japanese (`quail-jp')
;; translates Roman text (transliteration of Japanese in Latin
;; alphabets) to Hiragana text, which is then converted to
;; Kanji-and-Kana mixed text or Katakana text by commands specified in
;; CONVERSION-KEYS argument of the Quail package.
;; [There was an input method for Mule 2.3 called `Tamago' from the
;; Japanese `TAkusan MAtasete GOmen-nasai', or `Sorry for having you
;; wait so long'; this couldn't be included in Emacs 20. `Tamago' is
;; Japanese for `egg' (implicitly a hen's egg). Handa-san made a
;; smaller and simpler system; the smaller quail egg is also eaten in
;; Japan. Maybe others will be egged on to write more sorts of input
;; methods.]
;;; Code:
(require 'help-mode)
(eval-when-compile (require 'cl-lib))
(defgroup quail nil
"Quail: multilingual input method."
:group 'leim)
;; Buffer local variables
(defvar-local quail-current-package nil
"The current Quail package, which depends on the current input method.
See the documentation of `quail-package-alist' for the format.")
(put 'quail-current-package 'permanent-local t)
;; Quail uses the following variables to assist users.
;; A string containing available key sequences or translation list.
(defvar-local quail-guidance-str nil)
;; A buffer to show completion list of the current key sequence.
(defvar quail-completion-buf nil)
;; We may display the guidance string in a buffer on a one-line frame.
(defvar quail-guidance-buf nil)
(defvar quail-guidance-frame nil)
;; Each buffer in which Quail is activated should use different
;; guidance string.
(put 'quail-guidance-str 'permanent-local t)
(defvar-local quail-overlay nil
"Overlay which covers the current translation region of Quail.")
(defvar-local quail-conv-overlay nil
"Overlay which covers the text to be converted in Quail mode.")
(defvar-local quail-current-key nil
"Current key for translation in Quail mode.")
(defvar-local quail-current-str nil
"Currently selected translation of the current key.")
(defvar-local quail-current-translations nil
"Cons of indices and vector of possible translations of the current key.
Indices is a list of (CURRENT START END BLOCK BLOCKS), where
CURRENT is an index of the current translation,
START and END are indices of the start and end of the current block,
BLOCK is the current block index,
BLOCKS is a number of blocks of translation.")
(defvar-local quail-current-data nil
"Any Lisp object holding information of current translation status.
When a key sequence is mapped to TRANS and TRANS is a cons
of actual translation and some Lisp object to be referred
for translating the longer key sequence, this variable is set
to that Lisp object.")
;; Quail package handlers.
(defvar quail-package-alist nil
"List of Quail packages.
A Quail package is a list of these elements:
NAME, TITLE, QUAIL-MAP, GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST, UPDATE-TRANSLATION-FUNCTION,
CONVERSION-KEYS, SIMPLE.
QUAIL-MAP is a data structure to map key strings to translations. For
the format, see the documentation of `quail-map-p'.
DECODE-MAP is an alist of translations and corresponding keys.
See the documentation of `quail-define-package' for the other elements.")
;; Return various slots in the current quail-package.
(defsubst quail-name ()
"Return the name of the current Quail package."
(nth 0 quail-current-package))
(defun quail-indent-to (col)
(indent-to col)
(let ((end (point)))
(save-excursion
(unless (zerop (skip-chars-backward "\t "))
(put-text-property (point) end 'display (list 'space :align-to col))))))
;;;###autoload
(defun quail-title ()
"Return the title of the current Quail package."
(let ((title (nth 1 quail-current-package)))
;; TITLE may be a string or a list. If it is a list, each element
;; is a string or the form (VAR STR1 STR2), and the interpretation
;; of the list is the same as that of mode-line-format.
(if (stringp title)
title
(condition-case nil
(mapconcat
(lambda (x)
(cond ((stringp x) x)
((and (listp x) (symbolp (car x)) (= (length x) 3))
(if (symbol-value (car x))
(nth 1 x) (nth 2 x)))
(t "")))
title "")
(error "")))))
(defsubst quail-map ()
"Return the translation map of the current Quail package."
(nth 2 quail-current-package))
(defsubst quail-guidance ()
"Return an object used for `guidance' feature of the current Quail package.
See also the documentation of `quail-define-package'."
(nth 3 quail-current-package))
(defsubst quail-docstring ()
"Return the documentation string of the current Quail package."
(nth 4 quail-current-package))
(defsubst quail-translation-keymap ()
"Return translation keymap in the current Quail package.
Translation keymap is a keymap used while translation region is active."
(nth 5 quail-current-package))
(defsubst quail-forget-last-selection ()
"Return `forget-last-selection' flag of the current Quail package.
See also the documentation of `quail-define-package'."
(nth 6 quail-current-package))
(defsubst quail-deterministic ()
"Return `deterministic' flag of the current Quail package.
See also the documentation of `quail-define-package'."
(nth 7 quail-current-package))
(defsubst quail-kbd-translate ()
"Return `kbd-translate' flag of the current Quail package.
See also the documentation of `quail-define-package'."
(nth 8 quail-current-package))
(defsubst quail-show-layout ()
"Return `show-layout' flag of the current Quail package.
See also the documentation of `quail-define-package'."
(nth 9 quail-current-package))
(defsubst quail-decode-map ()
"Return decode map of the current Quail package.
It is an alist of translations and corresponding keys."
(nth 10 quail-current-package))
(defsubst quail-maximum-shortest ()
"Return `maximum-shortest' flag of the current Quail package.
See also the documentation of `quail-define-package'."
(nth 11 quail-current-package))
(defsubst quail-overlay-plist ()
"Return property list of an overlay used in the current Quail package."
(nth 12 quail-current-package))
(defsubst quail-update-translation-function ()
"Return a function for updating translation in the current Quail package."
(nth 13 quail-current-package))
(defsubst quail-conversion-keymap ()
"Return conversion keymap in the current Quail package.
Conversion keymap is a keymap used while conversion region is active
but translation region is not active."
(nth 14 quail-current-package))
(defsubst quail-simple ()
"Return t if the current Quail package is simple."
(nth 15 quail-current-package))
(defsubst quail-package (name)
"Return Quail package named NAME."
(assoc name quail-package-alist))
(defun quail-add-package (package)
"Add Quail package PACKAGE to `quail-package-alist'."
(let ((pac (quail-package (car package))))
(if pac
(setcdr pac (cdr package))
(setq quail-package-alist (cons package quail-package-alist)))))
(defun quail-select-package (name)
"Select Quail package named NAME as the current Quail package."
(let ((package (quail-package name)))
(if (null package)
(error "No Quail package `%s'" name))
(setq quail-current-package package)
(setq-default quail-current-package package)
name))
;;;###autoload
(defun quail-use-package (package-name &rest libraries)
"Start using Quail package PACKAGE-NAME.
The remaining arguments are LIBRARIES to be loaded before using the package.
This activates input method defined by PACKAGE-NAME by running
`quail-activate', which see."
(let ((package (quail-package package-name)))
(if (null package)
;; Perhaps we have not yet loaded necessary libraries.
(while libraries
(if (not (load (car libraries) t))
(progn
(with-output-to-temp-buffer "*Help*"
(princ "Quail package \"")
(princ package-name)
(princ (substitute-command-keys
"\" can't be activated\n because library \""))
(princ (car libraries))
(princ (substitute-command-keys "\" is not in `load-path'.
This might indicate a problem with your Emacs installation, as
LEIM (Libraries of Emacs Input Method) should normally always be
installed together with Emacs.")))
(error "Can't use the Quail package `%s'" package-name))
(setq libraries (cdr libraries))))))
(quail-select-package package-name)
(setq current-input-method-title (quail-title))
(quail-activate)
;; Hide all '... loaded' message.
(message nil))
(defvar quail-translation-keymap
(let ((map (make-keymap))
(i 0))
(while (< i ?\ )
(define-key map (char-to-string i) 'quail-other-command)
(setq i (1+ i)))
(while (< i 127)
(define-key map (char-to-string i) 'quail-self-insert-command)
(setq i (1+ i)))
(setq i 128)
(while (< i 256)
(define-key map (vector i) 'quail-self-insert-command)
(setq i (1+ i)))
(define-key map "\177" 'quail-delete-last-char)
(define-key map "\C-f" 'quail-next-translation)
(define-key map "\C-b" 'quail-prev-translation)
(define-key map "\C-n" 'quail-next-translation-block)
(define-key map "\C-p" 'quail-prev-translation-block)
(define-key map [right] 'quail-next-translation)
(define-key map [left] 'quail-prev-translation)
(define-key map [down] 'quail-next-translation-block)
(define-key map [up] 'quail-prev-translation-block)
(define-key map "\C-i" 'quail-completion)
(define-key map "\C-@" 'quail-select-current)
;; Following simple.el, Enter key on numeric keypad selects the
;; current translation just like `C-SPC', and `mouse-2' chooses
;; any completion visible in the *Quail Completions* buffer.
(define-key map [kp-enter] 'quail-select-current)
(define-key map [mouse-2] 'quail-mouse-choose-completion)
(define-key map [down-mouse-2] nil)
(define-key map "\C-h" 'quail-translation-help)
(define-key map [?\C- ] 'quail-select-current)
(define-key map [tab] 'quail-completion)
(define-key map [delete] 'quail-delete-last-char)
(define-key map [backspace] 'quail-delete-last-char)
map)
"Keymap used processing translation in complex Quail modes.
Only a few especially complex input methods use this map;
most use `quail-simple-translation-keymap' instead.
This map is activated while translation region is active.")
(defvar quail-translation-docstring
"When you type keys, the echo area shows the possible characters
which correspond to that key sequence, each preceded by a digit. You
can select one of the characters shown by typing the corresponding
digit. Alternatively, you can use C-f and C-b to move through the
line to select the character you want, then type a letter to begin
entering another Chinese character or type a space or punctuation
character.
If there are more than ten possible characters for the given spelling,
the echo area shows ten characters at a time; you can use C-n to move
to the next group of ten, and C-p to move back to the previous group
of ten.")
;; Categorize each Quail commands to make the output of quail-help
;; concise. This is done by putting `quail-help' property. The value
;; is:
;; hide -- never show this command
;; non-deterministic -- show only for non-deterministic input method
(let ((l '((quail-other-command . hide)
(quail-self-insert-command . hide)
(quail-delete-last-char . hide)
(quail-next-translation . non-deterministic)
(quail-prev-translation . non-deterministic)
(quail-next-translation-block . non-deterministic)
(quail-prev-translation-block . non-deterministic))))
(while l
(put (car (car l)) 'quail-help (cdr (car l)))
(setq l (cdr l))))
(defvar quail-simple-translation-keymap
(let ((map (make-keymap))
(i 0))
(while (< i ?\ )
(define-key map (char-to-string i) 'quail-other-command)
(setq i (1+ i)))
(while (< i 127)
(define-key map (char-to-string i) 'quail-self-insert-command)
(setq i (1+ i)))
(setq i 128)
(while (< i 256)
(define-key map (vector i) 'quail-self-insert-command)
(setq i (1+ i)))
(define-key map "\177" 'quail-delete-last-char)
(define-key map [delete] 'quail-delete-last-char)
(define-key map [backspace] 'quail-delete-last-char)
;;(let ((meta-map (make-sparse-keymap)))
;;(define-key map (char-to-string meta-prefix-char) meta-map)
;;(define-key map [escape] meta-map))
map)
"Keymap used while processing translation in simple Quail modes.
A few especially complex input methods use `quail-translation-keymap' instead.
This map is activated while translation region is active.")
(defvar quail-conversion-keymap
(let ((map (make-keymap))
(i ?\ ))
(while (< i 127)
(define-key map (char-to-string i) 'quail-self-insert-command)
(setq i (1+ i)))
(setq i 128)
(while (< i 256)
(define-key map (vector i) 'quail-self-insert-command)
(setq i (1+ i)))
(define-key map "\C-b" 'quail-conversion-backward-char)
(define-key map "\C-f" 'quail-conversion-forward-char)
(define-key map "\C-a" 'quail-conversion-beginning-of-region)
(define-key map "\C-e" 'quail-conversion-end-of-region)
(define-key map "\C-d" 'quail-conversion-delete-char)
(define-key map "\C-k" 'quail-conversion-delete-tail)
(define-key map "\C-h" 'quail-translation-help)
(define-key map "\177" 'quail-conversion-backward-delete-char)
(define-key map [delete] 'quail-conversion-backward-delete-char)
(define-key map [backspace] 'quail-conversion-backward-delete-char)
map)
"Keymap used for processing conversion in Quail mode.
This map is activated while conversion region is active but translation
region is not active.")
;; Just a dummy definition.
(defun quail-other-command ()
(interactive)
)
;;;###autoload
(defun quail-define-package (name language title
&optional guidance docstring translation-keys
forget-last-selection deterministic
kbd-translate show-layout create-decode-map
maximum-shortest overlay-plist
update-translation-function
conversion-keys simple)
"Define NAME as a new Quail package for input LANGUAGE.
TITLE is a string to be displayed at mode-line to indicate this package.
Optional arguments are GUIDANCE, DOCSTRING, TRANSLATION-KEYS,
FORGET-LAST-SELECTION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT,
CREATE-DECODE-MAP, MAXIMUM-SHORTEST, OVERLAY-PLIST,
UPDATE-TRANSLATION-FUNCTION, CONVERSION-KEYS and SIMPLE.
GUIDANCE specifies how a guidance string is shown in echo area.
If it is t, list of all possible translations for the current key is shown
with the currently selected translation being highlighted.
If it is an alist, the element has the form (CHAR . STRING). Each character
in the current key is searched in the list and the corresponding string is
shown.
If it is nil, the current key is shown.
DOCSTRING is the documentation string of this package. The command
`describe-input-method' shows this string while replacing the form
\\=\\=\\=\\<VAR> in the string by the value of VAR. That value should be a
string. For instance, the form \\=\\=\\=\\<quail-translation-docstring> is
replaced by a description about how to select a translation from a
list of candidates.
TRANSLATION-KEYS specifies additional key bindings used while translation
region is active. It is an alist of single key character vs. corresponding
command to be called.
FORGET-LAST-SELECTION non-nil means a selected translation is not kept
for the future to translate the same key. If this flag is nil, a
translation selected for a key is remembered so that it can be the
first candidate when the same key is entered later.
DETERMINISTIC non-nil means the first candidate of translation is
selected automatically without allowing users to select another
translation for a key. In this case, unselected translations are of
no use for an interactive use of Quail but can be used by some other
programs. If this flag is non-nil, FORGET-LAST-SELECTION is also set
to t.
KBD-TRANSLATE non-nil means input characters are translated from a
user's keyboard layout to the standard keyboard layout. See the
documentation of `quail-keyboard-layout' and
`quail-keyboard-layout-standard' for more detail.
SHOW-LAYOUT non-nil means the function `quail-help' (as used by
the command `describe-input-method') should show the user's keyboard
layout visually with translated characters. If KBD-TRANSLATE is
set, it is desirable to also set this flag, unless this package
defines no translations for single character keys.
CREATE-DECODE-MAP non-nil means decode map is also created. A decode
map is an alist of translations and corresponding original keys.
Although this map is not used by Quail itself, it can be used by some
other programs. For instance, Vietnamese supporting needs this map to
convert Vietnamese text to VIQR format which uses only ASCII
characters to represent Vietnamese characters.
MAXIMUM-SHORTEST non-nil means break key sequence to get maximum
length of the shortest sequence. When we don't have a translation of
key \"..ABCD\" but have translations of \"..AB\" and \"CD..\", break
the key at \"..AB\" and start translation of \"CD..\". Hangul
packages, for instance, use this facility. If this flag is nil, we
break the key just at \"..ABC\" and start translation of \"D..\".
OVERLAY-PLIST if non-nil is a property list put on an overlay which
covers Quail translation region.
UPDATE-TRANSLATION-FUNCTION if non-nil is a function to call to update
the current translation region according to a new translation data. By
default, a translated text or a user's key sequence (if no translation
for it) is inserted.
CONVERSION-KEYS specifies additional key bindings used while
conversion region is active. It is an alist of single key character
vs. corresponding command to be called.
If SIMPLE is non-nil, then we do not alter the meanings of
commands such as \\[forward-char], \\[backward-char], \\[next-line], \
\\[previous-line] and \\[indent-for-tab-command]; they are treated as
non-Quail commands."
(let (translation-keymap conversion-keymap)
(if deterministic (setq forget-last-selection t))
(if translation-keys
(progn
(setq translation-keymap (copy-keymap
(if simple quail-simple-translation-keymap
quail-translation-keymap)))
(dolist (trans translation-keys)
(define-key translation-keymap (car trans) (cdr trans))))
(setq translation-keymap
(if simple quail-simple-translation-keymap
quail-translation-keymap)))
(when conversion-keys
(setq conversion-keymap (copy-keymap quail-conversion-keymap))
(dolist (conv conversion-keys)
(define-key conversion-keymap (car conv) (cdr conv))))
(quail-add-package
(list name title (list nil) guidance (or docstring "")
translation-keymap
forget-last-selection deterministic kbd-translate show-layout
(if create-decode-map (list 'decode-map) nil)
maximum-shortest overlay-plist update-translation-function
conversion-keymap simple))
;; Update input-method-alist.
(let ((slot (assoc name input-method-alist))
(val (list language 'quail-use-package title docstring)))
(if slot (setcdr slot val)
(setq input-method-alist (cons (cons name val) input-method-alist)))))
(quail-select-package name))
;; Quail minor mode handlers.
;; Setup overlays used in Quail mode.
(defun quail-setup-overlays (conversion-mode)
(let ((pos (point)))
(if (overlayp quail-overlay)
(move-overlay quail-overlay pos pos)
(setq quail-overlay (make-overlay pos pos))
(if input-method-highlight-flag
(overlay-put quail-overlay 'face 'underline))
(let ((l (quail-overlay-plist)))
(while l
(overlay-put quail-overlay (car l) (car (cdr l)))
(setq l (cdr (cdr l))))))
(if conversion-mode
(if (overlayp quail-conv-overlay)
(if (not (overlay-start quail-conv-overlay))
(move-overlay quail-conv-overlay pos pos))
(setq quail-conv-overlay (make-overlay pos pos))
(if input-method-highlight-flag
(overlay-put quail-conv-overlay 'face 'underline))))))
;; Delete overlays used in Quail mode.
(defun quail-delete-overlays ()
(if (and (overlayp quail-overlay) (overlay-start quail-overlay))
(delete-overlay quail-overlay))
(if (and (overlayp quail-conv-overlay) (overlay-start quail-conv-overlay))
(delete-overlay quail-conv-overlay)))
(defun quail-deactivate ()
"Deactivate Quail input method.
This function runs the normal hook `quail-deactivate-hook'."
(interactive)
(quail-activate -1))
(defun quail-activate (&optional arg)
"Activate Quail input method.
With ARG, activate Quail input method if and only if arg is positive.
This function runs `quail-activate-hook' if it activates the input
method, `quail-deactivate-hook' if it deactivates it.
While this input method is active, the variable
`input-method-function' is bound to the function `quail-input-method'."
(if (and arg
(< (prefix-numeric-value arg) 0))
;; Let's deactivate Quail input method.
(unwind-protect
(progn
(quail-delete-overlays)
(setq describe-current-input-method-function nil)
(quail-hide-guidance)
(remove-hook 'post-command-hook #'quail-show-guidance t)
(run-hooks 'quail-deactivate-hook))
(kill-local-variable 'input-method-function))
;; Let's activate Quail input method.
(if (null quail-current-package)
;; Quail package is not yet selected. Select one now.
(let (name)
(if quail-package-alist
(setq name (car (car quail-package-alist)))
(error "No Quail package loaded"))
(quail-select-package name)))
(setq deactivate-current-input-method-function #'quail-deactivate)
(setq describe-current-input-method-function #'quail-help)
(quail-delete-overlays)
(setq quail-guidance-str "")
(quail-show-guidance)
;; If we are in minibuffer, turn off the current input method
;; before exiting.
(when (eq (selected-window) (minibuffer-window))
(add-hook 'minibuffer-exit-hook #'quail-exit-from-minibuffer)
(add-hook 'post-command-hook #'quail-show-guidance nil t))
(run-hooks 'quail-activate-hook)
(setq-local input-method-function #'quail-input-method)))
(defun quail-exit-from-minibuffer ()
(deactivate-input-method)
(if (<= (minibuffer-depth) 1)
(remove-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer)))
;; Keyboard layout translation handlers.
;; Some Quail packages provide localized keyboard simulation which
;; requires a particular keyboard layout. In this case, what we need
;; is locations of keys the user entered, not character codes
;; generated by those keys. However, for the moment, there's no
;; common way to get such information. So, we ask a user to give
;; information of his own keyboard layout, then translate it to the
;; standard layout which we defined so that all Quail packages depend
;; just on it.
(defconst quail-keyboard-layout-standard
"\
\
1!2@3#4$5%6^7&8*9(0)-_=+`~ \
qQwWeErRtTyYuUiIoOpP[{]} \
aAsSdDfFgGhHjJkKlL;:'\"\\| \
zZxXcCvVbBnNmM,<.>/? \
"
"Standard keyboard layout of printable characters Quail assumes.
See the documentation of `quail-keyboard-layout' for this format.
This layout is almost the same as that of VT100,
but the location of key \\ (backslash) is just right of key \\=' (single-quote),
not right of RETURN key.")
(defconst quail-keyboard-layout-len 180)
;; Here we provide several examples of famous keyboard layouts.
;; This is a candidate for a language environment-dependent setting.
(defvar quail-keyboard-layout-alist
(list
(cons "standard" quail-keyboard-layout-standard)
'("sun-type3" . "\
\
1!2@3#4$5%6^7&8*9(0)-_=+\\|`~\
qQwWeErRtTyYuUiIoOpP[{]} \
aAsSdDfFgGhHjJkKlL;:'\" \
zZxXcCvVbBnNmM,<.>/? \
")
'("atari-german" . "\
\
1!2\"3\2474$5%6&7/8(9)0=\337?'`#^ \
qQwWeErRtTzZuUiIoOpP\374\334+* \
aAsSdDfFgGhHjJkKlL\366\326\344\304~| \
<>yYxXcCvVbBnNmM,;.:-_ \
")
'("pc102-de" . "\
\
^\2601!2\"3\2474$5%6&7/8(9)0=\337?\264`#' \
qQwWeErRtTzZuUiIoOpP\374\334+* \
aAsSdDfFgGhHjJkKlL\366\326\344\304 \
<>yYxXcCvVbBnNmM,;.:-_ \
")
'("jp106" . "\
\
1!2\"3#4$5%6&7'8(9)0~-=^~\\| \
qQwWeErRtTyYuUiIoOpP@`[{ \
aAsSdDfFgGhHjJkKlL;+:*]} \
zZxXcCvVbBnNmM,<.>/?\\_ \
")
'("pc105-uk" . "\
\
`\2541!2\"3\2434$5%6^7&8*9(0)-_=+ \
qQwWeErRtTyYuUiIoOpP[{]} \
aAsSdDfFgGhHjJkKlL;:'@#~ \
\\|zZxXcCvVbBnNmM,<.>/? \
")
)
"Alist of keyboard names and corresponding layout strings.
See the documentation of `quail-keyboard-layout' for the format of
the layout string.")
(defcustom quail-keyboard-layout quail-keyboard-layout-standard
"A string which represents physical key layout of a particular keyboard.
We assume there are six rows and each row has 15 keys (columns),
the first row is above the `1' - `0' row,
the first column of the second row is left of key `1',
the first column of the third row is left of key `q',
the first column of the fourth row is left of key `a',
the first column of the fifth row is left of key `z',
the sixth row is below the `z' - `/' row.
Nth (N is even) and (N+1)th characters in the string are non-shifted
and shifted characters respectively at the same location.
The location of Nth character is row (N / 30) and column ((N mod 30) / 2).
The command `quail-set-keyboard-layout' usually sets this variable."
:group 'quail
:type `(choice
,@(mapcar (lambda (pair)
(list 'const :tag (car pair) (cdr pair)))
quail-keyboard-layout-alist)
(string :tag "Other")))
;; A non-standard keyboard layout may miss some key locations of the
;; standard layout while having additional key locations not in the
;; standard layout. This alist maps those additional key locations to
;; the missing locations. The value is updated automatically by
;; quail-set-keyboard-layout.
(defvar quail-keyboard-layout-substitution nil)
(defun quail-update-keyboard-layout (kbd-type)
(let ((layout (assoc kbd-type quail-keyboard-layout-alist)))
(if (null layout)
;; Here, we had better ask a user to define his own keyboard
;; layout interactively.
(error "Unknown keyboard type `%s'" kbd-type))
(setq quail-keyboard-layout (cdr layout))
(let ((i quail-keyboard-layout-len)
subst-list missing-list)
;; Sum up additional key locations not in the standard layout in
;; subst-list, and missing key locations in missing-list.
(while (> i 0)
(setq i (1- i))
(if (= (aref quail-keyboard-layout i) ? )
(if (/= (aref quail-keyboard-layout-standard i) ? )
(setq missing-list (cons i missing-list)))
(if (= (aref quail-keyboard-layout-standard i) ? )
(setq subst-list (cons (cons i nil) subst-list)))))
(setq quail-keyboard-layout-substitution subst-list)
;; If there are additional key locations, map them to missing
;; key locations.
(dolist (missing missing-list)
(while (and subst-list (cdr (car subst-list)))
(setq subst-list (cdr subst-list)))
(if subst-list
(setcdr (car subst-list) missing))))))
(defcustom quail-keyboard-layout-type "standard"
"Type of keyboard layout used in Quail base input method.
Available types are listed in the variable `quail-keyboard-layout-alist'."
:group 'quail
:type (cons 'choice (mapcar (lambda (elt)
(list 'const (car elt)))
quail-keyboard-layout-alist))
:set (lambda (symbol value)
(quail-update-keyboard-layout value)
(set symbol value)))
;;;###autoload
(defun quail-set-keyboard-layout (kbd-type)
"Set the current keyboard layout to the same as keyboard KBD-TYPE.
Since some Quail packages depends on a physical layout of keys (not
characters generated by them), those are created by assuming the
standard layout defined in `quail-keyboard-layout-standard'. This
function tells Quail system the layout of your keyboard so that what
you type is correctly handled."
(interactive
(let* ((completion-ignore-case t)
(type (completing-read "Keyboard type: "
quail-keyboard-layout-alist)))
(list type)))
(quail-update-keyboard-layout kbd-type)
(setq quail-keyboard-layout-type kbd-type))
(defun quail-keyboard-translate (char)
"Translate CHAR to the one in the standard keyboard layout."
(if (eq quail-keyboard-layout quail-keyboard-layout-standard)
;; All Quail packages are designed based on
;; `quail-keyboard-layout-standard'.
char
(let ((i 0))
;; Find the key location on the current keyboard layout.
(while (and (< i quail-keyboard-layout-len)
(/= char (aref quail-keyboard-layout i)))
(setq i (1+ i)))
(if (= i quail-keyboard-layout-len)
;; CHAR is not in quail-keyboard-layout, which means that a
;; user typed a key which generated a character code to be
;; handled out of Quail. Just return CHAR and make
;; quail-execute-non-quail-command handle it correctly.
char
(let ((ch (aref quail-keyboard-layout-standard i)))
(if (= ch ?\ )
;; This location not available in the standard keyboard
;; layout. Check if the location is used to substitute
;; for the other location of the standard layout.
(if (setq i (cdr (assq i quail-keyboard-layout-substitution)))
(aref quail-keyboard-layout-standard i)
;; Just return CHAR as well as above.
char)
ch))))))
(defun quail-keyseq-translate (keyseq)
(apply 'string
(mapcar #'quail-keyboard-translate keyseq)))
(defun quail-insert-kbd-layout (kbd-layout)
"Insert the visual keyboard layout table according to KBD-LAYOUT.
The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
(let (done-list layout i ch)
(setq bidi-paragraph-direction 'left-to-right)
;; At first, convert KBD-LAYOUT to the same size vector that
;; contains translated character or string.
(setq layout (string-to-vector kbd-layout)
i 0)
(while (< i quail-keyboard-layout-len)
(setq ch (aref kbd-layout i))
(if (quail-kbd-translate)
(setq ch (quail-keyboard-translate ch)))
(let* ((map (cdr (assq ch (cdr (quail-map)))))
(translation (and map (quail-get-translation
(car map) (char-to-string ch) 1))))
(if translation
(progn
(if (consp translation)
(setq translation
(if (> (length (cdr translation)) 0)
(aref (cdr translation) 0)
" ")))
(setq done-list (cons translation done-list)))
(setq translation (aref kbd-layout i)))
(aset layout i translation))
(setq i (1+ i)))
(let ((pos (point))
(bar (propertize "|" 'face 'bold))
lower upper row)
;; Make table without horizontal lines. Each column for a key
;; has the form "| LU |" where L is for lower key and U is
;; for a upper key. If width of L (U) is greater than 1,
;; preceding (following) space is not inserted.
(setq i 0)
(while (< i quail-keyboard-layout-len)
(when (= (% i 30) 0)
(setq row (/ i 30))
(if (> row 1)
(insert-char 32 (+ row (/ (- row 2) 2)))))
(setq lower (aref layout i)
upper (aref layout (1+ i)))
(insert bar)
(if (< (if (stringp lower) (string-width lower) (char-width lower)) 2)
(insert " "))
(if (characterp lower)
(setq lower
(if (eq (get-char-code-property lower 'general-category) 'Mn)
;; Pad the left and right of non-spacing characters.
(compose-string (string lower) 0 1
(format "\t%c\t" lower))
(string lower))))
(if (characterp upper)
(setq upper
(if (eq (get-char-code-property upper 'general-category) 'Mn)
;; Pad the left and right of non-spacing characters.
(compose-string (string upper) 0 1
(format "\t%c\t" upper))
(string upper))))
(insert (bidi-string-mark-left-to-right lower)
(propertize " " 'invisible t)
(bidi-string-mark-left-to-right upper))
(if (< (string-width upper) 2)
(insert " "))
(setq i (+ i 2))
(if (= (% i 30) 0)
(insert bar "\n")))
;; Insert horizontal lines while deleting blank key columns at the
;; beginning and end of each line.
(save-restriction
(narrow-to-region pos (point))
(goto-char pos)
;;(while (looking-at "[| ]*$")
;;(forward-line 1)
;;(delete-region pos (point)))
(let ((from1 100) (to1 0) from2 to2)
(while (not (eobp))
(if (looking-at "[| \u202c\u202d]*$")
;; The entire row is blank.
(delete-region (point) (match-end 0))
;; Delete blank key columns at the head.
(if (looking-at "\u202d? *\\(| \\)+")
(subst-char-in-region (point) (match-end 0) ?| ? ))
;; Delete blank key columns at the tail.
(if (re-search-forward "\\( |\\)+\u202c?$"
(line-end-position) t)
(delete-region (match-beginning 0) (point)))
(beginning-of-line))
;; Calculate the start and end columns of a horizontal line.
(if (eolp)
(setq from2 from1 to2 to1)
(skip-chars-forward " \u202d")
(setq from2 (current-column))
(end-of-line)
(setq to2 (current-column))
(if (< from2 from1)
(setq from1 from2))
(if (> to2 to1)
(setq to1 to2))
(beginning-of-line))
;; If the previous or the current line has at least one key
;; column, insert a horizontal line.
(when (> to1 0)
(insert-char 32 from1)
(setq pos (point))
(insert "+")
(insert-char ?- (- (- to1 from1) 2))
(insert "+")
(put-text-property pos (point) 'face 'bold)
(insert "\n"))
(setq from1 from2 to1 to2)
(forward-line 1)))
;; Insert "space bar" box.
(forward-line -1)
(setq pos (point))
(insert
" +-----------------------------+
| space bar |
+-----------------------------+
")
(put-text-property pos (point) 'face 'bold)
(insert ?\n)))
done-list))
;;;###autoload
(defun quail-show-keyboard-layout (&optional keyboard-type)
"Show the physical layout of the keyboard type KEYBOARD-TYPE.
The variable `quail-keyboard-layout-type' holds the currently selected
keyboard type."
(interactive
(list (completing-read (format-prompt "Keyboard type" "current choice")
quail-keyboard-layout-alist
nil t)))
(or (and keyboard-type (> (length keyboard-type) 0))
(setq keyboard-type quail-keyboard-layout-type))
(let ((layout (assoc keyboard-type quail-keyboard-layout-alist)))
(or layout
(error "Unknown keyboard type: %s" keyboard-type))
(with-output-to-temp-buffer "*Help*"
(with-current-buffer standard-output
(insert "Keyboard layout (keyboard type: "
keyboard-type
")\n")
(quail-insert-kbd-layout (cdr layout))))))
;; Quail map
(defsubst quail-map-p (object)
"Return t if OBJECT is a Quail map.
A Quail map holds information how a particular key should be translated.
Its format is (TRANSLATION . ALIST).
TRANSLATION is either a character, or a cons (INDEX . VECTOR).
In the latter case, each element of VECTOR is a candidate for the translation,
and INDEX points the currently selected translation.
ALIST is normally a list of elements that look like (CHAR . DEFN),
where DEFN is another Quail map for a longer key (CHAR added to the
current key). It may also be a symbol of a function which returns an
alist of the above format.
Just after a Quail package is read, TRANSLATION may be a string or a
vector. Then each element of the string or vector is a candidate for
the translation. These objects are transformed to cons cells in the
format \(INDEX . VECTOR), as described above."
(and (consp object)
(let ((translation (car object)))
(or (integerp translation) (null translation)
(vectorp translation) (stringp translation)
(symbolp translation)
(and (consp translation) (not (vectorp (cdr translation))))))
(let ((alist (cdr object)))
(or (and (listp alist) (consp (car alist)))
(symbolp alist)))))
;;;###autoload
(defmacro quail-define-rules (&rest rules)
"Define translation rules of the current Quail package.
Each argument is a list of KEY and TRANSLATION.
KEY is a string meaning a sequence of keystrokes to be translated.
TRANSLATION is a character, a string, a vector, a Quail map, or a function.
If it is a character, it is the sole translation of KEY.
If it is a string, each character is a candidate for the translation.
If it is a vector, each element (string or character) is a candidate
for the translation.
In these cases, a key specific Quail map is generated and assigned to KEY.
If TRANSLATION is a Quail map or a function symbol which returns a Quail map,
it is used to handle KEY.
The first argument may be an alist of annotations for the following
rules. Each element has the form (ANNOTATION . VALUE), where
ANNOTATION is a symbol indicating the annotation type. Currently
the following annotation types are supported.
append -- the value non-nil means that the following rules should
be appended to the rules of the current Quail package.
face -- the value is a face to use for displaying TRANSLATIONs in
candidate list.
advice -- the value is a function to call after one of RULES is
selected. The function is called with one argument, the
selected TRANSLATION string, after the TRANSLATION is
inserted.
no-decode-map --- the value non-nil means that decoding map is not
generated for the following translations."
(let ((l rules)
append no-decode-map props)
;; If the first argument is an alist of annotations, handle them.
(if (consp (car (car l)))
(let ((annotations (car l)))
(setq append (assq 'append annotations))
(if append
(setq annotations (delete append annotations)
append (cdr append)))
(setq no-decode-map (assq 'no-decode-map annotations))
(if no-decode-map
(setq annotations (delete no-decode-map annotations)
no-decode-map (cdr no-decode-map)))