2016-01-07 15:49:35 +01:00
# http://lua.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# Detection
# ‾‾‾‾‾‾‾‾‾
2021-07-26 22:41:00 +02:00
hook global BufCreate .*[.](lua|rockspec) %{
2017-11-03 08:34:41 +01:00
set-option buffer filetype lua
2016-01-07 15:49:35 +01:00
}
2019-04-10 05:54:19 +02:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=lua %{
2019-03-13 19:15:59 +01:00
require-module lua
2019-04-10 05:54:19 +02:00
2020-10-03 00:32:09 +02:00
hook window ModeChange pop:insert:.* -group lua-trim-indent lua-trim-indent
2019-04-10 05:54:19 +02:00
hook window InsertChar .* -group lua-indent lua-indent-on-char
hook window InsertChar \n -group lua-indent lua-indent-on-new-line
hook window InsertChar \n -group lua-insert lua-insert-on-new-line
alias window alt lua-alternative-file
hook -once -always window WinSetOption filetype=.* %{
remove-hooks window lua-.+
unalias window alt lua-alternative-file
}
}
hook -group lua-highlight global WinSetOption filetype=lua %{
add-highlighter window/lua ref lua
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/lua }
2019-03-13 19:15:59 +01:00
}
2019-04-10 05:54:19 +02:00
2019-03-13 19:15:59 +01:00
provide-module lua %[
2016-01-07 15:49:35 +01:00
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2018-06-28 14:10:22 +02:00
add-highlighter shared/lua regions
add-highlighter shared/lua/code default-region group
2018-11-12 21:38:13 +01:00
add-highlighter shared/lua/raw_string region -match-capture '\[(=*)\[' '\](=*)\]' fill string
add-highlighter shared/lua/raw_comment region -match-capture '--\[(=*)\[' '\](=*)\]' fill comment
2018-07-02 12:59:12 +02:00
add-highlighter shared/lua/double_string region '"' (?<!\\)(?:\\\\)*" fill string
add-highlighter shared/lua/single_string region "'" (?<!\\)(?:\\\\)*' fill string
add-highlighter shared/lua/comment region '--' $ fill comment
2016-01-07 15:49:35 +01:00
2021-07-29 00:10:43 +02:00
add-highlighter shared/lua/code/function_declaration regex \b(?:function\h+)(?:\w+\h*\.\h*)*([a-zA-Z_]\w*)\( 1:function
add-highlighter shared/lua/code/function_call regex \b([a-zA-Z_]\w*)\h*(?=[\(\{]) 1:function
2021-07-29 00:14:18 +02:00
add-highlighter shared/lua/code/keyword regex \b(and|break|do|else|elseif|end|for|function|goto|if|in|local|not|or|repeat|return|then|until|while)\b 0:keyword
2019-11-15 17:05:54 +01:00
add-highlighter shared/lua/code/value regex \b(false|nil|true|[0-9]+(:?\.[0-9])?(:?[eE]-?[0-9]+)?|0x[0-9a-fA-F])\b 0:value
2020-03-20 17:03:14 +01:00
add-highlighter shared/lua/code/operator regex (\+|-|\*|/|%|\^|==?|~=|<=?|>=?|\.\.|\.\.\.|#) 0:operator
2021-07-29 00:11:56 +02:00
add-highlighter shared/lua/code/builtin regex \b(_G|_ENV)\b 0:builtin
add-highlighter shared/lua/code/module regex \b(_G|_ENV)\b 0:module
2021-07-29 00:22:14 +02:00
add-highlighter shared/lua/code/attribute regex \B(<[a-zA-Z_]\w*>)\B 0:attribute
2016-01-07 15:49:35 +01:00
# Commands
# ‾‾‾‾‾‾‾‾
2018-05-06 23:29:52 +02:00
define-command lua-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ evaluate-commands %sh{
2016-03-03 18:56:50 +01:00
case $kak_buffile in
*spec/*_spec.lua)
2016-04-23 07:47:01 +02:00
altfile=$(eval printf %s\\n $(printf %s\\n $kak_buffile | sed s+spec/+'*'/+';'s/_spec//))
2019-10-08 18:06:54 +02:00
[ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit
2016-03-03 18:56:50 +01:00
;;
*.lua)
2020-08-23 18:55:50 +02:00
altfile=""
altdir=""
2016-03-03 18:56:50 +01:00
path=$kak_buffile
2016-04-23 07:47:01 +02:00
dirs=$(while [ $path ]; do printf %s\\n $path; path=${path%/*}; done | tail -n +2)
2016-03-03 18:56:50 +01:00
for dir in $dirs; do
altdir=$dir/spec
if [ -d $altdir ]; then
2016-03-08 20:33:21 +01:00
altfile=$altdir/$(realpath $kak_buffile --relative-to $dir | sed s+[^/]'*'/++';'s/.lua$/_spec.lua/)
2016-03-03 18:56:50 +01:00
break
fi
done
2020-08-23 18:55:50 +02:00
[ ! -d "$altdir" ] && echo "fail 'spec/ not found'" && exit
2016-03-03 18:56:50 +01:00
;;
*)
2019-10-08 18:06:54 +02:00
echo "fail 'alternative file not found'" && exit
2016-03-03 18:56:50 +01:00
;;
esac
2016-04-23 07:47:01 +02:00
printf %s\\n "edit $altfile"
2016-03-03 18:56:50 +01:00
}}
2020-03-20 17:03:14 +01:00
define-command -hidden lua-trim-indent %{
# remove trailing whitespaces
try %{ execute-keys -draft -itersel <a-x> s \h+$ <ret> d }
}
2017-11-03 08:34:41 +01:00
define-command -hidden lua-indent-on-char %{
2017-11-03 09:09:45 +01:00
evaluate-commands -no-hooks -draft -itersel %{
2016-11-15 21:23:46 +01:00
# align middle and end structures to start and indent when necessary, elseif is already covered by else
2020-03-20 17:03:14 +01:00
try %{ execute-keys -draft <a-x><a-k>^\h*(else)$<ret><a-semicolon><a-?>^\h*(if)<ret>s\A|.\z<ret>)<a-&> }
try %{ execute-keys -draft <a-x><a-k>^\h*(end)$<ret><a-semicolon><a-?>^\h*(for|function|if|while)<ret>s\A|.\z<ret>)<a-&> }
2016-01-07 15:49:35 +01:00
}
}
2017-11-03 08:34:41 +01:00
define-command -hidden lua-indent-on-new-line %{
2017-11-03 09:09:45 +01:00
evaluate-commands -no-hooks -draft -itersel %{
2020-10-03 00:32:09 +02:00
# remove trailing white spaces from previous line
try %{ execute-keys -draft k : lua-trim-indent <ret> }
2020-03-20 17:03:14 +01:00
# preserve previous non-empty line indent
try %{ execute-keys -draft <space><a-?>^[^\n]+$<ret>s\A|.\z<ret>)<a-&> }
2016-01-07 15:49:35 +01:00
# indent after start structure
2021-03-23 09:13:10 +01:00
try %{ execute-keys -draft <a-?>^[^\n]*\w+[^\n]*$<ret><a-k>\b(else|elseif|for|function|if|while)\b<ret><a-K>\bend\b<ret><a-:><semicolon><a-gt> }
2016-11-15 21:23:46 +01:00
}
}
2018-03-04 00:35:38 +01:00
define-command -hidden lua-insert-on-new-line %[
evaluate-commands -no-hooks -draft -itersel %[
2016-11-15 21:23:46 +01:00
# copy -- comment prefix and following white spaces
2021-07-13 18:21:47 +02:00
try %{ execute-keys -draft k<a-x>s^\h*\K--\h*<ret> y gh j <a-x><semicolon> P }
2016-01-07 15:49:35 +01:00
# wisely add end structure
2018-03-04 00:35:38 +01:00
evaluate-commands -save-regs x %[
try %{ execute-keys -draft k<a-x>s^\h+<ret>"xy } catch %{ reg x '' } # Save previous line indent in register x
2021-03-23 09:13:10 +01:00
try %[ execute-keys -draft k<a-x> <a-k>^<c-r>x\b[^\n]*(for|function|if|while)<ret><a-K>\bend\b<ret> J}iJ<a-x> <a-K>^<c-r>x(else|end|elseif)$<ret> # Validate previous line and that it is not closed yet
execute-keys -draft o<c-r>xend<esc> # auto insert end
execute-keys -draft k<a-x><a-k>\(function\b<ret>jjA)<esc> ] # auto insert ) for anonymous function
2018-03-04 00:35:38 +01:00
]
]
]
2016-01-07 15:49:35 +01:00
2019-03-13 19:15:59 +01:00
]