I'm writing what may become a plugin that needs to know what is currently visually selected.
To do it, I'm using the autocmd CursorMoved
event to detect the creation of
visual mode selections on the fly. So far, it works well enough, but for some
reason if I make a visual selection using vip
or vap
this isn't picked up
by the autocmd even though the cursor moves position.
Edit!
I think this is because in my code below I only check for mode() == 'v'
(i.e.
is visual mode character-wise) and vip
could be a line-wise visual selection.
...continued OP
Other text objects (like vi]
, viw
and the like) work fine as do regular
motions (like v$
v}
).
I wonder if perhaps it's a bug in Vim? If not, what is the correct way to
detect that vip
has been executed in visual mode? I can't find any other
autocmd that does the job.
Here are the relevant sections of code if it helps:
function! Update_visual_mark_list()
if mode() ==# 'v'
" exit visual mode
normal! v
" get end points of last visual selection
let [_, l1, c1, _] = getpos("'<")
let [_, l2, c2, _] = getpos("'>")
" do some stuff
" go back to last visual selection
normal! gv
endif
endfunction
augroup visual_walk
autocmd!
autocmd CursorMoved * call Update_visual_mark_list()
augroup END