"we need to use this number many times for sorting... so we calculate it only "once here let s:NERDTreeSortStarIndex = index(g:NERDTreeSortOrder, '*') "CLASS: Path "============================================================ let s:Path = {} let g:NERDTreePath = s:Path "FUNCTION: Path.AbsolutePathFor(str) {{{1 function! s:Path.AbsolutePathFor(str) let prependCWD = 0 if nerdtree#runningWindows() let prependCWD = a:str !~# '^.:\(\\\|\/\)' && a:str !~# '^\(\\\\\|\/\/\)' else let prependCWD = a:str !~# '^/' endif let toReturn = a:str if prependCWD let toReturn = getcwd() . s:Path.Slash() . a:str endif return toReturn endfunction "FUNCTION: Path.bookmarkNames() {{{1 function! s:Path.bookmarkNames() if !exists("self._bookmarkNames") call self.cacheDisplayString() endif return self._bookmarkNames endfunction "FUNCTION: Path.cacheDisplayString() {{{1 function! s:Path.cacheDisplayString() let self.cachedDisplayString = self.getLastPathComponent(1) if self.isExecutable let self.cachedDisplayString = self.cachedDisplayString . '*' endif let self._bookmarkNames = [] for i in g:NERDTreeBookmark.Bookmarks() if i.path.equals(self) call add(self._bookmarkNames, i.name) endif endfor if !empty(self._bookmarkNames) let self.cachedDisplayString .= ' {' . join(self._bookmarkNames) . '}' endif if self.isSymLink let self.cachedDisplayString .= ' -> ' . self.symLinkDest endif if self.isReadOnly let self.cachedDisplayString .= ' [RO]' endif endfunction "FUNCTION: Path.changeToDir() {{{1 function! s:Path.changeToDir() let dir = self.str({'format': 'Cd'}) if self.isDirectory ==# 0 let dir = self.getParent().str({'format': 'Cd'}) endif try execute "cd " . dir call nerdtree#echo("CWD is now: " . getcwd()) catch throw "NERDTree.PathChangeError: cannot change CWD to " . dir endtry endfunction "FUNCTION: Path.compareTo() {{{1 " "Compares this Path to the given path and returns 0 if they are equal, -1 if "this Path is "less than" the given path, or 1 if it is "greater". " "Args: "path: the path object to compare this to " "Return: "1, -1 or 0 function! s:Path.compareTo(path) let thisPath = self.getLastPathComponent(1) let thatPath = a:path.getLastPathComponent(1) "if the paths are the same then clearly we return 0 if thisPath ==# thatPath return 0 endif let thisSS = self.getSortOrderIndex() let thatSS = a:path.getSortOrderIndex() "compare the sort sequences, if they are different then the return "value is easy if thisSS < thatSS return -1 elseif thisSS > thatSS return 1 else "if the sort sequences are the same then compare the paths "alphabetically let pathCompare = g:NERDTreeCaseSensitiveSort ? thisPath <# thatPath : thisPath limit let toReturn = "<" . strpart(toReturn, len(toReturn) - limit + 1) endif endif return toReturn endfunction "FUNCTION: Path._strForUI() {{{1 function! s:Path._strForUI() let toReturn = '/' . join(self.pathSegments, '/') if self.isDirectory && toReturn != '/' let toReturn = toReturn . '/' endif return toReturn endfunction "FUNCTION: Path._strForCd() {{{1 " " returns a string that can be used with :cd function! s:Path._strForCd() return escape(self.str(), nerdtree#escChars()) endfunction "FUNCTION: Path._strForEdit() {{{1 " "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 cwd = getcwd() . s:Path.Slash() "return a relative path if we can let isRelative = 0 if nerdtree#runningWindows() let isRelative = stridx(tolower(p), tolower(cwd)) == 0 else let isRelative = stridx(p, cwd) == 0 endif if isRelative let p = strpart(p, strlen(cwd)) "handle the edge case where the file begins with a + (vim interprets "the +foo in `:e +foo` as an option to :edit) if p[0] == "+" let p = '\' . p endif endif if p ==# '' let p = '.' endif return p endfunction "FUNCTION: Path._strForGlob() {{{1 function! s:Path._strForGlob() let lead = s:Path.Slash() "if we are running windows then slap a drive letter on the front if nerdtree#runningWindows() let lead = self.drive . '\' endif let toReturn = lead . join(self.pathSegments, s:Path.Slash()) if !nerdtree#runningWindows() let toReturn = escape(toReturn, nerdtree#escChars()) endif return toReturn endfunction "FUNCTION: Path._str() {{{1 " "Gets the string path for this path object that is appropriate for the OS. "EG, in windows c:\foo\bar " in *nix /foo/bar function! s:Path._str() let lead = s:Path.Slash() "if we are running windows then slap a drive letter on the front if nerdtree#runningWindows() let lead = self.drive . '\' endif return lead . join(self.pathSegments, s:Path.Slash()) endfunction "FUNCTION: Path.strTrunk() {{{1 "Gets the path without the last segment on the end. function! s:Path.strTrunk() return self.drive . '/' . join(self.pathSegments[0:-2], '/') endfunction " FUNCTION: Path.tabnr() {{{1 " return the number of the first tab that is displaying this file " " return 0 if no tab was found function! s:Path.tabnr() let str = self.str() for t in range(tabpagenr('$')) for b in tabpagebuflist(t+1) if str == expand('#' . b . ':p') return t+1 endif endfor endfor return 0 endfunction "FUNCTION: Path.WinToUnixPath(pathstr){{{1 "Takes in a windows path and returns the unix equiv " "A class level method " "Args: "pathstr: the windows path to convert function! s:Path.WinToUnixPath(pathstr) if !nerdtree#runningWindows() return a:pathstr endif let toReturn = a:pathstr "remove the x:\ of the front let toReturn = substitute(toReturn, '^.*:\(\\\|/\)\?', '/', "") "remove the \\ network share from the front let toReturn = substitute(toReturn, '^\(\\\\\|\/\/\)[^\\\/]*\(\\\|\/\)[^\\\/]*\(\\\|\/\)\?', '/', "") "convert all \ chars to / let toReturn = substitute(toReturn, '\', '/', "g") return toReturn endfunction " vim: set sw=4 sts=4 et fdm=marker: