(devanagari-range): Renamed from `range'. All calls changed.
[bpt/emacs.git] / lisp / language / china-util.el
index 1466121..8a0a83d 100644 (file)
@@ -1,7 +1,7 @@
 ;;; china-util.el --- utilities for Chinese
 
-;; Copyright (C) 1995 Free Software Foundation, Inc.
 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
+;; Licensed to the Free Software Foundation.
 
 ;; 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 'cn-gb-2312)
-  (setq coding-category-iso-else 'iso-2022-cn)
-  (setq coding-category-big5 'cn-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 'cn-gb-2312)
-  (set-terminal-coding-system-internal 'cn-gb-2312)
-  (set-keyboard-coding-system-internal 'cn-gb-2312)
-  (setq sendmail-coding-system nil
-       rmail-file-coding-system 'iso-2022-cn)
-
-  (setq default-input-method '("Chinese-GB" . "quail-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 'cn-big5)
-  (setq coding-category-iso-else 'iso-2022-cn)
-  (setq coding-category-big5 'cn-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 'cn-big5)
-  (set-terminal-coding-system-internal 'cn-big5)
-  (set-keyboard-coding-system-internal 'cn-big5)
-  (setq sendmail-coding-system nil
-       rmail-file-coding-system 'iso-2022-cn)
-
-  (setq default-input-method '("Chinese-BIG5" . "quail-py-b5")))
+;;; Commentary:
 
-;;;###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 'iso-2022-cn)
-  (setq coding-category-big5 'cn-big5)
-  (setq coding-category-iso-8-2 'cn-big5)
-
-  (set-coding-priority
-   '(coding-category-iso-7
-     coding-category-iso-else
-     coding-category-iso-8-2
-     coding-category-big5))
-
-  (setq-default buffer-file-coding-system 'iso-2022-cn)
-  (set-terminal-coding-system-internal 'iso-2022-cn)
-  (set-keyboard-coding-system-internal 'iso-2022-cn)
-  (setq sendmail-coding-system nil
-       rmail-file-coding-system 'iso-2022-cn)
-
-  (setq default-input-method '("Chinese-CNS" . "quail-quick-cns")))
+;;; Code:
 
 ;; Hz/ZW encoding stuffs
 
 ;; 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
+  (let ((str (make-string 127 0))
+       (i 0))
+    (while (< i 33)
+      (aset str i i)
+      (setq i (1+ i)))
+    (while (< i 127)
+      (aset str i (+ i 128))
+      (setq i (1+ i)))
+    str))
+
 ;;;###autoload
 (defun decode-hz-region (beg end)
   "Decode HZ/ZW encoded text in the current region.
@@ -140,49 +73,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-7',
-      ;; 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-20227-7
-             (progn
-               (delete-char -2)
-               (insert iso2022-gb-designation)
-               (end-of-line)
-               (insert iso2022-ascii-designation))
-           ;; HZ -> iso-20227-7
-           (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,10 +137,9 @@ 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-7)
+           (encode-coding-region pos (point-max) 'iso-2022-7bit)
            (goto-char pos)
            (while (search-forward iso2022-gb-designation nil t)
              (delete-char -3)
@@ -228,9 +157,6 @@ Return the length of resulting text."
   (encode-hz-region (point-min) (point-max)))
 
 ;;
-(provide 'language/china-util)
+(provide 'china-util)
 
-;;; Local Variables:
-;;; generated-autoload-file: "../loaddefs.el"
-;;; End:
 ;;; china-util.el ends here