`mime-raw::text-decoder' -> `mime-text-decoder'.
[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.17 1997-03-15 23:58:06 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 ;;; @ buffer local variables in raw-buffer
30 ;;;
31
32 (defvar mime-text-decoder nil
33   "Function to decode text in current buffer.
34 Interface of the function is (CHARSET &optional ENCODING).
35 CHARSET is symbol of MIME charset and ENCODING is value of
36 Content-Transfer-Encoding.
37
38 Notice that this variable is usually used as buffer local variable in
39 raw-buffer.")
40
41 (make-variable-buffer-local 'mime-text-decoder)
42
43
44 ;;; @ code conversion
45 ;;;
46
47 (defvar mime-text-decoder-alist
48   '((mime/show-message-mode     . mime-charset/decode-buffer)
49     (mime-temp-message-mode     . mime-charset/decode-buffer)
50     (t                          . mime-charset/maybe-decode-buffer)
51     ))
52
53 (defun mime-charset/decode-buffer (charset &optional encoding)
54   (decode-mime-charset-region (point-min)(point-max)
55                               (or charset default-mime-charset))
56   )
57
58 (defun mime-charset/maybe-decode-buffer (charset &optional encoding)
59   (or (member encoding '(nil "7bit" "8bit" "binary"))
60       (mime-charset/decode-buffer charset)
61       ))
62
63 (defun mime-preview/decode-text-buffer (charset encoding)
64   (mime-decode-region (point-min) (point-max) encoding)
65   (let ((text-decoder
66          (save-excursion
67            (set-buffer mime::preview/article-buffer)
68            (or mime-text-decoder
69                (cdr (or (assq major-mode mime-text-decoder-alist)
70                         (assq t mime-text-decoder-alist)))
71                ))))
72     (and (functionp text-decoder)
73          (funcall text-decoder charset encoding)
74          )))
75
76
77 ;;; @ for URL
78 ;;;
79
80 (require 'browse-url)
81
82 (defvar mime-text-url-regexp
83   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]"
84   "*Regexp to match URL in text/plain body.")
85
86 (defun mime-text-browse-url (&optional url)
87   (if (fboundp browse-url-browser-function)
88       (if url 
89         (funcall browse-url-browser-function url)
90       (call-interactively browse-url-browser-function))
91     (if (fboundp mime-button-mother-dispatcher)
92         (call-interactively mime-button-mother-dispatcher)
93       )
94     ))
95
96
97 ;;; @ content filters for mime-text
98 ;;;
99
100 (defun mime-preview/filter-for-text/plain (ctype params encoding)
101   (mime-preview/decode-text-buffer (cdr (assoc "charset" params)) encoding)
102   (goto-char (point-max))
103   (if (not (eq (char-after (1- (point))) ?\n))
104       (insert "\n")
105     )
106   (if browse-url-browser-function
107       (progn
108         (goto-char (point-min))
109         (while (re-search-forward mime-text-url-regexp nil t)
110           (let ((beg (match-beginning 0))
111                 (end (match-end 0)))
112             (mime-add-button beg end
113                              (function mime-text-browse-url)
114                              (list (buffer-substring beg end))))
115           )))
116   (run-hooks 'mime-view-plain-text-preview-hook)
117   )
118
119 (defun mime-preview/filter-for-text/richtext (ctype params encoding)
120   (let* ((charset (cdr (assoc "charset" params)))
121          (beg (point-min))
122          )
123     (remove-text-properties beg (point-max) '(face nil))
124     (mime-preview/decode-text-buffer charset encoding)
125     (richtext-decode beg (point-max))
126     ))
127
128 (defun mime-preview/filter-for-text/enriched (ctype params encoding)
129   (let* ((charset (cdr (assoc "charset" params)))
130          (beg (point-min))
131          )
132     (remove-text-properties beg (point-max) '(face nil))
133     (mime-preview/decode-text-buffer charset encoding)
134     (enriched-decode beg (point-max))
135     ))
136
137
138 ;;; @ end
139 ;;;
140
141 (provide 'mime-text)
142
143 ;;; mime-text.el ends here