I have a simple custom operator, it echoes v:register
, it's mapped to ,f
. After reading this, i creat it like this :
function! s:foo(type,...)
echo v:register
endfunction
function! s:setupOpfunc(func)
let &opfunc = a:func
return 'g@'
endfunction
nnoremap <expr> ,f <sid>setupOpfunc('<sid>foo')
It wors fine on built in textobjects, e.g. "x,faw
output
x
But it doesn't work on custom textobject. Assume i have this simple custom text object:
vnoremap ix iw
" I know you can map to `iw` directly, but that's not the point of this problem.
onoremap ix :normal vix<cr>
"x,fix
output
"
I think it doesn't work because :normal ...
erased v:resiter
. I tried to use onoremap <expr> ...
to create omap
for textobject, but :h map-expression says :normal
( needed to visually select text object ) is blocked during it's expression evaluation.
Built in operator has no this kind of problem "xyix
happily copy word into register x
.
One crude way i know to make this work is to use some global variable:
function! s:setupOpfunc(func)
let g:__opfunc_rgister = v:register
...
endfunction
But it doesn't feel right, are there any other ways to make this work?
:onoremap ix :execute '"' . v:register . "normal vix"<cr>
will preserve the registers. However, I looked up a popular text objects package (wellle/targets.vim) and noticed it doesn't preservev:register
across invocations, so I don't think that's the answer... This is a great question, also curious to figure out what the right answer to it is!"
is start of comment in command line, so"xnormal vix
is no different form"xbalabalabala
, it's ignored,`]
won't be set to the right value inopfunc
.