diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index 318a20e5..995bd7b4 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -14,6 +14,7 @@ hook global BufCreate .*[.](lua) %{ hook global WinSetOption filetype=lua %{ require-module lua + hook window ModeChange pop:insert:.* -group lua-indent lua-trim-indent hook window InsertChar .* -group lua-indent lua-indent-on-char hook window InsertChar \n -group lua-indent lua-indent-on-new-line hook window InsertChar \n -group lua-insert lua-insert-on-new-line @@ -47,7 +48,7 @@ add-highlighter shared/lua/comment region '--' $ fill com add-highlighter shared/lua/code/keyword regex \b(and|break|do|else|elseif|end|for|function|goto|if|in|not|or|repeat|return|then|until|while)\b 0:keyword add-highlighter shared/lua/code/value regex \b(false|nil|true|[0-9]+(:?\.[0-9])?(:?[eE]-?[0-9]+)?|0x[0-9a-fA-F])\b 0:value -add-highlighter shared/lua/code/operator regex (\+|-|\*|/|%|\^|==?|~=|<=?|>=?|\.\.|#) 0:operator +add-highlighter shared/lua/code/operator regex (\+|-|\*|/|%|\^|==?|~=|<=?|>=?|\.\.|\.\.\.|#) 0:operator add-highlighter shared/lua/code/builtin regex \b(_G|_E)\b 0:builtin add-highlighter shared/lua/code/module regex \b(_G|_E)\b 0:module add-highlighter shared/lua/code/attribute regex \b(local)\b 0:attribute @@ -82,22 +83,27 @@ define-command lua-alternative-file -docstring 'Jump to the alternate file (impl printf %s\\n "edit $altfile" }} +define-command -hidden lua-trim-indent %{ + # remove trailing whitespaces + try %{ execute-keys -draft -itersel s \h+$ d } +} + define-command -hidden lua-indent-on-char %{ evaluate-commands -no-hooks -draft -itersel %{ # align middle and end structures to start and indent when necessary, elseif is already covered by else - try %{ execute-keys -draft ^\h*(else)$^\h*(if)s\A|\z) } - try %{ execute-keys -draft ^\h*(end)$^\h*(for|function|if|while)s\A|\z) } + try %{ execute-keys -draft ^\h*(else)$^\h*(if)s\A|.\z) } + try %{ execute-keys -draft ^\h*(end)$^\h*(for|function|if|while)s\A|.\z) } } } define-command -hidden lua-indent-on-new-line %{ evaluate-commands -no-hooks -draft -itersel %{ - # preserve previous line indent - try %{ execute-keys -draft K } + # preserve previous non-empty line indent + try %{ execute-keys -draft ^[^\n]+$s\A|.\z) } # remove trailing white spaces from previous line try %{ execute-keys -draft ks\h+$d } # indent after start structure - try %{ execute-keys -draft k^\h*(else|elseif|for|function|if|while)\bj } + try %{ execute-keys -draft ^[^\n]*\w+[^\n]*$^\h*(else|elseif|for|function|if|while)\b } } }