commit ddc8f1526c78f8d5d1ac0b899da96841d75a4d85 Author: jremmen Date: Sat Sep 24 22:25:31 2016 -0700 add vim-ripgrep diff --git a/plugin/vim-ripgrep.vim b/plugin/vim-ripgrep.vim new file mode 100644 index 0000000..cf6358b --- /dev/null +++ b/plugin/vim-ripgrep.vim @@ -0,0 +1,94 @@ +"if exists('g:loaded_rg') || &cp +" finish +"endif + +let g:loaded_rg = 1 + +if !exists('g:rg_binary') + let g:rg_binary = 'rg' +endif + +if !exists('g:rg_format') + let g:rg_format = "%f:%l:%c:%m" +endif + +if !exists('g:rg_command') + let g:rg_command = g:rg_binary . ' --vimgrep' +endif + +if !exists('g:rg_rootfiles') + let g:rg_root_types = ['.git'] +endif + +fun! s:Rg(txt) + call s:RgGrepContext(function('s:RgSearch'), a:txt) +endfun + +fun! s:RgSearch(txt) + silent! exe 'grep! ' . a:txt + if len(getqflist()) + copen + redraw! + if exists('g:rg_highlight') + call s:RgHighlight(a:txt) + endif + else + cclose + redraw! + echo "No match found for " . a:txt + endif +endfun + +fun! s:RgGrepContext(search, txt) + let l:grepprgb = &grepprg + let l:grepformatb = &grepformat + let &grepprg = g:rg_command + let &grepformat = g:rg_format + + if exists('g:rg_derive_root') + call s:RgPathContext(a:search, a:txt) + else + call a:search(a:txt) + endif + + let &grepprg = l:grepprgb + let &grepformat = l:grepformatb +endfun + +fun! s:RgPathContext(search, txt) + let l:cwdb = getcwd() + exe 'lcd '.s:RgRootDir() + call a:search(a:txt) + exe 'lcd '.l:cwdb +endfun + +fun! s:RgHighlight(txt) + let @/=escape(substitute(a:txt, '"', '', 'g'), '|') + call feedkeys(":let &hlsearch=1\", 'n') +endfun + +fun! s:RgRootDir() + let l:cwd = getcwd() + let l:dirs = split(getcwd(), '/') + + for l:dir in reverse(copy(l:dirs)) + for l:type in g:rg_root_types + let l:path = s:RgMakePath(l:dirs, l:dir) + if s:RgHasFile(l:path.'/'.l:type) + return l:path + endif + endfor + endfor + return l:cwd +endfun + +fun! s:RgMakePath(dirs, dir) + return '/'.join(a:dirs[0:index(a:dirs, a:dir)], '/') +endfun + +fun! s:RgHasFile(path) + return filereadable(a:path) || isdirectory(a:path) +endfun + +command! -nargs=* Rg :call s:Rg() +command! RgDir :call s:RgRootDir()