@@ -429,7 +429,6 @@ endfunction
429429" CLASS: MenuCallback {{{2
430430" ============================================================
431431let s: MenuCallback = {}
432-
433432" FUNCTION: MenuCallback.All() {{{3
434433function ! s: MenuCallback .All ()
435434 if ! exists (" s:menuCallbacks" )
@@ -438,16 +437,58 @@ function! s:MenuCallback.All()
438437 return s: menuCallbacks
439438endfunction
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)
449477endfunction
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
452493function ! 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
476515endfunction
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
23892427endfunction
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 )
23932431endfunction
23942432
23952433function ! NERDTreeRender ()
0 commit comments