2016-12-12 23:46:48 +03:00
|
|
|
# http://editorconfig.org/#file-format-details
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
decl -hidden bool editorconfig_trim_trailing_whitespace false
|
|
|
|
decl -hidden int editorconfig_insert_final_newline 0
|
|
|
|
|
|
|
|
def -hidden _editorconfig-bufwritepre-hook %{
|
|
|
|
hook buffer BufWritePre %val{buffile} -group editorconfig-hooks %{ %sh{
|
|
|
|
if [ ${kak_opt_editorconfig_trim_trailing_whitespace} = "true" ]; then
|
|
|
|
printf %s\\n "try %{ exec -draft %{ %s\h+$<ret>d } }"
|
|
|
|
fi
|
|
|
|
if [ ${kak_opt_editorconfig_insert_final_newline} -gt 0 ]; then
|
|
|
|
printf %s\\n "try %{ exec -draft %{ %s\v+\Z<ret>d } }"
|
|
|
|
fi
|
|
|
|
if [ ${kak_opt_editorconfig_insert_final_newline} -gt 1 ]; then
|
|
|
|
printf %s\\n "try %{ exec -draft %{ geo<esc> } }"
|
|
|
|
fi
|
|
|
|
} }
|
|
|
|
}
|
|
|
|
|
2016-12-28 22:18:31 +03:00
|
|
|
def editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{
|
|
|
|
remove-hooks buffer editorconfig-hooks
|
2016-02-23 02:11:20 +03:00
|
|
|
%sh{
|
2016-12-28 22:18:31 +03:00
|
|
|
command -v editorconfig >/dev/null 2>&1 || { echo 'echo -color Error editorconfig could not be found'; exit 1; }
|
|
|
|
editorconfig "${1:-$kak_buffile}" | awk -F= -- '
|
2016-12-12 23:46:48 +03:00
|
|
|
/indent_style=/ { indent_style = $2 }
|
|
|
|
/indent_size=/ { indent_size = $2 == "tab" ? 4 : $2 }
|
|
|
|
/tab_width=/ { tab_width = $2 }
|
|
|
|
/end_of_line=/ { end_of_line = $2 }
|
|
|
|
/charset=/ { charset = $2 }
|
|
|
|
/trim_trailing_whitespace/ { trim_trailing_whitespace = $2 }
|
|
|
|
/insert_final_newline/ { insert_final_newline = $2 }
|
2016-12-02 16:44:51 +03:00
|
|
|
|
|
|
|
END {
|
2016-12-12 23:46:48 +03:00
|
|
|
if (indent_style == "tab") {
|
2016-12-02 16:44:51 +03:00
|
|
|
print "set buffer indentwidth 0"
|
|
|
|
print "set buffer aligntab true"
|
|
|
|
}
|
2016-12-12 23:46:48 +03:00
|
|
|
if (indent_style == "space") {
|
2016-12-02 16:44:51 +03:00
|
|
|
print "set buffer indentwidth " (indent_size == "tab" ? 4 : indent_size)
|
|
|
|
print "set buffer aligntab false"
|
|
|
|
}
|
|
|
|
if (indent_size || tab_width) {
|
|
|
|
print "set buffer tabstop " (tab_width ? tab_width : indent_size)
|
|
|
|
}
|
2016-12-12 23:46:48 +03:00
|
|
|
if (end_of_line == "lf" || end_of_line == "crlf") {
|
|
|
|
print "set buffer eolformat " end_of_line
|
|
|
|
}
|
|
|
|
if (charset == "utf-8-bom") {
|
2016-12-19 19:10:59 +03:00
|
|
|
print "set buffer BOM utf8"
|
2016-12-12 23:46:48 +03:00
|
|
|
}
|
|
|
|
if (trim_trailing_whitespace == "true") {
|
|
|
|
print "set buffer editorconfig_trim_trailing_whitespace true"
|
|
|
|
}
|
|
|
|
if (insert_final_newline) {
|
|
|
|
print "set buffer editorconfig_insert_final_newline " (insert_final_newline == "true" ? 2 : 1)
|
2016-12-02 16:44:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
2016-02-23 02:11:20 +03:00
|
|
|
}
|
2016-12-12 23:46:48 +03:00
|
|
|
_editorconfig-bufwritepre-hook
|
2016-02-23 02:11:20 +03:00
|
|
|
}
|