I'm writing a script in which I am in need of closure functionalities. Take this function, for instance:
func! SetOpNorm(norm_comm, motion_key = 'y')
let l:norm_comm = a:norm_comm
let l:motion_key = a:motion_key
let s:OpFun = { type, ... ->
\ let l:NormFun = { sel ->
\ sil exe 'normal!' l:norm_comm
\ sil exe "normal! \<del>"
\ }
\ call function('s:MapOverOp', [l:NormFun, l:motion_key, a:type] + a:000)() }
set opfunc=s:OpFun
endfunc
The script would work except for the fact that vimscript closures accept an expr1
as their bodies and not a list of commands. Is there a way to work around this limitation?
I also tried using local functions to achieve the same result:
func! SetOpNorm(norm_comm, motion_key = 'y')
let l:norm_comm = a:norm_comm
let l:motion_key = a:motion_key
func! OpFun(type, ...)
func! NormFun(sel)
sil exe 'normal!' l:norm_comm
sil exe "normal! \<del>"
endfunc
call function('s:MapOverOp', [function('NormFun'), l:motion_key, a:type] + a:000)()
endfunc
set opfunc=OpFun
endfunc
But after some testing I think that this doesn't work as the inner local functions do not capture their sorrounding context.
closure
following the args of the inner function, per:h func-closure
??closure
as in a keyword I don't think so... I'm using neovim btw, mybe some things are different from regular vim:h func-closure
and it looks like exactly what I need. Didn't bother to look before because I had already read:h closure
. The naming might be a bit ambigous XD:func-closure
is linked inclosure
(but the reverse isn't true).