" NOTE: You must, of course, install ag / the_silver_searcher " Location of the ag utility if !exists("g:agprg") let g:agprg="ag --column" endif if !exists("g:ag_apply_qmappings") let g:ag_apply_qmappings=1 endif if !exists("g:ag_apply_lmappings") let g:ag_apply_lmappings=1 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 if !exists("g:ag_mapping_message") let g:ag_mapping_message=1 endif function! ag#Ag(cmd, args) let l:ag_executable = get(split(g:agprg, " "), 0) " 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?" return endif " If no pattern is provided, search for the word under the cursor if empty(a:args) let l:grepargs = expand("") else let l:grepargs = a:args . join(a:000, ' ') end " Format, used to manage column jump if a:cmd =~# '-g$' let s:agformat_backup=g:agformat let g:agformat="%f" elseif exists("s:agformat_backup") let g:agformat=s:agformat_backup elseif !exists("g:agformat") let g:agformat="%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 try let &grepprg=g:agprg let &grepformat=g:agformat 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 endtry if a:cmd =~# '^l' let l:match_count = len(getloclist(winnr())) else let l:match_count = len(getqflist()) endif if a:cmd =~# '^l' && 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 elseif l:match_count exe g:ag_qhandler let l:apply_mappings = g:ag_apply_qmappings let l:matches_window_prefix = 'c' " we're using the quickfix window endif " If highlighting is on, highlight the search keyword. if exists("g:aghighlight") let @/=a:args set hlsearch end redraw! 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 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" endif endif else echom 'No matches for "'.a:args.'"' endif endfunction function! ag#AgFromSearch(cmd, args) let search = getreg('/') " translate vim regular expression to perl regular expression. let search = substitute(search,'\(\\<\|\\>\)','\\b','g') call ag#Ag(a:cmd, '"' . search .'" '. a:args) endfunction function! ag#GetDocLocations() let dp = '' for p in split(&runtimepath,',') let p = p.'/doc/' if isdirectory(p) let dp = p.'*.txt '.dp endif endfor return dp endfunction function! ag#AgHelp(cmd,args) let args = a:args.' '.ag#GetDocLocations() call ag#Ag(a:cmd,args) endfunction