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 (defcustom mime-mailcap-file "~/.mailcap"
180 "*File name of user's mailcap file."
185 (defun mime-parse-mailcap-file (&optional filename order)
186 "Parse FILENAME as a mailcap, and return the result.
187 If optional argument ORDER is a function, result is sorted by it.
188 If optional argument ORDER is not specified, result is sorted original
189 order. Otherwise result is not sorted."
191 (setq filename mime-mailcap-file))
193 (insert-file-contents filename)
194 (mime-parse-mailcap-buffer (current-buffer) order)
199 (defun mime-format-mailcap-command (mtext situation)
200 "Return formated command string from MTEXT and SITUATION.
202 MTEXT is a command text of mailcap specification, such as
205 SITUATION is an association-list about information of entity. Its key
208 'type primary media-type
209 'subtype media-subtype
211 STRING parameter of Content-Type field"
217 (let ((chr (aref mtext i)))
222 (let ((file (cdr (assq 'filename situation))))
224 (error "'filename is not specified in situation.")
225 (setq dest (concat dest
226 (substring mtext p (1- i))
232 (let ((type (or (mime-type/subtype-string
233 (cdr (assq 'type situation))
234 (cdr (assq 'subtype situation)))
236 (setq dest (concat dest
237 (substring mtext p (1- i))
244 (if (not (string-match "}" mtext i))
245 (error "parse error!!!")
246 (let* ((me (match-end 0))
247 (attribute (substring mtext i (1- me)))
248 (parameter (cdr (assoc attribute situation))))
250 (error "\"%s\" is not specified in situation."
252 (setq dest (concat dest
253 (substring mtext p (- i 2))
259 (t (error "Invalid sequence `%%%c'." chr))
262 (setq dest (concat dest (substring mtext p i))
268 (concat dest (substring mtext p))
277 ;;; mime-conf.el ends here