Sync up with chao-1_3_0_9.
[elisp/flim.git] / mime-def.el
1 ;;; mime-def.el --- definition module about MIME
2
3 ;; Copyright (C) 1995,1996,1997,1998 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 (defconst mime-library-version-string "FLIM 1.4.0 - \"Kintetsu-Tanbabashi\"")
28
29
30 ;;; @ variables
31 ;;;
32
33 (require 'custom)
34
35 (defgroup mime nil
36   "Emacs MIME Interfaces"
37   :group 'news
38   :group 'mail)
39
40 (custom-handle-keyword 'default-mime-charset :group 'mime
41                        'custom-variable)
42
43 (defcustom mime-temp-directory (or (getenv "MIME_TMP_DIR")
44                                    (getenv "TM_TMP_DIR")
45                                    (getenv "TMPDIR")
46                                    (getenv "TMP")
47                                    (getenv "TEMP")
48                                    "/tmp/")
49   "*Directory for temporary files."
50   :group 'mime
51   :type 'directory)
52
53
54 ;;; @ required functions
55 ;;;
56
57 (unless (fboundp 'butlast)
58   (defun butlast (x &optional n)
59     "Returns a copy of LIST with the last N elements removed."
60     (if (and n (<= n 0)) x
61       (nbutlast (copy-sequence x) n)))
62   
63   (defun nbutlast (x &optional n)
64     "Modifies LIST to remove the last N elements."
65     (let ((m (length x)))
66       (or n (setq n 1))
67       (and (< n m)
68            (progn
69              (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
70              x))))
71   )
72
73 (defsubst eliminate-top-spaces (string)
74   "Eliminate top sequence of space or tab in STRING."
75   (if (string-match "^[ \t]+" string)
76       (substring string (match-end 0))
77     string))
78
79 (defsubst regexp-* (regexp)
80   (concat regexp "*"))
81
82 (defsubst regexp-or (&rest args)
83   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
84
85
86 ;;; @ about STD 11
87 ;;;
88
89 (defconst std11-quoted-pair-regexp "\\\\.")
90 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
91 (defconst std11-qtext-regexp
92   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
93 (defconst std11-quoted-string-regexp
94   (concat "\""
95           (regexp-*
96            (regexp-or std11-qtext-regexp std11-quoted-pair-regexp)
97            )
98           "\""))
99
100
101 ;;; @ about MIME
102 ;;;
103
104 (defconst mime-tspecials "][()<>@,\;:\\\"/?=")
105 (defconst mime-token-regexp (concat "[^" mime-tspecials "\000-\040]+"))
106 (defconst mime-charset-regexp mime-token-regexp)
107
108 (defconst mime-media-type/subtype-regexp
109   (concat mime-token-regexp "/" mime-token-regexp))
110
111
112 ;;; @@ Quoted-Printable
113 ;;;
114
115 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
116
117 (defconst quoted-printable-octet-regexp
118   (concat "=[" quoted-printable-hex-chars
119           "][" quoted-printable-hex-chars "]"))
120
121
122 ;;; @ MIME-entity
123 ;;;
124
125 (defsubst make-mime-entity (buffer
126                             header-start header-end body-start body-end
127                             &optional node-id
128                             content-type content-disposition
129                             encoding children)
130   (vector buffer header-start header-end body-start body-end
131           node-id content-type content-disposition encoding nil
132           children nil))
133
134 (defsubst mime-entity-buffer (entity)              (aref entity  0))
135 (defsubst mime-entity-header-start (entity)        (aref entity  1))
136 (defsubst mime-entity-header-end (entity)          (aref entity  2))
137 (defsubst mime-entity-body-start (entity)          (aref entity  3))
138 (defsubst mime-entity-body-end (entity)            (aref entity  4))
139 (defsubst mime-entity-node-id (entity)             (aref entity  5))
140 (defsubst mime-entity-content-type (entity)        (aref entity  6))
141 (defsubst mime-entity-content-disposition (entity) (aref entity  7))
142 (defsubst mime-entity-encoding (entity)            (aref entity  8))
143 (defsubst mime-entity-original-header (entity)     (aref entity  9))
144 (defsubst mime-entity-children (entity)            (aref entity 10))
145 (defsubst mime-entity-parsed-header (entity)       (aref entity 11))
146
147 (defsubst mime-entity-set-original-header (entity header)
148   (aset entity 9 header))
149 (defsubst mime-entity-set-parsed-header (entity header)
150   (aset entity 11 header))
151
152 (defsubst mime-entity-number (entity)
153   (reverse (mime-entity-node-id entity)))
154
155
156 ;;; @ utility
157 ;;;
158
159 (defsubst mime-type/subtype-string (type &optional subtype)
160   "Return type/subtype string from TYPE and SUBTYPE."
161   (if type
162       (if subtype
163           (format "%s/%s" type subtype)
164         (format "%s" type))))
165
166
167 ;;; @ end
168 ;;;
169
170 (provide 'mime-def)
171
172 ;;; mime-def.el ends here