mirror of
https://github.com/jremmen/vim-ripgrep.git
synced 2025-12-26 19:45:29 +08:00
Add function for searching a visual selection
This commit is contained in:
parent
88ce3691b6
commit
7d01d6976b
1 changed files with 18 additions and 1 deletions
|
|
@ -24,10 +24,27 @@ if !exists('g:rg_window_location')
|
||||||
let g:rg_window_location = 'botright'
|
let g:rg_window_location = 'botright'
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
fun! g:RgVisual() range
|
||||||
|
call s:RgGrepContext(function('s:RgSearch'), s:RgGetVisualSelection())
|
||||||
|
endfun
|
||||||
|
|
||||||
fun! s:Rg(txt)
|
fun! s:Rg(txt)
|
||||||
call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt))
|
call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt))
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
|
fun! s:RgGetVisualSelection()
|
||||||
|
" Why is this not a built-in Vim script function?!
|
||||||
|
let [line_start, column_start] = getpos("'<")[1:2]
|
||||||
|
let [line_end, column_end] = getpos("'>")[1:2]
|
||||||
|
let lines = getline(line_start, line_end)
|
||||||
|
if len(lines) == 0
|
||||||
|
return ''
|
||||||
|
endif
|
||||||
|
let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
|
||||||
|
let lines[0] = lines[0][column_start - 1:]
|
||||||
|
return join(lines, "\n")
|
||||||
|
endfun
|
||||||
|
|
||||||
fun! s:RgSearchTerm(txt)
|
fun! s:RgSearchTerm(txt)
|
||||||
if empty(a:txt)
|
if empty(a:txt)
|
||||||
return expand("<cword>")
|
return expand("<cword>")
|
||||||
|
|
@ -37,7 +54,7 @@ fun! s:RgSearchTerm(txt)
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
fun! s:RgSearch(txt)
|
fun! s:RgSearch(txt)
|
||||||
silent! exe 'grep! ' . a:txt
|
silent! exe 'grep! ' . '"' . a:txt . '"'
|
||||||
if len(getqflist())
|
if len(getqflist())
|
||||||
exe g:rg_window_location 'copen'
|
exe g:rg_window_location 'copen'
|
||||||
redraw!
|
redraw!
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue