Merge poe.
[elisp/apel.git] / mcs-nemacs.el
1 ;;; mcs-nemacs.el --- MIME charset implementation for Nemacs
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 (defvar charsets-mime-charset-alist
28   '(((ascii) . us-ascii)))
29
30 (defvar default-mime-charset 'iso-2022-jp)
31
32 (defvar mime-charset-coding-system-alist
33   '((iso-2022-jp     . 2)
34     (shift_jis       . 1)
35     ))
36
37 (defun mime-charset-to-coding-system (charset)
38   (if (stringp charset)
39       (setq charset (intern (downcase charset)))
40     )
41   (cdr (assq charset mime-charset-coding-system-alist)))
42
43 (defun detect-mime-charset-region (start end)
44   "Return MIME charset for region between START and END.
45 \[emu-nemacs.el]"
46   (if (save-excursion
47         (save-restriction
48           (narrow-to-region start end)
49           (goto-char start)
50           (re-search-forward "[\200-\377]" nil t)))
51       default-mime-charset
52     'us-ascii))
53
54 (defun encode-mime-charset-region (start end charset)
55   "Encode the text between START and END as MIME CHARSET.
56 \[emu-nemacs.el]"
57   (let ((cs (mime-charset-to-coding-system charset)))
58     (and (numberp cs)
59          (or (= cs 3)
60              (save-excursion
61                (save-restriction
62                  (narrow-to-region start end)
63                  (convert-region-kanji-code start end 3 cs))))
64          )))
65
66 (defun decode-mime-charset-region (start end charset &optional lbt)
67   "Decode the text between START and END as MIME CHARSET.
68 \[emu-nemacs.el]"
69   (let ((cs (mime-charset-to-coding-system charset))
70         (nl (cdr (assq lbt '((CRLF . "\r\n") (CR . "\r")
71                              (dos . "\r\n") (mac . "\r"))))))
72     (and (numberp cs)
73          (or (= cs 3)
74              (save-excursion
75                (save-restriction
76                  (narrow-to-region start end)
77                  (convert-region-kanji-code start end cs 3)
78                  (if nl
79                      (progn
80                        (goto-char (point-min))
81                        (while (search-forward nl nil t)
82                          (replace-match "\n")))
83                    )))
84              ))))
85
86 (defun encode-mime-charset-string (string charset)
87   "Encode the STRING as MIME CHARSET. [emu-nemacs.el]"
88   (let ((cs (mime-charset-to-coding-system charset)))
89     (if cs
90         (convert-string-kanji-code string 3 cs)
91       string)))
92
93 (defun decode-mime-charset-string (string charset &optional lbt)
94   "Decode the STRING as MIME CHARSET. [emu-nemacs.el]"
95   (with-temp-buffer
96     (insert string)
97     (decode-mime-charset-region (point-min)(point-max) charset lbt)
98     (buffer-string)))
99
100 (defun write-region-as-mime-charset (charset start end filename)
101   "Like `write-region', q.v., but code-convert by MIME CHARSET.
102 \[emu-nemacs.el]"
103   (let ((kanji-fileio-code
104          (or (mime-charset-to-coding-system charset) 0)))
105     (write-region start end filename)))
106
107
108 ;;; @ end
109 ;;;
110
111 (provide 'mcs-nemacs)
112
113 ;;; mcs-nemacs.el ends here