diff --git a/autoload/ag.vim b/autoload/ag.vim index 432ef444..f099ce09 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -60,7 +60,11 @@ if !exists("g:ag_working_path_mode") let g:ag_working_path_mode = 'c' endif -function! ag#AgBuffer(cmd, args) +if !exists("g:ag_default_window_type") + let g:ag_default_window_type = 'c' +endif + +function! ag#AgBuffer(cmd, args, ...) let l:bufs = filter(range(1, bufnr('$')), 'buflisted(v:val)') let l:files = [] for buf in l:bufs @@ -69,10 +73,10 @@ function! ag#AgBuffer(cmd, args) call add(l:files, l:file) endif endfor - call ag#Ag(a:cmd, a:args . ' ' . join(l:files, ' ')) + call ag#Ag(a:cmd, a:args . ' ' . join(l:files, ' '), a:000) endfunction -function! ag#Ag(cmd, args) +function! ag#Ag(cmd, args, ...) let l:ag_executable = get(split(g:ag_prg, " "), 0) " Ensure that `ag` is installed @@ -85,7 +89,7 @@ function! ag#Ag(cmd, args) if empty(a:args) let l:grepargs = expand("") else - let l:grepargs = a:args . join(a:000, ' ') + let l:grepargs = a:args end if empty(l:grepargs) @@ -95,7 +99,9 @@ function! ag#Ag(cmd, args) " Format, used to manage column jump if a:cmd =~# '-g$' - let s:ag_format_backup=g:ag_format + if exists("g:ag_format") + let s:ag_format_backup=g:ag_format + endif let g:ag_format="%f" elseif exists("s:ag_format_backup") let g:ag_format=s:ag_format_backup @@ -103,6 +109,25 @@ function! ag#Ag(cmd, args) let g:ag_format="%f:%l:%c:%m" endif + if a:0 == 1 + " Because a:000 is passed in from wrapper functions, check for that case + if type(a:1) == type([]) && len(a:1) > 0 + let l:matches_window_prefix = a:1[0] + elseif type(a:1) == type('') + let l:matches_window_prefix = a:1 + else + let l:matches_window_prefix = g:ag_default_window_type + end + else + let l:matches_window_prefix = g:ag_default_window_type + endif + + if l:matches_window_prefix == 'l' + let l:cmd = 'l' . a:cmd + else + let l:cmd = a:cmd + endif + let l:grepprg_bak=&grepprg let l:grepformat_bak=&grepformat let l:t_ti_bak=&t_ti @@ -120,11 +145,11 @@ function! ag#Ag(cmd, args) catch echom 'Failed to change directory to:'.l:cwd finally - silent! execute a:cmd . " " . escape(l:grepargs, '|') + silent! execute l:cmd . " " . escape(l:grepargs, '|') exe "lcd ".l:cwd_back endtry else " Someone chose an undefined value or 'c' so we revert to the default - silent! execute a:cmd . " " . escape(l:grepargs, '|') + silent! execute l:cmd . " " . escape(l:grepargs, '|') endif finally let &grepprg=l:grepprg_bak @@ -133,20 +158,18 @@ function! ag#Ag(cmd, args) let &t_te=l:t_te_bak endtry - if a:cmd =~# '^l' + if l:matches_window_prefix == 'l' let l:match_count = len(getloclist(winnr())) else let l:match_count = len(getqflist()) endif - if a:cmd =~# '^l' && l:match_count + if l:matches_window_prefix == '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. @@ -192,11 +215,11 @@ function! ag#Ag(cmd, args) endif endfunction -function! ag#AgFromSearch(cmd, args) +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) + call ag#Ag(a:cmd, '"' . search .'" '. a:args, a:000) endfunction function! ag#GetDocLocations() @@ -210,9 +233,9 @@ function! ag#GetDocLocations() return dp endfunction -function! ag#AgHelp(cmd,args) +function! ag#AgHelp(cmd,args,...) let args = a:args.' '.ag#GetDocLocations() - call ag#Ag(a:cmd,args) + call ag#Ag(a:cmd,args,a:000) endfunction function! s:guessProjectRoot() diff --git a/doc/ag.txt b/doc/ag.txt index e702c291..d8f7a6bf 100644 --- a/doc/ag.txt +++ b/doc/ag.txt @@ -11,13 +11,23 @@ the results in a split window. 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. + will open the result window for you. If [!] is not given the first + error is jumped to. By default, the results are displayed in the + |quickfix| list. This can be customized with the |g:ag_default_window_type| + option. + +:LAg [options] {pattern} [{directory}] *:LAg* + + Just like |:Ag| but matches are always placed in the |location-list|. + +:QAg [options] {pattern} [{directory}] *:QAg* + + Just like |:Ag| but matches are always placed in the |quickfix| list. :AgBuffer[!] [options] {pattern} *:AgBuffer* Search for {pattern} in all open buffers. Behaves just like the |:grep| - command, but will open the |Quickfix| window for you. If [!] is not given + command, but will open the result window for you. If [!] is not given the first error is jumped to. Note: this will not find changes in modified buffers, since ag can only @@ -25,29 +35,40 @@ the results in a split window. with the 'autowrite' option. A buffer will be ignored if it is a directory (an explorer, like netrw). +:LAgBuffer[!] [options] {pattern} *:LAgBuffer* + + Just like |:AgBuffer| but matches are always placed in the |location-list|. + +:QAgBuffer[!] [options] {pattern} *:QAgBuffer* + + Just like |:AgBuffer| but matches are always placed in the |quickfix| list. + :AgAdd [options] {pattern} [{directory}] *:AgAdd* Just like |:Ag|, but instead of making a new list, the matches are - appended to the current |quickfix| list. + appended to the current result list. -:AgFromSearch [{directory}] *:AgFromSearch* +:LAgAdd [options] {pattern} [{directory}] *:LAgAdd* - Just like |:Ag| but the pattern is from previous search. + Just like |:AgAdd| but matches are always placed in the |location-list|. -:LAg [options] {pattern} [{directory}] *:LAg* +:QAgAdd [options] {pattern} [{directory}] *:QAgAdd* - Just like |:Ag| but instead of the |quickfix| list, matches are placed in - the current |location-list|. + Just like |:AgAdd| but matches are always placed in the |quickfix| list. -:LAgBuffer [options] {pattern} *:LAgBuffer* +:AgFromSearch [{directory}] *:AgFromSearch* + + Just like |:Ag| but the pattern is from previous search. - Just like |:AgBuffer| but instead of the |quickfix| list, matches are - placed in the current |location-list|. +:LAgFromSearch [{directory}] *:LAgFromSearch* -:LAgAdd [options] {pattern} [{directory}] *:LAgAdd* + Just like |:AgFromSearch| but matches are always placed in the + |location-list|. - Just like |:AgAdd| but instead of the |quickfix| list, matches are added - to the current |location-list| +:QAgFromSearch [{directory}] *:QAgFromSearch* + + Just like |:AgFromSearch| but matches are always placed in the |quickfix| + list. :AgFile [options] {pattern} [{directory}] *:AgFile* @@ -55,15 +76,26 @@ the results in a split window. directory) for filenames matching the {pattern}. Behaves just like the |:grep| command, but will open the |Quickfix| window for you. +:LAgFile [options] {pattern} [{directory}] *:LAgFile* + + Just like |:AgFile| but matches are always placed in the |location-list|. + +:QAgFile [options] {pattern} [{directory}] *:QAgFile* + + Just like |:AgFile| but matches are always placed in the |quickfix| list. + :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* +:LAgHelp[!] [options] {pattern} *:LAgHelp* + + Just like |:AgHelp| but matches are always placed in the |location-list|. - Just like |:AgHelp| but instead of the |quickfix| list, matches are placed - in the current |location-list|. +:QAgHelp[!] [options] {pattern} *:QAgHelp* + + Just like |:AgHelp| but matches are always placed in the |quickfix| 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. on a @@ -145,6 +177,13 @@ the mappings are not applied (see |g:ag_apply_qmappings| and |g:ag_apply_lmappings| for more info. Default 1. Example: > let g:ag_mapping_message=0 < + *g:ag_default_window_type* +Specifies the window type to open with the results. Options are 'c' for +the |quickfix| list and "l" for the |location-list|. Both are always +accessible by using |:QAg| and |:LAg| respectively. Default "c". Example: > + let g:ag_default_window_type="l" +< + ============================================================================== MAPPINGS *ag-mappings* diff --git a/plugin/ag.vim b/plugin/ag.vim index 053f213c..eeb34769 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -1,11 +1,19 @@ " NOTE: You must, of course, install ag / the_silver_searcher command! -bang -nargs=* -complete=file Ag call ag#Ag('grep',) -command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep',) command! -bang -nargs=* -complete=file AgAdd call ag#Ag('grepadd', ) +command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep',) command! -bang -nargs=* -complete=file AgFromSearch call ag#AgFromSearch('grep', ) -command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep', ) -command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep',) -command! -bang -nargs=* -complete=file LAgAdd call ag#Ag('lgrepadd', ) command! -bang -nargs=* -complete=file AgFile call ag#Ag('grep -g', ) command! -bang -nargs=* -complete=help AgHelp call ag#AgHelp('grep',) -command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('lgrep',) +command! -bang -nargs=* -complete=file LAg call ag#Ag('grep', , 'l') +command! -bang -nargs=* -complete=file LAgAdd call ag#Ag('grepadd', , 'l') +command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('grep',, 'l') +command! -bang -nargs=* -complete=file LAgFromSearch call ag#AgFromSearch('grep', , 'l') +command! -bang -nargs=* -complete=file LAgFile call ag#Ag('grep -g', , 'l') +command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('grep',, 'l') +command! -bang -nargs=* -complete=file QAg call ag#Ag('grep', , 'c') +command! -bang -nargs=* -complete=file QAgAdd call ag#Ag('grepadd', , 'c') +command! -bang -nargs=* -complete=file QAgBuffer call ag#AgBuffer('grep',, 'c') +command! -bang -nargs=* -complete=file QAgFromSearch call ag#AgFromSearch('grep', , 'c') +command! -bang -nargs=* -complete=file QAgFile call ag#Ag('grep -g', , 'c') +command! -bang -nargs=* -complete=help QAgHelp call ag#AgHelp('grep',, 'c')