Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 58 additions & 35 deletions autoload/ag.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
" NOTE: You must, of course, install ag / the_silver_searcher

if exists('g:autoloaded_ag')
finish
endif
let g:autoloaded_ag = 1

" FIXME: Delete deprecated options below on or after 15-7 (6 months from when they were changed) {{{

if exists("g:agprg")
Expand Down Expand Up @@ -61,6 +66,12 @@ endfunction
function! ag#Ag(cmd, args)
let l:ag_executable = get(split(g:ag_prg, " "), 0)

if a:cmd =~# '^l'
let l:using_loclist = 1
else
let l:using_loclist = 0
endif

" Ensure that `ag` is installed
if !executable(l:ag_executable)
echoe "Ag command '" . l:ag_executable . "' was not found. Is the silver searcher installed and on your $PATH?"
Expand All @@ -84,30 +95,31 @@ function! ag#Ag(cmd, args)
let g:ag_format="%f:%l:%c:%m"
endif

let l:grepprg_bak=&grepprg
let l:grepformat_bak=&grepformat
let l:t_ti_bak=&t_ti
let l:t_te_bak=&t_te
let l:grepprg_bak = &l:grepprg
let l:grepformat_bak = &grepformat
let l:t_ti_bak = &t_ti
let l:t_te_bak = &t_te

try
let &grepprg=g:ag_prg
let &grepformat=g:ag_format
let &l:grepprg = g:ag_prg
let &grepformat = g:ag_format
set t_ti=
set t_te=
silent! execute a:cmd . " " . escape(l:grepargs, '|')
finally
let &grepprg=l:grepprg_bak
let &grepformat=l:grepformat_bak
let &t_ti=l:t_ti_bak
let &t_te=l:t_te_bak
let &l:grepprg = l:grepprg_bak
let &grepformat = l:grepformat_bak
let &t_ti = l:t_ti_bak
let &t_te = l:t_te_bak
endtry

if a:cmd =~# '^l'
if l:using_loclist
let l:match_count = len(getloclist(winnr()))
else
let l:match_count = len(getqflist())
endif

if a:cmd =~# '^l' && l:match_count
if l:using_loclist && l:match_count
exe g:ag_lhandler
let l:apply_mappings = g:ag_apply_lmappings
let l:matches_window_prefix = 'l' " we're using the location list
Expand All @@ -127,25 +139,21 @@ function! ag#Ag(cmd, args)

if l:match_count
if l:apply_mappings
nnoremap <silent> <buffer> h <C-W><CR><C-w>K
nnoremap <silent> <buffer> H <C-W><CR><C-w>K<C-w>b
nnoremap <silent> <buffer> o <CR>
nnoremap <silent> <buffer> t <C-w><CR><C-w>T
nnoremap <silent> <buffer> T <C-w><CR><C-w>TgT<C-W><C-W>
nnoremap <silent> <buffer> v <C-w><CR><C-w>H<C-W>b<C-W>J<C-W>t

exe 'nnoremap <silent> <buffer> e <CR><C-w><C-w>:' . l:matches_window_prefix .'close<CR>'
exe 'nnoremap <silent> <buffer> go <CR>:' . l:matches_window_prefix . 'open<CR>'
exe 'nnoremap <silent> <buffer> q :' . l:matches_window_prefix . 'close<CR>'

exe 'nnoremap <silent> <buffer> gv :let b:height=winheight(0)<CR><C-w><CR><C-w>H:' . l:matches_window_prefix . 'open<CR><C-w>J:exe printf(":normal %d\<lt>c-w>_", b:height)<CR>'
" Interpretation:
" :let b:height=winheight(0)<CR> Get the height of the quickfix/location list window
" <CR><C-w> Open the current item in a new split
" <C-w>H Slam the newly opened window against the left edge
" :copen<CR> -or- :lopen<CR> Open either the quickfix window or the location list (whichever we were using)
" <C-w>J Slam the quickfix/location list window against the bottom edge
" :exe printf(":normal %d\<lt>c-w>_", b:height)<CR> Restore the quickfix/location list window's height from before we opened the match
nnoremap <buffer> <silent> h <C-W><CR><C-w>K
nnoremap <buffer> <silent> H <C-W><CR><C-w>K<C-w>b
nnoremap <buffer> <silent> o <CR>
nnoremap <buffer> <silent> t <C-w><CR><C-w>T
nnoremap <buffer> <silent> T <C-w><CR><C-w>TgT<C-W><C-W>
nnoremap <buffer> <silent> v <C-w><CR><C-w>H<C-W>b<C-W>J<C-W>t

let l:closecmd = l:matches_window_prefix . 'close'
let l:opencmd = l:matches_window_prefix . 'open'

exe 'nnoremap <buffer> <silent> e <CR><C-w><C-w>:' . l:closecmd . '<CR>'
exe 'nnoremap <buffer> <silent> go <CR>:' . l:opencmd . '<CR>'
exe 'nnoremap <buffer> <silent> q :' . l:closecmd . '<CR>'

exe 'nnoremap <buffer> <silent> gv :call <SID>PreviewVertical("' . l:opencmd . '")<CR>'

if g:ag_mapping_message && l:apply_mappings
echom "ag.vim keys: q=quit <cr>/e/t/h/v=enter/edit/tab/split/vsplit go/T/H/gv=preview versions of same"
Expand All @@ -163,7 +171,16 @@ function! ag#AgFromSearch(cmd, args)
call ag#Ag(a:cmd, '"' . search .'" '. a:args)
endfunction

function! ag#GetDocLocations()
function! ag#AgHelp(cmd,args)
let args = a:args.' '.s:GetDocLocations()
call ag#Ag(a:cmd,args)
endfunction

"-----------------------------------------------------------------------------
" Private API
"-----------------------------------------------------------------------------

function! s:GetDocLocations()
let dp = ''
for p in split(&runtimepath,',')
let p = p.'/doc/'
Expand All @@ -174,7 +191,13 @@ function! ag#GetDocLocations()
return dp
endfunction

function! ag#AgHelp(cmd,args)
let args = a:args.' '.ag#GetDocLocations()
call ag#Ag(a:cmd,args)
" Called from within a list window, preserves its height after shuffling vsplit.
" The parameter indicates whether list was opened as copen or lopen.
function! s:PreviewVertical(opencmd)
let b:height = winheight(0) " Get the height of list window
exec "normal! \<C-w>\<CR>" | " Open current item in a new split
wincmd H " Slam newly opened window against the left edge
exec a:opencmd | " Move back to the list window
wincmd J " Slam the list window against the bottom edge
exec 'resize' b:height | " Restore the list window's height
endfunction