]> code.delx.au - gnu-emacs/blobdiff - lisp/international/encoded-kb.el
(set-buffer-file-coding-system): Finish previous change.
[gnu-emacs] / lisp / international / encoded-kb.el
index b63a7a39f6398d7606dfe4e7daddcda3653505ea..cba2f0e45b994840c0509de8a710e40853183f81 100644 (file)
@@ -1,7 +1,7 @@
-;;; encoded-kb.el --- Handler to input multibyte characters encoded somehow
+;;; encoded-kb.el --- handler to input multibyte characters encoded somehow
 
-;; Copyright (C) 1995 Free Software Foundation, Inc.
 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
+;; Licensed to the Free Software Foundation.
 
 ;; This file is part of GNU Emacs.
 
 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
-(defvar encoded-kbd-mode nil
-  "Non-nil if in Encoded-kbd minor mode.")
-(put 'encoded-kbd-mode 'permanent-local t)
+;;; Commentary:
 
-(or (assq 'encoded-kbd-mode minor-mode-alist)
-    (setq minor-mode-alist
-         (cons '(encoded-kbd-mode " Encoded-kbd") minor-mode-alist)))
+;;; Code:
 
-(defvar encoded-kbd-mode-map
-  (let ((map (make-sparse-keymap))
-       (i 128))
-    (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix)
-    (while (< i 256)
-      (define-key map (vector i) 'encoded-kbd-handle-8bit)
-      (setq i (1+ i)))
-    map)
+;; Usually this map is empty (even if Encoded-kbd mode is on), but if
+;; the keyboard coding system is iso-2022-based, it defines dummy key
+;; bindings for ESC $ ..., etc. so that those bindings in
+;; key-translation-map take effect.
+(defconst encoded-kbd-mode-map (make-sparse-keymap)
   "Keymap for Encoded-kbd minor mode.")
 
-(or (assq 'encoded-kbd-mode minor-mode-map-alist)
-    (setq minor-mode-map-alist
-         (cons (cons 'encoded-kbd-mode encoded-kbd-mode-map)
-               minor-mode-map-alist)))
-
 ;; Subsidiary keymaps for handling ISO2022 escape sequences.
 
 (defvar encoded-kbd-iso2022-esc-map
 
 (defvar encoded-kbd-iso2022-designation-map
   (let ((map (make-sparse-keymap))
-       (l charset-list))
+       (l charset-list)
+       final-char)
     (while l
-      (define-key map
-       (char-to-string (charset-iso-final-char (car l)))
-       'encoded-kbd-iso2022-designation)
+      (setq final-char (charset-iso-final-char (car l)))
+      (if (> final-char 0)
+         (define-key map (char-to-string final-char)
+           'encoded-kbd-iso2022-designation))
       (setq l (cdr l)))
     map)
   "Keymap for handling ISO2022 designation sequence in Encoded-kbd mode.")
 (fset 'encoded-kbd-iso2022-designation-prefix
       encoded-kbd-iso2022-designation-map)
 
-(defvar encoded-kbd-iso2022-non-ascii-map
-  (let ((map (make-keymap))
-       (i 32))
-    (while (< i 128)
-      (define-key map (char-to-string i) 'encoded-kbd-self-insert-iso2022-7bit)
-      (setq i (1+ i)))
-    (define-key map "\e" 'encoded-kbd-iso2022-esc-prefix)
-    map)
-  "Keymap for handling non-ASCII character set in Encoded-kbd mode.")
-
-;; One of the symbols `sjis', `iso2022-7', `iso2022-8', or `big5' to
-;; denote what kind of coding-system we are now handling in
-;; Encoded-kbd mode.
-(defvar encoded-kbd-coding nil)
-
-;; Keep information of designation state of ISO2022 encoding.  This is
-;; a vector of character sets currently designated to each graphic
-;; registers (0..3).
+;; Keep information of designation state of ISO2022 encoding.  When
+;; Encoded-kbd mode is on, this is set to a vector of length 4, the
+;; elements are character sets currently designated to graphic
+;; registers 0 thru 3.
 
 (defvar encoded-kbd-iso2022-designations nil)
-(make-variable-buffer-local 'encoded-kbd-iso2022-designations)
 (put 'encoded-kbd-iso2022-designations 'permanent-local t)
 
-;; Keep information of invocation state of ISO2022 encoding.  This is
-;; a vector of graphic register numbers currently invoked to each
-;; graphic plane (0..1), the third element is a single shifted graphic
-;; register number.
+;; Keep information of invocation state of ISO2022 encoding.  When
+;; Encoded-kbd mode is on, this is set to a vector of length 3,
+;; graphic register numbers currently invoked to graphic plane 1 and
+;; 2, and a single shifted graphic register number.
 
 (defvar encoded-kbd-iso2022-invocations nil)
-(make-variable-buffer-local 'encoded-kbd-iso2022-invocations)
 (put 'encoded-kbd-iso2022-invocations 'permanent-local t)
 
-(defun encoded-kbd-iso2022-designation ()
-  "Do ISO2022 designation according to the curren key in Encoded-kbd mode.
+(defsubst encoded-kbd-last-key ()
+  (let ((keys (this-single-command-keys)))
+    (aref keys (1- (length keys)))))
+
+(defun encoded-kbd-iso2022-designation (ignore)
+  "Do ISO2022 designation according to the current key in Encoded-kbd mode.
 The following key sequence may cause multilingual text insertion."
-  (interactive)
-  (let ((key-seq (this-command-keys))
+  (let ((key-seq (this-single-command-keys))
+       (prev-g0-charset (aref encoded-kbd-iso2022-designations
+                              (aref encoded-kbd-iso2022-invocations 0)))
        intermediate-char final-char
        reg dimension chars charset)
     (if (= (length key-seq) 4)
@@ -141,147 +120,192 @@ The following key sequence may cause multilingual text insertion."
              chars (if (< intermediate-char ?,) 94 96)
              final-char (aref key-seq 2)
              reg (mod intermediate-char 4))))
-    (if (setq charset (iso-charset dimension chars final-char))
-       (aset encoded-kbd-iso2022-designations reg charset)
-      (error "Character set of DIMENSION %s, CHARS %s, FINAL-CHAR `%c' is not supported"
-            dimension chars final-char))
-
-    (if (memq (aref encoded-kbd-iso2022-designations
-                (aref encoded-kbd-iso2022-invocations 0))
-             '(ascii latin-jisx0201))
-       ;; Graphic plane 0 (0x20..0x7f) is for ASCII.  We don't have
-       ;; to handle characters in this range specially.
-       (throw 'exit nil)
-      ;; Graphic plane 0 is for non-ASCII.
-      (let ((overriding-local-map encoded-kbd-iso2022-non-ascii-map))
-       (recursive-edit)))))
-
-(defun encoded-kbd-handle-8bit ()
-  "Handle an 8-bit character enterned in Encoded-kbd mode."
-  (interactive)
-  (cond ((eq encoded-kbd-coding 'iso2022-7)
-        (error "Can't handle the character code %d" last-command-char))
-
-       ((eq encoded-kbd-coding 'iso2022-8)
-        (cond ((= last-command-char ?\216)
-               (aset encoded-kbd-iso2022-invocations 2 2))
-
-              ((= last-command-char ?\217)
-               (aset encoded-kbd-iso2022-invocations 2 3))
-
-              ((> last-command-char ?\240)
-               (encoded-kbd-self-insert-iso2022-8bit))
-
-              (t
-               (error "Can't handle the character code %d"
-                      last-command-char))))
-
-       ((eq encoded-kbd-coding 'sjis)
-        (encoded-kbd-self-insert-sjis))
-
-       (t
-        (encoded-kbd-self-insert-big5))))
-
-(defun encoded-kbd-self-insert-iso2022-7bit ()
-  (interactive)
-  (let* ((charset (aref encoded-kbd-iso2022-designations
-                       (or (aref encoded-kbd-iso2022-invocations 2)
-                           (aref encoded-kbd-iso2022-invocations 0))))
-        (last-command-char
-         (if (= (charset-bytes charset) 1)
-             (make-char charset last-command-char)
-           (make-char charset last-command-char (read-char-exclusive)))))
-    (self-insert-command 1)
+    (aset encoded-kbd-iso2022-designations reg
+         (iso-charset dimension chars final-char)))
+  "")
+
+(defun encoded-kbd-iso2022-single-shift (ignore)
+  (let ((char (encoded-kbd-last-key)))
+    (aset encoded-kbd-iso2022-invocations 2
+         (aref encoded-kbd-iso2022-designations
+               (if (= char ?\216) 2 3))))
+  "")
+
+(defun encoded-kbd-self-insert-iso2022-7bit (ignore)
+  (let ((char (encoded-kbd-last-key))
+       (charset (aref encoded-kbd-iso2022-designations
+                      (or (aref encoded-kbd-iso2022-invocations 2)
+                          (aref encoded-kbd-iso2022-invocations 0)))))
     (aset encoded-kbd-iso2022-invocations 2 nil)
-    ))
-
-(defun encoded-kbd-self-insert-iso2022-8bit ()
-  (interactive)
-  (let* ((charset (aref encoded-kbd-iso2022-designations
-                       (or (aref encoded-kbd-iso2022-invocations 2)
-                           (aref encoded-kbd-iso2022-invocations 1))))
-        (last-command-char
-         (if (= (charset-bytes charset) 1)
-             (make-char charset last-command-char)
-           (make-char charset last-command-char (read-char-exclusive)))))
-    (self-insert-command 1)
+    (vector (if (= (charset-dimension charset) 1)
+               (make-char charset char)
+             (make-char charset char (read-char-exclusive))))))
+
+(defun encoded-kbd-self-insert-iso2022-8bit (ignore)
+  (let ((char (encoded-kbd-last-key))
+       (charset (aref encoded-kbd-iso2022-designations
+                      (or (aref encoded-kbd-iso2022-invocations 2)
+                          (aref encoded-kbd-iso2022-invocations 1)))))
     (aset encoded-kbd-iso2022-invocations 2 nil)
-    ))
-
-(defun encoded-kbd-self-insert-sjis ()
-  (interactive)
-  (let ((last-command-char
-        (if (or (< last-command-char ?\xA0) (>= last-command-char ?\xE0))
-            (decode-sjis-char (+ (ash last-command-char 8)
-                                 (read-char-exclusive)))
-          (make-char 'latin-jisx0201 last-command-char))))
-    (self-insert-command 1)))
-
-(defun encoded-kbd-self-insert-big5 ()
-  (interactive)
-  (let ((last-command-char
-        (decode-big5-char (+ (ash last-command-char 8)
-                             (read-char-exclusive)))))
-    (self-insert-command 1)))
+    (vector (if (= (charset-dimension charset) 1)
+               (make-char charset char)
+             (make-char charset char (read-char-exclusive))))))
+
+(defun encoded-kbd-self-insert-sjis (ignore)
+  (let ((char (encoded-kbd-last-key)))
+    (vector
+     (if (or (< char ?\xA0) (>= char ?\xE0))
+        (decode-sjis-char (+ (ash char 8) (read-char-exclusive)))
+       (make-char 'katakana-jisx0201 char)))))
+
+(defun encoded-kbd-self-insert-big5 (ignore)
+  (let ((char (encoded-kbd-last-key)))
+    (vector 
+     (decode-big5-char (+ (ash char 8) (read-char-exclusive))))))
+
+(defun encoded-kbd-self-insert-ccl (ignore)
+  (let ((str (char-to-string (encoded-kbd-last-key)))
+       (ccl (car (aref (coding-system-spec (keyboard-coding-system)) 4)))
+       (vec [nil nil nil nil nil nil nil nil nil])
+       result)
+    (while (= (length (setq result (ccl-execute-on-string ccl vec str t))) 0)
+      (dotimes (i 9) (aset vec i nil))
+      (setq str (format "%s%c" str (read-char-exclusive))))
+    (vector (aref result 0))))
+
+(defun encoded-kbd-setup-keymap (coding)
+  ;; At first, reset the keymap.
+  (define-key encoded-kbd-mode-map "\e" nil)
+  ;; Then setup the keymap according to the keyboard coding system.
+  (cond
+   ((eq (coding-system-type coding) 1) ; SJIS
+    (let ((i 128))
+      (while (< i 256)
+       (define-key key-translation-map
+         (vector i) 'encoded-kbd-self-insert-sjis)
+       (setq i (1+ i))))
+    8)
+
+   ((eq (coding-system-type coding) 3) ; Big5
+    (let ((i 161))
+      (while (< i 255)
+       (define-key key-translation-map
+         (vector i) 'encoded-kbd-self-insert-big5)
+       (setq i (1+ i))))
+    8)
+
+   ((eq (coding-system-type coding) 2) ; ISO-2022
+    (let ((flags (coding-system-flags coding))
+         use-designation)
+      (if (aref flags 8)
+         nil                           ; Don't support locking-shift.
+       (setq encoded-kbd-iso2022-designations (make-vector 4 nil)
+             encoded-kbd-iso2022-invocations (make-vector 3 nil))
+       (dotimes (i 4)
+         (if (aref flags i)
+             (if (charsetp (aref flags i))
+                 (aset encoded-kbd-iso2022-designations
+                       i (aref flags i))
+               (setq use-designation t)
+               (if (charsetp (car-safe (aref flags i)))
+                   (aset encoded-kbd-iso2022-designations
+                         i (car (aref flags i)))))))
+       (aset encoded-kbd-iso2022-invocations 0 0)
+       (if (aref encoded-kbd-iso2022-designations 1)
+           (aset encoded-kbd-iso2022-invocations 1 1))
+       (when use-designation
+         (define-key encoded-kbd-mode-map "\e" 'encoded-kbd-iso2022-esc-prefix)
+         (define-key key-translation-map "\e" 'encoded-kbd-iso2022-esc-prefix))
+       (when (or (aref flags 2) (aref flags 3))
+         (define-key key-translation-map
+           [?\216] 'encoded-kbd-iso2022-single-shift)
+         (define-key key-translation-map
+           [?\217] 'encoded-kbd-iso2022-single-shift))
+       (or (eq (aref flags 0) 'ascii)
+           (dotimes (i 96)
+             (define-key key-translation-map
+               (vector (+ 32 i)) 'encoded-kbd-self-insert-iso2022-7bit)))
+       (if (aref flags 7)
+           t
+         (dotimes (i 96)
+           (define-key key-translation-map
+             (vector (+ 160 i)) 'encoded-kbd-self-insert-iso2022-8bit))
+         8))))
+
+   ((eq (coding-system-type coding) 4) ; CCL-base
+    (let ((valid-codes (or (coding-system-get coding 'valid-codes)
+                          '((128 . 255))))
+         elt from to valid)
+      (while valid-codes
+       (setq elt (car valid-codes) valid-codes (cdr valid-codes))
+       (if (consp elt)
+           (setq from (car elt) to (cdr elt))
+         (setq from (setq to elt)))
+       (while (<= from to)
+         (if (>= from 128)
+             (define-key key-translation-map
+               (vector from) 'encoded-kbd-self-insert-ccl))
+         (setq from (1+ from))))
+      8))
+
+   (t
+    nil)))
+
+;; key-translation-map at the time Encoded-kbd mode is turned on is
+;; saved here.
+(defvar saved-key-translation-map nil)
+
+;; Input mode at the time Encoded-kbd mode is turned on is saved here.
+(defvar saved-input-mode nil)
 
+(put 'encoded-kbd-mode 'permanent-local t)
 ;;;###autoload
-(defun encoded-kbd-mode (&optional arg)
+(define-minor-mode encoded-kbd-mode
   "Toggle Encoded-kbd minor mode.
-With arg, turn Keyboard-kbd mode on if and only if arg is positive.
-
-When in Encoded-kbd mode, a text sent from a terminal keyboard
-is accepted as a multilingual text encoded in a coding system
-set by the command `encoded-kbd-set-coding-system'"
-  (interactive "P")
-  (setq encoded-kbd-mode
-       (if (null arg) (null encoded-kbd-mode)
-         (> (prefix-numeric-value arg) 0)))
-  (if encoded-kbd-mode
-      (let* ((coding (coding-system-vector (keyboard-coding-system)))
-            (input-mode (current-input-mode)))
-       (cond ((null coding)
-              (setq encoded-kbd-mode nil) 
-              (error "No coding-system for terminal keyboard is set"))
-
-             ((= (coding-vector-type coding) 1) ; SJIS
-              (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
-                              'use-8th-bit (nth 3 input-mode)) 
-              (setq encoded-kbd-coding 'sjis))
-
-             ((= (coding-vector-type coding) 2) ; ISO2022
-              (if (aref (coding-vector-flags coding) 7) ; 7-bit only
-                  (setq encoded-kbd-coding 'iso2022-7)
-                (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
-                                'use-8th-bit (nth 3 input-mode))       
-                (setq encoded-kbd-coding 'iso2022-8))
-              (make-variable-buffer-local 'encoded-kbd-iso2022-designations)
-              (setq encoded-kbd-iso2022-designations (make-vector 4 nil))
-              (let ((flags (coding-vector-flags coding))
-                    (i 0))
-                (while (< i 4)
-                  (if (charsetp (aref flags i))
-                      (aset encoded-kbd-iso2022-designations i
-                            (aref flags i)))
-                  (setq i (1+ i))))
-              (make-variable-buffer-local 'encoded-kbd-iso2022-invocations)
-              (setq encoded-kbd-iso2022-invocations (make-vector 3 nil))
-              (aset encoded-kbd-iso2022-invocations 0 0)
-              (aset encoded-kbd-iso2022-invocations 1 1))
-
-             ((= (coding-vector-type coding) 3) ; BIG5
-              (set-input-mode (nth 0 input-mode) (nth 1 input-mode)
-                              'use-8th-bit (nth 3 input-mode)) 
-              (setq encoded-kbd-coding 'big5))
-
-             (t
-              (setq encoded-kbd-mode nil)
-              (error "Coding-system `%s' is not supported in Encoded-kbd mode"
-                     (keyboard-coding-system))))
-       (setq inactivate-current-input-method-function 'encoded-kbd-mode)
-       (setq describe-current-input-method-function 'encoded-kbd-mode-help)
-       (run-hooks 'encoded-kbd-mode-hook))
-    (setq describe-current-input-method-function nil)
-    (setq current-input-method nil))
-  (force-mode-line-update))
+With arg, turn Encoded-kbd mode on if and only if arg is positive.
 
+You should not turn this mode on manually, instead use the command
+\\[set-keyboard-coding-system] which turns on or off this mode
+automatically.
+
+In Encoded-kbd mode, a text sent from keyboard is accepted
+as a multilingual text encoded in a coding system set by
+\\[set-keyboard-coding-system]."
+  :global t
+
+  (if encoded-kbd-mode
+      ;; We are turning on Encoded-kbd mode.
+      (let ((coding (keyboard-coding-system))
+           result)
+       (or saved-key-translation-map
+           (if (keymapp key-translation-map)
+               (setq saved-key-translation-map
+                     (copy-keymap key-translation-map))
+             (setq key-translation-map (make-sparse-keymap))))
+       (or saved-input-mode
+           (setq saved-input-mode
+                 (current-input-mode)))
+       (setq result (and coding (encoded-kbd-setup-keymap coding)))
+       (if result
+           (if (eq result 8)
+               (set-input-mode
+                (nth 0 saved-input-mode) 
+                (nth 1 saved-input-mode)
+                'use-8th-bit
+                (nth 3 saved-input-mode)))
+         (setq encoded-kbd-mode nil
+               saved-key-translation-map nil
+               saved-input-mode nil)
+         (error "Unsupported coding system in Encoded-kbd mode: %S"
+                coding)))
+
+    ;; We are turning off Encoded-kbd mode.
+    (setq key-translation-map saved-key-translation-map
+         saved-key-translation-map nil)
+    (apply 'set-input-mode saved-input-mode)
+    (setq saved-input-mode nil)))
+
+(provide 'encoded-kb)
+
+;;; arch-tag: 76f0f9b3-65e7-45c3-b692-59509a87ad44
 ;;; encoded-kb.el ends here