Skip to content

Commit

Permalink
Improve executable selection
Browse files Browse the repository at this point in the history
- Allow user to specify a priority list.
- Always pick best possible executable.

For example, if the user specifies "ack,ag" and neither of those are
found, we will now pick a found "rg" executable (previously we would
fall back to "grep" in that case).
  • Loading branch information
wincent committed Jan 8, 2017
1 parent dbf1b19 commit 024d370
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ let g:FerretLoaded=1
```

<p align="right"><a name="gferretexecutable" href="#user-content-gferretexecutable"><code>g:FerretExecutable</code></a></p>
### `g:FerretExecutable` (string, default: none)<a name="ferret-gferretexecutable-string-default-none" href="#user-content-ferret-gferretexecutable-string-default-none"></a>
### `g:FerretExecutable` (string, default: "rg,ag,ack")<a name="ferret-gferretexecutable-string-default-rgagack" href="#user-content-ferret-gferretexecutable-string-default-rgagack"></a>

Ferret will preferentially use `rg`, `ag`, `ack` and finally `grep` (in that order, using the first found executable), however you can force your preference for a specific tool to be used by setting an override in your <strong>`.vimrc`</strong>. Valid values are "rg", "ag", "ack" and "grep". If the requested executable does not exist, Ferret will fall-back to the next in the list.
Ferret will preferentially use `rg`, `ag`, `ack` and finally `grep` (in that order, using the first found executable), however you can force your preference for a specific tool to be used by setting an override in your <strong>`.vimrc`</strong>. Valid values are a comma-separated list of "rg", "ag", "ack" or "grep". If no requested executable exists, Ferret will fall-back to the next in the default list.

Example:

```
let g:FerretExecutable='ag'
" Prefer `ag` over `rg`.
let g:FerretExecutable='ag,rg'
```

<p align="right"><a name="gferretmap" href="#user-content-gferretmap"><code>g:FerretMap</code></a></p>
Expand Down
10 changes: 6 additions & 4 deletions doc/ferret.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,19 @@ To prevent Ferret from being loaded, set |g:FerretLoaded| to any value in your
<

*g:FerretExecutable*
|g:FerretExecutable| string (default: none)
|g:FerretExecutable| string (default: "rg,ag,ack")

Ferret will preferentially use `rg`, `ag`, `ack` and finally `grep` (in that order,
using the first found executable), however you can force your preference for
a specific tool to be used by setting an override in your |.vimrc|. Valid
values are "rg", "ag", "ack" and "grep". If the requested executable does
not exist, Ferret will fall-back to the next in the list.
values are a comma-separated list of "rg", "ag", "ack" or "grep". If no
requested executable exists, Ferret will fall-back to the next in the
default list.

Example:
>
let g:FerretExecutable='ag'
" Prefer `ag` over `rg`.
let g:FerretExecutable='ag,rg'
<

*g:FerretMap*
Expand Down
42 changes: 30 additions & 12 deletions plugin/ferret.vim
Original file line number Diff line number Diff line change
Expand Up @@ -441,31 +441,49 @@ let s:cpoptions = &cpoptions
set cpoptions&vim

""
" @option g:FerretExecutable string
" @option g:FerretExecutable string "rg,ag,ack"
"
" Ferret will preferentially use `rg`, `ag`, `ack` and finally `grep` (in that
" order, using the first found executable), however you can force your
" preference for a specific tool to be used by setting an override in your
" |.vimrc|. Valid values are "rg", "ag", "ack" and "grep". If the requested
" executable does not exist, Ferret will fall-back to the next in the list.
" |.vimrc|. Valid values are a comma-separated list of "rg", "ag", "ack" or
" "grep". If no requested executable exists, Ferret will fall-back to
" the next in the default list.
"
" Example:
"
" ```
" let g:FerretExecutable='ag'
" " Prefer `ag` over `rg`.
" let g:FerretExecutable='ag,rg'
" ```
let s:force=get(g:, 'FerretExecutable', '')
let s:force=get(g:, 'FerretExecutable', 'rg,ag,ack')

let s:executables={
\ 'rg': 'rg --vimgrep --no-heading',
\ 'ag': 'ag --vimgrep',
\ 'ack': 'ack'
\ }

" Would ideally have these in an autoload file, but want to defer autoload
" until as late as possible.
function! FerretExecutable()
if (s:force == 'rg' || s:force == '') && executable('rg')
return 'rg --vimgrep --no-heading'
elseif (s:force == 'ag' || s:force =='rg' || s:force == '') && executable('ag')
return 'ag --vimgrep'
elseif (s:force == 'ack' || s:force == 'ag' || s:force == 'rg' || s:force == '') && executable('ack')
return 'ack --column --with-filename'
elseif executable('grep')
let l:executables=split(s:force, '\v\s*,\s*')
let l:executables=filter(l:executables, 'index(["rg", "ag", "ack"], v:val) != -1')
if index(l:executables, 'rg') == -1
call add(l:executables, 'rg')
endif
if index(l:executables, 'ag') == -1
call add(l:executables, 'ag')
endif
if index(l:executables, 'ack') == -1
call add(l:executables, 'ack')
endif
for l:executable in l:executables
if executable(l:executable)
return s:executables[l:executable]
endif
endfor
if executable('grep')
let l:grepprg=&grepprg
set grepprg&
let l:default=&grepprg " default (on UNIX) is: grep -n $* /dev/null
Expand Down

0 comments on commit 024d370

Please sign in to comment.