2016-01-07 15:49:35 +01:00
# http://lua.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# require commenting.kak
# Detection
# ‾‾‾‾‾‾‾‾‾
hook global BufSetOption mimetype=text/x-lua %{
set buffer filetype lua
}
hook global BufCreate .*[.](lua) %{
2016-03-03 18:55:03 +01:00
set buffer mimetype text/x-lua
2016-01-07 15:49:35 +01:00
}
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
addhl -group / regions -default code lua \
string '"' (?<!\\)(\\\\)*" '' \
string "'" (?<!\\)(\\\\)*' '' \
comment '--' '$' '' \
comment '\Q--[[' ']]' '' \
addhl -group /lua/string fill string
addhl -group /lua/comment fill comment
2016-04-21 21:43:17 +02:00
addhl -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 15:49:35 +01:00
# Commands
# ‾‾‾‾‾‾‾‾
2016-03-03 18:56:50 +01:00
def lua-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ %sh{
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//))
[ ! -f $altfile ] && echo "echo -color Error 'implementation file not found'" && exit
2016-03-03 18:56:50 +01:00
;;
*.lua)
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
2016-04-23 07:47:01 +02:00
[ ! -d $altdir ] && echo "echo -color Error 'spec/ not found'" && exit
2016-03-03 18:56:50 +01:00
;;
*)
2016-04-23 07:47:01 +02:00
echo "echo -color Error '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
}}
2016-01-07 15:49:35 +01:00
def -hidden _lua_filter_around_selections %{
eval -draft -itersel %{
exec <a-x>
# remove trailing white spaces
try %{ exec -draft s \h + $ <ret> d }
}
}
def -hidden _lua_indent_on_char %{
eval -draft -itersel %{
# align end structure to start
try %{ exec -draft <a-x> <a-k> ^ \h * end $ <ret> <a-\;> <a-?> ^ \h * (for|function|if|while) <ret> s \A | \Z <ret> \' <a-&> }
# align _else_ statements to previous _if_
try %{ exec -draft <a-x> <a-k> ^ \h * else (if) ? $ <ret> <a-\;> <a-?> ^ \h * if <ret> s \A | \Z <ret> \' <a-&> }
}
}
def -hidden _lua_indent_on_new_line %{
eval -draft -itersel %{
# preserve previous line indent
try %{ exec -draft K <a-&> }
# filter previous line
try %{ exec -draft k : _lua_filter_around_selections <ret> }
# copy -- comment prefix and following white spaces
try %{ exec -draft k x s ^ \h * \K -- \h * <ret> y j p }
# indent after start structure
try %{ exec -draft k x <a-k> ^ \h * (else|elseif|for|function|if|while) \b <ret> j <a-gt> }
# wisely add end structure
eval -save-regs x %{
try %{ exec -draft k x s ^ \h + <ret> \" x y } catch %{ reg x '' }
try %{ exec -draft k x <a-k> ^ <c-r> x (for|function|if|while) <ret> j <a-a> i X <a-\;> K <a-K> ^ <c-r> x (for|function|if|while) . * \n <c-r> x end $ <ret> j x y p j a end <esc> <a-lt> }
}
}
}
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
2016-09-25 15:15:07 +02:00
hook -group lua-highlight global WinSetOption filetype=lua %{ addhl ref lua }
2016-01-07 15:49:35 +01:00
2016-09-25 15:15:07 +02:00
hook global WinSetOption filetype=lua %{
2016-01-07 15:49:35 +01:00
hook window InsertEnd .* -group lua-hooks _lua_filter_around_selections
hook window InsertChar .* -group lua-indent _lua_indent_on_char
hook window InsertChar \n -group lua-indent _lua_indent_on_new_line
2016-03-03 18:56:50 +01:00
alias window alt lua-alternative-file
2016-01-07 15:49:35 +01:00
set window comment_line_chars '--'
set window comment_selection_chars '\Q--[[:]]'
}
2016-09-28 08:45:01 +02:00
hool -group lua-highlight global WinSetOption filetype=(?!lua).* %{ rmhl lua }
2016-01-07 15:49:35 +01:00
hook global WinSetOption filetype=(?!lua).* %{
rmhooks window lua-indent
rmhooks window lua-hooks
2016-03-03 18:56:50 +01:00
unalias window alt lua-alternative-file
2016-01-07 15:49:35 +01:00
}