1 ;;; mime-conf.el --- mailcap parser and MIME playback configuration
3 ;; Copyright (C) 1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
7 ;; Original: 1997-06-27 mailcap.el by MORIOKA Tomohiko
8 ;; Renamed: 2000-11-24 to mime-conf.el by MORIOKA Tomohiko
9 ;; Keywords: mailcap, setting, configuration, MIME, multimedia
11 ;; This file is part of FLIM (Faithful Library about Internet Message).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
36 (defsubst mime-mailcap-skip-comment ()
37 (let ((chr (char-after (point))))
48 (defsubst mime-mailcap-look-at-token ()
49 (if (looking-at mime-token-regexp)
50 (let ((beg (match-beginning 0))
53 (buffer-substring beg end)
60 (defsubst mime-mailcap-look-at-type-field ()
61 (let ((type (mime-mailcap-look-at-token)))
63 (if (eq (char-after (point)) ?/)
66 (let ((subtype (mime-mailcap-look-at-token)))
68 (cons (cons 'type (intern type))
69 (unless (string= subtype "*")
70 (list (cons 'subtype (intern subtype)))
72 (list (cons 'type (intern type)))
79 (defsubst mime-mailcap-skip-field-separator ()
80 (let ((ret (looking-at "\\([ \t]\\|\\\\\n\\)*;\\([ \t]\\|\\\\\n\\)*")))
82 (goto-char (match-end 0))
89 (defsubst mime-mailcap-look-at-schar ()
90 (let ((chr (char-after (point))))
100 (defsubst mime-mailcap-look-at-qchar ()
101 (when (eq (char-after (point)) ?\\)
107 (defsubst mime-mailcap-look-at-mtext ()
109 (while (or (mime-mailcap-look-at-qchar)
110 (mime-mailcap-look-at-schar)))
111 (buffer-substring beg (point))
118 (defsubst mime-mailcap-look-at-field ()
119 (let ((token (mime-mailcap-look-at-token)))
121 (if (looking-at "[ \t]*=[ \t]*")
123 (goto-char (match-end 0))
124 (mime-mailcap-look-at-mtext))))
126 (cons (intern token) value)
128 (list (intern token))
135 (defun mime-mailcap-look-at-entry ()
136 (let ((type (mime-mailcap-look-at-type-field)))
137 (if (and type (mime-mailcap-skip-field-separator))
138 (let ((view (mime-mailcap-look-at-mtext))
141 (while (and (mime-mailcap-skip-field-separator)
142 (setq field (mime-mailcap-look-at-field))
144 (setq fields (cons field fields))
147 (list (cons 'view view))
155 (defun mime-parse-mailcap-buffer (&optional buffer order)
156 "Parse BUFFER as a mailcap, and return the result.
157 If optional argument ORDER is a function, result is sorted by it.
158 If optional argument ORDER is not specified, result is sorted original
159 order. Otherwise result is not sorted."
163 (goto-char (point-min))
166 (while (mime-mailcap-skip-comment))
167 (setq entry (mime-mailcap-look-at-entry))
169 (setq entries (cons entry entries))
172 (cond ((functionp order) (sort entries order))
173 ((null order) (nreverse entries))
179 (defvar mime-mailcap-file "~/.mailcap"
180 "*File name of user's mailcap file.")
183 (defun mime-parse-mailcap-file (&optional filename order)
184 "Parse FILENAME as a mailcap, and return the result.
185 If optional argument ORDER is a function, result is sorted by it.
186 If optional argument ORDER is not specified, result is sorted original
187 order. Otherwise result is not sorted."
189 (setq filename mime-mailcap-file))
191 (insert-file-contents filename)
192 (mime-parse-mailcap-buffer (current-buffer) order)
197 (defun mime-format-mailcap-command (mtext situation)
198 "Return formated command string from MTEXT and SITUATION.
200 MTEXT is a command text of mailcap specification, such as
203 SITUATION is an association-list about information of entity. Its key
206 'type primary media-type
207 'subtype media-subtype
209 STRING parameter of Content-Type field"
215 (let ((chr (aref mtext i)))
220 (let ((file (cdr (assq 'filename situation))))
222 (error "'filename is not specified in situation.")
223 (setq dest (concat dest
224 (substring mtext p (1- i))
230 (let ((type (or (mime-type/subtype-string
231 (cdr (assq 'type situation))
232 (cdr (assq 'subtype situation)))
234 (setq dest (concat dest
235 (substring mtext p (1- i))
242 (if (not (string-match "}" mtext i))
243 (error "parse error!!!")
244 (let* ((me (match-end 0))
245 (attribute (substring mtext i (1- me)))
246 (parameter (cdr (assoc attribute situation))))
248 (error "\"%s\" is not specified in situation."
250 (setq dest (concat dest
251 (substring mtext p (- i 2))
257 (t (error "Invalid sequence `%%%c'." chr))
260 (setq dest (concat dest (substring mtext p i))
266 (concat dest (substring mtext p))
275 ;;; mime-conf.el ends here