mailcap.el 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. ;;; mailcap.el --- MIME media types configuration -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
  3. ;; Author: William M. Perry <wmperry@aventail.com>
  4. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  5. ;; Keywords: news, mail, multimedia
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Provides configuration of MIME media types from directly from Lisp
  19. ;; and via the usual mailcap mechanism (RFC 1524). Deals with
  20. ;; mime.types similarly.
  21. ;;; Code:
  22. (autoload 'mail-header-parse-content-type "mail-parse")
  23. (defgroup mailcap nil
  24. "Definition of viewers for MIME types."
  25. :version "21.1"
  26. :group 'mime)
  27. (defvar mailcap-parse-args-syntax-table
  28. (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
  29. (modify-syntax-entry ?' "\"" table)
  30. (modify-syntax-entry ?` "\"" table)
  31. (modify-syntax-entry ?{ "(" table)
  32. (modify-syntax-entry ?} ")" table)
  33. table)
  34. "A syntax table for parsing SGML attributes.")
  35. (defvar mailcap-print-command
  36. (mapconcat 'identity
  37. (cons (if (boundp 'lpr-command)
  38. lpr-command
  39. "lpr")
  40. (when (boundp 'lpr-switches)
  41. (if (stringp lpr-switches)
  42. (list lpr-switches)
  43. lpr-switches)))
  44. " ")
  45. "Shell command (including switches) used to print PostScript files.")
  46. (defun mailcap--get-user-mime-data (sym)
  47. (let ((val (default-value sym))
  48. res)
  49. (dolist (entry val)
  50. (push (list (cdr (assq 'viewer entry))
  51. (cdr (assq 'type entry))
  52. (cdr (assq 'test entry)))
  53. res))
  54. (nreverse res)))
  55. (defun mailcap--set-user-mime-data (sym val)
  56. (let (res)
  57. (pcase-dolist (`(,viewer ,type ,test) val)
  58. (push `((viewer . ,viewer)
  59. (type . ,type)
  60. ,@(when test `((test . ,test))))
  61. res))
  62. (set-default sym (nreverse res))))
  63. (defcustom mailcap-user-mime-data nil
  64. "A list of viewers preferred for different MIME types.
  65. The elements of the list are alists of the following structure
  66. ((viewer . VIEWER)
  67. (type . MIME-TYPE)
  68. (test . TEST))
  69. where VIEWER is either a lisp command, e.g., a major-mode, or a
  70. string containing a shell command for viewing files of the
  71. defined MIME-TYPE. In case of a shell command, %s will be
  72. replaced with the file.
  73. MIME-TYPE is a regular expression being matched against the
  74. actual MIME type. It is implicitly surrounded with ^ and $.
  75. TEST is an lisp form which is evaluated in order to test if the
  76. entry should be chosen. The `test' entry is optional.
  77. When selecting a viewer for a given MIME type, the first viewer
  78. in this list with a matching MIME-TYPE and successful TEST is
  79. selected. Only if none matches, the standard `mailcap-mime-data'
  80. is consulted."
  81. :type '(repeat
  82. (list
  83. (choice (function :tag "Function or mode")
  84. (string :tag "Shell command"))
  85. (regexp :tag "MIME Type")
  86. (sexp :tag "Test (optional)")))
  87. :get #'mailcap--get-user-mime-data
  88. :set #'mailcap--set-user-mime-data
  89. :group 'mailcap)
  90. ;; Postpone using defcustom for this as it's so big and we essentially
  91. ;; have to have two copies of the data around then. Perhaps just
  92. ;; customize the Lisp viewers and rely on the normal configuration
  93. ;; files for the rest? -- fx
  94. (defvar mailcap-mime-data
  95. `(("application"
  96. ("vnd\\.ms-excel"
  97. (viewer . "gnumeric %s")
  98. (test . (getenv "DISPLAY"))
  99. (type . "application/vnd.ms-excel"))
  100. ("octet-stream"
  101. (viewer . mailcap-save-binary-file)
  102. (non-viewer . t)
  103. (type . "application/octet-stream"))
  104. ("dvi"
  105. (viewer . "xdvi -safer %s")
  106. (test . (eq window-system 'x))
  107. ("needsx11")
  108. (type . "application/dvi")
  109. ("print" . "dvips -qRP %s"))
  110. ("dvi"
  111. (viewer . "dvitty %s")
  112. (test . (not (getenv "DISPLAY")))
  113. (type . "application/dvi")
  114. ("print" . "dvips -qRP %s"))
  115. ("emacs-lisp"
  116. (viewer . mailcap-maybe-eval)
  117. (type . "application/emacs-lisp"))
  118. ("x-emacs-lisp"
  119. (viewer . mailcap-maybe-eval)
  120. (type . "application/x-emacs-lisp"))
  121. ("x-tar"
  122. (viewer . mailcap-save-binary-file)
  123. (non-viewer . t)
  124. (type . "application/x-tar"))
  125. ("x-latex"
  126. (viewer . tex-mode)
  127. (type . "application/x-latex"))
  128. ("x-tex"
  129. (viewer . tex-mode)
  130. (type . "application/x-tex"))
  131. ("latex"
  132. (viewer . tex-mode)
  133. (type . "application/latex"))
  134. ("tex"
  135. (viewer . tex-mode)
  136. (type . "application/tex"))
  137. ("texinfo"
  138. (viewer . texinfo-mode)
  139. (type . "application/tex"))
  140. ("zip"
  141. (viewer . mailcap-save-binary-file)
  142. (non-viewer . t)
  143. (type . "application/zip")
  144. ("copiousoutput"))
  145. ("pdf"
  146. (viewer . doc-view-mode)
  147. (type . "application/pdf")
  148. (test . (eq window-system 'x)))
  149. ("pdf"
  150. (viewer . "gv -safer %s")
  151. (type . "application/pdf")
  152. (test . window-system)
  153. ("print" . ,(concat "pdf2ps %s - | " mailcap-print-command)))
  154. ("pdf"
  155. (viewer . "gpdf %s")
  156. (type . "application/pdf")
  157. ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
  158. (test . (eq window-system 'x)))
  159. ("pdf"
  160. (viewer . "xpdf %s")
  161. (type . "application/pdf")
  162. ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
  163. (test . (eq window-system 'x)))
  164. ("pdf"
  165. (viewer . ,(concat "pdftotext %s -"))
  166. (type . "application/pdf")
  167. ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
  168. ("copiousoutput"))
  169. ("postscript"
  170. (viewer . "gv -safer %s")
  171. (type . "application/postscript")
  172. (test . window-system)
  173. ("print" . ,(concat mailcap-print-command " %s"))
  174. ("needsx11"))
  175. ("postscript"
  176. (viewer . "ghostview -dSAFER %s")
  177. (type . "application/postscript")
  178. (test . (eq window-system 'x))
  179. ("print" . ,(concat mailcap-print-command " %s"))
  180. ("needsx11"))
  181. ("postscript"
  182. (viewer . "ps2ascii %s")
  183. (type . "application/postscript")
  184. (test . (not (getenv "DISPLAY")))
  185. ("print" . ,(concat mailcap-print-command " %s"))
  186. ("copiousoutput"))
  187. ("sieve"
  188. (viewer . sieve-mode)
  189. (type . "application/sieve"))
  190. ("pgp-keys"
  191. (viewer . "gpg --import --interactive --verbose")
  192. (type . "application/pgp-keys")
  193. ("needsterminal")))
  194. ("audio"
  195. ("x-mpeg"
  196. (viewer . "maplay %s")
  197. (type . "audio/x-mpeg"))
  198. (".*"
  199. (viewer . "showaudio")
  200. (type . "audio/*")))
  201. ("message"
  202. ("rfc-*822"
  203. (viewer . mm-view-message)
  204. (test . (and (featurep 'gnus)
  205. (gnus-alive-p)))
  206. (type . "message/rfc822"))
  207. ("rfc-*822"
  208. (viewer . vm-mode)
  209. (type . "message/rfc822"))
  210. ("rfc-*822"
  211. (viewer . view-mode)
  212. (type . "message/rfc822")))
  213. ("image"
  214. ("x-xwd"
  215. (viewer . "xwud -in %s")
  216. (type . "image/x-xwd")
  217. ("compose" . "xwd -frame > %s")
  218. (test . (eq window-system 'x))
  219. ("needsx11"))
  220. ("x11-dump"
  221. (viewer . "xwud -in %s")
  222. (type . "image/x-xwd")
  223. ("compose" . "xwd -frame > %s")
  224. (test . (eq window-system 'x))
  225. ("needsx11"))
  226. ("windowdump"
  227. (viewer . "xwud -in %s")
  228. (type . "image/x-xwd")
  229. ("compose" . "xwd -frame > %s")
  230. (test . (eq window-system 'x))
  231. ("needsx11"))
  232. (".*"
  233. (viewer . "display %s")
  234. (type . "image/*")
  235. (test . (eq window-system 'x))
  236. ("needsx11"))
  237. (".*"
  238. (viewer . "ee %s")
  239. (type . "image/*")
  240. (test . (eq window-system 'x))
  241. ("needsx11")))
  242. ("text"
  243. ("plain"
  244. (viewer . view-mode)
  245. (type . "text/plain"))
  246. ("plain"
  247. (viewer . fundamental-mode)
  248. (type . "text/plain"))
  249. ("enriched"
  250. (viewer . enriched-decode)
  251. (type . "text/enriched"))
  252. ("dns"
  253. (viewer . dns-mode)
  254. (type . "text/dns")))
  255. ("video"
  256. ("mpeg"
  257. (viewer . "mpeg_play %s")
  258. (type . "video/mpeg")
  259. (test . (eq window-system 'x))
  260. ("needsx11")))
  261. ("x-world"
  262. ("x-vrml"
  263. (viewer . "webspace -remote %s -URL %u")
  264. (type . "x-world/x-vrml")
  265. ("description"
  266. "VRML document")))
  267. ("archive"
  268. ("tar"
  269. (viewer . tar-mode)
  270. (type . "archive/tar"))))
  271. "The mailcap structure is an assoc list of assoc lists.
  272. 1st assoc list is keyed on the major content-type
  273. 2nd assoc list is keyed on the minor content-type (which can be a regexp)
  274. Which looks like:
  275. -----------------
  276. ((\"application\"
  277. (\"postscript\" . <info>))
  278. (\"text\"
  279. (\"plain\" . <info>)))
  280. Where <info> is another assoc list of the various information
  281. related to the mailcap RFC 1524. This is keyed on the lowercase
  282. attribute name (viewer, test, etc). This looks like:
  283. ((viewer . VIEWERINFO)
  284. (test . TESTINFO)
  285. (xxxx . \"STRING\")
  286. FLAG)
  287. Where VIEWERINFO specifies how the content-type is viewed. Can be
  288. a string, in which case it is run through a shell, with appropriate
  289. parameters, or a symbol, in which case the symbol is `funcall'ed if
  290. and only if it exists as a function, with the buffer as an argument.
  291. TESTINFO is a test for the viewer's applicability, or nil. If nil, it
  292. means the viewer is always valid. If it is a Lisp function, it is
  293. called with a list of items from any extra fields from the
  294. Content-Type header as argument to return a boolean value for the
  295. validity. Otherwise, if it is a non-function Lisp symbol or list
  296. whose car is a symbol, it is `eval'led to yield the validity. If it
  297. is a string or list of strings, it represents a shell command to run
  298. to return a true or false shell value for the validity.")
  299. (put 'mailcap-mime-data 'risky-local-variable t)
  300. (defcustom mailcap-download-directory nil
  301. "Directory to which `mailcap-save-binary-file' downloads files by default.
  302. nil means your home directory."
  303. :type '(choice (const :tag "Home directory" nil)
  304. directory)
  305. :group 'mailcap)
  306. (defvar mailcap-poor-system-types
  307. '(ms-dos windows-nt)
  308. "Systems that don't have a Unix-like directory hierarchy.")
  309. ;;;
  310. ;;; Utility functions
  311. ;;;
  312. (defun mailcap-save-binary-file ()
  313. (goto-char (point-min))
  314. (unwind-protect
  315. (let ((file (read-file-name
  316. "Filename to save as: "
  317. (or mailcap-download-directory "~/")))
  318. (require-final-newline nil))
  319. (write-region (point-min) (point-max) file))
  320. (kill-buffer (current-buffer))))
  321. (defvar mailcap-maybe-eval-warning
  322. "*** WARNING ***
  323. This MIME part contains untrusted and possibly harmful content.
  324. If you evaluate the Emacs Lisp code contained in it, a lot of nasty
  325. things can happen. Please examine the code very carefully before you
  326. instruct Emacs to evaluate it. You can browse the buffer containing
  327. the code using \\[scroll-other-window].
  328. If you are unsure what to do, please answer \"no\"."
  329. "Text of warning message displayed by `mailcap-maybe-eval'.
  330. Make sure that this text consists only of few text lines. Otherwise,
  331. Gnus might fail to display all of it.")
  332. (defun mailcap-maybe-eval ()
  333. "Maybe evaluate a buffer of Emacs Lisp code."
  334. (let ((lisp-buffer (current-buffer)))
  335. (goto-char (point-min))
  336. (when
  337. (save-window-excursion
  338. (delete-other-windows)
  339. (let ((buffer (get-buffer-create (generate-new-buffer-name
  340. "*Warning*"))))
  341. (unwind-protect
  342. (with-current-buffer buffer
  343. (insert (substitute-command-keys
  344. mailcap-maybe-eval-warning))
  345. (goto-char (point-min))
  346. (display-buffer buffer)
  347. (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
  348. (kill-buffer buffer))))
  349. (eval-buffer (current-buffer)))
  350. (when (buffer-live-p lisp-buffer)
  351. (with-current-buffer lisp-buffer
  352. (emacs-lisp-mode)))))
  353. ;;;
  354. ;;; The mailcap parser
  355. ;;;
  356. (defun mailcap-replace-regexp (regexp to-string)
  357. ;; Quiet replace-regexp.
  358. (goto-char (point-min))
  359. (while (re-search-forward regexp nil t)
  360. (replace-match to-string t nil)))
  361. (defvar mailcap-parsed-p nil)
  362. (defun mailcap-parse-mailcaps (&optional path force)
  363. "Parse out all the mailcaps specified in a path string PATH.
  364. Components of PATH are separated by the `path-separator' character
  365. appropriate for this system. If FORCE, re-parse even if already
  366. parsed. If PATH is omitted, use the value of environment variable
  367. MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
  368. /usr/local/etc/mailcap."
  369. (interactive (list nil t))
  370. (when (or (not mailcap-parsed-p)
  371. force)
  372. (cond
  373. (path nil)
  374. ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
  375. ((memq system-type mailcap-poor-system-types)
  376. (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
  377. (t (setq path
  378. ;; This is per RFC 1524, specifically
  379. ;; with /usr before /usr/local.
  380. '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
  381. "/usr/local/etc/mailcap"))))
  382. (dolist (fname (reverse
  383. (if (stringp path)
  384. (split-string path path-separator t)
  385. path)))
  386. (when (and (file-readable-p fname) (file-regular-p fname))
  387. (mailcap-parse-mailcap fname)))
  388. (setq mailcap-parsed-p t)))
  389. (defun mailcap-parse-mailcap (fname)
  390. "Parse out the mailcap file specified by FNAME."
  391. (let (major ; The major mime type (image/audio/etc)
  392. minor ; The minor mime type (gif, basic, etc)
  393. save-pos ; Misc saved positions used in parsing
  394. viewer ; How to view this mime type
  395. info ; Misc info about this mime type
  396. )
  397. (with-temp-buffer
  398. (insert-file-contents fname)
  399. (set-syntax-table mailcap-parse-args-syntax-table)
  400. (mailcap-replace-regexp "#.*" "") ; Remove all comments
  401. (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
  402. (mailcap-replace-regexp "\n+" "\n") ; And blank lines
  403. (goto-char (point-max))
  404. (skip-chars-backward " \t\n")
  405. (delete-region (point) (point-max))
  406. (while (not (bobp))
  407. (skip-chars-backward " \t\n")
  408. (beginning-of-line)
  409. (setq save-pos (point)
  410. info nil)
  411. (skip-chars-forward "^/; \t\n")
  412. (downcase-region save-pos (point))
  413. (setq major (buffer-substring save-pos (point)))
  414. (skip-chars-forward " \t")
  415. (setq minor "")
  416. (when (eq (char-after) ?/)
  417. (forward-char)
  418. (skip-chars-forward " \t")
  419. (setq save-pos (point))
  420. (skip-chars-forward "^; \t\n")
  421. (downcase-region save-pos (point))
  422. (setq minor
  423. (cond
  424. ((eq ?* (or (char-after save-pos) 0)) ".*")
  425. ((= (point) save-pos) ".*")
  426. (t (regexp-quote (buffer-substring save-pos (point)))))))
  427. (skip-chars-forward " \t")
  428. ;;; Got the major/minor chunks, now for the viewers/etc
  429. ;;; The first item _must_ be a viewer, according to the
  430. ;;; RFC for mailcap files (#1524)
  431. (setq viewer "")
  432. (when (eq (char-after) ?\;)
  433. (forward-char)
  434. (skip-chars-forward " \t")
  435. (setq save-pos (point))
  436. (skip-chars-forward "^;\n")
  437. ;; skip \;
  438. (while (eq (char-before) ?\\)
  439. (backward-delete-char 1)
  440. (forward-char)
  441. (skip-chars-forward "^;\n"))
  442. (if (eq (or (char-after save-pos) 0) ?')
  443. (setq viewer (progn
  444. (narrow-to-region (1+ save-pos) (point))
  445. (goto-char (point-min))
  446. (prog1
  447. (read (current-buffer))
  448. (goto-char (point-max))
  449. (widen))))
  450. (setq viewer (buffer-substring save-pos (point)))))
  451. (setq save-pos (point))
  452. (end-of-line)
  453. (unless (equal viewer "")
  454. (setq info (nconc (list (cons 'viewer viewer)
  455. (cons 'type (concat major "/"
  456. (if (string= minor ".*")
  457. "*" minor))))
  458. (mailcap-parse-mailcap-extras save-pos (point))))
  459. (mailcap-mailcap-entry-passes-test info)
  460. (mailcap-add-mailcap-entry major minor info))
  461. (beginning-of-line)))))
  462. (defun mailcap-parse-mailcap-extras (st nd)
  463. "Grab all the extra stuff from a mailcap entry."
  464. (let (
  465. name ; From name=
  466. value ; its value
  467. results ; Assoc list of results
  468. name-pos ; Start of XXXX= position
  469. val-pos ; Start of value position
  470. done ; Found end of \'d ;s?
  471. )
  472. (save-restriction
  473. (narrow-to-region st nd)
  474. (goto-char (point-min))
  475. (skip-chars-forward " \n\t;")
  476. (while (not (eobp))
  477. (setq done nil)
  478. (setq name-pos (point))
  479. (skip-chars-forward "^ \n\t=;")
  480. (downcase-region name-pos (point))
  481. (setq name (buffer-substring name-pos (point)))
  482. (skip-chars-forward " \t\n")
  483. (if (not (eq (char-after (point)) ?=)) ; There is no value
  484. (setq value t)
  485. (skip-chars-forward " \t\n=")
  486. (setq val-pos (point))
  487. (if (memq (char-after val-pos) '(?\" ?'))
  488. (progn
  489. (setq val-pos (1+ val-pos))
  490. (condition-case nil
  491. (progn
  492. (forward-sexp 1)
  493. (backward-char 1))
  494. (error (goto-char (point-max)))))
  495. (while (not done)
  496. (skip-chars-forward "^;")
  497. (if (eq (char-after (1- (point))) ?\\ )
  498. (progn
  499. (subst-char-in-region (1- (point)) (point) ?\\ ? )
  500. (skip-chars-forward ";"))
  501. (setq done t))))
  502. (setq value (buffer-substring val-pos (point))))
  503. ;; `test' as symbol, others like "copiousoutput" and "needsx11" as
  504. ;; strings
  505. (push (cons (if (string-equal name "test") 'test name) value) results)
  506. (skip-chars-forward " \";\n\t"))
  507. results)))
  508. (defun mailcap-mailcap-entry-passes-test (info)
  509. "Replace the test clause of INFO itself with a boolean for some cases.
  510. This function supports only `test -n $DISPLAY' and `test -z $DISPLAY',
  511. replaces them with t or nil. As for others or if INFO has a interactive
  512. spec (needsterm, needsterminal, or needsx11) but DISPLAY is not set,
  513. the test clause will be unchanged."
  514. (let ((test (assq 'test info)) ; The test clause
  515. status)
  516. (setq status (and test (split-string (cdr test) " ")))
  517. (if (and (or (assoc "needsterm" info)
  518. (assoc "needsterminal" info)
  519. (assoc "needsx11" info))
  520. (not (getenv "DISPLAY")))
  521. (setq status nil)
  522. (cond
  523. ((and (equal (nth 0 status) "test")
  524. (equal (nth 1 status) "-n")
  525. (or (equal (nth 2 status) "$DISPLAY")
  526. (equal (nth 2 status) "\"$DISPLAY\"")))
  527. (setq status (if (getenv "DISPLAY") t nil)))
  528. ((and (equal (nth 0 status) "test")
  529. (equal (nth 1 status) "-z")
  530. (or (equal (nth 2 status) "$DISPLAY")
  531. (equal (nth 2 status) "\"$DISPLAY\"")))
  532. (setq status (if (getenv "DISPLAY") nil t)))
  533. (test nil)
  534. (t nil)))
  535. (and test (listp test) (setcdr test status))))
  536. ;;;
  537. ;;; The action routines.
  538. ;;;
  539. (defun mailcap-possible-viewers (major minor)
  540. "Return a list of possible viewers from MAJOR for minor type MINOR."
  541. (let ((exact '())
  542. (wildcard '()))
  543. (pcase-dolist (`(,type . ,attrs) major)
  544. (cond
  545. ((equal type minor)
  546. (push attrs exact))
  547. ((and minor (string-match (concat "^" type "$") minor))
  548. (push attrs wildcard))))
  549. (nconc exact wildcard)))
  550. (defun mailcap-unescape-mime-test (test type-info)
  551. (let (save-pos save-chr subst)
  552. (cond
  553. ((symbolp test) test)
  554. ((and (listp test) (symbolp (car test))) test)
  555. ((or (stringp test)
  556. (and (listp test) (stringp (car test))
  557. (setq test (mapconcat 'identity test " "))))
  558. (with-temp-buffer
  559. (insert test)
  560. (goto-char (point-min))
  561. (while (not (eobp))
  562. (skip-chars-forward "^%")
  563. (if (/= (- (point)
  564. (progn (skip-chars-backward "\\\\")
  565. (point)))
  566. 0) ; It is an escaped %
  567. (progn
  568. (delete-char 1)
  569. (skip-chars-forward "%."))
  570. (setq save-pos (point))
  571. (skip-chars-forward "%")
  572. (setq save-chr (char-after (point)))
  573. ;; Escapes:
  574. ;; %s: name of a file for the body data
  575. ;; %t: content-type
  576. ;; %{<parameter name}: value of parameter in mailcap entry
  577. ;; %n: number of sub-parts for multipart content-type
  578. ;; %F: a set of content-type/filename pairs for multiparts
  579. (cond
  580. ((null save-chr) nil)
  581. ((= save-chr ?t)
  582. (delete-region save-pos (progn (forward-char 1) (point)))
  583. (insert (or (cdr (assq 'type type-info)) "\"\"")))
  584. ((memq save-chr '(?M ?n ?F))
  585. (delete-region save-pos (progn (forward-char 1) (point)))
  586. (insert "\"\""))
  587. ((= save-chr ?{)
  588. (forward-char 1)
  589. (skip-chars-forward "^}")
  590. (downcase-region (+ 2 save-pos) (point))
  591. (setq subst (buffer-substring (+ 2 save-pos) (point)))
  592. (delete-region save-pos (1+ (point)))
  593. (insert (or (cdr (assoc subst type-info)) "\"\"")))
  594. (t nil))))
  595. (buffer-string)))
  596. (t (error "Bad value to mailcap-unescape-mime-test: %s" test)))))
  597. (defvar mailcap-viewer-test-cache nil)
  598. (defun mailcap-viewer-passes-test (viewer-info type-info)
  599. "Return non-nil if viewer specified by VIEWER-INFO passes its test clause.
  600. Also return non-nil if it has no test clause. TYPE-INFO is an argument
  601. to supply to the test."
  602. (let* ((test-info (assq 'test viewer-info))
  603. (test (cdr test-info))
  604. (otest test)
  605. (viewer (cdr (assq 'viewer viewer-info)))
  606. (default-directory (expand-file-name "~/"))
  607. status cache result)
  608. (cond ((not (or (stringp viewer) (fboundp viewer)))
  609. nil) ; Non-existent Lisp function
  610. ((setq cache (assoc test mailcap-viewer-test-cache))
  611. (cadr cache))
  612. ((not test-info) t) ; No test clause
  613. (t
  614. (setq
  615. result
  616. (cond
  617. ((not test) nil) ; Already failed test
  618. ((eq test t) t) ; Already passed test
  619. ((functionp test) ; Lisp function as test
  620. (funcall test type-info))
  621. ((and (symbolp test) ; Lisp variable as test
  622. (boundp test))
  623. (symbol-value test))
  624. ((and (listp test) ; List to be eval'd
  625. (symbolp (car test)))
  626. (eval test))
  627. (t
  628. (setq test (mailcap-unescape-mime-test test type-info)
  629. test (list shell-file-name nil nil nil
  630. shell-command-switch test)
  631. status (apply 'call-process test))
  632. (eq 0 status))))
  633. (push (list otest result) mailcap-viewer-test-cache)
  634. result))))
  635. (defun mailcap-add-mailcap-entry (major minor info)
  636. (let ((old-major (assoc major mailcap-mime-data)))
  637. (if (null old-major) ; New major area
  638. (push (cons major (list (cons minor info))) mailcap-mime-data)
  639. (let ((cur-minor (assoc minor old-major)))
  640. (cond
  641. ((or (null cur-minor) ; New minor area, or
  642. (assq 'test info)) ; Has a test, insert at beginning
  643. (setcdr old-major (cons (cons minor info) (cdr old-major))))
  644. ((and (not (assq 'test info)) ; No test info, replace completely
  645. (not (assq 'test cur-minor))
  646. (equal (assq 'viewer info) ; Keep alternative viewer
  647. (assq 'viewer cur-minor)))
  648. (setcdr cur-minor info))
  649. (t
  650. (setcdr old-major (cons (cons minor info) (cdr old-major))))))
  651. )))
  652. (defun mailcap-add (type viewer &optional test)
  653. "Add VIEWER as a handler for TYPE.
  654. If TEST is not given, it defaults to t."
  655. (let ((tl (split-string type "/")))
  656. (when (or (not (car tl))
  657. (not (cadr tl)))
  658. (error "%s is not a valid MIME type" type))
  659. (mailcap-add-mailcap-entry
  660. (car tl) (cadr tl)
  661. `((viewer . ,viewer)
  662. (test . ,(if test test t))
  663. (type . ,type)))))
  664. ;;;
  665. ;;; The main whabbo
  666. ;;;
  667. (defun mailcap-viewer-lessp (x y)
  668. "Return t if viewer X is more desirable than viewer Y."
  669. (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
  670. (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
  671. (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
  672. (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
  673. (cond
  674. ((and x-wild (not y-wild))
  675. nil)
  676. ((and (not x-wild) y-wild)
  677. t)
  678. ((and (not y-lisp) x-lisp)
  679. t)
  680. (t nil))))
  681. (defun mailcap-select-preferred-viewer (type-info)
  682. "Return an applicable viewer entry from `mailcap-user-mime-data'."
  683. (let ((info (mapcar (lambda (a) (cons (symbol-name (car a))
  684. (cdr a)))
  685. (cdr type-info)))
  686. viewer)
  687. (dolist (entry mailcap-user-mime-data)
  688. (when (and (null viewer)
  689. (string-match (concat "^" (cdr (assq 'type entry)) "$")
  690. (car type-info))
  691. (mailcap-viewer-passes-test entry info))
  692. (setq viewer entry)))
  693. viewer))
  694. (defun mailcap-mime-info (string &optional request no-decode)
  695. "Get the MIME viewer command for STRING, return nil if none found.
  696. Expects a complete content-type header line as its argument.
  697. Second argument REQUEST specifies what information to return. If it is
  698. nil or the empty string, the viewer (second field of the mailcap
  699. entry) will be returned. If it is a string, then the mailcap field
  700. corresponding to that string will be returned (print, description,
  701. whatever). If a number, then all the information for this specific
  702. viewer is returned. If `all', then all possible viewers for
  703. this type is returned.
  704. If NO-DECODE is non-nil, don't decode STRING."
  705. ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
  706. ;; `mail-parse.el'
  707. (let (
  708. major ; Major encoding (text, etc)
  709. minor ; Minor encoding (html, etc)
  710. info ; Other info
  711. major-info ; (assoc major mailcap-mime-data)
  712. viewers ; Possible viewers
  713. passed ; Viewers that passed the test
  714. viewer ; The one and only viewer
  715. ctl)
  716. (save-excursion
  717. (setq ctl
  718. (if no-decode
  719. (list (or string "text/plain"))
  720. (mail-header-parse-content-type (or string "text/plain"))))
  721. ;; Check if there's a user-defined viewer from `mailcap-user-mime-data'.
  722. (setq viewer (mailcap-select-preferred-viewer ctl))
  723. (if viewer
  724. (setq passed (list viewer))
  725. ;; None found, so heuristically select some applicable viewer
  726. ;; from `mailcap-mime-data'.
  727. (setq major (split-string (car ctl) "/"))
  728. (setq minor (cadr major)
  729. major (car major))
  730. (when (setq major-info (cdr (assoc major mailcap-mime-data)))
  731. (when (setq viewers (mailcap-possible-viewers major-info minor))
  732. (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
  733. (cdr a)))
  734. (cdr ctl)))
  735. (dolist (entry viewers)
  736. (when (mailcap-viewer-passes-test entry info)
  737. (push entry passed)))
  738. (setq passed (sort passed 'mailcap-viewer-lessp))
  739. (setq viewer (car passed))))
  740. (when (and (stringp (cdr (assq 'viewer viewer)))
  741. passed)
  742. (setq viewer (car passed))))
  743. (cond
  744. ((and (null viewer) (not (equal major "default")) request)
  745. (mailcap-mime-info "default" request no-decode))
  746. ((or (null request) (equal request ""))
  747. (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
  748. ((stringp request)
  749. (mailcap-unescape-mime-test
  750. (cdr-safe (assoc request viewer)) info))
  751. ((eq request 'all)
  752. passed)
  753. (t
  754. ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
  755. (setq viewer (copy-sequence viewer))
  756. (let ((view (assq 'viewer viewer))
  757. (test (assq 'test viewer)))
  758. (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
  759. (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
  760. viewer)))))
  761. ;;;
  762. ;;; Experimental MIME-types parsing
  763. ;;;
  764. (defvar mailcap-mime-extensions
  765. '(("" . "text/plain")
  766. (".1" . "text/plain") ;; Manual pages
  767. (".3" . "text/plain")
  768. (".8" . "text/plain")
  769. (".abs" . "audio/x-mpeg")
  770. (".aif" . "audio/aiff")
  771. (".aifc" . "audio/aiff")
  772. (".aiff" . "audio/aiff")
  773. (".ano" . "application/x-annotator")
  774. (".au" . "audio/ulaw")
  775. (".avi" . "video/x-msvideo")
  776. (".bcpio" . "application/x-bcpio")
  777. (".bin" . "application/octet-stream")
  778. (".cdf" . "application/x-netcdr")
  779. (".cpio" . "application/x-cpio")
  780. (".csh" . "application/x-csh")
  781. (".css" . "text/css")
  782. (".dvi" . "application/x-dvi")
  783. (".diff" . "text/x-patch")
  784. (".dpatch". "text/x-patch")
  785. (".el" . "application/emacs-lisp")
  786. (".eps" . "application/postscript")
  787. (".etx" . "text/x-setext")
  788. (".exe" . "application/octet-stream")
  789. (".fax" . "image/x-fax")
  790. (".gif" . "image/gif")
  791. (".hdf" . "application/x-hdf")
  792. (".hqx" . "application/mac-binhex40")
  793. (".htm" . "text/html")
  794. (".html" . "text/html")
  795. (".icon" . "image/x-icon")
  796. (".ief" . "image/ief")
  797. (".jpg" . "image/jpeg")
  798. (".macp" . "image/x-macpaint")
  799. (".man" . "application/x-troff-man")
  800. (".me" . "application/x-troff-me")
  801. (".mif" . "application/mif")
  802. (".mov" . "video/quicktime")
  803. (".movie" . "video/x-sgi-movie")
  804. (".mp2" . "audio/x-mpeg")
  805. (".mp3" . "audio/x-mpeg")
  806. (".mp2a" . "audio/x-mpeg2")
  807. (".mpa" . "audio/x-mpeg")
  808. (".mpa2" . "audio/x-mpeg2")
  809. (".mpe" . "video/mpeg")
  810. (".mpeg" . "video/mpeg")
  811. (".mpega" . "audio/x-mpeg")
  812. (".mpegv" . "video/mpeg")
  813. (".mpg" . "video/mpeg")
  814. (".mpv" . "video/mpeg")
  815. (".ms" . "application/x-troff-ms")
  816. (".nc" . "application/x-netcdf")
  817. (".nc" . "application/x-netcdf")
  818. (".oda" . "application/oda")
  819. (".patch" . "text/x-patch")
  820. (".pbm" . "image/x-portable-bitmap")
  821. (".pdf" . "application/pdf")
  822. (".pgm" . "image/portable-graymap")
  823. (".pict" . "image/pict")
  824. (".png" . "image/png")
  825. (".pnm" . "image/x-portable-anymap")
  826. (".pod" . "text/plain")
  827. (".ppm" . "image/portable-pixmap")
  828. (".ps" . "application/postscript")
  829. (".qt" . "video/quicktime")
  830. (".ras" . "image/x-raster")
  831. (".rgb" . "image/x-rgb")
  832. (".rtf" . "application/rtf")
  833. (".rtx" . "text/richtext")
  834. (".sh" . "application/x-sh")
  835. (".sit" . "application/x-stuffit")
  836. (".siv" . "application/sieve")
  837. (".snd" . "audio/basic")
  838. (".soa" . "text/dns")
  839. (".src" . "application/x-wais-source")
  840. (".tar" . "archive/tar")
  841. (".tcl" . "application/x-tcl")
  842. (".tex" . "application/x-tex")
  843. (".texi" . "application/texinfo")
  844. (".tga" . "image/x-targa")
  845. (".tif" . "image/tiff")
  846. (".tiff" . "image/tiff")
  847. (".tr" . "application/x-troff")
  848. (".troff" . "application/x-troff")
  849. (".tsv" . "text/tab-separated-values")
  850. (".txt" . "text/plain")
  851. (".vbs" . "video/mpeg")
  852. (".vox" . "audio/basic")
  853. (".vrml" . "x-world/x-vrml")
  854. (".wav" . "audio/x-wav")
  855. (".xls" . "application/vnd.ms-excel")
  856. (".wrl" . "x-world/x-vrml")
  857. (".xbm" . "image/xbm")
  858. (".xpm" . "image/xpm")
  859. (".xwd" . "image/windowdump")
  860. (".zip" . "application/zip")
  861. (".ai" . "application/postscript")
  862. (".jpe" . "image/jpeg")
  863. (".jpeg" . "image/jpeg")
  864. (".org" . "text/x-org"))
  865. "An alist of file extensions and corresponding MIME content-types.
  866. This exists for you to customize the information in Lisp. It is
  867. merged with values from mailcap files by `mailcap-parse-mimetypes'.")
  868. (defvar mailcap-mimetypes-parsed-p nil)
  869. (defun mailcap-parse-mimetypes (&optional path force)
  870. "Parse out all the mimetypes specified in a Unix-style path string PATH.
  871. Components of PATH are separated by the `path-separator' character
  872. appropriate for this system. If PATH is omitted, use the value of
  873. environment variable MIMETYPES if set; otherwise use a default path.
  874. If FORCE, re-parse even if already parsed."
  875. (interactive (list nil t))
  876. (when (or (not mailcap-mimetypes-parsed-p)
  877. force)
  878. (cond
  879. (path nil)
  880. ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
  881. ((memq system-type mailcap-poor-system-types)
  882. (setq path '("~/mime.typ" "~/etc/mime.typ")))
  883. (t (setq path
  884. ;; mime.types seems to be the normal name, definitely so
  885. ;; on current GNUish systems. The search order follows
  886. ;; that for mailcap.
  887. '("~/.mime.types"
  888. "/etc/mime.types"
  889. "/usr/etc/mime.types"
  890. "/usr/local/etc/mime.types"
  891. "/usr/local/www/conf/mime.types"
  892. "~/.mime-types"
  893. "/etc/mime-types"
  894. "/usr/etc/mime-types"
  895. "/usr/local/etc/mime-types"
  896. "/usr/local/www/conf/mime-types"))))
  897. (dolist (fname (reverse (if (stringp path)
  898. (split-string path path-separator t)
  899. path)))
  900. (when (file-readable-p fname)
  901. (mailcap-parse-mimetype-file fname)))
  902. (setq mailcap-mimetypes-parsed-p t)))
  903. (defun mailcap-parse-mimetype-file (fname)
  904. "Parse out a mime-types file FNAME."
  905. (let (type ; The MIME type for this line
  906. extns ; The extensions for this line
  907. save-pos ; Misc. saved buffer positions
  908. save-extn)
  909. (with-temp-buffer
  910. (insert-file-contents fname)
  911. (mailcap-replace-regexp "#.*" "")
  912. (mailcap-replace-regexp "\n+" "\n")
  913. (mailcap-replace-regexp "[ \t]+$" "")
  914. (goto-char (point-max))
  915. (skip-chars-backward " \t\n")
  916. (delete-region (point) (point-max))
  917. (goto-char (point-min))
  918. (while (not (eobp))
  919. (skip-chars-forward " \t\n")
  920. (setq save-pos (point))
  921. (skip-chars-forward "^ \t\n")
  922. (downcase-region save-pos (point))
  923. (setq type (buffer-substring save-pos (point)))
  924. (while (not (eolp))
  925. (skip-chars-forward " \t")
  926. (setq save-pos (point))
  927. (skip-chars-forward "^ \t\n")
  928. (setq save-extn (buffer-substring save-pos (point)))
  929. (push (cons (if (= (string-to-char save-extn) ?.)
  930. save-extn (concat "." save-extn))
  931. type)
  932. extns))
  933. (setq mailcap-mime-extensions (append extns mailcap-mime-extensions)
  934. extns nil)))))
  935. (defun mailcap-extension-to-mime (extn)
  936. "Return the MIME content type of the file extensions EXTN."
  937. (mailcap-parse-mimetypes)
  938. (if (and (stringp extn)
  939. (not (eq (string-to-char extn) ?.)))
  940. (setq extn (concat "." extn)))
  941. (cdr (assoc (downcase extn) mailcap-mime-extensions)))
  942. (defun mailcap-mime-types ()
  943. "Return a list of MIME media types."
  944. (mailcap-parse-mimetypes)
  945. (delete-dups
  946. (nconc
  947. (mapcar 'cdr mailcap-mime-extensions)
  948. (apply
  949. 'nconc
  950. (mapcar
  951. (lambda (l)
  952. (delq nil
  953. (mapcar
  954. (lambda (m)
  955. (let ((type (cdr (assq 'type (cdr m)))))
  956. (if (equal (cadr (split-string type "/"))
  957. "*")
  958. nil
  959. type)))
  960. (cdr l))))
  961. mailcap-mime-data)))))
  962. ;;;
  963. ;;; Useful supplementary functions
  964. ;;;
  965. (defun mailcap-file-default-commands (files)
  966. "Return a list of default commands for FILES."
  967. (mailcap-parse-mailcaps)
  968. (mailcap-parse-mimetypes)
  969. (let* ((all-mime-type
  970. ;; All unique MIME types from file extensions
  971. (delete-dups
  972. (mapcar (lambda (file)
  973. (mailcap-extension-to-mime
  974. (file-name-extension file t)))
  975. files)))
  976. (all-mime-info
  977. ;; All MIME info lists
  978. (delete-dups
  979. (mapcar (lambda (mime-type)
  980. (mailcap-mime-info mime-type 'all))
  981. all-mime-type)))
  982. (common-mime-info
  983. ;; Intersection of mime-infos from different mime-types;
  984. ;; or just the first MIME info for a single MIME type
  985. (if (cdr all-mime-info)
  986. (delq nil (mapcar (lambda (mi1)
  987. (unless (memq nil (mapcar
  988. (lambda (mi2)
  989. (member mi1 mi2))
  990. (cdr all-mime-info)))
  991. mi1))
  992. (car all-mime-info)))
  993. (car all-mime-info)))
  994. (commands
  995. ;; Command strings from `viewer' field of the MIME info
  996. (delete-dups
  997. (delq nil (mapcar
  998. (lambda (mime-info)
  999. (let ((command (cdr (assoc 'viewer mime-info))))
  1000. (if (stringp command)
  1001. (replace-regexp-in-string
  1002. ;; Replace mailcap's `%s' placeholder
  1003. ;; with dired's `?' placeholder
  1004. "%s" "?"
  1005. (replace-regexp-in-string
  1006. ;; Remove the final filename placeholder
  1007. "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" ""
  1008. command nil t)
  1009. nil t))))
  1010. common-mime-info)))))
  1011. commands))
  1012. (defun mailcap-view-mime (type)
  1013. "View the data in the current buffer that has MIME type TYPE.
  1014. `mailcap-mime-data' determines the method to use."
  1015. (let ((method (mailcap-mime-info type)))
  1016. (if (stringp method)
  1017. (shell-command-on-region (point-min) (point-max)
  1018. ;; Use stdin as the "%s".
  1019. (format method "-")
  1020. (current-buffer)
  1021. t)
  1022. (funcall method))))
  1023. (provide 'mailcap)
  1024. ;;; mailcap.el ends here