(latexenc-find-file-coding-system):
[bpt/emacs.git] / lisp / international / encoded-kb.el
CommitLineData
60370d40 1;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
4ed46869 2
2fd125a3
KH
3;; Copyright (C) 1997 Free Software Foundation, Inc.
4;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2004, 2005
5;; National Institute of Advanced Industrial Science and Technology (AIST)
6;; Registration Number H14PRO021
4ed46869
KH
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
369314dc
KH
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
4ed46869 24
60370d40
PJ
25;;; Commentary:
26
27;;; Code:
28
5dda46a3
KH
29;; Usually this map is empty (even if Encoded-kbd mode is on), but if
30;; the keyboard coding system is iso-2022-based, it defines dummy key
31;; bindings for ESC $ ..., etc. so that those bindings in
32;; key-translation-map take effect.
068b074e 33(defconst encoded-kbd-mode-map (make-sparse-keymap)
4ed46869
KH
34 "Keymap for Encoded-kbd minor mode.")
35
4ed46869
KH
36;; Subsidiary keymaps for handling ISO2022 escape sequences.
37
38(defvar encoded-kbd-iso2022-esc-map
39 (let ((map (make-sparse-keymap)))
40 (define-key map "$" 'encoded-kbd-iso2022-esc-dollar-prefix)
41 (define-key map "(" 'encoded-kbd-iso2022-designation-prefix)
42 (define-key map ")" 'encoded-kbd-iso2022-designation-prefix)
43 (define-key map "," 'encoded-kbd-iso2022-designation-prefix)
44 (define-key map "-" 'encoded-kbd-iso2022-designation-prefix)
4ed46869
KH
45 map)
46 "Keymap for handling ESC code in Encoded-kbd mode.")
f61d15a7 47(fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map)
4ed46869
KH
48
49(defvar encoded-kbd-iso2022-esc-dollar-map
50 (let ((map (make-sparse-keymap)))
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 (define-key map "@" 'encoded-kbd-iso2022-designation)
56 (define-key map "A" 'encoded-kbd-iso2022-designation)
57 (define-key map "B" 'encoded-kbd-iso2022-designation)
4ed46869 58 map)
f61d15a7 59 "Keymap for handling ESC $ sequence in Encoded-kbd mode.")
4ed46869
KH
60(fset 'encoded-kbd-iso2022-esc-dollar-prefix
61 encoded-kbd-iso2022-esc-dollar-map)
62
63(defvar encoded-kbd-iso2022-designation-map
64 (let ((map (make-sparse-keymap))
164edac8
KH
65 (l charset-list)
66 final-char)
f61d15a7 67 (while l
164edac8 68 (setq final-char (charset-iso-final-char (car l)))
71b898fe 69 (if (> final-char 0)
164edac8
KH
70 (define-key map (char-to-string final-char)
71 'encoded-kbd-iso2022-designation))
f61d15a7 72 (setq l (cdr l)))
4ed46869
KH
73 map)
74 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
75(fset 'encoded-kbd-iso2022-designation-prefix
76 encoded-kbd-iso2022-designation-map)
77
068b074e
KH
78;; Keep information of designation state of ISO2022 encoding. When
79;; Encoded-kbd mode is on, this is set to a vector of length 4, the
80;; elements are character sets currently designated to graphic
81;; registers 0 thru 3.
4ed46869
KH
82
83(defvar encoded-kbd-iso2022-designations nil)
4ed46869
KH
84(put 'encoded-kbd-iso2022-designations 'permanent-local t)
85
068b074e
KH
86;; Keep information of invocation state of ISO2022 encoding. When
87;; Encoded-kbd mode is on, this is set to a vector of length 3,
88;; graphic register numbers currently invoked to graphic plane 1 and
89;; 2, and a single shifted graphic register number.
4ed46869
KH
90
91(defvar encoded-kbd-iso2022-invocations nil)
4ed46869
KH
92(put 'encoded-kbd-iso2022-invocations 'permanent-local t)
93
5dda46a3
KH
94(defsubst encoded-kbd-last-key ()
95 (let ((keys (this-single-command-keys)))
96 (aref keys (1- (length keys)))))
97
98(defun encoded-kbd-iso2022-designation (ignore)
e8dd0160 99 "Do ISO2022 designation according to the current key in Encoded-kbd mode.
4ed46869 100The following key sequence may cause multilingual text insertion."
5dda46a3 101 (let ((key-seq (this-single-command-keys))
a0d4676a
KH
102 (prev-g0-charset (aref encoded-kbd-iso2022-designations
103 (aref encoded-kbd-iso2022-invocations 0)))
4ed46869
KH
104 intermediate-char final-char
105 reg dimension chars charset)
f61d15a7
KH
106 (if (= (length key-seq) 4)
107 ;; ESC $ <intermediate-char> <final-char>
108 (setq intermediate-char (aref key-seq 2)
4ed46869
KH
109 dimension 2
110 chars (if (< intermediate-char ?,) 94 96)
f61d15a7 111 final-char (aref key-seq 3)
4ed46869
KH
112 reg (mod intermediate-char 4))
113 (if (= (aref key-seq 1) ?$)
f61d15a7 114 ;; ESC $ <final-char>
4ed46869
KH
115 (setq dimension 2
116 chars 94
f61d15a7 117 final-char (aref key-seq 2)
4ed46869 118 reg 0)
f61d15a7
KH
119 ;; ESC <intermediate-char> <final-char>
120 (setq intermediate-char (aref key-seq 1)
4ed46869
KH
121 dimension 1
122 chars (if (< intermediate-char ?,) 94 96)
f61d15a7 123 final-char (aref key-seq 2)
4ed46869 124 reg (mod intermediate-char 4))))
5dda46a3
KH
125 (aset encoded-kbd-iso2022-designations reg
126 (iso-charset dimension chars final-char)))
127 "")
128
129(defun encoded-kbd-iso2022-single-shift (ignore)
130 (let ((char (encoded-kbd-last-key)))
e1a050dc 131 (aset encoded-kbd-iso2022-invocations 2 (if (= char ?\216) 2 3)))
5dda46a3
KH
132 "")
133
134(defun encoded-kbd-self-insert-iso2022-7bit (ignore)
135 (let ((char (encoded-kbd-last-key))
136 (charset (aref encoded-kbd-iso2022-designations
137 (or (aref encoded-kbd-iso2022-invocations 2)
138 (aref encoded-kbd-iso2022-invocations 0)))))
4ed46869 139 (aset encoded-kbd-iso2022-invocations 2 nil)
5dda46a3
KH
140 (vector (if (= (charset-dimension charset) 1)
141 (make-char charset char)
142 (make-char charset char (read-char-exclusive))))))
143
144(defun encoded-kbd-self-insert-iso2022-8bit (ignore)
145 (let ((char (encoded-kbd-last-key))
146 (charset (aref encoded-kbd-iso2022-designations
147 (or (aref encoded-kbd-iso2022-invocations 2)
148 (aref encoded-kbd-iso2022-invocations 1)))))
149 (aset encoded-kbd-iso2022-invocations 2 nil)
150 (vector (if (= (charset-dimension charset) 1)
151 (make-char charset char)
152 (make-char charset char (read-char-exclusive))))))
153
154(defun encoded-kbd-self-insert-sjis (ignore)
155 (let ((char (encoded-kbd-last-key)))
156 (vector
157 (if (or (< char ?\xA0) (>= char ?\xE0))
158 (decode-sjis-char (+ (ash char 8) (read-char-exclusive)))
159 (make-char 'katakana-jisx0201 char)))))
160
161(defun encoded-kbd-self-insert-big5 (ignore)
162 (let ((char (encoded-kbd-last-key)))
163 (vector
164 (decode-big5-char (+ (ash char 8) (read-char-exclusive))))))
165
166(defun encoded-kbd-self-insert-ccl (ignore)
167 (let ((str (char-to-string (encoded-kbd-last-key)))
4c8d5b4b 168 (ccl (car (aref (coding-system-spec (keyboard-coding-system)) 4)))
81a23e88 169 (vec [nil nil nil nil nil nil nil nil nil])
4c8d5b4b
KH
170 result)
171 (while (= (length (setq result (ccl-execute-on-string ccl vec str t))) 0)
81a23e88
KH
172 (dotimes (i 9) (aset vec i nil))
173 (setq str (format "%s%c" str (read-char-exclusive))))
5dda46a3 174 (vector (aref result 0))))
068b074e
KH
175
176(defun encoded-kbd-setup-keymap (coding)
177 ;; At first, reset the keymap.
5dda46a3 178 (define-key encoded-kbd-mode-map "\e" nil)
068b074e
KH
179 ;; Then setup the keymap according to the keyboard coding system.
180 (cond
5dda46a3 181 ((eq (coding-system-type coding) 1) ; SJIS
068b074e
KH
182 (let ((i 128))
183 (while (< i 256)
af1781ac 184 (define-key key-translation-map
068b074e 185 (vector i) 'encoded-kbd-self-insert-sjis)
5dda46a3
KH
186 (setq i (1+ i))))
187 8)
068b074e 188
5dda46a3 189 ((eq (coding-system-type coding) 3) ; Big5
068b074e
KH
190 (let ((i 161))
191 (while (< i 255)
af1781ac 192 (define-key key-translation-map
068b074e 193 (vector i) 'encoded-kbd-self-insert-big5)
5dda46a3
KH
194 (setq i (1+ i))))
195 8)
196
197 ((eq (coding-system-type coding) 2) ; ISO-2022
198 (let ((flags (coding-system-flags coding))
199 use-designation)
200 (if (aref flags 8)
201 nil ; Don't support locking-shift.
202 (setq encoded-kbd-iso2022-designations (make-vector 4 nil)
203 encoded-kbd-iso2022-invocations (make-vector 3 nil))
204 (dotimes (i 4)
205 (if (aref flags i)
206 (if (charsetp (aref flags i))
207 (aset encoded-kbd-iso2022-designations
208 i (aref flags i))
209 (setq use-designation t)
210 (if (charsetp (car-safe (aref flags i)))
211 (aset encoded-kbd-iso2022-designations
212 i (car (aref flags i)))))))
213 (aset encoded-kbd-iso2022-invocations 0 0)
214 (if (aref encoded-kbd-iso2022-designations 1)
215 (aset encoded-kbd-iso2022-invocations 1 1))
216 (when use-designation
217 (define-key encoded-kbd-mode-map "\e" 'encoded-kbd-iso2022-esc-prefix)
218 (define-key key-translation-map "\e" 'encoded-kbd-iso2022-esc-prefix))
219 (when (or (aref flags 2) (aref flags 3))
220 (define-key key-translation-map
221 [?\216] 'encoded-kbd-iso2022-single-shift)
222 (define-key key-translation-map
223 [?\217] 'encoded-kbd-iso2022-single-shift))
224 (or (eq (aref flags 0) 'ascii)
225 (dotimes (i 96)
226 (define-key key-translation-map
227 (vector (+ 32 i)) 'encoded-kbd-self-insert-iso2022-7bit)))
228 (if (aref flags 7)
229 t
230 (dotimes (i 96)
231 (define-key key-translation-map
232 (vector (+ 160 i)) 'encoded-kbd-self-insert-iso2022-8bit))
233 8))))
234
235 ((eq (coding-system-type coding) 4) ; CCL-base
068b074e 236 (let ((valid-codes (or (coding-system-get coding 'valid-codes)
4c8d5b4b 237 '((128 . 255))))
5dda46a3 238 elt from to valid)
068b074e
KH
239 (while valid-codes
240 (setq elt (car valid-codes) valid-codes (cdr valid-codes))
241 (if (consp elt)
242 (setq from (car elt) to (cdr elt))
243 (setq from (setq to elt)))
244 (while (<= from to)
245 (if (>= from 128)
5dda46a3 246 (define-key key-translation-map
068b074e 247 (vector from) 'encoded-kbd-self-insert-ccl))
5dda46a3
KH
248 (setq from (1+ from))))
249 8))
068b074e
KH
250
251 (t
5dda46a3 252 nil)))
068b074e 253
5dda46a3
KH
254;; key-translation-map at the time Encoded-kbd mode is turned on is
255;; saved here.
256(defvar saved-key-translation-map nil)
068b074e 257
586ec68a
KH
258;; Input mode at the time Encoded-kbd mode is turned on is saved here.
259(defvar saved-input-mode nil)
260
ff14aafb 261(put 'encoded-kbd-mode 'permanent-local t)
f61d15a7 262;;;###autoload
ff14aafb 263(define-minor-mode encoded-kbd-mode
4ed46869 264 "Toggle Encoded-kbd minor mode.
586ec68a 265With arg, turn Encoded-kbd mode on if and only if arg is positive.
4ed46869 266
a0d4676a 267You should not turn this mode on manually, instead use the command
f7e5a632 268\\[set-keyboard-coding-system] which turns on or off this mode
a0d4676a
KH
269automatically.
270
271In Encoded-kbd mode, a text sent from keyboard is accepted
272as a multilingual text encoded in a coding system set by
f7e5a632 273\\[set-keyboard-coding-system]."
64a2e5ca 274 :global t :group 'keyboard :group 'mule
5dda46a3 275
4ed46869 276 (if encoded-kbd-mode
5dda46a3
KH
277 ;; We are turning on Encoded-kbd mode.
278 (let ((coding (keyboard-coding-system))
279 result)
280 (or saved-key-translation-map
281 (if (keymapp key-translation-map)
282 (setq saved-key-translation-map
283 (copy-keymap key-translation-map))
284 (setq key-translation-map (make-sparse-keymap))))
285 (or saved-input-mode
286 (setq saved-input-mode
287 (current-input-mode)))
288 (setq result (and coding (encoded-kbd-setup-keymap coding)))
289 (if result
290 (if (eq result 8)
291 (set-input-mode
292 (nth 0 saved-input-mode)
293 (nth 1 saved-input-mode)
294 'use-8th-bit
295 (nth 3 saved-input-mode)))
296 (setq encoded-kbd-mode nil
297 saved-key-translation-map nil
298 saved-input-mode nil)
299 (error "Unsupported coding system in Encoded-kbd mode: %S"
300 coding)))
301
302 ;; We are turning off Encoded-kbd mode.
303 (setq key-translation-map saved-key-translation-map
304 saved-key-translation-map nil)
305 (apply 'set-input-mode saved-input-mode)
306 (setq saved-input-mode nil)))
4ed46869 307
9c93c95a
MR
308(provide 'encoded-kb)
309
ab5796a9 310;;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
4ed46869 311;;; encoded-kb.el ends here