Require mime-view (mime-text.el is autoloaded by mime-view); variable
[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-decode-text-body (charset encoding)
68   "Decode current buffer as text body.
69 It decodes MIME-encoding as ENCODING then code-converts as MIME
70 CHARSET.  CHARSET is SYMBOL and ENCODING is nil or STRING.
71
72 It calls text decoder for MIME charset specified by buffer local
73 variable `mime-text-decoder' and variable `mime-text-decoder-alist'."
74   (mime-decode-region (point-min) (point-max) encoding)
75   (goto-char (point-min))
76   (while (search-forward "\r\n" nil t)
77     (replace-match "\n")
78     )
79   (let ((text-decoder
80          (save-excursion
81            (set-buffer mime-raw-buffer)
82            (or mime-text-decoder
83                (cdr (or (assq major-mode mime-text-decoder-alist)
84                         (assq t mime-text-decoder-alist)))
85                ))))
86     (and (functionp text-decoder)
87          (funcall text-decoder charset encoding)
88          )))
89
90
91 ;;; @ for URL
92 ;;;
93
94 (require 'browse-url)
95
96 (defvar mime-text-url-regexp
97   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]"
98   "*Regexp to match URL in text/plain body.")
99
100 (defun mime-text-browse-url (&optional url)
101   (if (fboundp browse-url-browser-function)
102       (if url 
103         (funcall browse-url-browser-function url)
104       (call-interactively browse-url-browser-function))
105     (if (fboundp mime-button-mother-dispatcher)
106         (call-interactively mime-button-mother-dispatcher)
107       )
108     ))
109
110
111 ;;; @ content filters for mime-text
112 ;;;
113
114 (defun mime-view-filter-for-text/plain (ctype params encoding)
115   (mime-decode-text-body (cdr (assoc "charset" params)) encoding)
116   (goto-char (point-max))
117   (if (not (eq (char-after (1- (point))) ?\n))
118       (insert "\n")
119     )
120   (if browse-url-browser-function
121       (progn
122         (goto-char (point-min))
123         (while (re-search-forward mime-text-url-regexp nil t)
124           (let ((beg (match-beginning 0))
125                 (end (match-end 0)))
126             (mime-add-button beg end
127                              (function mime-text-browse-url)
128                              (list (buffer-substring beg end))))
129           )))
130   (run-hooks 'mime-view-plain-text-preview-hook)
131   )
132
133 (defun mime-view-filter-for-text/richtext (ctype params encoding)
134   (let* ((charset (cdr (assoc "charset" params)))
135          (beg (point-min))
136          )
137     (remove-text-properties beg (point-max) '(face nil))
138     (mime-decode-text-body charset encoding)
139     (richtext-decode beg (point-max))
140     ))
141
142 (defun mime-view-filter-for-text/enriched (ctype params encoding)
143   (let* ((charset (cdr (assoc "charset" params)))
144          (beg (point-min))
145          )
146     (remove-text-properties beg (point-max) '(face nil))
147     (mime-decode-text-body charset encoding)
148     (enriched-decode beg (point-max))
149     ))
150
151
152 ;;; @ end
153 ;;;
154
155 (provide 'mime-text)
156
157 ;;; mime-text.el ends here