comparison lisp/international/encoded-kb.el @ 17052:d0d7b244b1d0

Initial revision
author Karl Heuer <kwzh@gnu.org>
date Thu, 20 Feb 1997 07:02:49 +0000
parents
children 70194012fb3a
comparison
equal deleted inserted replaced
17051:fd0b17a79b07 17052:d0d7b244b1d0
1 ;; encoded-kb.el -- handler for inputting multibyte characters encoded somehow
2
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 (defvar encoded-kbd-mode nil
23 "Non-nil if in Encoded-kbd minor mode.")
24 (put 'encoded-kbd-mode 'permanent-local t)
25
26 (or (assq 'encoded-kbd-mode minor-mode-alist)
27 (setq minor-mode-alist
28 (cons '(encoded-kbd-mode " Encoded-kbd") minor-mode-alist)))
29
30 (defvar encoded-kbd-mode-map
31 (let ((map (make-sparse-keymap))
32 (i 128))
33 (define-key map "\e" 'encoded-kbd-handle-iso2022-esc)
34 (while (< i 256)
35 (define-key map (vector i) 'encoded-kbd-handle-8bit)
36 (setq i (1+ i)))
37 map)
38 "Keymap for Encoded-kbd minor mode.")
39
40 (or (assq 'encoded-kbd-mode minor-mode-map-alist)
41 (setq minor-mode-map-alist
42 (cons (cons 'encoded-kbd-mode encoded-kbd-mode-map)
43 minor-mode-map-alist)))
44
45 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
46
47 (defvar encoded-kbd-iso2022-esc-map
48 (let ((map (make-sparse-keymap)))
49 (define-key map "$" 'encoded-kbd-iso2022-esc-dollar-prefix)
50 (define-key map "(" 'encoded-kbd-iso2022-designation-prefix)
51 (define-key map ")" 'encoded-kbd-iso2022-designation-prefix)
52 (define-key map "," 'encoded-kbd-iso2022-designation-prefix)
53 (define-key map "-" 'encoded-kbd-iso2022-designation-prefix)
54 (append map '((t . encoded-kbd-outernal-command)))
55 map)
56 "Keymap for handling ESC code in Encoded-kbd mode.")
57
58 (defvar encoded-kbd-iso2022-esc-dollar-map
59 (let ((map (make-sparse-keymap)))
60 (define-key map "(" 'encoded-kbd-iso2022-designation-prefix)
61 (define-key map ")" 'encoded-kbd-iso2022-designation-prefix)
62 (define-key map "," 'encoded-kbd-iso2022-designation-prefix)
63 (define-key map "-" 'encoded-kbd-iso2022-designation-prefix)
64 (define-key map "@" 'encoded-kbd-iso2022-designation)
65 (define-key map "A" 'encoded-kbd-iso2022-designation)
66 (define-key map "B" 'encoded-kbd-iso2022-designation)
67 (append map '((t . encoded-kbd-outernal-command)))
68 map)
69 "Keymap for handling ESC $ sequence handling in Encoded-kbd mode.")
70 (fset 'encoded-kbd-iso2022-esc-dollar-prefix
71 encoded-kbd-iso2022-esc-dollar-map)
72
73 (defvar encoded-kbd-iso2022-designation-map
74 (let ((map (make-sparse-keymap))
75 (i 48))
76 (while (< i 128)
77 (define-key map (char-to-string i) 'encoded-kbd-iso2022-designation)
78 (setq i (1+ i)))
79 (append map '((t . encoded-kbd-outernal-command)))
80 map)
81 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
82 (fset 'encoded-kbd-iso2022-designation-prefix
83 encoded-kbd-iso2022-designation-map)
84
85 (defvar encoded-kbd-iso2022-non-ascii-map
86 (let ((map (make-keymap))
87 (i 32))
88 (while (< i 128)
89 (define-key map (char-to-string i) 'encoded-kbd-self-insert-iso2022-7bit)
90 (setq i (1+ i)))
91 map)
92 "Keymap for handling non-ASCII character set in Encoded-kbd mode.")
93
94 ;; One of the symbols `sjis', `iso2022-7', `iso2022-8', or `big5' to
95 ;; denote what kind of coding-system we are now handling in
96 ;; Encoded-kbd mode.
97 (defvar encoded-kbd-coding nil)
98
99 ;; Keep information of designation state of ISO2022 encoding. This is
100 ;; a vector of character sets currently designated to each graphic
101 ;; registers (0..3).
102
103 (defvar encoded-kbd-iso2022-designations nil)
104 (make-variable-buffer-local 'encoded-kbd-iso2022-designations)
105 (put 'encoded-kbd-iso2022-designations 'permanent-local t)
106
107 ;; Keep information of invocation state of ISO2022 encoding. This is
108 ;; a vector of graphic register numbers currently invoked to each
109 ;; graphic plane (0..1), the third element is a single shifted graphic
110 ;; register number.
111
112 (defvar encoded-kbd-iso2022-invocations nil)
113 (make-variable-buffer-local 'encoded-kbd-iso2022-invocations)
114 (put 'encoded-kbd-iso2022-invocations 'permanent-local t)
115
116 (defun encoded-kbd-iso2022-designation ()
117 "Do ISO2022 designation according to the curren key in Encoded-kbd mode.
118 The following key sequence may cause multilingual text insertion."
119 (interactive)
120 (let ((key-seq (this-command-keys))
121 intermediate-char final-char
122 reg dimension chars charset)
123 (if (= (length key-seq) 3)
124 ;; (ESC) $ <intermediate-char> <final-char>
125 (setq intermediate-char (aref key-seq 1)
126 dimension 2
127 chars (if (< intermediate-char ?,) 94 96)
128 final-char (aref key-seq 2)
129 reg (mod intermediate-char 4))
130 (if (= (aref key-seq 1) ?$)
131 ;; (ESC) $ <final-char>
132 (setq dimension 2
133 chars 94
134 final-char (aref key-seq 1)
135 reg 0)
136 ;; (ESC) <intermediate-char> <final-char>
137 (setq intermediate-char (aref key-seq 0)
138 dimension 1
139 chars (if (< intermediate-char ?,) 94 96)
140 final-char (aref key-seq 1)
141 reg (mod intermediate-char 4))))
142 (if (setq charset (iso-charset dimension chars final-char))
143 (aset encoded-kbd-iso2022-designations reg charset)
144 (error "Character set of DIMENSION %s, CHARS %s, FINAL-CHAR `%c' is not supported"
145 dimension chars final-char))
146
147 (if (eq (aref encoded-kbd-iso2022-designations
148 (aref encoded-kbd-iso2022-invocations 0))
149 'ascii)
150 ;; Graphic plane 0 (0x20..0x7f) is for ASCII. We don't have
151 ;; to handle characters in this range specially.
152 (throw 'exit nil)
153 ;; Graphic plane 0 is for non-ASCII.
154 (setq overriding-local-map encoded-kbd-iso2022-non-ascii-map))))
155
156 (defun encoded-kbd-handle-iso2022-esc ()
157 (interactive)
158 (let ((overriding-local-map encoded-kbd-iso2022-esc-map))
159 (recursive-edit)))
160
161 (defun encoded-kbd-handle-8bit ()
162 "Handle an 8-bit character enterned in Encoded-kbd mode."
163 (interactive)
164 (cond ((eq encoded-kbd-coding 'iso2022-7)
165 (error "Can't handle the character code %d" last-command-char))
166
167 ((eq encoded-kbd-coding 'iso2022-8)
168 (cond ((= last-command-char ?\216)
169 (aset encoded-kbd-iso2022-invocations 2 2))
170
171 ((= last-command-char ?\217)
172 (aset encoded-kbd-iso2022-invocations 2 3))
173
174 ((> last-command-char ?\240)
175 (encoded-kbd-self-insert-iso2022-8bit))
176
177 (t
178 (error "Can't handle the character code %d"
179 last-command-char))))
180
181 ((eq encoded-kbd-coding 'sjis)
182 (encoded-kbd-self-insert-sjis))
183
184 (t
185 (encoded-kbd-self-insert-big5))))
186
187 (defun encoded-kbd-self-insert-iso2022-7bit ()
188 (interactive)
189 (let* ((charset (aref encoded-kbd-iso2022-designations
190 (or (aref encoded-kbd-iso2022-invocations 2)
191 (aref encoded-kbd-iso2022-invocations 0))))
192 (last-command-char
193 (if (= (charset-bytes charset) 1)
194 (make-char charset last-command-char)
195 (make-char charset last-command-char (read-char-exclusive)))))
196 (self-insert-command 1)
197 (aset encoded-kbd-iso2022-invocations 2 nil)
198 ))
199
200 (defun encoded-kbd-self-insert-iso2022-8bit ()
201 (interactive)
202 (let* ((charset (aref encoded-kbd-iso2022-designations
203 (or (aref encoded-kbd-iso2022-invocations 2)
204 (aref encoded-kbd-iso2022-invocations 1))))
205 (last-command-char
206 (if (= (charset-bytes charset) 1)
207 (make-char charset last-command-char)
208 (make-char charset last-command-char (read-char-exclusive)))))
209 (self-insert-command 1)
210 (aset encoded-kbd-iso2022-invocations 2 nil)
211 ))
212
213 (defun encoded-kbd-self-insert-sjis ()
214 (interactive)
215 (let ((last-command-char
216 (if (or (< last-command-char ?\xA0) (>= last-command-char ?\xE0))
217 (decode-sjis-char (+ (ash last-command-char 8)
218 (read-char-exclusive)))
219 (make-char 'latin-jisx0201 last-command-char))))
220 (self-insert-command 1)))
221
222 (defun encoded-kbd-self-insert-big5 ()
223 (interactive)
224 (let ((last-command-char
225 (decode-big5-char (+ (ash last-command-char 8)
226 (read-char-exclusive)))))
227 (self-insert-command 1)))
228
229 (defun encoded-kbd-mode (&optional arg)
230 "Toggle Encoded-kbd minor mode.
231 With arg, turn Keyboard-kbd mode on in and only if arg is positive.
232
233 When in Encoded-kbd mode, a text sent from a terminal keyboard
234 is accepted as a multilingual text encoded in a coding-system
235 set by the command `set-keyboard-coding-system'"
236 (interactive "P")
237 (setq encoded-kbd-mode
238 (if (null arg) (null encoded-kbd-mode)
239 (> (prefix-numeric-value arg) 0)))
240 (if encoded-kbd-mode
241 (let* ((coding (coding-system-vector (keyboard-coding-system)))
242 (input-mode (current-input-mode)))
243 (cond ((null coding)
244 (setq encoded-kbd-mode nil)
245 (error "No coding-system for terminal keyboard is set"))
246
247 ((= (coding-vector-type coding) 1) ; SJIS
248 (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
249 'use-8th-bit (nth 3 input-mode))
250 (setq encoded-kbd-coding 'sjis))
251
252 ((= (coding-vector-type coding) 2) ; ISO2022
253 (if (aref (coding-vector-flags coding) 7) ; 7-bit only
254 (setq encoded-kbd-coding 'iso2022-7)
255 (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
256 'use-8th-bit (nth 3 input-mode))
257 (setq encoded-kbd-coding 'iso2022-8))
258 (make-variable-buffer-local 'encoded-kbd-iso2022-designations)
259 (setq encoded-kbd-iso2022-designations (make-vector 4 nil))
260 (let ((flags (coding-vector-flags coding))
261 (i 0))
262 (while (< i 4)
263 (if (and (aref flags i)
264 (> (aref flags i) 0))
265 (aset encoded-kbd-iso2022-designations i
266 (aref flags i)))
267 (setq i (1+ i))))
268 (make-variable-buffer-local 'encoded-kbd-iso2022-invocations)
269 (setq encoded-kbd-iso2022-invocations (make-vector 3 0))
270 (aset encoded-kbd-iso2022-invocations 1 1))
271
272 ((= (coding-vector-type coding) 3) ; BIG5
273 (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
274 'use-8th-bit (nth 3 input-mode))
275 (setq encoded-kbd-coding 'big5))
276
277 (t
278 (setq encoded-kbd-mode nil)
279 (error "Coding-system `%s' is not supported in Encoded-kbd mode"
280 (keyboard-coding-system))))
281
282 (run-hooks 'encoded-kbd-mode-hook)))
283 (force-mode-line-update))
284
285 ;;; encoded-kb.el ends here