diff --git a/rc/extra/nim.kak b/rc/extra/nim.kak index 95df5c8fa..6cc619b62 100644 --- a/rc/extra/nim.kak +++ b/rc/extra/nim.kak @@ -4,7 +4,7 @@ # Detection # ‾‾‾‾‾‾‾‾‾ -hook global BufCreate .*\.nim %{ +hook global BufCreate .*\.nim(s|ble)? %{ set-option buffer filetype nim } @@ -53,9 +53,37 @@ add-highlighter shared/nim/code regex %{'[^'\n]'} 0:string add-highlighter shared/nim/code regex '#[^\n]+' 0:comment +# Commands +# ‾‾‾‾‾‾‾‾ + +def -hidden nim-indent-on-new-line %{ + eval -draft -itersel %{ + # copy '#' comment prefix and following white spaces + try %{ exec -draft k s ^\h*#\h* y jgh P } + # preserve previous line indent + try %{ exec -draft \; K } + # cleanup trailing whitespaces from previous line + try %{ exec -draft k s \h+$ d } + # indent after line ending with const, let, var, ':' or '=' + try %{ exec -draft k x (:|=|const|let|var)$ j } + } +} + # Initialization # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ hook -group nim-highlight global WinSetOption filetype=nim %{ add-highlighter window ref nim } +hook global WinSetOption filetype=nim %{ + set buffer tabstop 2 + set buffer indentwidth 2 + hook window InsertChar \n -group nim-indent nim-indent-on-new-line + # cleanup trailing whitespaces on current line insert end + hook window InsertEnd .* -group nim-indent %{ try %{ exec -draft \; s ^\h+$ d } } +} + hook -group nim-highlight global WinSetOption filetype=(?!nim).* %{ remove-highlighter window/nim } + +hook global WinSetOption filetype=(?!nim).* %{ + remove-hooks window nim-indent +}