| 1 | ;;; mcs-ltn1.el --- MIME charset implementation for Emacs 19 |
|---|
| 2 | ;;; and XEmacs without MULE |
|---|
| 3 | |
|---|
| 4 | ;; Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc. |
|---|
| 5 | |
|---|
| 6 | ;; Author: MORIOKA Tomohiko <tomo@m17n.org> |
|---|
| 7 | ;; Keywords: emulation, compatibility, Mule |
|---|
| 8 | |
|---|
| 9 | ;; This file is part of APEL (A Portable Emacs Library). |
|---|
| 10 | |
|---|
| 11 | ;; This program is free software; you can redistribute it and/or |
|---|
| 12 | ;; modify it under the terms of the GNU General Public License as |
|---|
| 13 | ;; published by the Free Software Foundation; either version 2, or (at |
|---|
| 14 | ;; your option) any later version. |
|---|
| 15 | |
|---|
| 16 | ;; This program is distributed in the hope that it will be useful, but |
|---|
| 17 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 19 | ;; General Public License for more details. |
|---|
| 20 | |
|---|
| 21 | ;; You should have received a copy of the GNU General Public License |
|---|
| 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
|---|
| 23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|---|
| 24 | ;; Boston, MA 02111-1307, USA. |
|---|
| 25 | |
|---|
| 26 | ;;; Code: |
|---|
| 27 | |
|---|
| 28 | (defvar charsets-mime-charset-alist |
|---|
| 29 | '(((ascii) . us-ascii))) |
|---|
| 30 | |
|---|
| 31 | (defvar default-mime-charset 'iso-8859-1) |
|---|
| 32 | |
|---|
| 33 | (defsubst lbt-to-string (lbt) |
|---|
| 34 | (cdr (assq lbt '((nil . nil) |
|---|
| 35 | (CRLF . "\r\n") |
|---|
| 36 | (CR . "\r") |
|---|
| 37 | (dos . "\r\n") |
|---|
| 38 | (mac . "\r")))) |
|---|
| 39 | ) |
|---|
| 40 | |
|---|
| 41 | (defun mime-charset-to-coding-system (charset) |
|---|
| 42 | (if (stringp charset) |
|---|
| 43 | (setq charset (intern (downcase charset)))) |
|---|
| 44 | (if (memq charset (list 'us-ascii default-mime-charset)) |
|---|
| 45 | charset)) |
|---|
| 46 | |
|---|
| 47 | (defalias 'mime-charset-p 'mime-charset-to-coding-system) |
|---|
| 48 | |
|---|
| 49 | (defun detect-mime-charset-region (start end) |
|---|
| 50 | "Return MIME charset for region between START and END." |
|---|
| 51 | (if (save-excursion |
|---|
| 52 | (goto-char start) |
|---|
| 53 | (re-search-forward "[\200-\377]" end t)) |
|---|
| 54 | default-mime-charset |
|---|
| 55 | 'us-ascii)) |
|---|
| 56 | |
|---|
| 57 | (defun encode-mime-charset-region (start end charset &optional lbt) |
|---|
| 58 | "Encode the text between START and END as MIME CHARSET." |
|---|
| 59 | (let ((newline (lbt-to-string lbt))) |
|---|
| 60 | (if newline |
|---|
| 61 | (save-excursion |
|---|
| 62 | (save-restriction |
|---|
| 63 | (narrow-to-region start end) |
|---|
| 64 | (goto-char (point-min)) |
|---|
| 65 | (while (search-forward "\n" nil t) |
|---|
| 66 | (replace-match newline)) |
|---|
| 67 | ))) |
|---|
| 68 | )) |
|---|
| 69 | |
|---|
| 70 | (defun decode-mime-charset-region (start end charset &optional lbt) |
|---|
| 71 | "Decode the text between START and END as MIME CHARSET." |
|---|
| 72 | (let ((newline (lbt-to-string lbt))) |
|---|
| 73 | (if newline |
|---|
| 74 | (save-excursion |
|---|
| 75 | (save-restriction |
|---|
| 76 | (narrow-to-region start end) |
|---|
| 77 | (goto-char (point-min)) |
|---|
| 78 | (while (search-forward newline nil t) |
|---|
| 79 | (replace-match "\n")) |
|---|
| 80 | ))) |
|---|
| 81 | )) |
|---|
| 82 | |
|---|
| 83 | (defun encode-mime-charset-string (string charset &optional lbt) |
|---|
| 84 | "Encode the STRING as MIME CHARSET." |
|---|
| 85 | (if lbt |
|---|
| 86 | (with-temp-buffer |
|---|
| 87 | (insert string) |
|---|
| 88 | (encode-mime-charset-region (point-min)(point-max) charset lbt) |
|---|
| 89 | (buffer-string)) |
|---|
| 90 | string)) |
|---|
| 91 | |
|---|
| 92 | (defun decode-mime-charset-string (string charset &optional lbt) |
|---|
| 93 | "Decode the STRING as MIME CHARSET." |
|---|
| 94 | (if lbt |
|---|
| 95 | (with-temp-buffer |
|---|
| 96 | (insert string) |
|---|
| 97 | (decode-mime-charset-region (point-min)(point-max) charset lbt) |
|---|
| 98 | (buffer-string)) |
|---|
| 99 | string)) |
|---|
| 100 | |
|---|
| 101 | (defalias 'write-region-as-mime-charset 'write-region) |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | ;;; @ end |
|---|
| 105 | ;;; |
|---|
| 106 | |
|---|
| 107 | (require 'product) |
|---|
| 108 | (product-provide (provide 'mcs-ltn1) (require 'apel-ver)) |
|---|
| 109 | |
|---|
| 110 | ;;; mcs-ltn1.el ends here |
|---|