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 ()
104 (while (cond ((mailcap-look-at-qchar)
107 (buffer-substring p0 (- (point) 2))
108 (char-to-string (char-before (point)))
112 ((mailcap-look-at-schar)
114 (concat dest (buffer-substring p0 (point)))
121 (defsubst mailcap-look-at-field ()
122 (let ((token (mailcap-look-at-token)))
124 (if (looking-at "[ \t]*=[ \t]*")
126 (goto-char (match-end 0))
127 (mailcap-look-at-mtext))))
129 (cons (intern token) value)
131 (list (intern token))
138 (defun mailcap-look-at-entry ()
139 (let ((type (mailcap-look-at-type-field)))
140 (if (and type (mailcap-skip-field-separator))
141 (let ((view (mailcap-look-at-mtext))
144 (while (and (mailcap-skip-field-separator)
145 (setq field (mailcap-look-at-field))
147 (setq fields (cons field fields))
150 (list (cons 'view view))
157 (defun mailcap-parse-buffer (&optional buffer order)
158 "Parse BUFFER as a mailcap, and return the result.
159 If optional argument ORDER is a function, result is sorted by it.
160 If optional argument ORDER is not specified, result is sorted original
161 order. Otherwise result is not sorted."
165 (goto-char (point-min))
168 (while (mailcap-skip-comment))
169 (setq entry (mailcap-look-at-entry))
171 (setq entries (cons entry entries))
174 (cond ((functionp order) (sort entries order))
175 ((null order) (nreverse entries))
179 (defvar mailcap-file "~/.mailcap"
180 "*File name of user's mailcap file.")
182 (defun mailcap-parse-file (&optional filename order)
183 "Parse FILENAME as a mailcap, and return the result.
184 If optional argument ORDER is a function, result is sorted by it.
185 If optional argument ORDER is not specified, result is sorted original
186 order. Otherwise result is not sorted."
188 (setq filename mailcap-file))
190 (insert-file-contents filename)
191 (mailcap-parse-buffer (current-buffer) order)
200 ;;; mailcap.el ends here