(setup-english-environment): Adjusted for the change of coding
[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
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 the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 (defvar encoded-kbd-mode nil
24 "Non-nil if in Encoded-kbd minor mode.")
25 (put 'encoded-kbd-mode 'permanent-local t)
26
27 (or (assq 'encoded-kbd-mode minor-mode-alist)
28 (setq minor-mode-alist
29 (cons '(encoded-kbd-mode " Encoded-kbd") minor-mode-alist)))
30
31 (defvar encoded-kbd-mode-map
32 (let ((map (make-sparse-keymap))
33 (i 128))
34 (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix)
35 (while (< i 256)
36 (define-key map (vector i) 'encoded-kbd-handle-8bit)
37 (setq i (1+ i)))
38 map)
39 "Keymap for Encoded-kbd minor mode.")
40
41 (or (assq 'encoded-kbd-mode minor-mode-map-alist)
42 (setq minor-mode-map-alist
43 (cons (cons 'encoded-kbd-mode encoded-kbd-mode-map)
44 minor-mode-map-alist)))
45
46 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
47
48 (defvar encoded-kbd-iso2022-esc-map
49 (let ((map (make-sparse-keymap)))
50 (define-key map "$" 'encoded-kbd-iso2022-esc-dollar-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 (define-key map "-" 'encoded-kbd-iso2022-designation-prefix)
55 map)
56 "Keymap for handling ESC code in Encoded-kbd mode.")
57 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map)
58
59 (defvar encoded-kbd-iso2022-esc-dollar-map
60 (let ((map (make-sparse-keymap)))
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-prefix)
65 (define-key map "@" 'encoded-kbd-iso2022-designation)
66 (define-key map "A" 'encoded-kbd-iso2022-designation)
67 (define-key map "B" 'encoded-kbd-iso2022-designation)
68 map)
69 "Keymap for handling ESC $ sequence 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 (l charset-list))
76 (while l
77 (define-key map
78 (char-to-string (charset-iso-final-char (car l)))
79 'encoded-kbd-iso2022-designation)
80 (setq l (cdr l)))
81 map)
82 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
83 (fset 'encoded-kbd-iso2022-designation-prefix
84 encoded-kbd-iso2022-designation-map)
85
86 (defvar encoded-kbd-iso2022-non-ascii-map
87 (let ((map (make-keymap))
88 (i 32))
89 (while (< i 128)
90 (define-key map (char-to-string i) 'encoded-kbd-self-insert-iso2022-7bit)
91 (setq i (1+ i)))
92 (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix)
93 map)
94 "Keymap for handling non-ASCII character set in Encoded-kbd mode.")
95
96 ;; One of the symbols `sjis', `iso2022-7', `iso2022-8', or `big5' to
97 ;; denote what kind of coding-system we are now handling in
98 ;; Encoded-kbd mode.
99 (defvar encoded-kbd-coding nil)
100
101 ;; Keep information of designation state of ISO2022 encoding. This is
102 ;; a vector of character sets currently designated to each graphic
103 ;; registers (0..3).
104
105 (defvar encoded-kbd-iso2022-designations nil)
106 (make-variable-buffer-local 'encoded-kbd-iso2022-designations)
107 (put 'encoded-kbd-iso2022-designations 'permanent-local t)
108
109 ;; Keep information of invocation state of ISO2022 encoding. This is
110 ;; a vector of graphic register numbers currently invoked to each
111 ;; graphic plane (0..1), the third element is a single shifted graphic
112 ;; register number.
113
114 (defvar encoded-kbd-iso2022-invocations nil)
115 (make-variable-buffer-local 'encoded-kbd-iso2022-invocations)
116 (put 'encoded-kbd-iso2022-invocations 'permanent-local t)
117
118 (defun encoded-kbd-iso2022-designation ()
119 "Do ISO2022 designation according to the curren key in Encoded-kbd mode.
120 The following key sequence may cause multilingual text insertion."
121 (interactive)
122 (let ((key-seq (this-command-keys))
123 intermediate-char final-char
124 reg dimension chars charset)
125 (if (= (length key-seq) 4)
126 ;; ESC $ <intermediate-char> <final-char>
127 (setq intermediate-char (aref key-seq 2)
128 dimension 2
129 chars (if (< intermediate-char ?,) 94 96)
130 final-char (aref key-seq 3)
131 reg (mod intermediate-char 4))
132 (if (= (aref key-seq 1) ?$)
133 ;; ESC $ <final-char>
134 (setq dimension 2
135 chars 94
136 final-char (aref key-seq 2)
137 reg 0)
138 ;; ESC <intermediate-char> <final-char>
139 (setq intermediate-char (aref key-seq 1)
140 dimension 1
141 chars (if (< intermediate-char ?,) 94 96)
142 final-char (aref key-seq 2)
143 reg (mod intermediate-char 4))))
144 (if (setq charset (iso-charset dimension chars final-char))
145 (aset encoded-kbd-iso2022-designations reg charset)
146 (error "Character set of DIMENSION %s, CHARS %s, FINAL-CHAR `%c' is not supported"
147 dimension chars final-char))
148
149 (if (memq (aref encoded-kbd-iso2022-designations
150 (aref encoded-kbd-iso2022-invocations 0))
151 '(ascii latin-jisx0201))
152 ;; Graphic plane 0 (0x20..0x7f) is for ASCII. We don't have
153 ;; to handle characters in this range specially.
154 (throw 'exit nil)
155 ;; Graphic plane 0 is for non-ASCII.
156 (let ((overriding-local-map encoded-kbd-iso2022-non-ascii-map))
157 (recursive-edit)))))
158
159 (defun encoded-kbd-handle-8bit ()
160 "Handle an 8-bit character enterned in Encoded-kbd mode."
161 (interactive)
162 (cond ((eq encoded-kbd-coding 'iso2022-7)
163 (error "Can't handle the character code %d" last-command-char))
164
165 ((eq encoded-kbd-coding 'iso2022-8)
166 (cond ((= last-command-char ?\216)
167 (aset encoded-kbd-iso2022-invocations 2 2))
168
169 ((= last-command-char ?\217)
170 (aset encoded-kbd-iso2022-invocations 2 3))
171
172 ((> last-command-char ?\240)
173 (encoded-kbd-self-insert-iso2022-8bit))
174
175 (t
176 (error "Can't handle the character code %d"
177 last-command-char))))
178
179 ((eq encoded-kbd-coding 'sjis)
180 (encoded-kbd-self-insert-sjis))
181
182 (t
183 (encoded-kbd-self-insert-big5))))
184
185 (defun encoded-kbd-self-insert-iso2022-7bit ()
186 (interactive)
187 (let* ((charset (aref encoded-kbd-iso2022-designations
188 (or (aref encoded-kbd-iso2022-invocations 2)
189 (aref encoded-kbd-iso2022-invocations 0))))
190 (last-command-char
191 (if (= (charset-bytes charset) 1)
192 (make-char charset last-command-char)
193 (make-char charset last-command-char (read-char-exclusive)))))
194 (self-insert-command 1)
195 (aset encoded-kbd-iso2022-invocations 2 nil)
196 ))
197
198 (defun encoded-kbd-self-insert-iso2022-8bit ()
199 (interactive)
200 (let* ((charset (aref encoded-kbd-iso2022-designations
201 (or (aref encoded-kbd-iso2022-invocations 2)
202 (aref encoded-kbd-iso2022-invocations 1))))
203 (last-command-char
204 (if (= (charset-bytes charset) 1)
205 (make-char charset last-command-char)
206 (make-char charset last-command-char (read-char-exclusive)))))
207 (self-insert-command 1)
208 (aset encoded-kbd-iso2022-invocations 2 nil)
209 ))
210
211 (defun encoded-kbd-self-insert-sjis ()
212 (interactive)
213 (let ((last-command-char
214 (if (or (< last-command-char ?\xA0) (>= last-command-char ?\xE0))
215 (decode-sjis-char (+ (ash last-command-char 8)
216 (read-char-exclusive)))
217 (make-char 'latin-jisx0201 last-command-char))))
218 (self-insert-command 1)))
219
220 (defun encoded-kbd-self-insert-big5 ()
221 (interactive)
222 (let ((last-command-char
223 (decode-big5-char (+ (ash last-command-char 8)
224 (read-char-exclusive)))))
225 (self-insert-command 1)))
226
227 ;;;###autoload
228 (defun encoded-kbd-mode (&optional arg)
229 "Toggle Encoded-kbd minor mode.
230 With arg, turn Keyboard-kbd mode on if and only if arg is positive.
231
232 When in Encoded-kbd mode, a text sent from a terminal keyboard
233 is accepted as a multilingual text encoded in a coding system
234 set by the command `encoded-kbd-set-coding-system'"
235 (interactive "P")
236 (setq encoded-kbd-mode
237 (if (null arg) (null encoded-kbd-mode)
238 (> (prefix-numeric-value arg) 0)))
239 (if encoded-kbd-mode
240 (let ((coding (keyboard-coding-system))
241 (input-mode (current-input-mode)))
242 (cond ((null coding)
243 (setq encoded-kbd-mode nil)
244 (error "No coding-system for terminal keyboard is set"))
245
246 ((= (coding-system-type coding) 1) ; SJIS
247 (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
248 'use-8th-bit (nth 3 input-mode))
249 (setq encoded-kbd-coding 'sjis))
250
251 ((= (coding-system-type coding) 2) ; ISO2022
252 (if (aref (coding-system-flags coding) 7) ; 7-bit only
253 (setq encoded-kbd-coding 'iso2022-7)
254 (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
255 'use-8th-bit (nth 3 input-mode))
256 (setq encoded-kbd-coding 'iso2022-8))
257 (make-variable-buffer-local 'encoded-kbd-iso2022-designations)
258 (setq encoded-kbd-iso2022-designations (make-vector 4 nil))
259 (let ((flags (coding-system-flags coding))
260 (i 0))
261 (while (< i 4)
262 (if (charsetp (aref flags i))
263 (aset encoded-kbd-iso2022-designations i
264 (aref flags i)))
265 (setq i (1+ i))))
266 (make-variable-buffer-local 'encoded-kbd-iso2022-invocations)
267 (setq encoded-kbd-iso2022-invocations (make-vector 3 nil))
268 (aset encoded-kbd-iso2022-invocations 0 0)
269 (aset encoded-kbd-iso2022-invocations 1 1))
270
271 ((= (coding-system-type coding) 3) ; BIG5
272 (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
273 'use-8th-bit (nth 3 input-mode))
274 (setq encoded-kbd-coding 'big5))
275
276 (t
277 (setq encoded-kbd-mode nil)
278 (error "Coding-system `%s' is not supported in Encoded-kbd mode"
279 (keyboard-coding-system))))
280 (setq inactivate-current-input-method-function 'encoded-kbd-mode)
281 (setq describe-current-input-method-function 'encoded-kbd-mode-help)
282 (run-hooks 'encoded-kbd-mode-hook))
283 (setq describe-current-input-method-function nil)
284 (setq current-input-method nil))
285 (force-mode-line-update))
286
287 ;;; encoded-kb.el ends here