summaryrefslogtreecommitdiff
path: root/vim/.vim/plugin/search.vim
blob: b4e105486a257af11ebd347a982e78ada561982e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
" http://vim.wikia.com/wiki/Searching_for_files
function! MySearch(...)
    let l:prevgrepprg=&grepprg
    let l:prevgrepformat=&grepformat
    if a:1 == "Rg"
        set grepprg=rg\ -uu\ --vimgrep
        set grepformat=%f:%l:%c:%m
    elseif a:1 == "Gnufind"
        set grepprg=find\ .\ -name
        set grepformat=%f
    elseif a:1 == "Gitfind"
        set grepprg=git\ ls-files\ -i\ -x
        set grepformat=%f
    endif
    execute 'silent grep!' a:2 | cwindow | redraw!
    let &grepprg=l:prevgrepprg
    let &grepformat=l:prevgrepformat
endfun

command! -nargs=+ -complete=file_in_path Rg      call MySearch("Rg", <q-args>)
command! -nargs=+ -complete=file_in_path Gnufind call MySearch("Gnufind", <q-args>)
command! -nargs=+ -complete=file_in_path Gitfind call MySearch("Gitfind", <q-args>)
command! -nargs=+ -complete=file_in_path GitGrep execute 'silent Ggrep!' <q-args> | cw | redraw!
command! -nargs=+ -complete=file_in_path VimGrep execute 'silent vimgrep!' <q-args> | cw | redraw!

" Default search and find programs
function! FillSearch(...)
    if a:0 == 0
        let l:filltext = ""
    else
        let l:is_visual=(a:1 == "v")
        let l:line=getline(l:is_visual ? "'<" : "'[")
        let [line1,col1] = getpos(l:is_visual ? "'<" : "'[")[1:2]
        let [line2,col2] = getpos(l:is_visual ? "'>" : "']")[1:2]
        let l:filltext =l:line[col1 - 1: col2 - 1]
    endif
    call feedkeys(':' . g:my_fillprg . ' "' . l:filltext . '"'."\<Left>")
endfunction

let g:my_searchprgs = ['Rg', 'GitGrep', 'VimGrep']
let g:my_findprgs = ['Gnufind', 'Gitfind']
let g:my_searchprg = 0
let g:my_findprg = 0

nnoremap <silent> g/  :let g:my_fillprg=g:my_searchprgs[g:my_searchprg]<cr>:set opfunc=FillSearch<cr>g@
nnoremap <silent> ,g  :let g:my_fillprg=g:my_searchprgs[g:my_searchprg]<cr>:call FillSearch()<cr>
xnoremap <silent> ,g  :<c-u>let g:my_fillprg=g:my_searchprgs[g:my_searchprg]<cr>:call FillSearch(visualmode())<cr>
nnoremap <silent> ,f  :let g:my_fillprg=g:my_findprgs[g:my_findprg]<cr>:call FillSearch()<cr>

" Cycle search / find prgs
nnoremap <silent> cog :let g:my_searchprg=(g:my_searchprg+1)%len(g:my_searchprgs)<cr>:let &ro = &ro<cr>
nnoremap <silent> cof :let g:my_findprg=(g:my_findprg+1)%len(g:my_findprgs)<cr>:let &ro = &ro<cr>