Background Grep

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Background Grep

jason heddings-2
Hello-

I have written the following mappings and functions to get a background grep
command that I can later pull the results in from.  Instead of just asking
so much from the list, I thought I'd try to contribute.

Any suggestions on if this seems appropriate or if there is an easier way?

Thanks,
--jah


" grep setup
set grepprg=C:\Development\Cygwin\bin\grep.exe\ -n
map <A-/> :call BackgroundGrep("<cword>", "*")<CR>
map <A-?> :call LoadBackgroundGrepResults()<CR>


" runs a search in the background
function! BackgroundGrep(search, fpatt)
  let g:bg_grep_file = tempname()
  slient execute "!start " . &grepprg  . " -R " . a:search  . " " . a:fpatt
. " >"  . g:bg_grep_file . " 2>&1"
  echo g:bg_grep_file
endfunction


" loads the results of the last background search
function! LoadBackgroundGrepResults()
  if !exists("g:bg_grep_file")
    echohl ErrorMsg | echo "BackgroundGrep() must be run first" | echohl
None
    return
  endif

  if !filereadable(g:bg_grep_file)
    echohl ErrorMsg | echo "Cannot open bg_grep_file: " . g:bg_grep_file |
echohl None
    return
  endif

  execute "cfile " . g:bg_grep_file
endfunction