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.
23 (defvar encoded-kbd-mode nil
24 "Non-nil if in Encoded-kbd minor mode.")
25 (put 'encoded-kbd-mode 'permanent-local t)
27 (or (assq 'encoded-kbd-mode minor-mode-alist)
28 (setq minor-mode-alist
29 (cons '(encoded-kbd-mode " Encoded-kbd") minor-mode-alist)))
31 (defvar encoded-kbd-mode-map
32 (let ((map (make-sparse-keymap))
34 (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix)
36 (define-key map (vector i) 'encoded-kbd-handle-8bit)
39 "Keymap for Encoded-kbd minor mode.")
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)))
46 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
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)
56 "Keymap for handling ESC code in Encoded-kbd mode.")
57 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map)
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)
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)
73 (defvar encoded-kbd-iso2022-designation-map
74 (let ((map (make-sparse-keymap))
78 (char-to-string (charset-iso-final-char (car l)))
79 'encoded-kbd-iso2022-designation)
82 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
83 (fset 'encoded-kbd-iso2022-designation-prefix
84 encoded-kbd-iso2022-designation-map)
86 (defvar encoded-kbd-iso2022-non-ascii-map
87 (let ((map (make-keymap))
90 (define-key map (char-to-string i) 'encoded-kbd-self-insert-iso2022-7bit)
92 (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix)
95 (define-key map (vector i) 'encoded-kbd-handle-8bit)
98 "Keymap for handling non-ASCII character set in Encoded-kbd mode.")
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
103 (defvar encoded-kbd-coding nil)
105 ;; Keep information of designation state of ISO2022 encoding. This is
106 ;; a vector of character sets currently designated to each graphic
109 (defvar encoded-kbd-iso2022-designations nil)
110 (put 'encoded-kbd-iso2022-designations 'permanent-local t)
112 ;; Keep information of invocation state of ISO2022 encoding. This is
113 ;; a vector of graphic register numbers currently invoked to each
114 ;; graphic plane (0..1), the third element is a single shifted graphic
117 (defvar encoded-kbd-iso2022-invocations nil)
118 (put 'encoded-kbd-iso2022-invocations 'permanent-local t)
120 (defun encoded-kbd-iso2022-designation ()
121 "Do ISO2022 designation according to the current key in Encoded-kbd mode.
122 The following key sequence may cause multilingual text insertion."
124 (let ((key-seq (this-command-keys))
125 (prev-g0-charset (aref encoded-kbd-iso2022-designations
126 (aref encoded-kbd-iso2022-invocations 0)))
127 intermediate-char final-char
128 reg dimension chars charset)
129 (if (= (length key-seq) 4)
130 ;; ESC $ <intermediate-char> <final-char>
131 (setq intermediate-char (aref key-seq 2)
133 chars (if (< intermediate-char ?,) 94 96)
134 final-char (aref key-seq 3)
135 reg (mod intermediate-char 4))
136 (if (= (aref key-seq 1) ?$)
137 ;; ESC $ <final-char>
140 final-char (aref key-seq 2)
142 ;; ESC <intermediate-char> <final-char>
143 (setq intermediate-char (aref key-seq 1)
145 chars (if (< intermediate-char ?,) 94 96)
146 final-char (aref key-seq 2)
147 reg (mod intermediate-char 4))))
148 (if (setq charset (iso-charset dimension chars final-char))
149 (aset encoded-kbd-iso2022-designations reg charset)
150 (error "Character set of DIMENSION %s, CHARS %s, FINAL-CHAR `%c' is not supported"
151 dimension chars final-char))
153 (if (memq (aref encoded-kbd-iso2022-designations
154 (aref encoded-kbd-iso2022-invocations 0))
155 '(ascii latin-jisx0201))
156 ;; Graphic plane 0 (0x20..0x7f) is for ASCII. We don't have
157 ;; to handle characters in this range specially.
158 (if (not (memq prev-g0-charset '(ascii latin-jisx0201)))
159 ;; We must exit recursive edit now.
161 ;; Graphic plane 0 is for non-ASCII.
162 (if (memq prev-g0-charset '(ascii latin-jisx0201))
163 ;; We must handle keys specially.
164 (let ((overriding-local-map encoded-kbd-iso2022-non-ascii-map))
165 (recursive-edit))))))
167 (defun encoded-kbd-handle-8bit ()
168 "Handle an 8-bit character entered in Encoded-kbd mode."
170 (cond ((eq encoded-kbd-coding 'iso2022-7)
171 (error "Can't handle the character code %d" last-command-char))
173 ((eq encoded-kbd-coding 'iso2022-8)
174 (cond ((= last-command-char ?\216)
175 (aset encoded-kbd-iso2022-invocations 2 2))
177 ((= last-command-char ?\217)
178 (aset encoded-kbd-iso2022-invocations 2 3))
180 ((> last-command-char ?\240)
181 (encoded-kbd-self-insert-iso2022-8bit))
184 (error "Can't handle the character code %d"
185 last-command-char))))
187 ((eq encoded-kbd-coding 'sjis)
188 (encoded-kbd-self-insert-sjis))
191 (encoded-kbd-self-insert-big5))))
193 (defun encoded-kbd-self-insert-iso2022-7bit ()
195 (let* ((charset (aref encoded-kbd-iso2022-designations
196 (or (aref encoded-kbd-iso2022-invocations 2)
197 (aref encoded-kbd-iso2022-invocations 0))))
199 (if (= (charset-dimension charset) 1)
200 (make-char charset last-command-char)
201 (make-char charset last-command-char (read-char-exclusive)))))
202 (self-insert-command 1)
203 (aset encoded-kbd-iso2022-invocations 2 nil)
206 (defun encoded-kbd-self-insert-iso2022-8bit ()
208 (let* ((charset (aref encoded-kbd-iso2022-designations
209 (or (aref encoded-kbd-iso2022-invocations 2)
210 (aref encoded-kbd-iso2022-invocations 1))))
212 (if (= (charset-dimension charset) 1)
213 (make-char charset last-command-char)
214 (make-char charset last-command-char (read-char-exclusive)))))
215 (self-insert-command 1)
216 (aset encoded-kbd-iso2022-invocations 2 nil)
219 (defun encoded-kbd-self-insert-sjis ()
221 (let ((last-command-char
222 (if (or (< last-command-char ?\xA0) (>= last-command-char ?\xE0))
223 (decode-sjis-char (+ (ash last-command-char 8)
224 (read-char-exclusive)))
225 (make-char 'katakana-jisx0201 last-command-char))))
226 (self-insert-command 1)))
228 (defun encoded-kbd-self-insert-big5 ()
230 (let ((last-command-char
231 (decode-big5-char (+ (ash last-command-char 8)
232 (read-char-exclusive)))))
233 (self-insert-command 1)))
235 ;; Input mode at the time Encoded-kbd mode is turned on is saved here.
236 (defvar saved-input-mode nil)
239 (defun encoded-kbd-mode (&optional arg)
240 "Toggle Encoded-kbd minor mode.
241 With arg, turn Encoded-kbd mode on if and only if arg is positive.
243 You should not turn this mode on manually, instead use the command
244 \\[set-keyboard-coding-system] which turns on or off this mode
247 In Encoded-kbd mode, a text sent from keyboard is accepted
248 as a multilingual text encoded in a coding system set by
249 \\[set-keyboard-coding-system]."
251 ;; We must at first reset input-mode to the original.
252 (apply 'set-input-mode saved-input-mode))
253 (setq encoded-kbd-mode
254 (if (null arg) (null encoded-kbd-mode)
255 (> (prefix-numeric-value arg) 0)))
257 (let ((coding (keyboard-coding-system)))
258 (setq saved-input-mode (current-input-mode))
260 (setq encoded-kbd-mode nil)
261 (error "No coding system for keyboard input is set"))
263 ((= (coding-system-type coding) 1) ; SJIS
265 (nth 0 saved-input-mode) (nth 1 saved-input-mode)
266 'use-8th-bit (nth 3 saved-input-mode))
267 (setq encoded-kbd-coding 'sjis))
269 ((= (coding-system-type coding) 2) ; ISO2022
270 (if (aref (coding-system-flags coding) 7) ; 7-bit only
271 (setq encoded-kbd-coding 'iso2022-7)
273 (nth 0 saved-input-mode) (nth 1 saved-input-mode)
274 'use-8th-bit (nth 3 saved-input-mode))
275 (setq encoded-kbd-coding 'iso2022-8))
276 (setq encoded-kbd-iso2022-designations (make-vector 4 nil))
277 (let ((flags (coding-system-flags coding))
280 (if (charsetp (aref flags i))
281 (aset encoded-kbd-iso2022-designations i
283 (if (charsetp (car-safe (aref flags i)))
284 (aset encoded-kbd-iso2022-designations i
285 (car (aref flags i)))))
287 (setq encoded-kbd-iso2022-invocations (make-vector 3 nil))
288 (aset encoded-kbd-iso2022-invocations 0 0)
289 (aset encoded-kbd-iso2022-invocations 1 1))
291 ((= (coding-system-type coding) 3) ; BIG5
293 (nth 0 saved-input-mode) (nth 1 saved-input-mode)
294 'use-8th-bit (nth 3 saved-input-mode))
295 (setq encoded-kbd-coding 'big5))
298 (setq encoded-kbd-mode nil)
299 (error "Coding-system `%s' is not supported in Encoded-kbd mode"
300 (keyboard-coding-system))))
301 (run-hooks 'encoded-kbd-mode-hook))))
303 ;;; encoded-kb.el ends here