From 2c3b2e9b522abb2b5b8659256196ca5731967dd5 Mon Sep 17 00:00:00 2001 From: Martin Chaine Date: Mon, 12 Dec 2016 21:46:48 +0100 Subject: [PATCH 1/5] fix indent options and add whitespaces ones --- rc/extra/editorconfig.kak | 60 ++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/rc/extra/editorconfig.kak b/rc/extra/editorconfig.kak index 4a66aff6..138d8ecb 100644 --- a/rc/extra/editorconfig.kak +++ b/rc/extra/editorconfig.kak @@ -1,35 +1,61 @@ -def editorconfig-load -docstring "Set indentation options according to editorconfig file" %{ +# 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+$d } }" + fi + if [ ${kak_opt_editorconfig_insert_final_newline} -gt 0 ]; then + printf %s\\n "try %{ exec -draft %{ %s\v+\Zd } }" + fi + if [ ${kak_opt_editorconfig_insert_final_newline} -gt 1 ]; then + printf %s\\n "try %{ exec -draft %{ geo } }" + fi + } } +} + +def editorconfig-load -docstring "Set indentation optins and whitespace behavior according to editorconfig file" %{ %sh{ command -v editorconfig >/dev/null 2>&1 || { echo 'echo -color Error The editorconfig tool could not be found'; exit 1; } - editorconfig $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 } + editorconfig "${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 } + /insert_final_newline/ { insert_final_newline = $2 } END { - if (indent_style == "tab") - { + if (indent_style == "tab") { print "set buffer indentwidth 0" print "set buffer aligntab true" } - if (indent_style == "spaces") { + if (indent_style == "space") { 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) } - if (end_of_line) { - if (end_of_line == "lf" || end_of_line == "crlf") - print "set buffer eolformat " end_of_line - else - print "error" + if (end_of_line == "lf" || end_of_line == "crlf") { + print "set buffer eolformat " end_of_line + } + if (charset == "utf-8-bom") { + print "set buffer BOM utf-8" + } + 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) } - if (charset) - print "set buffer BOM" (charset == "utf-8-bom" ? true : false) } ' } + _editorconfig-bufwritepre-hook } From 88a8548017b687e57efa2747a8426c91a7f51f07 Mon Sep 17 00:00:00 2001 From: Martin Chaine Date: Tue, 13 Dec 2016 13:21:03 +0100 Subject: [PATCH 2/5] fix typo --- rc/extra/editorconfig.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/extra/editorconfig.kak b/rc/extra/editorconfig.kak index 138d8ecb..0071ff97 100644 --- a/rc/extra/editorconfig.kak +++ b/rc/extra/editorconfig.kak @@ -18,7 +18,7 @@ def -hidden _editorconfig-bufwritepre-hook %{ } } } -def editorconfig-load -docstring "Set indentation optins and whitespace behavior according to editorconfig file" %{ +def editorconfig-load -docstring "Set indentation options and whitespace behavior according to editorconfig" %{ %sh{ command -v editorconfig >/dev/null 2>&1 || { echo 'echo -color Error The editorconfig tool could not be found'; exit 1; } editorconfig "${kak_buffile}" | awk -F= -- ' From 292d9c9f010b48ac72d81fe5e3f199e19a472f85 Mon Sep 17 00:00:00 2001 From: Martin Chaine Date: Mon, 19 Dec 2016 17:10:59 +0100 Subject: [PATCH 3/5] fix BOM option value --- rc/extra/editorconfig.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/extra/editorconfig.kak b/rc/extra/editorconfig.kak index 0071ff97..4e492597 100644 --- a/rc/extra/editorconfig.kak +++ b/rc/extra/editorconfig.kak @@ -46,7 +46,7 @@ def editorconfig-load -docstring "Set indentation options and whitespace behavio print "set buffer eolformat " end_of_line } if (charset == "utf-8-bom") { - print "set buffer BOM utf-8" + print "set buffer BOM utf8" } if (trim_trailing_whitespace == "true") { print "set buffer editorconfig_trim_trailing_whitespace true" From 45fb4b6bac0307babf295ec4307647fd4820a427 Mon Sep 17 00:00:00 2001 From: Martin Chaine Date: Wed, 28 Dec 2016 20:18:31 +0100 Subject: [PATCH 4/5] allow to override filename + remove existing hooks --- rc/extra/editorconfig.kak | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rc/extra/editorconfig.kak b/rc/extra/editorconfig.kak index 4e492597..6cc79410 100644 --- a/rc/extra/editorconfig.kak +++ b/rc/extra/editorconfig.kak @@ -18,10 +18,11 @@ def -hidden _editorconfig-bufwritepre-hook %{ } } } -def editorconfig-load -docstring "Set indentation options and whitespace behavior according to editorconfig" %{ +def editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{ + remove-hooks buffer editorconfig-hooks %sh{ - command -v editorconfig >/dev/null 2>&1 || { echo 'echo -color Error The editorconfig tool could not be found'; exit 1; } - editorconfig "${kak_buffile}" | awk -F= -- ' + command -v editorconfig >/dev/null 2>&1 || { echo 'echo -color 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 } From d6bde05d27d8f969f20e5509e85bc71c4f2f5fe2 Mon Sep 17 00:00:00 2001 From: Martin Chaine Date: Sun, 1 Jan 2017 19:19:40 +0100 Subject: [PATCH 5/5] don't handle trailing newline option kakoune handles files as a list of lines with a newline ending --- rc/extra/editorconfig.kak | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/rc/extra/editorconfig.kak b/rc/extra/editorconfig.kak index 6cc79410..a6f43461 100644 --- a/rc/extra/editorconfig.kak +++ b/rc/extra/editorconfig.kak @@ -2,21 +2,6 @@ # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 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+$d } }" - fi - if [ ${kak_opt_editorconfig_insert_final_newline} -gt 0 ]; then - printf %s\\n "try %{ exec -draft %{ %s\v+\Zd } }" - fi - if [ ${kak_opt_editorconfig_insert_final_newline} -gt 1 ]; then - printf %s\\n "try %{ exec -draft %{ geo } }" - fi - } } -} def editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{ remove-hooks buffer editorconfig-hooks @@ -29,7 +14,6 @@ def editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set form /end_of_line=/ { end_of_line = $2 } /charset=/ { charset = $2 } /trim_trailing_whitespace/ { trim_trailing_whitespace = $2 } - /insert_final_newline/ { insert_final_newline = $2 } END { if (indent_style == "tab") { @@ -52,11 +36,12 @@ def editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set form 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) - } } ' } - _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+$d } }" + fi + } } }