1 ;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; National Institute of Advanced Industrial Science and Technology (AIST)
11 ;; Registration Number H13PRO009
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
34 ;; Usually this map is empty (even if Encoded-kbd mode is on), but if
35 ;; the keyboard coding system is iso-2022-based, it defines dummy key
36 ;; bindings for ESC $ ..., etc. so that those bindings in
37 ;; key-translation-map take effect.
38 (defconst encoded-kbd-mode-map
(make-sparse-keymap)
39 "Keymap for Encoded-kbd minor mode.")
41 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
43 (defvar encoded-kbd-iso2022-esc-map
44 (let ((map (make-sparse-keymap)))
45 (define-key map
"$" 'encoded-kbd-iso2022-esc-dollar-prefix
)
46 (define-key map
"(" 'encoded-kbd-iso2022-designation-prefix
)
47 (define-key map
")" 'encoded-kbd-iso2022-designation-prefix
)
48 (define-key map
"," 'encoded-kbd-iso2022-designation-prefix
)
49 (define-key map
"-" 'encoded-kbd-iso2022-designation-prefix
)
51 "Keymap for handling ESC code in Encoded-kbd mode.")
52 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map
)
54 (defvar encoded-kbd-iso2022-esc-dollar-map
55 (let ((map (make-sparse-keymap)))
56 (define-key map
"(" 'encoded-kbd-iso2022-designation-prefix
)
57 (define-key map
")" 'encoded-kbd-iso2022-designation-prefix
)
58 (define-key map
"," 'encoded-kbd-iso2022-designation-prefix
)
59 (define-key map
"-" 'encoded-kbd-iso2022-designation-prefix
)
60 (define-key map
"@" 'encoded-kbd-iso2022-designation
)
61 (define-key map
"A" 'encoded-kbd-iso2022-designation
)
62 (define-key map
"B" 'encoded-kbd-iso2022-designation
)
64 "Keymap for handling ESC $ sequence in Encoded-kbd mode.")
65 (fset 'encoded-kbd-iso2022-esc-dollar-prefix
66 encoded-kbd-iso2022-esc-dollar-map
)
68 (defvar encoded-kbd-iso2022-designation-map
69 (let ((map (make-sparse-keymap))
73 (setq final-char
(charset-iso-final-char (car l
)))
75 (define-key map
(char-to-string final-char
)
76 'encoded-kbd-iso2022-designation
))
79 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
80 (fset 'encoded-kbd-iso2022-designation-prefix
81 encoded-kbd-iso2022-designation-map
)
83 ;; Keep information of designation state of ISO2022 encoding. When
84 ;; Encoded-kbd mode is on, this is set to a vector of length 4, the
85 ;; elements are character sets currently designated to graphic
86 ;; registers 0 thru 3.
88 (defvar encoded-kbd-iso2022-designations nil
)
89 (put 'encoded-kbd-iso2022-designations
'permanent-local t
)
91 ;; Keep information of invocation state of ISO2022 encoding. When
92 ;; Encoded-kbd mode is on, this is set to a vector of length 3,
93 ;; graphic register numbers currently invoked to graphic plane 1 and
94 ;; 2, and a single shifted graphic register number.
96 (defvar encoded-kbd-iso2022-invocations nil
)
97 (put 'encoded-kbd-iso2022-invocations
'permanent-local t
)
99 (defsubst encoded-kbd-last-key
()
100 (let ((keys (this-single-command-keys)))
101 (aref keys
(1- (length keys
)))))
103 (defun encoded-kbd-iso2022-designation (ignore)
104 "Do ISO2022 designation according to the current key in Encoded-kbd mode.
105 The following key sequence may cause multilingual text insertion."
106 (let ((key-seq (this-single-command-keys))
107 (prev-g0-charset (aref encoded-kbd-iso2022-designations
108 (aref encoded-kbd-iso2022-invocations
0)))
109 intermediate-char final-char
110 reg dimension chars charset
)
111 (if (= (length key-seq
) 4)
112 ;; ESC $ <intermediate-char> <final-char>
113 (setq intermediate-char
(aref key-seq
2)
115 chars
(if (< intermediate-char ?
,) 94 96)
116 final-char
(aref key-seq
3)
117 reg
(mod intermediate-char
4))
118 (if (= (aref key-seq
1) ?$
)
119 ;; ESC $ <final-char>
122 final-char
(aref key-seq
2)
124 ;; ESC <intermediate-char> <final-char>
125 (setq intermediate-char
(aref key-seq
1)
127 chars
(if (< intermediate-char ?
,) 94 96)
128 final-char
(aref key-seq
2)
129 reg
(mod intermediate-char
4))))
130 (aset encoded-kbd-iso2022-designations reg
131 (iso-charset dimension chars final-char
)))
134 (defun encoded-kbd-iso2022-single-shift (ignore)
135 (let ((char (encoded-kbd-last-key)))
136 (aset encoded-kbd-iso2022-invocations
2 (if (= char ?
\216) 2 3)))
139 (defun encoded-kbd-self-insert-iso2022-7bit (ignore)
140 (let ((char (encoded-kbd-last-key))
141 (charset (aref encoded-kbd-iso2022-designations
142 (or (aref encoded-kbd-iso2022-invocations
2)
143 (aref encoded-kbd-iso2022-invocations
0)))))
144 (aset encoded-kbd-iso2022-invocations
2 nil
)
145 (vector (if (= (charset-dimension charset
) 1)
146 (make-char charset char
)
147 (make-char charset char
(read-char-exclusive))))))
149 (defun encoded-kbd-self-insert-iso2022-8bit (ignore)
150 (let ((char (encoded-kbd-last-key))
151 (charset (aref encoded-kbd-iso2022-designations
152 (or (aref encoded-kbd-iso2022-invocations
2)
153 (aref encoded-kbd-iso2022-invocations
1)))))
154 (aset encoded-kbd-iso2022-invocations
2 nil
)
155 (vector (if (= (charset-dimension charset
) 1)
156 (make-char charset char
)
157 (make-char charset char
(read-char-exclusive))))))
159 (defun encoded-kbd-self-insert-sjis (ignore)
160 (let ((char (encoded-kbd-last-key)))
162 (if (or (< char ?
\xA0) (>= char ?
\xE0))
163 (decode-sjis-char (+ (ash char
8) (read-char-exclusive)))
164 (make-char 'katakana-jisx0201 char
)))))
166 (defun encoded-kbd-self-insert-big5 (ignore)
167 (let ((char (encoded-kbd-last-key)))
169 (decode-big5-char (+ (ash char
8) (read-char-exclusive))))))
171 (defun encoded-kbd-self-insert-ccl (ignore)
172 (let ((str (char-to-string (encoded-kbd-last-key)))
173 (ccl (coding-system-get (keyboard-coding-system) :ccl-decoder
))
174 (vec [nil nil nil nil nil nil nil nil nil
])
176 (while (= (length (setq result
(ccl-execute-on-string ccl vec str t
))) 0)
177 (dotimes (i 9) (aset vec i nil
))
178 (setq str
(format "%s%c" str
(read-char-exclusive))))
179 (vector (aref result
0))))
182 ;; Decode list of codes in CODE-LIST by CHARSET and return the decoded
183 ;; characters. If CODE-LIST is too short for the dimension of
184 ;; CHARSET, read new codes and append them to the tail of CODE-LIST.
185 ;; Return nil if CODE-LIST can't be decoded.
187 (defun encoded-kbd-decode-code-list (charset code-list
)
188 (let ((dimension (charset-dimension charset
))
190 (while (> dimension
(length code-list
))
191 (nconc code-list
(list (read-char-exclusive))))
192 (setq code
(car code-list
))
194 (decode-char charset code
)
195 (setq code-list
(cdr code-list
)
196 code
(logior (lsh code
8) (car code-list
)))
198 (decode-char charset code
)
199 (setq code-list
(cdr code-list
)
200 code
(logior (lsh code
8) (car code-list
)))
202 (decode-char charset code
)
203 ;; As Emacs can't handle full 32-bit integer, we must give a
204 ;; cons of higher and lower 16-bit codes to decode-char.
205 (setq code
(cons (lsh code -
8)
206 (logior (lsh (car code-list
) 8) (cadr code-list
))))
207 (decode-char charset code
))))))
209 (defun encoded-kbd-self-insert-charset (ignore)
211 (coding-system-get (keyboard-coding-system) :charset-list
))
212 (code-list (list (encoded-kbd-last-key)))
214 (while (and charset-list
(not char
))
215 (setq char
(encoded-kbd-decode-code-list (car charset-list
) code-list
)
216 charset-list
(cdr charset-list
)))
219 (setq unread-command-events
(cdr code-list
))
220 (vector (car code-list
)))))
222 (defun encoded-kbd-self-insert-utf-8 (arg)
224 (let ((char (encoded-kbd-last-key))
227 (setq len
1 char
(logand char
#x1F
)))
229 (setq len
2 char
(logand char
#x0F
)))
231 (setq len
3 char
(logand char
#x07
)))
233 (setq len
4 char
0)))
235 (setq char
(logior (lsh char
6) (logand (read-char-exclusive) #x3F
))
239 (defun encoded-kbd-setup-keymap (coding)
240 ;; At first, reset the keymap.
241 (define-key encoded-kbd-mode-map
"\e" nil
)
242 ;; Then setup the keymap according to the keyboard coding system.
244 ((eq (coding-system-type coding
) 'shift-jis
)
247 (define-key key-translation-map
248 (vector i
) 'encoded-kbd-self-insert-sjis
)
252 ((eq (coding-system-type coding
) 'charset
)
255 (let ((dim (charset-dimension x
))
256 (code-space (get-charset-property x
:code-space
)))
257 (cons (aref code-space
(* (1- dim
) 2))
258 (aref code-space
(1+ (* (1- dim
) 2))))))
259 (coding-system-get coding
:charset-list
)))
260 (let ((from (max (car elt
) 128))
263 (define-key key-translation-map
264 (vector from
) 'encoded-kbd-self-insert-charset
)
265 (setq from
(1+ from
)))))
268 ((eq (coding-system-type coding
) 'iso-2022
)
269 (let ((flags (coding-system-get coding
:flags
))
270 (designation (coding-system-get coding
:designation
)))
271 (if (memq 'locking-shift flags
)
272 nil
; Don't support locking-shift.
273 (setq encoded-kbd-iso2022-designations
(make-vector 4 nil
)
274 encoded-kbd-iso2022-invocations
(make-vector 3 nil
))
276 (if (aref designation i
)
277 (if (charsetp (aref designation i
))
278 (aset encoded-kbd-iso2022-designations
279 i
(aref designation i
))
280 (if (charsetp (car-safe (aref designation i
)))
281 (aset encoded-kbd-iso2022-designations
282 i
(car (aref designation i
)))))))
283 (aset encoded-kbd-iso2022-invocations
0 0)
284 (if (aref encoded-kbd-iso2022-designations
1)
285 (aset encoded-kbd-iso2022-invocations
1 1))
286 (when (memq 'designation flags
)
287 (define-key encoded-kbd-mode-map
"\e" 'encoded-kbd-iso2022-esc-prefix
)
288 (define-key key-translation-map
"\e" 'encoded-kbd-iso2022-esc-prefix
))
289 (when (or (aref designation
2) (aref designation
3))
290 (define-key key-translation-map
291 [?
\216] 'encoded-kbd-iso2022-single-shift
)
292 (define-key key-translation-map
293 [?
\217] 'encoded-kbd-iso2022-single-shift
))
294 (or (eq (aref designation
0) 'ascii
)
296 (define-key key-translation-map
297 (vector (+ 32 i
)) 'encoded-kbd-self-insert-iso2022-7bit
)))
298 (if (memq '7-bit flags
)
301 (define-key key-translation-map
302 (vector (+ 160 i
)) 'encoded-kbd-self-insert-iso2022-8bit
))
305 ((eq (coding-system-type coding
) 4) ; CCL-base
306 (let ((valid-codes (or (coding-system-get coding
:valid
)
310 (setq elt
(car valid-codes
) valid-codes
(cdr valid-codes
))
312 (setq from
(car elt
) to
(cdr elt
))
313 (setq from
(setq to elt
)))
316 (define-key key-translation-map
317 (vector from
) 'encoded-kbd-self-insert-ccl
))
318 (setq from
(1+ from
))))
321 ((eq (coding-system-type coding
) 'utf-8
)
324 (define-key key-translation-map
325 (vector i
) 'encoded-kbd-self-insert-utf-8
)
332 ;; key-translation-map at the time Encoded-kbd mode is turned on is
334 (defvar saved-key-translation-map nil
)
336 ;; Input mode at the time Encoded-kbd mode is turned on is saved here.
337 (defvar saved-input-mode nil
)
339 (put 'encoded-kbd-mode
'permanent-local t
)
341 (define-minor-mode encoded-kbd-mode
342 "Toggle Encoded-kbd minor mode.
343 With arg, turn Encoded-kbd mode on if and only if arg is positive.
345 You should not turn this mode on manually, instead use the command
346 \\[set-keyboard-coding-system] which turns on or off this mode
349 In Encoded-kbd mode, a text sent from keyboard is accepted
350 as a multilingual text encoded in a coding system set by
351 \\[set-keyboard-coding-system]."
352 :global t
:group
'keyboard
:group
'mule
355 ;; We are turning on Encoded-kbd mode.
356 (let ((coding (keyboard-coding-system))
358 (or saved-key-translation-map
359 (if (keymapp key-translation-map
)
360 (setq saved-key-translation-map
361 (copy-keymap key-translation-map
))
362 (setq key-translation-map
(make-sparse-keymap))))
364 (setq saved-input-mode
365 (current-input-mode)))
366 (setq result
(and coding
(encoded-kbd-setup-keymap coding
)))
370 (nth 0 saved-input-mode
)
371 (nth 1 saved-input-mode
)
373 (nth 3 saved-input-mode
)))
374 (setq encoded-kbd-mode nil
375 saved-key-translation-map nil
376 saved-input-mode nil
)
377 (error "Unsupported coding system in Encoded-kbd mode: %S"
380 ;; We are turning off Encoded-kbd mode.
381 (when saved-input-mode
382 (setq key-translation-map saved-key-translation-map
383 saved-key-translation-map nil
)
384 (apply 'set-input-mode saved-input-mode
)
385 (setq saved-input-mode nil
))))
387 (provide 'encoded-kb
)
389 ;;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
390 ;;; encoded-kb.el ends here