xiaohanqing
2017-08-25 10:30:46 +08:00
"vim8.0
"异步 project 搜索
function! AsyncGrep(keyword, ...)
let file = exists('a:1') ? a:1 : '*.*'
if a:keyword!~'\k' "暂时只支持关键字搜索
return 'incorrect a:keyword'
endif
if file!~'\f'
return 'incorrect file'
endif
if len(getqflist())>0
cgetexpr ""
let w:quickfix_title="searching..."
redraw
endif
let job_opt = {}
let job_opt.out_io = 'buffer'
let job_opt.out_mode = 'nl'
let job_opt.out_name = ''
let job_opt.out_msg = ''
let job_opt.out_cb = ''
let job_opt.err_cb = {job, message -> execute("echom ".string(message), "")}
let job_opt.exit_cb = function({kw, fl, job, status -> execute("
\ if exists('g:grepJobtimer')|
\ call timer_stop(g:grepJobtimer)|
\ endif|
\ let buf_line = getbufline(ch_getbufnr(job, 'out'),0, '$')|
\ let buf_line = filter(buf_line, 'len(v:val)>0')|
\ if len(buf_line)<1|
\ echo 'can not find ".kw." in ".fl."'|
\ return|
\ endif|
\ cgetexpr buf_line|
\ copen|
\ let w:quickfix_title='search ".kw."'|
\ echo 'search finish'|
\ ", "")}, [a:keyword, file])
let job_opt.close_cb = ""
if exists('g:grepJob') && type(g:grepJob)==v:t_job && job_status(g:grepJob)=='run' "防止多任务冲突
call job_stop(g:grepJob)
endif
let job_command = [&shell, &shellcmdflag]
"TODO 兼容更多的搜索工具
let re_option=''
if &grepprg=~'grep'
let re_option = '-r'
elseif &grepprg=~'findstr'
let re_option = '/S'
endif
let job_command += [substitute(&grepprg, '\$\*', '"'.a:keyword.'"', 'g').' '.re_option.' '.file]
let g:grepJob = job_start(job_command, extend(g:defaultJobOpt, job_opt))
if job_status(g:grepJob)=='fail'
return 'search fail'
endif
if exists('g:grepJobtimer')
call timer_stop(g:grepJobtimer)
endif
let g:grepJobtimer = ProgressBar('searching', '.', 3)
return 'search start'
endfunction
"进度条
function! ProgressBar(leadingMsg, marker, num, ...)
let interval = exists('a:1') ? a:1 : 700
return timer_start(interval, function({leadingMsg, maker, num, timer -> execute("
\ if mode(1)==#'R' || mode(1)=='n'|
\ let timer_info=timer_info(timer)|
\ echo leadingMsg.repeat(maker, ((9999*num-timer_info[0].repeat)%num)+1)|
\ endif|
\ ", "")}, [a:leadingMsg, a:marker, a:num]), {'repeat': 9999*a:num})
endfunction!