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.
31 (defsubst mailcap-skip-comment ()
32 (let ((chr (char-after (point))))
43 (defsubst mailcap-look-at-token ()
44 (if (looking-at mime-token-regexp)
45 (let ((beg (match-beginning 0))
48 (buffer-substring beg end)
55 (defsubst mailcap-look-at-type-field ()
56 (let ((type (mailcap-look-at-token)))
58 (if (eq (char-after (point)) ?/)
61 (let ((subtype (mailcap-look-at-token)))
63 (cons (cons 'type (intern type))
64 (unless (string= subtype "*")
65 (list (cons 'subtype (intern subtype)))
67 (list (cons 'type (intern type)))
74 (defsubst mailcap-skip-field-separator ()
75 (let ((ret (looking-at "\\([ \t]\\|\\\\\n\\)*;\\([ \t]\\|\\\\\n\\)*")))
77 (goto-char (match-end 0))
84 (defsubst mailcap-look-at-schar ()
85 (let ((chr (char-after (point))))
94 (defsubst mailcap-look-at-qchar ()
95 (let ((chr (char-after (point))))
101 (defsubst mailcap-look-at-mtext ()
103 (while (or (mailcap-look-at-qchar)
104 (mailcap-look-at-schar)))
105 (buffer-substring beg (point))
112 (defsubst mailcap-look-at-field ()
113 (let ((token (mailcap-look-at-token)))
115 (if (looking-at "[ \t]*=[ \t]*")
117 (goto-char (match-end 0))
118 (mailcap-look-at-mtext))))
120 (cons (intern token) value)
122 (list (intern token))
129 (defun mailcap-look-at-entry ()
130 (let ((type (mailcap-look-at-type-field)))
131 (if (and type (mailcap-skip-field-separator))
132 (let ((view (mailcap-look-at-mtext))
135 (while (and (mailcap-skip-field-separator)
136 (setq field (mailcap-look-at-field))
138 (setq fields (cons field fields))
141 (list (cons 'view view))
148 (defun mailcap-parse-buffer (&optional buffer order)
149 "Parse BUFFER as a mailcap, and return the result.
150 If optional argument ORDER is a function, result is sorted by it.
151 If optional argument ORDER is not specified, result is sorted original
152 order. Otherwise result is not sorted."
156 (goto-char (point-min))
159 (while (mailcap-skip-comment))
160 (setq entry (mailcap-look-at-entry))
162 (setq entries (cons entry entries))
165 (cond ((functionp order) (sort entries order))
166 ((null order) (nreverse entries))
170 (defvar mailcap-file "~/.mailcap"
171 "*File name of user's mailcap file.")
173 (defun mailcap-parse-file (&optional filename order)
174 "Parse FILENAME as a mailcap, and return the result.
175 If optional argument ORDER is a function, result is sorted by it.
176 If optional argument ORDER is not specified, result is sorted original
177 order. Otherwise result is not sorted."
179 (setq filename mailcap-file))
181 (insert-file-contents filename)
182 (mailcap-parse-buffer (current-buffer) order)
185 (defun mailcap-format-command (mtext situation)
186 "Return formated command string from MTEXT and SITUATION.
188 MTEXT is a command text of mailcap specification, such as
191 SITUATION is an association-list about information of entity. Its key
194 'type primary media-type
195 'subtype media-subtype
197 STRING parameter of Content-Type field"
203 (let ((chr (aref mtext i)))
208 (let ((file (cdr (assq 'filename situation))))
210 (error "'filename is not specified in situation.")
211 (setq dest (concat dest
212 (substring mtext p (1- i))
218 (let ((type (or (mime-type/subtype-string
219 (cdr (assq 'type situation))
220 (cdr (assq 'subtype situation)))
222 (setq dest (concat dest
223 (substring mtext p (1- i))
230 (if (not (string-match "}" mtext i))
231 (error "parse error!!!")
232 (let* ((me (match-end 0))
233 (attribute (substring mtext i (1- me)))
234 (parameter (cdr (assoc attribute situation))))
236 (error "\"%s\" is not specified in situation."
238 (setq dest (concat dest
239 (substring mtext p (- i 2))
245 (t (error "Invalid sequence `%%%c'." chr))
248 (setq dest (concat dest (substring mtext p i))
254 (concat dest (substring mtext p))
263 ;;; mailcap.el ends here