From 498cbe0fa0c56282a377cf34f01072c8b5cc9c7b Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Sun, 2 Jul 2017 16:00:06 +0800 Subject: [PATCH] Add background searching feature --- autoload/SpaceVim/layers/core/statusline.vim | 1 + autoload/SpaceVim/mapping/space.vim | 5 +++ autoload/SpaceVim/plugins/searcher.vim | 47 ++++++++++++++++++++ init.vim | 1 + 4 files changed, 54 insertions(+) create mode 100644 autoload/SpaceVim/plugins/searcher.vim diff --git a/autoload/SpaceVim/layers/core/statusline.vim b/autoload/SpaceVim/layers/core/statusline.vim index 1f055dc07..323fa7307 100644 --- a/autoload/SpaceVim/layers/core/statusline.vim +++ b/autoload/SpaceVim/layers/core/statusline.vim @@ -241,6 +241,7 @@ function! s:active() abort if index(s:loaded_sections, 'version control info') != -1 call add(lsec, s:git_branch()) endif + call add(lsec, SpaceVim#plugins#searcher#count()) if index(s:loaded_sections, 'battery status') != -1 call add(rsec, s:battery_status()) endif diff --git a/autoload/SpaceVim/mapping/space.vim b/autoload/SpaceVim/mapping/space.vim index c4ab43beb..c4ba65b28 100644 --- a/autoload/SpaceVim/mapping/space.vim +++ b/autoload/SpaceVim/mapping/space.vim @@ -105,6 +105,11 @@ function! SpaceVim#mapping#space#init() abort call SpaceVim#mapping#space#def('nnoremap', ['s', 'p'], 'Unite grep:.', 'grep in project', 1) call SpaceVim#mapping#space#def('nnoremap', ['s', 'P'], "execute 'Unite grep:.::' . expand(\"\") . ' -start-insert'", \ 'grep in project', 1) + " Searching background + call SpaceVim#mapping#space#def('nnoremap', ['s', 'j'], 'call SpaceVim#plugins#searcher#find()', 'Background search keywords in project', 1) + call SpaceVim#mapping#space#def('nnoremap', ['s', 'J'], 'call SpaceVim#plugins#searcher#find(expand(""))', + \ 'Background search cursor words in project', 1) + call SpaceVim#mapping#space#def('nnoremap', ['s', 'l'], 'call SpaceVim#plugins#searcher#list()', 'List all searching results', 1) " Searching tools " ag diff --git a/autoload/SpaceVim/plugins/searcher.vim b/autoload/SpaceVim/plugins/searcher.vim new file mode 100644 index 000000000..7c239986a --- /dev/null +++ b/autoload/SpaceVim/plugins/searcher.vim @@ -0,0 +1,47 @@ +let s:rst = [] +function! SpaceVim#plugins#searcher#find(...) + if a:0 == 0 + let expr = input('search expr: ') + else + let expr = a:1 + endif + call jobstart(['ag', expr], { + \ 'on_stdout' : function('s:search_stdout'), + \ 'on_exit' : function('s:search_exit'), + \ }) +endfunction +function! s:search_stdout(id, data, event) abort + for data in a:data + let info = split(data, '\:\d\+\:') + if len(info) == 2 + let [fname, text] = info + let lnum = matchstr(data, '\:\d\+\:')[1:-2] + call add(s:rst, { + \ 'filename' : fnamemodify(fname, ':p'), + \ 'lnum' : lnum, + \ 'text' : text, + \ }) + endif + endfor +endfunction + +function! s:search_exit(id, data, event) abort + let &l:statusline = SpaceVim#layers#core#statusline#get(1) +endfunction + + +function! SpaceVim#plugins#searcher#list() + call setqflist(s:rst) + let s:rst = [] + copen +endfunction + +function! SpaceVim#plugins#searcher#count() + if empty(s:rst) + return '' + else + return ' ' . len(s:rst) . ' items ' + endif +endfunction + + diff --git a/init.vim b/init.vim index de677ab68..4e371e1fb 100644 --- a/init.vim +++ b/init.vim @@ -1 +1,2 @@ execute 'source' fnamemodify(expand(''), ':h').'/config/main.vim' +" abfgkprt