Add arch taglines
[bpt/emacs.git] / lisp / language / china-util.el
index 9896d6d..cc7aaa6 100644 (file)
@@ -1,7 +1,8 @@
-;;; china-util.el --- utilities for Chinese
+;;; china-util.el --- utilities for Chinese  -*- coding: iso-2022-7bit -*-
 
-;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
+;; Copyright (C) 1995, 2003 Electrotechnical Laboratory, JAPAN.
 ;; Licensed to the Free Software Foundation.
+;; Copyright (C) 1995, 2001 Free Software Foundation, Inc.
 
 ;; Keywords: mule, multilingual, Chinese
 
 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
-;;; Code:
-
-;;;###autoload
-(defun setup-chinese-gb-environment ()
-  "Setup multilingual environment (MULE) for Chinese GB2312 users."
-  (interactive)
-  (setup-english-environment)
-
-  (setq primary-language "Chinese-GB")
-
-  (setq coding-category-iso-8-2 'chinese-iso-8bit)
-  (setq coding-category-iso-else 'chinese-iso-7bit)
-  (setq coding-category-big5 'chinese-big5)
-
-  (set-coding-priority
-   '(coding-category-iso-7
-     coding-category-iso-else
-     coding-category-iso-8-2
-     coding-category-big5
-     coding-category-iso-8-1
-     coding-category-emacs-mule))
-
-  (setq-default buffer-file-coding-system 'chinese-iso-8bit)
-  (set-terminal-coding-system-internal 'chinese-iso-8bit)
-  (set-keyboard-coding-system-internal 'chinese-iso-8bit)
-  (setq sendmail-coding-system nil
-       rmail-file-coding-system 'chinese-iso-7bit)
-
-  (setq default-input-method '("Chinese-GB" . "chinese-py")))
-
-;;;###autoload
-(defun setup-chinese-big5-environment ()
-  "Setup multilingual environment (MULE) for Chinese Big5 users."
-  (interactive)
-  (setup-english-environment)
-
-  (setq primary-language "Chinese-BIG5")
-
-  (setq coding-category-iso-8-2 'chinese-big5)
-  (setq coding-category-iso-else 'chinese-iso-7bit)
-  (setq coding-category-big5 'chinese-big5)
-
-  (set-coding-priority
-   '(coding-category-iso-7
-     coding-category-iso-else
-     coding-category-big5
-     coding-category-iso-8-2
-     coding-category-emacs-mule))
-
-  (setq-default buffer-file-coding-system 'chinese-big5)
-  (set-terminal-coding-system-internal 'chinese-big5)
-  (set-keyboard-coding-system-internal 'chinese-big5)
-  (setq sendmail-coding-system nil
-       rmail-file-coding-system 'chienese-iso-7bit)
-
-  (setq default-input-method '("Chinese-BIG5" . "chinese-b5-py")))
-
-;;;###autoload
-(defun setup-chinese-cns-environment ()
-  "Setup multilingual environment (MULE) for Chinese CNS11643 family users."
-  (interactive)
-  (setup-english-environment)
-
-  (setq primary-language "Chinese-CNS")
-
-  (setq coding-category-iso-else 'chinese-iso-7bit)
-  (setq coding-category-big5 'chinese-big5)
-  (setq coding-category-iso-8-2 'chinese-big5)
-
-  (set-coding-priority
-   '(coding-category-iso-7
-     coding-category-iso-else
-     coding-category-iso-8-2
-     coding-category-big5))
+;;; Commentary:
 
-  (setq-default buffer-file-coding-system 'chinese-iso-7bit)
-  (set-terminal-coding-system-internal 'chinese-iso-7bit)
-  (set-keyboard-coding-system-internal 'chinese-iso-7bit)
-  (setq sendmail-coding-system nil
-       rmail-file-coding-system 'chinese-iso-7bit)
-
-  (setq default-input-method '("Chinese-CNS" . "quail-quick-cns")))
+;;; Code:
 
-;; Hz/ZW encoding stuffs
+;; Hz/ZW/EUC-TW encoding stuff
 
 ;; HZ is an encoding method for Chinese character set GB2312 used
 ;; widely in Internet.  It is very similar to 7-bit environment of
 ;; encodes Chinese characters line by line by starting each line with
 ;; the sequence "zW".  It also uses only 7-bit as HZ.
 
+;; EUC-TW is similar to EUC-KS or EUC-JP.  Its main character set is
+;; plane 1 of CNS 11643; characters of planes 2 to 7 are accessed with
+;; a single shift escape followed by three bytes: the first gives the
+;; plane, the second and third the character code.  Note that characters
+;; of plane 1 are (redundantly) accessible with a single shift escape
+;; also.
+
 ;; ISO-2022 escape sequence to designate GB2312.
 (defvar iso2022-gb-designation "\e$A")
 ;; HZ escape sequence to designate GB2312.
 ;; Regexp of ZW sequence to start GB2312.
 (defvar zw-start-gb "^zW")
 ;; Regexp for start of GB2312 in an encoding mixture of HZ and ZW.
-(defvar hz/zw-start-gb (concat hz-gb-designnation "\\|" zw-start-gb))
+(defvar hz/zw-start-gb
+  (concat hz-gb-designnation "\\|" zw-start-gb "\\|[^\0-\177]"))
 
 (defvar decode-hz-line-continuation nil
   "Flag to tell if we should care line continuation convention of Hz.")
 
+(defconst hz-set-msb-table
+  (eval-when-compile
+    (let ((chars nil)
+         (i 0))
+      (while (< i 33)
+       (push i chars)
+       (setq i (1+ i)))
+      (while (< i 127)
+       (push (+ i 128) chars)
+       (setq i (1+ i)))
+      (apply 'string (nreverse chars)))))
+
 ;;;###autoload
 (defun decode-hz-region (beg end)
   "Decode HZ/ZW encoded text in the current region.
@@ -140,49 +82,46 @@ Return the length of resulting text."
   (interactive "r")
   (save-excursion
     (save-restriction
-      (narrow-to-region beg end)
-
-      ;; We, at first, convert HZ/ZW to `iso-2022-7bit',
-      ;; then decode it.
-
-      ;; "~\n" -> "\n"
-      (goto-char (point-min))
-      (while (search-forward "~" nil t)
-       (if (= (following-char) ?\n) (delete-char -1))
-       (if (not (eobp)) (forward-char 1)))
-
-      ;; "^zW...\n" -> Chinese GB2312
-      ;; "~{...~}"  -> Chinese GB2312
-      (goto-char (point-min))
-      (let ((chinese-found nil))
+      (let (pos ch)
+       (narrow-to-region beg end)
+
+       ;; We, at first, convert HZ/ZW to `euc-china',
+       ;; then decode it.
+
+       ;; "~\n" -> "\n", "~~" -> "~"
+       (goto-char (point-min))
+       (while (search-forward "~" nil t)
+         (setq ch (following-char))
+         (if (or (= ch ?\n) (= ch ?~)) (delete-char -1)))
+
+       ;; "^zW...\n" -> Chinese GB2312
+       ;; "~{...~}"  -> Chinese GB2312
+       (goto-char (point-min))
+       (setq beg nil)
        (while (re-search-forward hz/zw-start-gb nil t)
-         (if (= (char-after (match-beginning 0)) ?z)
-             ;; ZW -> iso-2022-7bit
-             (progn
-               (delete-char -2)
-               (insert iso2022-gb-designation)
-               (end-of-line)
-               (insert iso2022-ascii-designation))
-           ;; HZ -> iso-2022-7bit
-           (delete-char -2)
-           (insert iso2022-gb-designation)
-           (let ((pos (save-excursion (end-of-line) (point))))
-             (if (search-forward hz-ascii-designnation pos t)
-                 (replace-match iso2022-ascii-designation)
-               (if (not decode-hz-line-continuation)
-                   (insert iso2022-ascii-designation)))))
-         (setq chinese-found t))
-       (if (or chinese-found
-               (let ((enable-multibyte-characters nil))
-                 ;; Here we check if the text contains EUC (China) codes.
-                 ;; If any, we had better decode them also.
-                 (goto-char (point-min))
-                 (re-search-forward "[\240-\377]" nil t))) 
-           (decode-coding-region (point-min) (point-max) 'euc-china)))
-
-      ;; "~~" -> "~"
-      (goto-char (point-min))
-      (while (search-forward "~~" nil t) (delete-char -1))
+         (setq pos (match-beginning 0)
+               ch (char-after pos))
+         ;; Record the first position to start conversion.
+         (or beg (setq beg pos))
+         (end-of-line)
+         (setq end (point))
+         (if (>= ch 128)               ; 8bit GB2312
+             nil
+           (goto-char pos)
+           (delete-char 2)
+           (setq end (- end 2))
+           (if (= ch ?z)                       ; ZW -> euc-china
+               (progn
+                 (translate-region (point) end hz-set-msb-table)
+                 (goto-char end))
+             (if (search-forward hz-ascii-designnation
+                                 (if decode-hz-line-continuation nil end)
+                                 t)
+                 (delete-char -2))
+             (setq end (point))
+             (translate-region pos (point) hz-set-msb-table))))
+       (if beg
+           (decode-coding-region beg end 'euc-china)))
       (- (point-max) (point-min)))))
 
 ;;;###autoload
@@ -207,8 +146,7 @@ Return the length of resulting text."
       ;; Chinese GB2312 -> "~{...~}"
       (goto-char (point-min))
       (if (re-search-forward "\\cc" nil t)
-         (let ((enable-multibyte-characters nil)
-               pos)
+         (let (pos)
            (goto-char (setq pos (match-beginning 0)))
            (encode-coding-region pos (point-max) 'iso-2022-7bit)
            (goto-char pos)
@@ -227,7 +165,257 @@ Return the length of resulting text."
   (interactive)
   (encode-hz-region (point-min) (point-max)))
 
+;; The following sets up a translation table (big5-to-cns) from Big 5
+;; to CNS encoding, using some auxiliary functions to make the code
+;; more readable.
+
+;; Many kudos to Himi!  The used code has been adapted from his
+;; mule-ucs package.
+
+(eval-when-compile
+(defun big5-to-flat-code (num)
+  "Convert NUM in Big 5 encoding to a `flat code'.
+0xA140 will be mapped to position 0, 0xA141 to position 1, etc.
+There are no gaps in the flat code."
+
+  (let ((hi (/ num 256))
+        (lo (% num 256)))
+    (+ (* 157 (- hi #xa1))
+       (- lo (if (>= lo #xa1) 98 64)))))
+
+(defun flat-code-to-big5 (num)
+  "Convert NUM from a `flat code' to Big 5 encoding.
+This is the inverse function of `big5-to-flat-code'."
+
+  (let ((hi (/ num 157))
+        (lo (% num 157)))
+    (+ (* 256 (+ hi #xa1))
+       (+ lo (if (< lo 63) 64 98)))))
+
+(defun euc-to-flat-code (num)
+  "Convert NUM in EUC encoding (in GL representation) to a `flat code'.
+0x2121 will be mapped to position 0, 0x2122 to position 1, etc.
+There are no gaps in the flat code."
+
+  (let ((hi (/ num 256))
+        (lo (% num 256)))
+    (+ (* 94 (- hi #x21))
+       (- lo #x21))))
+
+(defun flat-code-to-euc (num)
+  "Convert NUM from a `flat code' to EUC encoding (in GL representation).
+The inverse function of `euc-to-flat-code'.  The high and low bytes are
+returned in a list."
+
+  (let ((hi (/ num 94))
+        (lo (% num 94)))
+    (list (+ hi #x21) (+ lo #x21))))
+
+(defun expand-euc-big5-alist (alist)
+  "Create a translation table and fills it with data given in ALIST.
+Elements of ALIST can be either given as
+
+  ((euc-charset . startchar) . (big5-range-begin . big5-range-end))
+
+or as
+
+  (euc-character . big5-charcode)
+
+The former maps a range of glyphs in an EUC charset (where STARTCHAR
+is in GL representation) to a certain range of Big 5 encoded
+characters, the latter maps a single glyph.  Glyphs which can't be
+mapped will be represented with the byte 0xFF.
+
+The return value is the filled translation table."
+
+  (let ((chartable (make-char-table 'translation-table #xFF))
+        char
+        big5
+        i
+        end
+        codepoint
+        charset)
+    (dolist (elem alist)
+      (setq char (car elem)
+            big5 (cdr elem))
+      (cond ((and (consp char)
+                  (consp big5))
+            (setq i (big5-to-flat-code (car big5))
+                  end (big5-to-flat-code (cdr big5))
+                  codepoint (euc-to-flat-code (cdr char))
+                  charset (car char))
+            (while (>= end i)
+              (aset chartable
+                    (decode-big5-char (flat-code-to-big5 i))
+                    (apply (function make-char)
+                           charset
+                           (flat-code-to-euc codepoint)))
+              (setq i (1+ i)
+                    codepoint (1+ codepoint))))
+            ((and (char-valid-p char)
+                  (numberp big5))
+            (setq i (decode-big5-char big5))
+            (aset chartable i char))
+            (t
+             (error "Unknown slot type: %S" elem))))
+    ;; the return value
+    chartable)))
+
+;; All non-CNS encodings are commented out.
+
+(define-translation-table 'big5-to-cns
+  (eval-when-compile
+  (expand-euc-big5-alist
+   '(
+     ;; Symbols
+     ((chinese-cns11643-1 . #x2121) . (#xA140 . #xA1F5))
+     (?\e$(G"X\e(B . #xA1F6)
+     (?\e$(G"W\e(B . #xA1F7)
+     ((chinese-cns11643-1 . #x2259) . (#xA1F8 . #xA2AE))
+     ((chinese-cns11643-1 . #x2421) . (#xA2AF . #xA3BF))
+     ;; Control codes (vendor dependent)
+     ((chinese-cns11643-1 . #x4221) . (#xA3C0 . #xA3E0))
+     ;; Level 1 Ideographs
+     ((chinese-cns11643-1 . #x4421) . (#xA440 . #xACFD))
+     (?\e$(GWS\e(B . #xACFE)
+     ((chinese-cns11643-1 . #x5323) . (#xAD40 . #xAFCF))
+     ((chinese-cns11643-1 . #x5754) . (#xAFD0 . #xBBC7))
+     ((chinese-cns11643-1 . #x6B51) . (#xBBC8 . #xBE51))
+     (?\e$(GkP\e(B . #xBE52)
+     ((chinese-cns11643-1 . #x6F5C) . (#xBE53 . #xC1AA))
+     ((chinese-cns11643-1 . #x7536) . (#xC1AB . #xC2CA))
+     (?\e$(Gu5\e(B . #xC2CB)
+     ((chinese-cns11643-1 . #x7737) . (#xC2CC . #xC360))
+     ((chinese-cns11643-1 . #x782E) . (#xC361 . #xC3B8))
+     (?\e$(Gxe\e(B . #xC3B9)
+     (?\e$(Gxd\e(B . #xC3BA)
+     ((chinese-cns11643-1 . #x7866) . (#xC3BB . #xC455))
+     (?\e$(Gx-\e(B . #xC456)
+     ((chinese-cns11643-1 . #x7962) . (#xC457 . #xC67E))
+     ;; Symbols
+     ((chinese-cns11643-1 . #x2621) . (#xC6A1 . #xC6BE))
+     ;; Radicals
+     (?\e$(G'#\e(B . #xC6BF)
+     (?\e$(G'$\e(B . #xC6C0)
+     (?\e$(G'&\e(B . #xC6C1)
+     (?\e$(G'(\e(B . #xC6C2)
+     (?\e$(G'-\e(B . #xC6C3)
+     (?\e$(G'.\e(B . #xC6C4)
+     (?\e$(G'/\e(B . #xC6C5)
+     (?\e$(G'4\e(B . #xC6C6)
+     (?\e$(G'7\e(B . #xC6C7)
+     (?\e$(G':\e(B . #xC6C8)
+     (?\e$(G'<\e(B . #xC6C9)
+     (?\e$(G'B\e(B . #xC6CA)
+     (?\e$(G'G\e(B . #xC6CB)
+     (?\e$(G'N\e(B . #xC6CC)
+     (?\e$(G'S\e(B . #xC6CD)
+     (?\e$(G'T\e(B . #xC6CE)
+     (?\e$(G'U\e(B . #xC6CF)
+     (?\e$(G'Y\e(B . #xC6D0)
+     (?\e$(G'Z\e(B . #xC6D1)
+     (?\e$(G'a\e(B . #xC6D2)
+     (?\e$(G'f\e(B . #xC6D3)
+     (?\e$(G()\e(B . #xC6D4)
+     (?\e$(G(*\e(B . #xC6D5)
+     (?\e$(G(c\e(B . #xC6D6)
+     (?\e$(G(l\e(B . #xC6D7)
+     ;; Diacritical Marks
+     ; ((japanese-jisx0208 . #x212F) . (#xC6D8 . #xC6D9))
+     ;; Japanese Kana Supplement
+     ; ((japanese-jisx0208 . #x2133) . (#xC6DA . #xC6E3))
+     ;; Japanese Hiragana
+     ; ((japanese-jisx0208 . #x2421) . (#xC6E7 . #xC77A))
+     ;; Japanese Katakana
+     ; ((japanese-jisx0208 . #x2521) . (#xC77B . #xC7F2))
+     ;; Cyrillic Characters
+     ; ((japanese-jisx0208 . #x2721) . (#xC7F3 . #xC854))
+     ; ((japanese-jisx0208 . #x2751) . (#xC855 . #xC875))
+     ;; Special Chinese Characters
+     (?\e$(J!#\e(B . #xC879)
+     (?\e$(J!$\e(B . #xC87B)
+     (?\e$(J!*\e(B . #xC87D)
+     (?\e$(J!R\e(B . #xC8A2)
+
+     ;; JIS X 0208 NOT SIGN (cf. U+00AC)
+     ; (?\e$B"L\e(B . #xC8CD)
+     ;; JIS X 0212 BROKEN BAR (cf. U+00A6)
+     ; (?\e$(D"C\e(B . #xC8CE)
+
+     ;; GB 2312 characters
+     ; (?\e$A!d\e(B . #xC8CF)
+     ; (?\e$A!e\e(B . #xC8D0)
+        ;;;;; C8D1 - Japanese `(\e$B3t\e(B)'
+     ; (?\e$A!m\e(B . #xC8D2)
+        ;;;;; C8D2 - Tel.
+
+     ;; Level 2 Ideographs
+     ((chinese-cns11643-2 . #x2121) . (#xC940 . #xC949))
+     (?\e$(GDB\e(B . #xC94A);; a duplicate of #xA461
+     ((chinese-cns11643-2 . #x212B) . (#xC94B . #xC96B))
+     ((chinese-cns11643-2 . #x214D) . (#xC96C . #xC9BD))
+     (?\e$(H!L\e(B . #xC9BE)
+     ((chinese-cns11643-2 . #x217D) . (#xC9BF . #xC9EC))
+     ((chinese-cns11643-2 . #x224E) . (#xC9ED . #xCAF6))
+     (?\e$(H"M\e(B . #xCAF7)
+     ((chinese-cns11643-2 . #x2439) . (#xCAF8 . #xD6CB))
+     (?\e$(H>c\e(B . #xD6CC)
+     ((chinese-cns11643-2 . #x3770) . (#xD6CD . #xD779))
+     (?\e$(H?j\e(B . #xD77A)
+     ((chinese-cns11643-2 . #x387E) . (#xD77B . #xDADE))
+     (?\e$(H7o\e(B . #xDADF)
+     ((chinese-cns11643-2 . #x3E64) . (#xDAE0 . #xDBA6))
+     ((chinese-cns11643-2 . #x3F6B) . (#xDBA7 . #xDDFB))
+     (?\e$(HAv\e(B . #xDDFC);; a duplicate of #xDCD1
+     ((chinese-cns11643-2 . #x4424) . (#xDDFD . #xE8A2))
+     ((chinese-cns11643-2 . #x554C) . (#xE8A3 . #xE975))
+     ((chinese-cns11643-2 . #x5723) . (#xE976 . #xEB5A))
+     ((chinese-cns11643-2 . #x5A29) . (#xEB5B . #xEBF0))
+     (?\e$(HUK\e(B . #xEBF1)
+     ((chinese-cns11643-2 . #x5B3F) . (#xEBF2 . #xECDD))
+     (?\e$(HW"\e(B . #xECDE)
+     ((chinese-cns11643-2 . #x5C6A) . (#xECDF . #xEDA9))
+     ((chinese-cns11643-2 . #x5D75) . (#xEDAA . #xEEEA))
+     (?\e$(Hd/\e(B . #xEEEB)
+     ((chinese-cns11643-2 . #x6039) . (#xEEEC . #xF055))
+     (?\e$(H]t\e(B . #xF056)
+     ((chinese-cns11643-2 . #x6243) . (#xF057 . #xF0CA))
+     (?\e$(HZ(\e(B . #xF0CB)
+     ((chinese-cns11643-2 . #x6337) . (#xF0CC . #xF162))
+     ((chinese-cns11643-2 . #x6430) . (#xF163 . #xF16A))
+     (?\e$(Hga\e(B . #xF16B)
+     ((chinese-cns11643-2 . #x6438) . (#xF16C . #xF267))
+     (?\e$(Hi4\e(B . #xF268)
+     ((chinese-cns11643-2 . #x6573) . (#xF269 . #xF2C2))
+     ((chinese-cns11643-2 . #x664E) . (#xF2C3 . #xF374))
+     ((chinese-cns11643-2 . #x6762) . (#xF375 . #xF465))
+     ((chinese-cns11643-2 . #x6935) . (#xF466 . #xF4B4))
+     (?\e$(HfM\e(B . #xF4B5)
+     ((chinese-cns11643-2 . #x6962) . (#xF4B6 . #xF4FC))
+     ((chinese-cns11643-2 . #x6A4C) . (#xF4FD . #xF662))
+     (?\e$(HjK\e(B . #xF663)
+     ((chinese-cns11643-2 . #x6C52) . (#xF664 . #xF976))
+     ((chinese-cns11643-2 . #x7167) . (#xF977 . #xF9C3))
+     (?\e$(Hqf\e(B . #xF9C4)
+     (?\e$(Hr4\e(B . #xF9C5)
+     (?\e$(Hr@\e(B . #xF9C6)
+     ((chinese-cns11643-2 . #x7235) . (#xF9C7 . #xF9D1))
+     ((chinese-cns11643-2 . #x7241) . (#xF9D2 . #xF9D5))
+
+     ;; Additional Ideographs
+     (?\e$(IC7\e(B . #xF9D6)
+     (?\e$(IOP\e(B . #xF9D7)
+     (?\e$(IDN\e(B . #xF9D8)
+     (?\e$(IPJ\e(B . #xF9D9)
+     (?\e$(I,]\e(B . #xF9DA)
+     (?\e$(I=~\e(B . #xF9DB)
+     (?\e$(IK\\e(B . #xF9DC)
+    )
+  ))
+)
+
 ;;
 (provide 'china-util)
 
+;;; arch-tag: 5a47b084-b9ac-420e-8191-70c5b3a14836
 ;;; china-util.el ends here