1 ;;; mailcap.el --- mailcap parser
3 ;; Copyright (C) 1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Keywords: mailcap, setting, configuration, MIME, multimedia
9 ;; This file is part of SEMI (Spadework for Emacs MIME Interfaces).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
34 (defsubst mailcap-skip-comment ()
35 (let ((chr (char-after (point))))
46 (defsubst mailcap-look-at-token ()
47 (if (looking-at mime-token-regexp)
48 (let ((beg (match-beginning 0))
51 (buffer-substring beg end)
58 (defsubst mailcap-look-at-type-field ()
59 (let ((type (mailcap-look-at-token)))
61 (if (eq (char-after (point)) ?/)
64 (let ((subtype (mailcap-look-at-token)))
66 (cons (cons 'type (intern type))
67 (unless (string= subtype "*")
68 (list (cons 'subtype (intern subtype)))
70 (list (cons 'type (intern type)))
77 (defsubst mailcap-skip-field-separator ()
78 (let ((ret (looking-at "\\([ \t]\\|\\\\\n\\)*;\\([ \t]\\|\\\\\n\\)*")))
80 (goto-char (match-end 0))
87 (defsubst mailcap-look-at-schar ()
88 (let ((chr (char-after (point))))
97 (defsubst mailcap-look-at-qchar ()
98 (let ((chr (char-after (point))))
101 (char-before (point))
104 (defsubst mailcap-look-at-mtext ()
106 (while (or (mailcap-look-at-qchar)
107 (mailcap-look-at-schar)))
108 (buffer-substring beg (point))
115 (defsubst mailcap-look-at-field ()
116 (let ((token (mailcap-look-at-token)))
118 (if (looking-at "[ \t]*=[ \t]*")
120 (goto-char (match-end 0))
121 (mailcap-look-at-mtext))))
123 (cons (intern token) value)
125 (list (intern token))
132 (defun mailcap-look-at-entry ()
133 (let ((type (mailcap-look-at-type-field)))
134 (if (and type (mailcap-skip-field-separator))
135 (let ((view (mailcap-look-at-mtext))
138 (while (and (mailcap-skip-field-separator)
139 (setq field (mailcap-look-at-field))
141 (setq fields (cons field fields))
144 (list (cons 'view view))
151 (defun mailcap-parse-buffer (&optional buffer order)
152 "Parse BUFFER as a mailcap, and return the result.
153 If optional argument ORDER is a function, result is sorted by it.
154 If optional argument ORDER is not specified, result is sorted original
155 order. Otherwise result is not sorted."
159 (goto-char (point-min))
162 (while (mailcap-skip-comment))
163 (setq entry (mailcap-look-at-entry))
165 (setq entries (cons entry entries))
168 (cond ((functionp order) (sort entries order))
169 ((null order) (nreverse entries))
173 (defvar mailcap-file "~/.mailcap"
174 "*File name of user's mailcap file.")
176 (defun mailcap-parse-file (&optional filename order)
177 "Parse FILENAME as a mailcap, and return the result.
178 If optional argument ORDER is a function, result is sorted by it.
179 If optional argument ORDER is not specified, result is sorted original
180 order. Otherwise result is not sorted."
182 (setq filename mailcap-file))
184 (insert-file-contents filename)
185 (mailcap-parse-buffer (current-buffer) order)
188 (defun mailcap-format-command (mtext situation)
189 "Return formated command string from MTEXT and SITUATION.
191 MTEXT is a command text of mailcap specification, such as
194 SITUATION is an association-list about information of entity. Its key
197 'type primary media-type
198 'subtype media-subtype
200 STRING parameter of Content-Type field"
206 (let ((chr (aref mtext i)))
211 (let ((file (cdr (assq 'filename situation))))
213 (error "'filename is not specified in situation.")
214 (setq dest (concat dest
215 (substring mtext p (1- i))
221 (let ((type (or (mime-type/subtype-string
222 (cdr (assq 'type situation))
223 (cdr (assq 'subtype situation)))
225 (setq dest (concat dest
226 (substring mtext p (1- i))
233 (if (not (string-match "}" mtext i))
234 (error "parse error!!!")
235 (let* ((me (match-end 0))
236 (attribute (substring mtext i (1- me)))
237 (parameter (cdr (assoc attribute situation))))
239 (error "\"%s\" is not specified in situation."
241 (setq dest (concat dest
242 (substring mtext p (- i 2))
248 (t (error "Invalid sequence `%%%c'." chr))
251 (setq dest (concat dest (substring mtext p i))
257 (concat dest (substring mtext p))
266 ;;; mailcap.el ends here