Commits

Matthew Marshall  committed 0615d11

Adding ag plugin.

  • Participants
  • Parent commits 28fd755

Comments (0)

Files changed (4)

File bundle/ag.vim/README.md

+# ag.vim #
+
+This plugin is a front for ag, A.K.A.
+[the_silver_searcher](https://github.com/ggreer/the_silver_searcher).  Ag can
+be used as a replacement for 153% of the uses of `ack`.  This plugin will allow
+you to run ag from vim, and shows the results in a split window.
+
+## Installation ##
+
+### Ack
+
+You have to install [ag](https://github.com/ggreer/the_silver_searcher), of course.
+
+Usually it's a package named '[the_silver_searcher](https://github.com/ggreer/the_silver_searcher)', but it should install
+easily from Github, otherwise:
+
+    git clone https://github.com/ggreer/the_silver_searcher
+    cd the_silver_searcher && ./build.sh && install
+
+Then, if you're using [pathogen](https://github.com/tpope/vim-pathogen):
+
+    cd ~/.vim/bundle && git clone https://github.com/rking/vim-detailed
+    # And run :Helptags from within vim
+
+If you're using something else (whether it's manual install or some vim package
+manager), feel free to give me the pasteable line to put here as instructions.
+Really, people should probably be using Pathogen, in my opinion.
+
+### Configuation
+
+You can specify a custom ag name and path in your .vimrc like so:
+
+    let g:agprg="<custom-ag-path-goes-here> -H --nocolor --nogroup --column"
+
+## Usage ##
+
+    :Ag [options] {pattern} [{directory}]
+
+Search recursively in {directory} (which defaults to the current directory) for the {pattern}.
+
+Files containing the search term will be listed in the split window, along with
+the line number of the occurrence, once for each occurrence.  [Enter] on a line
+in this window will open the file, and place the cursor on the matching line.
+
+Just like where you use :grep, :grepadd, :lgrep, and :lgrepadd, you can use `:Ag`, `:AgAdd`, `:LAg`, and `:LAgAdd` respectively. (See `doc/ag.txt`, or install and `:h Ag` for more information.)
+
+### Gotchas ###
+
+Some characters have special meaning, and need to be escaped your search pattern. For instance, '#'. You have to escape it like this `:Ag '\\\#define foo'` to search for `#define foo`. (From [blueyed in issue #5](https://github.com/mileszs/ack.vim/issues/5).)
+
+Sometimes `git grep` is even faster, though in my experience it's not noticably so.
+
+### Keyboard Shortcuts ###
+
+In the quickfix window, you can use:
+
+    o    to open (same as enter)
+    go   to preview file (open but maintain focus on ack.vim results)
+    t    to open in new tab
+    T    to open in new tab silently
+    h    to open in horizontal split
+    H    to open in horizontal split silently
+    v    to open in vertical split
+    gv   to open in vertical split silently
+    q    to close the quickfix window
+
+### Acknowledgements
+
+This Vim plugin is derived (and by derived, I mean copied, almost entirely)
+from [milesz's ack.vim](https://github.com/mileszs/ack.vim), which I also
+recommend installing since you might be in a situation where you have ack but
+not ag, and don't want to stop to install ag. Also, ack supports `--type`, and
+a few other features.

File bundle/ag.vim/autoload/ag.vim

+" NOTE: You must, of course, install ag / the_silver_searcher
+
+" Location of the ag utility
+if !exists("g:agprg")
+  let s:agcommand = executable('ag-grep') ? 'ag-grep' : 'ag'
+  let g:agprg=s:agcommand." -H --nocolor --nogroup --column"
+endif
+
+if !exists("g:ag_apply_qmappings")
+  let g:ag_apply_qmappings = !exists("g:ag_qhandler")
+endif
+
+if !exists("g:ag_apply_lmappings")
+  let g:ag_apply_lmappings = !exists("g:ag_lhandler")
+endif
+
+if !exists("g:ag_qhandler")
+  let g:ag_qhandler="botright copen"
+endif
+
+if !exists("g:ag_lhandler")
+  let g:ag_lhandler="botright lopen"
+endif
+
+function! s:Ag(cmd, args)
+  redraw
+  echo "Searching ..."
+
+  " If no pattern is provided, search for the word under the cursor
+  if empty(a:args)
+    let l:grepargs = expand("<cword>")
+  else
+    let l:grepargs = a:args . join(a:000, ' ')
+  end
+
+  " Format, used to manage column jump
+  if a:cmd =~# '-g$'
+    let g:agformat="%f"
+  else
+    let g:agformat="%f:%l:%c:%m"
+  end
+
+  let grepprg_bak=&grepprg
+  let grepformat_bak=&grepformat
+  try
+    let &grepprg=g:agprg
+    let &grepformat=g:agformat
+    silent execute a:cmd . " " . escape(l:grepargs, '|')
+  finally
+    let &grepprg=grepprg_bak
+    let &grepformat=grepformat_bak
+  endtry
+
+  if a:cmd =~# '^l'
+    exe g:ag_lhandler
+    let l:apply_mappings = g:ag_apply_lmappings
+  else
+    exe g:ag_qhandler
+    let l:apply_mappings = g:ag_apply_qmappings
+  endif
+
+  if l:apply_mappings
+    exec "nnoremap <silent> <buffer> q :ccl<CR>"
+    exec "nnoremap <silent> <buffer> t <C-W><CR><C-W>T"
+    exec "nnoremap <silent> <buffer> T <C-W><CR><C-W>TgT<C-W><C-W>"
+    exec "nnoremap <silent> <buffer> o <CR>"
+    exec "nnoremap <silent> <buffer> go <CR><C-W><C-W>"
+    exec "nnoremap <silent> <buffer> h <C-W><CR><C-W>K"
+    exec "nnoremap <silent> <buffer> H <C-W><CR><C-W>K<C-W>b"
+    exec "nnoremap <silent> <buffer> v <C-W><CR><C-W>H<C-W>b<C-W>J<C-W>t"
+    exec "nnoremap <silent> <buffer> gv <C-W><CR><C-W>H<C-W>b<C-W>J"
+  endif
+
+  " If highlighting is on, highlight the search keyword.
+  if exists("g:aghighlight")
+    let @/=a:args
+    set hlsearch
+  end
+
+  redraw!
+endfunction
+
+function! s:AgFromSearch(cmd, args)
+  let search =  getreg('/')
+  " translate vim regular expression to perl regular expression.
+  let search = substitute(search,'\(\\<\|\\>\)','\\b','g')
+  call s:Ag(a:cmd, '"' .  search .'" '. a:args)
+endfunction
+
+function! s:GetDocLocations()
+    let dp = ''
+    for p in split(&rtp,',')
+        let p = p.'/doc/'
+        if isdirectory(p)
+            let dp = p.'*.txt '.dp
+        endif
+    endfor
+    return dp
+endfunction
+
+function! s:AgHelp(cmd,args)
+    let args = a:args.' '.s:GetDocLocations()
+    call s:Ag(a:cmd,args)
+endfunction
+
+command! -bang -nargs=* -complete=file Ag call s:Ag('grep<bang>',<q-args>)
+command! -bang -nargs=* -complete=file AgAdd call s:Ag('grepadd<bang>', <q-args>)
+command! -bang -nargs=* -complete=file AgFromSearch call s:AgFromSearch('grep<bang>', <q-args>)
+command! -bang -nargs=* -complete=file LAg call s:Ag('lgrep<bang>', <q-args>)
+command! -bang -nargs=* -complete=file LAgAdd call s:Ag('lgrepadd<bang>', <q-args>)
+command! -bang -nargs=* -complete=file AgFile call s:Ag('grep<bang> -g', <q-args>)
+command! -bang -nargs=* -complete=help AgHelp call s:AgHelp('grep<bang>',<q-args>)
+command! -bang -nargs=* -complete=help LAgHelp call s:AgHelp('lgrep<bang>',<q-args>)
+command! -bang -nargs=+ -complete=file Ag let g:agprg = 'ag --nogroup --nocolor --column'
+      \|call s:Ag('grep<bang>',<q-args>)

File bundle/ag.vim/doc/ag.txt

+*ag.txt*   Plugin that integrates ag with Vim
+
+==============================================================================
+INTRODUCTION                                                             *ag*
+
+This plugin is a front for the_silver_searcher: ag.  Ag can be used as a
+replacement for ag.  This plugin will allow you to run ag from vim, and
+shows the results in a split window.
+
+:Ag[!] [options] {pattern} [{directory}]                               *:Ag*
+
+    Search recursively in {directory} (which defaults to the current
+    directory) for the {pattern}.  Behaves just like the |:grep| command, but
+    will open the |Quickfix| window for you. If [!] is not given the first
+    error is jumped to.
+
+:AgAdd [options] {pattern} [{directory}]                            *:AgAdd*
+
+    Just like |:Ag|, but instead of making a new list, the matches are
+    appended to the current |quickfix| list.
+
+:AgFromSearch [{directory}]                                  *:AgFromSearch*
+
+    Just like |:Ag| but the pattern is from previous search.
+
+:LAg [options] {pattern} [{directory}]                                *:LAg*
+
+    Just like |:Ag| but instead of the |quickfix| list, matches are placed in
+    the current |location-list|.
+
+:LAgAdd [options] {pattern} [{directory}]                          *:LAgAdd*
+
+    Just like |:AgAdd| but instead of the |quickfix| list, matches are added
+    to the current |location-list|
+
+:AgFile [options] {pattern} [{directory}]                          *:AgFile*
+
+    Search recursively in {directory} (which defaults to the current
+    directory) for filenames matching the {pattern}.  Behaves just like the
+    |:grep| command, but will open the |Quickfix| window for you.
+
+:AgHelp[!] [options] {pattern}                                      *:AgHelp*
+
+    Search vim documentation files for the {pattern}.  Behaves just like the
+    |:Ag| command, but searches only vim documentation .txt files
+
+:LAgHelp [options] {pattern}                                        *:LAgHelp*
+
+    Just like |:AgHelp| but instead of the |quickfix| list, matches are placed
+    in the current |location-list|.
+
+Files containing the search term will be listed in the split window, along
+with the line number of the occurrence, once for each occurrence.  <Enter> on
+a line in this window will open the file, and place the cursor on the matching
+line.
+
+See http://betterthangrep.com/ for more information.
+
+==============================================================================
+MAPPINGS                                                        *ag-mappings*
+
+The following keyboard shortcuts are available in the quickfix window:
+
+o                   open file (same as enter).
+
+go                  preview file (open but maintain focus on ag.vim results).
+
+t                   open in a new tab.
+
+T                   open in new tab silently.
+
+h                   open in horizontal split.
+
+H                  open in horizontal split silently.
+
+v                   open in vertical split.
+
+gv                  open in vertical split silently.
+
+q                   close the quickfix window.

File bundle/ag.vim/plugin/ag.vim

+" NOTE: You must, of course, install ag / the_silver_searcher
+
+" Location of the ag utility
+if !exists("g:agprg")
+  let s:agcommand = executable('ag-grep') ? 'ag-grep' : 'ag'
+  let g:agprg=s:agcommand." -H --nocolor --nogroup --column"
+endif
+
+if !exists("g:ag_apply_qmappings")
+  let g:ag_apply_qmappings = !exists("g:ag_qhandler")
+endif
+
+if !exists("g:ag_apply_lmappings")
+  let g:ag_apply_lmappings = !exists("g:ag_lhandler")
+endif
+
+if !exists("g:ag_qhandler")
+  let g:ag_qhandler="botright copen"
+endif
+
+if !exists("g:ag_lhandler")
+  let g:ag_lhandler="botright lopen"
+endif
+
+function! s:Ag(cmd, args)
+  redraw
+  echo "Searching ..."
+
+  " If no pattern is provided, search for the word under the cursor
+  if empty(a:args)
+    let l:grepargs = expand("<cword>")
+  else
+    let l:grepargs = a:args . join(a:000, ' ')
+  end
+
+  " Format, used to manage column jump
+  if a:cmd =~# '-g$'
+    let g:agformat="%f"
+  else
+    let g:agformat="%f:%l:%c:%m"
+  end
+
+  let grepprg_bak=&grepprg
+  let grepformat_bak=&grepformat
+  try
+    let &grepprg=g:agprg
+    let &grepformat=g:agformat
+    silent execute a:cmd . " " . escape(l:grepargs, '|')
+  finally
+    let &grepprg=grepprg_bak
+    let &grepformat=grepformat_bak
+  endtry
+
+  if a:cmd =~# '^l'
+    exe g:ag_lhandler
+    let l:apply_mappings = g:ag_apply_lmappings
+  else
+    exe g:ag_qhandler
+    let l:apply_mappings = g:ag_apply_qmappings
+  endif
+
+  if l:apply_mappings
+    exec "nnoremap <silent> <buffer> q :ccl<CR>"
+    exec "nnoremap <silent> <buffer> t <C-W><CR><C-W>T"
+    exec "nnoremap <silent> <buffer> T <C-W><CR><C-W>TgT<C-W><C-W>"
+    exec "nnoremap <silent> <buffer> o <CR>"
+    exec "nnoremap <silent> <buffer> go <CR><C-W><C-W>"
+    exec "nnoremap <silent> <buffer> h <C-W><CR><C-W>K"
+    exec "nnoremap <silent> <buffer> H <C-W><CR><C-W>K<C-W>b"
+    exec "nnoremap <silent> <buffer> v <C-W><CR><C-W>H<C-W>b<C-W>J<C-W>t"
+    exec "nnoremap <silent> <buffer> gv <C-W><CR><C-W>H<C-W>b<C-W>J"
+  endif
+
+  " If highlighting is on, highlight the search keyword.
+  if exists("g:aghighlight")
+    let @/=a:args
+    set hlsearch
+  end
+
+  redraw!
+endfunction
+
+function! s:AgFromSearch(cmd, args)
+  let search =  getreg('/')
+  " translate vim regular expression to perl regular expression.
+  let search = substitute(search,'\(\\<\|\\>\)','\\b','g')
+  call s:Ag(a:cmd, '"' .  search .'" '. a:args)
+endfunction
+
+function! s:GetDocLocations()
+    let dp = ''
+    for p in split(&rtp,',')
+        let p = p.'/doc/'
+        if isdirectory(p)
+            let dp = p.'*.txt '.dp
+        endif
+    endfor
+    return dp
+endfunction
+
+function! s:AgHelp(cmd,args)
+    let args = a:args.' '.s:GetDocLocations()
+    call s:Ag(a:cmd,args)
+endfunction
+
+command! -bang -nargs=* -complete=file Ag call s:Ag('grep<bang>',<q-args>)
+command! -bang -nargs=* -complete=file AgAdd call s:Ag('grepadd<bang>', <q-args>)
+command! -bang -nargs=* -complete=file AgFromSearch call s:AgFromSearch('grep<bang>', <q-args>)
+command! -bang -nargs=* -complete=file LAg call s:Ag('lgrep<bang>', <q-args>)
+command! -bang -nargs=* -complete=file LAgAdd call s:Ag('lgrepadd<bang>', <q-args>)
+command! -bang -nargs=* -complete=file AgFile call s:Ag('grep<bang> -g', <q-args>)
+command! -bang -nargs=* -complete=help AgHelp call s:AgHelp('grep<bang>',<q-args>)
+command! -bang -nargs=* -complete=help LAgHelp call s:AgHelp('lgrep<bang>',<q-args>)
+command! -bang -nargs=+ -complete=file Ag let g:agprg = 'ag --nogroup --nocolor --column'
+      \|call s:Ag('grep<bang>',<q-args>)