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