2016-01-07 17:49:35 +03:00
|
|
|
# http://lua.org
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global BufCreate .*[.](lua) %{
|
2016-11-15 23:23:46 +03:00
|
|
|
set buffer filetype lua
|
2016-01-07 17:49:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Highlighters
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-01-04 03:07:45 +03:00
|
|
|
add-highlighter -group / regions -default code lua \
|
2016-01-07 17:49:35 +03:00
|
|
|
string '"' (?<!\\)(\\\\)*" '' \
|
|
|
|
string "'" (?<!\\)(\\\\)*' '' \
|
|
|
|
comment '--' '$' '' \
|
|
|
|
comment '\Q--[[' ']]' '' \
|
|
|
|
|
2017-01-04 03:07:45 +03:00
|
|
|
add-highlighter -group /lua/string fill string
|
2016-01-07 17:49:35 +03:00
|
|
|
|
2017-01-04 03:07:45 +03:00
|
|
|
add-highlighter -group /lua/comment fill comment
|
2016-01-07 17:49:35 +03:00
|
|
|
|
2017-01-04 03:07:45 +03:00
|
|
|
add-highlighter -group /lua/code regex \b(and|break|do|else|elseif|end|false|for|function|goto|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b 0:keyword
|
2016-01-07 17:49:35 +03:00
|
|
|
|
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2016-03-03 20:56:50 +03:00
|
|
|
def lua-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ %sh{
|
|
|
|
case $kak_buffile in
|
|
|
|
*spec/*_spec.lua)
|
2016-04-23 08:47:01 +03:00
|
|
|
altfile=$(eval printf %s\\n $(printf %s\\n $kak_buffile | sed s+spec/+'*'/+';'s/_spec//))
|
2017-07-19 18:18:52 +03:00
|
|
|
[ ! -f $altfile ] && echo "echo -markup '{Error}implementation file not found'" && exit
|
2016-03-03 20:56:50 +03:00
|
|
|
;;
|
|
|
|
*.lua)
|
|
|
|
path=$kak_buffile
|
2016-04-23 08:47:01 +03:00
|
|
|
dirs=$(while [ $path ]; do printf %s\\n $path; path=${path%/*}; done | tail -n +2)
|
2016-03-03 20:56:50 +03:00
|
|
|
for dir in $dirs; do
|
|
|
|
altdir=$dir/spec
|
|
|
|
if [ -d $altdir ]; then
|
2016-03-08 22:33:21 +03:00
|
|
|
altfile=$altdir/$(realpath $kak_buffile --relative-to $dir | sed s+[^/]'*'/++';'s/.lua$/_spec.lua/)
|
2016-03-03 20:56:50 +03:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2017-07-19 18:18:52 +03:00
|
|
|
[ ! -d $altdir ] && echo "echo -markup '{Error}spec/ not found'" && exit
|
2016-03-03 20:56:50 +03:00
|
|
|
;;
|
|
|
|
*)
|
2017-07-19 18:18:52 +03:00
|
|
|
echo "echo -markup '{Error}alternative file not found'" && exit
|
2016-03-03 20:56:50 +03:00
|
|
|
;;
|
|
|
|
esac
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "edit $altfile"
|
2016-03-03 20:56:50 +03:00
|
|
|
}}
|
|
|
|
|
2017-01-13 03:56:30 +03:00
|
|
|
def -hidden lua-filter-around-selections %{
|
2016-11-15 23:23:46 +03:00
|
|
|
eval -no-hooks -draft -itersel %{
|
2016-01-07 17:49:35 +03:00
|
|
|
# remove trailing white spaces
|
2016-11-15 23:23:46 +03:00
|
|
|
try %{ exec -draft <a-x>s\h+$<ret>d }
|
2016-01-07 17:49:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 03:56:30 +03:00
|
|
|
def -hidden lua-indent-on-char %{
|
2016-11-15 23:23:46 +03:00
|
|
|
eval -no-hooks -draft -itersel %{
|
|
|
|
# align middle and end structures to start and indent when necessary, elseif is already covered by else
|
|
|
|
try %{ exec -draft <a-x><a-k>^\h*(else)$<ret><a-\;><a-?>^\h*(if)<ret>s\A|\Z<ret>'<a-&> }
|
|
|
|
try %{ exec -draft <a-x><a-k>^\h*(end)$<ret><a-\;><a-?>^\h*(for|function|if|while)<ret>s\A|\Z<ret>'<a-&> }
|
2016-01-07 17:49:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 03:56:30 +03:00
|
|
|
def -hidden lua-indent-on-new-line %{
|
2016-11-15 23:23:46 +03:00
|
|
|
eval -no-hooks -draft -itersel %{
|
2016-01-07 17:49:35 +03:00
|
|
|
# preserve previous line indent
|
2016-11-15 23:23:46 +03:00
|
|
|
try %{ exec -draft <space>K<a-&> }
|
2016-01-07 17:49:35 +03:00
|
|
|
# filter previous line
|
2017-01-13 03:56:30 +03:00
|
|
|
try %{ exec -draft k:lua-filter-around-selections<ret> }
|
2016-01-07 17:49:35 +03:00
|
|
|
# indent after start structure
|
2016-11-15 23:23:46 +03:00
|
|
|
try %{ exec -draft k<a-x><a-k>^\h*(else|elseif|for|function|if|while)\b<ret>j<a-gt> }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 03:56:30 +03:00
|
|
|
def -hidden lua-insert-on-new-line %{
|
2016-11-15 23:23:46 +03:00
|
|
|
eval -no-hooks -draft -itersel %{
|
|
|
|
# copy -- comment prefix and following white spaces
|
|
|
|
try %{ exec -draft k<a-x>s^\h*\K--\h*<ret>yjp }
|
2016-01-07 17:49:35 +03:00
|
|
|
# wisely add end structure
|
|
|
|
eval -save-regs x %{
|
2016-11-15 23:23:46 +03:00
|
|
|
try %{ exec -draft k<a-x>s^\h+<ret>"xy } catch %{ reg x '' }
|
|
|
|
try %{ exec -draft k<a-x><a-k>^<c-r>x(for|function|if|while)<ret>j<a-a>iX<a-\;>K<a-K>^<c-r>x(for|function|if|while).*\n<c-r>x(else|end|elseif[^\n]*)$<ret>jxypjaend<esc><a-lt> }
|
2016-01-07 17:49:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-01-04 03:07:45 +03:00
|
|
|
hook -group lua-highlight global WinSetOption filetype=lua %{ add-highlighter ref lua }
|
2016-01-07 17:49:35 +03:00
|
|
|
|
2016-09-25 16:15:07 +03:00
|
|
|
hook global WinSetOption filetype=lua %{
|
2017-01-13 03:56:30 +03:00
|
|
|
hook window InsertChar .* -group lua-indent lua-indent-on-char
|
|
|
|
hook window InsertChar \n -group lua-insert lua-insert-on-new-line
|
|
|
|
hook window InsertChar \n -group lua-indent lua-indent-on-new-line
|
2016-01-07 17:49:35 +03:00
|
|
|
|
2016-03-03 20:56:50 +03:00
|
|
|
alias window alt lua-alternative-file
|
2016-01-07 17:49:35 +03:00
|
|
|
}
|
|
|
|
|
2017-01-04 03:07:45 +03:00
|
|
|
hook -group lua-highlight global WinSetOption filetype=(?!lua).* %{ remove-highlighter lua }
|
2016-09-28 09:45:01 +03:00
|
|
|
|
2016-01-07 17:49:35 +03:00
|
|
|
hook global WinSetOption filetype=(?!lua).* %{
|
2017-01-04 03:07:45 +03:00
|
|
|
remove-hooks window lua-indent
|
|
|
|
remove-hooks window lua-insert
|
2016-03-03 20:56:50 +03:00
|
|
|
|
|
|
|
unalias window alt lua-alternative-file
|
2016-01-07 17:49:35 +03:00
|
|
|
}
|