kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

moon.kak (5620B)


      1 # http://moonscript.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](moon) %{
      8     set-option buffer filetype moon
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=moon %{
     15     require-module moon
     16 
     17     hook window ModeChange pop:insert:.* -group moon-trim-indent moon-trim-indent
     18     hook window InsertChar .* -group moon-indent moon-indent-on-char
     19     hook window InsertChar \n -group moon-insert moon-insert-on-new-line
     20     hook window InsertChar \n -group moon-indent moon-indent-on-new-line
     21 
     22     alias window alt moon-alternative-file
     23 
     24     hook -once -always window WinSetOption filetype=.* %{
     25         remove-hooks window moon-.+
     26         unalias window alt moon-alternative-file
     27     }
     28 }
     29 
     30 hook -group moon-highlight global WinSetOption filetype=moon %{
     31     add-highlighter window/moon ref moon
     32     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/moon }
     33 }
     34 
     35 
     36 provide-module moon %[
     37 
     38 # Highlighters
     39 # ‾‾‾‾‾‾‾‾‾‾‾‾
     40 
     41 add-highlighter shared/moon regions
     42 add-highlighter shared/moon/code default-region group
     43 add-highlighter shared/moon/double_string region '"'  (?<!\\)(\\\\)*" regions
     44 add-highlighter shared/moon/single_string region "'"  (?<!\\)(\\\\)*' fill string
     45 add-highlighter shared/moon/comment       region '--' '$'             fill comment
     46 
     47 add-highlighter shared/moon/double_string/base default-region fill string
     48 add-highlighter shared/moon/double_string/interpolation region -recurse \{ \Q#{ \} fill meta
     49 
     50 add-highlighter shared/moon/code/ regex \\\w+ 0:function
     51 add-highlighter shared/moon/code/ regex [\W\)\}]\h+\K\.\w+ 0:function
     52 add-highlighter shared/moon/code/ regex (\+|-|\*|/|%|\^|==?|[~!]=|<=?|>=?|\.\.\.?|#|!) 0:operator
     53 add-highlighter shared/moon/code/ regex [-=]> 0:function
     54 add-highlighter shared/moon/code/ regex \b\w+: 0:variable
     55 add-highlighter shared/moon/code/ regex \w+\h*(?=[\(!]) 0:function
     56 add-highlighter shared/moon/code/ regex (?<!\w)[@:]\w+ 0:variable
     57 add-highlighter shared/moon/code/ regex (?<!\w)[@:]__(name|class|inherited):? 0:meta
     58 add-highlighter shared/moon/code/ regex (?<!\w)@@(\w+:?)? 0:meta
     59 add-highlighter shared/moon/code/ regex (\w+)\h*=\h*(?:\(.*?\)\h*)?[-=]> 1:function
     60 add-highlighter shared/moon/code/ regex \b(and|break|class|continue|do|else(if)?|export|extends|for|from|if|import|in|local|not|or|return|switch|then|unless|using|when|while|with)\b 0:keyword
     61 add-highlighter shared/moon/code/ regex \b(true|false|nil|super|self)\b 0:value
     62 add-highlighter shared/moon/code/ regex \b([0-9]+(:?\.[0-9])?(:?[eE]-?[0-9]+)?|0x[0-9a-fA-F]+)\b 0:value
     63 add-highlighter shared/moon/code/ regex class(\h+\w+)?(?:\h+extends(\h+\w+))?\h*$ 1:type 2:attribute
     64 add-highlighter shared/moon/code/ regex \b(_G|_ENV)\b 0:module
     65 add-highlighter shared/moon/code/ regex ^\h*export\h+[\*^]\h*$ 0:meta
     66 add-highlighter shared/moon/code/ regex ^\h*local\h+\*\h*$ 0:meta
     67 
     68 # Commands
     69 # ‾‾‾‾‾‾‾‾
     70 
     71 define-command moon-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ evaluate-commands %sh{
     72     case "$kak_buffile" in
     73         *spec/*_spec.moon)
     74             altfile=$(find "$(dirname "$kak_buffile" | sed s,spec$,,g)"*/ -name "$(basename "$kak_buffile" | sed s/_spec//)" | head -n1)
     75             [ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit
     76         ;;
     77         *.moon)
     78             path="$kak_buffile"
     79             dirs=$(while [ $path ]; do printf %s\\n $path; path=${path%/*}; done | tail -n +2)
     80             for dir in $dirs; do
     81                 altdir=$dir/spec
     82                 if [ -d $altdir ]; then
     83                     altfile=$altdir/$(realpath "$kak_buffile" --relative-to $dir | sed s+[^/]'*'/++';'s/.moon$/_spec.moon/)
     84                     break
     85                 fi
     86             done
     87             [ ! -d $altdir ] && echo "fail 'spec/ not found'" && exit
     88         ;;
     89         *)
     90             echo "fail 'alternative file not found'" && exit
     91         ;;
     92     esac
     93     printf %s\\n "edit $altfile"
     94 }}
     95 
     96 define-command -hidden moon-trim-indent %{
     97     evaluate-commands -draft -itersel %{
     98         execute-keys x
     99         # remove trailing white spaces
    100         try %{ execute-keys -draft s \h + $ <ret> d }
    101     }
    102 }
    103 
    104 define-command -hidden moon-indent-on-char %{
    105     evaluate-commands -draft -itersel %{
    106         # align _else_ statements to start
    107         try %{ execute-keys -draft x <a-k> ^ \h * (else(if)?) $ <ret> <a-semicolon> <a-?> ^ \h * (if|unless|when) <ret> s \A | \z <ret> ) <a-&> }
    108         # align _when_ to _switch_ then indent
    109         try %{ execute-keys -draft x <a-k> ^ \h * (when) $ <ret> <a-semicolon> <a-?> ^ \h * (switch) <ret> s \A | \z <ret> ) <a-&> ) , <gt> }
    110     }
    111 }
    112 
    113 define-command -hidden moon-insert-on-new-line %{
    114     evaluate-commands -draft -itersel %{
    115         # copy -- comment prefix and following white spaces
    116         try %{ execute-keys -draft k x s ^\h*\K--\h* <ret> y gh j P }
    117     }
    118 }
    119 
    120 define-command -hidden moon-indent-on-new-line %{
    121     evaluate-commands -draft -itersel %{
    122         # preserve previous line indent
    123         try %{ execute-keys -draft <semicolon> K <a-&> }
    124         # filter previous line
    125         try %{ execute-keys -draft k : moon-trim-indent <ret> }
    126         # indent after start structure
    127         try %{ execute-keys -draft k x <a-k> ^ \h * (class|else(if)?|for|if|switch|unless|when|while|with) \b | ([:=]|[-=]>) $ <ret> j <a-gt> }
    128         # deindent after return statements
    129         try %{ execute-keys -draft k x <a-k> ^ \h * (break|return) \b <ret> j <a-lt> }
    130     }
    131 }
    132 
    133 ]