;;; mime-text.el --- mime-view content filter for text
-;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
+;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
-;; Version:
-;; $Id: mime-text.el,v 0.5 1997-02-25 07:30:15 tmorioka Exp $
;; Keywords: text, MIME, multimedia, mail, news
-;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
+;; This file is part of WEMI (Widget based Emacs MIME Interfaces).
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;;; Code:
-;;; @ code conversion
+(require 'mime-view)
+(autoload 'widget-convert-text "wid-edit")
+
+
+;;; @ buffer local variables in raw-buffer
;;;
-(defvar mime-text-decoder-alist
- '((mime/show-message-mode . mime-charset/decode-buffer)
- (mime/temporary-message-mode . mime-charset/decode-buffer)
- (t . mime-charset/maybe-decode-buffer)
- ))
+(defvar mime-text-decoder nil
+ "Function to decode text in current buffer.
+Interface of the function is (CHARSET &optional ENCODING).
+CHARSET is symbol of MIME charset and ENCODING is value of
+Content-Transfer-Encoding.
+
+Notice that this variable is usually used as buffer local variable in
+raw-buffer.")
+
+(make-variable-buffer-local 'mime-text-decoder)
+
+
+;;; @ code conversion
+;;;
-(defun mime-charset/decode-buffer (charset &optional encoding)
+(defun mime-text-decode-buffer (charset &optional encoding)
+ "Decode text of current buffer as CHARSET.
+It code-converts current buffer from network representation specified
+by MIME CHARSET to internal code. CHARSET is symbol of MIME charset.
+See also variable `mime-charset-coding-system-alist'."
(decode-mime-charset-region (point-min)(point-max)
(or charset default-mime-charset))
)
-(defun mime-charset/maybe-decode-buffer (charset &optional encoding)
+(defun mime-text-decode-buffer-maybe (charset &optional encoding)
+ "Decode text of current buffer as CHARSET if ENCODING is actual encoding.
+It code-converts current buffer from network representation specified
+by MIME CHARSET to internal code if ENCODING is not nil, \"7bit\",
+\"8bit\" or \"binary\". CHARSET is symbol of MIME charset.
+See also variable `mime-charset-coding-system-alist'."
(or (member encoding '(nil "7bit" "8bit" "binary"))
- (mime-charset/decode-buffer charset)
+ (mime-text-decode-buffer charset)
))
-(defun mime-preview/decode-text-buffer (charset encoding)
- (mime-decode-region (point-min) (point-max) encoding)
- (let* ((mode mime::preview/original-major-mode)
- (m (or (save-excursion
- (set-buffer mime::preview/article-buffer)
- mime::article/code-converter)
- (cdr (or (assq mode mime-text-decoder-alist)
- (assq t mime-text-decoder-alist)))
- ))
- )
- (and (functionp m)
- (funcall m charset encoding)
- )))
+(defun mime-text-decode-body (situation)
+ "Decode current buffer as text body.
+It decodes MIME-encoding then code-converts as MIME-charset.
+MIME-encoding is value of field 'encoding of SITUATION. It must be
+'nil or string. MIME-charset is value of field \"charset\" of
+SITUATION. It must be symbol.
+This function calls text-decoder for MIME-charset specified by buffer
+local variable `mime-text-decoder' and variable
+`mime-text-decoder-alist'."
+ (let ((encoding (cdr (assq 'encoding situation))))
+ (mime-decode-region (point-min) (point-max) encoding)
+ (goto-char (point-min))
+ (while (search-forward "\r\n" nil t)
+ (replace-match "\n")
+ )
+ (let ((text-decoder
+ (save-excursion
+ (set-buffer mime-raw-buffer)
+ (or mime-text-decoder
+ (cdr (or (assq major-mode mime-text-decoder-alist)
+ (assq t mime-text-decoder-alist)))
+ ))))
+ (and (functionp text-decoder)
+ (funcall text-decoder (cdr (assoc "charset" situation)) encoding)
+ ))
+ (run-hooks 'mime-text-decode-hook)
+ ))
;;; @ for URL
;;;
-(defvar tm:URL-regexp
- "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
+(require 'browse-url)
-(defvar browse-url-browser-function nil)
+(defvar mime-text-url-regexp
+ "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]"
+ "*Regexp to match URL in text/plain body.")
-(defun tm:browse-url (&optional url)
+(defun mime-text-browse-url (&optional url)
(if (fboundp browse-url-browser-function)
(if url
(funcall browse-url-browser-function url)
(call-interactively browse-url-browser-function))
- (if (fboundp tm:mother-button-dispatcher)
- (call-interactively tm:mother-button-dispatcher)
+ (if (fboundp mime-button-mother-dispatcher)
+ (call-interactively mime-button-mother-dispatcher)
)
))
+(defsubst mime-text-add-url-buttons ()
+ "Add URL-buttons for text body."
+ (goto-char (point-min))
+ (while (re-search-forward mime-text-url-regexp nil t)
+ (let ((beg (match-beginning 0))
+ (end (match-end 0)))
+ (widget-convert-text 'url-link beg end)
+ )))
+
+(defun mime-text-add-url-buttons-maybe ()
+ "Add URL-buttons if 'browse-url-browser-function is not 'nil."
+ (if browse-url-browser-function
+ (mime-text-add-url-buttons)
+ ))
+
;;; @ content filters for mime-text
;;;
-(defun mime-preview/filter-for-text/plain (ctype params encoding)
- (mime-preview/decode-text-buffer (cdr (assoc "charset" params)) encoding)
+(defun mime-preview-filter-for-text/plain (situation)
+ (mime-text-decode-body situation)
(goto-char (point-max))
(if (not (eq (char-after (1- (point))) ?\n))
(insert "\n")
)
- (if browse-url-browser-function
- (progn
- (goto-char (point-min))
- (while (re-search-forward tm:URL-regexp nil t)
- (let ((beg (match-beginning 0))
- (end (match-end 0)))
- (tm:add-button beg end
- (function tm:browse-url)
- (list (buffer-substring beg end))))
- )))
- (run-hooks 'mime-view-plain-text-preview-hook)
+ (mime-text-add-url-buttons)
+ (run-hooks 'mime-preview-text/plain-hook)
)
-(defun mime-preview/filter-for-text/richtext (ctype params encoding)
- (let* ((mode mime::preview/original-major-mode)
- (m (assq mode mime-text-decoder-alist))
- (charset (cdr (assoc "charset" params)))
- (beg (point-min))
- )
+(defun mime-preview-filter-for-text/richtext (situation)
+ (let ((beg (point-min)))
(remove-text-properties beg (point-max) '(face nil))
- (mime-preview/decode-text-buffer charset encoding)
+ (mime-text-decode-body situation)
(richtext-decode beg (point-max))
))
-(defun mime-preview/filter-for-text/enriched (ctype params encoding)
- (let* ((mode mime::preview/original-major-mode)
- (m (assq mode mime-text-decoder-alist))
- (charset (cdr (assoc "charset" params)))
- (beg (point-min))
- )
+(defun mime-preview-filter-for-text/enriched (situation)
+ (let ((beg (point-min)))
(remove-text-properties beg (point-max) '(face nil))
- (mime-preview/decode-text-buffer charset encoding)
+ (mime-text-decode-body situation)
(enriched-decode beg (point-max))
))