diff options
author | Wynn Wolf Arbor | 2020-06-05 21:37:18 +0200 |
---|---|---|
committer | Wynn Wolf Arbor | 2020-06-05 21:51:11 +0200 |
commit | 9de2e215ca473de3441746788b1cc85bde9c87f3 (patch) | |
tree | 6a1557b853ee5fa46279b0877cdd14deb36bae81 /plugin | |
download | vim-ripgrep-9de2e215ca473de3441746788b1cc85bde9c87f3.tar.gz |
Initial import.
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/vim-ripgrep.vim | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/plugin/vim-ripgrep.vim b/plugin/vim-ripgrep.vim new file mode 100644 index 0000000..fc1bd58 --- /dev/null +++ b/plugin/vim-ripgrep.vim @@ -0,0 +1,63 @@ +if exists('g:rg_plugin_loaded') || &compatible + finish +endif + +let g:rg_plugin_loaded = 1 + +let g:rg_binary = 'rg' +let g:rg_args = ' --vimgrep' +let g:rg_format = '%f:%l:%c:%m' + +fun! s:err(msg) + echohl ErrorMsg | echo a:msg | echohl None +endfun + +fun! s:ArgsOrCword(args) + if empty(a:args) + return expand('<cword>') + else + return a:args + endif +endfun + +fun! s:PerformSearch(args) + if empty(a:args) + call s:err('No search pattern given and <cword> was empty.') + return + endif + + let l:opts = ' ' + if &ignorecase + let l:opts = l:opts . '-i ' + endif + if &smartcase + let l:opts = l:opts . '-S ' + endif + + silent exe 'grep! ' . l:opts . a:args + + if len(getqflist()) + exe 'copen' | redraw! + else + cclose | redraw! + echo 'No match found for ' . a:args + endif +endfun + +fun! s:WithRgContext(fun, args) + let l:prev_grepprg = &grepprg + let l:prev_grepformat = &grepformat + let &grepprg = g:rg_binary . g:rg_args + let &grepformat = g:rg_format + + call a:fun(a:args) + + let &grepprg = l:prev_grepprg + let &grepformat = l:prev_grepformat +endfun + +fun! s:Rg(args) + call s:WithRgContext(function('s:PerformSearch'), s:ArgsOrCword(a:args)) +endfun + +command! -nargs=* -complete=file Rg call s:Rg(<q-args>) |