1 ;;; mcs-om.el --- MIME charset implementation for Mule 1.* and Mule 2.*
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, Mule
8 ;; This file is part of APEL (A Portable Emacs Library).
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.
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.
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.
29 (defsubst lbt-to-string (lbt)
30 (cdr (assq lbt '((nil . nil)
37 (defun encode-mime-charset-region (start end charset &optional lbt)
38 "Encode the text between START and END as MIME CHARSET."
39 (let ((cs (mime-charset-to-coding-system charset lbt)))
41 (code-convert start end *internal* cs)
42 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
43 (let ((newline (lbt-to-string lbt)))
46 (narrow-to-region start end)
47 (code-convert (point-min) (point-max) *internal* cs)
49 (goto-char (point-min))
50 (while (search-forward "\n" nil t)
51 (replace-match newline))))))))))
53 (defun decode-mime-charset-region (start end charset &optional lbt)
54 "Decode the text between START and END as MIME CHARSET."
55 (let ((cs (mime-charset-to-coding-system charset lbt)))
57 (code-convert start end cs *internal*)
58 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
59 (let ((newline (lbt-to-string lbt)))
63 (narrow-to-region start end)
64 (goto-char (point-min))
65 (while (search-forward newline nil t)
66 (replace-match "\n")))
67 (code-convert (point-min) (point-max) cs *internal*))
68 (code-convert start end cs *internal*)))))))
70 (defun encode-mime-charset-string (string charset &optional lbt)
71 "Encode the STRING as MIME CHARSET."
72 (let ((cs (mime-charset-to-coding-system charset lbt)))
74 (code-convert-string string *internal* cs)
75 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
76 (let ((newline (lbt-to-string lbt)))
80 (code-convert (point-min) (point-max) *internal* cs)
81 (goto-char (point-min))
82 (while (search-forward "\n" nil t)
83 (replace-match newline))
85 (decode-coding-string string cs)))
88 (defun decode-mime-charset-string (string charset &optional lbt)
89 "Decode the STRING which is encoded in MIME CHARSET."
90 (let ((cs (mime-charset-to-coding-system charset lbt)))
92 (decode-coding-string string cs)
93 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
94 (let ((newline (lbt-to-string lbt)))
98 (goto-char (point-min))
99 (while (search-forward newline nil t)
100 (replace-match "\n"))
101 (code-convert (point-min) (point-max) cs *internal*)
103 (decode-coding-string string cs)))
107 ((and (>= emacs-major-version 19) (>= emacs-minor-version 29))
108 ;; for MULE 2.3 based on Emacs 19.34.
109 (defun write-region-as-mime-charset (charset start end filename
110 &optional append visit lockname)
111 "Like `write-region', q.v., but code-convert by MIME CHARSET."
112 (let ((file-coding-system
113 (or (mime-charset-to-coding-system charset)
115 (write-region start end filename append visit lockname)))
118 ;; for MULE 2.3 based on Emacs 19.28.
119 (defun write-region-as-mime-charset (charset start end filename
120 &optional append visit lockname)
121 "Like `write-region', q.v., but code-convert by MIME CHARSET."
122 (let ((file-coding-system
123 (or (mime-charset-to-coding-system charset)
125 (write-region start end filename append visit)))
129 ;;; @ to coding-system
136 (defvar mime-charset-coding-system-alist
137 '((iso-8859-1 . *ctext*)
139 (gb2312 . *euc-china*)
141 (iso-2022-jp-2 . *iso-2022-ss2-7*)
142 (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
144 (x-shiftjis . *sjis*)
147 (defsubst mime-charset-to-coding-system (charset &optional lbt)
148 "Return coding-system corresponding with CHARSET.
149 CHARSET is a symbol whose name is MIME charset.
150 If optional argument LBT (`CRLF', `LF', `CR', `unix', `dos' or `mac')
151 is specified, it is used as line break code type of coding-system."
152 (if (stringp charset)
153 (setq charset (intern (downcase charset)))
155 (setq charset (or (cdr (assq charset mime-charset-coding-system-alist))
156 (intern (concat "*" (symbol-name charset) "*"))))
158 (setq charset (intern (format "%s%s" charset
159 (cond ((eq lbt 'CRLF) 'dos)
164 (if (coding-system-p charset)
172 (defvar charsets-mime-charset-alist
174 '(((lc-ascii) . us-ascii)
175 ((lc-ascii lc-ltn1) . iso-8859-1)
176 ((lc-ascii lc-ltn2) . iso-8859-2)
177 ((lc-ascii lc-ltn3) . iso-8859-3)
178 ((lc-ascii lc-ltn4) . iso-8859-4)
179 ;;; ((lc-ascii lc-crl) . iso-8859-5)
180 ((lc-ascii lc-crl) . koi8-r)
181 ((lc-ascii lc-arb) . iso-8859-6)
182 ((lc-ascii lc-grk) . iso-8859-7)
183 ((lc-ascii lc-hbw) . iso-8859-8)
184 ((lc-ascii lc-ltn5) . iso-8859-9)
185 ((lc-ascii lc-roman lc-jpold lc-jp) . iso-2022-jp)
186 ((lc-ascii lc-kr) . euc-kr)
187 ((lc-ascii lc-cn) . gb2312)
188 ((lc-ascii lc-big5-1 lc-big5-2) . big5)
189 ((lc-ascii lc-roman lc-ltn1 lc-grk
190 lc-jpold lc-cn lc-jp lc-kr
191 lc-jp2) . iso-2022-jp-2)
192 ((lc-ascii lc-roman lc-ltn1 lc-grk
193 lc-jpold lc-cn lc-jp lc-kr lc-jp2
194 lc-cns1 lc-cns2) . iso-2022-int-1)
196 lc-ltn1 lc-ltn2 lc-crl lc-grk
197 lc-jpold lc-cn lc-jp lc-kr lc-jp2
198 lc-cns1 lc-cns2 lc-cns3 lc-cns4
199 lc-cns5 lc-cns6 lc-cns7) . iso-2022-int-1)
204 (let ((pair (car alist)))
208 (cons (mapcar (function
212 (throw 'not-found nil)
216 (setq alist (cdr alist)))
219 (defvar default-mime-charset 'x-ctext
220 "Default value of MIME-charset.
221 It is used when MIME-charset is not specified.
224 (defvar default-mime-charset-for-write
226 "Default value of MIME-charset for encoding.
227 It is used when suitable MIME-charset is not found.
230 (defun detect-mime-charset-region (start end)
231 "Return MIME charset for region between START and END."
232 (or (charsets-to-mime-charset
233 (cons lc-ascii (find-charset-region start end)))
234 default-mime-charset-for-write))
242 ;;; mcs-om.el ends here