From 6f5bdefbed785b1e7259afee62bd0ec96a1c71f9 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Mon, 22 Jan 2018 20:17:52 +0800 Subject: [PATCH] Add support searching in another dir --- autoload/SpaceVim/mapping/space.vim | 4 +++- autoload/SpaceVim/plugins/flygrep.vim | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/autoload/SpaceVim/mapping/space.vim b/autoload/SpaceVim/mapping/space.vim index aafb72e52..f2899acac 100644 --- a/autoload/SpaceVim/mapping/space.vim +++ b/autoload/SpaceVim/mapping/space.vim @@ -178,7 +178,9 @@ function! SpaceVim#mapping#space#init() abort call SpaceVim#mapping#space#def('nnoremap', ['s', 'B'], "call SpaceVim#plugins#flygrep#open({'input' : expand(\"\"), 'files':'@buffers'})", \ 'grep cursor word in all loaded buffers', 1) " Searching in files in an arbitrary directory - call SpaceVim#mapping#space#def('nnoremap', ['s', 'f'], 'Unite grep', 'grep in arbitrary directory', 1) + call SpaceVim#mapping#space#def('nnoremap', ['s', 'f'], "call SpaceVim#plugins#flygrep#open({'input' :" + \ . " input(\"grep pattern:\"), 'dir' : input(\"arbitrary dir:\", '', 'dir')})", + \ 'grep in arbitrary directory', 1) call SpaceVim#mapping#space#def('nnoremap', ['s', 'F'], "execute 'Unite grep:::' . expand(\"\") . ' -start-insert'", \ 'grep in arbitrary directory', 1) " Searching in project diff --git a/autoload/SpaceVim/plugins/flygrep.vim b/autoload/SpaceVim/plugins/flygrep.vim index 4a2e49f63..da318f946 100644 --- a/autoload/SpaceVim/plugins/flygrep.vim +++ b/autoload/SpaceVim/plugins/flygrep.vim @@ -25,7 +25,7 @@ function! SpaceVim#plugins#flygrep#open(agrv) abort else let s:grep_files = '' endif - let dir = get(a:agrv, 'dir', '') + let dir = expand(get(a:agrv, 'dir', '')) if !empty(dir) && isdirectory(dir) let s:grep_dir = dir else @@ -43,6 +43,7 @@ let s:grep_timer_id = 0 function! s:grep_timer(timer) abort let s:grepid = s:JOB.start(s:get_search_cmd(s:grep_exe, s:grep_expr), { \ 'on_stdout' : function('s:grep_stdout'), + \ 'on_stderr' : function('s:grep_stderr'), \ 'in_io' : 'null', \ 'on_exit' : function('s:grep_exit'), \ }) @@ -105,6 +106,17 @@ function! s:grep_stdout(id, data, event) abort call s:MPT._build_prompt() endfunction +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 + call append('$', 'job:' . string(s:get_search_cmd(s:grep_exe, s:grep_expr))) + call s:MPT._build_prompt() +endfunction + function! s:grep_exit(id, data, event) abort redrawstatus let s:grepid = 0 @@ -122,8 +134,10 @@ function! s:get_search_cmd(exe, expr) abort return ['rg', '-H', '-n', '-i', a:expr] + s:grep_files elseif !empty(s:grep_files) && type(s:grep_files) == 1 return ['rg', '-H', '-n', '-i', a:expr] + [s:grep_files] + elseif !empty(s:grep_dir) + return ['rg', '-H', '-n', '-i', a:expr] + [s:grep_dir] else - return ['rg', '-H', '-n', '-i', a:expr] + return ['rg', '-H', '-n', '-i', a:expr, s:grep_dir] endif else return [a:exe, a:expr]