:help g@
doc> *g@* *E774* *E775*
doc> g@{motion} Call the function set by the 'operatorfunc' option.
(snip)
doc>
doc> Here is an example that counts the number of spaces with <F4>: >
doc>
doc> nmap <silent> <F4> :set opfunc=CountSpaces<CR>g@
doc> vmap <silent> <F4> :<C-U>call CountSpaces(visualmode(), 1)<CR>
doc>
doc> function! CountSpaces(type, ...)
doc> let sel_save = &selection
doc> let &selection = "inclusive"
doc> let reg_save = @@
doc>
doc> if a:0 " Invoked from Visual mode, use '< and '> marks.
doc> silent exe "normal! `<" . a:type . "`>y"
doc> elseif a:type == 'line'
doc> silent exe "normal! '[V']y"
doc> elseif a:type == 'block'
doc> silent exe "normal! `[\<C-V>`]y"
doc> else
doc> silent exe "normal! `[v`]y"
doc> endif
doc>
doc> echomsg strlen(substitute(@@, '[^ ]', '', 'g'))
doc>
doc> let &selection = sel_save
doc> let @@ = reg_save
doc> endfunction
This sample script does not work properly when use visual-block-mode with '$'.
How to reproduce:
(0) Prepare test.txt containing the following.
a a
b b b b
cc
(1) Copy to ~/.vimrc above sample script.
(2) Open test.txt
(3) Input below command.
<C-V>G$
(4) Press <F4>
Expected behavior:
- Display '5' on command line.
Actual behavior:
- Display '3'.
I think that we should use 'gv' in this case.
Attached a patch.
PS
Is it necessary condition "elseif a:type == 'block'" ?
Help says unnecessary. (Following excerpt)
doc> Although "block" would rarely appear, since it can
doc> only result from Visual mode where "g@" is not useful.
Thanks.
--
Best regards,
Hirohito Higashi