(mel-define-service): Change size of obarray to 7.
[elisp/flim.git] / mime-def.el
1 ;;; mime-def.el --- definition module about MIME
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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 'mcharset)
28
29 (eval-and-compile
30   (defconst mime-library-product ["FLIM" (1 12 5) "Hirahata"]
31     "Product name, version number and code name of MIME-library package.")
32   )
33
34 (defmacro mime-product-name (product)
35   `(aref ,product 0))
36
37 (defmacro mime-product-version (product)
38   `(aref ,product 1))
39
40 (defmacro mime-product-code-name (product)
41   `(aref ,product 2))
42
43 (defconst mime-library-version
44   (eval-when-compile
45     (concat (mime-product-name mime-library-product) " "
46             (mapconcat #'number-to-string
47                        (mime-product-version mime-library-product) ".")
48             " - \"" (mime-product-code-name mime-library-product) "\"")))
49
50
51 ;;; @ variables
52 ;;;
53
54 (require 'custom)
55
56 (eval-when-compile (require 'cl))
57
58 (defgroup mime nil
59   "Emacs MIME Interfaces"
60   :group 'news
61   :group 'mail)
62
63 (custom-handle-keyword 'default-mime-charset :group 'mime
64                        'custom-variable)
65
66 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
67   "*List of encoding names for uuencode format."
68   :group 'mime
69   :type '(repeat string))
70
71
72 ;;; @ required functions
73 ;;;
74
75 (defsubst regexp-* (regexp)
76   (concat regexp "*"))
77
78 (defsubst regexp-or (&rest args)
79   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
80
81
82 ;;; @ about STD 11
83 ;;;
84
85 (eval-and-compile
86   (defconst std11-quoted-pair-regexp "\\\\.")
87   (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
88   (defconst std11-qtext-regexp
89     (eval-when-compile
90       (concat "[^" (apply #'string std11-non-qtext-char-list) "]"))))
91 (defconst std11-quoted-string-regexp
92   (eval-when-compile
93     (concat "\""
94             (regexp-*
95              (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
96             "\"")))
97
98
99 ;;; @ about MIME
100 ;;;
101
102 (defconst mime-tspecial-char-list
103   '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=))
104 (defconst mime-token-regexp
105   (eval-when-compile
106     (concat "[^" mime-tspecial-char-list "\000-\040]+")))
107 (defconst mime-charset-regexp mime-token-regexp)
108
109 (defconst mime-media-type/subtype-regexp
110   (concat mime-token-regexp "/" mime-token-regexp))
111
112
113 ;;; @@ base64 / B
114 ;;;
115
116 (defconst base64-token-regexp "[A-Za-z0-9+/]")
117 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
118
119 (defconst B-encoded-text-regexp
120   (concat "\\(\\("
121           base64-token-regexp
122           base64-token-regexp
123           base64-token-regexp
124           base64-token-regexp
125           "\\)*"
126           base64-token-regexp
127           base64-token-regexp
128           base64-token-padding-regexp
129           base64-token-padding-regexp
130           "\\)"))
131
132 ;; (defconst eword-B-encoding-and-encoded-text-regexp
133 ;;   (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
134
135
136 ;;; @@ Quoted-Printable / Q
137 ;;;
138
139 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
140
141 (defconst quoted-printable-octet-regexp
142   (concat "=[" quoted-printable-hex-chars
143           "][" quoted-printable-hex-chars "]"))
144
145 (defconst Q-encoded-text-regexp
146   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
147
148 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
149 ;;   (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
150
151
152 ;;; @ Content-Type
153 ;;;
154
155 (defsubst make-mime-content-type (type subtype &optional parameters)
156   (list* (cons 'type type)
157          (cons 'subtype subtype)
158          (nreverse parameters))
159   )
160
161 (defsubst mime-content-type-primary-type (content-type)
162   "Return primary-type of CONTENT-TYPE."
163   (cdr (car content-type)))
164
165 (defsubst mime-content-type-subtype (content-type)
166   "Return primary-type of CONTENT-TYPE."
167   (cdr (cadr content-type)))
168
169 (defsubst mime-content-type-parameters (content-type)
170   "Return primary-type of CONTENT-TYPE."
171   (cddr content-type))
172
173 (defsubst mime-content-type-parameter (content-type parameter)
174   "Return PARAMETER value of CONTENT-TYPE."
175   (cdr (assoc parameter (mime-content-type-parameters content-type))))
176
177
178 (defsubst mime-type/subtype-string (type &optional subtype)
179   "Return type/subtype string from TYPE and SUBTYPE."
180   (if type
181       (if subtype
182           (format "%s/%s" type subtype)
183         (format "%s" type))))
184
185
186 ;;; @ Content-Disposition
187 ;;;
188
189 (defsubst mime-content-disposition-type (content-disposition)
190   "Return disposition-type of CONTENT-DISPOSITION."
191   (cdr (car content-disposition)))
192
193 (defsubst mime-content-disposition-parameters (content-disposition)
194   "Return disposition-parameters of CONTENT-DISPOSITION."
195   (cdr content-disposition))
196
197 (defsubst mime-content-disposition-parameter (content-disposition parameter)
198   "Return PARAMETER value of CONTENT-DISPOSITION."
199   (cdr (assoc parameter (cdr content-disposition))))
200
201 (defsubst mime-content-disposition-filename (content-disposition)
202   "Return filename of CONTENT-DISPOSITION."
203   (mime-content-disposition-parameter content-disposition "filename"))
204
205
206 ;;; @ MIME entity
207 ;;;
208
209 (defmacro make-mime-entity-internal (representation-type location
210                                      &optional content-type
211                                      children parent node-id
212                                      ;; for NOV
213                                      decoded-subject decoded-from
214                                      date message-id references
215                                      chars lines
216                                      xref
217                                      ;; for other fields
218                                      original-header parsed-header
219                                      ;; for buffer representation
220                                      buffer
221                                      header-start header-end
222                                      body-start body-end)
223   `(vector ,representation-type ,location
224            ,content-type nil nil ,children ,parent ,node-id
225            ;; for NOV
226            ,decoded-subject ,decoded-from
227            ,date ,message-id ,references
228            ,chars ,lines
229            ,xref
230            ;; for other fields
231            ,original-header ,parsed-header
232            ;; for buffer representation
233            ,buffer ,header-start ,header-end ,body-start ,body-end))
234
235 (defmacro mime-entity-representation-type-internal (entity)
236   `(aref ,entity 0))
237 (defmacro mime-entity-set-representation-type-internal (entity type)
238   `(aset ,entity 0 ,type))
239 (defmacro mime-entity-location-internal (entity)
240   `(aref ,entity 1))
241 (defmacro mime-entity-set-location-internal (entity location)
242   `(aset ,entity 1 ,location))
243
244 (defmacro mime-entity-content-type-internal (entity)
245   `(aref ,entity 2))
246 (defmacro mime-entity-set-content-type-internal (entity type)
247   `(aset ,entity 2 ,type))
248 (defmacro mime-entity-content-disposition-internal (entity)
249   `(aref ,entity 3))
250 (defmacro mime-entity-set-content-disposition-internal (entity disposition)
251   `(aset ,entity 3 ,disposition))
252 (defmacro mime-entity-encoding-internal (entity)
253   `(aref ,entity 4))
254 (defmacro mime-entity-set-encoding-internal (entity encoding)
255   `(aset ,entity 4 ,encoding))
256
257 (defmacro mime-entity-children-internal (entity)
258   `(aref ,entity 5))
259 (defmacro mime-entity-set-children-internal (entity children)
260   `(aset ,entity 5 ,children))
261 (defmacro mime-entity-parent-internal (entity)
262   `(aref ,entity 6))
263 (defmacro mime-entity-node-id-internal (entity)
264   `(aref ,entity 7))
265
266 (defmacro mime-entity-decoded-subject-internal (entity)
267   `(aref ,entity 8))
268 (defmacro mime-entity-set-decoded-subject-internal (entity subject)
269   `(aset ,entity 8 ,subject))
270 (defmacro mime-entity-decoded-from-internal (entity)
271   `(aref ,entity 9))
272 (defmacro mime-entity-set-decoded-from-internal (entity from)
273   `(aset ,entity 9 ,from))
274 (defmacro mime-entity-date-internal (entity)
275   `(aref ,entity 10))
276 (defmacro mime-entity-set-date-internal (entity date)
277   `(aset ,entity 10 ,date))
278 (defmacro mime-entity-message-id-internal (entity)
279   `(aref ,entity 11))
280 (defmacro mime-entity-set-message-id-internal (entity message-id)
281   `(aset ,entity 11 ,message-id))
282 (defmacro mime-entity-references-internal (entity)
283   `(aref ,entity 12))
284 (defmacro mime-entity-set-references-internal (entity references)
285   `(aset ,entity 12 ,references))
286 (defmacro mime-entity-chars-internal (entity)
287   `(aref ,entity 13))
288 (defmacro mime-entity-set-chars-internal (entity chars)
289   `(aset ,entity 13 ,chars))
290 (defmacro mime-entity-lines-internal (entity)
291   `(aref ,entity 14))
292 (defmacro mime-entity-set-lines-internal (entity lines)
293   `(aset ,entity 14 ,lines))
294 (defmacro mime-entity-xref-internal (entity)
295   `(aref ,entity 15))
296 (defmacro mime-entity-set-xref-internal (entity xref)
297   `(aset ,entity 15 ,xref))
298
299 (defmacro mime-entity-original-header-internal (entity)
300   `(aref ,entity 16))
301 (defmacro mime-entity-set-original-header-internal (entity header)
302   `(aset ,entity 16 ,header))
303 (defmacro mime-entity-parsed-header-internal (entity)
304   `(aref ,entity 17))
305 (defmacro mime-entity-set-parsed-header-internal (entity header)
306   `(aset ,entity 17 ,header))
307
308 (defmacro mime-entity-buffer-internal (entity)
309   `(aref ,entity 18))
310 (defmacro mime-entity-set-buffer-internal (entity buffer)
311   `(aset ,entity 18 ,buffer))
312 (defmacro mime-entity-header-start-internal (entity)
313   `(aref ,entity 19))
314 (defmacro mime-entity-set-header-start-internal (entity point)
315   `(aset ,entity 19 ,point))
316 (defmacro mime-entity-header-end-internal (entity)
317   `(aref ,entity 20))
318 (defmacro mime-entity-set-header-end-internal (entity point)
319   `(aset ,entity 20 ,point))
320 (defmacro mime-entity-body-start-internal (entity)
321   `(aref ,entity 21))
322 (defmacro mime-entity-set-body-start-internal (entity point)
323   `(aset ,entity 21 ,point))
324 (defmacro mime-entity-body-end-internal (entity)
325   `(aref ,entity 22))
326 (defmacro mime-entity-set-body-end-internal (entity point)
327   `(aset ,entity 22 ,point))
328
329
330 ;;; @ message structure
331 ;;;
332
333 (defvar mime-message-structure nil
334   "Information about structure of message.
335 Please use reference function `mime-entity-SLOT' to get value of SLOT.
336
337 Following is a list of slots of the structure:
338
339 buffer                  buffer includes this entity (buffer).
340 node-id                 node-id (list of integers)
341 header-start            minimum point of header in raw-buffer
342 header-end              maximum point of header in raw-buffer
343 body-start              minimum point of body in raw-buffer
344 body-end                maximum point of body in raw-buffer
345 content-type            content-type (content-type)
346 content-disposition     content-disposition (content-disposition)
347 encoding                Content-Transfer-Encoding (string or nil)
348 children                entities included in this entity (list of entity)
349
350 If an entity includes other entities in its body, such as multipart or
351 message/rfc822, `mime-entity' structures of them are included in
352 `children', so the `mime-entity' structure become a tree.")
353
354 (make-variable-buffer-local 'mime-message-structure)
355
356
357 ;;; @ for mm-backend
358 ;;;
359
360 (require 'alist)
361
362 (defvar mime-entity-implementation-alist nil)
363
364 (defmacro mm-define-backend (type &optional parents)
365   "Define TYPE as a mm-backend.
366 If PARENTS is specified, TYPE inherits PARENTS.
367 Each parent must be backend name (symbol)."
368   (if parents
369       `(let ((rest ',(reverse parents)))
370          (while rest
371            (set-alist 'mime-entity-implementation-alist
372                       ',type
373                       (copy-alist
374                        (cdr (assq (car rest)
375                                   mime-entity-implementation-alist))))
376            (setq rest (cdr rest))
377            ))))
378
379 (defmacro mm-define-method (name args &rest body)
380   "Define NAME as a method function of (nth 1 (car ARGS)) backend.
381
382 ARGS is like an argument list of lambda, but (car ARGS) must be
383 specialized parameter.  (car (car ARGS)) is name of variable and (nth
384 1 (car ARGS)) is name of backend."
385   (let* ((specializer (car args))
386          (class (nth 1 specializer))
387          (self (car specializer)))
388     `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
389            (func (lambda ,(if self
390                               (cons self (cdr args))
391                             (cdr args))
392                    ,@body)))
393        (if imps
394            (set-alist 'mime-entity-implementation-alist
395                       ',class (put-alist ',name func imps))
396          (set-alist 'mime-entity-implementation-alist
397                     ',class
398                     (list (cons ',name func)))
399          ))))
400
401 (put 'mm-define-method 'lisp-indent-function 'defun)
402
403 (eval-when-compile
404   (defmacro eval-module-depended-macro (module definition)
405     (condition-case nil
406         (progn
407           (require (eval module))
408           definition)
409       (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
410       ))
411   )
412
413 (eval-module-depended-macro
414  'edebug
415  (def-edebug-spec mm-define-method
416    (&define name ((arg symbolp)
417                   [&rest arg]
418                   [&optional ["&optional" arg &rest arg]]
419                   &optional ["&rest" arg]
420                   )
421             def-body))
422  )
423
424 (defsubst mm-arglist-to-arguments (arglist)
425   (let (dest)
426     (while arglist
427       (let ((arg (car arglist)))
428         (or (memq arg '(&optional &rest))
429             (setq dest (cons arg dest)))
430         )
431       (setq arglist (cdr arglist)))
432     (nreverse dest)))
433
434
435 ;;; @ for mel-backend
436 ;;;
437
438 (defvar mel-service-list nil)
439
440 (defmacro mel-define-service (name &optional args &rest rest)
441   "Define NAME as a service for Content-Transfer-Encodings.
442 If ARGS is specified, NAME is defined as a generic function for the
443 service."
444   `(progn
445      (add-to-list 'mel-service-list ',name)
446      (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
447      ,@(if args
448            `((defun ,name ,args
449                ,@rest
450                (funcall (mel-find-function ',name ,(car (last args)))
451                         ,@(mm-arglist-to-arguments (butlast args)))
452                )))
453      ))
454
455 (put 'mel-define-service 'lisp-indent-function 'defun)
456
457
458 (defvar mel-encoding-module-alist nil)
459
460 (defsubst mel-find-function-from-obarray (ob-array encoding)
461   (let* ((f (intern-soft encoding ob-array)))
462     (or f
463         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
464           (while (and rest
465                       (progn
466                         (require (car rest))
467                         (null (setq f (intern-soft encoding ob-array)))
468                         ))
469             (setq rest (cdr rest))
470             )
471           f))))
472
473 (defsubst mel-copy-method (service src-backend dst-backend)
474   (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
475          (f (mel-find-function-from-obarray oa src-backend))
476          sym)
477     (when f
478       (setq sym (intern dst-backend oa))
479       (or (fboundp sym)
480           (fset sym (symbol-function f))
481           ))))
482        
483 (defsubst mel-copy-backend (src-backend dst-backend)
484   (let ((services mel-service-list))
485     (while services
486       (mel-copy-method (car services) src-backend dst-backend)
487       (setq services (cdr services)))))
488
489 (defmacro mel-define-backend (type &optional parents)
490   "Define TYPE as a mel-backend.
491 If PARENTS is specified, TYPE inherits PARENTS.
492 Each parent must be backend name (string)."
493   (cons 'progn
494         (mapcar (lambda (parent)
495                   `(mel-copy-backend ,parent ,type)
496                   )
497                 parents)))
498
499 (defmacro mel-define-method (name args &rest body)
500   "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
501 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
502 specialized parameter.  (car (car (last ARGS))) is name of variable
503 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
504   (let* ((specializer (car (last args)))
505          (class (nth 1 specializer)))
506     `(progn
507        (mel-define-service ,name)
508        (fset (intern ,class ,(intern (format "%s-obarray" name)))
509              (lambda ,(butlast args)
510                ,@body)))))
511
512 (put 'mel-define-method 'lisp-indent-function 'defun)
513
514 (defmacro mel-define-method-function (spec function)
515   "Set SPEC's function definition to FUNCTION.
516 First element of SPEC is service.
517 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
518 must be specialized parameter.  (car (car (last ARGS))) is name of
519 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
520   (let* ((name (car spec))
521          (args (cdr spec))
522          (specializer (car (last args)))
523          (class (nth 1 specializer)))
524     `(let (sym)
525        (mel-define-service ,name)
526        (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
527        (or (fboundp sym)
528            (fset sym (symbol-function ,function))))))
529
530 (defmacro mel-define-function (function spec)
531   (let* ((name (car spec))
532          (args (cdr spec))
533          (specializer (car (last args)))
534          (class (nth 1 specializer)))
535     `(progn
536        (define-function ,function
537          (intern ,class ,(intern (format "%s-obarray" name))))
538        )))
539
540 (defvar base64-dl-module
541   (if (and (fboundp 'base64-encode-string)
542            (subrp (symbol-function 'base64-encode-string)))
543       nil
544     (if (fboundp 'dynamic-link)
545         (let ((path (expand-file-name "base64.so" exec-directory)))
546           (and (file-exists-p path)
547                path)
548           ))))
549
550
551 ;;; @ end
552 ;;;
553
554 (provide 'mime-def)
555
556 ;;; mime-def.el ends here