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 (defun encode-mime-charset-region (start end charset)
30 "Encode the text between START and END as MIME CHARSET."
31 (let ((cs (mime-charset-to-coding-system charset)))
33 (code-convert start end *internal* cs)
36 (defun decode-mime-charset-region (start end charset &optional lbt)
37 "Decode the text between START and END as MIME CHARSET."
38 (let ((cs (mime-charset-to-coding-system charset lbt))
41 (code-convert start end cs *internal*)
42 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
44 (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
47 (narrow-to-region start end)
48 (goto-char (point-min))
49 (while (search-forward newline nil t)
50 (replace-match "\n")))
51 (code-convert (point-min) (point-max) cs *internal*))
52 (code-convert start end cs *internal*)))))))
54 (defun encode-mime-charset-string (string charset)
55 "Encode the STRING as MIME CHARSET."
56 (let ((cs (mime-charset-to-coding-system charset)))
58 (code-convert-string string *internal* cs)
61 (defun decode-mime-charset-string (string charset &optional lbt)
62 "Decode the STRING which is encoded in MIME CHARSET."
63 (let ((cs (mime-charset-to-coding-system charset lbt))
66 (decode-coding-string string cs)
67 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
69 (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
72 (goto-char (point-min))
73 (while (search-forward newline nil t)
75 (code-convert (point-min) (point-max) cs *internal*)
77 (decode-coding-string string cs)))
81 (running-emacs-19_29-or-later
82 ;; for MULE 2.3 based on Emacs 19.34.
83 (defun write-region-as-mime-charset (charset start end filename
84 &optional append visit lockname)
85 "Like `write-region', q.v., but code-convert by MIME CHARSET."
86 (let ((file-coding-system
87 (or (mime-charset-to-coding-system charset)
89 (write-region start end filename append visit lockname)))
92 ;; for MULE 2.3 based on Emacs 19.28.
93 (defun write-region-as-mime-charset (charset start end filename
94 &optional append visit lockname)
95 "Like `write-region', q.v., but code-convert by MIME CHARSET."
96 (let ((file-coding-system
97 (or (mime-charset-to-coding-system charset)
99 (write-region start end filename append visit)))
103 ;;; @ to coding-system
110 (defvar mime-charset-coding-system-alist
111 '((iso-8859-1 . *ctext*)
113 (gb2312 . *euc-china*)
115 (iso-2022-jp-2 . *iso-2022-ss2-7*)
116 (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
118 (x-shiftjis . *sjis*)
121 (defsubst mime-charset-to-coding-system (charset &optional lbt)
122 (if (stringp charset)
123 (setq charset (intern (downcase charset)))
125 (setq charset (or (cdr (assq charset mime-charset-coding-system-alist))
126 (intern (concat "*" (symbol-name charset) "*"))))
128 (setq charset (intern (format "%s%s" charset
129 (cond ((eq lbt 'CRLF) 'dos)
134 (if (coding-system-p charset)
142 (defvar charsets-mime-charset-alist
144 '(((lc-ascii) . us-ascii)
145 ((lc-ascii lc-ltn1) . iso-8859-1)
146 ((lc-ascii lc-ltn2) . iso-8859-2)
147 ((lc-ascii lc-ltn3) . iso-8859-3)
148 ((lc-ascii lc-ltn4) . iso-8859-4)
149 ;;; ((lc-ascii lc-crl) . iso-8859-5)
150 ((lc-ascii lc-crl) . koi8-r)
151 ((lc-ascii lc-arb) . iso-8859-6)
152 ((lc-ascii lc-grk) . iso-8859-7)
153 ((lc-ascii lc-hbw) . iso-8859-8)
154 ((lc-ascii lc-ltn5) . iso-8859-9)
155 ((lc-ascii lc-roman lc-jpold lc-jp) . iso-2022-jp)
156 ((lc-ascii lc-kr) . euc-kr)
157 ((lc-ascii lc-cn) . gb2312)
158 ((lc-ascii lc-big5-1 lc-big5-2) . big5)
159 ((lc-ascii lc-roman lc-ltn1 lc-grk
160 lc-jpold lc-cn lc-jp lc-kr
161 lc-jp2) . iso-2022-jp-2)
162 ((lc-ascii lc-roman lc-ltn1 lc-grk
163 lc-jpold lc-cn lc-jp lc-kr lc-jp2
164 lc-cns1 lc-cns2) . iso-2022-int-1)
166 lc-ltn1 lc-ltn2 lc-crl lc-grk
167 lc-jpold lc-cn lc-jp lc-kr lc-jp2
168 lc-cns1 lc-cns2 lc-cns3 lc-cns4
169 lc-cns5 lc-cns6 lc-cns7) . iso-2022-int-1)
174 (let ((pair (car alist)))
178 (cons (mapcar (function
182 (throw 'not-found nil)
186 (setq alist (cdr alist)))
189 (defvar default-mime-charset 'x-ctext
190 "Default value of MIME-charset.
191 It is used when MIME-charset is not specified.
194 (defun detect-mime-charset-region (start end)
195 "Return MIME charset for region between START and END."
196 (charsets-to-mime-charset
197 (cons lc-ascii (find-charset-region start end))))
205 ;;; mcs-om.el ends here