diff --git a/plugin/slash.vim b/plugin/slash.vim index 6b99444..efecf7b 100644 --- a/plugin/slash.vim +++ b/plugin/slash.vim @@ -29,15 +29,60 @@ function! s:wrap(seq) return a:seq."\(slash-trailer)" endfunction +function! s:set_repeated_move_type(seq) + " Vmaps work by replacing * and # with yank text then search the underlying + " word. We have to recognize these modes in order to override * and # keys + " in normal mode later, remapping then to n or N as desired. + + if mode() == 'v' && a:seq[0:1] ==# 'y/' + let b:slash_repeated_move = 'visual_forward' + elseif mode() == 'v' && a:seq[0:1] ==# 'y?' + let b:slash_repeated_move = 'visual_backward' + else + let b:slash_repeated_move = 'normal' + endif +endfunction + +function! s:revert_search_direction(key) + if a:key ==# 'n' + return 'N' + elseif a:key ==# 'N' + return 'n' + elseif + return a:key + endif +endfunction + +function! s:star_to_forward_backward(key) + if a:key ==# '*' + return 'n' + elseif a:key ==# '#' + return 'N' + else + return a:key + endif +endfunction + function! s:immobile(seq) + let repeated_move = get(b:, 'slash_repeated_move', '') + + if repeated_move ==# 'normal' + return a:seq + elseif repeated_move ==# 'visual_forward' + return s:star_to_forward_backward(a:seq) + elseif repeated_move ==# 'visual_backward' + return s:revert_search_direction(s:star_to_forward_backward(a:seq)) + endif + let s:winline = winline() + call s:set_repeated_move_type(a:seq) return a:seq."\(slash-prev)" endfunction function! s:trailer() augroup slash autocmd! - autocmd CursorMoved,CursorMovedI * set nohlsearch | autocmd! slash + autocmd CursorMoved,CursorMovedI * set nohlsearch | let b:slash_repeated_move = '' | autocmd! slash augroup END let seq = foldclosed('.') != -1 ? 'zo' : ''