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