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