2

Is there a builtin function to guess the MIME type of a file in Emacs Lisp? I would like to display the MIME type for files in Dired buffers.

3

Yes. In standard library mailcap.el you will find functions that deal with mime types.

For example, mailcap-mime-types gives a list of known mime types - M-: (mailcap-mime-types) returns something like this:

("text/plain" "audio/x-mpeg" "audio/aiff" "application/x-annotator" 
 "audio/ulaw" "video/x-msvideo" "application/x-bcpio" "application/octet-stream"
 "application/x-netcdr" "application/x-cpio" "application/x-csh" "text/css" 
 "application/x-dvi" "text/x-patch" "test/x-patch" "application/emacs-lisp" 
 "application/postscript" "text/x-setext" "image/x-fax" "image/gif" 
 "application/x-hdf" "application/mac-binhex40" "text/html" "image/x-icon" "image/ief"
 "image/jpeg" "image/x-macpaint" "application/x-troff-man" "application/x-troff-me"
 "application/mif" "video/quicktime" "video/x-sgi-movie" "audio/x-mpeg2" 
 "video/mpeg" "application/x-troff-ms" "application/x-netcdf" "application/oda"
 "image/x-portable-bitmap" "application/pdf" "image/portable-graymap" "image/pict"
 "image/png" "image/x-portable-anymap" "image/portable-pixmap" "image/x-raster"
 "image/x-rgb" "application/rtf" "text/richtext" "application/x-sh" 
 "application/x-stuffit" "application/sieve" "audio/basic" "text/dns" 
 "application/x-wais-source" "archive/tar" "application/x-tcl" 
 "application/x-tex" "application/texinfo" "image/x-targa" "image/tiff" 
 "application/x-troff" "text/tab-separated-values" "x-world/x-vrml" 
 "audio/x-wav" "application/vnd.ms-excel" "image/xbm" "image/xpm" 
 "image/windowdump" "application/zip" "text/x-org" "application/x-x509-ca-cert"
 "application/x-x509-user-cert" "application/dvi" "application/x-emacs-lisp" 
 "application/x-tar" "application/x-latex" "application/latex" 
 "application/tex" "application/pgp-keys" "message/rfc822" "image/x-xwd" "text/enriched")

And for example, mailcap-extension-to-mime returns mime types applicable to files with given extensions.

The code in mailcap-file-default-commands shows some of what you can do. This part, for example, returns all unique MIME types for the given FILES:

(mailcap-delete-duplicates
   (mapcar (lambda (file) (mailcap-extension-to-mime (file-name-extension file t)))
           files))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.