refactor the initNerdTreeMirror function

This commit is contained in:
Martin Grenfell 2008-12-24 00:52:00 +13:00
parent 402fea6902
commit b09e0fbfa4

View File

@ -1803,6 +1803,8 @@ function! s:initNerdTreeInPlace(dir)
endfunction endfunction
" FUNCTION: s:initNerdTreeMirror() {{{2 " FUNCTION: s:initNerdTreeMirror() {{{2
function! s:initNerdTreeMirror() function! s:initNerdTreeMirror()
"get the names off all the nerd tree buffers
let treeBufNames = [] let treeBufNames = []
for i in range(1, tabpagenr("$")) for i in range(1, tabpagenr("$"))
let nextName = s:tabpagevar(i, 'NERDTreeBufName') let nextName = s:tabpagevar(i, 'NERDTreeBufName')
@ -1810,47 +1812,41 @@ function! s:initNerdTreeMirror()
call add(treeBufNames, nextName) call add(treeBufNames, nextName)
endif endif
endfor endfor
let treeBufNames = s:unique(treeBufNames)
let trees = {} "map the option names (that the user will be prompted with) to the nerd
for i in treeBufNames "tree buffer names
let treeRoot = getbufvar(i, "NERDTreeRoot") let options = {}
let trees[treeRoot.path.strForOS(0)] = i let i = 0
endfor while i < len(treeBufNames)
let bufName = treeBufNames[i]
let bufferName = '' let treeRoot = getbufvar(bufName, "NERDTreeRoot")
if len(keys(trees)) > 1 let options[i+1 . '. ' . treeRoot.path.strForOS(0) . ' (buf name: ' . bufName . ')'] = bufName
let options = insert(keys(trees), 'Select a tree to mirror:', 0) let i = i + 1
let i = 1
while i < len(options)
let options[i] = i . ' ' . options[i]
let i += 1
endwhile endwhile
let choice = inputlist(options) "work out which tree to mirror, if there is more than 1 then ask the user
let bufferName = ''
if len(keys(options)) > 1
let choice = inputlist(sort(keys(options)))
if choice < 1 || choice > len(options) || choice == '' if choice < 1 || choice > len(options) || choice == ''
return return
endif endif
let bufferName = options[keys(options)[choice-1]]
let bufferName = trees[keys(trees)[choice-1]] elseif len(keys(options)) == 1
elseif len(keys(trees)) == 1 let bufferName = values(options)[0]
let bufferName = values(trees)[0]
else else
return s:initNerdTree('') call s:echo("No trees to mirror")
return
endif endif
if s:treeExistsForTab() if s:treeExistsForTab() && s:isTreeOpen()
if s:isTreeOpen()
call s:closeTree() call s:closeTree()
endif endif
endif
let t:NERDTreeBufName = bufferName let t:NERDTreeBufName = bufferName
call s:createTreeWin() call s:createTreeWin()
exec 'buffer ' . bufferName exec 'buffer ' . bufferName
endfunction endfunction
" FUNCTION: s:tabpagevar(tabnr, var) {{{2 " FUNCTION: s:tabpagevar(tabnr, var) {{{2
function! s:tabpagevar(tabnr, var) function! s:tabpagevar(tabnr, var)
@ -1879,6 +1875,17 @@ endfunction
function! s:treeExistsForTab() function! s:treeExistsForTab()
return exists("t:NERDTreeBufName") return exists("t:NERDTreeBufName")
endfunction endfunction
" Function: s:unique(list) {{{2
" returns a:list without duplicates
function! s:unique(list)
let uniqlist = []
for elem in a:list
if index(uniqlist, elem) == -1
let uniqlist += [elem]
endif
endfor
return uniqlist
endfunction
" SECTION: Public Functions {{{1 " SECTION: Public Functions {{{1
"============================================================ "============================================================
"Returns the node that the cursor is currently on. "Returns the node that the cursor is currently on.