nerdtree.vim 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. if exists('g:loaded_nerdtree_autoload')
  2. finish
  3. endif
  4. let g:loaded_nerdtree_autoload = 1
  5. let s:rootNERDTreePath = resolve(expand('<sfile>:p:h:h'))
  6. "FUNCTION: nerdtree#version(...) {{{1
  7. " If any value is given as an argument, the entire line of text from the
  8. " change log is shown for the current version; otherwise, only the version
  9. " number is shown.
  10. function! nerdtree#version(...) abort
  11. let l:text = 'Unknown'
  12. try
  13. let l:changelog = readfile(join([s:rootNERDTreePath, 'CHANGELOG.md'], nerdtree#slash()))
  14. let l:line = 0
  15. while l:line <= len(l:changelog)
  16. if l:changelog[l:line] =~# '\d\+\.\d\+'
  17. let l:text = substitute(l:changelog[l:line], '.*\(\d\+.\d\+\).*', '\1', '')
  18. let l:text .= substitute(l:changelog[l:line+1], '^.\{-}\(\.\d\+\).\{-}:\(.*\)', a:0>0 ? '\1:\2' : '\1', '')
  19. break
  20. endif
  21. let l:line += 1
  22. endwhile
  23. catch
  24. endtry
  25. return l:text
  26. endfunction
  27. " SECTION: General Functions {{{1
  28. "============================================================
  29. " FUNCTION: nerdtree#closeTreeOnOpen() {{{2
  30. function! nerdtree#closeTreeOnOpen() abort
  31. return g:NERDTreeQuitOnOpen == 1 || g:NERDTreeQuitOnOpen == 3
  32. endfunction
  33. " FUNCTION: nerdtree#closeBookmarksOnOpen() {{{2
  34. function! nerdtree#closeBookmarksOnOpen() abort
  35. return g:NERDTreeQuitOnOpen == 2 || g:NERDTreeQuitOnOpen == 3
  36. endfunction
  37. " FUNCTION: nerdtree#slash() {{{2
  38. " Return the path separator used by the underlying file system. Special
  39. " consideration is taken for the use of the 'shellslash' option on Windows
  40. " systems.
  41. function! nerdtree#slash() abort
  42. if nerdtree#runningWindows()
  43. if exists('+shellslash') && &shellslash
  44. return '/'
  45. endif
  46. return '\'
  47. endif
  48. return '/'
  49. endfunction
  50. "FUNCTION: nerdtree#checkForBrowse(dir) {{{2
  51. "inits a window tree in the current buffer if appropriate
  52. function! nerdtree#checkForBrowse(dir) abort
  53. if !isdirectory(a:dir)
  54. return
  55. endif
  56. if s:reuseWin(a:dir)
  57. return
  58. endif
  59. call g:NERDTreeCreator.CreateWindowTree(a:dir)
  60. endfunction
  61. "FUNCTION: s:reuseWin(dir) {{{2
  62. "finds a NERDTree buffer with root of dir, and opens it.
  63. function! s:reuseWin(dir) abort
  64. let path = g:NERDTreePath.New(fnamemodify(a:dir, ':p'))
  65. for i in range(1, bufnr('$'))
  66. unlet! nt
  67. let nt = getbufvar(i, 'NERDTree')
  68. if empty(nt)
  69. continue
  70. endif
  71. if nt.isWinTree() && nt.root.path.equals(path)
  72. call nt.setPreviousBuf(bufnr('#'))
  73. exec 'buffer ' . i
  74. return 1
  75. endif
  76. endfor
  77. return 0
  78. endfunction
  79. " FUNCTION: nerdtree#completeBookmarks(A,L,P) {{{2
  80. " completion function for the bookmark commands
  81. function! nerdtree#completeBookmarks(A,L,P) abort
  82. return filter(g:NERDTreeBookmark.BookmarkNames(), 'v:val =~# "^' . a:A . '"')
  83. endfunction
  84. "FUNCTION: nerdtree#compareNodes(n1, n2) {{{2
  85. function! nerdtree#compareNodes(n1, n2) abort
  86. return nerdtree#compareNodePaths(a:n1.path, a:n2.path)
  87. endfunction
  88. "FUNCTION: nerdtree#compareNodePaths(p1, p2) {{{2
  89. function! nerdtree#compareNodePaths(p1, p2) abort
  90. let sortKey1 = a:p1.getSortKey()
  91. let sortKey2 = a:p2.getSortKey()
  92. let i = 0
  93. while i < min([len(sortKey1), len(sortKey2)])
  94. " Compare chunks upto common length.
  95. " If chunks have different type, the one which has
  96. " integer type is the lesser.
  97. if type(sortKey1[i]) == type(sortKey2[i])
  98. if sortKey1[i] <# sortKey2[i]
  99. return - 1
  100. elseif sortKey1[i] ># sortKey2[i]
  101. return 1
  102. endif
  103. elseif type(sortKey1[i]) == type(0)
  104. return -1
  105. elseif type(sortKey2[i]) == type(0)
  106. return 1
  107. endif
  108. let i += 1
  109. endwhile
  110. " Keys are identical upto common length.
  111. " The key which has smaller chunks is the lesser one.
  112. if len(sortKey1) < len(sortKey2)
  113. return -1
  114. elseif len(sortKey1) > len(sortKey2)
  115. return 1
  116. else
  117. return 0
  118. endif
  119. endfunction
  120. " FUNCTION: nerdtree#deprecated(func, [msg]) {{{2
  121. " Issue a deprecation warning for a:func. If a second arg is given, use this
  122. " as the deprecation message
  123. function! nerdtree#deprecated(func, ...) abort
  124. let msg = a:0 ? a:func . ' ' . a:1 : a:func . ' is deprecated'
  125. if !exists('s:deprecationWarnings')
  126. let s:deprecationWarnings = {}
  127. endif
  128. if !has_key(s:deprecationWarnings, a:func)
  129. let s:deprecationWarnings[a:func] = 1
  130. echomsg msg
  131. endif
  132. endfunction
  133. " FUNCTION: nerdtree#exec(cmd, ignoreAll) {{{2
  134. " Same as :exec cmd but, if ignoreAll is TRUE, set eventignore=all for the duration
  135. function! nerdtree#exec(cmd, ignoreAll) abort
  136. let old_ei = &eventignore
  137. if a:ignoreAll
  138. set eventignore=all
  139. endif
  140. try
  141. exec a:cmd
  142. finally
  143. let &eventignore = old_ei
  144. endtry
  145. endfunction
  146. " FUNCTION: nerdtree#has_opt(options, name) {{{2
  147. function! nerdtree#has_opt(options, name) abort
  148. return has_key(a:options, a:name) && a:options[a:name] ==# 1
  149. endfunction
  150. " FUNCTION: nerdtree#loadClassFiles() {{{2
  151. function! nerdtree#loadClassFiles() abort
  152. runtime lib/nerdtree/path.vim
  153. runtime lib/nerdtree/menu_controller.vim
  154. runtime lib/nerdtree/menu_item.vim
  155. runtime lib/nerdtree/key_map.vim
  156. runtime lib/nerdtree/bookmark.vim
  157. runtime lib/nerdtree/tree_file_node.vim
  158. runtime lib/nerdtree/tree_dir_node.vim
  159. runtime lib/nerdtree/opener.vim
  160. runtime lib/nerdtree/creator.vim
  161. runtime lib/nerdtree/flag_set.vim
  162. runtime lib/nerdtree/nerdtree.vim
  163. runtime lib/nerdtree/ui.vim
  164. runtime lib/nerdtree/event.vim
  165. runtime lib/nerdtree/notifier.vim
  166. endfunction
  167. " FUNCTION: nerdtree#postSourceActions() {{{2
  168. function! nerdtree#postSourceActions() abort
  169. call g:NERDTreeBookmark.CacheBookmarks(1)
  170. call nerdtree#ui_glue#createDefaultBindings()
  171. "load all nerdtree plugins
  172. runtime! nerdtree_plugin/**/*.vim
  173. endfunction
  174. "FUNCTION: nerdtree#runningWindows(dir) {{{2
  175. function! nerdtree#runningWindows() abort
  176. return has('win16') || has('win32') || has('win64')
  177. endfunction
  178. "FUNCTION: nerdtree#runningCygwin(dir) {{{2
  179. function! nerdtree#runningCygwin() abort
  180. return has('win32unix')
  181. endfunction
  182. " SECTION: View Functions {{{1
  183. "============================================================
  184. "FUNCTION: nerdtree#echo {{{2
  185. "A wrapper for :echo. Appends 'NERDTree:' on the front of all messages
  186. "
  187. "Args:
  188. "msg: the message to echo
  189. function! nerdtree#echo(msg) abort
  190. redraw
  191. echomsg empty(a:msg) ? '' : ('NERDTree: ' . a:msg)
  192. endfunction
  193. "FUNCTION: nerdtree#echoError {{{2
  194. "Wrapper for nerdtree#echo, sets the message type to errormsg for this message
  195. "Args:
  196. "msg: the message to echo
  197. function! nerdtree#echoError(msg) abort
  198. echohl errormsg
  199. call nerdtree#echo(a:msg)
  200. echohl normal
  201. endfunction
  202. "FUNCTION: nerdtree#echoWarning {{{2
  203. "Wrapper for nerdtree#echo, sets the message type to warningmsg for this message
  204. "Args:
  205. "msg: the message to echo
  206. function! nerdtree#echoWarning(msg) abort
  207. echohl warningmsg
  208. call nerdtree#echo(a:msg)
  209. echohl normal
  210. endfunction
  211. "FUNCTION: nerdtree#renderView {{{2
  212. function! nerdtree#renderView() abort
  213. call b:NERDTree.render()
  214. endfunction
  215. " vim: set sw=4 sts=4 et fdm=marker: