1 ;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; This file is part of GNU Emacs.
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)
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.
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.
27 (defconst encoded-kbd-mode-map (make-sparse-keymap)
28 "Keymap for Encoded-kbd minor mode.")
30 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
32 (defvar encoded-kbd-iso2022-esc-map
33 (let ((map (make-sparse-keymap)))
34 (define-key map "$" 'encoded-kbd-iso2022-esc-dollar-prefix)
35 (define-key map "(" 'encoded-kbd-iso2022-designation-prefix)
36 (define-key map ")" 'encoded-kbd-iso2022-designation-prefix)
37 (define-key map "," 'encoded-kbd-iso2022-designation-prefix)
38 (define-key map "-" 'encoded-kbd-iso2022-designation-prefix)
40 "Keymap for handling ESC code in Encoded-kbd mode.")
41 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map)
43 (defvar encoded-kbd-iso2022-esc-dollar-map
44 (let ((map (make-sparse-keymap)))
45 (define-key map "(" 'encoded-kbd-iso2022-designation-prefix)
46 (define-key map ")" 'encoded-kbd-iso2022-designation-prefix)
47 (define-key map "," 'encoded-kbd-iso2022-designation-prefix)
48 (define-key map "-" 'encoded-kbd-iso2022-designation-prefix)
49 (define-key map "@" 'encoded-kbd-iso2022-designation)
50 (define-key map "A" 'encoded-kbd-iso2022-designation)
51 (define-key map "B" 'encoded-kbd-iso2022-designation)
53 "Keymap for handling ESC $ sequence in Encoded-kbd mode.")
54 (fset 'encoded-kbd-iso2022-esc-dollar-prefix
55 encoded-kbd-iso2022-esc-dollar-map)
57 (defvar encoded-kbd-iso2022-designation-map
58 (let ((map (make-sparse-keymap))
62 (setq final-char (charset-iso-final-char (car l)))
64 (define-key map (char-to-string final-char)
65 'encoded-kbd-iso2022-designation))
68 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
69 (fset 'encoded-kbd-iso2022-designation-prefix
70 encoded-kbd-iso2022-designation-map)
72 (defvar encoded-kbd-iso2022-non-ascii-map
73 (let ((map (make-keymap))
76 (define-key map (char-to-string i) 'encoded-kbd-self-insert-iso2022-7bit)
78 (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix)
81 (define-key map (vector i) 'encoded-kbd-handle-8bit)
84 "Keymap for handling non-ASCII character set in Encoded-kbd mode.")
86 ;; One of the symbols `sjis', `iso2022-7', `iso2022-8', or `big5' to
87 ;; denote what kind of coding-system we are now handling in
89 (defvar encoded-kbd-coding nil)
91 ;; Keep information of designation state of ISO2022 encoding. When
92 ;; Encoded-kbd mode is on, this is set to a vector of length 4, the
93 ;; elements are character sets currently designated to graphic
94 ;; registers 0 thru 3.
96 (defvar encoded-kbd-iso2022-designations nil)
97 (put 'encoded-kbd-iso2022-designations 'permanent-local t)
99 ;; Keep information of invocation state of ISO2022 encoding. When
100 ;; Encoded-kbd mode is on, this is set to a vector of length 3,
101 ;; graphic register numbers currently invoked to graphic plane 1 and
102 ;; 2, and a single shifted graphic register number.
104 (defvar encoded-kbd-iso2022-invocations nil)
105 (put 'encoded-kbd-iso2022-invocations 'permanent-local t)
107 (defun encoded-kbd-iso2022-designation ()
108 "Do ISO2022 designation according to the current key in Encoded-kbd mode.
109 The following key sequence may cause multilingual text insertion."
111 (let ((key-seq (this-command-keys))
112 (prev-g0-charset (aref encoded-kbd-iso2022-designations
113 (aref encoded-kbd-iso2022-invocations 0)))
114 intermediate-char final-char
115 reg dimension chars charset)
116 (if (= (length key-seq) 4)
117 ;; ESC $ <intermediate-char> <final-char>
118 (setq intermediate-char (aref key-seq 2)
120 chars (if (< intermediate-char ?,) 94 96)
121 final-char (aref key-seq 3)
122 reg (mod intermediate-char 4))
123 (if (= (aref key-seq 1) ?$)
124 ;; ESC $ <final-char>
127 final-char (aref key-seq 2)
129 ;; ESC <intermediate-char> <final-char>
130 (setq intermediate-char (aref key-seq 1)
132 chars (if (< intermediate-char ?,) 94 96)
133 final-char (aref key-seq 2)
134 reg (mod intermediate-char 4))))
135 (if (setq charset (iso-charset dimension chars final-char))
136 (aset encoded-kbd-iso2022-designations reg charset)
137 (error "Character set of DIMENSION %s, CHARS %s, FINAL-CHAR `%c' is not supported"
138 dimension chars final-char))
140 (if (memq (aref encoded-kbd-iso2022-designations
141 (aref encoded-kbd-iso2022-invocations 0))
142 '(ascii latin-jisx0201))
143 ;; Graphic plane 0 (0x20..0x7f) is for ASCII. We don't have
144 ;; to handle characters in this range specially.
145 (if (not (memq prev-g0-charset '(ascii latin-jisx0201)))
146 ;; We must exit recursive edit now.
148 ;; Graphic plane 0 is for non-ASCII.
149 (if (memq prev-g0-charset '(ascii latin-jisx0201))
150 ;; We must handle keys specially.
151 (let ((overriding-local-map encoded-kbd-iso2022-non-ascii-map))
152 (recursive-edit))))))
154 (defun encoded-kbd-handle-8bit ()
155 "Handle an 8-bit character entered in Encoded-kbd mode."
157 (cond ((eq encoded-kbd-coding 'iso2022-7)
158 (error "Can't handle the character code %d" last-command-char))
160 ((eq encoded-kbd-coding 'iso2022-8)
161 (cond ((= last-command-char ?\216)
162 (aset encoded-kbd-iso2022-invocations 2 2))
164 ((= last-command-char ?\217)
165 (aset encoded-kbd-iso2022-invocations 2 3))
167 ((>= last-command-char ?\240)
168 (encoded-kbd-self-insert-iso2022-8bit))
171 (error "Can't handle the character code %d"
172 last-command-char))))
174 ((eq encoded-kbd-coding 'sjis)
175 (encoded-kbd-self-insert-sjis))
178 (encoded-kbd-self-insert-big5))))
180 (defun encoded-kbd-self-insert-iso2022-7bit ()
182 (let* ((charset (aref encoded-kbd-iso2022-designations
183 (or (aref encoded-kbd-iso2022-invocations 2)
184 (aref encoded-kbd-iso2022-invocations 0))))
185 (char (if (= (charset-dimension charset) 1)
186 (make-char charset last-command-char)
187 (make-char charset last-command-char (read-char-exclusive)))))
188 (aset encoded-kbd-iso2022-invocations 2 nil)
189 (setq unread-command-events (cons char unread-command-events))))
191 (defun encoded-kbd-self-insert-iso2022-8bit ()
194 ((= last-command-char ?\216) ; SS2 (Single Shift 2)
195 (aset encoded-kbd-iso2022-invocations 2 2))
196 ((= last-command-char ?\217) ; SS3 (Single Shift 3)
197 (aset encoded-kbd-iso2022-invocations 2 3))
199 (let* ((charset (aref encoded-kbd-iso2022-designations
200 (or (aref encoded-kbd-iso2022-invocations 2)
201 (aref encoded-kbd-iso2022-invocations 1))))
202 (char (if (= (charset-dimension charset) 1)
203 (make-char charset last-command-char)
204 (make-char charset last-command-char
205 (read-char-exclusive)))))
206 (aset encoded-kbd-iso2022-invocations 2 nil)
207 (setq unread-command-events (cons char unread-command-events))))))
209 (defun encoded-kbd-self-insert-sjis ()
211 (let ((char (if (or (< last-command-char ?\xA0) (>= last-command-char ?\xE0))
212 (decode-sjis-char (+ (ash last-command-char 8)
213 (read-char-exclusive)))
214 (make-char 'katakana-jisx0201 last-command-char))))
215 (setq unread-command-events (cons char unread-command-events))))
217 (defun encoded-kbd-self-insert-big5 ()
219 (let ((char (decode-big5-char (+ (ash last-command-char 8)
220 (read-char-exclusive)))))
221 (setq unread-command-events (cons char unread-command-events))))
223 (defun encoded-kbd-self-insert-ccl ()
225 (let ((str (char-to-string last-command-char))
226 (ccl (car (aref (coding-system-spec (keyboard-coding-system)) 4)))
227 (vec [nil nil nil nil nil nil nil nil nil])
229 (while (= (length (setq result (ccl-execute-on-string ccl vec str t))) 0)
230 (dotimes (i 9) (aset vec i nil))
231 (setq str (format "%s%c" str (read-char-exclusive))))
232 (setq unread-command-events
233 (append result unread-command-events))))
235 (defun encoded-kbd-setup-keymap (coding)
236 ;; At first, reset the keymap.
237 (setcdr encoded-kbd-mode-map nil)
238 ;; Then setup the keymap according to the keyboard coding system.
240 ((eq encoded-kbd-coding 'sjis)
243 (define-key encoded-kbd-mode-map
244 (vector i) 'encoded-kbd-self-insert-sjis)
247 ((eq encoded-kbd-coding 'big5)
250 (define-key encoded-kbd-mode-map
251 (vector i) 'encoded-kbd-self-insert-big5)
254 ((eq encoded-kbd-coding 'iso2022-7)
255 (define-key encoded-kbd-mode-map "\e" 'encoded-kbd-iso2022-esc-prefix))
257 ((eq encoded-kbd-coding 'iso2022-8)
258 (define-key encoded-kbd-mode-map
259 (vector ?\216) 'encoded-kbd-self-insert-iso2022-8bit)
260 (define-key encoded-kbd-mode-map
261 (vector ?\217) 'encoded-kbd-self-insert-iso2022-8bit)
264 (define-key encoded-kbd-mode-map
265 (vector i) 'encoded-kbd-self-insert-iso2022-8bit)
268 ((eq encoded-kbd-coding 'ccl)
269 (let ((valid-codes (or (coding-system-get coding 'valid-codes)
273 (setq elt (car valid-codes) valid-codes (cdr valid-codes))
275 (setq from (car elt) to (cdr elt))
276 (setq from (setq to elt)))
279 (define-key encoded-kbd-mode-map
280 (vector from) 'encoded-kbd-self-insert-ccl))
281 (setq from (1+ from))))))
284 (error "Invalid value in encoded-kbd-coding: %s" encoded-kbd-coding))))
287 ;; Input mode at the time Encoded-kbd mode is turned on is saved here.
288 (defvar saved-input-mode nil)
290 (put 'encoded-kbd-mode 'permanent-local t)
292 (define-minor-mode encoded-kbd-mode
293 "Toggle Encoded-kbd minor mode.
294 With arg, turn Encoded-kbd mode on if and only if arg is positive.
296 You should not turn this mode on manually, instead use the command
297 \\[set-keyboard-coding-system] which turns on or off this mode
300 In Encoded-kbd mode, a text sent from keyboard is accepted
301 as a multilingual text encoded in a coding system set by
302 \\[set-keyboard-coding-system]."
304 ;; We must at first reset input-mode to the original.
305 (if saved-input-mode (apply 'set-input-mode saved-input-mode))
307 (let ((coding (keyboard-coding-system)))
308 (setq saved-input-mode (current-input-mode))
310 (setq encoded-kbd-mode nil)
311 (error "No coding system for keyboard input is set"))
313 ((= (coding-system-type coding) 1) ; SJIS
315 (nth 0 saved-input-mode) (nth 1 saved-input-mode)
316 'use-8th-bit (nth 3 saved-input-mode))
317 (setq encoded-kbd-coding 'sjis))
319 ((= (coding-system-type coding) 2) ; ISO2022
320 (if (aref (coding-system-flags coding) 7) ; 7-bit only
321 (setq encoded-kbd-coding 'iso2022-7)
323 (nth 0 saved-input-mode) (nth 1 saved-input-mode)
324 'use-8th-bit (nth 3 saved-input-mode))
325 (setq encoded-kbd-coding 'iso2022-8))
326 (setq encoded-kbd-iso2022-designations (make-vector 4 nil))
327 (let ((flags (coding-system-flags coding))
330 (if (charsetp (aref flags i))
331 (aset encoded-kbd-iso2022-designations i
333 (if (charsetp (car-safe (aref flags i)))
334 (aset encoded-kbd-iso2022-designations i
335 (car (aref flags i)))))
337 (setq encoded-kbd-iso2022-invocations (make-vector 3 nil))
338 (aset encoded-kbd-iso2022-invocations 0 0)
339 (aset encoded-kbd-iso2022-invocations 1 1))
341 ((= (coding-system-type coding) 3) ; BIG5
343 (nth 0 saved-input-mode) (nth 1 saved-input-mode)
344 'use-8th-bit (nth 3 saved-input-mode))
345 (setq encoded-kbd-coding 'big5))
347 ((= (coding-system-type coding) 4) ; CCL based coding
349 (nth 0 saved-input-mode) (nth 1 saved-input-mode)
350 'use-8th-bit (nth 3 saved-input-mode))
351 (setq encoded-kbd-coding 'ccl))
354 (setq encoded-kbd-mode nil)
355 (error "Coding-system `%s' is not supported in Encoded-kbd mode"
356 (keyboard-coding-system))))
357 (encoded-kbd-setup-keymap coding))))
359 ;;; encoded-kb.el ends here