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