vim9scriptvim9
const MAXCOUNT = 1000
const TIMEOUT = 500
augroup index_after_slash | au!
au CmdlineLeave /,\? IndexAfterSlash()
augroup END
def IndexAfterSlash()
if getcmdline() == '' || state() =~ 'm'
return
endif
timer_start(0, {->() => mode() =~ '[nv]' ? SearchIndex() : 0})
enddef
def SearchIndex(): string
var incomplete: number
var total: number
var current: number
var result: dict<any>
try
result = searchcount({maxcount: MAXCOUNT, timeout: TIMEOUT})
current = result.current
total = result.total
incomplete = result.incomplete
catch
echohl ErrorMsg | echom v:exception | echohl NONE
return ''
endtry
var msg = ''
var pat = getreg('/')->substitute('\%x00', '^@', 'g')
if incomplete == 0
msg = printf('[%*d/%d] %s', len(total), current, total, pat)
elseif incomplete == 1 # recomputing took too much time
msg = printf('[?/??] %s', pat)
elseif incomplete == 2 # too many matches
if result.total == (result.maxcount + 1) && result.current <= result.maxcount
msg = printf('[%*d/>%d] %s', len(total - 1), current, total - 1, pat)
else
msg = printf('[>%*d/>%d] %s', len(total - 1), current - 1, total - 1, pat)
endif
endif
if strchars(msg, 1) > (v:echospace + (&cmdheight - 1) * &columns)
var n = v:echospace - 3
var n1 = n % 2 ? n / 2 : n / 2 - 1
var n2 = n / 2
var matchlist = matchlist(msg, '\(.\{' .. n1 .. '}\).*\(.\{' .. n2 .. '}\)')
msg = matchlist[1] .. '...' .. matchlist[2]
endif
echo msg
return ''
enddef
nmap n <plug>(n)<plug>(search_index)
nmap N <plug>(N)<plug>(search_index)
nno <plug>(n) n
nno <plug>(N) N
nno <expr> <plug>(search_index) <sid>SearchIndex()
This requires a recent Vim version. It works on 8.2.21592332.