Don't compile and install other packages.
[elisp/semi.git] / mime-text.el
1 ;;; mime-text.el --- mime-view content filter for text
2
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version:
7 ;;      $Id: mime-text.el,v 0.11 1997-03-04 12:39:25 morioka Exp $
8 ;; Keywords: text, MIME, multimedia, mail, news
9
10 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 ;;; @ code conversion
30 ;;;
31
32 (defvar mime-text-decoder-alist
33   '((mime/show-message-mode     . mime-charset/decode-buffer)
34     (mime-temp-message-mode     . mime-charset/decode-buffer)
35     (t                          . mime-charset/maybe-decode-buffer)
36     ))
37
38 (defun mime-charset/decode-buffer (charset &optional encoding)
39   (decode-mime-charset-region (point-min)(point-max)
40                               (or charset default-mime-charset))
41   )
42
43 (defun mime-charset/maybe-decode-buffer (charset &optional encoding)
44   (or (member encoding '(nil "7bit" "8bit" "binary"))
45       (mime-charset/decode-buffer charset)
46       ))
47
48 (defun mime-preview/decode-text-buffer (charset encoding)
49   (mime-decode-region (point-min) (point-max) encoding)
50   (let* ((mode mime::preview/original-major-mode)
51          (m (or (save-excursion
52                   (set-buffer mime::preview/article-buffer)
53                   mime::article/code-converter)
54                 (cdr (or (assq mode mime-text-decoder-alist)
55                          (assq t mime-text-decoder-alist)))
56                 ))
57          )
58     (and (functionp m)
59          (funcall m charset encoding)
60          )))
61
62
63 ;;; @ for URL
64 ;;;
65
66 (require 'browse-url)
67
68 (defvar mime-text-url-regexp
69   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]"
70   "*Regexp to match URL in text/plain body.")
71
72 (defun mime-text-browse-url (&optional url)
73   (if (fboundp browse-url-browser-function)
74       (if url 
75         (funcall browse-url-browser-function url)
76       (call-interactively browse-url-browser-function))
77     (if (fboundp mime-button-mother-dispatcher)
78         (call-interactively mime-button-mother-dispatcher)
79       )
80     ))
81
82
83 ;;; @ content filters for mime-text
84 ;;;
85
86 (defun mime-preview/filter-for-text/plain (ctype params encoding)
87   (mime-preview/decode-text-buffer (cdr (assoc "charset" params)) encoding)
88   (goto-char (point-max))
89   (if (not (eq (char-after (1- (point))) ?\n))
90       (insert "\n")
91     )
92   (if browse-url-browser-function
93       (progn
94         (goto-char (point-min))
95         (while (re-search-forward mime-text-url-regexp nil t)
96           (let ((beg (match-beginning 0))
97                 (end (match-end 0)))
98             (mime-add-button beg end
99                              (function mime-text-browse-url)
100                              (list (buffer-substring beg end))))
101           )))
102   (run-hooks 'mime-view-plain-text-preview-hook)
103   )
104
105 (defun mime-preview/filter-for-text/richtext (ctype params encoding)
106   (let* ((mode mime::preview/original-major-mode)
107          (m (assq mode mime-text-decoder-alist))
108          (charset (cdr (assoc "charset" params)))
109          (beg (point-min))
110          )
111     (remove-text-properties beg (point-max) '(face nil))
112     (mime-preview/decode-text-buffer charset encoding)
113     (richtext-decode beg (point-max))
114     ))
115
116 (defun mime-preview/filter-for-text/enriched (ctype params encoding)
117   (let* ((mode mime::preview/original-major-mode)
118          (m (assq mode mime-text-decoder-alist))
119          (charset (cdr (assoc "charset" params)))
120          (beg (point-min))
121          )
122     (remove-text-properties beg (point-max) '(face nil))
123     (mime-preview/decode-text-buffer charset encoding)
124     (enriched-decode beg (point-max))
125     ))
126
127
128 ;;; @ end
129 ;;;
130
131 (provide 'mime-text)
132
133 ;;; mime-text.el ends here