kakoune/rc/detection/file.kak
Matthias Margush 99c09daec3 Fix filetype detection
The -i flag on Mac OS means:
    ჻ man file | grep -i -- -i
    -i      If the file is a regular file, do not classify its contents.

The --mime-type option is (mostly) portable:
- Linux uses --mime-type
- macOS uses --mime-type
- FreeBSD uses --mime-type
- NetBSD uses --mime-type
- OpenBSD uses --mime-type and does not use the same implementation as everybody else
- Solaris does not support MIME types at all
2020-02-21 21:25:57 -08:00

19 lines
700 B
Plaintext

hook global BufOpenFile .* %{ evaluate-commands %sh{
if [ -z "${kak_opt_filetype}" ]; then
mime=$(file -b --mime-type -L "${kak_buffile}")
mime=${mime%;*}
case "${mime}" in
application/*+xml) filetype="xml" ;;
image/*+xml) filetype="xml" ;; #SVG
message/rfc822) filetype="mail" ;;
text/x-shellscript) filetype="sh" ;;
text/x-*) filetype="${mime#text/x-}" ;;
text/*) filetype="${mime#text/}" ;;
application/*) filetype="${mime#application/}" ;;
esac
if [ -n "${filetype}" ]; then
printf "set-option buffer filetype '%s'\n" "${filetype}"
fi
fi
} }