Skip to content

Instantly share code, notes, and snippets.

@mhinz
Created October 3, 2013 22:43
Show Gist options
  • Save mhinz/6818264 to your computer and use it in GitHub Desktop.
Save mhinz/6818264 to your computer and use it in GitHub Desktop.
if has_key(g:toplevel, 'cdlist')
let s:cdlist = g:toplevel.cdlist
else
let s:cdlist = [
\ [ '.git', 'finddir' ],
\ [ '.hg', 'finddir' ],
\ [ '.bzr', 'finddir' ],
\ [ 'foo', 'find' ],
\ ]
endif
" toplevel#cd#find {{{1
function! toplevel#cd#find(bang) abort
if exists('b:toplevel_cd')
return toplevel#cd_to_root(a:bang, b:toplevel_cd)
endif
let curdir = resolve(expand('<afile>:p:h')) .';'
for dir in s:cdlist
let root = (dir[1] == 'finddir') ? finddir(dir[0], curdir) : findfile(dir[0], curdir)
if !empty(root)
let b:toplevel_cd = fnamemodify(root, ':p:h:h')
return toplevel#cd_to_root(a:bang, b:toplevel_cd)
endif
endfor
echohl ErrorMsg
echo 'No VCS found.'
echohl NONE
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment