0

I am trying to curry a function in Vimscript to map an operator. I have a function written in Lua, and I want to pass 'n' or 'v' to that function, depending on the mode that trigger that mapping.

function! Commented(mode) abort let Callback = function("Toggle_commented", [a:mode]) set opfunc=Callback return 'g@' endfunction function! Toggle_commented(type) abort execute "lua require('commented').toggle_comment()" endfunction 

But right now when I run this code, it will return E117: Unknown function: Callback. Why is that? how can I fix this error?

0

1 Answer 1

2

Afaik, opfunc requires literal function name not the variable name holding function reference.

If what you want is to simplify opfunc creation there is another way:

func! ToggleComment(type = '') if a:type == '' " HERE you set opfunc to ToggleComment let &opfunc = matchstr(expand('<sfile>'), '[^. ]*$') return 'g@' endif " Do your ToggleCommenting stuff here " execute "lua require('commented').toggle_comment()" endfunc nnoremap <silent> <expr> <space>c ToggleComment() 

Check let &opfunc = matchstr(expand('<sfile>'), '[^. ]*$') line. Here expand('<sfile>') would be function ToggleComment and with matchstr you extract function name.

Here for example my operator for vimscript evaluation:

" Essential for my vimscripting " run selected vimscript xnoremap <silent> <space>v y:@"<cr> " run vimscript line nmap <space>vv V<space>v " run operator func! s:vimrun(type = '') if a:type == '' let &opfunc = matchstr(expand('<sfile>'), '[^. ]*$') return 'g@' endif let commands = #{line: "'[V']y", char: "`[v`]y", block: "`[\<c-v>`]y"} silent exe "noautocmd keepjumps normal! " .. get(commands, a:type, '') @" endfunc nnoremap <silent> <expr> <space>v <SID>vimrun() 

With <space>vip I can evaluate paragraph as vimscript...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.