(defun setup-chinese-gb-environment ()
"Setup multilingual environment (MULE) for Chinese GB2312 users."
(interactive)
(set-language-environment "Chinese-GB"))
(defun setup-chinese-big5-environment ()
"Setup multilingual environment (MULE) for Chinese Big5 users."
(interactive)
(set-language-environment "Chinese-BIG5"))
(defun setup-chinese-cns-environment ()
"Setup multilingual environment (MULE) for Chinese CNS11643 family users."
(interactive)
(set-language-environment "Chinese-CNS"))
(defvar iso2022-gb-designation "\e$A")
(defvar hz-gb-designnation "~{")
(defvar iso2022-ascii-designation "\e(B")
(defvar hz-ascii-designnation "~}")
(defvar zw-start-gb "^zW")
(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))
(defun decode-hz-region (beg end)
"Decode HZ/ZW encoded text in the current region.
Return the length of resulting text."
(interactive "r")
(save-excursion
(save-restriction
(let (pos ch)
(narrow-to-region beg end)
(goto-char (point-min))
(while (search-forward "~" nil t)
(setq ch (following-char))
(if (or (= ch ?\n) (= ch ?~)) (delete-char -1)))
(goto-char (point-min))
(setq beg nil)
(while (re-search-forward hz/zw-start-gb nil t)
(setq pos (match-beginning 0)
ch (char-after pos))
(or beg (setq beg pos))
(end-of-line)
(setq end (point))
(if (>= ch 128) nil
(goto-char pos)
(delete-char 2)
(setq end (- end 2))
(if (= ch ?z) (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)))))
(defun decode-hz-buffer ()
"Decode HZ/ZW encoded text in the current buffer."
(interactive)
(decode-hz-region (point-min) (point-max)))
(defun encode-hz-region (beg end)
"Encode the text in the current region to HZ.
Return the length of resulting text."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(while (search-forward "~" nil t) (insert ?~))
(goto-char (point-min))
(if (re-search-forward "\\cc" nil t)
(let (pos)
(goto-char (setq pos (match-beginning 0)))
(encode-coding-region pos (point-max) 'iso-2022-7bit)
(goto-char pos)
(while (search-forward iso2022-gb-designation nil t)
(delete-char -3)
(insert hz-gb-designnation))
(goto-char pos)
(while (search-forward iso2022-ascii-designation nil t)
(delete-char -3)
(insert hz-ascii-designnation))))
(- (point-max) (point-min)))))
(defun encode-hz-buffer ()
"Encode the text in the current buffer to HZ."
(interactive)
(encode-hz-region (point-min) (point-max)))
(provide 'china-util)