diff --git a/autoload/ag.vim b/autoload/ag.vim index a5139d4f..bd46ed60 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -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") @@ -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?" @@ -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 @@ -127,25 +139,21 @@ function! ag#Ag(cmd, args) if l:match_count if l:apply_mappings - nnoremap h K - nnoremap H Kb - nnoremap o - nnoremap t T - nnoremap T TgT - nnoremap v HbJt - - exe 'nnoremap e :' . l:matches_window_prefix .'close' - exe 'nnoremap go :' . l:matches_window_prefix . 'open' - exe 'nnoremap q :' . l:matches_window_prefix . 'close' - - exe 'nnoremap gv :let b:height=winheight(0)H:' . l:matches_window_prefix . 'openJ:exe printf(":normal %d\c-w>_", b:height)' - " Interpretation: - " :let b:height=winheight(0) Get the height of the quickfix/location list window - " Open the current item in a new split - " H Slam the newly opened window against the left edge - " :copen -or- :lopen Open either the quickfix window or the location list (whichever we were using) - " J Slam the quickfix/location list window against the bottom edge - " :exe printf(":normal %d\c-w>_", b:height) Restore the quickfix/location list window's height from before we opened the match + nnoremap h K + nnoremap H Kb + nnoremap o + nnoremap t T + nnoremap T TgT + nnoremap v HbJt + + let l:closecmd = l:matches_window_prefix . 'close' + let l:opencmd = l:matches_window_prefix . 'open' + + exe 'nnoremap e :' . l:closecmd . '' + exe 'nnoremap go :' . l:opencmd . '' + exe 'nnoremap q :' . l:closecmd . '' + + exe 'nnoremap gv :call PreviewVertical("' . l:opencmd . '")' if g:ag_mapping_message && l:apply_mappings echom "ag.vim keys: q=quit /e/t/h/v=enter/edit/tab/split/vsplit go/T/H/gv=preview versions of same" @@ -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/' @@ -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! \\" | " 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