aboutsummaryrefslogblamecommitdiffstats
path: root/plugin/vim-ripgrep.vim
blob: fc1bd58645455f6c3a9661752b532ea7384ec100 (plain) (tree)






























































                                                                                
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>)