2016-12-12 23:46:48 +03:00
|
|
|
# http://editorconfig.org/#file-format-details
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2018-09-28 14:41:44 +03:00
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global BufCreate .*[.](editorconfig) %{
|
|
|
|
set-option buffer filetype ini
|
|
|
|
set-option buffer static_words indent_style indent_size tab_width \
|
|
|
|
end_of_line charset insert_final_newline trim_trailing_whitespace root \
|
|
|
|
latin1 utf-8 utf-8-bom utf-16be utf-16le lf cr crlf unset space tab
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
declare-option -hidden bool editorconfig_trim_trailing_whitespace false
|
2016-12-12 23:46:48 +03:00
|
|
|
|
2018-04-19 20:28:32 +03:00
|
|
|
define-command editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{
|
2016-12-28 22:18:31 +03:00
|
|
|
remove-hooks buffer editorconfig-hooks
|
2018-05-07 00:29:52 +03:00
|
|
|
evaluate-commands %sh{
|
2017-07-19 18:18:52 +03:00
|
|
|
command -v editorconfig >/dev/null 2>&1 || { echo 'echo -markup "{Error}editorconfig could not be found"'; exit 1; }
|
2016-12-28 22:18:31 +03:00
|
|
|
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 }
|
2017-08-23 22:11:46 +03:00
|
|
|
/trim_trailing_whitespace=/ { trim_trailing_whitespace = $2 }
|
2016-12-02 16:44:51 +03:00
|
|
|
|
|
|
|
END {
|
2016-12-12 23:46:48 +03:00
|
|
|
if (indent_style == "tab") {
|
2017-11-03 10:34:41 +03:00
|
|
|
print "set-option buffer indentwidth 0"
|
|
|
|
print "set-option buffer aligntab true"
|
2016-12-02 16:44:51 +03:00
|
|
|
}
|
2016-12-12 23:46:48 +03:00
|
|
|
if (indent_style == "space") {
|
2017-11-03 10:34:41 +03:00
|
|
|
print "set-option buffer indentwidth " (indent_size == "tab" ? 4 : indent_size)
|
|
|
|
print "set-option buffer aligntab false"
|
2016-12-02 16:44:51 +03:00
|
|
|
}
|
|
|
|
if (indent_size || tab_width) {
|
2017-11-03 10:34:41 +03:00
|
|
|
print "set-option buffer tabstop " (tab_width ? tab_width : indent_size)
|
2016-12-02 16:44:51 +03:00
|
|
|
}
|
2016-12-12 23:46:48 +03:00
|
|
|
if (end_of_line == "lf" || end_of_line == "crlf") {
|
2017-11-03 10:34:41 +03:00
|
|
|
print "set-option buffer eolformat " end_of_line
|
2016-12-12 23:46:48 +03:00
|
|
|
}
|
|
|
|
if (charset == "utf-8-bom") {
|
2017-11-03 10:34:41 +03:00
|
|
|
print "set-option buffer BOM utf8"
|
2016-12-12 23:46:48 +03:00
|
|
|
}
|
|
|
|
if (trim_trailing_whitespace == "true") {
|
2017-11-03 10:34:41 +03:00
|
|
|
print "set-option buffer editorconfig_trim_trailing_whitespace true"
|
2016-12-12 23:46:48 +03:00
|
|
|
}
|
2016-12-02 16:44:51 +03:00
|
|
|
}
|
|
|
|
'
|
2016-02-23 02:11:20 +03:00
|
|
|
}
|
2018-05-07 00:29:52 +03:00
|
|
|
hook buffer BufWritePre %val{buffile} -group editorconfig-hooks %{ evaluate-commands %sh{
|
2017-01-01 21:19:40 +03:00
|
|
|
if [ ${kak_opt_editorconfig_trim_trailing_whitespace} = "true" ]; then
|
2017-11-03 11:09:45 +03:00
|
|
|
printf %s\\n "try %{ execute-keys -draft %{ %s\h+$<ret>d } }"
|
2017-01-01 21:19:40 +03:00
|
|
|
fi
|
|
|
|
} }
|
2016-02-23 02:11:20 +03:00
|
|
|
}
|