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))))
98 (defsubst mailcap-look-at-qchar ()
99 (when (eq (char-after (point)) ?\\)
105 (defsubst mailcap-look-at-mtext ()
107 (while (or (mailcap-look-at-qchar)
108 (mailcap-look-at-schar)))
109 (buffer-substring beg (point))
116 (defsubst mailcap-look-at-field ()
117 (let ((token (mailcap-look-at-token)))
119 (if (looking-at "[ \t]*=[ \t]*")
121 (goto-char (match-end 0))
122 (mailcap-look-at-mtext))))
124 (cons (intern token) value)
126 (list (intern token))
133 (defun mailcap-look-at-entry ()
134 (let ((type (mailcap-look-at-type-field)))
135 (if (and type (mailcap-skip-field-separator))
136 (let ((view (mailcap-look-at-mtext))
139 (while (and (mailcap-skip-field-separator)
140 (setq field (mailcap-look-at-field))
142 (setq fields (cons field fields))
145 (list (cons 'view view))
152 (defun mailcap-parse-buffer (&optional buffer order)
153 "Parse BUFFER as a mailcap, and return the result.
154 If optional argument ORDER is a function, result is sorted by it.
155 If optional argument ORDER is not specified, result is sorted original
156 order. Otherwise result is not sorted."
160 (goto-char (point-min))
163 (while (mailcap-skip-comment))
164 (setq entry (mailcap-look-at-entry))
166 (setq entries (cons entry entries))
169 (cond ((functionp order) (sort entries order))
170 ((null order) (nreverse entries))
175 (defcustom mailcap-file "~/.mailcap"
176 "*File name of user's mailcap file."
180 (defun mailcap-parse-file (&optional filename order)
181 "Parse FILENAME as a mailcap, and return the result.
182 If optional argument ORDER is a function, result is sorted by it.
183 If optional argument ORDER is not specified, result is sorted original
184 order. Otherwise result is not sorted."
186 (setq filename mailcap-file))
188 (insert-file-contents filename)
189 (mailcap-parse-buffer (current-buffer) order)
192 (defun mailcap-format-command (mtext situation)
193 "Return formated command string from MTEXT and SITUATION.
195 MTEXT is a command text of mailcap specification, such as
198 SITUATION is an association-list about information of entity. Its key
201 'type primary media-type
202 'subtype media-subtype
204 STRING parameter of Content-Type field"
210 (let ((chr (aref mtext i)))
215 (let ((file (cdr (assq 'filename situation))))
217 (error "'filename is not specified in situation.")
218 (setq dest (concat dest
219 (substring mtext p (1- i))
225 (let ((type (or (mime-type/subtype-string
226 (cdr (assq 'type situation))
227 (cdr (assq 'subtype situation)))
229 (setq dest (concat dest
230 (substring mtext p (1- i))
237 (if (not (string-match "}" mtext i))
238 (error "parse error!!!")
239 (let* ((me (match-end 0))
240 (attribute (substring mtext i (1- me)))
241 (parameter (cdr (assoc attribute situation))))
243 (error "\"%s\" is not specified in situation."
245 (setq dest (concat dest
246 (substring mtext p (- i 2))
252 (t (error "Invalid sequence `%%%c'." chr))
255 (setq dest (concat dest (substring mtext p i))
261 (concat dest (substring mtext p))
270 ;;; mailcap.el ends here