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

Add user customizable function for searching for the project root. #127

Open
wants to merge 2 commits 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
19 changes: 13 additions & 6 deletions autoload/ag.vim
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if !exists("g:ag_mapping_message")
endif

if !exists("g:ag_working_path_mode")
let g:ag_working_path_mode = 'c'
let g:ag_working_path_mode = 'c'
endif

function! ag#AgBuffer(cmd, args)
Expand Down Expand Up @@ -114,17 +114,18 @@ function! ag#Ag(cmd, args)
set t_te=
if g:ag_working_path_mode ==? 'r' " Try to find the projectroot for current buffer
let l:cwd_back = getcwd()
let l:cwd = s:guessProjectRoot()
let l:cwd = g:Ag_get_project_root()
try
exe "lcd ".l:cwd
exe "lcd" l:cwd
echo 'Searching from' l:cwd
catch
echom 'Failed to change directory to:'.l:cwd
echom 'Failed to change directory to' l:cwd
finally
silent! execute a:cmd . " " . escape(l:grepargs, '|')
silent! execute a: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 a:cmd escape(l:grepargs, '|')
endif
finally
let &grepprg=l:grepprg_bak
Expand Down Expand Up @@ -232,3 +233,9 @@ function! s:guessProjectRoot()
" Nothing found, fallback to current working dir
return getcwd()
endfunction

if !exists("g:Ag_get_project_root")
let g:Ag_get_project_root = function("s:guessProjectRoot")
endif

" vim:ts=2:sw=2:et
13 changes: 11 additions & 2 deletions doc/ag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ For background, see: https://github.com/rking/ag.vim/pull/88
*g:ag_working_path_mode*
A mapping that describes where ag will be run. Default is the current working
directory. Specifying 'r' as the argument will tell it to run from the project
rootdirectory. For now any other mapping will result to the default.
Example:
rootdirectory. For now any other mapping will result to the default. The
function that locates the root directory can be customized with
|g:Ag_get_project_root|. Example: >
let g:ag_working_path_mode='r'
<

*g:ag_highlight*
If 1, highlight the search terms after searching. Default: 0. Example: >
Expand Down Expand Up @@ -146,6 +148,13 @@ the mappings are not applied (see |g:ag_apply_qmappings| and
let g:ag_mapping_message=0
<

*g:Ag_get_project_root*
An alternative function to use when searching for the project root when
|g:ag_working_path_mode| is set to 'r'. It should return the root directory
from which ag will be executed. Example: >
let g:Ag_get_project_root=function("s:guessProjectRoot")
<

==============================================================================
MAPPINGS *ag-mappings*

Expand Down