A tree explorer plugin for vim.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.9 KiB

  1. " ============================================================================
  2. " File: fs_menu.vim
  3. " Description: plugin for the NERD Tree that provides a file system menu
  4. " Maintainer: Martin Grenfell <martin.grenfell at gmail dot com>
  5. " Last Change: 17 July, 2009
  6. " License: This program is free software. It comes without any warranty,
  7. " to the extent permitted by applicable law. You can redistribute
  8. " it and/or modify it under the terms of the Do What The Fuck You
  9. " Want To Public License, Version 2, as published by Sam Hocevar.
  10. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  11. "
  12. " ============================================================================
  13. if exists("g:loaded_nerdtree_fs_menu")
  14. finish
  15. endif
  16. let g:loaded_nerdtree_fs_menu = 1
  17. call NERDTreeAddMenuItem({'text': '(a)dd a childnode', 'shortcut': 'a', 'callback': 'NERDTreeAddNode'})
  18. call NERDTreeAddMenuItem({'text': '(m)ove the current node', 'shortcut': 'm', 'callback': 'NERDTreeMoveNode'})
  19. call NERDTreeAddMenuItem({'text': '(d)elete the current node', 'shortcut': 'd', 'callback': 'NERDTreeDeleteNode'})
  20. if has("gui_mac") || has("gui_macvim")
  21. call NERDTreeAddMenuItem({'text': '(r)eveal in Finder the current node', 'shortcut': 'r', 'callback': 'NERDTreeRevealInFinder'})
  22. call NERDTreeAddMenuItem({'text': '(o)pen the current node with system editor', 'shortcut': 'o', 'callback': 'NERDTreeExecuteFile'})
  23. call NERDTreeAddMenuItem({'text': '(q)uicklook the current node', 'shortcut': 'q', 'callback': 'NERDTreeQuickLook'})
  24. endif
  25. if g:NERDTreePath.CopyingSupported()
  26. call NERDTreeAddMenuItem({'text': '(c)copy the current node', 'shortcut': 'c', 'callback': 'NERDTreeCopyNode'})
  27. endif
  28. "FUNCTION: s:echo(msg){{{1
  29. function! s:echo(msg)
  30. redraw
  31. echomsg "NERDTree: " . a:msg
  32. endfunction
  33. "FUNCTION: s:echoWarning(msg){{{1
  34. function! s:echoWarning(msg)
  35. echohl warningmsg
  36. call s:echo(a:msg)
  37. echohl normal
  38. endfunction
  39. "FUNCTION: s:promptToDelBuffer(bufnum, msg){{{1
  40. "prints out the given msg and, if the user responds by pushing 'y' then the
  41. "buffer with the given bufnum is deleted
  42. "
  43. "Args:
  44. "bufnum: the buffer that may be deleted
  45. "msg: a message that will be echoed to the user asking them if they wish to
  46. " del the buffer
  47. function! s:promptToDelBuffer(bufnum, msg)
  48. echo a:msg
  49. if nr2char(getchar()) ==# 'y'
  50. exec "silent bdelete! " . a:bufnum
  51. endif
  52. endfunction
  53. "FUNCTION: NERDTreeAddNode(){{{1
  54. function! NERDTreeAddNode()
  55. let curDirNode = g:NERDTreeDirNode.GetSelected()
  56. let newNodeName = input("Add a childnode\n".
  57. \ "==========================================================\n".
  58. \ "Enter the dir/file name to be created. Dirs end with a '/'\n" .
  59. \ "", curDirNode.path.str() . g:NERDTreePath.Slash(), "file")
  60. if newNodeName ==# ''
  61. call s:echo("Node Creation Aborted.")
  62. return
  63. endif
  64. try
  65. let newPath = g:NERDTreePath.Create(newNodeName)
  66. let parentNode = b:NERDTreeRoot.findNode(newPath.getParent())
  67. let newTreeNode = g:NERDTreeFileNode.New(newPath)
  68. if parentNode.isOpen || !empty(parentNode.children)
  69. call parentNode.addChild(newTreeNode, 1)
  70. call NERDTreeRender()
  71. call newTreeNode.putCursorHere(1, 0)
  72. endif
  73. catch /^NERDTree/
  74. call s:echoWarning("Node Not Created.")
  75. endtry
  76. endfunction
  77. "FUNCTION: NERDTreeMoveNode(){{{1
  78. function! NERDTreeMoveNode()
  79. let curNode = g:NERDTreeFileNode.GetSelected()
  80. let newNodePath = input("Rename the current node\n" .
  81. \ "==========================================================\n" .
  82. \ "Enter the new path for the node: \n" .
  83. \ "", curNode.path.str(), "file")
  84. if newNodePath ==# ''
  85. call s:echo("Node Renaming Aborted.")
  86. return
  87. endif
  88. try
  89. let bufnum = bufnr(curNode.path.str())
  90. call curNode.rename(newNodePath)
  91. call NERDTreeRender()
  92. "if the node is open in a buffer, ask the user if they want to
  93. "close that buffer
  94. if bufnum != -1
  95. let prompt = "\nNode renamed.\n\nThe old file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)"
  96. call s:promptToDelBuffer(bufnum, prompt)
  97. endif
  98. call curNode.putCursorHere(1, 0)
  99. redraw
  100. catch /^NERDTree/
  101. call s:echoWarning("Node Not Renamed.")
  102. endtry
  103. endfunction
  104. " FUNCTION: NERDTreeDeleteNode() {{{1
  105. function! NERDTreeDeleteNode()
  106. let currentNode = g:NERDTreeFileNode.GetSelected()
  107. let confirmed = 0
  108. if currentNode.path.isDirectory
  109. let choice =input("Delete the current node\n" .
  110. \ "==========================================================\n" .
  111. \ "STOP! To delete this entire directory, type 'yes'\n" .
  112. \ "" . currentNode.path.str() . ": ")
  113. let confirmed = choice ==# 'yes'
  114. else
  115. echo "Delete the current node\n" .
  116. \ "==========================================================\n".
  117. \ "Are you sure you wish to delete the node:\n" .
  118. \ "" . currentNode.path.str() . " (yN):"
  119. let choice = nr2char(getchar())
  120. let confirmed = choice ==# 'y'
  121. endif
  122. if confirmed
  123. try
  124. call currentNode.delete()
  125. call NERDTreeRender()
  126. "if the node is open in a buffer, ask the user if they want to
  127. "close that buffer
  128. let bufnum = bufnr(currentNode.path.str())
  129. if buflisted(bufnum)
  130. let prompt = "\nNode deleted.\n\nThe file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)"
  131. call s:promptToDelBuffer(bufnum, prompt)
  132. endif
  133. redraw
  134. catch /^NERDTree/
  135. call s:echoWarning("Could not remove node")
  136. endtry
  137. else
  138. call s:echo("delete aborted")
  139. endif
  140. endfunction
  141. " FUNCTION: NERDTreeCopyNode() {{{1
  142. function! NERDTreeCopyNode()
  143. let currentNode = g:NERDTreeFileNode.GetSelected()
  144. let newNodePath = input("Copy the current node\n" .
  145. \ "==========================================================\n" .
  146. \ "Enter the new path to copy the node to: \n" .
  147. \ "", currentNode.path.str(), "file")
  148. if newNodePath != ""
  149. "strip trailing slash
  150. let newNodePath = substitute(newNodePath, '\/$', '', '')
  151. let confirmed = 1
  152. if currentNode.path.copyingWillOverwrite(newNodePath)
  153. call s:echo("Warning: copying may overwrite files! Continue? (yN)")
  154. let choice = nr2char(getchar())
  155. let confirmed = choice ==# 'y'
  156. endif
  157. if confirmed
  158. try
  159. let newNode = currentNode.copy(newNodePath)
  160. if !empty(newNode)
  161. call NERDTreeRender()
  162. call newNode.putCursorHere(0, 0)
  163. endif
  164. catch /^NERDTree/
  165. call s:echoWarning("Could not copy node")
  166. endtry
  167. endif
  168. else
  169. call s:echo("Copy aborted.")
  170. endif
  171. redraw
  172. endfunction
  173. function! NERDTreeQuickLook()
  174. let treenode = g:NERDTreeFileNode.GetSelected()
  175. if treenode != {}
  176. call system("qlmanage -p 2>/dev/null '" . treenode.path.str() . "'")
  177. endif
  178. endfunction
  179. function! NERDTreeRevealInFinder()
  180. let treenode = g:NERDTreeFileNode.GetSelected()
  181. if treenode != {}
  182. let x = system("open -R '" . treenode.path.str() . "'")
  183. endif
  184. endfunction
  185. function! NERDTreeExecuteFile()
  186. let treenode = g:NERDTreeFileNode.GetSelected()
  187. if treenode != {}
  188. let x = system("open '" . treenode.path.str() . "'")
  189. endif
  190. endfunction
  191. " vim: set sw=4 sts=4 et fdm=marker: