diff --git a/autoload/ack.vim b/autoload/ack.vim index b6afdba4..7b30dd10 100644 --- a/autoload/ack.vim +++ b/autoload/ack.vim @@ -15,6 +15,20 @@ endif " Public API "----------------------------------------------------------------------------- +function! s:FindProjectRoot() + let current_dir = expand('%:p:h') + while current_dir != '/' + for marker in g:ack_root_markers + let marker_path = current_dir . '/' . marker + if isdirectory(marker_path) || filereadable(marker_path) + return current_dir + endif + endfor + let current_dir = fnamemodify(current_dir, ':h') + endwhile + return '' +endfunction + function! ack#Ack(cmd, args) "{{{ call s:Init(a:cmd) redraw @@ -50,13 +64,14 @@ function! ack#Ack(cmd, args) "{{{ " NOTE: we escape special chars, but not everything using shellescape to " allow for passing arguments etc let l:escaped_args = escape(l:grepargs, '|#%') + let l:final_args = l:escaped_args.' '.s:FindProjectRoot() echo "Searching ..." if g:ack_use_dispatch - call s:SearchWithDispatch(l:grepprg, l:escaped_args, l:grepformat) + call s:SearchWithDispatch(l:grepprg, l:final_args, l:grepformat) else - call s:SearchWithGrep(a:cmd, l:grepprg, l:escaped_args, l:grepformat) + call s:SearchWithGrep(a:cmd, l:grepprg, l:final_args, l:grepformat) endif " Dispatch has no callback mechanism currently, we just have to display the diff --git a/doc/ack.txt b/doc/ack.txt index 22e884bc..14276527 100644 --- a/doc/ack.txt +++ b/doc/ack.txt @@ -232,6 +232,19 @@ Example: > let g:ack_use_cword_for_empty_search = 0 < + +g:ack_root_markers +Default: [''] + +Use this option to enable auto set search directory which contains ack_root_markers, +otherwise the search directory will be current directory. + +Example: +> + let g:ack_root_markers = ['.git', '.svn', '.hg'] +< + + ============================================================================== MAPPINGS *ack-mappings* diff --git a/plugin/ack.vim b/plugin/ack.vim index 202ae2ea..b56c3ba7 100644 --- a/plugin/ack.vim +++ b/plugin/ack.vim @@ -67,6 +67,10 @@ if !exists("g:ack_use_cword_for_empty_search") let g:ack_use_cword_for_empty_search = 1 endif +if !exists("g:ack_root_markers") + let g:ack_root_markers = [''] +endif + command! -bang -nargs=* -complete=file Ack call ack#Ack('grep', ) command! -bang -nargs=* -complete=file AckAdd call ack#Ack('grepadd', ) command! -bang -nargs=* -complete=file AckFromSearch call ack#AckFromSearch('grep', )