(Download): Renamed from "Anonymous FTP"; modify for
[elisp/apel.git] / mcs-nemacs.el
1 ;;; mcs-nemacs.el --- MIME charset implementation for Nemacs
2
3 ;; Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 &optional lbt)
46   (if (stringp charset)
47       (setq charset (intern (downcase charset)))
48     )
49   (cdr (assq charset mime-charset-coding-system-alist)))
50
51 (fset 'mime-charset-p 'mime-charset-to-coding-system)
52
53 (defun detect-mime-charset-region (start end)
54   "Return MIME charset for region between START and END.
55 \[emu-nemacs.el]"
56   (if (save-excursion
57         (save-restriction
58           (narrow-to-region start end)
59           (goto-char start)
60           (re-search-forward "[\200-\377]" nil t)))
61       default-mime-charset
62     'us-ascii))
63
64 (defun encode-mime-charset-region (start end charset &optional lbt)
65   "Encode the text between START and END as MIME CHARSET.
66 \[emu-nemacs.el]"
67   (let ((cs (mime-charset-to-coding-system charset))
68         (nl (lbt-to-string lbt)))
69     (and (numberp cs)
70          (or (= cs 3)
71              (save-excursion
72                (save-restriction
73                  (narrow-to-region start end)
74                  (convert-region-kanji-code start end 3 cs)
75                  (if nl
76                      (progn
77                        (goto-char (point-min))
78                        (while (search-forward "\n" nil t)
79                          (replace-match nl)))
80                    )))
81              ))))
82
83 (defun decode-mime-charset-region (start end charset &optional lbt)
84   "Decode the text between START and END as MIME CHARSET.
85 \[emu-nemacs.el]"
86   (let ((cs (mime-charset-to-coding-system charset))
87         (nl (lbt-to-string lbt)))
88     (and (numberp cs)
89          (or (= cs 3)
90              (save-excursion
91                (save-restriction
92                  (narrow-to-region start end)
93                  (convert-region-kanji-code start end cs 3)
94                  (if nl
95                      (progn
96                        (goto-char (point-min))
97                        (while (search-forward nl nil t)
98                          (replace-match "\n")))
99                    )))
100              ))))
101
102 (defun encode-mime-charset-string (string charset &optional lbt)
103   "Encode the STRING as MIME CHARSET. [emu-nemacs.el]"
104   (with-temp-buffer
105     (insert string)
106     (encode-mime-charset-region (point-min)(point-max) charset lbt)
107     (buffer-string)))
108
109 (defun decode-mime-charset-string (string charset &optional lbt)
110   "Decode the STRING as MIME CHARSET. [emu-nemacs.el]"
111   (with-temp-buffer
112     (insert string)
113     (decode-mime-charset-region (point-min)(point-max) charset lbt)
114     (buffer-string)))
115
116 (defun write-region-as-mime-charset (charset start end filename)
117   "Like `write-region', q.v., but code-convert by MIME CHARSET.
118 \[emu-nemacs.el]"
119   (let ((kanji-fileio-code
120          (or (mime-charset-to-coding-system charset) 0)))
121     (write-region start end filename)))
122
123
124 ;;; @ end
125 ;;;
126
127 (require 'product)
128 (product-provide (provide 'mcs-nemacs) (require 'apel-ver))
129
130 ;;; mcs-nemacs.el ends here