Centralize file mimetype detection in mime.kak

This way, 'file --mime-type $buffer' is called only once per file
rather than once per filetype detection hook.
This commit is contained in:
Maxime Coste 2013-11-12 20:38:00 +00:00
parent db5050fab0
commit b2cb5b421c
5 changed files with 15 additions and 19 deletions

View File

@ -41,6 +41,6 @@ XDG_CONFIG_HOME ?= $(HOME)/.config
userconfig:
mkdir -p $(XDG_CONFIG_HOME)/kak/autoload
ln -s $(CURDIR)/rc/{asciidoc,client,cpp,diff,git,grep,kakrc,make,sh,mail,man}.kak $(XDG_CONFIG_HOME)/kak/autoload/
ln -s $(CURDIR)/rc/{asciidoc,client,cpp,diff,git,grep,kakrc,mime,make,sh,mail,man}.kak $(XDG_CONFIG_HOME)/kak/autoload/
.PHONY: tags userconfig

View File

@ -2,12 +2,9 @@ hook global BufCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) %{
set buffer filetype cpp
}
hook global BufOpen .* %{ %sh{
mimetype="$(file -b --mime-type ${kak_bufname})"
if [[ "${mimetype}" == "text/x-c++" || "${mimetype}" == "text/x-c" ]]; then
echo set buffer filetype cpp;
fi
} }
hook global BufSetOption mimetype=text/x-c(\+\+)? %{
set buffer filetype cpp
}
hook global WinSetOption filetype=cpp %~
addhl group cpp-highlight
@ -35,6 +32,8 @@ hook global WinSetOption filetype=cpp %~
try %{ exec -draft <c-s>k<a-x>s^\h*\K(/{2,})<ret>y<c-o>P }
# indent after visibility specifier
try %[ exec -draft k<a-x><a-k>^\h*(public|private|protected):\h*$<ret>j<a-gt> ]
# indent after if|else|while|for
try %[ exec -draft <a-F>)MB<a-k>\`(if|else|while|for)\h*\(.*\)\n\h*\n\'<ret><a-space><space><a-gt> ]
_ @
hook window InsertChar \} -id cpp-indent %[

View File

@ -1,9 +1,4 @@
hook global BufOpen .* %{ %sh{
mimetype="$(file -b --mime-type ${kak_bufname})"
if [[ "${mimetype}" == "message/rfc822" ]]; then
echo set buffer filetype mail;
fi
} }
hook global BufSetOption mimetype=message/rfc822 %{ set buffer filetype mail }
hook global WinSetOption filetype=mail %~
addhl group mail-highlight

5
src/rc/mime.kak Normal file
View File

@ -0,0 +1,5 @@
decl str mimetype "text/plain"
hook global BufOpen .* %{
set buffer mimetype %sh{file -b --mime-type ${kak_bufname} }
}

View File

@ -2,12 +2,9 @@ hook global BufCreate .*\.(sh) %{
set buffer filetype sh
}
hook global BufOpen .* %{ %sh{
mimetype="$(file -b --mime-type ${kak_bufname})"
if [[ "${mimetype}" == "text/x-shellscript" ]]; then
echo set buffer filetype sh;
fi
} }
hook global BufSetOption mimetype=text/x-shellscript %{
set buffer filetype sh
}
hook global WinSetOption filetype=sh %~
addhl group sh-highlight