Skip to content

Commit a52b974

Browse files
committed
Add location list feature
Inspired by jremmen#35 (jremmen#35)
1 parent 1ee3f0b commit a52b974

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
```vim
44
:Rg <string|pattern>
5+
:LRg <string|pattern> "store to location list
56
```
67

78
Word under cursor will be searched if no argument is passed to `Rg`
89

910
## configuration
1011

1112

12-
| Setting | Default | Details
13-
| --------------------- | --------------------------- | ----------
14-
| `g:rg_binary` | `rg` | path to rg
15-
| `g:rg_format` | `%f:%l:%c:%m` | value of grepformat
16-
| `g:rg_option` | `--vimgrep` | search command option
17-
| `g:rg_highlight` | `false` | true if you want matches highlighted
18-
| `g:rg_derive_root` | `false` | true if you want to find project root from cwd
19-
| `g:rg_root_types` | `['.git']` | list of files/dir found in project root
20-
| `g:rg_window_location` | `botright` | quickfix window location
13+
| Setting | Default | Details
14+
| --------------------- | --------------------------- | ----------
15+
| `g:rg_binary` | `rg` | path to rg
16+
| `g:rg_format` | `%f:%l:%c:%m` | value of grepformat
17+
| `g:rg_option` | `--vimgrep` | search command option
18+
| `g:rg_highlight` | `0` | true if you want matches highlighted
19+
| `g:rg_derive_root` | `0` | true if you want to find project root from cwd
20+
| `g:rg_root_types` | `['.git']` | list of files/dir found in project root
21+
| `g:rg_use_location_list` | `0` | if `1`, use location list instead of quickfix list
22+
| `g:rg_window_location` | `botright` | quickfix window location
2123

2224
## misc
2325

plugin/vim-ripgrep.vim

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ if !exists('g:rg_window_location')
2828
let g:rg_window_location = 'botright'
2929
endif
3030

31+
if !exists('g:rg_use_location_list')
32+
let g:rg_use_location_list = 0
33+
endif
34+
3135
fun! g:RgVisual() range
3236
call s:RgGrepContext(function('s:RgSearch'), '"' . s:RgGetVisualSelection() . '"')
3337
endfun
@@ -36,6 +40,13 @@ fun! s:Rg(txt)
3640
call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt))
3741
endfun
3842

43+
fun! s:LRg(txt)
44+
let l:rg_use_location_list_bak = g:rg_use_location_list
45+
let g:rg_use_location_list = 1
46+
call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt))
47+
let g:rg_use_location_list = l:rg_use_location_list_bak
48+
endfun
49+
3950
fun! s:RgGetVisualSelection()
4051
" Why is this not a built-in Vim script function?!
4152
let [line_start, column_start] = getpos("'<")[1:2]
@@ -65,15 +76,24 @@ fun! s:RgSearch(txt)
6576
if &smartcase == 1
6677
let l:rgopts = l:rgopts . '-S '
6778
endif
68-
silent! exe 'grep! ' . l:rgopts . a:txt
69-
if len(getqflist())
70-
exe g:rg_window_location 'copen'
79+
if (g:rg_use_location_list==1)
80+
let l:rg_grep_cmd = 'lgrep! '
81+
let l:rg_window_cmd = 'lopen'
82+
let l:rg_window_close_cmd = 'lclose'
83+
else
84+
let l:rg_grep_cmd = 'grep! '
85+
let l:rg_window_cmd = 'copen'
86+
let l:rg_window_close_cmd = 'cclose'
87+
endif
88+
silent! exe l:rg_grep_cmd . l:rgopts . a:txt
89+
if (g:rg_use_location_list ? len(getloclist(0)) : len(getqflist()))
90+
exe g:rg_window_location . ' ' . l:rg_window_cmd
7191
redraw!
7292
if exists('g:rg_highlight')
7393
call s:RgHighlight(a:txt)
7494
endif
7595
else
76-
cclose
96+
exe l:rg_window_close_cmd
7797
redraw!
7898
echo "No match found for " . a:txt
7999
endif
@@ -161,4 +181,5 @@ fun! s:RgGetCwd() abort
161181
endfun
162182

163183
command! -nargs=* -complete=file Rg :call s:Rg(<q-args>)
184+
command! -nargs=* -complete=file LRg :call s:LRg(<q-args>)
164185
command! -complete=file RgRoot :call s:RgShowRoot()

0 commit comments

Comments
 (0)