Merge poe.
[elisp/apel.git] / mcs-om.el
1 ;;; mcs-om.el --- MIME charset implementation for Mule 1.* and Mule 2.*
2
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
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 ;;; Code:
26
27 (require 'poem)
28
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)))
32     (if cs
33         (code-convert start end *internal* cs)
34       )))
35
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))
39         newline)
40     (if cs
41         (code-convert start end cs *internal*)
42       (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
43           (progn
44             (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
45                 (save-excursion
46                   (save-restriction
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*)))))))
53
54 (defun encode-mime-charset-string (string charset)
55   "Encode the STRING as MIME CHARSET."
56   (let ((cs (mime-charset-to-coding-system charset)))
57     (if cs
58         (code-convert-string string *internal* cs)
59       string)))
60
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))
64         newline)
65     (if cs
66         (decode-coding-string string cs)
67       (if (and lbt (setq cs (mime-charset-to-coding-system charset)))
68           (progn
69             (if (setq newline (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")))))
70                 (with-temp-buffer
71                  (insert string)
72                  (goto-char (point-min))
73                  (while (search-forward newline nil t)
74                    (replace-match "\n"))
75                  (code-convert (point-min) (point-max) cs *internal*)
76                  (buffer-string))
77               (decode-coding-string string cs)))
78         string))))
79
80 (cond
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)
88                *noconv*)))
89       (write-region start end filename append visit lockname)))
90   )
91  (t
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)
98                *noconv*)))
99       (write-region start end filename append visit)))
100   ))
101
102
103 ;;; @ to coding-system
104 ;;;
105
106 (require 'cyrillic)
107
108 (defvar mime-charset-coding-system-alist
109   '((iso-8859-1      . *ctext*)
110     (x-ctext         . *ctext*)
111     (gb2312          . *euc-china*)
112     (koi8-r          . *koi8*)
113     (iso-2022-jp-2   . *iso-2022-ss2-7*)
114     (x-iso-2022-jp-2 . *iso-2022-ss2-7*)
115     (shift_jis       . *sjis*)
116     (x-shiftjis      . *sjis*)
117     ))
118
119 (defsubst mime-charset-to-coding-system (charset &optional lbt)
120   (if (stringp charset)
121       (setq charset (intern (downcase charset)))
122     )
123   (setq charset (or (cdr (assq charset mime-charset-coding-system-alist))
124                     (intern (concat "*" (symbol-name charset) "*"))))
125   (if lbt
126       (setq charset (intern (format "%s%s" charset
127                                     (cond ((eq lbt 'CRLF) 'dos)
128                                           ((eq lbt 'LF) 'unix)
129                                           ((eq lbt 'CR) 'mac)
130                                           (t lbt)))))
131     )
132   (if (coding-system-p charset)
133       charset
134     ))
135
136
137 ;;; @ detection
138 ;;;
139
140 (defvar charsets-mime-charset-alist
141   (let ((alist
142          '(((lc-ascii)                                  . us-ascii)
143            ((lc-ascii lc-ltn1)                          . iso-8859-1)
144            ((lc-ascii lc-ltn2)                          . iso-8859-2)
145            ((lc-ascii lc-ltn3)                          . iso-8859-3)
146            ((lc-ascii lc-ltn4)                          . iso-8859-4)
147 ;;;        ((lc-ascii lc-crl)                           . iso-8859-5)
148            ((lc-ascii lc-crl)                           . koi8-r)
149            ((lc-ascii lc-arb)                           . iso-8859-6)
150            ((lc-ascii lc-grk)                           . iso-8859-7)
151            ((lc-ascii lc-hbw)                           . iso-8859-8)
152            ((lc-ascii lc-ltn5)                          . iso-8859-9)
153            ((lc-ascii lc-roman lc-jpold lc-jp)          . iso-2022-jp)
154            ((lc-ascii lc-kr)                            . euc-kr)
155            ((lc-ascii lc-cn)                            . gb2312)
156            ((lc-ascii lc-big5-1 lc-big5-2)              . big5)
157            ((lc-ascii lc-roman lc-ltn1 lc-grk
158                       lc-jpold lc-cn lc-jp lc-kr
159                       lc-jp2)                           . iso-2022-jp-2)
160            ((lc-ascii lc-roman lc-ltn1 lc-grk
161                       lc-jpold lc-cn lc-jp lc-kr lc-jp2
162                       lc-cns1 lc-cns2)                  . iso-2022-int-1)
163            ((lc-ascii lc-roman
164                       lc-ltn1 lc-ltn2 lc-crl lc-grk
165                       lc-jpold lc-cn lc-jp lc-kr lc-jp2
166                       lc-cns1 lc-cns2 lc-cns3 lc-cns4
167                       lc-cns5 lc-cns6 lc-cns7)          . iso-2022-int-1)
168            ))
169         dest)
170     (while alist
171       (catch 'not-found
172         (let ((pair (car alist)))
173           (setq dest
174                 (append dest
175                         (list
176                          (cons (mapcar (function
177                                         (lambda (cs)
178                                           (if (boundp cs)
179                                               (symbol-value cs)
180                                             (throw 'not-found nil)
181                                             )))
182                                        (car pair))
183                                (cdr pair)))))))
184       (setq alist (cdr alist)))
185     dest))
186
187 (defvar default-mime-charset 'x-ctext
188   "Default value of MIME-charset.
189 It is used when MIME-charset is not specified.
190 It must be symbol.")
191
192 (defun detect-mime-charset-region (start end)
193   "Return MIME charset for region between START and END."
194   (charsets-to-mime-charset
195    (cons lc-ascii (find-charset-region start end))))
196
197
198 ;;; @ end
199 ;;;
200
201 (provide 'mcs-om)
202
203 ;;; mcs-om.el ends here