Commit | Line | Data |
---|---|---|
3fdc9c8f | 1 | ;;; encoded-kb.el --- Handler to input multibyte characters encoded somehow |
4ed46869 | 2 | |
4ed46869 | 3 | ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. |
fa526c4a | 4 | ;; Licensed to the Free Software Foundation. |
4ed46869 KH |
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 | |
369314dc KH |
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. | |
4ed46869 KH |
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 | ||
068b074e KH |
27 | (let ((slot (assq 'encoded-kbd-mode minor-mode-alist)) |
28 | (name " Encoded-kbd")) | |
29 | (if slot | |
30 | (setcar (cdr slot) name) | |
4ed46869 | 31 | (setq minor-mode-alist |
068b074e | 32 | (cons '(encoded-kbd-mode " Encoded-kbd") minor-mode-alist)))) |
4ed46869 | 33 | |
068b074e | 34 | (defconst encoded-kbd-mode-map (make-sparse-keymap) |
4ed46869 KH |
35 | "Keymap for Encoded-kbd minor mode.") |
36 | ||
068b074e KH |
37 | (let ((slot (assq 'encoded-kbd-mode minor-mode-map-alist))) |
38 | (if slot | |
39 | (setcdr slot encoded-kbd-mode-map) | |
4ed46869 KH |
40 | (setq minor-mode-map-alist |
41 | (cons (cons 'encoded-kbd-mode encoded-kbd-mode-map) | |
068b074e | 42 | minor-mode-map-alist)))) |
4ed46869 KH |
43 | |
44 | ;; Subsidiary keymaps for handling ISO2022 escape sequences. | |
45 | ||
46 | (defvar encoded-kbd-iso2022-esc-map | |
47 | (let ((map (make-sparse-keymap))) | |
48 | (define-key map "$" 'encoded-kbd-iso2022-esc-dollar-prefix) | |
49 | (define-key map "(" 'encoded-kbd-iso2022-designation-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) | |
4ed46869 KH |
53 | map) |
54 | "Keymap for handling ESC code in Encoded-kbd mode.") | |
f61d15a7 | 55 | (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map) |
4ed46869 KH |
56 | |
57 | (defvar encoded-kbd-iso2022-esc-dollar-map | |
58 | (let ((map (make-sparse-keymap))) | |
59 | (define-key map "(" 'encoded-kbd-iso2022-designation-prefix) | |
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) | |
64 | (define-key map "A" 'encoded-kbd-iso2022-designation) | |
65 | (define-key map "B" 'encoded-kbd-iso2022-designation) | |
4ed46869 | 66 | map) |
f61d15a7 | 67 | "Keymap for handling ESC $ sequence in Encoded-kbd mode.") |
4ed46869 KH |
68 | (fset 'encoded-kbd-iso2022-esc-dollar-prefix |
69 | encoded-kbd-iso2022-esc-dollar-map) | |
70 | ||
71 | (defvar encoded-kbd-iso2022-designation-map | |
72 | (let ((map (make-sparse-keymap)) | |
164edac8 KH |
73 | (l charset-list) |
74 | final-char) | |
f61d15a7 | 75 | (while l |
164edac8 | 76 | (setq final-char (charset-iso-final-char (car l))) |
71b898fe | 77 | (if (> final-char 0) |
164edac8 KH |
78 | (define-key map (char-to-string final-char) |
79 | 'encoded-kbd-iso2022-designation)) | |
f61d15a7 | 80 | (setq l (cdr l))) |
4ed46869 KH |
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))) | |
f61d15a7 | 92 | (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix) |
a0d4676a KH |
93 | (setq i 160) |
94 | (while (< i 256) | |
95 | (define-key map (vector i) 'encoded-kbd-handle-8bit) | |
96 | (setq i (1+ i))) | |
4ed46869 KH |
97 | map) |
98 | "Keymap for handling non-ASCII character set in Encoded-kbd mode.") | |
99 | ||
100 | ;; One of the symbols `sjis', `iso2022-7', `iso2022-8', or `big5' to | |
101 | ;; denote what kind of coding-system we are now handling in | |
102 | ;; Encoded-kbd mode. | |
103 | (defvar encoded-kbd-coding nil) | |
104 | ||
068b074e KH |
105 | ;; Keep information of designation state of ISO2022 encoding. When |
106 | ;; Encoded-kbd mode is on, this is set to a vector of length 4, the | |
107 | ;; elements are character sets currently designated to graphic | |
108 | ;; registers 0 thru 3. | |
4ed46869 KH |
109 | |
110 | (defvar encoded-kbd-iso2022-designations nil) | |
4ed46869 KH |
111 | (put 'encoded-kbd-iso2022-designations 'permanent-local t) |
112 | ||
068b074e KH |
113 | ;; Keep information of invocation state of ISO2022 encoding. When |
114 | ;; Encoded-kbd mode is on, this is set to a vector of length 3, | |
115 | ;; graphic register numbers currently invoked to graphic plane 1 and | |
116 | ;; 2, and a single shifted graphic register number. | |
4ed46869 KH |
117 | |
118 | (defvar encoded-kbd-iso2022-invocations nil) | |
4ed46869 KH |
119 | (put 'encoded-kbd-iso2022-invocations 'permanent-local t) |
120 | ||
121 | (defun encoded-kbd-iso2022-designation () | |
e8dd0160 | 122 | "Do ISO2022 designation according to the current key in Encoded-kbd mode. |
4ed46869 KH |
123 | The following key sequence may cause multilingual text insertion." |
124 | (interactive) | |
125 | (let ((key-seq (this-command-keys)) | |
a0d4676a KH |
126 | (prev-g0-charset (aref encoded-kbd-iso2022-designations |
127 | (aref encoded-kbd-iso2022-invocations 0))) | |
4ed46869 KH |
128 | intermediate-char final-char |
129 | reg dimension chars charset) | |
f61d15a7 KH |
130 | (if (= (length key-seq) 4) |
131 | ;; ESC $ <intermediate-char> <final-char> | |
132 | (setq intermediate-char (aref key-seq 2) | |
4ed46869 KH |
133 | dimension 2 |
134 | chars (if (< intermediate-char ?,) 94 96) | |
f61d15a7 | 135 | final-char (aref key-seq 3) |
4ed46869 KH |
136 | reg (mod intermediate-char 4)) |
137 | (if (= (aref key-seq 1) ?$) | |
f61d15a7 | 138 | ;; ESC $ <final-char> |
4ed46869 KH |
139 | (setq dimension 2 |
140 | chars 94 | |
f61d15a7 | 141 | final-char (aref key-seq 2) |
4ed46869 | 142 | reg 0) |
f61d15a7 KH |
143 | ;; ESC <intermediate-char> <final-char> |
144 | (setq intermediate-char (aref key-seq 1) | |
4ed46869 KH |
145 | dimension 1 |
146 | chars (if (< intermediate-char ?,) 94 96) | |
f61d15a7 | 147 | final-char (aref key-seq 2) |
4ed46869 KH |
148 | reg (mod intermediate-char 4)))) |
149 | (if (setq charset (iso-charset dimension chars final-char)) | |
150 | (aset encoded-kbd-iso2022-designations reg charset) | |
151 | (error "Character set of DIMENSION %s, CHARS %s, FINAL-CHAR `%c' is not supported" | |
152 | dimension chars final-char)) | |
153 | ||
f61d15a7 | 154 | (if (memq (aref encoded-kbd-iso2022-designations |
a0d4676a | 155 | (aref encoded-kbd-iso2022-invocations 0)) |
f61d15a7 | 156 | '(ascii latin-jisx0201)) |
4ed46869 KH |
157 | ;; Graphic plane 0 (0x20..0x7f) is for ASCII. We don't have |
158 | ;; to handle characters in this range specially. | |
a0d4676a | 159 | (if (not (memq prev-g0-charset '(ascii latin-jisx0201))) |
e8dd0160 | 160 | ;; We must exit recursive edit now. |
a0d4676a | 161 | (throw 'exit nil)) |
4ed46869 | 162 | ;; Graphic plane 0 is for non-ASCII. |
a0d4676a | 163 | (if (memq prev-g0-charset '(ascii latin-jisx0201)) |
e8dd0160 | 164 | ;; We must handle keys specially. |
a0d4676a KH |
165 | (let ((overriding-local-map encoded-kbd-iso2022-non-ascii-map)) |
166 | (recursive-edit)))))) | |
4ed46869 KH |
167 | |
168 | (defun encoded-kbd-handle-8bit () | |
e8dd0160 | 169 | "Handle an 8-bit character entered in Encoded-kbd mode." |
4ed46869 KH |
170 | (interactive) |
171 | (cond ((eq encoded-kbd-coding 'iso2022-7) | |
172 | (error "Can't handle the character code %d" last-command-char)) | |
173 | ||
174 | ((eq encoded-kbd-coding 'iso2022-8) | |
175 | (cond ((= last-command-char ?\216) | |
176 | (aset encoded-kbd-iso2022-invocations 2 2)) | |
177 | ||
178 | ((= last-command-char ?\217) | |
179 | (aset encoded-kbd-iso2022-invocations 2 3)) | |
180 | ||
75c44f31 | 181 | ((>= last-command-char ?\240) |
4ed46869 KH |
182 | (encoded-kbd-self-insert-iso2022-8bit)) |
183 | ||
184 | (t | |
185 | (error "Can't handle the character code %d" | |
186 | last-command-char)))) | |
187 | ||
188 | ((eq encoded-kbd-coding 'sjis) | |
189 | (encoded-kbd-self-insert-sjis)) | |
190 | ||
191 | (t | |
192 | (encoded-kbd-self-insert-big5)))) | |
193 | ||
194 | (defun encoded-kbd-self-insert-iso2022-7bit () | |
195 | (interactive) | |
196 | (let* ((charset (aref encoded-kbd-iso2022-designations | |
197 | (or (aref encoded-kbd-iso2022-invocations 2) | |
198 | (aref encoded-kbd-iso2022-invocations 0)))) | |
946fdacf KH |
199 | (char (if (= (charset-dimension charset) 1) |
200 | (make-char charset last-command-char) | |
201 | (make-char charset last-command-char (read-char-exclusive))))) | |
4ed46869 | 202 | (aset encoded-kbd-iso2022-invocations 2 nil) |
946fdacf | 203 | (setq unread-command-events (cons char unread-command-events)))) |
4ed46869 KH |
204 | |
205 | (defun encoded-kbd-self-insert-iso2022-8bit () | |
206 | (interactive) | |
068b074e KH |
207 | (cond |
208 | ((= last-command-char ?\216) ; SS2 (Single Shift 2) | |
209 | (aset encoded-kbd-iso2022-invocations 2 2)) | |
210 | ((= last-command-char ?\217) ; SS3 (Single Shift 3) | |
211 | (aset encoded-kbd-iso2022-invocations 2 3)) | |
212 | (t | |
4ed46869 KH |
213 | (let* ((charset (aref encoded-kbd-iso2022-designations |
214 | (or (aref encoded-kbd-iso2022-invocations 2) | |
215 | (aref encoded-kbd-iso2022-invocations 1)))) | |
946fdacf KH |
216 | (char (if (= (charset-dimension charset) 1) |
217 | (make-char charset last-command-char) | |
068b074e KH |
218 | (make-char charset last-command-char |
219 | (read-char-exclusive))))) | |
4ed46869 | 220 | (aset encoded-kbd-iso2022-invocations 2 nil) |
068b074e | 221 | (setq unread-command-events (cons char unread-command-events)))))) |
4ed46869 KH |
222 | |
223 | (defun encoded-kbd-self-insert-sjis () | |
224 | (interactive) | |
946fdacf KH |
225 | (let ((char (if (or (< last-command-char ?\xA0) (>= last-command-char ?\xE0)) |
226 | (decode-sjis-char (+ (ash last-command-char 8) | |
227 | (read-char-exclusive))) | |
228 | (make-char 'katakana-jisx0201 last-command-char)))) | |
229 | (setq unread-command-events (cons char unread-command-events)))) | |
4ed46869 KH |
230 | |
231 | (defun encoded-kbd-self-insert-big5 () | |
232 | (interactive) | |
946fdacf KH |
233 | (let ((char (decode-big5-char (+ (ash last-command-char 8) |
234 | (read-char-exclusive))))) | |
235 | (setq unread-command-events (cons char unread-command-events)))) | |
4ed46869 | 236 | |
068b074e KH |
237 | (defun encoded-kbd-self-insert-ccl () |
238 | (interactive) | |
239 | (let ((str (char-to-string last-command-char)) | |
240 | (coding (keyboard-coding-system))) | |
241 | (setq str (decode-coding-string str coding)) | |
242 | (setq unread-command-events | |
243 | (append (string-to-list str) unread-command-events)))) | |
244 | ||
245 | (defun encoded-kbd-setup-keymap (coding) | |
246 | ;; At first, reset the keymap. | |
247 | (setcdr encoded-kbd-mode-map nil) | |
248 | ;; Then setup the keymap according to the keyboard coding system. | |
249 | (cond | |
250 | ((eq encoded-kbd-coding 'sjis) | |
251 | (let ((i 128)) | |
252 | (while (< i 256) | |
253 | (define-key encoded-kbd-mode-map | |
254 | (vector i) 'encoded-kbd-self-insert-sjis) | |
255 | (setq i (1+ i))))) | |
256 | ||
257 | ((eq encoded-kbd-coding 'big5) | |
258 | (let ((i 161)) | |
259 | (while (< i 255) | |
260 | (define-key encoded-kbd-mode-map | |
261 | (vector i) 'encoded-kbd-self-insert-big5) | |
262 | (setq i (1+ i))))) | |
263 | ||
264 | ((eq encoded-kbd-coding 'iso2022-7) | |
265 | (define-key encoded-kbd-mode-map "\e" 'encoded-kbd-iso2022-esc-prefix)) | |
266 | ||
267 | ((eq encoded-kbd-coding 'iso2022-8) | |
268 | (define-key encoded-kbd-mode-map | |
269 | (vector ?\216) 'encoded-kbd-self-insert-iso2022-8bit) | |
270 | (define-key encoded-kbd-mode-map | |
271 | (vector ?\217) 'encoded-kbd-self-insert-iso2022-8bit) | |
272 | (let ((i 160)) | |
273 | (while (< i 256) | |
274 | (define-key encoded-kbd-mode-map | |
275 | (vector i) 'encoded-kbd-self-insert-iso2022-8bit) | |
276 | (setq i (1+ i))))) | |
277 | ||
278 | ((eq encoded-kbd-coding 'ccl) | |
279 | (let ((valid-codes (or (coding-system-get coding 'valid-codes) | |
280 | '((128 255)))) | |
281 | elt from to) | |
282 | (while valid-codes | |
283 | (setq elt (car valid-codes) valid-codes (cdr valid-codes)) | |
284 | (if (consp elt) | |
285 | (setq from (car elt) to (cdr elt)) | |
286 | (setq from (setq to elt))) | |
287 | (while (<= from to) | |
288 | (if (>= from 128) | |
289 | (define-key encoded-kbd-mode-map | |
290 | (vector from) 'encoded-kbd-self-insert-ccl)) | |
291 | (setq from (1+ from)))))) | |
292 | ||
293 | (t | |
294 | (error "Invalid value in encoded-kbd-coding: %s" encoded-kbd-coding)))) | |
295 | ||
296 | ||
586ec68a KH |
297 | ;; Input mode at the time Encoded-kbd mode is turned on is saved here. |
298 | (defvar saved-input-mode nil) | |
299 | ||
f61d15a7 | 300 | ;;;###autoload |
4ed46869 KH |
301 | (defun encoded-kbd-mode (&optional arg) |
302 | "Toggle Encoded-kbd minor mode. | |
586ec68a | 303 | With arg, turn Encoded-kbd mode on if and only if arg is positive. |
4ed46869 | 304 | |
a0d4676a | 305 | You should not turn this mode on manually, instead use the command |
f7e5a632 | 306 | \\[set-keyboard-coding-system] which turns on or off this mode |
a0d4676a KH |
307 | automatically. |
308 | ||
309 | In Encoded-kbd mode, a text sent from keyboard is accepted | |
310 | as a multilingual text encoded in a coding system set by | |
f7e5a632 | 311 | \\[set-keyboard-coding-system]." |
586ec68a KH |
312 | (if encoded-kbd-mode |
313 | ;; We must at first reset input-mode to the original. | |
314 | (apply 'set-input-mode saved-input-mode)) | |
4ed46869 KH |
315 | (setq encoded-kbd-mode |
316 | (if (null arg) (null encoded-kbd-mode) | |
317 | (> (prefix-numeric-value arg) 0))) | |
318 | (if encoded-kbd-mode | |
586ec68a KH |
319 | (let ((coding (keyboard-coding-system))) |
320 | (setq saved-input-mode (current-input-mode)) | |
4ed46869 KH |
321 | (cond ((null coding) |
322 | (setq encoded-kbd-mode nil) | |
586ec68a | 323 | (error "No coding system for keyboard input is set")) |
4ed46869 | 324 | |
c4ede6a2 | 325 | ((= (coding-system-type coding) 1) ; SJIS |
586ec68a KH |
326 | (set-input-mode |
327 | (nth 0 saved-input-mode) (nth 1 saved-input-mode) | |
328 | 'use-8th-bit (nth 3 saved-input-mode)) | |
4ed46869 KH |
329 | (setq encoded-kbd-coding 'sjis)) |
330 | ||
c4ede6a2 KH |
331 | ((= (coding-system-type coding) 2) ; ISO2022 |
332 | (if (aref (coding-system-flags coding) 7) ; 7-bit only | |
4ed46869 | 333 | (setq encoded-kbd-coding 'iso2022-7) |
586ec68a KH |
334 | (set-input-mode |
335 | (nth 0 saved-input-mode) (nth 1 saved-input-mode) | |
336 | 'use-8th-bit (nth 3 saved-input-mode)) | |
4ed46869 | 337 | (setq encoded-kbd-coding 'iso2022-8)) |
4ed46869 | 338 | (setq encoded-kbd-iso2022-designations (make-vector 4 nil)) |
c4ede6a2 | 339 | (let ((flags (coding-system-flags coding)) |
4ed46869 KH |
340 | (i 0)) |
341 | (while (< i 4) | |
f61d15a7 | 342 | (if (charsetp (aref flags i)) |
4ed46869 | 343 | (aset encoded-kbd-iso2022-designations i |
586ec68a KH |
344 | (aref flags i)) |
345 | (if (charsetp (car-safe (aref flags i))) | |
346 | (aset encoded-kbd-iso2022-designations i | |
347 | (car (aref flags i))))) | |
4ed46869 | 348 | (setq i (1+ i)))) |
f61d15a7 KH |
349 | (setq encoded-kbd-iso2022-invocations (make-vector 3 nil)) |
350 | (aset encoded-kbd-iso2022-invocations 0 0) | |
4ed46869 KH |
351 | (aset encoded-kbd-iso2022-invocations 1 1)) |
352 | ||
c4ede6a2 | 353 | ((= (coding-system-type coding) 3) ; BIG5 |
586ec68a KH |
354 | (set-input-mode |
355 | (nth 0 saved-input-mode) (nth 1 saved-input-mode) | |
356 | 'use-8th-bit (nth 3 saved-input-mode)) | |
4ed46869 KH |
357 | (setq encoded-kbd-coding 'big5)) |
358 | ||
068b074e KH |
359 | ((= (coding-system-type coding) 4) ; CCL based coding |
360 | (set-input-mode | |
361 | (nth 0 saved-input-mode) (nth 1 saved-input-mode) | |
362 | 'use-8th-bit (nth 3 saved-input-mode)) | |
363 | (setq encoded-kbd-coding 'ccl)) | |
364 | ||
4ed46869 KH |
365 | (t |
366 | (setq encoded-kbd-mode nil) | |
367 | (error "Coding-system `%s' is not supported in Encoded-kbd mode" | |
368 | (keyboard-coding-system)))) | |
068b074e | 369 | (encoded-kbd-setup-keymap coding) |
586ec68a | 370 | (run-hooks 'encoded-kbd-mode-hook)))) |
4ed46869 KH |
371 | |
372 | ;;; encoded-kb.el ends here |