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