Merge chao-1_14_1-1.
[elisp/flim.git] / mmgeneric.el
1 ;;; mmgeneric.el --- MIME generic entity module
2
3 ;; Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: definition, MIME, multimedia, mail, news
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
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 'luna)
28
29
30 ;;; @ MIME entity
31 ;;;
32
33 (autoload 'mime-entity-content-type "mime")
34 (autoload 'mime-parse-multipart "mime-parse")
35 (autoload 'mime-parse-message "mime-parse")
36 ;; (autoload 'mime-parse-encapsulated "mime-parse")
37 ;; (autoload 'mime-parse-external "mime-parse")
38 (autoload 'mime-entity-content "mime")
39
40 (eval-and-compile
41   (luna-define-class mime-entity ()
42                      (location
43                       content-type children parent
44                       node-id
45                       content-disposition encoding
46                       ;; for other fields
47                       original-header parsed-header))
48
49   (luna-define-internal-accessors 'mime-entity)
50   )
51
52 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
53 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
54
55 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
56                                              field-name)
57   (or (symbolp field-name)
58       (setq field-name (intern (capitalize (capitalize field-name)))))
59   (cdr (assq field-name
60              (mime-entity-original-header-internal entity))))
61
62 (luna-define-method mime-insert-text-content ((entity mime-entity))
63   (insert
64    (decode-mime-charset-string (mime-entity-content entity)
65                                (or (mime-content-type-parameter
66                                     (mime-entity-content-type entity)
67                                     "charset")
68                                    default-mime-charset)
69                                'CRLF)
70    ))
71
72
73 ;;; @ for mm-backend
74 ;;;
75
76 (defmacro mm-expand-class-name (type)
77   `(intern (format "mime-%s-entity" ,type)))
78
79 (defmacro mm-define-backend (type &optional parents)
80   `(luna-define-class ,(mm-expand-class-name type)
81                       ,(nconc (mapcar (lambda (parent)
82                                         (mm-expand-class-name parent)
83                                         )
84                                       parents)
85                               '(mime-entity))))
86
87 (defmacro mm-define-method (name args &rest body)
88   (or (eq name 'initialize-instance)
89       (setq name (intern (format "mime-%s" name))))
90   (let ((spec (car args)))
91     (setq args
92           (cons (list (car spec)
93                       (mm-expand-class-name (nth 1 spec)))
94                 (cdr args)))
95     `(luna-define-method ,name ,args ,@body)
96     ))
97
98 (put 'mm-define-method 'lisp-indent-function 'defun)
99
100 (def-edebug-spec mm-define-method
101   (&define name ((arg symbolp)
102                  [&rest arg]
103                  [&optional ["&optional" arg &rest arg]]
104                  &optional ["&rest" arg]
105                  )
106            def-body))
107
108
109 ;;; @ header filter
110 ;;;
111
112 ;; [tomo] We should think about specification of better filtering
113 ;; mechanism.  Please discuss in the emacs-mime mailing lists.
114
115 (defun mime-visible-field-p (field-name visible-fields invisible-fields)
116   (or (catch 'found
117         (while visible-fields
118           (let ((regexp (car visible-fields)))
119             (if (string-match regexp field-name)
120                 (throw 'found t)
121               ))
122           (setq visible-fields (cdr visible-fields))
123           ))
124       (catch 'found
125         (while invisible-fields
126           (let ((regexp (car invisible-fields)))
127             (if (string-match regexp field-name)
128                 (throw 'found nil)
129               ))
130           (setq invisible-fields (cdr invisible-fields))
131           )
132         t)))
133
134 (defun mime-insert-header-from-buffer (buffer start end
135                                               &optional invisible-fields
136                                               visible-fields)
137   (let ((the-buf (current-buffer))
138         (mode-obj (mime-find-field-presentation-method 'wide))
139         field-decoder
140         f-b p f-e field-name len field field-body)
141     (save-excursion
142       (set-buffer buffer)
143       (save-restriction
144         (narrow-to-region start end)
145         (goto-char start)
146         (while (re-search-forward std11-field-head-regexp nil t)
147           (setq f-b (match-beginning 0)
148                 p (match-end 0)
149                 field-name (buffer-substring f-b p)
150                 len (string-width field-name)
151                 f-e (std11-field-end))
152           (when (mime-visible-field-p field-name
153                                       visible-fields invisible-fields)
154             (setq field (intern
155                          (capitalize (buffer-substring f-b (1- p))))
156                   field-body (buffer-substring p f-e)
157                   field-decoder (inline (mime-find-field-decoder-internal
158                                          field mode-obj)))
159             (with-current-buffer the-buf
160               (insert field-name)
161               (insert (if field-decoder
162                           (funcall field-decoder field-body len)
163                         ;; Don't decode
164                         field-body))
165               (insert "\n")
166               )))))))
167
168
169 ;;; @ end
170 ;;;
171
172 (provide 'mmgeneric)
173
174 ;;; mmgeneric.el ends here