Skip to content

Commit 846d5b4

Browse files
author
Martin Grenfell
committed
latest nerd tree hacks
1 parent 7beadc9 commit 846d5b4

File tree

3 files changed

+94
-30
lines changed

3 files changed

+94
-30
lines changed

ftplugin/nerdtree_fs_menu.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ if exists("g:loaded_nerdtree_fs_menu")
1515
endif
1616
let g:loaded_nerdtree_fs_menu = 1
1717

18-
call NERDTreeAddMenuItem('(a)dd a childnode', 'a', 'NERDTreeAddNode')
19-
call NERDTreeAddMenuItem('(m)ove the curent node', 'm', 'NERDTreeMoveNode')
20-
call NERDTreeAddMenuItem('(d)elete the curent node', 'd', 'NERDTreeDeleteNode')
18+
call NERDTreeAddMenuItem({'text': '(a)dd a childnode', 'shortcut': 'a', 'callback': 'NERDTreeAddNode'})
19+
call NERDTreeAddMenuItem({'text': '(m)ove the curent node', 'shortcut': 'm', 'callback': 'NERDTreeMoveNode'})
20+
call NERDTreeAddMenuItem({'text': '(d)elete the curent node', 'shortcut': 'd', 'callback': 'NERDTreeDeleteNode'})
2121
if g:NERDTreePath.CopyingSupported()
22-
call NERDTreeAddMenuItem('(c)copy the current node', 'c', 'NERDTreeCopyNode')
22+
call NERDTreeAddMenuItem({'text': '(c)copy the current node', 'shortcut': 'c', 'callback': 'NERDTreeCopyNode'})
2323
endif
2424

2525
"FUNCTION: s:echo(msg){{{1

ftplugin/nerdtree_git_menu.vim

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,30 @@
1010
" See http://sam.zoy.org/wtfpl/COPYING for more details.
1111
"
1212
" ============================================================================
13+
"
14+
" Adds a "g" submenu to the NERD tree menu.
15+
"
16+
" Note: this plugin assumes that the current tree root has a .git dir under
17+
" it, and that the working tree and the .git repo are in the same place
18+
"
1319
if exists("g:loaded_nerdtree_git_menu")
1420
finish
1521
endif
1622
let g:loaded_nerdtree_git_menu = 1
1723

18-
call NERDTreeAddMenuItem('(g)it menu', 'g', 'NERDTreeGitMenu')
24+
call NERDTreeAddMenuItem({
25+
\ 'text': '(g)it menu',
26+
\ 'shortcut': 'g',
27+
\ 'check_to_enable_callback': 'NERDTreeGitMenuEnabled',
28+
\ 'callback': 'NERDTreeGitMenu' })
29+
30+
function! NERDTreeGitMenuEnabled()
31+
return isdirectory(s:GitRepoPath())
32+
endfunction
33+
34+
function! s:GitRepoPath()
35+
return b:NERDTreeRoot.path.str(0) . ".git"
36+
endfunction
1937

2038
function! NERDTreeGitMenu()
2139
let node = g:NERDTreeFileNode.GetSelected()
@@ -36,26 +54,34 @@ function! NERDTreeGitMenu()
3654
let choice = nr2char(getchar())
3755

3856
if choice ==# "a"
39-
call s:promptCommand('git add ', path.strForOS(1), 'file')
57+
call s:promptCommand('add ', path.strForOS(1), 'file')
4058
elseif choice ==# "c"
41-
call s:promptCommand('git checkout ', path.strForOS(1), 'file')
59+
call s:promptCommand('checkout ', path.strForOS(1), 'file')
4260
elseif choice ==# "m"
43-
call s:promptCommand('git mv ', path.strForOS(1), 'file')
61+
let p = path.strForOS(1)
62+
call s:promptCommand('mv ', p . ' ' . p, 'file')
4463
elseif choice ==# "r"
45-
call s:promptCommand('git rm ', path.strForOS(1), 'file')
64+
call s:promptCommand('rm ', path.strForOS(1), 'file')
4665
endif
4766

4867
call node.parent.refresh()
4968
call NERDTreeRender()
5069
endfunction
5170

52-
function! s:promptCommand(cmd_base, cmd_tail_default, complete)
53-
let cmd_tail = input(":!" . a:cmd_base, a:cmd_tail_default, a:complete)
71+
function! s:promptCommand(sub_command, cmd_tail_default, complete)
72+
let extra_options = ' --git-dir=' . s:GitRepoPath()
73+
let extra_options .= ' --work-tree=' . b:NERDTreeRoot.path.str(0) . ' '
74+
let base = "git" . extra_options . a:sub_command
75+
76+
let cmd_tail = input(":!" . base, a:cmd_tail_default, a:complete)
5477
if cmd_tail != ''
55-
let output = system(a:cmd_base . cmd_tail)
78+
let output = system(base . cmd_tail)
5679
redraw!
5780
if v:shell_error != 0
58-
echom output
81+
echo output
5982
endif
83+
else
84+
redraw
85+
echo "Aborted"
6086
endif
6187
endfunction

plugin/NERD_tree.vim

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ endfunction
429429
"CLASS: MenuCallback {{{2
430430
"============================================================
431431
let s:MenuCallback = {}
432-
433432
"FUNCTION: MenuCallback.All() {{{3
434433
function! s:MenuCallback.All()
435434
if !exists("s:menuCallbacks")
@@ -438,16 +437,58 @@ function! s:MenuCallback.All()
438437
return s:menuCallbacks
439438
endfunction
440439

441-
"FUNCTION: MenuCallback.Create(text, shortcut, callback) {{{3
442-
function! s:MenuCallback.Create(text, shortcut, callback)
440+
"FUNCTION: MenuCallback.AllEnabledCallbacks() {{{3
441+
function! s:MenuCallback.AllEnabledCallbacks()
442+
let toReturn = []
443+
for i in s:MenuCallback.All()
444+
if i.enabled()
445+
call add(toReturn, i)
446+
endif
447+
endfor
448+
return toReturn
449+
endfunction
450+
451+
"FUNCTION: MenuCallback.FindByShortcut(shortcut) {{{3
452+
function! s:MenuCallback.FindByShortcut(shortcut)
453+
for i in s:MenuCallback.All()
454+
if i.shortcut ==# a:shortcut
455+
return i
456+
endif
457+
endfor
458+
return {}
459+
endfunction
460+
461+
"FUNCTION: MenuCallback.Create(options) {{{3
462+
function! s:MenuCallback.Create(options)
443463
let newCallback = {}
444464
let newCallback = copy(self)
445-
let newCallback.text = a:text
446-
let newCallback.shortcut = a:shortcut
447-
let newCallback.callback = a:callback
465+
466+
let shortcut = a:options['shortcut']
467+
let callback = a:options['callback']
468+
469+
470+
let newCallback.text = a:options['text']
471+
let newCallback.shortcut = a:options['shortcut']
472+
let newCallback.callback = a:options['callback']
473+
if has_key(a:options, 'check_to_enable_callback')
474+
let newCallback.check_to_enable_callback = a:options['check_to_enable_callback']
475+
endif
448476
call add(s:MenuCallback.All(), newCallback)
449477
endfunction
450478

479+
"FUNCTION: MenuCallback.enabled() {{{3
480+
function! s:MenuCallback.enabled()
481+
if has_key(self, "check_to_enable_callback")
482+
return {self.check_to_enable_callback}()
483+
endif
484+
return 1
485+
endfunction
486+
487+
"FUNCTION: MenuCallback.execute() {{{3
488+
function! s:MenuCallback.execute()
489+
call {self.callback}()
490+
endfunction
491+
451492
"FUNCTION: MenuCallback.ShowMenu() {{{3
452493
function! s:MenuCallback.ShowMenu()
453494
let curNode = s:TreeFileNode.GetSelected()
@@ -459,23 +500,20 @@ function! s:MenuCallback.ShowMenu()
459500
let prompt = "NERDTree Menu\n" .
460501
\ "==========================================================\n"
461502

462-
for i in s:MenuCallback.All()
503+
for i in s:MenuCallback.AllEnabledCallbacks()
463504
let prompt .= i.text . "\n"
464505
endfor
465506

466507
echo prompt
467508

468-
let choice = nr2char(getchar())
509+
let callback = s:MenuCallback.FindByShortcut(nr2char(getchar()))
510+
if !empty(callback) && callback.enabled()
511+
redraw
512+
call callback.execute()
513+
endif
469514

470-
for i in s:MenuCallback.All()
471-
if choice ==# i.shortcut
472-
exec "call " . i.callback . "()"
473-
return
474-
endif
475-
endfor
476515
endfunction
477516

478-
479517
"CLASS: TreeFileNode {{{2
480518
"This class is the parent of the TreeDirNode class and constitures the
481519
"'Component' part of the composite design pattern between the treenode
@@ -2388,8 +2426,8 @@ function! NERDTreeGetCurrentPath()
23882426
endif
23892427
endfunction
23902428

2391-
function! NERDTreeAddMenuItem(text, shortcut, callback)
2392-
call s:MenuCallback.Create(a:text, a:shortcut, a:callback)
2429+
function! NERDTreeAddMenuItem(options)
2430+
call s:MenuCallback.Create(a:options)
23932431
endfunction
23942432

23952433
function! NERDTreeRender()

0 commit comments

Comments
 (0)