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