2018-01-22 23:49:27 +08:00
|
|
|
scriptencoding utf-8
|
2017-07-06 21:59:09 +08:00
|
|
|
let s:MPT = SpaceVim#api#import('prompt')
|
|
|
|
let s:JOB = SpaceVim#api#import('job')
|
2017-07-08 16:46:53 +08:00
|
|
|
let s:SYS = SpaceVim#api#import('system')
|
2018-01-21 23:41:07 +08:00
|
|
|
let s:BUFFER = SpaceVim#api#import('vim#buffer')
|
2017-07-06 21:59:09 +08:00
|
|
|
let s:grepid = 0
|
2017-12-30 19:58:10 +08:00
|
|
|
let s:MPT._prompt.mpt = '➭ '
|
2017-07-06 21:59:09 +08:00
|
|
|
|
2018-01-21 23:41:07 +08:00
|
|
|
" keys:
|
|
|
|
" files: files for grep, @buffers means listed buffer.
|
2018-01-22 19:40:32 +08:00
|
|
|
" dir: specific a directory for grep
|
2018-01-21 23:10:46 +08:00
|
|
|
function! SpaceVim#plugins#flygrep#open(agrv) abort
|
2018-01-25 20:58:21 +08:00
|
|
|
noautocmd rightbelow split __flygrep__
|
2017-12-05 20:49:16 +08:00
|
|
|
setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber
|
|
|
|
let save_tve = &t_ve
|
|
|
|
setlocal t_ve=
|
|
|
|
" setlocal nomodifiable
|
|
|
|
setf SpaceVimFlyGrep
|
|
|
|
redraw!
|
2018-01-21 23:10:46 +08:00
|
|
|
let s:MPT._prompt.begin = get(a:agrv, 'input', '')
|
2018-01-21 23:41:07 +08:00
|
|
|
let fs = get(a:agrv, 'files', '')
|
|
|
|
if fs ==# '@buffers'
|
|
|
|
let s:grep_files = map(s:BUFFER.listed_buffers(), 'bufname(v:val)')
|
2018-01-22 00:14:44 +08:00
|
|
|
elseif !empty(fs)
|
|
|
|
let s:grep_files = fs
|
|
|
|
else
|
|
|
|
let s:grep_files = ''
|
2018-01-21 23:41:07 +08:00
|
|
|
endif
|
2018-01-22 20:17:52 +08:00
|
|
|
let dir = expand(get(a:agrv, 'dir', ''))
|
2018-01-22 19:40:32 +08:00
|
|
|
if !empty(dir) && isdirectory(dir)
|
|
|
|
let s:grep_dir = dir
|
|
|
|
else
|
|
|
|
let s:grep_dir = ''
|
|
|
|
endif
|
2018-01-22 22:17:52 +08:00
|
|
|
let s:grep_exe = get(a:agrv, 'cmd', s:grep_default_exe)
|
|
|
|
let s:grep_opt = get(a:agrv, 'opt', s:grep_default_opt)
|
|
|
|
let s:grep_ropt = get(a:agrv, 'ropt', s:grep_default_ropt)
|
2017-12-05 20:49:16 +08:00
|
|
|
call s:MPT.open()
|
|
|
|
let &t_ve = save_tve
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
2017-07-07 20:47:26 +08:00
|
|
|
let s:grep_expr = ''
|
2018-01-22 22:17:52 +08:00
|
|
|
let [s:grep_default_exe, s:grep_default_opt, s:grep_default_ropt] = SpaceVim#mapping#search#default_tool()
|
2017-07-07 20:47:26 +08:00
|
|
|
let s:grep_timer_id = 0
|
|
|
|
|
2017-07-07 21:05:28 +08:00
|
|
|
" @vimlint(EVL103, 1, a:timer)
|
2017-07-07 20:47:26 +08:00
|
|
|
function! s:grep_timer(timer) abort
|
2018-01-22 23:19:02 +08:00
|
|
|
let cmd = s:get_search_cmd(join(split(s:grep_expr), '.*'))
|
2018-01-22 22:08:21 +08:00
|
|
|
call SpaceVim#logger#info('grep cmd: ' . string(cmd))
|
|
|
|
let s:grepid = s:JOB.start(cmd, {
|
2017-12-05 20:49:16 +08:00
|
|
|
\ 'on_stdout' : function('s:grep_stdout'),
|
2018-01-22 20:17:52 +08:00
|
|
|
\ 'on_stderr' : function('s:grep_stderr'),
|
2017-12-05 20:49:16 +08:00
|
|
|
\ 'in_io' : 'null',
|
|
|
|
\ 'on_exit' : function('s:grep_exit'),
|
|
|
|
\ })
|
2017-07-07 20:47:26 +08:00
|
|
|
endfunction
|
2017-07-07 21:05:28 +08:00
|
|
|
" @vimlint(EVL103, 0, a:timer)
|
2018-01-25 19:16:59 +08:00
|
|
|
let s:filter_file = ''
|
|
|
|
function! s:start_filter() abort
|
|
|
|
let s:MPT._handle_fly = function('s:filter')
|
|
|
|
let s:MPT._prompt = {
|
|
|
|
\ 'mpt' : s:MPT._prompt.mpt,
|
|
|
|
\ 'begin' : '',
|
|
|
|
\ 'cursor' : '',
|
|
|
|
\ 'end' : '',
|
|
|
|
\ }
|
|
|
|
let s:filter_file = tempname()
|
|
|
|
try
|
|
|
|
call writefile(getbufline('%', 1, '$'), s:filter_file, 'b')
|
|
|
|
catch
|
|
|
|
call SpaceVim#logger#info('FlyGrep: Failed to write filter content to temp file')
|
|
|
|
endtry
|
2018-01-25 19:37:53 +08:00
|
|
|
call s:MPT._build_prompt()
|
2018-01-25 19:16:59 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:filter(expr) abort
|
|
|
|
call s:MPT._build_prompt()
|
|
|
|
if a:expr ==# ''
|
|
|
|
redrawstatus
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
try
|
|
|
|
call matchdelete(s:hi_id)
|
|
|
|
catch
|
|
|
|
endtr
|
|
|
|
hi def link FlyGrepPattern MoreMsg
|
|
|
|
let s:hi_id = matchadd('FlyGrepPattern', '\c' . join(split(a:expr), '\|'), 1)
|
|
|
|
let s:grep_expr = a:expr
|
|
|
|
let s:grep_timer_id = timer_start(200, function('s:filter_timer'), {'repeat' : 1})
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:filter_timer(timer) abort
|
|
|
|
let cmd = s:get_filter_cmd(join(split(s:grep_expr), '.*'))
|
|
|
|
call SpaceVim#logger#info('filter cmd: ' . string(cmd))
|
|
|
|
let s:grepid = s:JOB.start(cmd, {
|
|
|
|
\ 'on_stdout' : function('s:grep_stdout'),
|
|
|
|
\ 'on_stderr' : function('s:grep_stderr'),
|
|
|
|
\ 'in_io' : 'null',
|
|
|
|
\ 'on_exit' : function('s:grep_exit'),
|
|
|
|
\ })
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:get_filter_cmd(expr) abort
|
|
|
|
let cmd = [s:grep_exe] + SpaceVim#mapping#search#getFopt(s:grep_exe)
|
|
|
|
return cmd + [a:expr] + [s:filter_file]
|
|
|
|
endfunction
|
2017-07-07 20:47:26 +08:00
|
|
|
|
2017-07-06 21:59:09 +08:00
|
|
|
function! s:flygrep(expr) abort
|
2017-12-05 20:49:16 +08:00
|
|
|
call s:MPT._build_prompt()
|
|
|
|
if a:expr ==# ''
|
|
|
|
redrawstatus
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
try
|
2017-12-06 00:08:08 +08:00
|
|
|
call matchdelete(s:hi_id)
|
2017-12-05 20:49:16 +08:00
|
|
|
catch
|
|
|
|
endtr
|
2018-01-22 23:35:40 +08:00
|
|
|
hi def link FlyGrepPattern MoreMsg
|
|
|
|
let s:hi_id = matchadd('FlyGrepPattern', '\c' . join(split(a:expr), '\|'), 1)
|
2017-12-05 20:49:16 +08:00
|
|
|
let s:grep_expr = a:expr
|
2018-01-24 22:28:29 +08:00
|
|
|
let s:grep_timer_id = timer_start(200, function('s:grep_timer'), {'repeat' : 1})
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
let s:MPT._handle_fly = function('s:flygrep')
|
|
|
|
|
|
|
|
function! s:close_buffer() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if s:grepid != 0
|
|
|
|
call s:JOB.stop(s:grepid)
|
|
|
|
endif
|
|
|
|
if s:grep_timer_id != 0
|
|
|
|
call timer_stop(s:grep_timer_id)
|
|
|
|
endif
|
|
|
|
q
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
let s:MPT._onclose = function('s:close_buffer')
|
|
|
|
|
|
|
|
|
|
|
|
function! s:close_grep_job() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if s:grepid != 0
|
|
|
|
call s:JOB.stop(s:grepid)
|
|
|
|
endif
|
|
|
|
if s:grep_timer_id != 0
|
|
|
|
call timer_stop(s:grep_timer_id)
|
|
|
|
endif
|
|
|
|
normal! "_ggdG
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
let s:MPT._oninputpro = function('s:close_grep_job')
|
|
|
|
|
|
|
|
" @vimlint(EVL103, 1, a:data)
|
|
|
|
" @vimlint(EVL103, 1, a:id)
|
|
|
|
" @vimlint(EVL103, 1, a:event)
|
|
|
|
function! s:grep_stdout(id, data, event) abort
|
2017-12-05 20:49:16 +08:00
|
|
|
let datas =filter(a:data, '!empty(v:val)')
|
|
|
|
if getline(1) ==# ''
|
|
|
|
call setline(1, datas)
|
|
|
|
else
|
|
|
|
call append('$', datas)
|
|
|
|
endif
|
|
|
|
call s:MPT._build_prompt()
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
2018-01-22 20:17:52 +08:00
|
|
|
function! s:grep_stderr(id, data, event) abort
|
|
|
|
let datas =filter(a:data, '!empty(v:val)')
|
|
|
|
if getline(1) ==# ''
|
|
|
|
call setline(1, datas)
|
|
|
|
else
|
|
|
|
call append('$', datas)
|
|
|
|
endif
|
2018-01-22 22:20:35 +08:00
|
|
|
call append('$', 'job:' . string(s:get_search_cmd(s:grep_expr)))
|
2018-01-22 20:17:52 +08:00
|
|
|
call s:MPT._build_prompt()
|
|
|
|
endfunction
|
|
|
|
|
2017-07-06 21:59:09 +08:00
|
|
|
function! s:grep_exit(id, data, event) abort
|
2017-12-05 20:49:16 +08:00
|
|
|
redrawstatus
|
|
|
|
let s:grepid = 0
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" @vimlint(EVL103, 0, a:data)
|
|
|
|
" @vimlint(EVL103, 0, a:id)
|
|
|
|
" @vimlint(EVL103, 0, a:event)
|
|
|
|
|
2018-01-22 22:02:15 +08:00
|
|
|
function! s:get_search_cmd(expr) abort
|
2018-01-22 22:08:21 +08:00
|
|
|
let cmd = [s:grep_exe] + s:grep_opt
|
2018-01-22 22:02:15 +08:00
|
|
|
if !empty(s:grep_files) && type(s:grep_files) == 3
|
|
|
|
return cmd + [a:expr] + s:grep_files
|
|
|
|
elseif !empty(s:grep_files) && type(s:grep_files) == 1
|
|
|
|
return cmd + [a:expr] + [s:grep_files]
|
|
|
|
elseif !empty(s:grep_dir)
|
|
|
|
return cmd + [a:expr] + [s:grep_dir]
|
2017-12-05 20:49:16 +08:00
|
|
|
else
|
2018-01-22 22:53:02 +08:00
|
|
|
return cmd + [a:expr] + s:grep_ropt
|
2017-12-05 20:49:16 +08:00
|
|
|
endif
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:next_item() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if line('.') == line('$')
|
|
|
|
normal! gg
|
|
|
|
else
|
|
|
|
normal! j
|
|
|
|
endif
|
|
|
|
redrawstatus
|
|
|
|
call s:MPT._build_prompt()
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:previous_item() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if line('.') == 1
|
|
|
|
normal! G
|
|
|
|
else
|
|
|
|
normal! k
|
|
|
|
endif
|
|
|
|
redrawstatus
|
|
|
|
call s:MPT._build_prompt()
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:open_item() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if getline('.') !=# ''
|
|
|
|
if s:grepid != 0
|
|
|
|
call s:JOB.stop(s:grepid)
|
2017-07-06 21:59:09 +08:00
|
|
|
endif
|
2017-12-05 20:49:16 +08:00
|
|
|
call s:MPT._clear_prompt()
|
|
|
|
let s:MPT._quit = 1
|
|
|
|
let line = getline('.')
|
|
|
|
let filename = fnameescape(split(line, ':\d\+:')[0])
|
|
|
|
let linenr = matchstr(line, ':\d\+:')[1:-2]
|
2018-01-25 20:58:21 +08:00
|
|
|
let colum = matchstr(line, '\(:\d\+\)\@<=:\d\+:')[1:-2]
|
|
|
|
noautocmd q
|
2017-12-05 20:49:16 +08:00
|
|
|
exe 'e ' . filename
|
2018-01-25 20:58:21 +08:00
|
|
|
call cursor(linenr, colum)
|
2017-12-18 22:48:48 +08:00
|
|
|
redraw!
|
2017-12-05 20:49:16 +08:00
|
|
|
endif
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|
|
|
|
|
2017-07-08 23:02:07 +08:00
|
|
|
function! s:double_click() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if line('.') !=# ''
|
|
|
|
if s:grepid != 0
|
|
|
|
call s:JOB.stop(s:grepid)
|
|
|
|
endif
|
|
|
|
call s:MPT._clear_prompt()
|
|
|
|
let s:MPT._quit = 1
|
|
|
|
let isfname = &isfname
|
|
|
|
if s:SYS.isWindows
|
|
|
|
set isfname-=:
|
2017-07-08 23:02:07 +08:00
|
|
|
endif
|
2017-12-05 20:49:16 +08:00
|
|
|
normal! gF
|
|
|
|
let nr = bufnr('%')
|
|
|
|
q
|
|
|
|
exe 'silent b' . nr
|
|
|
|
normal! :
|
|
|
|
let &isfname = isfname
|
|
|
|
endif
|
2017-07-08 23:02:07 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:move_cursor() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if v:mouse_win == winnr()
|
|
|
|
let cl = line('.')
|
|
|
|
if cl < v:mouse_lnum
|
|
|
|
exe 'normal! ' . (v:mouse_lnum - cl) . 'j'
|
|
|
|
elseif cl > v:mouse_lnum
|
|
|
|
exe 'normal! ' . (cl - v:mouse_lnum) . 'k'
|
2017-07-08 23:02:07 +08:00
|
|
|
endif
|
2017-12-05 20:49:16 +08:00
|
|
|
endif
|
|
|
|
call s:MPT._build_prompt()
|
2017-07-08 23:02:07 +08:00
|
|
|
endfunction
|
|
|
|
|
2017-07-06 21:59:09 +08:00
|
|
|
let s:MPT._function_key = {
|
2017-12-05 20:49:16 +08:00
|
|
|
\ "\<Tab>" : function('s:next_item'),
|
2018-01-23 00:19:37 +08:00
|
|
|
\ "\<C-j>" : function('s:next_item'),
|
2017-12-05 20:49:16 +08:00
|
|
|
\ "\<ScrollWheelDown>" : function('s:next_item'),
|
|
|
|
\ "\<S-tab>" : function('s:previous_item'),
|
2018-01-23 00:19:37 +08:00
|
|
|
\ "\<C-k>" : function('s:previous_item'),
|
2017-12-05 20:49:16 +08:00
|
|
|
\ "\<ScrollWheelUp>" : function('s:previous_item'),
|
|
|
|
\ "\<Return>" : function('s:open_item'),
|
|
|
|
\ "\<LeftMouse>" : function('s:move_cursor'),
|
|
|
|
\ "\<2-LeftMouse>" : function('s:double_click'),
|
2018-01-25 19:16:59 +08:00
|
|
|
\ "\<C-f>" : function('s:start_filter'),
|
2017-12-05 20:49:16 +08:00
|
|
|
\ }
|
|
|
|
|
|
|
|
if has('nvim')
|
|
|
|
call extend(s:MPT._function_key,
|
|
|
|
\ {
|
|
|
|
\ "\x80\xfdJ" : function('s:previous_item'),
|
|
|
|
\ "\x80\xfc \x80\xfdJ" : function('s:previous_item'),
|
|
|
|
\ "\x80\xfc@\x80\xfdJ" : function('s:previous_item'),
|
|
|
|
\ "\x80\xfc`\x80\xfdJ" : function('s:previous_item'),
|
|
|
|
\ "\x80\xfdK" : function('s:next_item'),
|
|
|
|
\ "\x80\xfc \x80\xfdK" : function('s:next_item'),
|
|
|
|
\ "\x80\xfc@\x80\xfdK" : function('s:next_item'),
|
|
|
|
\ "\x80\xfc`\x80\xfdK" : function('s:next_item'),
|
|
|
|
\ }
|
|
|
|
\ )
|
|
|
|
endif
|
2017-07-06 21:59:09 +08:00
|
|
|
|
|
|
|
" statusline api
|
|
|
|
function! SpaceVim#plugins#flygrep#lineNr() abort
|
2017-12-05 20:49:16 +08:00
|
|
|
if getline(1) ==# ''
|
|
|
|
return ''
|
|
|
|
else
|
|
|
|
return line('.') . '/' . line('$')
|
|
|
|
endif
|
2017-07-06 21:59:09 +08:00
|
|
|
endfunction
|