lua.kak (6434B)
1 # http://lua.org 2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate .*[.](lua|rockspec) %{ 8 set-option buffer filetype lua 9 } 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 14 hook global WinSetOption filetype=lua %{ 15 require-module lua 16 17 hook window ModeChange pop:insert:.* -group lua-trim-indent lua-trim-indent 18 hook window InsertChar .* -group lua-indent lua-indent-on-char 19 hook window InsertChar \n -group lua-indent lua-indent-on-new-line 20 hook window InsertChar \n -group lua-insert lua-insert-on-new-line 21 22 alias window alt lua-alternative-file 23 24 hook -once -always window WinSetOption filetype=.* %{ 25 remove-hooks window lua-.+ 26 unalias window alt lua-alternative-file 27 } 28 } 29 30 hook -group lua-highlight global WinSetOption filetype=lua %{ 31 add-highlighter window/lua ref lua 32 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/lua } 33 } 34 35 36 provide-module lua %§ 37 38 # Highlighters 39 # ‾‾‾‾‾‾‾‾‾‾‾‾ 40 41 add-highlighter shared/lua regions 42 add-highlighter shared/lua/code default-region group 43 add-highlighter shared/lua/raw_string region -match-capture '\[(=*)\[' '\](=*)\]' fill string 44 add-highlighter shared/lua/raw_comment region -match-capture '--\[(=*)\[' '\](=*)\]' fill comment 45 add-highlighter shared/lua/double_string region '"' (?<!\\)(?:\\\\)*" fill string 46 add-highlighter shared/lua/single_string region "'" (?<!\\)(?:\\\\)*' fill string 47 add-highlighter shared/lua/comment region '--' $ fill comment 48 49 add-highlighter shared/lua/code/variable regex \b\w*\b 0:variable # Everything in Lua is a variable! 50 add-highlighter shared/lua/code/function_declaration regex \b(?:function\h+)(?:\w+\h*\.\h*)*([a-zA-Z_]\w*)\( 1:function 51 add-highlighter shared/lua/code/function_call regex \b([a-zA-Z_]\w*)\h*(?=[\(\{]) 1:function 52 add-highlighter shared/lua/code/keyword regex \b(break|do|else|elseif|end|for|function|goto|if|in|local|repeat|return|then|until|while)\b 0:keyword 53 add-highlighter shared/lua/code/value regex \b(false|nil|true|self|[0-9]+(:?\.[0-9])?(:?[eE]-?[0-9]+)?|0x[0-9a-fA-F]+)\b 0:value 54 add-highlighter shared/lua/code/symbolic_operator regex (\+|-|\*|/|%|\^|==?|~=|<=?|>=?|\.\.\.?|#) 0:operator 55 add-highlighter shared/lua/code/keyword_operator regex \b(and|or|not)\b 0:operator 56 add-highlighter shared/lua/code/module regex \b(_G|_ENV)\b 0:module 57 add-highlighter shared/lua/code/attribute regex \B(<[a-zA-Z_]\w*>)\B 0:attribute 58 add-highlighter shared/lua/code/label regex \s(::\w*::) 1:meta 59 add-highlighter shared/lua/code/goto_label regex "\bgoto (\w*)\b" 1:meta 60 61 # Commands 62 # ‾‾‾‾‾‾‾‾ 63 64 define-command lua-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ evaluate-commands %sh{ 65 case "$kak_buffile" in 66 *spec/*_spec.lua) 67 altfile=$(find "$(dirname "$kak_buffile" | sed s,spec$,,g)"*/ -name "$(basename "$kak_buffile" | sed s/_spec//)" | head -n1) 68 [ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit 69 ;; 70 *.lua) 71 altfile="" 72 altdir="" 73 path="$kak_buffile" 74 dirs=$(while [ $path ]; do printf %s\\n $path; path=${path%/*}; done | tail -n +2) 75 for dir in $dirs; do 76 altdir=$dir/spec 77 if [ -d $altdir ]; then 78 altfile=$altdir/$(realpath "$kak_buffile" --relative-to $dir | sed s+[^/]'*'/++';'s/.lua$/_spec.lua/) 79 break 80 fi 81 done 82 [ ! -d "$altdir" ] && echo "fail 'spec/ not found'" && exit 83 ;; 84 *) 85 echo "fail 'alternative file not found'" && exit 86 ;; 87 esac 88 printf %s\\n "edit $altfile" 89 }} 90 91 define-command -hidden lua-trim-indent %[ 92 # remove trailing whitespaces 93 try %[ execute-keys -draft -itersel x s \h+$ <ret> d ] 94 ] 95 96 define-command -hidden lua-indent-on-char %[ 97 evaluate-commands -no-hooks -draft -itersel %[ 98 # unindent middle and end structures 99 try %[ execute-keys -draft \ 100 <a-h><a-k>^\h*(\b(end|else|elseif|until)\b|[)}])$<ret> \ 101 :lua-indent-on-new-line<ret> \ 102 <a-lt> 103 ] 104 ] 105 ] 106 107 define-command -hidden lua-indent-on-new-line %[ 108 evaluate-commands -no-hooks -draft -itersel %[ 109 # remove trailing white spaces from previous line 110 try %[ execute-keys -draft k : lua-trim-indent <ret> ] 111 # preserve previous non-empty line indent 112 try %[ execute-keys -draft ,gh<a-?>^\N+$<ret>s\A|.\z<ret>)<a-&> ] 113 # add one indentation level if the previous line is not a comment and: 114 # - starts with a block keyword that is not closed on the same line, 115 # - or contains an unclosed function expression, 116 # - or ends with an enclosed '(' or '{' 117 try %[ execute-keys -draft \ 118 , Kx \ 119 <a-K>\A\h*--<ret> \ 120 <a-K>\A\N*\b(end|until)\b<ret> \ 121 <a-k>\A(\h*\b(do|else|elseif|for|(local\h+)?function|if|repeat|while)\b|\N*[({]$|\N*\bfunction\b\h*[(])<ret> \ 122 <a-:><semicolon><a-gt> 123 ] 124 ] 125 ] 126 127 define-command -hidden lua-insert-on-new-line %[ 128 evaluate-commands -no-hooks -draft -itersel %[ 129 # copy -- comment prefix and following white spaces 130 try %[ execute-keys -draft kxs^\h*\K--\h*<ret> y gh j x<semicolon> P ] 131 # wisely add end structure 132 evaluate-commands -save-regs x %[ 133 # save previous line indent in register x 134 try %[ execute-keys -draft kxs^\h+<ret>"xy ] catch %[ reg x '' ] 135 try %[ 136 # check that starts with a block keyword that is not closed on the same line 137 execute-keys -draft \ 138 kx \ 139 <a-k>^\h*\b(else|elseif|do|for|(local\h+)?function|if|while)\b|\N\bfunction\b\h*[(]<ret> \ 140 <a-K>\bend\b<ret> 141 # check that the block is empty and is not closed on a different line 142 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> 143 # auto insert end 144 execute-keys -draft o<c-r>xend<esc> 145 # auto insert ) for anonymous function 146 execute-keys -draft kx<a-k>\([^)\n]*function\b<ret>jjA)<esc> 147 ] 148 ] 149 ] 150 ] 151 152 §