creator.vim (11193B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | "CLASS: Creator "Creates primary/secondary/mirror nerdtree windows. Sets up all the window and "buffer options and key mappings etc. "============================================================ let s:Creator = {} let g:NERDTreeCreator = s:Creator "FUNCTION: s:Creator._bindMappings() {{{1 function! s:Creator._bindMappings() "make <cr> do the same as the activate node mapping nnoremap <silent> <buffer> <cr> :call nerdtree#ui_glue#invokeKeyMap(g:NERDTreeMapActivateNode)<cr> call g:NERDTreeKeyMap.BindAll() command! -buffer -nargs=? Bookmark :call nerdtree#ui_glue#bookmarkNode('<args>') command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=1 RevealBookmark :call nerdtree#ui_glue#revealBookmark('<args>') command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=1 OpenBookmark :call nerdtree#ui_glue#openBookmark('<args>') command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=* ClearBookmarks call nerdtree#ui_glue#clearBookmarks('<args>') command! -buffer -complete=customlist,nerdtree#completeBookmarks -nargs=+ BookmarkToRoot call g:NERDTreeBookmark.ToRoot('<args>') command! -buffer -nargs=0 ClearAllBookmarks call g:NERDTreeBookmark.ClearAll() <bar> call b:NERDTree.render() command! -buffer -nargs=0 ReadBookmarks call g:NERDTreeBookmark.CacheBookmarks(0) <bar> call b:NERDTree.render() command! -buffer -nargs=0 WriteBookmarks call g:NERDTreeBookmark.Write() endfunction "FUNCTION: s:Creator._broadcastInitEvent() {{{1 function! s:Creator._broadcastInitEvent() silent doautocmd User NERDTreeInit endfunction " FUNCTION: s:Creator.BufNamePrefix() {{{2 function! s:Creator.BufNamePrefix() return 'NERD_tree_' endfunction "FUNCTION: s:Creator.CreatePrimary(a:name) {{{1 function! s:Creator.CreatePrimary(name) let creator = s:Creator.New() call creator.createPrimary(a:name) endfunction "FUNCTION: s:Creator.createPrimary(a:name) {{{1 "name: the name of a bookmark or a directory function! s:Creator.createPrimary(name) let path = self._pathForString(a:name) "abort if exception was thrown (bookmark/dir doesn't exist) if empty(path) return endif "if instructed to, then change the vim CWD to the dir the NERDTree is "inited in if g:NERDTreeChDirMode != 0 call path.changeToDir() endif if g:NERDTree.ExistsForTab() if g:NERDTree.IsOpen() call g:NERDTree.Close() endif unlet t:NERDTreeBufName endif call self._createTreeWin() call self._createNERDTree(path) let b:NERDTreeType = "primary" let b:treeShowHelp = 0 let b:NERDTreeIgnoreEnabled = 1 let b:NERDTreeShowFiles = g:NERDTreeShowFiles let b:NERDTreeShowHidden = g:NERDTreeShowHidden let b:NERDTreeShowBookmarks = g:NERDTreeShowBookmarks call b:NERDTree.render() call b:NERDTreeRoot.putCursorHere(0, 0) call self._broadcastInitEvent() endfunction "FUNCTION: s:Creator.CreateSecondary(dir) {{{1 function! s:Creator.CreateSecondary(dir) let creator = s:Creator.New() call creator.createSecondary(a:dir) endfunction "FUNCTION: s:Creator.createSecondary(dir) {{{1 function! s:Creator.createSecondary(dir) try let path = g:NERDTreePath.New(a:dir) catch /^NERDTree.InvalidArgumentsError/ call nerdtree#echo("Invalid directory name:" . a:name) return endtry "we want the directory buffer to disappear when we do the :edit below setlocal bufhidden=wipe let previousBuf = expand("#") "we need a unique name for each secondary tree buffer to ensure they are "all independent exec "silent edit " . self._nextBufferName() let b:NERDTreePreviousBuf = bufnr(previousBuf) call self._createNERDTree(path) call self._setCommonBufOptions() let b:NERDTreeType = "secondary" call b:NERDTree.render() call self._broadcastInitEvent() endfunction " FUNCTION: s:Creator._createNERDTree(path) {{{1 function! s:Creator._createNERDTree(path) let b:NERDTree = g:NERDTree.New(a:path) "TODO: This is kept for compatability only since many things use "b:NERDTreeRoot instead of the new NERDTree.root "Remove this one day let b:NERDTreeRoot = b:NERDTree.root call b:NERDTree.root.open() endfunction " FUNCTION: s:Creator.CreateMirror() {{{1 function! s:Creator.CreateMirror() let creator = s:Creator.New() call creator.createMirror() endfunction " FUNCTION: s:Creator.createMirror() {{{1 function! s:Creator.createMirror() "get the names off all the nerd tree buffers let treeBufNames = [] for i in range(1, tabpagenr("$")) let nextName = self._tabpagevar(i, 'NERDTreeBufName') if nextName != -1 && (!exists("t:NERDTreeBufName") || nextName != t:NERDTreeBufName) call add(treeBufNames, nextName) endif endfor let treeBufNames = self._uniq(treeBufNames) "map the option names (that the user will be prompted with) to the nerd "tree buffer names let options = {} let i = 0 while i < len(treeBufNames) let bufName = treeBufNames[i] let treeRoot = getbufvar(bufName, "NERDTreeRoot") let options[i+1 . '. ' . treeRoot.path.str() . ' (buf name: ' . bufName . ')'] = bufName let i = i + 1 endwhile "work out which tree to mirror, if there is more than 1 then ask the user let bufferName = '' if len(keys(options)) > 1 let choices = ["Choose a tree to mirror"] let choices = extend(choices, sort(keys(options))) let choice = inputlist(choices) if choice < 1 || choice > len(options) || choice ==# '' return endif let bufferName = options[sort(keys(options))[choice-1]] elseif len(keys(options)) ==# 1 let bufferName = values(options)[0] else call nerdtree#echo("No trees to mirror") return endif if g:NERDTree.ExistsForTab() && g:NERDTree.IsOpen() call g:NERDTree.Close() endif let t:NERDTreeBufName = bufferName call self._createTreeWin() exec 'buffer ' . bufferName if !&hidden call b:NERDTree.render() endif endfunction "FUNCTION: s:Creator._createTreeWin() {{{1 "Inits the NERD tree window. ie. opens it, sizes it, sets all the local "options etc function! s:Creator._createTreeWin() "create the nerd tree window let splitLocation = g:NERDTreeWinPos ==# "left" ? "topleft " : "botright " let splitSize = g:NERDTreeWinSize if !exists('t:NERDTreeBufName') let t:NERDTreeBufName = self._nextBufferName() silent! exec splitLocation . 'vertical ' . splitSize . ' new' silent! exec "edit " . t:NERDTreeBufName else silent! exec splitLocation . 'vertical ' . splitSize . ' split' silent! exec "buffer " . t:NERDTreeBufName endif setlocal winfixwidth call self._setCommonBufOptions() endfunction "FUNCTION: s:Creator.New() {{{1 function! s:Creator.New() let newCreator = copy(self) return newCreator endfunction " FUNCTION: s:Creator._nextBufferName() {{{2 " returns the buffer name for the next nerd tree function! s:Creator._nextBufferName() let name = s:Creator.BufNamePrefix() . self._nextBufferNumber() return name endfunction " FUNCTION: s:Creator._nextBufferNumber() {{{2 " the number to add to the nerd tree buffer name to make the buf name unique function! s:Creator._nextBufferNumber() if !exists("s:Creator._NextBufNum") let s:Creator._NextBufNum = 1 else let s:Creator._NextBufNum += 1 endif return s:Creator._NextBufNum endfunction "FUNCTION: s:Creator._pathForString(str) {{{1 "find a bookmark or adirectory for the given string function! s:Creator._pathForString(str) let path = {} if g:NERDTreeBookmark.BookmarkExistsFor(a:str) let path = g:NERDTreeBookmark.BookmarkFor(a:str).path else let dir = a:str ==# '' ? getcwd() : a:str "hack to get an absolute path if a relative path is given if dir =~# '^\.' let dir = getcwd() . g:NERDTreePath.Slash() . dir endif let dir = g:NERDTreePath.Resolve(dir) try let path = g:NERDTreePath.New(dir) catch /^NERDTree.InvalidArgumentsError/ call nerdtree#echo("No bookmark or directory found for: " . a:str) return endtry endif if !path.isDirectory let path = path.getParent() endif return path endfunction "FUNCTION: s:Creator._setCommonBufOptions() {{{1 function! s:Creator._setCommonBufOptions() "throwaway buffer options setlocal noswapfile setlocal buftype=nofile setlocal bufhidden=hide setlocal nowrap setlocal foldcolumn=0 setlocal foldmethod=manual setlocal nofoldenable setlocal nobuflisted setlocal nospell if g:NERDTreeShowLineNumbers setlocal nu else setlocal nonu if v:version >= 703 setlocal nornu endif endif iabc <buffer> if g:NERDTreeHighlightCursorline setlocal cursorline endif call self._setupStatusline() let b:treeShowHelp = 0 let b:NERDTreeIgnoreEnabled = 1 let b:NERDTreeShowFiles = g:NERDTreeShowFiles let b:NERDTreeShowHidden = g:NERDTreeShowHidden let b:NERDTreeShowBookmarks = g:NERDTreeShowBookmarks call self._bindMappings() setlocal filetype=nerdtree endfunction "FUNCTION: s:Creator._setupStatusline() {{{1 function! s:Creator._setupStatusline() if g:NERDTreeStatusline != -1 let &l:statusline = g:NERDTreeStatusline 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() call creator.togglePrimary(a:dir) endfunction "FUNCTION: s:Creator.togglePrimary(dir) {{{1 "Toggles the NERD tree. I.e the NERD tree is open, it is closed, if it is "closed it is restored or initialized (if it doesnt exist) " "Args: "dir: the full path for the root node (is only used if the NERD tree is being "initialized. function! s:Creator.togglePrimary(dir) if g:NERDTree.ExistsForTab() if !g:NERDTree.IsOpen() call self._createTreeWin() if !&hidden call b:NERDTree.render() endif call b:NERDTree.ui.restoreScreenState() else call g:NERDTree.Close() endif else call self.createPrimary(a: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: |