Do some cleanup in mailcap.el

* lisp/net/mailcap.el: Use lexical-binding.
(mailcap--set-user-mime-data, mailcap-possible-viewers): Use pcase
destructuring.
(mailcap-mime-data): Remove some entries for ancient functions.
(mailcap-parse-mailcaps, mailcap-mime-info): Nix single-branch ifs.
(mailcap-parse-mimetype-file): Just use append.
(mailcap-command-p): Remove unused function.
This commit is contained in:
Mark Oteiza 2017-08-07 21:50:11 -04:00
parent 919ac3ae16
commit c3445aed51

View file

@ -1,4 +1,4 @@
;;; mailcap.el --- MIME media types configuration ;;; mailcap.el --- MIME media types configuration -*- lexical-binding: t -*-
;; Copyright (C) 1998-2017 Free Software Foundation, Inc. ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
@ -29,7 +29,6 @@
;;; Code: ;;; Code:
(eval-when-compile (require 'cl-lib))
(autoload 'mail-header-parse-content-type "mail-parse") (autoload 'mail-header-parse-content-type "mail-parse")
(defgroup mailcap nil (defgroup mailcap nil
@ -70,11 +69,10 @@
(defun mailcap--set-user-mime-data (sym val) (defun mailcap--set-user-mime-data (sym val)
(let (res) (let (res)
(dolist (entry val) (pcase-dolist (`(,viewer ,type ,test) val)
(push `((viewer . ,(car entry)) (push `((viewer . ,viewer)
(type . ,(cadr entry)) (type . ,type)
,@(when (cl-caddr entry) ,@(when test `((test . ,test))))
`((test . ,(cl-caddr entry)))))
res)) res))
(set-default sym (nreverse res)))) (set-default sym (nreverse res))))
@ -121,12 +119,6 @@ is consulted."
(viewer . "gnumeric %s") (viewer . "gnumeric %s")
(test . (getenv "DISPLAY")) (test . (getenv "DISPLAY"))
(type . "application/vnd.ms-excel")) (type . "application/vnd.ms-excel"))
("x-x509-ca-cert"
(viewer . ssl-view-site-cert)
(type . "application/x-x509-ca-cert"))
("x-x509-user-cert"
(viewer . ssl-view-user-cert)
(type . "application/x-x509-user-cert"))
("octet-stream" ("octet-stream"
(viewer . mailcap-save-binary-file) (viewer . mailcap-save-binary-file)
(non-viewer . t) (non-viewer . t)
@ -172,10 +164,6 @@ is consulted."
(non-viewer . t) (non-viewer . t)
(type . "application/zip") (type . "application/zip")
("copiousoutput")) ("copiousoutput"))
("pdf"
(viewer . pdf-view-mode)
(type . "application/pdf")
(test . (eq window-system 'x)))
("pdf" ("pdf"
(viewer . doc-view-mode) (viewer . doc-view-mode)
(type . "application/pdf") (type . "application/pdf")
@ -434,9 +422,8 @@ MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
(if (stringp path) (if (stringp path)
(split-string path path-separator t) (split-string path path-separator t)
path))) path)))
(if (and (file-readable-p fname) (when (and (file-readable-p fname) (file-regular-p fname))
(file-regular-p fname)) (mailcap-parse-mailcap fname)))
(mailcap-parse-mailcap fname)))
(setq mailcap-parsed-p t))) (setq mailcap-parsed-p t)))
(defun mailcap-parse-mailcap (fname) (defun mailcap-parse-mailcap (fname)
@ -597,13 +584,12 @@ the test clause will be unchanged."
"Return a list of possible viewers from MAJOR for minor type MINOR." "Return a list of possible viewers from MAJOR for minor type MINOR."
(let ((exact '()) (let ((exact '())
(wildcard '())) (wildcard '()))
(while major (pcase-dolist (`(,type . ,attrs) major)
(cond (cond
((equal (car (car major)) minor) ((equal type minor)
(push (cdr (car major)) exact)) (push attrs exact))
((and minor (string-match (concat "^" (car (car major)) "$") minor)) ((and minor (string-match (concat "^" type "$") minor))
(push (cdr (car major)) wildcard))) (push attrs wildcard))))
(setq major (cdr major)))
(nconc exact wildcard))) (nconc exact wildcard)))
(defun mailcap-unescape-mime-test (test type-info) (defun mailcap-unescape-mime-test (test type-info)
@ -801,10 +787,9 @@ If NO-DECODE is non-nil, don't decode STRING."
(setq info (mapcar (lambda (a) (cons (symbol-name (car a)) (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
(cdr a))) (cdr a)))
(cdr ctl))) (cdr ctl)))
(while viewers (dolist (entry viewers)
(if (mailcap-viewer-passes-test (car viewers) info) (when (mailcap-viewer-passes-test entry info)
(push (car viewers) passed)) (push entry passed)))
(setq viewers (cdr viewers)))
(setq passed (sort passed 'mailcap-viewer-lessp)) (setq passed (sort passed 'mailcap-viewer-lessp))
(setq viewer (car passed)))) (setq viewer (car passed))))
(when (and (stringp (cdr (assq 'viewer viewer))) (when (and (stringp (cdr (assq 'viewer viewer)))
@ -971,8 +956,8 @@ If FORCE, re-parse even if already parsed."
(dolist (fname (reverse (if (stringp path) (dolist (fname (reverse (if (stringp path)
(split-string path path-separator t) (split-string path path-separator t)
path))) path)))
(if (and (file-readable-p fname)) (when (file-readable-p fname)
(mailcap-parse-mimetype-file fname))) (mailcap-parse-mimetype-file fname)))
(setq mailcap-mimetypes-parsed-p t))) (setq mailcap-mimetypes-parsed-p t)))
(defun mailcap-parse-mimetype-file (fname) (defun mailcap-parse-mimetype-file (fname)
@ -980,7 +965,7 @@ If FORCE, re-parse even if already parsed."
(let (type ; The MIME type for this line (let (type ; The MIME type for this line
extns ; The extensions for this line extns ; The extensions for this line
save-pos ; Misc. saved buffer positions save-pos ; Misc. saved buffer positions
) save-extn)
(with-temp-buffer (with-temp-buffer
(insert-file-contents fname) (insert-file-contents fname)
(mailcap-replace-regexp "#.*" "") (mailcap-replace-regexp "#.*" "")
@ -1000,15 +985,13 @@ If FORCE, re-parse even if already parsed."
(skip-chars-forward " \t") (skip-chars-forward " \t")
(setq save-pos (point)) (setq save-pos (point))
(skip-chars-forward "^ \t\n") (skip-chars-forward "^ \t\n")
(setq extns (cons (buffer-substring save-pos (point)) extns))) (setq save-extn (buffer-substring save-pos (point)))
(while extns (push (cons (if (= (string-to-char save-extn) ?.)
(setq mailcap-mime-extensions save-extn (concat "." save-extn))
(cons type)
(cons (if (= (string-to-char (car extns)) ?.) extns))
(car extns) (setq mailcap-mime-extensions (append extns mailcap-mime-extensions)
(concat "." (car extns))) type) extns nil)))))
mailcap-mime-extensions)
extns (cdr extns)))))))
(defun mailcap-extension-to-mime (extn) (defun mailcap-extension-to-mime (extn)
"Return the MIME content type of the file extensions EXTN." "Return the MIME content type of the file extensions EXTN."
@ -1018,9 +1001,6 @@ If FORCE, re-parse even if already parsed."
(setq extn (concat "." extn))) (setq extn (concat "." extn)))
(cdr (assoc (downcase extn) mailcap-mime-extensions))) (cdr (assoc (downcase extn) mailcap-mime-extensions)))
;; Unused?
(defalias 'mailcap-command-p 'executable-find)
(defun mailcap-mime-types () (defun mailcap-mime-types ()
"Return a list of MIME media types." "Return a list of MIME media types."
(mailcap-parse-mimetypes) (mailcap-parse-mimetypes)