1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 08:40:06 +08:00

Add support for grep in loaded buffers

This commit is contained in:
wsdjeg 2018-01-21 23:41:07 +08:00
parent 9ae4c3e476
commit fa9214cd49
2 changed files with 16 additions and 3 deletions

View File

@ -172,8 +172,9 @@ function! SpaceVim#mapping#space#init() abort
call SpaceVim#mapping#space#def('nnoremap', ['s', 'S'], "execute 'Unite grep:%::' . expand(\"<cword>\") . ' -start-insert'",
\ 'grep cursor word in current buffer', 1)
" Searching in all loaded buffers
call SpaceVim#mapping#space#def('nnoremap', ['s', 'b'], 'Unite grep:$buffers', 'grep in all loaded buffers', 1)
call SpaceVim#mapping#space#def('nnoremap', ['s', 'B'], "execute 'Unite grep:$buffers::' . expand(\"<cword>\") . ' -start-insert'",
call SpaceVim#mapping#space#def('nnoremap', ['s', 'b'], "call SpaceVim#plugins#flygrep#open({'input' : input(\"grep pattern:\"), 'files':'@buffers'})",
\ 'grep in all loaded buffers', 1)
call SpaceVim#mapping#space#def('nnoremap', ['s', 'B'], "call SpaceVim#plugins#flygrep#open({'input' : expand(\"<cword>\"), '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)

View File

@ -1,9 +1,13 @@
let s:MPT = SpaceVim#api#import('prompt')
let s:JOB = SpaceVim#api#import('job')
let s:SYS = SpaceVim#api#import('system')
let s:BUFFER = SpaceVim#api#import('vim#buffer')
let s:grepid = 0
let s:MPT._prompt.mpt = '➭ '
" keys:
" files: files for grep, @buffers means listed buffer.
let s:grep_files = ''
function! SpaceVim#plugins#flygrep#open(agrv) abort
rightbelow split __flygrep__
setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber
@ -13,6 +17,10 @@ function! SpaceVim#plugins#flygrep#open(agrv) abort
setf SpaceVimFlyGrep
redraw!
let s:MPT._prompt.begin = get(a:agrv, 'input', '')
let fs = get(a:agrv, 'files', '')
if fs ==# '@buffers'
let s:grep_files = map(s:BUFFER.listed_buffers(), 'bufname(v:val)')
endif
call s:MPT.open()
let &t_ve = save_tve
endfunction
@ -100,7 +108,11 @@ function! s:get_search_cmd(exe, expr) abort
if a:exe ==# 'grep'
return ['grep', '-inHR', '--exclude-dir', '.git', a:expr, '.']
elseif a:exe ==# 'rg'
return ['rg', '-n', '-i', a:expr]
if !empty(s:grep_files) && type(s:grep_files) == 3
return ['rg', '-n', '-i', a:expr] + s:grep_files
else
return ['rg', '-n', '-i', a:expr]
endif
else
return [a:exe, a:expr]
endif