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 (if (= char ?
\216) 2 3)))
136 (defun encoded-kbd-self-insert-iso2022-7bit (ignore)
137 (let ((char (encoded-kbd-last-key))
138 (charset (aref encoded-kbd-iso2022-designations
139 (or (aref encoded-kbd-iso2022-invocations
2)
140 (aref encoded-kbd-iso2022-invocations
0)))))
141 (aset encoded-kbd-iso2022-invocations
2 nil
)
142 (vector (if (= (charset-dimension charset
) 1)
143 (make-char charset char
)
144 (make-char charset char
(read-char-exclusive))))))
146 (defun encoded-kbd-self-insert-iso2022-8bit (ignore)
147 (let ((char (encoded-kbd-last-key))
148 (charset (aref encoded-kbd-iso2022-designations
149 (or (aref encoded-kbd-iso2022-invocations
2)
150 (aref encoded-kbd-iso2022-invocations
1)))))
151 (aset encoded-kbd-iso2022-invocations
2 nil
)
152 (vector (if (= (charset-dimension charset
) 1)
153 (make-char charset char
)
154 (make-char charset char
(read-char-exclusive))))))
156 (defun encoded-kbd-self-insert-sjis (ignore)
157 (let ((char (encoded-kbd-last-key)))
159 (if (or (< char ?
\xA0) (>= char ?
\xE0))
160 (decode-sjis-char (+ (ash char
8) (read-char-exclusive)))
161 (make-char 'katakana-jisx0201 char
)))))
163 (defun encoded-kbd-self-insert-big5 (ignore)
164 (let ((char (encoded-kbd-last-key)))
166 (decode-big5-char (+ (ash char
8) (read-char-exclusive))))))
168 (defun encoded-kbd-self-insert-ccl (ignore)
169 (let ((str (char-to-string (encoded-kbd-last-key)))
170 (ccl (coding-system-get (keyboard-coding-system) :ccl-decoder
))
171 (vec [nil nil nil nil nil nil nil nil nil
])
173 (while (= (length (setq result
(ccl-execute-on-string ccl vec str t
))) 0)
174 (dotimes (i 9) (aset vec i nil
))
175 (setq str
(format "%s%c" str
(read-char-exclusive))))
176 (vector (aref result
0))))
179 ;; Decode list of codes in CODE-LIST by CHARSET and return the decoded
180 ;; characters. If CODE-LIST is too short for the dimension of
181 ;; CHARSET, read new codes and append them to the tail of CODE-LIST.
182 ;; Return nil if CODE-LIST can't be decoded.
184 (defun encoded-kbd-decode-code-list (charset code-list
)
185 (let ((dimension (charset-dimension charset
))
187 (while (> dimension
(length code-list
))
188 (nconc code-list
(list (read-char-exclusive))))
189 (setq code
(car code-list
))
191 (decode-char charset code
)
192 (setq code-list
(cdr code-list
)
193 code
(logior (lsh code
8) (car code-list
)))
195 (decode-char charset code
)
196 (setq code-list
(cdr code-list
)
197 code
(logior (lsh code
8) (car code-list
)))
199 (decode-char charset code
)
200 ;; As Emacs can't handle full 32-bit integer, we must give a
201 ;; cons of higher and lower 16-bit codes to decode-char.
202 (setq code
(cons (lsh code -
8)
203 (logior (lsh (car code-list
) 8) (cadr code-list
))))
204 (decode-char charset code
))))))
206 (defun encoded-kbd-self-insert-charset (ignore)
208 (coding-system-get (keyboard-coding-system) :charset-list
))
209 (code-list (list (encoded-kbd-last-key)))
211 (while (and charset-list
(not char
))
212 (setq char
(encoded-kbd-decode-code-list (car charset-list
) code-list
)
213 charset-list
(cdr charset-list
)))
216 (setq unread-command-events
(cdr code-list
))
217 (vector (car code-list
)))))
219 (defun encoded-kbd-self-insert-utf-8 (arg)
221 (let ((char (encoded-kbd-last-key))
224 (setq len
1 char
(logand char
#x1F
)))
226 (setq len
2 char
(logand char
#x0F
)))
228 (setq len
3 char
(logand char
#x07
)))
230 (setq len
4 char
0)))
232 (setq char
(logior (lsh char
6) (logand (read-char-exclusive) #x3F
))
236 (defun encoded-kbd-setup-keymap (coding)
237 ;; At first, reset the keymap.
238 (define-key encoded-kbd-mode-map
"\e" nil
)
239 ;; Then setup the keymap according to the keyboard coding system.
241 ((eq (coding-system-type coding
) 'shift-jis
)
244 (define-key key-translation-map
245 (vector i
) 'encoded-kbd-self-insert-sjis
)
249 ((eq (coding-system-type coding
) 'charset
)
252 (let ((dim (charset-dimension x
))
253 (code-space (get-charset-property x
:code-space
)))
254 (cons (aref code-space
(* (1- dim
) 2))
255 (aref code-space
(1+ (* (1- dim
) 2))))))
256 (coding-system-get coding
:charset-list
)))
257 (let ((from (max (car elt
) 128))
260 (define-key key-translation-map
261 (vector from
) 'encoded-kbd-self-insert-charset
)
262 (setq from
(1+ from
)))))
265 ((eq (coding-system-type coding
) 'iso-2022
)
266 (let ((flags (coding-system-get coding
:flags
))
267 (designation (coding-system-get coding
:designation
)))
268 (if (memq 'locking-shift flags
)
269 nil
; Don't support locking-shift.
270 (setq encoded-kbd-iso2022-designations
(make-vector 4 nil
)
271 encoded-kbd-iso2022-invocations
(make-vector 3 nil
))
273 (if (aref designation i
)
274 (if (charsetp (aref designation i
))
275 (aset encoded-kbd-iso2022-designations
276 i
(aref designation i
))
277 (if (charsetp (car-safe (aref designation i
)))
278 (aset encoded-kbd-iso2022-designations
279 i
(car (aref designation i
)))))))
280 (aset encoded-kbd-iso2022-invocations
0 0)
281 (if (aref encoded-kbd-iso2022-designations
1)
282 (aset encoded-kbd-iso2022-invocations
1 1))
283 (when (memq 'designation flags
)
284 (define-key encoded-kbd-mode-map
"\e" 'encoded-kbd-iso2022-esc-prefix
)
285 (define-key key-translation-map
"\e" 'encoded-kbd-iso2022-esc-prefix
))
286 (when (or (aref designation
2) (aref designation
3))
287 (define-key key-translation-map
288 [?
\216] 'encoded-kbd-iso2022-single-shift
)
289 (define-key key-translation-map
290 [?
\217] 'encoded-kbd-iso2022-single-shift
))
291 (or (eq (aref designation
0) 'ascii
)
293 (define-key key-translation-map
294 (vector (+ 32 i
)) 'encoded-kbd-self-insert-iso2022-7bit
)))
295 (if (memq '7-bit flags
)
298 (define-key key-translation-map
299 (vector (+ 160 i
)) 'encoded-kbd-self-insert-iso2022-8bit
))
302 ((eq (coding-system-type coding
) 4) ; CCL-base
303 (let ((valid-codes (or (coding-system-get coding
:valid
)
307 (setq elt
(car valid-codes
) valid-codes
(cdr valid-codes
))
309 (setq from
(car elt
) to
(cdr elt
))
310 (setq from
(setq to elt
)))
313 (define-key key-translation-map
314 (vector from
) 'encoded-kbd-self-insert-ccl
))
315 (setq from
(1+ from
))))
318 ((eq (coding-system-type coding
) 'utf-8
)
321 (define-key key-translation-map
322 (vector i
) 'encoded-kbd-self-insert-utf-8
)
329 ;; key-translation-map at the time Encoded-kbd mode is turned on is
331 (defvar saved-key-translation-map nil
)
333 ;; Input mode at the time Encoded-kbd mode is turned on is saved here.
334 (defvar saved-input-mode nil
)
336 (put 'encoded-kbd-mode
'permanent-local t
)
338 (define-minor-mode encoded-kbd-mode
339 "Toggle Encoded-kbd minor mode.
340 With arg, turn Encoded-kbd mode on if and only if arg is positive.
342 You should not turn this mode on manually, instead use the command
343 \\[set-keyboard-coding-system] which turns on or off this mode
346 In Encoded-kbd mode, a text sent from keyboard is accepted
347 as a multilingual text encoded in a coding system set by
348 \\[set-keyboard-coding-system]."
352 ;; We are turning on Encoded-kbd mode.
353 (let ((coding (keyboard-coding-system))
355 (or saved-key-translation-map
356 (if (keymapp key-translation-map
)
357 (setq saved-key-translation-map
358 (copy-keymap key-translation-map
))
359 (setq key-translation-map
(make-sparse-keymap))))
361 (setq saved-input-mode
362 (current-input-mode)))
363 (setq result
(and coding
(encoded-kbd-setup-keymap coding
)))
367 (nth 0 saved-input-mode
)
368 (nth 1 saved-input-mode
)
370 (nth 3 saved-input-mode
)))
371 (setq encoded-kbd-mode nil
372 saved-key-translation-map nil
373 saved-input-mode nil
)
374 (error "Unsupported coding system in Encoded-kbd mode: %S"
377 ;; We are turning off Encoded-kbd mode.
378 (setq key-translation-map saved-key-translation-map
379 saved-key-translation-map nil
)
380 (apply 'set-input-mode saved-input-mode
)
381 (setq saved-input-mode nil
)))
383 (provide 'encoded-kb
)
385 ;;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
386 ;;; encoded-kb.el ends here