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