Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option to choose default result window #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
53 changes: 38 additions & 15 deletions autoload/ag.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -85,7 +89,7 @@ function! ag#Ag(cmd, args)
if empty(a:args)
let l:grepargs = expand("<cword>")
else
let l:grepargs = a:args . join(a:000, ' ')
let l:grepargs = a:args
end

if empty(l:grepargs)
Expand All @@ -95,14 +99,35 @@ 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
elseif !exists("g:ag_format")
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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
75 changes: 57 additions & 18 deletions doc/ag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,91 @@ 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
find what is on disk! You can save buffers automatically when searching
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*

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.

: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. <Enter> on a
Expand Down Expand Up @@ -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*
Expand Down
18 changes: 13 additions & 5 deletions plugin/ag.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
" NOTE: You must, of course, install ag / the_silver_searcher
command! -bang -nargs=* -complete=file Ag call ag#Ag('grep<bang>',<q-args>)
command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep<bang>',<q-args>)
command! -bang -nargs=* -complete=file AgAdd call ag#Ag('grepadd<bang>', <q-args>)
command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep<bang>',<q-args>)
command! -bang -nargs=* -complete=file AgFromSearch call ag#AgFromSearch('grep<bang>', <q-args>)
command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep<bang>', <q-args>)
command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep<bang>',<q-args>)
command! -bang -nargs=* -complete=file LAgAdd call ag#Ag('lgrepadd<bang>', <q-args>)
command! -bang -nargs=* -complete=file AgFile call ag#Ag('grep<bang> -g', <q-args>)
command! -bang -nargs=* -complete=help AgHelp call ag#AgHelp('grep<bang>',<q-args>)
command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('lgrep<bang>',<q-args>)
command! -bang -nargs=* -complete=file LAg call ag#Ag('grep<bang>', <q-args>, 'l')
command! -bang -nargs=* -complete=file LAgAdd call ag#Ag('grepadd<bang>', <q-args>, 'l')
command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('grep<bang>',<q-args>, 'l')
command! -bang -nargs=* -complete=file LAgFromSearch call ag#AgFromSearch('grep<bang>', <q-args>, 'l')
command! -bang -nargs=* -complete=file LAgFile call ag#Ag('grep<bang> -g', <q-args>, 'l')
command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('grep<bang>',<q-args>, 'l')
command! -bang -nargs=* -complete=file QAg call ag#Ag('grep<bang>', <q-args>, 'c')
command! -bang -nargs=* -complete=file QAgAdd call ag#Ag('grepadd<bang>', <q-args>, 'c')
command! -bang -nargs=* -complete=file QAgBuffer call ag#AgBuffer('grep<bang>',<q-args>, 'c')
command! -bang -nargs=* -complete=file QAgFromSearch call ag#AgFromSearch('grep<bang>', <q-args>, 'c')
command! -bang -nargs=* -complete=file QAgFile call ag#Ag('grep<bang> -g', <q-args>, 'c')
command! -bang -nargs=* -complete=help QAgHelp call ag#AgHelp('grep<bang>',<q-args>, 'c')