1 ;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
31 ;; Usually this map is empty (even if Encoded-kbd mode is on), but if
32 ;; the keyboard coding system is iso-2022-based, it defines dummy key
33 ;; bindings for ESC $ ..., etc. so that those bindings in
34 ;; input-decode-map take effect.
35 (defconst encoded-kbd-mode-map
(make-sparse-keymap)
36 "Keymap for Encoded-kbd minor mode.")
38 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
40 (defvar encoded-kbd-iso2022-esc-map
41 (let ((map (make-sparse-keymap)))
42 (define-key map
"$" 'encoded-kbd-iso2022-esc-dollar-prefix
)
43 (define-key map
"(" 'encoded-kbd-iso2022-designation-prefix
)
44 (define-key map
")" 'encoded-kbd-iso2022-designation-prefix
)
45 (define-key map
"," 'encoded-kbd-iso2022-designation-prefix
)
46 (define-key map
"-" 'encoded-kbd-iso2022-designation-prefix
)
48 "Keymap for handling ESC code in Encoded-kbd mode.")
49 (fset 'encoded-kbd-iso2022-esc-prefix encoded-kbd-iso2022-esc-map
)
51 (defvar encoded-kbd-iso2022-esc-dollar-map
52 (let ((map (make-sparse-keymap)))
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 (define-key map
"@" 'encoded-kbd-iso2022-designation
)
58 (define-key map
"A" 'encoded-kbd-iso2022-designation
)
59 (define-key map
"B" 'encoded-kbd-iso2022-designation
)
61 "Keymap for handling ESC $ sequence in Encoded-kbd mode.")
62 (fset 'encoded-kbd-iso2022-esc-dollar-prefix
63 encoded-kbd-iso2022-esc-dollar-map
)
65 (defvar encoded-kbd-iso2022-designation-map
66 (let ((map (make-sparse-keymap))
70 (setq final-char
(charset-iso-final-char (car l
)))
72 (define-key map
(char-to-string final-char
)
73 'encoded-kbd-iso2022-designation
))
76 "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
77 (fset 'encoded-kbd-iso2022-designation-prefix
78 encoded-kbd-iso2022-designation-map
)
80 ;; Keep information of designation state of ISO2022 encoding. When
81 ;; Encoded-kbd mode is on, this is set to a vector of length 4, the
82 ;; elements are character sets currently designated to graphic
83 ;; registers 0 thru 3.
85 (defvar encoded-kbd-iso2022-designations nil
)
86 (put 'encoded-kbd-iso2022-designations
'permanent-local t
)
88 ;; Keep information of invocation state of ISO2022 encoding. When
89 ;; Encoded-kbd mode is on, this is set to a vector of length 3,
90 ;; graphic register numbers currently invoked to graphic plane 1 and
91 ;; 2, and a single shifted graphic register number.
93 (defvar encoded-kbd-iso2022-invocations nil
)
94 (put 'encoded-kbd-iso2022-invocations
'permanent-local t
)
96 (defsubst encoded-kbd-last-key
()
97 (let ((keys (this-single-command-keys)))
98 (aref keys
(1- (length keys
)))))
100 (defun encoded-kbd-iso2022-designation (ignore)
101 "Do ISO2022 designation according to the current key in Encoded-kbd mode.
102 The following key sequence may cause multilingual text insertion."
103 (let ((key-seq (this-single-command-keys))
104 (prev-g0-charset (aref encoded-kbd-iso2022-designations
105 (aref encoded-kbd-iso2022-invocations
0)))
106 intermediate-char final-char
107 reg dimension chars charset
)
108 (if (= (length key-seq
) 4)
109 ;; ESC $ <intermediate-char> <final-char>
110 (setq intermediate-char
(aref key-seq
2)
112 chars
(if (< intermediate-char ?
,) 94 96)
113 final-char
(aref key-seq
3)
114 reg
(mod intermediate-char
4))
115 (if (= (aref key-seq
1) ?$
)
116 ;; ESC $ <final-char>
119 final-char
(aref key-seq
2)
121 ;; ESC <intermediate-char> <final-char>
122 (setq intermediate-char
(aref key-seq
1)
124 chars
(if (< intermediate-char ?
,) 94 96)
125 final-char
(aref key-seq
2)
126 reg
(mod intermediate-char
4))))
127 (aset encoded-kbd-iso2022-designations reg
128 (iso-charset dimension chars final-char
)))
131 (defun encoded-kbd-iso2022-single-shift (ignore)
132 (let ((char (encoded-kbd-last-key)))
133 (aset encoded-kbd-iso2022-invocations
2 (if (= char ?
\216) 2 3)))
136 (defun encoded-kbd-self-insert-iso2022-7bit (ignore)
137 (let ((char (encoded-kbd-last-key))
138 (charset (aref encoded-kbd-iso2022-designations
139 (or (aref encoded-kbd-iso2022-invocations
2)
140 (aref encoded-kbd-iso2022-invocations
0)))))
141 (aset encoded-kbd-iso2022-invocations
2 nil
)
142 (vector (if (= (charset-dimension charset
) 1)
143 (make-char charset char
)
144 (make-char charset char
(read-char-exclusive))))))
146 (defun encoded-kbd-self-insert-iso2022-8bit (ignore)
147 (let ((char (encoded-kbd-last-key))
148 (charset (aref encoded-kbd-iso2022-designations
149 (or (aref encoded-kbd-iso2022-invocations
2)
150 (aref encoded-kbd-iso2022-invocations
1)))))
151 (aset encoded-kbd-iso2022-invocations
2 nil
)
152 (vector (if (= (charset-dimension charset
) 1)
153 (make-char charset char
)
154 (make-char charset char
(read-char-exclusive))))))
156 (defun encoded-kbd-self-insert-sjis (ignore)
157 (let ((char (encoded-kbd-last-key)))
159 (if (or (< char ?
\xA0) (>= char ?
\xE0))
160 (decode-sjis-char (+ (ash char
8) (read-char-exclusive)))
161 (make-char 'katakana-jisx0201 char
)))))
163 (defun encoded-kbd-self-insert-big5 (ignore)
164 (let ((char (encoded-kbd-last-key)))
166 (decode-big5-char (+ (ash char
8) (read-char-exclusive))))))
168 (defun encoded-kbd-self-insert-ccl (ignore)
169 (let ((str (char-to-string (encoded-kbd-last-key)))
170 (ccl (car (aref (coding-system-spec (keyboard-coding-system)) 4)))
171 (vec [nil nil nil nil nil nil nil nil nil
])
173 (while (= (length (setq result
(ccl-execute-on-string ccl vec str t
))) 0)
174 (dotimes (i 9) (aset vec i nil
))
175 (setq str
(format "%s%c" str
(read-char-exclusive))))
176 (vector (aref result
0))))
178 (defun encoded-kbd-setup-keymap (keymap coding
)
179 ;; At first, reset the keymap.
180 (define-key encoded-kbd-mode-map
"\e" nil
)
181 ;; Then setup the keymap according to the keyboard coding system.
183 ((eq (coding-system-type coding
) 1) ; SJIS
187 (vector i
) 'encoded-kbd-self-insert-sjis
)
191 ((eq (coding-system-type coding
) 3) ; Big5
195 (vector i
) 'encoded-kbd-self-insert-big5
)
199 ((eq (coding-system-type coding
) 2) ; ISO-2022
200 (let ((flags (coding-system-flags coding
))
203 nil
; Don't support locking-shift.
204 (setq encoded-kbd-iso2022-designations
(make-vector 4 nil
)
205 encoded-kbd-iso2022-invocations
(make-vector 3 nil
))
208 (if (charsetp (aref flags i
))
209 (aset encoded-kbd-iso2022-designations
211 (setq use-designation t
)
212 (if (charsetp (car-safe (aref flags i
)))
213 (aset encoded-kbd-iso2022-designations
214 i
(car (aref flags i
)))))))
215 (aset encoded-kbd-iso2022-invocations
0 0)
216 (if (aref encoded-kbd-iso2022-designations
1)
217 (aset encoded-kbd-iso2022-invocations
1 1))
218 (when use-designation
219 (define-key encoded-kbd-mode-map
"\e" 'encoded-kbd-iso2022-esc-prefix
)
220 (define-key keymap
"\e" 'encoded-kbd-iso2022-esc-prefix
))
221 (when (or (aref flags
2) (aref flags
3))
223 [?
\216] 'encoded-kbd-iso2022-single-shift
)
225 [?
\217] 'encoded-kbd-iso2022-single-shift
))
226 (or (eq (aref flags
0) 'ascii
)
229 (vector (+ 32 i
)) 'encoded-kbd-self-insert-iso2022-7bit
)))
234 (vector (+ 160 i
)) 'encoded-kbd-self-insert-iso2022-8bit
))
237 ((eq (coding-system-type coding
) 4) ; CCL-base
238 (let ((valid-codes (or (coding-system-get coding
'valid-codes
)
242 (setq elt
(car valid-codes
) valid-codes
(cdr valid-codes
))
244 (setq from
(car elt
) to
(cdr elt
))
245 (setq from
(setq to elt
)))
249 (vector from
) 'encoded-kbd-self-insert-ccl
))
250 (setq from
(1+ from
))))
257 (defun encoded-kbd-setup-display (display)
258 "Set up a `input-decode-map' for `keyboard-coding-system' on DISPLAY.
260 DISPLAY may be a display id, a frame, or nil for the selected frame's display."
261 (let ((frame (if (framep display
) display
(car (frames-on-display-list display
)))))
263 (with-selected-frame frame
264 ;; Remove any previous encoded-kb keymap from input-decode-map.
265 (let ((m input-decode-map
)
268 (if (not (equal (keymap-prompt m
) "encoded-kb"))
271 (setq m
(keymap-parent m
)))
272 ;; We've found an encoded-kb map, but maybe the prompt we get
273 ;; is really inherited from the encoded-kb map.
275 (while (and (keymapp (setq mp
(keymap-parent m
)))
276 (equal (keymap-prompt mp
) "encoded-kb"))
279 ;; (assert (equal (keymap-prompt m) "encoded-kb"))
280 ;; (assert (eq mp (keymap-parent m)))
281 ;; (assert (not (and (keymapp mp)
282 ;; (equal (keymap-prompt mp) "encoded-kb"))))
283 ;; (assert (eq m (if child
284 ;; (keymap-parent child) input-decode-map)))
285 ;; We can finally do the actual removal.
287 (set-keymap-parent child mp
)
288 (setq input-decode-map mp
))
291 (if (keyboard-coding-system)
292 ;; We are turning on Encoded-kbd mode.
293 (let ((coding (keyboard-coding-system))
294 (keymap (make-sparse-keymap "encoded-kb"))
295 (cim (current-input-mode))
297 ;; Place `keymap' as the immediate parent of input-decode-map
298 ;; rather than on top, so that later `define-key' on
299 ;; input-decode-map don't end up accidentally changing our
300 ;; part of the keymap, which would lead to bugs when/if we later
301 ;; on remove that part.
302 (set-keymap-parent keymap
(keymap-parent input-decode-map
))
303 (set-keymap-parent input-decode-map keymap
)
304 (unless (terminal-parameter nil
'encoded-kbd-saved-input-meta-mode
)
305 (set-terminal-parameter nil
'encoded-kbd-saved-input-mode
307 (setq result
(and coding
(encoded-kbd-setup-keymap keymap coding
)))
309 (when (and (eq result
8)
310 (memq (nth 2 cim
) '(t nil
)))
311 (set-input-meta-mode 'use-8th-bit
))
312 (set-terminal-parameter
313 nil
'encoded-kbd-saved-input-meta-mode nil
)
314 (error "Unsupported coding system in Encoded-kbd mode: %S"
316 ;; We are turning off Encoded-kbd mode.
317 (let ((old (terminal-parameter nil
'encoded-kbd-saved-input-meta-mode
)))
318 (when (and old
(not (equal (nth 2 (current-input-mode)) old
)))
319 (set-input-meta-mode old
))
320 (set-terminal-parameter
321 nil
'encoded-kbd-saved-input-meta-mode nil
))))))
323 (provide 'encoded-kb
)
325 ;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
326 ;;; encoded-kb.el ends here