vcs.vim 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. " ============================================================================
  2. " File: vcs.vim
  3. " Description: NERDTree plugin that provides a command to open on the root of
  4. " a version control system repository.
  5. " Maintainer: Phil Runninger
  6. " License: This program is free software. It comes without any warranty,
  7. " to the extent permitted by applicable law. You can redistribute
  8. " it and/or modify it under the terms of the Do What The Fuck You
  9. " Want To Public License, Version 2, as published by Sam Hocevar.
  10. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  11. "
  12. " ============================================================================
  13. command! -n=? -complete=dir -bar NERDTreeVCS :call <SID>CreateTabTreeVCS('<args>')
  14. command! -n=? -complete=dir -bar NERDTreeToggleVCS :call <SID>ToggleTabTreeVCS('<args>')
  15. " FUNCTION: s:CreateTabTreeVCS(a:name) {{{1
  16. function! s:CreateTabTreeVCS(name)
  17. let l:path = g:NERDTreeCreator._pathForString(a:name)
  18. let l:path = s:FindParentVCSRoot(l:path)
  19. call g:NERDTreeCreator.createTabTree(empty(l:path) ? '' : l:path._str())
  20. endfunction
  21. " FUNCTION: s:ToggleTabTreeVCS(a:name) {{{1
  22. " Behaves the same as ToggleTabTree except roots directory at VCS root
  23. function! s:ToggleTabTreeVCS(name)
  24. let l:path = g:NERDTreeCreator._pathForString(a:name)
  25. let l:path = s:FindParentVCSRoot(l:path)
  26. call g:NERDTreeCreator.toggleTabTree(empty(l:path) ? '' : l:path._str())
  27. endfunction
  28. " FUNCTION: s:FindParentVCSRoot(a:path) {{{1
  29. " Finds the root version control system folder of the given path. If a:path is
  30. " not part of a repository, return the original path.
  31. function! s:FindParentVCSRoot(path)
  32. let l:path = a:path
  33. while !empty(l:path) &&
  34. \ l:path._str() !~# '^\(\a:[\\\/]\|\/\)$' &&
  35. \ !isdirectory(l:path._str() . '/.git') &&
  36. \ !isdirectory(l:path._str() . '/.svn') &&
  37. \ !isdirectory(l:path._str() . '/.hg') &&
  38. \ !isdirectory(l:path._str() . '/.bzr') &&
  39. \ !isdirectory(l:path._str() . '/_darcs')
  40. let l:path = l:path.getParent()
  41. endwhile
  42. return (empty(l:path) || l:path._str() =~# '^\(\a:[\\\/]\|\/\)$') ? a:path : l:path
  43. endfunction