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.
30 ;;; @ buffer local variables in raw-buffer
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.
39 Notice that this variable is usually used as buffer local variable in
42 (make-variable-buffer-local 'mime-text-decoder)
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))
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)
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)
84 (set-buffer mime-raw-buffer)
86 (cdr (or (assq major-mode mime-text-decoder-alist)
87 (assq t mime-text-decoder-alist)))
89 (and (functionp text-decoder)
90 (funcall text-decoder (cdr (assoc "charset" situation)) encoding)
92 (run-hooks 'mime-text-decode-hook)
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.")
105 (defun mime-text-browse-url (&optional url)
106 (if (fboundp browse-url-browser-function)
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)
115 (defsubst mime-text-add-url-buttons ()
116 "Add URL-buttons for text body."
117 (goto-char (point-min))
118 (while (re-search-forward mime-text-url-regexp nil t)
119 (let ((beg (match-beginning 0))
121 (mime-add-button beg end #'mime-text-browse-url
122 (list (buffer-substring beg end)))
125 (defun mime-text-add-url-buttons-maybe ()
126 "Add URL-buttons if 'browse-url-browser-function is not 'nil."
127 (if browse-url-browser-function
128 (mime-text-add-url-buttons)
132 ;;; @ content filters for mime-text
135 (defun mime-preview-filter-for-text/plain (situation)
136 (mime-text-decode-body situation)
137 (goto-char (point-max))
138 (if (not (eq (char-after (1- (point))) ?\n))
141 (mime-text-add-url-buttons)
142 (run-hooks 'mime-preview-text/plain-hook)
145 (defun mime-preview-filter-for-text/richtext (situation)
146 (let ((beg (point-min)))
147 (remove-text-properties beg (point-max) '(face nil))
148 (mime-text-decode-body situation)
149 (richtext-decode beg (point-max))
152 (defun mime-preview-filter-for-text/enriched (situation)
153 (let ((beg (point-min)))
154 (remove-text-properties beg (point-max) '(face nil))
155 (mime-text-decode-body situation)
156 (enriched-decode beg (point-max))
165 ;;; mime-text.el ends here