aboutsummaryrefslogtreecommitdiffstats
path: root/plugin
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--plugin/vim-ripgrep.vim63
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>)