*** empty log message ***
[gnus] / lisp / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'mm-util)
28
29 (defun mm-encode-body ()
30   "Encode a body.
31 Should be called narrowed to the body that is to be encoded.
32 If there is more than one non-ASCII MULE charset, then list of found
33 MULE charsets are returned.
34 If successful, the MIME charset is returned.
35 If no encoding was done, nil is returned."
36   (save-excursion
37     (goto-char (point-min))
38     (let ((charsets
39            (delq 'ascii (find-charset-region (point-min) (point-max))))
40           charset)
41       (cond
42        ;; No encoding.
43        ((null charsets)
44         nil)
45        ;; Too many charsets.
46        ((> (length charsets) 1)
47         charsets)
48        ;; We encode.
49        (t
50         (let ((mime-charset (mm-mule-charset-to-mime-charset (car charsets)))
51               start)
52           (when (not (mm-coding-system-equal
53                       mime-charset buffer-file-coding-system))
54             (while (not (eobp))
55               (if (eq (char-charset (following-char)) 'ascii)
56                   (when start
57                     (mm-encode-coding-region start (point) mime-charset)
58                     (setq start nil))
59                 (unless start
60                   (setq start (point))))
61               (forward-char 1))
62             (when start
63               (mm-encode-coding-region start (point) mime-charset)
64               (setq start nil)))
65           mime-charset))))))
66
67 (defun mm-body-encoding ()
68   "Return the encoding of the current buffer."
69   (if (null (delq 'ascii (find-charset-region (point-min) (point-max))))
70       '7bit
71     '8bit))
72
73 ;;;
74 ;;; Functions for decoding
75 ;;;
76
77 (defun mm-decode-body (charset)
78   (save-excursion
79     (mm-decode-coding-region (point-min) (point-max) charset)))
80
81 (provide 'mm-bodies)
82
83 ;; mm-bodies.el ends here