2016-01-07 17:49:35 +03:00
|
|
|
# http://lua.org
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2021-07-26 23:41:00 +03:00
|
|
|
hook global BufCreate .*[.](lua|rockspec) %{
|
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
|
|
|
|
2020-10-03 01:32:09 +03:00
|
|
|
hook window ModeChange pop:insert:.* -group lua-trim-indent lua-trim-indent
|
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
|
|
|
|
2021-09-09 01:17:17 +03:00
|
|
|
provide-module lua %§
|
2019-03-13 21:15:59 +03:00
|
|
|
|
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
|
2021-09-07 20:46:50 +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
|
|
|
|
2023-09-30 20:57:30 +03:00
|
|
|
add-highlighter shared/lua/code/variable regex \b\w*\b 0:variable # Everything in Lua is a variable!
|
2021-07-29 01:10:43 +03: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 01:48:09 +03:00
|
|
|
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
|
2023-09-30 20:57:30 +03:00
|
|
|
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
|
2021-08-04 19:19:53 +03:00
|
|
|
add-highlighter shared/lua/code/symbolic_operator regex (\+|-|\*|/|%|\^|==?|~=|<=?|>=?|\.\.|\.\.\.|#) 0:operator
|
|
|
|
add-highlighter shared/lua/code/keyword_operator regex \b(and|or|not)\b 0:operator
|
2021-07-29 01:11:56 +03:00
|
|
|
add-highlighter shared/lua/code/module regex \b(_G|_ENV)\b 0:module
|
2021-07-29 01:22:14 +03:00
|
|
|
add-highlighter shared/lua/code/attribute regex \B(<[a-zA-Z_]\w*>)\B 0:attribute
|
2023-09-30 20:57:30 +03:00
|
|
|
add-highlighter shared/lua/code/label regex \s(::\w*::) 1:meta
|
|
|
|
add-highlighter shared/lua/code/goto_label regex "\bgoto (\w*)\b" 1:meta
|
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//))
|
2019-10-08 19:06:54 +03:00
|
|
|
[ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit
|
2016-03-03 20:56:50 +03:00
|
|
|
;;
|
|
|
|
*.lua)
|
2020-08-23 19:55:50 +03:00
|
|
|
altfile=""
|
|
|
|
altdir=""
|
2016-03-03 20:56:50 +03:00
|
|
|
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
|
2020-08-23 19:55:50 +03:00
|
|
|
[ ! -d "$altdir" ] && echo "fail 'spec/ not found'" && exit
|
2016-03-03 20:56:50 +03:00
|
|
|
;;
|
|
|
|
*)
|
2019-10-08 19:06:54 +03:00
|
|
|
echo "fail '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
|
|
|
}}
|
|
|
|
|
2021-09-09 01:17:17 +03:00
|
|
|
define-command -hidden lua-trim-indent %[
|
2020-03-20 19:03:14 +03:00
|
|
|
# remove trailing whitespaces
|
2022-03-17 01:20:07 +03:00
|
|
|
try %[ execute-keys -draft -itersel x s \h+$ <ret> d ]
|
2021-09-09 01:17:17 +03:00
|
|
|
]
|
2020-03-20 19:03:14 +03:00
|
|
|
|
2021-09-09 01:17:17 +03:00
|
|
|
define-command -hidden lua-indent-on-char %[
|
|
|
|
evaluate-commands -no-hooks -draft -itersel %[
|
2021-09-10 07:43:59 +03:00
|
|
|
# unindent middle and end structures
|
|
|
|
try %[ execute-keys -draft \
|
|
|
|
<a-h><a-k>^\h*(\b(end|else|elseif|until)\b|[)}])$<ret> \
|
|
|
|
:lua-indent-on-new-line<ret> \
|
|
|
|
<a-lt>
|
|
|
|
]
|
2021-09-09 01:17:17 +03:00
|
|
|
]
|
|
|
|
]
|
2016-01-07 17:49:35 +03:00
|
|
|
|
2021-09-09 01:17:17 +03:00
|
|
|
define-command -hidden lua-indent-on-new-line %[
|
|
|
|
evaluate-commands -no-hooks -draft -itersel %[
|
2020-10-03 01:32:09 +03:00
|
|
|
# remove trailing white spaces from previous line
|
2021-09-09 01:17:17 +03:00
|
|
|
try %[ execute-keys -draft k : lua-trim-indent <ret> ]
|
2020-03-20 19:03:14 +03:00
|
|
|
# preserve previous non-empty line indent
|
2022-04-15 01:14:17 +03:00
|
|
|
try %[ execute-keys -draft ,gh<a-?>^[^\n]+$<ret>s\A|.\z<ret>)<a-&> ]
|
2021-09-10 07:43:59 +03:00
|
|
|
# add one indentation level if the previous line is not a comment and:
|
|
|
|
# - starts with a block keyword that is not closed on the same line,
|
|
|
|
# - or contains an unclosed function expression,
|
|
|
|
# - or ends with an enclosed '(' or '{'
|
|
|
|
try %[ execute-keys -draft \
|
2022-04-15 01:14:17 +03:00
|
|
|
, Kx \
|
2021-09-10 07:43:59 +03:00
|
|
|
<a-K>\A\h*--<ret> \
|
|
|
|
<a-K>\A[^\n]*\b(end|until)\b<ret> \
|
|
|
|
<a-k>\A(\h*\b(do|else|elseif|for|function|if|repeat|while)\b|[^\n]*[({]$|[^\n]*\bfunction\b\h*[(])<ret> \
|
|
|
|
<a-:><semicolon><a-gt>
|
|
|
|
]
|
2021-09-09 01:17:17 +03:00
|
|
|
]
|
|
|
|
]
|
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
|
2022-03-17 01:20:07 +03:00
|
|
|
try %[ execute-keys -draft kxs^\h*\K--\h*<ret> y gh j x<semicolon> P ]
|
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 %[
|
2021-09-11 06:51:12 +03:00
|
|
|
# save previous line indent in register x
|
2022-03-17 01:20:07 +03:00
|
|
|
try %[ execute-keys -draft kxs^\h+<ret>"xy ] catch %[ reg x '' ]
|
2021-09-11 06:51:12 +03:00
|
|
|
try %[
|
|
|
|
# check that starts with a block keyword that is not closed on the same line
|
|
|
|
execute-keys -draft \
|
2022-03-17 01:20:07 +03:00
|
|
|
kx \
|
2021-09-11 06:51:12 +03:00
|
|
|
<a-k>^\h*\b(else|elseif|do|for|function|if|while)\b|[^\n]\bfunction\b\h*[(]<ret> \
|
|
|
|
<a-K>\bend\b<ret>
|
|
|
|
# check that the block is empty and is not closed on a different line
|
2022-03-17 01:20:07 +03:00
|
|
|
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>
|
2021-09-11 06:51:12 +03:00
|
|
|
# auto insert end
|
|
|
|
execute-keys -draft o<c-r>xend<esc>
|
|
|
|
# auto insert ) for anonymous function
|
2022-03-17 01:20:07 +03:00
|
|
|
execute-keys -draft kx<a-k>\([^)\n]*function\b<ret>jjA)<esc>
|
2021-09-11 06:51:12 +03:00
|
|
|
]
|
2018-03-04 02:35:38 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2016-01-07 17:49:35 +03:00
|
|
|
|
2021-09-09 01:17:17 +03:00
|
|
|
§
|