nerdtree

A tree explorer plugin for vim.
Index Commits Files Refs
commit f982f61e8a1dfdd66e6741f79bb71eb8408875fb
parent 86488b1aa027f66465e1f740f7d425a30ca98c50
Author: Martin Grenfell <martin.grenfell@gmail.com>
Date:   Mon,  7 Jul 2014 20:12:05 +0100

continue breaking up the autoload/nerdtree god module

Diffstat:
Mautoload/nerdtree.vim | 263+------------------------------------------------------------------------------
Mautoload/nerdtree/ui_glue.vim | 164+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mlib/nerdtree/creator.vim | 34++++++++++++++++++++++++++++++++--
Mlib/nerdtree/key_map.vim | 2+-
Mlib/nerdtree/opener.vim | 78+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mlib/nerdtree/path.vim | 17+++++++++++++----
Mplugin/NERD_tree.vim | 11++---------
7 files changed, 281 insertions(+), 288 deletions(-)
diff --git a/autoload/nerdtree.vim b/autoload/nerdtree.vim
@@ -9,29 +9,6 @@ endfunction
 
 " SECTION: General Functions {{{1
 "============================================================
-"FUNCTION: nerdtree#bufInWindows(bnum){{{2
-"[[STOLEN FROM VTREEEXPLORER.VIM]]
-"Determine the number of windows open to this buffer number.
-"Care of Yegappan Lakshman.  Thanks!
-"
-"Args:
-"bnum: the subject buffers buffer number
-function! nerdtree#bufInWindows(bnum)
-    let cnt = 0
-    let winnum = 1
-    while 1
-        let bufnum = winbufnr(winnum)
-        if bufnum < 0
-            break
-        endif
-        if bufnum ==# a:bnum
-            let cnt = cnt + 1
-        endif
-        let winnum = winnum + 1
-    endwhile
-
-    return cnt
-endfunction
 
 "FUNCTION: nerdtree#checkForBrowse(dir) {{{2
 "inits a secondary nerd tree in the current buffer if appropriate
@@ -57,7 +34,6 @@ function! nerdtree#compareNodes(n1, n2)
     return a:n1.path.compareTo(a:n2.path)
 endfunction
 
-
 " FUNCTION: nerdtree#deprecated(func, [msg]) {{{2
 " Issue a deprecation warning for a:func. If a second arg is given, use this
 " as the deprecation message
@@ -73,15 +49,6 @@ function! nerdtree#deprecated(func, ...)
     endif
 endfunction
 
-"FUNCTION: nerdtree#escChars(dir) {{{2
-function! nerdtree#escChars()
-    if nerdtree#runningWindows()
-        return " `\|\"#%&,?()\*^<>"
-    endif
-
-    return " \\`\|\"#%&,?()\*^<>[]"
-endfunction
-
 " FUNCTION: nerdtree#exec(cmd) {{{2
 " same as :exec cmd  but eventignore=all is set for the duration
 function! nerdtree#exec(cmd)
@@ -91,68 +58,11 @@ function! nerdtree#exec(cmd)
     let &ei = old_ei
 endfunction
 
-" FUNCTION: nerdtree#findAndRevealPath() {{{2
-function! nerdtree#findAndRevealPath()
-    try
-        let p = g:NERDTreePath.New(expand("%:p"))
-    catch /^NERDTree.InvalidArgumentsError/
-        call nerdtree#echo("no file for the current buffer")
-        return
-    endtry
-
-    if p.isUnixHiddenPath()
-        let showhidden=g:NERDTreeShowHidden
-        let g:NERDTreeShowHidden = 1
-    endif
-
-    if !nerdtree#treeExistsForTab()
-        try
-            let cwd = g:NERDTreePath.New(getcwd())
-        catch /^NERDTree.InvalidArgumentsError/
-            call nerdtree#echo("current directory does not exist.")
-            let cwd = p.getParent()
-        endtry
-
-        if p.isUnder(cwd)
-            call g:NERDTreeCreator.CreatePrimary(cwd.str())
-        else
-            call g:NERDTreeCreator.CreatePrimary(p.getParent().str())
-        endif
-    else
-        if !p.isUnder(g:NERDTreeFileNode.GetRootForTab().path)
-            if !nerdtree#isTreeOpen()
-                call g:NERDTreeCreator.TogglePrimary('')
-            else
-                call nerdtree#putCursorInTreeWin()
-            endif
-            let b:NERDTreeShowHidden = g:NERDTreeShowHidden
-            call nerdtree#chRoot(g:NERDTreeDirNode.New(p.getParent()))
-        else
-            if !nerdtree#isTreeOpen()
-                call g:NERDTreeCreator.TogglePrimary("")
-            endif
-        endif
-    endif
-    call nerdtree#putCursorInTreeWin()
-    call b:NERDTreeRoot.reveal(p)
-
-    if p.isUnixHiddenFile()
-        let g:NERDTreeShowHidden = showhidden
-    endif
-endfunction
-
 " FUNCTION: nerdtree#has_opt(options, name) {{{2
 function! nerdtree#has_opt(options, name)
     return has_key(a:options, a:name) && a:options[a:name] == 1
 endfunction
 
-" FUNCTION: nerdtree#invokeKeyMap(key) {{{2
-"this is needed since I cant figure out how to invoke dict functions from a
-"key map
-function! nerdtree#invokeKeyMap(key)
-    call g:NERDTreeKeyMap.Invoke(a:key)
-endfunction
-
 " FUNCTION: nerdtree#loadClassFiles() {{{2
 function! nerdtree#loadClassFiles()
     runtime lib/nerdtree/path.vim
@@ -180,24 +90,6 @@ function! nerdtree#runningWindows()
     return has("win16") || has("win32") || has("win64")
 endfunction
 
-" FUNCTION: nerdtree#tabpagevar(tabnr, var) {{{2
-function! nerdtree#tabpagevar(tabnr, var)
-    let currentTab = tabpagenr()
-    let old_ei = &ei
-    set ei=all
-
-    exec "tabnext " . a:tabnr
-    let v = -1
-    if exists('t:' . a:var)
-        exec 'let v = t:' . a:var
-    endif
-    exec "tabnext " . currentTab
-
-    let &ei = old_ei
-
-    return v
-endfunction
-
 " Function: nerdtree#treeExistsForBuffer()   {{{2
 " Returns 1 if a nerd tree root exists in the current buffer
 function! nerdtree#treeExistsForBuf()
@@ -229,54 +121,6 @@ function! nerdtree#treeWid()
     return 2
 endfunction
 
-"FUNCTION: nerdtree#upDir(keepState) {{{2
-"moves the tree up a level
-"
-"Args:
-"keepState: 1 if the current root should be left open when the tree is
-"re-rendered
-function! nerdtree#upDir(keepState)
-    let cwd = b:NERDTreeRoot.path.str({'format': 'UI'})
-    if cwd ==# "/" || cwd =~# '^[^/]..$'
-        call nerdtree#echo("already at top dir")
-    else
-        if !a:keepState
-            call b:NERDTreeRoot.close()
-        endif
-
-        let oldRoot = b:NERDTreeRoot
-
-        if empty(b:NERDTreeRoot.parent)
-            let path = b:NERDTreeRoot.path.getParent()
-            let newRoot = g:NERDTreeDirNode.New(path)
-            call newRoot.open()
-            call newRoot.transplantChild(b:NERDTreeRoot)
-            let b:NERDTreeRoot = newRoot
-        else
-            let b:NERDTreeRoot = b:NERDTreeRoot.parent
-        endif
-
-        if g:NERDTreeChDirMode ==# 2
-            call b:NERDTreeRoot.path.changeToDir()
-        endif
-
-        call nerdtree#renderView()
-        call oldRoot.putCursorHere(0, 0)
-    endif
-endfunction
-
-" Function: nerdtree#unique(list)   {{{2
-" returns a:list without duplicates
-function! nerdtree#unique(list)
-  let uniqlist = []
-  for elem in a:list
-    if index(uniqlist, elem) ==# -1
-      let uniqlist += [elem]
-    endif
-  endfor
-  return uniqlist
-endfunction
-
 " SECTION: View Functions {{{1
 "============================================================
 "
@@ -299,6 +143,7 @@ endfunction
 function! nerdtree#chRoot(node)
     call s:chRoot(a:node)
 endfunction
+
 "FUNCTION: nerdtree#closeTree() {{{2
 "Closes the primary NERD tree window for this tab
 function! nerdtree#closeTree()
@@ -476,23 +321,6 @@ function! nerdtree#echoWarning(msg)
     echohl normal
 endfunction
 
-"FUNCTION: nerdtree#firstUsableWindow(){{{2
-"find the window number of the first normal window
-function! nerdtree#firstUsableWindow()
-    let i = 1
-    while i <= winnr("$")
-        let bnum = winbufnr(i)
-        if bnum != -1 && getbufvar(bnum, '&buftype') ==# ''
-                    \ && !getwinvar(i, '&previewwindow')
-                    \ && (!getbufvar(bnum, '&modified') || &hidden)
-            return i
-        endif
-
-        let i += 1
-    endwhile
-    return -1
-endfunction
-
 "FUNCTION: nerdtree#getPath(ln) {{{2
 "Gets the full path to the node that is rendered on the given line number
 "
@@ -588,95 +416,6 @@ function! nerdtree#isTreeOpen()
     return nerdtree#getTreeWinNum() != -1
 endfunction
 
-"FUNCTION: nerdtree#isWindowUsable(winnumber) {{{2
-"Returns 0 if opening a file from the tree in the given window requires it to
-"be split, 1 otherwise
-"
-"Args:
-"winnumber: the number of the window in question
-function! nerdtree#isWindowUsable(winnumber)
-    "gotta split if theres only one window (i.e. the NERD tree)
-    if winnr("$") ==# 1
-        return 0
-    endif
-
-    let oldwinnr = winnr()
-    call nerdtree#exec(a:winnumber . "wincmd p")
-    let specialWindow = getbufvar("%", '&buftype') != '' || getwinvar('%', '&previewwindow')
-    let modified = &modified
-    call nerdtree#exec(oldwinnr . "wincmd p")
-
-    "if its a special window e.g. quickfix or another explorer plugin then we
-    "have to split
-    if specialWindow
-        return 0
-    endif
-
-    if &hidden
-        return 1
-    endif
-
-    return !modified || nerdtree#bufInWindows(winbufnr(a:winnumber)) >= 2
-endfunction
-
-" FUNCTION: nerdtree#jumpToChild(direction) {{{2
-" Args:
-" direction: 0 if going to first child, 1 if going to last
-function! nerdtree#jumpToChild(currentNode, direction)
-    if a:currentNode.isRoot()
-        return nerdtree#echo("cannot jump to " . (a:direction ? "last" : "first") .  " child")
-    end
-    let dirNode = a:currentNode.parent
-    let childNodes = dirNode.getVisibleChildren()
-
-    let targetNode = childNodes[0]
-    if a:direction
-        let targetNode = childNodes[len(childNodes) - 1]
-    endif
-
-    if targetNode.equals(a:currentNode)
-        let siblingDir = a:currentNode.parent.findOpenDirSiblingWithVisibleChildren(a:direction)
-        if siblingDir != {}
-            let indx = a:direction ? siblingDir.getVisibleChildCount()-1 : 0
-            let targetNode = siblingDir.getChildByIndex(indx, 1)
-        endif
-    endif
-
-    call targetNode.putCursorHere(1, 0)
-
-    call nerdtree#centerView()
-endfunction
-
-" FUNCTION: nerdtree#jumpToSibling(currentNode, forward) {{{2
-" moves the cursor to the sibling of the current node in the given direction
-"
-" Args:
-" forward: 1 if the cursor should move to the next sibling, 0 if it should
-" move back to the previous sibling
-function! nerdtree#jumpToSibling(currentNode, forward)
-    let sibling = a:currentNode.findSibling(a:forward)
-
-    if !empty(sibling)
-        call sibling.putCursorHere(1, 0)
-        call nerdtree#centerView()
-    endif
-endfunction
-
-"FUNCTION: nerdtree#promptToDelBuffer(bufnum, msg){{{2
-"prints out the given msg and, if the user responds by pushing 'y' then the
-"buffer with the given bufnum is deleted
-"
-"Args:
-"bufnum: the buffer that may be deleted
-"msg: a message that will be echoed to the user asking them if they wish to
-"     del the buffer
-function! nerdtree#promptToDelBuffer(bufnum, msg)
-    echo a:msg
-    if nr2char(getchar()) ==# 'y'
-        exec "silent bdelete! " . a:bufnum
-    endif
-endfunction
-
 "FUNCTION: nerdtree#putCursorOnBookmarkTable(){{{2
 "Places the cursor at the top of the bookmarks table
 function! nerdtree#putCursorOnBookmarkTable()
diff --git a/autoload/nerdtree/ui_glue.vim b/autoload/nerdtree/ui_glue.vim
@@ -86,9 +86,10 @@ endfunction
 "handle the user activating the updir line
 function! s:activateAll()
     if getline(".") ==# nerdtree#treeUpDirLine()
-        return nerdtree#upDir(0)
+        return nerdtree#ui_glue#upDir(0)
     endif
 endfunction
+
 "FUNCTION: s:activateDirNode() {{{1
 "handle the user activating a tree node
 function! s:activateDirNode(node)
@@ -245,6 +246,56 @@ function! s:displayHelp()
     call nerdtree#centerView()
 endfunction
 
+" FUNCTION: s:findAndRevealPath() {{{1
+function! s:findAndRevealPath()
+    try
+        let p = g:NERDTreePath.New(expand("%:p"))
+    catch /^NERDTree.InvalidArgumentsError/
+        call nerdtree#echo("no file for the current buffer")
+        return
+    endtry
+
+    if p.isUnixHiddenPath()
+        let showhidden=g:NERDTreeShowHidden
+        let g:NERDTreeShowHidden = 1
+    endif
+
+    if !nerdtree#treeExistsForTab()
+        try
+            let cwd = g:NERDTreePath.New(getcwd())
+        catch /^NERDTree.InvalidArgumentsError/
+            call nerdtree#echo("current directory does not exist.")
+            let cwd = p.getParent()
+        endtry
+
+        if p.isUnder(cwd)
+            call g:NERDTreeCreator.CreatePrimary(cwd.str())
+        else
+            call g:NERDTreeCreator.CreatePrimary(p.getParent().str())
+        endif
+    else
+        if !p.isUnder(g:NERDTreeFileNode.GetRootForTab().path)
+            if !nerdtree#isTreeOpen()
+                call g:NERDTreeCreator.TogglePrimary('')
+            else
+                call nerdtree#putCursorInTreeWin()
+            endif
+            let b:NERDTreeShowHidden = g:NERDTreeShowHidden
+            call nerdtree#chRoot(g:NERDTreeDirNode.New(p.getParent()))
+        else
+            if !nerdtree#isTreeOpen()
+                call g:NERDTreeCreator.TogglePrimary("")
+            endif
+        endif
+    endif
+    call nerdtree#putCursorInTreeWin()
+    call b:NERDTreeRoot.reveal(p)
+
+    if p.isUnixHiddenFile()
+        let g:NERDTreeShowHidden = showhidden
+    endif
+endfunction
+
 "FUNCTION: s:handleLeftClick() {{{1
 "Checks if the click should open the current node
 function! s:handleLeftClick()
@@ -296,16 +347,52 @@ function! s:handleMiddleMouse()
     endif
 endfunction
 
+" FUNCTION: s:jumpToChild(direction) {{{2
+" Args:
+" direction: 0 if going to first child, 1 if going to last
+function! s:jumpToChild(currentNode, direction)
+    if a:currentNode.isRoot()
+        return nerdtree#echo("cannot jump to " . (a:direction ? "last" : "first") .  " child")
+    end
+    let dirNode = a:currentNode.parent
+    let childNodes = dirNode.getVisibleChildren()
+
+    let targetNode = childNodes[0]
+    if a:direction
+        let targetNode = childNodes[len(childNodes) - 1]
+    endif
+
+    if targetNode.equals(a:currentNode)
+        let siblingDir = a:currentNode.parent.findOpenDirSiblingWithVisibleChildren(a:direction)
+        if siblingDir != {}
+            let indx = a:direction ? siblingDir.getVisibleChildCount()-1 : 0
+            let targetNode = siblingDir.getChildByIndex(indx, 1)
+        endif
+    endif
+
+    call targetNode.putCursorHere(1, 0)
+
+    call nerdtree#centerView()
+endfunction
+
+
+" FUNCTION: nerdtree#ui_glue#invokeKeyMap(key) {{{1
+"this is needed since I cant figure out how to invoke dict functions from a
+"key map
+function! nerdtree#ui_glue#invokeKeyMap(key)
+    call g:NERDTreeKeyMap.Invoke(a:key)
+endfunction
+
 " FUNCTION: s:jumpToFirstChild() {{{1
 " wrapper for the jump to child method
 function! s:jumpToFirstChild(node)
-    call nerdtree#jumpToChild(a:node, 0)
+    call s:jumpToChild(a:node, 0)
 endfunction
 
 " FUNCTION: s:jumpToLastChild() {{{1
 " wrapper for the jump to child method
 function! s:jumpToLastChild(node)
-    call nerdtree#jumpToChild(a:node, 1)
+    call s:jumpToChild(a:node, 1)
 endfunction
 
 " FUNCTION: s:jumpToParent(node) {{{1
@@ -328,12 +415,27 @@ endfunction
 
 " FUNCTION: s:jumpToNextSibling(node) {{{1
 function! s:jumpToNextSibling(node)
-    call nerdtree#jumpToSibling(a:node, 1)
+    call s:jumpToSibling(a:node, 1)
 endfunction
 
 " FUNCTION: s:jumpToPrevSibling(node) {{{1
 function! s:jumpToPrevSibling(node)
-    call nerdtree#jumpToSibling(a:node, 0)
+    call s:jumpToSibling(a:node, 0)
+endfunction
+
+" FUNCTION: s:jumpToSibling(currentNode, forward) {{{2
+" moves the cursor to the sibling of the current node in the given direction
+"
+" Args:
+" forward: 1 if the cursor should move to the next sibling, 0 if it should
+" move back to the previous sibling
+function! s:jumpToSibling(currentNode, forward)
+    let sibling = a:currentNode.findSibling(a:forward)
+
+    if !empty(sibling)
+        call sibling.putCursorHere(1, 0)
+        call nerdtree#centerView()
+    endif
 endfunction
 
 " FUNCTION: nerdtree#ui_glue#openBookmark(name) {{{1
@@ -441,6 +543,18 @@ function! s:refreshCurrent(node)
     call nerdtree#echo("Refreshing node. This could take a while... DONE")
 endfunction
 
+" FUNCTION: nerdtree#ui_glue#setupCommands() {{{1
+function! nerdtree#ui_glue#setupCommands()
+    command! -n=? -complete=dir -bar NERDTree :call g:NERDTreeCreator.CreatePrimary('<args>')
+    command! -n=? -complete=dir -bar NERDTreeToggle :call g:NERDTreeCreator.TogglePrimary('<args>')
+    command! -n=0 -bar NERDTreeClose :call nerdtree#closeTreeIfOpen()
+    command! -n=1 -complete=customlist,nerdtree#completeBookmarks -bar NERDTreeFromBookmark call g:NERDTreeCreator.CreatePrimary('<args>')
+    command! -n=0 -bar NERDTreeMirror call g:NERDTreeCreator.CreateMirror()
+    command! -n=0 -bar NERDTreeFind call nerdtree#ui_glue#findAndRevealPath()
+    command! -n=0 -bar NERDTreeFocus call NERDTreeFocus()
+    command! -n=0 -bar NERDTreeCWD call NERDTreeCWD()
+endfunction
+
 " Function: s:SID()   {{{1
 function s:SID()
     if !exists("s:sid")
@@ -505,14 +619,50 @@ function! s:toggleZoom()
     endif
 endfunction
 
+"FUNCTION: nerdtree#ui_glue#upDir(keepState) {{{1
+"moves the tree up a level
+"
+"Args:
+"keepState: 1 if the current root should be left open when the tree is
+"re-rendered
+function! nerdtree#ui_glue#upDir(keepState)
+    let cwd = b:NERDTreeRoot.path.str({'format': 'UI'})
+    if cwd ==# "/" || cwd =~# '^[^/]..$'
+        call nerdtree#echo("already at top dir")
+    else
+        if !a:keepState
+            call b:NERDTreeRoot.close()
+        endif
+
+        let oldRoot = b:NERDTreeRoot
+
+        if empty(b:NERDTreeRoot.parent)
+            let path = b:NERDTreeRoot.path.getParent()
+            let newRoot = g:NERDTreeDirNode.New(path)
+            call newRoot.open()
+            call newRoot.transplantChild(b:NERDTreeRoot)
+            let b:NERDTreeRoot = newRoot
+        else
+            let b:NERDTreeRoot = b:NERDTreeRoot.parent
+        endif
+
+        if g:NERDTreeChDirMode ==# 2
+            call b:NERDTreeRoot.path.changeToDir()
+        endif
+
+        call nerdtree#renderView()
+        call oldRoot.putCursorHere(0, 0)
+    endif
+endfunction
+
 " FUNCTION: s:upDirCurrentRootOpen() {{{1
 function! s:upDirCurrentRootOpen()
-    call nerdtree#upDir(1)
+    call nerdtree#ui_glue#upDir(1)
 endfunction
 
 " FUNCTION: s:upDirCurrentRootClosed() {{{1
 function! s:upDirCurrentRootClosed()
-    call nerdtree#upDir(0)
+    call nerdtree#ui_glue#upDir(0)
 endfunction
 
 " vim: set sw=4 sts=4 et fdm=marker:
diff --git a/lib/nerdtree/creator.vim b/lib/nerdtree/creator.vim
@@ -122,12 +122,12 @@ function! s:Creator.createMirror()
     "get the names off all the nerd tree buffers
     let treeBufNames = []
     for i in range(1, tabpagenr("$"))
-        let nextName = nerdtree#tabpagevar(i, 'NERDTreeBufName')
+        let nextName = self._tabpagevar(i, 'NERDTreeBufName')
         if nextName != -1 && (!exists("t:NERDTreeBufName") || nextName != t:NERDTreeBufName)
             call add(treeBufNames, nextName)
         endif
     endfor
-    let treeBufNames = nerdtree#unique(treeBufNames)
+    let treeBufNames = self._uniq(treeBufNames)
 
     "map the option names (that the user will be prompted with) to the nerd
     "tree buffer names
@@ -290,6 +290,24 @@ function! s:Creator._setupStatusline()
     endif
 endfunction
 
+" FUNCTION: s:Creator._tabpagevar(tabnr, var) {{{1
+function! s:Creator._tabpagevar(tabnr, var)
+    let currentTab = tabpagenr()
+    let old_ei = &ei
+    set ei=all
+
+    exec "tabnext " . a:tabnr
+    let v = -1
+    if exists('t:' . a:var)
+        exec 'let v = t:' . a:var
+    endif
+    exec "tabnext " . currentTab
+
+    let &ei = old_ei
+
+    return v
+endfunction
+
 "FUNCTION: s:Creator.TogglePrimary(dir) {{{1
 function! s:Creator.TogglePrimary(dir)
     let creator = s:Creator.New()
@@ -319,4 +337,16 @@ function! s:Creator.togglePrimary(dir)
     endif
 endfunction
 
+" Function: s:Creator._uniq(list)   {{{1
+" returns a:list without duplicates
+function! s:Creator._uniq(list)
+  let uniqlist = []
+  for elem in a:list
+    if index(uniqlist, elem) ==# -1
+      let uniqlist += [elem]
+    endif
+  endfor
+  return uniqlist
+endfunction
+
 " vim: set sw=4 sts=4 et fdm=marker:
diff --git a/lib/nerdtree/key_map.vim b/lib/nerdtree/key_map.vim
@@ -44,7 +44,7 @@ function! s:KeyMap.bind()
 
     let premap = self.key == "<LeftRelease>" ? " <LeftRelease>" : " "
 
-    exec 'nnoremap <buffer> <silent> '. self.key . premap . ':call nerdtree#invokeKeyMap("'. keymapInvokeString .'")<cr>'
+    exec 'nnoremap <buffer> <silent> '. self.key . premap . ':call nerdtree#ui_glue#invokeKeyMap("'. keymapInvokeString .'")<cr>'
 endfunction
 
 "FUNCTION: KeyMap.Remove(key, scope) {{{1
diff --git a/lib/nerdtree/opener.vim b/lib/nerdtree/opener.vim
@@ -3,6 +3,29 @@
 let s:Opener = {}
 let g:NERDTreeOpener = s:Opener
 
+"FUNCTION: s:Opener._bufInWindows(bnum){{{1
+"[[STOLEN FROM VTREEEXPLORER.VIM]]
+"Determine the number of windows open to this buffer number.
+"Care of Yegappan Lakshman.  Thanks!
+"
+"Args:
+"bnum: the subject buffers buffer number
+function! s:Opener._bufInWindows(bnum)
+    let cnt = 0
+    let winnum = 1
+    while 1
+        let bufnum = winbufnr(winnum)
+        if bufnum < 0
+            break
+        endif
+        if bufnum ==# a:bnum
+            let cnt = cnt + 1
+        endif
+        let winnum = winnum + 1
+    endwhile
+
+    return cnt
+endfunction
 "FUNCTION: Opener._checkToCloseTree(newtab) {{{1
 "Check the class options and global options (i.e. NERDTreeQuitOnOpen) to see
 "if the tree should be closed now.
@@ -21,6 +44,24 @@ function! s:Opener._checkToCloseTree(newtab)
     endif
 endfunction
 
+
+"FUNCTION: s:Opener._firstUsableWindow(){{{1
+"find the window number of the first normal window
+function! s:Opener._firstUsableWindow()
+    let i = 1
+    while i <= winnr("$")
+        let bnum = winbufnr(i)
+        if bnum != -1 && getbufvar(bnum, '&buftype') ==# ''
+                    \ && !getwinvar(i, '&previewwindow')
+                    \ && (!getbufvar(bnum, '&modified') || &hidden)
+            return i
+        endif
+
+        let i += 1
+    endwhile
+    return -1
+endfunction
+
 "FUNCTION: Opener._gotoTargetWin() {{{1
 function! s:Opener._gotoTargetWin()
     if b:NERDTreeType ==# "secondary"
@@ -48,6 +89,37 @@ function! s:Opener._gotoTargetWin()
     endif
 endfunction
 
+"FUNCTION: s:Opener._isWindowUsable(winnumber) {{{1
+"Returns 0 if opening a file from the tree in the given window requires it to
+"be split, 1 otherwise
+"
+"Args:
+"winnumber: the number of the window in question
+function! s:Opener._isWindowUsable(winnumber)
+    "gotta split if theres only one window (i.e. the NERD tree)
+    if winnr("$") ==# 1
+        return 0
+    endif
+
+    let oldwinnr = winnr()
+    call nerdtree#exec(a:winnumber . "wincmd p")
+    let specialWindow = getbufvar("%", '&buftype') != '' || getwinvar('%', '&previewwindow')
+    let modified = &modified
+    call nerdtree#exec(oldwinnr . "wincmd p")
+
+    "if its a special window e.g. quickfix or another explorer plugin then we
+    "have to split
+    if specialWindow
+        return 0
+    endif
+
+    if &hidden
+        return 1
+    endif
+
+    return !modified || self._bufInWindows(winbufnr(a:winnumber)) >= 2
+endfunction
+
 "FUNCTION: Opener.New(path, opts) {{{1
 "Args:
 "
@@ -206,12 +278,12 @@ endfunction
 
 "FUNCTION: Opener._previousWindow() {{{1
 function! s:Opener._previousWindow()
-    if !nerdtree#isWindowUsable(winnr("#")) && nerdtree#firstUsableWindow() ==# -1
+    if !self._isWindowUsable(winnr("#")) && self._firstUsableWindow() ==# -1
         call self._newSplit()
     else
         try
-            if !nerdtree#isWindowUsable(winnr("#"))
-                call nerdtree#exec(nerdtree#firstUsableWindow() . "wincmd w")
+            if !self._isWindowUsable(winnr("#"))
+                call nerdtree#exec(self._firstUsableWindow() . "wincmd w")
             else
                 call nerdtree#exec('wincmd p')
             endif
diff --git a/lib/nerdtree/path.vim b/lib/nerdtree/path.vim
@@ -170,7 +170,7 @@ function! s:Path.copy(dest)
 
     let dest = s:Path.WinToUnixPath(a:dest)
 
-    let cmd = g:NERDTreeCopyCmd . " " . escape(self.str(), nerdtree#escChars()) . " " . escape(dest, nerdtree#escChars())
+    let cmd = g:NERDTreeCopyCmd . " " . escape(self.str(), self._escChars()) . " " . escape(dest, self._escChars())
     let success = system(cmd)
     if success != 0
         throw "NERDTree.CopyError: Could not copy ''". self.str() ."'' to: '" . a:dest . "'"
@@ -289,6 +289,15 @@ function! s:Path.exists()
     return filereadable(p) || isdirectory(p)
 endfunction
 
+"FUNCTION: Path._escChars() {{{1
+function! s:Path._escChars()
+    if nerdtree#runningWindows()
+        return " `\|\"#%&,?()\*^<>"
+    endif
+
+    return " \\`\|\"#%&,?()\*^<>[]"
+endfunction
+
 "FUNCTION: Path.getDir() {{{1
 "
 "Returns this path if it is a directory, else this paths parent.
@@ -625,7 +634,7 @@ endfunction
 "
 " returns a string that can be used with :cd
 function! s:Path._strForCd()
-    return escape(self.str(), nerdtree#escChars())
+    return escape(self.str(), self._escChars())
 endfunction
 
 "FUNCTION: Path._strForEdit() {{{1
@@ -633,7 +642,7 @@ endfunction
 "Return: the string for this path that is suitable to be used with the :edit
 "command
 function! s:Path._strForEdit()
-    let p = escape(self.str({'format': 'UI'}), nerdtree#escChars())
+    let p = escape(self.str({'format': 'UI'}), self._escChars())
     let cwd = getcwd() . s:Path.Slash()
 
     "return a relative path if we can
@@ -673,7 +682,7 @@ function! s:Path._strForGlob()
     let toReturn = lead . join(self.pathSegments, s:Path.Slash())
 
     if !nerdtree#runningWindows()
-        let toReturn = escape(toReturn, nerdtree#escChars())
+        let toReturn = escape(toReturn, self._escChars())
     endif
     return toReturn
 endfunction
diff --git a/plugin/NERD_tree.vim b/plugin/NERD_tree.vim
@@ -142,15 +142,8 @@ call nerdtree#loadClassFiles()
 
 " SECTION: Commands {{{1
 "============================================================
-"init the command that users start the nerd tree with
-command! -n=? -complete=dir -bar NERDTree :call g:NERDTreeCreator.CreatePrimary('<args>')
-command! -n=? -complete=dir -bar NERDTreeToggle :call g:NERDTreeCreator.TogglePrimary('<args>')
-command! -n=0 -bar NERDTreeClose :call nerdtree#closeTreeIfOpen()
-command! -n=1 -complete=customlist,nerdtree#completeBookmarks -bar NERDTreeFromBookmark call g:NERDTreeCreator.CreatePrimary('<args>')
-command! -n=0 -bar NERDTreeMirror call g:NERDTreeCreator.CreateMirror()
-command! -n=0 -bar NERDTreeFind call nerdtree#findAndRevealPath()
-command! -n=0 -bar NERDTreeFocus call NERDTreeFocus()
-command! -n=0 -bar NERDTreeCWD call NERDTreeCWD()
+call nerdtree#ui_glue#setupCommands()
+
 " SECTION: Auto commands {{{1
 "============================================================
 augroup NERDTree