2016-01-07 17:49:35 +03:00
# http://lua.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# Detection
# ‾‾‾‾‾‾‾‾‾
hook global BufCreate .*[.](lua) %{
2017-11-03 10:34:41 +03:00
set-option buffer filetype lua
2016-01-07 17:49:35 +03:00
}
2019-04-10 06:54:19 +03:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=lua %{
2019-03-13 21:15:59 +03:00
require-module lua
2019-04-10 06:54:19 +03: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 21:15:59 +03:00
}
2019-04-10 06:54:19 +03:00
2019-03-13 21:15:59 +03:00
provide-module lua %[
2016-01-07 17:49:35 +03:00
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2018-06-28 15:10:22 +03:00
add-highlighter shared/lua regions
add-highlighter shared/lua/code default-region group
2018-11-12 23:38:13 +03: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 13:59:12 +03: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 17:49:35 +03:00
2018-06-28 15:10:22 +03:00
add-highlighter shared/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
# ‾‾‾‾‾‾‾‾
2018-05-07 00:29:52 +03:00
define-command lua-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ evaluate-commands %sh{
2016-03-03 20:56:50 +03:00
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-11-03 10:34:41 +03:00
define-command -hidden lua-indent-on-char %{
2017-11-03 11:09:45 +03:00
evaluate-commands -no-hooks -draft -itersel %{
2016-11-15 23:23:46 +03:00
# align middle and end structures to start and indent when necessary, elseif is already covered by else
2018-03-25 03:45:27 +03:00
try %{ execute-keys -draft <a-x><a-k>^\h*(else)$<ret><a-\;><a-?>^\h*(if)<ret>s\A|\z<ret>)<a-&> }
try %{ execute-keys -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-11-03 10:34:41 +03:00
define-command -hidden lua-indent-on-new-line %{
2017-11-03 11:09:45 +03:00
evaluate-commands -no-hooks -draft -itersel %{
2016-01-07 17:49:35 +03:00
# preserve previous line indent
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft <space>K<a-&> }
2018-03-04 02:35:38 +03:00
# remove trailing white spaces from previous line
try %{ execute-keys -draft k<a-x>s\h+$<ret>d }
2016-01-07 17:49:35 +03:00
# indent after start structure
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft k<a-x><a-k>^\h*(else|elseif|for|function|if|while)\b<ret>j<a-gt> }
2016-11-15 23:23:46 +03:00
}
}
2018-03-04 02:35:38 +03:00
define-command -hidden lua-insert-on-new-line %[
evaluate-commands -no-hooks -draft -itersel %[
2016-11-15 23:23:46 +03:00
# copy -- comment prefix and following white spaces
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft k<a-x>s^\h*\K--\h*<ret>yghjP }
2016-01-07 17:49:35 +03:00
# wisely add end structure
2018-03-04 02:35:38 +03: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
try %[ execute-keys -draft k<a-x> <a-k>^<c-r>x(for|function|if|while)<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
]
]
]
2016-01-07 17:49:35 +03:00
2019-03-13 21:15:59 +03:00
]