diff --git a/ftplugin/nerdtree_git_menu.vim b/ftplugin/nerdtree_git_menu.vim index ed354d9..bc6e4bd 100644 --- a/ftplugin/nerdtree_git_menu.vim +++ b/ftplugin/nerdtree_git_menu.vim @@ -10,12 +10,30 @@ " See http://sam.zoy.org/wtfpl/COPYING for more details. " " ============================================================================ +" +" Adds a "g" submenu to the NERD tree menu. +" +" Note: this plugin assumes that the current tree root has a .git dir under +" it, and that the working tree and the .git repo are in the same place +" if exists("g:loaded_nerdtree_git_menu") finish endif let g:loaded_nerdtree_git_menu = 1 -call NERDTreeAddMenuItem('(g)it menu', 'g', 'NERDTreeGitMenu') +call NERDTreeAddMenuItem({ + \ 'text': '(g)it menu', + \ 'shortcut': 'g', + \ 'check_to_enable_callback': 'NERDTreeGitMenuEnabled', + \ 'callback': 'NERDTreeGitMenu' }) + +function! NERDTreeGitMenuEnabled() + return isdirectory(s:GitRepoPath()) +endfunction + +function! s:GitRepoPath() + return b:NERDTreeRoot.path.str(0) . ".git" +endfunction function! NERDTreeGitMenu() let node = g:NERDTreeFileNode.GetSelected() @@ -36,26 +54,34 @@ function! NERDTreeGitMenu() let choice = nr2char(getchar()) if choice ==# "a" - call s:promptCommand('git add ', path.strForOS(1), 'file') + call s:promptCommand('add ', path.strForOS(1), 'file') elseif choice ==# "c" - call s:promptCommand('git checkout ', path.strForOS(1), 'file') + call s:promptCommand('checkout ', path.strForOS(1), 'file') elseif choice ==# "m" - call s:promptCommand('git mv ', path.strForOS(1), 'file') + let p = path.strForOS(1) + call s:promptCommand('mv ', p . ' ' . p, 'file') elseif choice ==# "r" - call s:promptCommand('git rm ', path.strForOS(1), 'file') + call s:promptCommand('rm ', path.strForOS(1), 'file') endif call node.parent.refresh() call NERDTreeRender() endfunction -function! s:promptCommand(cmd_base, cmd_tail_default, complete) - let cmd_tail = input(":!" . a:cmd_base, a:cmd_tail_default, a:complete) +function! s:promptCommand(sub_command, cmd_tail_default, complete) + let extra_options = ' --git-dir=' . s:GitRepoPath() + let extra_options .= ' --work-tree=' . b:NERDTreeRoot.path.str(0) . ' ' + let base = "git" . extra_options . a:sub_command + + let cmd_tail = input(":!" . base, a:cmd_tail_default, a:complete) if cmd_tail != '' - let output = system(a:cmd_base . cmd_tail) + let output = system(base . cmd_tail) redraw! if v:shell_error != 0 - echom output + echo output endif + else + redraw + echo "Aborted" endif endfunction