1 ;;; mime-text.el --- mime-view content filter for text
3 ;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: text, MIME, multimedia, mail, news
8 ;; This file is part of SEMI (Suite of Emacs MIME Interfaces).
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.
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.
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.
27 ;;; @ buffer local variables in raw-buffer
30 (defvar mime-text-decoder nil
31 "Function to decode text in current buffer.
32 Interface of the function is (CHARSET &optional ENCODING).
33 CHARSET is symbol of MIME charset and ENCODING is value of
34 Content-Transfer-Encoding.
36 Notice that this variable is usually used as buffer local variable in
39 (make-variable-buffer-local 'mime-text-decoder)
45 (defvar mime-text-decoder-alist
46 '((mime-show-message-mode . mime-text-decode-buffer)
47 (mime-temp-message-mode . mime-text-decode-buffer)
48 (t . mime-text-decode-buffer-maybe)
50 "Alist of major-mode vs. mime-text-decoder.
51 Each element looks like (SYMBOL . FUNCTION). SYMBOL is major-mode or
54 Specification of FUNCTION is described in DOC-string of variable
57 This value is overridden by buffer local variable `mime-text-decoder'
60 (defun mime-text-decode-buffer (charset &optional encoding)
61 "Decode text of current buffer as CHARSET.
62 It code-converts current buffer from network representation specified
63 by MIME CHARSET to internal code. CHARSET is symbol of MIME charset.
64 See also variable `mime-charset-coding-system-alist'."
65 (decode-mime-charset-region (point-min)(point-max)
66 (or charset default-mime-charset))
69 (defun mime-text-decode-buffer-maybe (charset &optional encoding)
70 "Decode text of current buffer as CHARSET if ENCODING is actual encoding.
71 It code-converts current buffer from network representation specified
72 by MIME CHARSET to internal code if ENCODING is not nil, \"7bit\",
73 \"8bit\" or \"binary\". CHARSET is symbol of MIME charset.
74 See also variable `mime-charset-coding-system-alist'."
75 (or (member encoding '(nil "7bit" "8bit" "binary"))
76 (mime-text-decode-buffer charset)
79 (defun mime-decode-text-body (charset encoding)
80 "Decode current buffer as text body.
81 It decodes MIME-encoding as ENCODING then code-converts as MIME
82 CHARSET. CHARSET is SYMBOL and ENCODING is nil or STRING.
84 It calls text decoder for MIME charset specified by buffer local
85 variable `mime-text-decoder' and variable `mime-text-decoder-alist'."
86 (mime-decode-region (point-min) (point-max) encoding)
87 (goto-char (point-min))
88 (while (search-forward "\r\n" nil t)
93 (set-buffer mime-raw-buffer)
95 (cdr (or (assq major-mode mime-text-decoder-alist)
96 (assq t mime-text-decoder-alist)))
98 (and (functionp text-decoder)
99 (funcall text-decoder charset encoding)
106 (require 'browse-url)
108 (defvar mime-text-url-regexp
109 "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]"
110 "*Regexp to match URL in text/plain body.")
112 (defun mime-text-browse-url (&optional url)
113 (if (fboundp browse-url-browser-function)
115 (funcall browse-url-browser-function url)
116 (call-interactively browse-url-browser-function))
117 (if (fboundp mime-button-mother-dispatcher)
118 (call-interactively mime-button-mother-dispatcher)
123 ;;; @ content filters for mime-text
126 (defun mime-view-filter-for-text/plain (ctype params encoding)
127 (mime-decode-text-body (cdr (assoc "charset" params)) encoding)
128 (goto-char (point-max))
129 (if (not (eq (char-after (1- (point))) ?\n))
132 (if browse-url-browser-function
134 (goto-char (point-min))
135 (while (re-search-forward mime-text-url-regexp nil t)
136 (let ((beg (match-beginning 0))
138 (mime-add-button beg end
139 (function mime-text-browse-url)
140 (list (buffer-substring beg end))))
142 (run-hooks 'mime-view-plain-text-preview-hook)
145 (defun mime-view-filter-for-text/richtext (ctype params encoding)
146 (let* ((charset (cdr (assoc "charset" params)))
149 (remove-text-properties beg (point-max) '(face nil))
150 (mime-decode-text-body charset encoding)
151 (richtext-decode beg (point-max))
154 (defun mime-view-filter-for-text/enriched (ctype params encoding)
155 (let* ((charset (cdr (assoc "charset" params)))
158 (remove-text-properties beg (point-max) '(face nil))
159 (mime-decode-text-body charset encoding)
160 (enriched-decode beg (point-max))
169 ;;; mime-text.el ends here