(eword-decode-string, eword-decode-region): Mention language info in doc string.
[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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Code:
26
27 (require 'luna)
28
29 (eval-when-compile
30   (require 'eword-decode) ; mime-find-field-presentation-method
31   )
32
33
34 ;;; @ MIME entity
35 ;;;
36
37 (autoload 'mime-entity-content-type "mime")
38 (autoload 'mime-parse-multipart "mime-parse")
39 (autoload 'mime-parse-message "mime-parse")
40 ;; (autoload 'mime-parse-encapsulated "mime-parse")
41 ;; (autoload 'mime-parse-external "mime-parse")
42 (autoload 'mime-entity-content "mime")
43
44 (luna-define-class mime-entity ()
45                    (location
46                     content-type children parent
47                     node-id
48                     content-disposition encoding
49                     ;; for other fields
50                     original-header parsed-header))
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-internal-accessors 'mime-entity)
56
57 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
58                                              field-name)
59   (or (symbolp field-name)
60       (setq field-name (intern (capitalize field-name))))
61   (cdr (assq field-name
62              (mime-entity-original-header-internal entity))))
63
64 ;; unlimited patch by simm-emacs@fan.gr.jp
65 ;;   Tue, 01 Feb 2000 13:32:14 +0900
66 (luna-define-method mime-insert-text-content ((entity mime-entity))
67   (insert
68    (decode-mime-charset-string-unlimited
69     (mime-entity-content entity)
70     (or (mime-content-type-parameter
71          (mime-entity-content-type entity)
72          "charset")
73         default-mime-charset-unlimited
74         default-mime-charset)
75     'CRLF)))
76
77
78 ;;; @ for mm-backend
79 ;;;
80
81 (defmacro mm-expand-class-name (type)
82   `(intern (format "mime-%s-entity" ,type)))
83
84 (defmacro mm-define-backend (type &optional parents)
85   `(luna-define-class ,(mm-expand-class-name type)
86                       ,(nconc (mapcar (lambda (parent)
87                                         (mm-expand-class-name parent)
88                                         )
89                                       parents)
90                               '(mime-entity))))
91
92 (defmacro mm-define-method (name args &rest body)
93   (or (eq name 'initialize-instance)
94       (setq name (intern (format "mime-%s" name))))
95   (let ((spec (car args)))
96     (setq args
97           (cons (list (car spec)
98                       (mm-expand-class-name (nth 1 spec)))
99                 (cdr args)))
100     `(luna-define-method ,name ,args ,@body)
101     ))
102
103 (put 'mm-define-method 'lisp-indent-function 'defun)
104
105 (def-edebug-spec mm-define-method
106   (&define name ((arg symbolp)
107                  [&rest arg]
108                  [&optional ["&optional" arg &rest arg]]
109                  &optional ["&rest" arg]
110                  )
111            def-body))
112
113
114 ;;; @ header filter
115 ;;;
116
117 ;; [tomo] We should think about specification of better filtering
118 ;; mechanism.  Please discuss in the emacs-mime mailing lists.
119
120 (defun mime-visible-field-p (field-name visible-fields invisible-fields)
121   (or (catch 'found
122         (while visible-fields
123           (let ((regexp (car visible-fields)))
124             (if (string-match regexp field-name)
125                 (throw 'found t)
126               ))
127           (setq visible-fields (cdr visible-fields))
128           ))
129       (catch 'found
130         (while invisible-fields
131           (let ((regexp (car invisible-fields)))
132             (if (string-match regexp field-name)
133                 (throw 'found nil)
134               ))
135           (setq invisible-fields (cdr invisible-fields))
136           )
137         t)))
138
139 (defun mime-insert-header-from-buffer (buffer start end
140                                               &optional invisible-fields
141                                               visible-fields)
142   (let ((the-buf (current-buffer))
143         (mode-obj (mime-find-field-presentation-method 'wide))
144         field-decoder
145         f-b p f-e field-name len field field-body)
146     (save-excursion
147       (set-buffer buffer)
148       (save-restriction
149         (narrow-to-region start end)
150         (goto-char start)
151         (while (re-search-forward std11-field-head-regexp nil t)
152           (setq f-b (match-beginning 0)
153                 p (match-end 0)
154                 field-name (buffer-substring f-b p)
155                 len (string-width field-name)
156                 f-e (std11-field-end))
157           (when (mime-visible-field-p field-name
158                                       visible-fields invisible-fields)
159             (setq field (intern
160                          (capitalize (buffer-substring f-b (1- p))))
161                   field-body (buffer-substring p f-e)
162                   field-decoder (inline (mime-find-field-decoder-internal
163                                          field mode-obj)))
164             (with-current-buffer the-buf
165               (insert field-name)
166               (insert (if field-decoder
167                           (funcall field-decoder field-body len)
168                         ;; Don't decode
169                         field-body))
170               (insert "\n")
171               )))))))
172
173
174 ;;; @ end
175 ;;;
176
177 (provide 'mmgeneric)
178
179 ;;; mmgeneric.el ends here