Merge apel-mcs-2-9_12_2.
[elisp/apel.git] / mcs-xm.el
1 ;;; mcs-xm.el --- MIME charset implementation for XEmacs-mule
2
3 ;; Copyright (C) 1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, Mule
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;    This module requires Emacs 20.0.93, XEmacs 20.3-b5 (with mule)
28 ;;    or later.
29
30 ;;; Code:
31
32 (require 'mcs-20)
33
34
35 (defun encode-mime-charset-region (start end charset &optional lbt)
36   "Encode the text between START and END as MIME CHARSET."
37   (let ((cs (mime-charset-to-coding-system charset lbt)))
38     (if cs
39         (encode-coding-region start end cs)
40       )))
41
42
43 (defcustom mime-charset-decoder-alist
44   '((iso-2022-jp . decode-mime-charset-region-with-iso646-unification)
45     (iso-2022-jp-2 . decode-mime-charset-region-with-iso646-unification)
46     (x-ctext . decode-mime-charset-region-with-iso646-unification)
47     (hz-gb-2312 . decode-mime-charset-region-for-hz)
48     (t . decode-mime-charset-region-default))
49   "Alist MIME-charset vs. decoder function."
50   :group 'i18n
51   :type '(repeat (cons mime-charset function)))
52
53 (defsubst decode-mime-charset-region-default (start end charset lbt)
54   (let ((cs (mime-charset-to-coding-system charset lbt)))
55     (if cs
56         (decode-coding-region start end cs)
57       )))
58
59 (defcustom mime-iso646-character-unification-alist
60   (eval-when-compile
61     (let (dest
62           (i 33))
63       (while (< i 92)
64         (setq dest
65               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
66                           (format "%c" i))
67                     dest))
68         (setq i (1+ i)))
69       (setq i 93)
70       (while (< i 126)
71         (setq dest
72               (cons (cons (char-to-string (make-char 'latin-jisx0201 i))
73                           (format "%c" i))
74                     dest))
75         (setq i (1+ i)))
76       (nreverse dest)))
77   "Alist unified string vs. canonical string."
78   :group 'i18n
79   :type '(repeat (cons string string)))
80
81 (defcustom mime-unified-character-face nil
82   "*Face of unified character."
83   :group 'i18n
84   :type 'face)
85
86 (defcustom mime-character-unification-limit-size 2048
87   "*Limit size to unify characters."
88   :group 'i18n
89   :type 'integer)
90
91 (defun decode-mime-charset-region-with-iso646-unification (start end charset
92                                                                  lbt)
93   (decode-mime-charset-region-default start end charset lbt)
94   (if (<= (- end start) mime-character-unification-limit-size)
95       (save-excursion
96         (let ((rest mime-iso646-character-unification-alist))
97           (while rest
98             (let ((pair (car rest)))
99               (goto-char (point-min))
100               (while (search-forward (car pair) nil t)
101                 (let ((str (cdr pair)))
102                   (put-text-property 0 (length str)
103                                      'face mime-unified-character-face str)
104                   (replace-match str 'fixed-case 'literal)
105                   )
106                 ))
107             (setq rest (cdr rest)))))
108     ))
109
110 (defun decode-mime-charset-region-for-hz (start end charset lbt)
111   (if lbt
112       (save-restriction
113         (narrow-to-region start end)
114         (decode-coding-region (point-min)(point-max)
115                               (mime-charset-to-coding-system 'raw-text lbt))
116         (decode-hz-region (point-min)(point-max)))
117     (decode-hz-region start end)))
118
119 (defun decode-mime-charset-region (start end charset &optional lbt)
120   "Decode the text between START and END as MIME CHARSET."
121   (if (stringp charset)
122       (setq charset (intern (downcase charset)))
123     )
124   (let ((func (cdr (or (assq charset mime-charset-decoder-alist)
125                        (assq t mime-charset-decoder-alist)))))
126     (funcall func start end charset lbt)))
127
128 (defsubst encode-mime-charset-string (string charset &optional lbt)
129   "Encode the STRING as MIME CHARSET."
130   (let ((cs (mime-charset-to-coding-system charset lbt)))
131     (if cs
132         (encode-coding-string string cs)
133       string)))
134
135 ;; (defsubst decode-mime-charset-string (string charset)
136 ;;   "Decode the STRING as MIME CHARSET."
137 ;;   (let ((cs (mime-charset-to-coding-system charset)))
138 ;;     (if cs
139 ;;         (decode-coding-string string cs)
140 ;;       string)))
141 (defun decode-mime-charset-string (string charset &optional lbt)
142   "Decode the STRING as MIME CHARSET."
143   (with-temp-buffer
144     (insert string)
145     (decode-mime-charset-region (point-min)(point-max) charset lbt)
146     (buffer-string)))
147
148
149 (defvar charsets-mime-charset-alist
150   '(((ascii)                                            . us-ascii)
151     ((ascii latin-iso8859-1)                            . iso-8859-1)
152     ((ascii latin-iso8859-2)                            . iso-8859-2)
153     ((ascii latin-iso8859-3)                            . iso-8859-3)
154     ((ascii latin-iso8859-4)                            . iso-8859-4)
155     ((ascii cyrillic-iso8859-5)                         . iso-8859-5)
156 ;;; ((ascii cyrillic-iso8859-5)                         . koi8-r)
157     ((ascii arabic-iso8859-6)                           . iso-8859-6)
158     ((ascii greek-iso8859-7)                            . iso-8859-7)
159     ((ascii hebrew-iso8859-8)                           . iso-8859-8)
160     ((ascii latin-iso8859-9)                            . iso-8859-9)
161     ((ascii latin-jisx0201
162             japanese-jisx0208-1978 japanese-jisx0208)   . iso-2022-jp)
163     ((ascii latin-jisx0201
164             katakana-jisx0201 japanese-jisx0208)        . shift_jis)
165     ((ascii korean-ksc5601)                             . euc-kr)
166     ((ascii chinese-gb2312)                             . gb2312)
167     ((ascii chinese-big5-1 chinese-big5-2)              . big5)
168     ((ascii latin-iso8859-1 greek-iso8859-7
169             latin-jisx0201 japanese-jisx0208-1978
170             chinese-gb2312 japanese-jisx0208
171             korean-ksc5601 japanese-jisx0212)           . iso-2022-jp-2)
172     ;; ((ascii latin-iso8859-1 greek-iso8859-7
173     ;;         latin-jisx0201 japanese-jisx0208-1978
174     ;;         chinese-gb2312 japanese-jisx0208
175     ;;         korean-ksc5601 japanese-jisx0212
176     ;;         chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
177     ))
178
179
180 (defun coding-system-to-mime-charset (coding-system)
181   "Convert CODING-SYSTEM to a MIME-charset.
182 Return nil if corresponding MIME-charset is not found."
183   (setq coding-system
184         (coding-system-name (coding-system-base coding-system)))
185   (or (car (rassq coding-system mime-charset-coding-system-alist))
186       coding-system))
187
188 (defun mime-charset-list ()
189   "Return a list of all existing MIME-charset."
190   (let ((dest (mapcar (function car) mime-charset-coding-system-alist))
191         (rest (coding-system-list))
192         cs)
193     (while rest
194       (setq cs (coding-system-name (coding-system-base (car rest))))
195       (or (rassq cs mime-charset-coding-system-alist)
196           (memq cs dest)
197           (setq dest (cons cs dest)))
198       (setq rest (cdr rest)))
199     dest))
200
201
202 ;;; @ end
203 ;;;
204
205 (provide 'mcs-xm)
206
207 ;;; mcs-xm.el ends here