Skip to main content
type-check everything
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
vim9script

const MAXCOUNT: number = 10001'000
const TIMEOUT: number = 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: string = ''
    var pat: string = 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: number = v:echospace - 3
        var n1: number = n % 2 ? n / 2 : n / 2 - 1
        var n2: number = n / 2
        var matchlist: list<string> = 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()
vim9script

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()
vim9script

const MAXCOUNT: number = 1'000
const TIMEOUT: number = 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: string = ''
    var pat: string = 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: number = v:echospace - 3
        var n1: number = n % 2 ? n / 2 : n / 2 - 1
        var n2: number = n / 2
        var matchlist: list<string> = 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()
added 6 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
vim9vim9script

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()
vim9

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()
vim9script

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()
deleted 5 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
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.

vim9script

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.2159.

vim9

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.2332.

added 2 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
added 61 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
deleted 33 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
added 2310 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
added 1 character in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
added 233 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
added 11 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
deleted 30 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
edited body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
deleted 120 characters in body
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading
Source Link
user938271
  • 6k
  • 1
  • 16
  • 24
Loading