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