kakoune

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

teal.kak (5555B)


      1 hook global WinCreate ".*\.tl" %{
      2   set-option window filetype teal
      3 }
      4 
      5 provide-module -override teal %§
      6   require-module lua
      7   add-highlighter shared/teal regions
      8   add-highlighter shared/teal/code default-region group
      9   add-highlighter shared/teal/code/base ref lua
     10   add-highlighter shared/teal/code/constant regex '\b([A-Z_]+)\b' 0:value
     11   add-highlighter shared/teal/code/function_call regex '\b([a-zA-Z_]\w*)\h*(?=[\(\{"''])' 1:function
     12   add-highlighter shared/teal/code/builtin_types regex '\b(boolean|number|integer|thread|any)\b' 0:type
     13   add-highlighter shared/teal/code/string_type regex '\b(string)\b\h*(?![\(\{\.\h:="''])' 1:type
     14   add-highlighter shared/teal/code/keywords regex '\b(function|enum|record|interface|global|macroexp|metamethod|metatable|where|break|do|else|elseif|end|for|goto|if|in|local|repeat|return|then|until|while)\b' 0:keyword
     15   add-highlighter shared/teal/code/type_keyword regex '\b(type)\b\h*h*(?![\(\{\h:="''])' 1:keyword
     16   add-highlighter shared/teal/code/operators regex '\b(and|or|not|is|as)\b' 0:operator
     17 
     18   add-highlighter shared/teal/multiline_string  region -match-capture   '\[(=*)\[' '\](=*)\]' fill string
     19   add-highlighter shared/teal/multiline_comment region -match-capture '--\[(=*)\[' '\](=*)\]' fill comment
     20   add-highlighter shared/teal/double_string region '"' (?<!\\)(?:\\\\)*" fill string
     21   add-highlighter shared/teal/single_string region "'" (?<!\\)(?:\\\\)*' fill string
     22   add-highlighter shared/teal/comment region '--' $ fill comment
     23 
     24   declare-option str-list tl_static_words                                        \
     25   'function' 'boolean' 'number' 'integer' 'string' 'nil' 'thread' 'any' 'macroexp' 'enum' 'record' 'interface'     \
     26   'local' 'global' 'is' 'end' 'metamethod' 'return' 'print' 'table' 'false' 'true' 'self' 'break' 'and' 'or' 'not'   \
     27   'do' 'else' 'elseif' 'end' 'for' 'function' 'goto' 'if' 'in' 'repeat' 'return' 'then' 'until' 'while'    \
     28   'require' 'as' 'where' '__is' '__add' '__sub' '__mul' '__div' '__mod' '__pow' '__unm' '__idiv' '__band' '__bor'  \
     29   '__bxor' '__bnot' '__shl' '__shr' '__concat' '__len' '__eq' '__lt' '__le' '__index' '__newindex' '__call'      \
     30   '__tostring' '__tonumber' '__gc' '__close' '__pairs' '__ipairs' 'io' 'debug' 'pairs' 'ipairs' 'assert' 'tostring'  \
     31   'tonumber' 'os' '_VERSION' '_G' 'math' 'setmetatable' 'getmetatable' 'coroutine' 'pcall' 'xpcall'
     32 §
     33 
     34 hook global WinSetOption filetype=teal %§
     35   require-module teal
     36 
     37   set-option window comment_line '--'
     38   set-option window comment_block_begin '--[['
     39   set-option window comment_block_end ']]'
     40 
     41   add-highlighter window/teal ref teal
     42   hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/teal }
     43   set-option window static_words %opt{tl_static_words}
     44 
     45   define-command -hidden teal-trim-indent %[
     46     # remove trailing whitespaces
     47     try %[ execute-keys -draft -itersel x s \h+$ <ret> d ]
     48   ]
     49 
     50   define-command -hidden teal-indent-on-char %[
     51     evaluate-commands -no-hooks -draft -itersel %[
     52       # unindent middle and end structures
     53       try %[ execute-keys -draft \
     54         <a-h><a-k>^\h*(\b(end|else|elseif|until)\b|[)}])$<ret> \
     55         :teal-indent-on-new-line<ret> \
     56         <a-lt>
     57       ]
     58     ]
     59   ]
     60 
     61   define-command -hidden teal-indent-on-new-line %[
     62     evaluate-commands -no-hooks -draft -itersel %[
     63       # remove trailing white spaces from previous line
     64       try %[ execute-keys -draft k : teal-trim-indent <ret> ]
     65       # preserve previous non-empty line indent
     66       try %[ execute-keys -draft ,gh<a-?>^\N+$<ret>s\A|.\z<ret>)<a-&> ]
     67       # add one indentation level if the previous line is not a comment and:
     68       #   - starts with a block keyword that is not closed on the same line,
     69       #   - or contains an unclosed function expression,
     70       #   - or ends with an enclosed '(' or '{'
     71       try %[ execute-keys -draft \
     72         , Kx \
     73         <a-K>\A\h*--<ret> \
     74         <a-K>\A\N*\b(end|until)\b<ret> \
     75         <a-k>\A(\h*\b(do|else|elseif|for|((local|global)\h+)?(function|macroexp|record|enum|inteface)|if|repeat|while)\b|\N*[({]$|\N*\b(function|macroexp)\b\h*[(])<ret> \
     76         <a-:><semicolon><a-gt>
     77       ]
     78     ]
     79   ]
     80 
     81   define-command -hidden teal-insert-on-new-line %[
     82     evaluate-commands -no-hooks -draft -itersel %[
     83       # copy -- comment prefix and following white spaces
     84       try %[ execute-keys -draft kxs^\h*\K--\h*<ret> y gh j x<semicolon> P ]
     85       # wisely add end structure
     86       evaluate-commands -save-regs x %[
     87         # save previous line indent in register x
     88         try %[ execute-keys -draft kxs^\h+<ret>"xy ] catch %[ set-register x '' ]
     89         try %[
     90           # check that starts with a block keyword that is not closed on the same line
     91           execute-keys -draft \
     92             kx \
     93             <a-k>^\h*\b(else|elseif|do|for|((local|global)\h+)?(function|macroexp|record|enum|inteface)|if|while)\b|\N\b(function|macroexp)\b\h*[(]<ret> \
     94             <a-K>\bend\b<ret>
     95           # check that the block is empty and is not closed on a different line
     96           execute-keys -draft <a-a>i <a-K>^\N+\n\N+\n<ret> jx <a-K>^<c-r>x\b(else|elseif|end)\b<ret>
     97           # auto insert end
     98           execute-keys -draft o<c-r>xend<esc>
     99           # auto insert ) for anonymous function
    100           execute-keys -draft kx<a-k>\([^)\n]*function\b<ret>jjA)<esc>
    101         ]
    102       ]
    103     ]
    104   ]
    105 
    106   hook window InsertChar .* -group teal-indent teal-indent-on-char
    107   hook window InsertChar \n -group teal-indent teal-indent-on-new-line
    108   hook window InsertChar \n -group teal-insert teal-insert-on-new-line
    109 §