]> git.r.bdr.sh - rbdr/dotfiles/blob - vim/plugin/ack.vim
8cc0d45a4056d85ac908791f38e3ae431a3aac72
[rbdr/dotfiles] / vim / plugin / ack.vim
1 " NOTE: You must, of course, install the ack script
2 " in your path.
3 " On Debian / Ubuntu:
4 " sudo apt-get install ack-grep
5 " On your vimrc:
6 " let g:ackprg="ack-grep -H --nocolor --nogroup --column"
7 "
8 " With MacPorts:
9 " sudo port install p5-app-ack
10
11 " Location of the ack utility
12 if !exists("g:ackprg")
13 let g:ackprg="ack -H --nocolor --nogroup --column"
14 endif
15
16 function! s:Ack(cmd, args)
17 redraw
18 echo "Searching ..."
19
20 " If no pattern is provided, search for the word under the cursor
21 if empty(a:args)
22 let l:grepargs = expand("<cword>")
23 else
24 let l:grepargs = a:args
25 end
26
27 " Format, used to manage column jump
28 if a:cmd =~# '-g$'
29 let g:ackformat="%f"
30 else
31 let g:ackformat="%f:%l:%c:%m"
32 end
33
34 let grepprg_bak=&grepprg
35 let grepformat_bak=&grepformat
36 try
37 let &grepprg=g:ackprg
38 let &grepformat=g:ackformat
39 silent execute a:cmd . " " . l:grepargs
40 finally
41 let &grepprg=grepprg_bak
42 let &grepformat=grepformat_bak
43 endtry
44
45 if a:cmd =~# '^l'
46 botright lopen
47 else
48 botright copen
49 endif
50
51 " TODO: Document this!
52 exec "nnoremap <silent> <buffer> q :ccl<CR>"
53 exec "nnoremap <silent> <buffer> t <C-W><CR><C-W>T"
54 exec "nnoremap <silent> <buffer> T <C-W><CR><C-W>TgT<C-W><C-W>"
55 exec "nnoremap <silent> <buffer> o <CR>"
56 exec "nnoremap <silent> <buffer> go <CR><C-W><C-W>"
57
58 " If highlighting is on, highlight the search keyword.
59 if exists("g:ackhighlight")
60 let @/=a:args
61 set hlsearch
62 end
63
64 redraw!
65 endfunction
66
67 function! s:AckFromSearch(cmd, args)
68 let search = getreg('/')
69 " translate vim regular expression to perl regular expression.
70 let search = substitute(search,'\(\\<\|\\>\)','\\b','g')
71 call s:Ack(a:cmd, '"' . search .'" '. a:args)
72 endfunction
73
74 command! -bang -nargs=* -complete=file Ack call s:Ack('grep<bang>',<q-args>)
75 command! -bang -nargs=* -complete=file AckAdd call s:Ack('grepadd<bang>', <q-args>)
76 command! -bang -nargs=* -complete=file AckFromSearch call s:AckFromSearch('grep<bang>', <q-args>)
77 command! -bang -nargs=* -complete=file LAck call s:Ack('lgrep<bang>', <q-args>)
78 command! -bang -nargs=* -complete=file LAckAdd call s:Ack('lgrepadd<bang>', <q-args>)
79 command! -bang -nargs=* -complete=file AckFile call s:Ack('grep<bang> -g', <q-args>)