@@ -29,20 +29,60 @@ function! s:wrap(seq)
2929 return a: seq ." \<plug> (slash-trailer)"
3030endfunction
3131
32+ function ! s: set_repeated_move_type (seq)
33+ " Vmaps work by replacing * and # with yank text then search the underlying
34+ " word. We have to recognize these modes in order to override * and # keys
35+ " in normal mode later, remapping then to n or N as desired.
36+
37+ if mode () == ' v' && a: seq [0 :1 ] == # ' y/'
38+ let b: slash_repeated_move = ' visual_forward'
39+ elseif mode () == ' v' && a: seq [0 :1 ] == # ' y?'
40+ let b: slash_repeated_move = ' visual_backward'
41+ else
42+ let b: slash_repeated_move = ' normal'
43+ endif
44+ endfunction
45+
46+ function ! s: revert_search_direction (key )
47+ if a: key == # ' n'
48+ return ' N'
49+ elseif a: key == # ' N'
50+ return ' n'
51+ elseif
52+ return a: key
53+ endif
54+ endfunction
55+
56+ function ! s: star_to_forward_backward (key )
57+ if a: key == # ' *'
58+ return ' n'
59+ elseif a: key == # ' #'
60+ return ' N'
61+ else
62+ return a: key
63+ endif
64+ endfunction
65+
3266function ! s: immobile (seq)
33- if exists (' b:slash_repeated_move' )
67+ let repeated_move = get (b: , ' slash_repeated_move' , ' ' )
68+
69+ if repeated_move == # ' normal'
3470 return a: seq
71+ elseif repeated_move == # ' visual_forward'
72+ return s: star_to_forward_backward (a: seq )
73+ elseif repeated_move == # ' visual_backward'
74+ return s: revert_search_direction (s: star_to_forward_backward (a: seq ))
3575 endif
3676
3777 let s: winline = winline ()
38- let b: slash_repeated_move = 1
78+ call s: set_repeated_move_type ( a: seq )
3979 return a: seq ." \<plug> (slash-prev)"
4080endfunction
4181
4282function ! s: trailer ()
4383 augroup slash
4484 autocmd !
45- autocmd CursorMoved ,CursorMovedI * set nohlsearch | unlet ! b: slash_repeated_move | autocmd ! slash
85+ autocmd CursorMoved ,CursorMovedI * set nohlsearch | let b: slash_repeated_move = ' ' | autocmd ! slash
4686 augroup END
4787
4888 let seq = foldclosed (' .' ) != -1 ? ' zo' : ' '
0 commit comments