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