Function: mailcap-mime-info

mailcap-mime-info is a byte-compiled function defined in mailcap.el.gz.

Signature

(mailcap-mime-info STRING &optional REQUEST NO-DECODE)

Documentation

Get the MIME viewer command for STRING, return nil if none found.

Expects a complete content-type header line as its argument.

Second argument REQUEST specifies what information to return. If it is nil or the empty string, the viewer (second field of the mailcap entry) will be returned. If it is a string, then the mailcap field corresponding to that string will be returned (print, description, whatever). If a number, then all the information for this specific viewer is returned. If all, then all possible viewers for this type is returned.

If NO-DECODE is non-nil, don't decode STRING.

Source Code

;; Defined in /usr/src/emacs/lisp/net/mailcap.el.gz
(defun mailcap-mime-info (string &optional request no-decode)
  "Get the MIME viewer command for STRING, return nil if none found.
Expects a complete content-type header line as its argument.

Second argument REQUEST specifies what information to return.  If it is
nil or the empty string, the viewer (second field of the mailcap
entry) will be returned.  If it is a string, then the mailcap field
corresponding to that string will be returned (print, description,
whatever).  If a number, then all the information for this specific
viewer is returned.  If `all', then all possible viewers for
this type is returned.

If NO-DECODE is non-nil, don't decode STRING."
  ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
  ;; `mail-parse.el'
  (let (
	major              ; Major encoding (text, etc)
	minor              ; Minor encoding (html, etc)
	info               ; Other info
	major-info         ; (assoc major mailcap--computed-mime-data)
	viewers            ; Possible viewers
	passed             ; Viewers that passed the test
	viewer             ; The one and only viewer
	ctl)
    (save-excursion
      (setq ctl
	    (if no-decode
		(list (or string "text/plain"))
	      (mail-header-parse-content-type (or string "text/plain"))))
      ;; Check if there's a user-defined viewer from `mailcap-user-mime-data'.
      (setq viewer (mailcap-select-preferred-viewer ctl))
      (if viewer
          (setq passed (list viewer))
        ;; None found, so heuristically select some applicable viewer
        ;; from `mailcap--computed-mime-data'.
        (mailcap-parse-mailcaps nil t)
        (setq major (split-string (car ctl) "/"))
        (setq minor (cadr major)
              major (car major))
        (when (setq major-info (cdr (assoc major mailcap--computed-mime-data)))
          (when (setq viewers (mailcap-possible-viewers major-info minor))
            (setq info (mapcar (lambda (a)
                                 (cons (symbol-name (car a)) (cdr a)))
                               (cdr ctl)))
            (dolist (entry viewers)
              (when (mailcap-viewer-passes-test entry info)
                (push entry passed)))
            (setq passed (sort (nreverse passed) #'mailcap-viewer-lessp))
            ;; When we want to prefer entries from the user's
            ;; ~/.mailcap file, then we filter out the system entries
            ;; and see whether we have anything left.
            (when mailcap-prefer-mailcap-viewers
              (when-let ((user-entries
                          (seq-filter (lambda (elem)
                                        (eq (cdr (assq 'source elem)) 'user))
                                      passed)))
                (setq passed user-entries)))
            (setq viewer (car passed))))
        (when (and (stringp (cdr (assq 'viewer viewer)))
                   passed)
          (setq viewer (car passed))))
      (cond
       ((and (null viewer) (not (equal major "default")) request)
        (mailcap-mime-info "default" request no-decode))
       ((or (null request) (equal request ""))
        (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
       ((stringp request)
        (mailcap-unescape-mime-test
         (cdr-safe (assoc request viewer)) info))
       ((eq request 'all)
        passed)
       (t
        ;; MUST make a copy *sigh*, else we modify mailcap--computed-mime-data
        (setq viewer (copy-sequence viewer))
        (let ((view (assq 'viewer viewer))
              (test (assq 'test viewer)))
          (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
          (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
        viewer)))))