(mime-entity-representation-type): New function.
[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 ;;; @ code conversion
31 ;;;
32
33 (defun mime-text-insert-decoded-body (entity)
34   "Insert text body of ENTITY in SITUATION.
35 It decodes MIME-encoding then code-converts as MIME-charset.
36 MIME-encoding is value of field 'encoding of SITUATION.  It must be
37 'nil or string.  MIME-charset is value of field \"charset\" of
38 SITUATION.  It must be symbol."
39   (let* ((buffer (mime-entity-buffer entity))
40          (presentation-type
41           (save-excursion
42             (set-buffer buffer)
43             (or mime-raw-representation-type
44                 (cdr (or (assq major-mode mime-raw-representation-type-alist)
45                          (assq t mime-raw-representation-type-alist)))
46                 ))))
47     (let ((str (mime-entity-content entity)))
48       (insert
49        (if (or (eq presentation-type 'binary)
50                (not (member (mime-entity-encoding entity)
51                             '(nil "7bit" "8bit" "binary"))))
52            (decode-mime-charset-string
53             str
54             (or (mime-content-type-parameter
55                  (mime-entity-content-type entity) "charset")
56                 default-mime-charset))
57          str))
58       ))
59   (run-hooks 'mime-text-decode-hook)
60   )
61
62
63 ;;; @ for URL
64 ;;;
65
66 (require 'browse-url)
67
68 (defvar mime-text-url-regexp
69   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]"
70   "*Regexp to match URL in text/plain body.")
71
72 (defun mime-text-browse-url (&optional url)
73   (if (fboundp browse-url-browser-function)
74       (if url 
75         (funcall browse-url-browser-function url)
76       (call-interactively browse-url-browser-function))
77     (if (fboundp mime-button-mother-dispatcher)
78         (call-interactively mime-button-mother-dispatcher)
79       )
80     ))
81
82 (defsubst mime-text-add-url-buttons ()
83   "Add URL-buttons for text body."
84   (goto-char (point-min))
85   (while (re-search-forward mime-text-url-regexp nil t)
86     (let ((beg (match-beginning 0))
87           (end (match-end 0)))
88       (mime-add-button beg end #'mime-text-browse-url
89                        (list (buffer-substring beg end)))
90       )))
91
92 (defun mime-text-add-url-buttons-maybe ()
93   "Add URL-buttons if 'browse-url-browser-function is not 'nil."
94   (if browse-url-browser-function
95       (mime-text-add-url-buttons)
96     ))
97
98
99 ;;; @ content filters for mime-text
100 ;;;
101
102 (defun mime-display-text/plain (entity situation)
103   (save-restriction
104     (narrow-to-region (point-max)(point-max))
105     (mime-text-insert-decoded-body entity)
106     (goto-char (point-max))
107     (if (not (eq (char-after (1- (point))) ?\n))
108         (insert "\n")
109       )
110     (mime-text-add-url-buttons)
111     (run-hooks 'mime-display-text/plain-hook)
112     ))
113
114 (defun mime-display-text/richtext (entity situation)
115   (save-restriction
116     (narrow-to-region (point-max)(point-max))
117     (mime-text-insert-decoded-body entity)
118     (let ((beg (point-min)))
119       (remove-text-properties beg (point-max) '(face nil))
120       (richtext-decode beg (point-max))
121       )))
122
123 (defun mime-display-text/enriched (entity situation)
124   (save-restriction
125     (narrow-to-region (point-max)(point-max))
126     (mime-text-insert-decoded-body entity)
127     (let ((beg (point-min)))
128       (remove-text-properties beg (point-max) '(face nil))
129       (enriched-decode beg (point-max))
130       )))
131
132
133 ;;; @ end
134 ;;;
135
136 (provide 'mime-text)
137
138 ;;; mime-text.el ends here