File: mime-conf.el

package info (click to toggle)
flim 1%3A1.14.9%2B0.20201117-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 864 kB
  • sloc: lisp: 7,300; makefile: 86; sh: 86
file content (256 lines) | stat: -rw-r--r-- 6,815 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
;;; mime-conf.el --- mailcap parser and MIME playback configuration  -*- lexical-binding: t -*-

;; Copyright (C) 1997,1998,1999,2000,2004 Free Software Foundation, Inc.

;; Author: MORIOKA Tomohiko <tomo@m17n.org>
;; Created: 1997-06-27
;; Original: 1997-06-27 mailcap.el by MORIOKA Tomohiko
;;	Renamed: 2000-11-24 to mime-conf.el by MORIOKA Tomohiko
;; Keywords: mailcap, setting, configuration, MIME, multimedia

;; This file is part of FLIM (Faithful Library about Internet Message).

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.

;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Code:

(require 'mime-def)


;;; @ comment
;;;

(defsubst mime-mailcap-skip-comment ()
  (let ((chr (following-char)))
    (when (and chr
	       (or (= chr ?\n)
		   (= chr ?#)))
      (forward-line)
      t)))


;;; @ token
;;;

(defsubst mime-mailcap-look-at-token ()
  (if (looking-at mime-token-regexp)
      (let ((beg (match-beginning 0))
	    (end (match-end 0)))
	(goto-char end)
	(buffer-substring beg end))))


;;; @ typefield
;;;

(defsubst mime-mailcap-look-at-type-field ()
  (let ((type (mime-mailcap-look-at-token)))
    (if type
	(if (eq (following-char) ?/)
	    (progn
	      (forward-char)
	      (let ((subtype (mime-mailcap-look-at-token)))
		(if subtype
		    (cons (cons 'type (intern type))
			  (unless (string= subtype "*")
			    (list (cons 'subtype (intern subtype))))))))
	  (list (cons 'type (intern type)))))))


;;; @ field separator
;;;

(defsubst mime-mailcap-skip-field-separator ()
  (let ((ret (looking-at "\\([ \t]\\|\\\\\n\\)*;\\([ \t]\\|\\\\\n\\)*")))
    (when ret
      (goto-char (match-end 0))
      t)))


;;; @ mtext
;;;

(defsubst mime-mailcap-look-at-schar ()
  (let ((chr (following-char)))
    (if (and chr
	     (>= chr ?\s)
	     (/= chr ?\;)
	     (/= chr ?\\))
	(prog1
	    chr
	  (forward-char)))))

(defsubst mime-mailcap-look-at-qchar ()
  (when (eq (following-char) ?\\)
    (prog2
	(forward-char)
	(following-char)
      (forward-char))))

(defsubst mime-mailcap-look-at-mtext ()
  (let ((beg (point)))
    (while (or (mime-mailcap-look-at-qchar)
	       (mime-mailcap-look-at-schar)))
    (buffer-substring beg (point))))


;;; @ field
;;;

(defsubst mime-mailcap-look-at-field ()
  (let ((token (mime-mailcap-look-at-token)))
    (if token
	(if (looking-at "[ \t]*=[ \t]*")
	    (let ((value (progn
			   (goto-char (match-end 0))
			   (mime-mailcap-look-at-mtext))))
	      (if value
		  (cons (intern token) value)))
	  (list (intern token))))))


;;; @ mailcap entry
;;;

(defun mime-mailcap-look-at-entry ()
  (let ((type (mime-mailcap-look-at-type-field)))
    (if (and type (mime-mailcap-skip-field-separator))
	(let ((view (mime-mailcap-look-at-mtext))
	      fields field)
	  (when view
	    (while (and (mime-mailcap-skip-field-separator)
			(setq field (mime-mailcap-look-at-field)))
	      (setq fields (cons field fields)))
	    (nconc type
		   (list (cons 'view view))
		   fields))))))


;;; @ main
;;;

;;;###autoload
(defun mime-parse-mailcap-buffer (&optional buffer order)
  "Parse BUFFER as a mailcap, and return the result.
If optional argument ORDER is a function, result is sorted by it.
If optional argument ORDER is not specified, result is sorted original
order.  Otherwise result is not sorted."
  (save-excursion
    (if buffer
	(set-buffer buffer))
    (goto-char (point-min))
    (let (entries entry)
      (while (progn
	       (while (mime-mailcap-skip-comment))
	       (setq entry (mime-mailcap-look-at-entry)))
	(setq entries (cons entry entries))
	(forward-line))
      (cond ((functionp order) (sort entries order))
	    ((null order) (nreverse entries))
	    (t entries)))))


;;;###autoload
(defvar mime-mailcap-file "~/.mailcap"
  "*File name of user's mailcap file.")

;;;###autoload
(defun mime-parse-mailcap-file (&optional filename order)
  "Parse FILENAME as a mailcap, and return the result.
If optional argument ORDER is a function, result is sorted by it.
If optional argument ORDER is not specified, result is sorted original
order.  Otherwise result is not sorted."
  (or filename
      (setq filename mime-mailcap-file))
  (with-temp-buffer
    (insert-file-contents filename)
    (mime-parse-mailcap-buffer (current-buffer) order)))


;;;###autoload
(defun mime-format-mailcap-command (mtext situation)
  "Return formated command string from MTEXT and SITUATION.

MTEXT is a command text of mailcap specification, such as
view-command.

SITUATION is an association-list about information of entity.  Its key
may be:

	'type		primary media-type
	'subtype	media-subtype
	'filename	filename
	STRING		parameter of Content-Type field"
  (let ((i 0)
	(len (length mtext))
	(p 0)
	dest)
    (while (< i len)
      (let ((chr (aref mtext i)))
	(cond
	 ((eq chr ?%)
	  (setq i (1+ i)
		chr (aref mtext i))
	  (cond
	   ((eq chr ?s)
	    (let ((file (cdr (assq 'filename situation))))
	      (if file
		  (setq file (shell-quote-argument file))
		(error "'filename is not specified in situation."))
	      (setq dest (concat dest
				 (substring mtext p (1- i))
				 ;; if the situation (wrongly) quotes
				 ;; the argument, fix it.
				 (if (eq ?' (aref mtext (- i 2)))
				     (concat "'" file "'")
				   file))
		    i (1+ i)
		    p i)))
	   ((eq chr ?t)
	    (let ((type (or (mime-type/subtype-string
			     (cdr (assq 'type situation))
			     (cdr (assq 'subtype situation)))
			    "text/plain")))
	      (setq dest (concat dest (substring mtext p (1- i)) type)
		    i (1+ i)
		    p i)))
	   ((eq chr ?\{)
	    (setq i (1+ i))
	    (unless (string-match "}" mtext i)
	      (error "parse error!!!"))
	    (let* ((me (match-end 0))
		   (attribute (substring mtext i (1- me)))
		   (parameter (cdr (assoc attribute situation))))
	      (unless parameter
		(error "\"%s\" is not specified in situation." attribute))
	      (setq dest (concat dest (substring mtext p (- i 2)) parameter)
		    i me
		    p i)))
	   (t (error "Invalid sequence `%%%c'." chr))))
	 ((eq chr ?\\)
	  (setq dest (concat dest (substring mtext p i))
		p (1+ i)
		i (+ i 2)))
	 (t (setq i (1+ i))))))
    (concat dest (substring mtext p))))


;;; @ end
;;;

(provide 'mime-conf)

;;; mime-conf.el ends here