1 ;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2004, 2005
5 ;; National Institute of Advanced Industrial Science and Technology (AIST)
6 ;; Registration Number H14PRO021
8 ;; National Institute of Advanced Industrial Science and Technology (AIST)
9 ;; Registration Number H13PRO009
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
32 ;; Usually this map is empty (even if Encoded-kbd mode is on), but if
33 ;; the keyboard coding system is iso-2022-based, it defines dummy key
34 ;; bindings for ESC $ ..., etc. so that those bindings in
35 ;; key-translation-map take effect.
36 (defconst encoded-kbd-mode-map
(make-sparse-keymap)
37 "Keymap for Encoded-kbd minor mode.")
39 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
41 (defvar encoded-kbd-iso2022-esc-map
42 (let ((map (make-sparse-keymap)))
43 (define-key map
"$" 'encoded-kbd-iso2022-esc-dollar-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
)
47 (define-key map
"-" 'encoded-kbd-iso2022-designation-prefix
)
49 "Keymap for handling ESC code in Encoded-kbd mode.")
50 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map
)
52 (defvar encoded-kbd-iso2022-esc-dollar-map
53 (let ((map (make-sparse-keymap)))
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-prefix
)
58 (define-key map
"@" 'encoded-kbd-iso2022-designation
)
59 (define-key map
"A" 'encoded-kbd-iso2022-designation
)
60 (define-key map
"B" 'encoded-kbd-iso2022-designation
)
62 "Keymap for handling ESC $ sequence in Encoded-kbd mode.")
63 (fset 'encoded-kbd-iso2022-esc-dollar-prefix
64 encoded-kbd-iso2022-esc-dollar-map
)
66 (defvar encoded-kbd-iso2022-designation-map
67 (let ((map (make-sparse-keymap))
71 (setq final-char
(charset-iso-final-char (car l
)))
73 (define-key map
(char-to-string final-char
)
74 'encoded-kbd-iso2022-designation
))
77 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
78 (fset 'encoded-kbd-iso2022-designation-prefix
79 encoded-kbd-iso2022-designation-map
)
81 ;; Keep information of designation state of ISO2022 encoding. When
82 ;; Encoded-kbd mode is on, this is set to a vector of length 4, the
83 ;; elements are character sets currently designated to graphic
84 ;; registers 0 thru 3.
86 (defvar encoded-kbd-iso2022-designations nil
)
87 (put 'encoded-kbd-iso2022-designations
'permanent-local t
)
89 ;; Keep information of invocation state of ISO2022 encoding. When
90 ;; Encoded-kbd mode is on, this is set to a vector of length 3,
91 ;; graphic register numbers currently invoked to graphic plane 1 and
92 ;; 2, and a single shifted graphic register number.
94 (defvar encoded-kbd-iso2022-invocations nil
)
95 (put 'encoded-kbd-iso2022-invocations
'permanent-local t
)
97 (defsubst encoded-kbd-last-key
()
98 (let ((keys (this-single-command-keys)))
99 (aref keys
(1- (length keys
)))))
101 (defun encoded-kbd-iso2022-designation (ignore)
102 "Do ISO2022 designation according to the current key in Encoded-kbd mode.
103 The following key sequence may cause multilingual text insertion."
104 (let ((key-seq (this-single-command-keys))
105 (prev-g0-charset (aref encoded-kbd-iso2022-designations
106 (aref encoded-kbd-iso2022-invocations
0)))
107 intermediate-char final-char
108 reg dimension chars charset
)
109 (if (= (length key-seq
) 4)
110 ;; ESC $ <intermediate-char> <final-char>
111 (setq intermediate-char
(aref key-seq
2)
113 chars
(if (< intermediate-char ?
,) 94 96)
114 final-char
(aref key-seq
3)
115 reg
(mod intermediate-char
4))
116 (if (= (aref key-seq
1) ?$
)
117 ;; ESC $ <final-char>
120 final-char
(aref key-seq
2)
122 ;; ESC <intermediate-char> <final-char>
123 (setq intermediate-char
(aref key-seq
1)
125 chars
(if (< intermediate-char ?
,) 94 96)
126 final-char
(aref key-seq
2)
127 reg
(mod intermediate-char
4))))
128 (aset encoded-kbd-iso2022-designations reg
129 (iso-charset dimension chars final-char
)))
132 (defun encoded-kbd-iso2022-single-shift (ignore)
133 (let ((char (encoded-kbd-last-key)))
134 (aset encoded-kbd-iso2022-invocations
2 (if (= char ?
\216) 2 3)))
137 (defun encoded-kbd-self-insert-iso2022-7bit (ignore)
138 (let ((char (encoded-kbd-last-key))
139 (charset (aref encoded-kbd-iso2022-designations
140 (or (aref encoded-kbd-iso2022-invocations
2)
141 (aref encoded-kbd-iso2022-invocations
0)))))
142 (aset encoded-kbd-iso2022-invocations
2 nil
)
143 (vector (if (= (charset-dimension charset
) 1)
144 (make-char charset char
)
145 (make-char charset char
(read-char-exclusive))))))
147 (defun encoded-kbd-self-insert-iso2022-8bit (ignore)
148 (let ((char (encoded-kbd-last-key))
149 (charset (aref encoded-kbd-iso2022-designations
150 (or (aref encoded-kbd-iso2022-invocations
2)
151 (aref encoded-kbd-iso2022-invocations
1)))))
152 (aset encoded-kbd-iso2022-invocations
2 nil
)
153 (vector (if (= (charset-dimension charset
) 1)
154 (make-char charset char
)
155 (make-char charset char
(read-char-exclusive))))))
157 (defun encoded-kbd-self-insert-sjis (ignore)
158 (let ((char (encoded-kbd-last-key)))
160 (if (or (< char ?
\xA0) (>= char ?
\xE0))
161 (decode-sjis-char (+ (ash char
8) (read-char-exclusive)))
162 (make-char 'katakana-jisx0201 char
)))))
164 (defun encoded-kbd-self-insert-big5 (ignore)
165 (let ((char (encoded-kbd-last-key)))
167 (decode-big5-char (+ (ash char
8) (read-char-exclusive))))))
169 (defun encoded-kbd-self-insert-ccl (ignore)
170 (let ((str (char-to-string (encoded-kbd-last-key)))
171 (ccl (coding-system-get (keyboard-coding-system) :ccl-decoder
))
172 (vec [nil nil nil nil nil nil nil nil nil
])
174 (while (= (length (setq result
(ccl-execute-on-string ccl vec str t
))) 0)
175 (dotimes (i 9) (aset vec i nil
))
176 (setq str
(format "%s%c" str
(read-char-exclusive))))
177 (vector (aref result
0))))
180 ;; Decode list of codes in CODE-LIST by CHARSET and return the decoded
181 ;; characters. If CODE-LIST is too short for the dimension of
182 ;; CHARSET, read new codes and append them to the tail of CODE-LIST.
183 ;; Return nil if CODE-LIST can't be decoded.
185 (defun encoded-kbd-decode-code-list (charset code-list
)
186 (let ((dimension (charset-dimension charset
))
188 (while (> dimension
(length code-list
))
189 (nconc code-list
(list (read-char-exclusive))))
190 (setq code
(car code-list
))
192 (decode-char charset code
)
193 (setq code-list
(cdr code-list
)
194 code
(logior (lsh code
8) (car code-list
)))
196 (decode-char charset code
)
197 (setq code-list
(cdr code-list
)
198 code
(logior (lsh code
8) (car code-list
)))
200 (decode-char charset code
)
201 ;; As Emacs can't handle full 32-bit integer, we must give a
202 ;; cons of higher and lower 16-bit codes to decode-char.
203 (setq code
(cons (lsh code -
8)
204 (logior (lsh (car code-list
) 8) (cadr code-list
))))
205 (decode-char charset code
))))))
207 (defun encoded-kbd-self-insert-charset (ignore)
209 (coding-system-get (keyboard-coding-system) :charset-list
))
210 (code-list (list (encoded-kbd-last-key)))
212 (while (and charset-list
(not char
))
213 (setq char
(encoded-kbd-decode-code-list (car charset-list
) code-list
)
214 charset-list
(cdr charset-list
)))
217 (setq unread-command-events
(cdr code-list
))
218 (vector (car code-list
)))))
220 (defun encoded-kbd-self-insert-utf-8 (arg)
222 (let ((char (encoded-kbd-last-key))
225 (setq len
1 char
(logand char
#x1F
)))
227 (setq len
2 char
(logand char
#x0F
)))
229 (setq len
3 char
(logand char
#x07
)))
231 (setq len
4 char
0)))
233 (setq char
(logior (lsh char
6) (logand (read-char-exclusive) #x3F
))
237 (defun encoded-kbd-setup-keymap (coding)
238 ;; At first, reset the keymap.
239 (define-key encoded-kbd-mode-map
"\e" nil
)
240 ;; Then setup the keymap according to the keyboard coding system.
242 ((eq (coding-system-type coding
) 'shift-jis
)
245 (define-key key-translation-map
246 (vector i
) 'encoded-kbd-self-insert-sjis
)
250 ((eq (coding-system-type coding
) 'charset
)
253 (let ((dim (charset-dimension x
))
254 (code-space (get-charset-property x
:code-space
)))
255 (cons (aref code-space
(* (1- dim
) 2))
256 (aref code-space
(1+ (* (1- dim
) 2))))))
257 (coding-system-get coding
:charset-list
)))
258 (let ((from (max (car elt
) 128))
261 (define-key key-translation-map
262 (vector from
) 'encoded-kbd-self-insert-charset
)
263 (setq from
(1+ from
)))))
266 ((eq (coding-system-type coding
) 'iso-2022
)
267 (let ((flags (coding-system-get coding
:flags
))
268 (designation (coding-system-get coding
:designation
)))
269 (if (memq 'locking-shift flags
)
270 nil
; Don't support locking-shift.
271 (setq encoded-kbd-iso2022-designations
(make-vector 4 nil
)
272 encoded-kbd-iso2022-invocations
(make-vector 3 nil
))
274 (if (aref designation i
)
275 (if (charsetp (aref designation i
))
276 (aset encoded-kbd-iso2022-designations
277 i
(aref designation i
))
278 (if (charsetp (car-safe (aref designation i
)))
279 (aset encoded-kbd-iso2022-designations
280 i
(car (aref designation i
)))))))
281 (aset encoded-kbd-iso2022-invocations
0 0)
282 (if (aref encoded-kbd-iso2022-designations
1)
283 (aset encoded-kbd-iso2022-invocations
1 1))
284 (when (memq 'designation flags
)
285 (define-key encoded-kbd-mode-map
"\e" 'encoded-kbd-iso2022-esc-prefix
)
286 (define-key key-translation-map
"\e" 'encoded-kbd-iso2022-esc-prefix
))
287 (when (or (aref designation
2) (aref designation
3))
288 (define-key key-translation-map
289 [?
\216] 'encoded-kbd-iso2022-single-shift
)
290 (define-key key-translation-map
291 [?
\217] 'encoded-kbd-iso2022-single-shift
))
292 (or (eq (aref designation
0) 'ascii
)
294 (define-key key-translation-map
295 (vector (+ 32 i
)) 'encoded-kbd-self-insert-iso2022-7bit
)))
296 (if (memq '7-bit flags
)
299 (define-key key-translation-map
300 (vector (+ 160 i
)) 'encoded-kbd-self-insert-iso2022-8bit
))
303 ((eq (coding-system-type coding
) 4) ; CCL-base
304 (let ((valid-codes (or (coding-system-get coding
:valid
)
308 (setq elt
(car valid-codes
) valid-codes
(cdr valid-codes
))
310 (setq from
(car elt
) to
(cdr elt
))
311 (setq from
(setq to elt
)))
314 (define-key key-translation-map
315 (vector from
) 'encoded-kbd-self-insert-ccl
))
316 (setq from
(1+ from
))))
319 ((eq (coding-system-type coding
) 'utf-8
)
322 (define-key key-translation-map
323 (vector i
) 'encoded-kbd-self-insert-utf-8
)
330 ;; key-translation-map at the time Encoded-kbd mode is turned on is
332 (defvar saved-key-translation-map nil
)
334 ;; Input mode at the time Encoded-kbd mode is turned on is saved here.
335 (defvar saved-input-mode nil
)
337 (put 'encoded-kbd-mode
'permanent-local t
)
339 (define-minor-mode encoded-kbd-mode
340 "Toggle Encoded-kbd minor mode.
341 With arg, turn Encoded-kbd mode on if and only if arg is positive.
343 You should not turn this mode on manually, instead use the command
344 \\[set-keyboard-coding-system] which turns on or off this mode
347 In Encoded-kbd mode, a text sent from keyboard is accepted
348 as a multilingual text encoded in a coding system set by
349 \\[set-keyboard-coding-system]."
350 :global t
:group
'keyboard
:group
'mule
353 ;; We are turning on Encoded-kbd mode.
354 (let ((coding (keyboard-coding-system))
356 (or saved-key-translation-map
357 (if (keymapp key-translation-map
)
358 (setq saved-key-translation-map
359 (copy-keymap key-translation-map
))
360 (setq key-translation-map
(make-sparse-keymap))))
362 (setq saved-input-mode
363 (current-input-mode)))
364 (setq result
(and coding
(encoded-kbd-setup-keymap coding
)))
368 (nth 0 saved-input-mode
)
369 (nth 1 saved-input-mode
)
371 (nth 3 saved-input-mode
)))
372 (setq encoded-kbd-mode nil
373 saved-key-translation-map nil
374 saved-input-mode nil
)
375 (error "Unsupported coding system in Encoded-kbd mode: %S"
378 ;; We are turning off Encoded-kbd mode.
379 (setq key-translation-map saved-key-translation-map
380 saved-key-translation-map nil
)
381 (apply 'set-input-mode saved-input-mode
)
382 (setq saved-input-mode nil
)))
384 (provide 'encoded-kb
)
386 ;;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
387 ;;; encoded-kb.el ends here