Permalink
Browse files

Add plugin ag.vim

  • Loading branch information...
1 parent 7dbecb0 commit 4dd2dc27219d22e4e1da47d0ccc813916e2e8e11 @lilydjwg committed Mar 4, 2013
Showing with 321 additions and 0 deletions.
  1. +115 −0 autoload/ag.vim
  2. +80 −0 doc/ag.txt
  3. +11 −0 doc/tags
  4. +115 −0 plugin/ag.vim
View
115 autoload/ag.vim
@@ -0,0 +1,115 @@
+" 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." --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>)
View
80 doc/ag.txt
@@ -0,0 +1,80 @@
+*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.
View
11 doc/tags
@@ -54,6 +54,11 @@
.snippet snipMate.txt /*.snippet*
.snippets snipMate.txt /*.snippets*
:Abolish abolish.txt /*:Abolish*
+:Ag ag.txt /*:Ag*
+:AgAdd ag.txt /*:AgAdd*
+:AgFile ag.txt /*:AgFile*
+:AgFromSearch ag.txt /*:AgFromSearch*
+:AgHelp ag.txt /*:AgHelp*
:AlignMapsClean Align.txt /*:AlignMapsClean*
:ColorSchemeEditor ColorSchemeEditor.txt /*:ColorSchemeEditor*
:Explore pi_netrw.txt /*:Explore*
@@ -107,6 +112,9 @@
:IndentGuidesDisable indent_guides.txt /*:IndentGuidesDisable*
:IndentGuidesEnable indent_guides.txt /*:IndentGuidesEnable*
:IndentGuidesToggle indent_guides.txt /*:IndentGuidesToggle*
+:LAg ag.txt /*:LAg*
+:LAgAdd ag.txt /*:LAgAdd*
+:LAgHelp ag.txt /*:LAgHelp*
:Linediff linediff.txt /*:Linediff*
:LinediffReset linediff.txt /*:LinediffReset*
:Mark mark.txt /*:Mark*
@@ -434,6 +442,9 @@ abolish-patterns abolish.txt /*abolish-patterns*
abolish-search abolish.txt /*abolish-search*
abolish-substitute abolish.txt /*abolish-substitute*
abolish.txt abolish.txt /*abolish.txt*
+ag ag.txt /*ag*
+ag-mappings ag.txt /*ag-mappings*
+ag.txt ag.txt /*ag.txt*
align Align.txt /*align*
align-align Align.txt /*align-align*
align-codepoint Align.txt /*align-codepoint*
View
115 plugin/ag.vim
@@ -0,0 +1,115 @@
+" 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." --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>)

0 comments on commit 4dd2dc2

Please sign in to comment.