diff options
| author | Michael Mogenson <michael.mogenson@gmail.com> | 2019-07-09 11:06:32 -0400 |
|---|---|---|
| committer | Michael Mogenson <michael.mogenson@gmail.com> | 2019-08-20 14:55:17 -0400 |
| commit | 37ded5e400aac6e4cbe6ebcdc12795dd842d081a (patch) | |
| tree | e91a472d0992687e4b4f7ca2dd7b1c92e58244a6 | |
| parent | 09c514901d84fa9f9be6fa61af183c606e3a5926 (diff) | |
Check if buffile is path before loading editorconfig settings
Check if buffile is a full path by checking for the beginning
'/' character in editorconfig-load command. This avoids a parsing
error from feeding a *scratch*/*debug*/*grep*/etc. buffer name to the
editorcofig command. Don't clear editorconfig hooks until after checking
for a valid bufffile path. This way, opening the *debug* buffer will
not clear the hooks from a previously parsed .editorconfig file. If
trim_trailing_whitespace is true, print the hook directly from awk. This
removes the need to save a editorcofig_trim_trailing_whitespace option.
Note: Setting the max_line_length requires a window to be created.
Therefore, a global hook to load .editorconfig settings should be:
hook global WinCreate .* %{editorconfig-load}
| -rw-r--r-- | rc/detection/editorconfig.kak | 88 |
1 files changed, 43 insertions, 45 deletions
diff --git a/rc/detection/editorconfig.kak b/rc/detection/editorconfig.kak index ec1bec0a..f59e3bb7 100644 --- a/rc/detection/editorconfig.kak +++ b/rc/detection/editorconfig.kak @@ -11,53 +11,51 @@ hook global BufCreate .*[.](editorconfig) %{ 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" %{ - remove-hooks buffer editorconfig-hooks evaluate-commands %sh{ - command -v editorconfig >/dev/null 2>&1 || { echo 'echo -markup "{Error}editorconfig could not be found"'; exit 1; } - editorconfig "${1:-$kak_buffile}" | awk -F= -- ' - /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 } - /max_line_length=/ { max_line_length = $2 } + command -v editorconfig >/dev/null 2>&1 || { printf %s\\n 'echo -markup "{Error}editorconfig could not be found"'; exit 1; } + + 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_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 } + /max_line_length=/ { max_line_length = $2 } - END { - if (indent_style == "tab") { - print "set-option buffer indentwidth 0" - print "set-option buffer aligntab true" - } - if (indent_style == "space") { - print "set-option buffer indentwidth " (indent_size == "tab" ? 4 : indent_size) - print "set-option buffer aligntab false" - } - if (indent_size || tab_width) { - print "set-option buffer tabstop " (tab_width ? tab_width : indent_size) - } - if (end_of_line == "lf" || end_of_line == "crlf") { - print "set-option buffer eolformat " end_of_line - } - if (charset == "utf-8-bom") { - print "set-option buffer BOM utf8" - } - if (trim_trailing_whitespace == "true") { - print "set-option buffer editorconfig_trim_trailing_whitespace true" - } - if (max_line_length && max_line_length != "off") { - print "set window autowrap_column " max_line_length - print "autowrap-enable" - print "add-highlighter window/ column %sh{ echo $((" max_line_length "+1)) } default,bright-black" - } - } - ' + END { + if (indent_style == "tab") { + print "set-option buffer indentwidth 0" + print "set-option buffer aligntab true" + } + if (indent_style == "space") { + print "set-option buffer indentwidth " (indent_size == "tab" ? 4 : indent_size) + print "set-option buffer aligntab false" + } + if (indent_size || tab_width) { + print "set-option buffer tabstop " (tab_width ? tab_width : indent_size) + } + if (end_of_line == "lf" || end_of_line == "crlf") { + print "set-option buffer eolformat " end_of_line + } + if (charset == "utf-8-bom") { + print "set-option buffer BOM utf8" + } + if (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") { + print "set window autowrap_column " max_line_length + print "autowrap-enable" + 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 - } } } |
