diff --git a/rc/lua.kak b/rc/lua.kak new file mode 100644 index 00000000..a1454759 --- /dev/null +++ b/rc/lua.kak @@ -0,0 +1,88 @@ +# http://lua.org +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +# require commenting.kak + +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufSetOption mimetype=text/x-lua %{ + set buffer filetype lua +} + +hook global BufCreate .*[.](lua) %{ + set buffer filetype lua +} + +# Highlighters +# ‾‾‾‾‾‾‾‾‾‾‾‾ + +addhl -group / regions -default code lua \ + string '"' (? 0:keyword + +# Commands +# ‾‾‾‾‾‾‾‾ + +def -hidden _lua_filter_around_selections %{ + eval -draft -itersel %{ + exec + # remove trailing white spaces + try %{ exec -draft s \h + $ d } + } +} + +def -hidden _lua_indent_on_char %{ + eval -draft -itersel %{ + # align end structure to start + try %{ exec -draft ^ \h * end $ ^ \h * (for|function|if|while) s \A | \Z \' } + # align _else_ statements to previous _if_ + try %{ exec -draft ^ \h * else (if) ? $ ^ \h * if s \A | \Z \' } + } +} + +def -hidden _lua_indent_on_new_line %{ + eval -draft -itersel %{ + # preserve previous line indent + try %{ exec -draft K } + # filter previous line + try %{ exec -draft k : _lua_filter_around_selections } + # copy -- comment prefix and following white spaces + try %{ exec -draft k x s ^ \h * \K -- \h * y j p } + # indent after start structure + try %{ exec -draft k x ^ \h * (else|elseif|for|function|if|while) \b j } + # wisely add end structure + eval -save-regs x %{ + try %{ exec -draft k x s ^ \h + \" x y } catch %{ reg x '' } + try %{ exec -draft k x ^ x (for|function|if|while) j i X K ^ x (for|function|if|while) . * \n x end $ j x y p j a end } + } + } +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=lua %{ + addhl ref lua + + hook window InsertEnd .* -group lua-hooks _lua_filter_around_selections + hook window InsertChar .* -group lua-indent _lua_indent_on_char + hook window InsertChar \n -group lua-indent _lua_indent_on_new_line + + set window comment_line_chars '--' + set window comment_selection_chars '\Q--[[:]]' +} + +hook global WinSetOption filetype=(?!lua).* %{ + rmhl lua + rmhooks window lua-indent + rmhooks window lua-hooks +}