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