(config-semi): fixed.
[elisp/semi.git] / mime-text.el
1 ;;; mime-text.el --- mime-view content filter for text
2
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version:
7 ;;      $Id: mime-text.el,v 0.23 1997-04-03 17:39:31 morioka Exp $
8 ;; Keywords: text, MIME, multimedia, mail, news
9
10 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 ;;; @ buffer local variables in raw-buffer
30 ;;;
31
32 (defvar mime-text-decoder nil
33   "Function to decode text in current buffer.
34 Interface of the function is (CHARSET &optional ENCODING).
35 CHARSET is symbol of MIME charset and ENCODING is value of
36 Content-Transfer-Encoding.
37
38 Notice that this variable is usually used as buffer local variable in
39 raw-buffer.")
40
41 (make-variable-buffer-local 'mime-text-decoder)
42
43
44 ;;; @ code conversion
45 ;;;
46
47 (defvar mime-text-decoder-alist
48   '((mime-show-message-mode     . mime-text-decode-buffer)
49     (mime-temp-message-mode     . mime-text-decode-buffer)
50     (t                          . mime-text-decode-buffer-maybe)
51     )
52   "Alist of major-mode vs. mime-text-decoder.
53 Each element looks like (SYMBOL . FUNCTION).  SYMBOL is major-mode or
54 t.  t means default.
55
56 Specification of FUNCTION is described in DOC-string of variable
57 `mime-text-decoder'.
58
59 This value is overridden by buffer local variable `mime-text-decoder'
60 if it is not nil.")
61
62 (defun mime-text-decode-buffer (charset &optional encoding)
63   "Decode text of current buffer as CHARSET.
64 It code-converts current buffer from network representation specified
65 by MIME CHARSET to internal code.  CHARSET is symbol of MIME charset.
66 See also variable `mime-charset-coding-system-alist'."
67   (decode-mime-charset-region (point-min)(point-max)
68                               (or charset default-mime-charset))
69   )
70
71 (defun mime-text-decode-buffer-maybe (charset &optional encoding)
72   "Decode text of current buffer as CHARSET if ENCODING is actual encoding.
73 It code-converts current buffer from network representation specified
74 by MIME CHARSET to internal code if ENCODING is not nil, \"7bit\",
75 \"8bit\" or \"binary\".  CHARSET is symbol of MIME charset.
76 See also variable `mime-charset-coding-system-alist'."
77   (or (member encoding '(nil "7bit" "8bit" "binary"))
78       (mime-text-decode-buffer charset)
79       ))
80
81 (defun mime-decode-text-body (charset encoding)
82   "Decode current buffer as text body.
83 It decodes MIME-encoding as ENCODING then code-converts as MIME
84 CHARSET.  CHARSET is SYMBOL and ENCODING is nil or STRING.
85
86 It calls text decoder for MIME charset specified by buffer local
87 variable `mime-text-decoder' and variable `mime-text-decoder-alist'."
88   (mime-decode-region (point-min) (point-max) encoding)
89   (let ((text-decoder
90          (save-excursion
91            (set-buffer mime-raw-buffer)
92            (or mime-text-decoder
93                (cdr (or (assq major-mode mime-text-decoder-alist)
94                         (assq t mime-text-decoder-alist)))
95                ))))
96     (and (functionp text-decoder)
97          (funcall text-decoder charset encoding)
98          )))
99
100
101 ;;; @ for URL
102 ;;;
103
104 (require 'browse-url)
105
106 (defvar mime-text-url-regexp
107   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]"
108   "*Regexp to match URL in text/plain body.")
109
110 (defun mime-text-browse-url (&optional url)
111   (if (fboundp browse-url-browser-function)
112       (if url 
113         (funcall browse-url-browser-function url)
114       (call-interactively browse-url-browser-function))
115     (if (fboundp mime-button-mother-dispatcher)
116         (call-interactively mime-button-mother-dispatcher)
117       )
118     ))
119
120
121 ;;; @ content filters for mime-text
122 ;;;
123
124 (defun mime-view-filter-for-text/plain (ctype params encoding)
125   (mime-decode-text-body (cdr (assoc "charset" params)) encoding)
126   (goto-char (point-max))
127   (if (not (eq (char-after (1- (point))) ?\n))
128       (insert "\n")
129     )
130   (if browse-url-browser-function
131       (progn
132         (goto-char (point-min))
133         (while (re-search-forward mime-text-url-regexp nil t)
134           (let ((beg (match-beginning 0))
135                 (end (match-end 0)))
136             (mime-add-button beg end
137                              (function mime-text-browse-url)
138                              (list (buffer-substring beg end))))
139           )))
140   (run-hooks 'mime-view-plain-text-preview-hook)
141   )
142
143 (defun mime-view-filter-for-text/richtext (ctype params encoding)
144   (let* ((charset (cdr (assoc "charset" params)))
145          (beg (point-min))
146          )
147     (remove-text-properties beg (point-max) '(face nil))
148     (mime-decode-text-body charset encoding)
149     (richtext-decode beg (point-max))
150     ))
151
152 (defun mime-view-filter-for-text/enriched (ctype params encoding)
153   (let* ((charset (cdr (assoc "charset" params)))
154          (beg (point-min))
155          )
156     (remove-text-properties beg (point-max) '(face nil))
157     (mime-decode-text-body charset encoding)
158     (enriched-decode beg (point-max))
159     ))
160
161
162 ;;; @ end
163 ;;;
164
165 (provide 'mime-text)
166
167 ;;; mime-text.el ends here