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