Skip to content

Commit 9670818

Browse files
committed
Enforce search behavior according to &foldopen
Sneaking now behaves like / when "foldopen-=search" is set: Closed folds are considered to be single targets and are not opened when sneaking to them. They are opened, however, when the default-&foldopen is used or "foldopen+=search" is set. Resolve #102. This does not affect streak-mode (which still ignores &foldopen).
1 parent c6b5708 commit 9670818

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

plugin/sneak.vim

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,19 @@ func! sneak#to(op, input, inputlen, count, repeatmotion, reverse, inclusive, str
152152
let nudge = sneak#util#nudge(!a:reverse) "special case for t
153153
endif
154154

155+
" detect whether folds should be opened during search according to &foldopen
156+
let l:search_foldopen = !empty(matchstr(&foldopen,"search"))
157+
155158
for i in range(1, max([1, skip])) "jump to the [count]th match
159+
" if in a closed fold, jump to end of it for 'set foldopen-=search'
160+
let l:winview = winsaveview()
161+
if !l:search_foldopen && foldclosed('.') > -1
162+
call cursor(a:reverse ? foldclosed('.') : foldclosedend('.'),
163+
\ a:reverse ? 1 : col('$'))
164+
endif
156165
let matchpos = s.dosearch()
157-
if 0 == max(matchpos)
166+
if 0 == max(matchpos) " if no search results found
167+
call winrestview(l:winview) " revert jump to end of fold and break
158168
break
159169
else
160170
let nudge = !a:inclusive
@@ -172,6 +182,11 @@ func! sneak#to(op, input, inputlen, count, repeatmotion, reverse, inclusive, str
172182
endif
173183
"search succeeded
174184

185+
" open folds when jumping to matches if necessary
186+
if l:search_foldopen
187+
norm! zv
188+
endif
189+
175190
call sneak#hl#removehl()
176191

177192
if (!is_op || a:op ==# 'y') "position _after_ search

0 commit comments

Comments
 (0)