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