是否有可能在运算符挂起的映射中确定运动是线性,块状还是正常?

Ben*_*oit 8 vim

当声明映射时,omap或者onoremap我希望能够处理运动将是块状,线性或标准的情况.

例如,让我们考虑以下块:

abcd
efgh
ijkl
mnop
Run Code Online (Sandbox Code Playgroud)

光标在字母f上.假设我定义从操作映射K:normal! vjl(去字母k).

onoremap K :normal! vjl<cr>
Run Code Online (Sandbox Code Playgroud)

有趣的是,当我跑步dvKdK,我分别得到了d^VK

abcd   abcd   abcd
el     el     eh
mnop   mnop   il
              mnop
Run Code Online (Sandbox Code Playgroud)

但是当我运行dVK它不起作用时,我会完全一样dvK.

我尝试使用visualmode()(映射定义为@=visualmode()<cr>jl<cr>但不起作用.看起来这个函数的返回值在使用时不会立即受到影响v,V或者在运算符挂起模式下不会立即影响CTRL-V.

有人有线索吗?
谢谢

seh*_*ehe 0

我已经写了一些关于操作员待处理映射的答案。在其中之一,我根据文档勾画了一个应该处理各种情况(字符、行、块明智选择)的函数的轮廓:

 g@{motion}     Call the function set by the 'operatorfunc' option.
        The '[ mark is positioned at the start of the text
        moved over by {motion}, the '] mark on the last
        character of the text.
        The function is called with one String argument:
            "line"  {motion} was |linewise|
            "char"  {motion} was |characterwise|
            "block" {motion} was |blockwise-visual|
        Although "block" would rarely appear, since it can
        only result from Visual mode where "g@" is not useful.
        {not available when compiled without the |+eval|
        feature}
Run Code Online (Sandbox Code Playgroud)

下面是一个使用 来计算空格数量的示例<F4>:>

nmap <silent> <F4> :set opfunc=CountSpaces<CR>g@
vmap <silent> <F4> :<C-U>call CountSpaces(visualmode(), 1)<CR>

function! CountSpaces(type, ...)
  let sel_save = &selection
  let &selection = "inclusive"
  let reg_save = @@

  if a:0  " Invoked from Visual mode, use '< and '> marks.
    silent exe "normal! `<" . a:type . "`>y"
  elseif a:type == 'line'
    silent exe "normal! '[V']y"
  elseif a:type == 'block'
    silent exe "normal! `[\<C-V>`]y"
  else
    silent exe "normal! `[v`]y"
  endif

  echomsg strlen(substitute(@@, '[^ ]', '', 'g'))

  let &selection = sel_save
  let @@ = reg_save
endfunction
Run Code Online (Sandbox Code Playgroud)

1 vim 从 vmap 内部调用函数