Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-50
[bpt/emacs.git] / lisp / international / encoded-kb.el
1 ;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2002 Free Software Foundation, Inc.
6 ;; Copyright (C) 2003
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H13PRO009
9
10 ;; This file is part of GNU Emacs.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
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.")
37
38 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
39
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)
47 map)
48 "Keymap for handling ESC code in Encoded-kbd mode.")
49 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map)
50
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)
60 map)
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)
64
65 (defvar encoded-kbd-iso2022-designation-map
66 (let ((map (make-sparse-keymap))
67 (l charset-list)
68 final-char)
69 (while l
70 (setq final-char (charset-iso-final-char (car l)))
71 (if (> final-char 0)
72 (define-key map (char-to-string final-char)
73 'encoded-kbd-iso2022-designation))
74 (setq l (cdr l)))
75 map)
76 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
77 (fset 'encoded-kbd-iso2022-designation-prefix
78 encoded-kbd-iso2022-designation-map)
79
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.
84
85 (defvar encoded-kbd-iso2022-designations nil)
86 (put 'encoded-kbd-iso2022-designations 'permanent-local t)
87
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.
92
93 (defvar encoded-kbd-iso2022-invocations nil)
94 (put 'encoded-kbd-iso2022-invocations 'permanent-local t)
95
96 (defsubst encoded-kbd-last-key ()
97 (let ((keys (this-single-command-keys)))
98 (aref keys (1- (length keys)))))
99
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)
111 dimension 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>
117 (setq dimension 2
118 chars 94
119 final-char (aref key-seq 2)
120 reg 0)
121 ;; ESC <intermediate-char> <final-char>
122 (setq intermediate-char (aref key-seq 1)
123 dimension 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)))
129 "")
130
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))))
136 "")
137
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))))))
147
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))))))
157
158 (defun encoded-kbd-self-insert-sjis (ignore)
159 (let ((char (encoded-kbd-last-key)))
160 (vector
161 (if (or (< char ?\xA0) (>= char ?\xE0))
162 (decode-sjis-char (+ (ash char 8) (read-char-exclusive)))
163 (make-char 'katakana-jisx0201 char)))))
164
165 (defun encoded-kbd-self-insert-big5 (ignore)
166 (let ((char (encoded-kbd-last-key)))
167 (vector
168 (decode-big5-char (+ (ash char 8) (read-char-exclusive))))))
169
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])
174 result)
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))))
179
180 (defun encoded-kbd-self-insert-charset (ignore)
181 (let* ((charset-list
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))))
188
189 (defun encoded-kbd-self-insert-utf-8 (arg)
190 (interactive "p")
191 (let ((char (encoded-kbd-last-key))
192 len)
193 (cond ((< char #xE0)
194 (setq len 1 char (logand char #x1F)))
195 ((< char #xF0)
196 (setq len 2 char (logand char #x0F)))
197 ((< char #xF8)
198 (setq len 3 char (logand char #x07)))
199 (t
200 (setq len 4 char 0)))
201 (while (> len 0)
202 (setq char (logior (lsh char 6) (logand (read-char-exclusive) #x3F))
203 len (1- len)))
204 (vector char)))
205
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.
210 (cond
211 ((eq (coding-system-type coding) 'shift-jis)
212 (let ((i 128))
213 (while (< i 256)
214 (define-key key-translation-map
215 (vector i) 'encoded-kbd-self-insert-sjis)
216 (setq i (1+ i))))
217 8)
218
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)))
224 (while (<= from to)
225 (define-key key-translation-map
226 (vector from) 'encoded-kbd-self-insert-charset)
227 (setq from (1+ from))))
228 8)
229
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))
237 (dotimes (i 4)
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)
257 (dotimes (i 96)
258 (define-key key-translation-map
259 (vector (+ 32 i)) 'encoded-kbd-self-insert-iso2022-7bit)))
260 (if (memq '7-bit flags)
261 t
262 (dotimes (i 96)
263 (define-key key-translation-map
264 (vector (+ 160 i)) 'encoded-kbd-self-insert-iso2022-8bit))
265 8))))
266
267 ((eq (coding-system-type coding) 4) ; CCL-base
268 (let ((valid-codes (or (coding-system-get coding :valid)
269 '((128 . 255))))
270 elt from to valid)
271 (while valid-codes
272 (setq elt (car valid-codes) valid-codes (cdr valid-codes))
273 (if (consp elt)
274 (setq from (car elt) to (cdr elt))
275 (setq from (setq to elt)))
276 (while (<= from to)
277 (if (>= from 128)
278 (define-key key-translation-map
279 (vector from) 'encoded-kbd-self-insert-ccl))
280 (setq from (1+ from))))
281 8))
282
283 ((eq (coding-system-type coding) 'utf-8)
284 (let ((i #xC0))
285 (while (< i 256)
286 (define-key key-translation-map
287 (vector i) 'encoded-kbd-self-insert-utf-8)
288 (setq i (1+ i)))))
289
290 (t
291 nil)))
292
293 ;; key-translation-map at the time Encoded-kbd mode is turned on is
294 ;; saved here.
295 (defvar saved-key-translation-map nil)
296
297 ;; Input mode at the time Encoded-kbd mode is turned on is saved here.
298 (defvar saved-input-mode nil)
299
300 (put 'encoded-kbd-mode 'permanent-local t)
301 ;;;###autoload
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.
305
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
308 automatically.
309
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]."
313 :global t
314
315 (if encoded-kbd-mode
316 ;; We are turning on Encoded-kbd mode.
317 (let ((coding (keyboard-coding-system))
318 result)
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))))
324 (or saved-input-mode
325 (setq saved-input-mode
326 (current-input-mode)))
327 (setq result (and coding (encoded-kbd-setup-keymap coding)))
328 (if result
329 (if (eq result 8)
330 (set-input-mode
331 (nth 0 saved-input-mode)
332 (nth 1 saved-input-mode)
333 'use-8th-bit
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"
339 coding)))
340
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)))
346
347 (provide 'encoded-kb)
348
349 ;;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
350 ;;; encoded-kb.el ends here