;;; mule5-gbk.el --- gbk coding system for Mule version 5 ;; Copyright (C) 2007 Katsumi Yamaoka ;; Author: Katsumi Yamaoka ;; Keywords: gbk, coding-system ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; This junk provides the gbk coding system limitedly for GNU Emacs ;; 21.4 and 22.1 (and also for Emacs under development to which the ;; Unicode-2 feature has not yet been merged). It will be useful for ;; reading and writing gbk mails. You need to have installed the ;; iconv program that supports utf-8 and gbk. If you use Emacs 21.4, ;; you need to also use the Mule-UCS package to have the utf-8 coding ;; system that supports CJK text. ;; Note that encoding of partial contents by `encode-coding-region' in ;; a buffer won't work properly even if the region is narrowed; use it ;; for the whole contents of a buffer or use `encode-coding-string' ;; for partial contents instead. In addition, buffer's contents will ;; be broken (i.e. they will be encoded) when writing them to a file. ;;; Code: (eval-and-compile (if (or (featurep 'xemacs) (and (not (boundp 'mule-version))) (not (string-match "\\`5\\." mule-version)) (and (= emacs-major-version 21) (< emacs-minor-version 3))) (error "mule5-gbk.el doesn't support this version of Emacs"))) (defun mule5-gbk-post-read-conversion (length) "Decode gbk." (save-excursion (save-restriction (narrow-to-region (point) (+ (point) length)) (let ((data (buffer-string)) (coding-system-for-read 'binary) (coding-system-for-write 'binary)) (delete-region (point-min) (point-max)) (set-buffer-multibyte t) (insert (with-temp-buffer (set-buffer-multibyte nil) (insert data) (call-process-region (point-min) (point-max) "iconv" t t nil "-f" "gbk" "-t" "utf-8") (decode-coding-string (buffer-string) 'utf-8))) (- (point-max) (point-min)))))) (defun mule5-gbk-pre-write-conversion (beg end) "Encode gbk." (save-excursion (save-restriction (narrow-to-region beg end) (let ((data (buffer-string)) (coding-system-for-read 'binary) (coding-system-for-write 'binary)) (delete-region beg end) (set-buffer-multibyte t) (insert (with-temp-buffer (set-buffer-multibyte nil) (insert (encode-coding-string data 'utf-8)) (call-process-region (point-min) (point-max) "iconv" t t nil "-f" "utf-8" "-t" "gbk") (string-as-multibyte (buffer-string))))))) nil) (make-coding-system 'chinese-gbk 0 ?c "GBK encoding for Chinese (MIME:GBK), produced by mule5-gbk.el." nil '((post-read-conversion . mule5-gbk-post-read-conversion) (pre-write-conversion . mule5-gbk-pre-write-conversion) (safe-charsets ascii chinese-cns11643-5 chinese-cns11643-6 chinese-cns11643-7 chinese-gb2312) (mime-charset . gbk))) (define-coding-system-alias 'gbk 'chinese-gbk) (define-coding-system-alias 'cp936 'chinese-gbk) (define-coding-system-alias 'windows-936 'chinese-gbk) (eval '(if (and (boundp 'mm-charset-synonym-alist) (assq 'gbk mm-charset-synonym-alist)) (setq mm-charset-synonym-alist (delq (assq 'gbk mm-charset-synonym-alist) mm-charset-synonym-alist)))) (provide 'mule5-gbk) ;;; mule5-gbk.el ends here