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.
33 (defvar mime-charset-coding-system-alist
34 '((iso-8859-1 . *ctext*)
36 (gb2312 . *euc-china*)
38 (iso-2022-jp-2 . *iso-2022-ss2-7*)
39 (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
41 (x-shiftjis . *sjis*)))
43 (defsubst mime-charset-to-coding-system (charset &optional lbt)
44 "Return coding-system corresponding with CHARSET.
45 CHARSET is a symbol whose name is MIME charset.
46 If optional argument LBT (`CRLF', `LF', `CR', `unix', `dos' or `mac')
47 is specified, it is used as line break code type of coding-system."
49 (setq charset (intern (downcase charset))))
50 (setq charset (or (cdr (assq charset mime-charset-coding-system-alist))
51 (intern (concat "*" (symbol-name charset) "*"))))
53 (setq charset (intern (format "%s%s" charset
54 (cond ((eq lbt 'CRLF) 'dos)
58 (if (coding-system-p charset)
61 (defsubst lbt-to-string (lbt)
62 (cdr (assq lbt '((nil . nil)
68 (defun encode-mime-charset-region (start end charset &optional lbt)
69 "Encode the text between START and END as MIME CHARSET."
70 (let ((cs (mime-charset-to-coding-system charset lbt)))
72 (code-convert start end *internal* cs)
73 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
74 (let ((newline (lbt-to-string lbt)))
77 (narrow-to-region start end)
78 (code-convert (point-min) (point-max) *internal* cs)
80 (goto-char (point-min))
81 (while (search-forward "\n" nil t)
82 (replace-match newline))))))))))
84 (defun decode-mime-charset-region (start end charset &optional lbt)
85 "Decode the text between START and END as MIME CHARSET."
86 (let ((cs (mime-charset-to-coding-system charset lbt)))
88 (code-convert start end cs *internal*)
89 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
90 (let ((newline (lbt-to-string lbt)))
94 (narrow-to-region start end)
95 (goto-char (point-min))
96 (while (search-forward newline nil t)
97 (replace-match "\n")))
98 (code-convert (point-min) (point-max) cs *internal*))
99 (code-convert start end cs *internal*)))))))
101 (defun encode-mime-charset-string (string charset &optional lbt)
102 "Encode the STRING as MIME CHARSET."
103 (let ((cs (mime-charset-to-coding-system charset lbt)))
105 (code-convert-string string *internal* cs)
106 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
107 (let ((newline (lbt-to-string lbt)))
111 (code-convert (point-min) (point-max) *internal* cs)
112 (goto-char (point-min))
113 (while (search-forward "\n" nil t)
114 (replace-match newline))
116 (decode-coding-string string cs)))
119 (defun decode-mime-charset-string (string charset &optional lbt)
120 "Decode the STRING which is encoded in MIME CHARSET."
121 (let ((cs (mime-charset-to-coding-system charset lbt)))
123 (decode-coding-string string cs)
124 (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
125 (let ((newline (lbt-to-string lbt)))
129 (goto-char (point-min))
130 (while (search-forward newline nil t)
131 (replace-match "\n"))
132 (code-convert (point-min) (point-max) cs *internal*)
134 (decode-coding-string string cs)))
138 ((and (>= emacs-major-version 19) (>= emacs-minor-version 29))
139 ;; for MULE 2.3 based on Emacs 19.34.
140 (defun write-region-as-mime-charset (charset start end filename
141 &optional append visit lockname)
142 "Like `write-region', q.v., but code-convert by MIME CHARSET."
143 (let ((file-coding-system
144 (or (mime-charset-to-coding-system charset)
146 (write-region start end filename append visit lockname)))
149 ;; for MULE 2.3 based on Emacs 19.28.
150 (defun write-region-as-mime-charset (charset start end filename
151 &optional append visit lockname)
152 "Like `write-region', q.v., but code-convert by MIME CHARSET."
153 (let ((file-coding-system
154 (or (mime-charset-to-coding-system charset)
156 (write-region start end filename append visit)))
163 (defvar charsets-mime-charset-alist
165 '(((lc-ascii) . us-ascii)
166 ((lc-ascii lc-ltn1) . iso-8859-1)
167 ((lc-ascii lc-ltn2) . iso-8859-2)
168 ((lc-ascii lc-ltn3) . iso-8859-3)
169 ((lc-ascii lc-ltn4) . iso-8859-4)
170 ;;; ((lc-ascii lc-crl) . iso-8859-5)
171 ((lc-ascii lc-crl) . koi8-r)
172 ((lc-ascii lc-arb) . iso-8859-6)
173 ((lc-ascii lc-grk) . iso-8859-7)
174 ((lc-ascii lc-hbw) . iso-8859-8)
175 ((lc-ascii lc-ltn5) . iso-8859-9)
176 ((lc-ascii lc-roman lc-jpold lc-jp) . iso-2022-jp)
177 ((lc-ascii lc-kr) . euc-kr)
178 ((lc-ascii lc-cn) . gb2312)
179 ((lc-ascii lc-big5-1 lc-big5-2) . big5)
180 ((lc-ascii lc-roman lc-ltn1 lc-grk
181 lc-jpold lc-cn lc-jp lc-kr
182 lc-jp2) . iso-2022-jp-2)
183 ((lc-ascii lc-roman lc-ltn1 lc-grk
184 lc-jpold lc-cn lc-jp lc-kr lc-jp2
185 lc-cns1 lc-cns2) . iso-2022-int-1)
187 lc-ltn1 lc-ltn2 lc-crl lc-grk
188 lc-jpold lc-cn lc-jp lc-kr lc-jp2
189 lc-cns1 lc-cns2 lc-cns3 lc-cns4
190 lc-cns5 lc-cns6 lc-cns7) . iso-2022-int-1)
195 (let ((pair (car alist)))
199 (cons (mapcar (function
203 (throw 'not-found nil)
207 (setq alist (cdr alist)))
210 (defvar default-mime-charset 'x-ctext
211 "Default value of MIME-charset.
212 It is used when MIME-charset is not specified.
215 (defvar default-mime-charset-for-write
217 "Default value of MIME-charset for encoding.
218 It is used when suitable MIME-charset is not found.
221 (defun detect-mime-charset-region (start end)
222 "Return MIME charset for region between START and END."
223 (or (charsets-to-mime-charset
224 (cons lc-ascii (find-charset-region start end)))
225 default-mime-charset-for-write))
232 (product-provide (provide 'mcs-om) (require 'apel-ver))
234 ;;; mcs-om.el ends here