Merge remote-tracking branch 'mogenson/editorconfig'

This commit is contained in:
Maxime Coste 2019-09-08 22:57:14 +10:00
commit 64511baceb

View File

@ -11,13 +11,15 @@ hook global BufCreate .*[.](editorconfig) %{
latin1 utf-8 utf-8-bom utf-16be utf-16le lf cr crlf unset space tab max_line_length latin1 utf-8 utf-8-bom utf-16be utf-16le lf cr crlf unset space tab max_line_length
} }
declare-option -hidden bool editorconfig_trim_trailing_whitespace false
define-command editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{ define-command editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{
remove-hooks buffer editorconfig-hooks
evaluate-commands %sh{ evaluate-commands %sh{
command -v editorconfig >/dev/null 2>&1 || { echo 'echo -markup "{Error}editorconfig could not be found"'; exit 1; } command -v editorconfig >/dev/null 2>&1 || { printf %s\\n 'echo -markup "{Error}editorconfig could not be found"'; exit 1; }
editorconfig "${1:-$kak_buffile}" | awk -F= -- '
file="${1:-$kak_buffile}"
case $file in
/*) # $kak_buffile is a full path that starts with a '/'
printf %s\\n "remove-hooks buffer editorconfig-hooks"
editorconfig "$file" | awk -v file="$file" -F= -- '
/indent_style=/ { indent_style = $2 } /indent_style=/ { indent_style = $2 }
/indent_size=/ { indent_size = $2 == "tab" ? 4 : $2 } /indent_size=/ { indent_size = $2 == "tab" ? 4 : $2 }
/tab_width=/ { tab_width = $2 } /tab_width=/ { tab_width = $2 }
@ -45,7 +47,7 @@ define-command editorconfig-load -params ..1 -docstring "editorconfig-load [file
print "set-option buffer BOM utf8" print "set-option buffer BOM utf8"
} }
if (trim_trailing_whitespace == "true") { if (trim_trailing_whitespace == "true") {
print "set-option buffer editorconfig_trim_trailing_whitespace true" print "hook buffer BufWritePre \"" file "\" -group editorconfig-hooks %{ try %{ execute-keys -draft %{ %s\\h+$<ret>d } } }"
} }
if (max_line_length && max_line_length != "off") { if (max_line_length && max_line_length != "off") {
print "set window autowrap_column " max_line_length print "set window autowrap_column " max_line_length
@ -53,11 +55,7 @@ define-command editorconfig-load -params ..1 -docstring "editorconfig-load [file
print "add-highlighter window/ column %sh{ echo $((" max_line_length "+1)) } default,bright-black" print "add-highlighter window/ column %sh{ echo $((" max_line_length "+1)) } default,bright-black"
} }
} }
' ' ;;
esac
} }
hook buffer BufWritePre %val{buffile} -group editorconfig-hooks %{ evaluate-commands %sh{
if [ ${kak_opt_editorconfig_trim_trailing_whitespace} = "true" ]; then
printf %s\\n "try %{ execute-keys -draft %{ %s\h+$<ret>d } }"
fi
} }
} }