1 ;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2002 Free Software Foundation, Inc.
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H13PRO009
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
31 ;; Usually this map is empty (even if Encoded-kbd mode is on), but if
32 ;; the keyboard coding system is iso-2022-based, it defines dummy key
33 ;; bindings for ESC $ ..., etc. so that those bindings in
34 ;; key-translation-map take effect.
35 (defconst encoded-kbd-mode-map
(make-sparse-keymap)
36 "Keymap for Encoded-kbd minor mode.")
38 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
40 (defvar encoded-kbd-iso2022-esc-map
41 (let ((map (make-sparse-keymap)))
42 (define-key map
"$" 'encoded-kbd-iso2022-esc-dollar-prefix
)
43 (define-key map
"(" 'encoded-kbd-iso2022-designation-prefix
)
44 (define-key map
")" 'encoded-kbd-iso2022-designation-prefix
)
45 (define-key map
"," 'encoded-kbd-iso2022-designation-prefix
)
46 (define-key map
"-" 'encoded-kbd-iso2022-designation-prefix
)
48 "Keymap for handling ESC code in Encoded-kbd mode.")
49 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map
)
51 (defvar encoded-kbd-iso2022-esc-dollar-map
52 (let ((map (make-sparse-keymap)))
53 (define-key map
"(" 'encoded-kbd-iso2022-designation-prefix
)
54 (define-key map
")" 'encoded-kbd-iso2022-designation-prefix
)
55 (define-key map
"," 'encoded-kbd-iso2022-designation-prefix
)
56 (define-key map
"-" 'encoded-kbd-iso2022-designation-prefix
)
57 (define-key map
"@" 'encoded-kbd-iso2022-designation
)
58 (define-key map
"A" 'encoded-kbd-iso2022-designation
)
59 (define-key map
"B" 'encoded-kbd-iso2022-designation
)
61 "Keymap for handling ESC $ sequence in Encoded-kbd mode.")
62 (fset 'encoded-kbd-iso2022-esc-dollar-prefix
63 encoded-kbd-iso2022-esc-dollar-map
)
65 (defvar encoded-kbd-iso2022-designation-map
66 (let ((map (make-sparse-keymap))
70 (setq final-char
(charset-iso-final-char (car l
)))
72 (define-key map
(char-to-string final-char
)
73 'encoded-kbd-iso2022-designation
))
76 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
77 (fset 'encoded-kbd-iso2022-designation-prefix
78 encoded-kbd-iso2022-designation-map
)
80 ;; Keep information of designation state of ISO2022 encoding. When
81 ;; Encoded-kbd mode is on, this is set to a vector of length 4, the
82 ;; elements are character sets currently designated to graphic
83 ;; registers 0 thru 3.
85 (defvar encoded-kbd-iso2022-designations nil
)
86 (put 'encoded-kbd-iso2022-designations
'permanent-local t
)
88 ;; Keep information of invocation state of ISO2022 encoding. When
89 ;; Encoded-kbd mode is on, this is set to a vector of length 3,
90 ;; graphic register numbers currently invoked to graphic plane 1 and
91 ;; 2, and a single shifted graphic register number.
93 (defvar encoded-kbd-iso2022-invocations nil
)
94 (put 'encoded-kbd-iso2022-invocations
'permanent-local t
)
96 (defsubst encoded-kbd-last-key
()
97 (let ((keys (this-single-command-keys)))
98 (aref keys
(1- (length keys
)))))
100 (defun encoded-kbd-iso2022-designation (ignore)
101 "Do ISO2022 designation according to the current key in Encoded-kbd mode.
102 The following key sequence may cause multilingual text insertion."
103 (let ((key-seq (this-single-command-keys))
104 (prev-g0-charset (aref encoded-kbd-iso2022-designations
105 (aref encoded-kbd-iso2022-invocations
0)))
106 intermediate-char final-char
107 reg dimension chars charset
)
108 (if (= (length key-seq
) 4)
109 ;; ESC $ <intermediate-char> <final-char>
110 (setq intermediate-char
(aref key-seq
2)
112 chars
(if (< intermediate-char ?
,) 94 96)
113 final-char
(aref key-seq
3)
114 reg
(mod intermediate-char
4))
115 (if (= (aref key-seq
1) ?$
)
116 ;; ESC $ <final-char>
119 final-char
(aref key-seq
2)
121 ;; ESC <intermediate-char> <final-char>
122 (setq intermediate-char
(aref key-seq
1)
124 chars
(if (< intermediate-char ?
,) 94 96)
125 final-char
(aref key-seq
2)
126 reg
(mod intermediate-char
4))))
127 (aset encoded-kbd-iso2022-designations reg
128 (iso-charset dimension chars final-char
)))
131 (defun encoded-kbd-iso2022-single-shift (ignore)
132 (let ((char (encoded-kbd-last-key)))
133 (aset encoded-kbd-iso2022-invocations
2
134 (aref encoded-kbd-iso2022-designations
135 (if (= char ?
\216) 2 3))))
138 (defun encoded-kbd-self-insert-iso2022-7bit (ignore)
139 (let ((char (encoded-kbd-last-key))
140 (charset (aref encoded-kbd-iso2022-designations
141 (or (aref encoded-kbd-iso2022-invocations
2)
142 (aref encoded-kbd-iso2022-invocations
0)))))
143 (aset encoded-kbd-iso2022-invocations
2 nil
)
144 (vector (if (= (charset-dimension charset
) 1)
145 (make-char charset char
)
146 (make-char charset char
(read-char-exclusive))))))
148 (defun encoded-kbd-self-insert-iso2022-8bit (ignore)
149 (let ((char (encoded-kbd-last-key))
150 (charset (aref encoded-kbd-iso2022-designations
151 (or (aref encoded-kbd-iso2022-invocations
2)
152 (aref encoded-kbd-iso2022-invocations
1)))))
153 (aset encoded-kbd-iso2022-invocations
2 nil
)
154 (vector (if (= (charset-dimension charset
) 1)
155 (make-char charset char
)
156 (make-char charset char
(read-char-exclusive))))))
158 (defun encoded-kbd-self-insert-sjis (ignore)
159 (let ((char (encoded-kbd-last-key)))
161 (if (or (< char ?
\xA0) (>= char ?
\xE0))
162 (decode-sjis-char (+ (ash char
8) (read-char-exclusive)))
163 (make-char 'katakana-jisx0201 char
)))))
165 (defun encoded-kbd-self-insert-big5 (ignore)
166 (let ((char (encoded-kbd-last-key)))
168 (decode-big5-char (+ (ash char
8) (read-char-exclusive))))))
170 (defun encoded-kbd-self-insert-ccl (ignore)
171 (let ((str (char-to-string (encoded-kbd-last-key)))
172 (ccl (coding-system-get (keyboard-coding-system) :ccl-decoder
))
173 (vec [nil nil nil nil nil nil nil nil nil
])
175 (while (= (length (setq result
(ccl-execute-on-string ccl vec str t
))) 0)
176 (dotimes (i 9) (aset vec i nil
))
177 (setq str
(format "%s%c" str
(read-char-exclusive))))
178 (vector (aref result
0))))
180 (defun encoded-kbd-self-insert-charset (ignore)
182 (coding-system-get (keyboard-coding-system) :charset-list
))
183 (charset (car charset-list
))
184 ;; For the moment, we can assume that the length of CHARSET-LIST
185 ;; is 1, and the dimension of CHARSET is 1.
186 (char (encoded-kbd-last-key)))
187 (vector (or (decode-char charset char
) char
))))
189 (defun encoded-kbd-self-insert-utf-8 (arg)
191 (let ((char (encoded-kbd-last-key))
194 (setq len
1 char
(logand char
#x1F
)))
196 (setq len
2 char
(logand char
#x0F
)))
198 (setq len
3 char
(logand char
#x07
)))
200 (setq len
4 char
0)))
202 (setq char
(logior (lsh char
6) (logand (read-char-exclusive) #x3F
))
206 (defun encoded-kbd-setup-keymap (coding)
207 ;; At first, reset the keymap.
208 (define-key encoded-kbd-mode-map
"\e" nil
)
209 ;; Then setup the keymap according to the keyboard coding system.
211 ((eq (coding-system-type coding
) 'shift-jis
)
214 (define-key key-translation-map
215 (vector i
) 'encoded-kbd-self-insert-sjis
)
219 ((eq (coding-system-type coding
) 'charset
)
220 (let* ((charset (car (coding-system-get coding
:charset-list
)))
221 (code-space (get-charset-property charset
:code-space
))
222 (from (max (aref code-space
0) 128))
223 (to (aref code-space
1)))
225 (define-key key-translation-map
226 (vector from
) 'encoded-kbd-self-insert-charset
)
227 (setq from
(1+ from
))))
230 ((eq (coding-system-type coding
) 'iso-2022
)
231 (let ((flags (coding-system-get coding
:flags
))
232 (designation (coding-system-get coding
:designation
)))
233 (if (memq 'locking-shift flags
)
234 nil
; Don't support locking-shift.
235 (setq encoded-kbd-iso2022-designations
(make-vector 4 nil
)
236 encoded-kbd-iso2022-invocations
(make-vector 3 nil
))
238 (if (aref designation i
)
239 (if (charsetp (aref designation i
))
240 (aset encoded-kbd-iso2022-designations
241 i
(aref designation i
))
242 (if (charsetp (car-safe (aref designation i
)))
243 (aset encoded-kbd-iso2022-designations
244 i
(car (aref designation i
)))))))
245 (aset encoded-kbd-iso2022-invocations
0 0)
246 (if (aref encoded-kbd-iso2022-designations
1)
247 (aset encoded-kbd-iso2022-invocations
1 1))
248 (when (memq 'designation flags
)
249 (define-key encoded-kbd-mode-map
"\e" 'encoded-kbd-iso2022-esc-prefix
)
250 (define-key key-translation-map
"\e" 'encoded-kbd-iso2022-esc-prefix
))
251 (when (or (aref designation
2) (aref designation
3))
252 (define-key key-translation-map
253 [?
\216] 'encoded-kbd-iso2022-single-shift
)
254 (define-key key-translation-map
255 [?
\217] 'encoded-kbd-iso2022-single-shift
))
256 (or (eq (aref designation
0) 'ascii
)
258 (define-key key-translation-map
259 (vector (+ 32 i
)) 'encoded-kbd-self-insert-iso2022-7bit
)))
260 (if (memq '7-bit flags
)
263 (define-key key-translation-map
264 (vector (+ 160 i
)) 'encoded-kbd-self-insert-iso2022-8bit
))
267 ((eq (coding-system-type coding
) 4) ; CCL-base
268 (let ((valid-codes (or (coding-system-get coding
:valid
)
272 (setq elt
(car valid-codes
) valid-codes
(cdr valid-codes
))
274 (setq from
(car elt
) to
(cdr elt
))
275 (setq from
(setq to elt
)))
278 (define-key key-translation-map
279 (vector from
) 'encoded-kbd-self-insert-ccl
))
280 (setq from
(1+ from
))))
283 ((eq (coding-system-type coding
) 'utf-8
)
286 (define-key key-translation-map
287 (vector i
) 'encoded-kbd-self-insert-utf-8
)
293 ;; key-translation-map at the time Encoded-kbd mode is turned on is
295 (defvar saved-key-translation-map nil
)
297 ;; Input mode at the time Encoded-kbd mode is turned on is saved here.
298 (defvar saved-input-mode nil
)
300 (put 'encoded-kbd-mode
'permanent-local t
)
302 (define-minor-mode encoded-kbd-mode
303 "Toggle Encoded-kbd minor mode.
304 With arg, turn Encoded-kbd mode on if and only if arg is positive.
306 You should not turn this mode on manually, instead use the command
307 \\[set-keyboard-coding-system] which turns on or off this mode
310 In Encoded-kbd mode, a text sent from keyboard is accepted
311 as a multilingual text encoded in a coding system set by
312 \\[set-keyboard-coding-system]."
316 ;; We are turning on Encoded-kbd mode.
317 (let ((coding (keyboard-coding-system))
319 (or saved-key-translation-map
320 (if (keymapp key-translation-map
)
321 (setq saved-key-translation-map
322 (copy-keymap key-translation-map
))
323 (setq key-translation-map
(make-sparse-keymap))))
325 (setq saved-input-mode
326 (current-input-mode)))
327 (setq result
(and coding
(encoded-kbd-setup-keymap coding
)))
331 (nth 0 saved-input-mode
)
332 (nth 1 saved-input-mode
)
334 (nth 3 saved-input-mode
)))
335 (setq encoded-kbd-mode nil
336 saved-key-translation-map nil
337 saved-input-mode nil
)
338 (error "Unsupported coding system in Encoded-kbd mode: %S"
341 ;; We are turning off Encoded-kbd mode.
342 (setq key-translation-map saved-key-translation-map
343 saved-key-translation-map nil
)
344 (apply 'set-input-mode saved-input-mode
)
345 (setq saved-input-mode nil
)))
347 (provide 'encoded-kb
)
349 ;;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
350 ;;; encoded-kb.el ends here