diff --git a/rc/extra/moon.kak b/rc/extra/moon.kak new file mode 100644 index 00000000..98658cd9 --- /dev/null +++ b/rc/extra/moon.kak @@ -0,0 +1,91 @@ +# http://moonscript.org +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +# require commenting.kak + +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufSetOption mimetype=text/x-moon %{ + set buffer filetype moon +} + +hook global BufCreate .*[.](moon) %{ + set buffer filetype moon +} + +# Highlighters +# ‾‾‾‾‾‾‾‾‾‾‾‾ + +addhl -group / regions -default code moon \ + double_string '"' (? 0:keyword + +# Commands +# ‾‾‾‾‾‾‾‾ + +def -hidden _moon_filter_around_selections %{ + eval -draft -itersel %{ + exec + # remove trailing white spaces + try %{ exec -draft s \h + $ d } + } +} + +def -hidden _moon_indent_on_char %{ + eval -draft -itersel %{ + # align _else_ statements to start + try %{ exec -draft ^ \h * (else(if)?) $ ^ \h * (if|unless|when) s \A | \Z \' } + # align _when_ to _switch_ then indent + try %{ exec -draft ^ \h * (when) $ ^ \h * (switch) s \A | \Z \' \' } + # align _catch_ and _finally_ to _try_ + try %{ exec -draft ^ \h * (catch|finally) $ ^ \h * (try) s \A | \Z \' } + } +} + +def -hidden _moon_indent_on_new_line %{ + eval -draft -itersel %{ + # preserve previous line indent + try %{ exec -draft K } + # filter previous line + try %{ exec -draft k : _moon_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 * (class|else(if)?|for|if|switch|unless|when|while|with) \b | ([:=]|[-=]>) $ j } + # deindent after return statements + try %{ exec -draft k x ^ \h * (break|return) \b j } + } +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=moon %{ + addhl ref moon + + hook window InsertEnd .* -group moon-hooks _moon_filter_around_selections + hook window InsertChar .* -group moon-indent _moon_indent_on_char + hook window InsertChar \n -group moon-indent _moon_indent_on_new_line + + set window comment_line_chars '--' + set window comment_selection_chars '' +} + +hook global WinSetOption filetype=(?!moon).* %{ + rmhl moon + rmhooks window moon-indent + rmhooks window moon-hooks +}