|
| 1 | +" ============================================================================ |
| 2 | +" File: nerdtree_fs_menu.vim |
| 3 | +" Description: plugin for the NERD Tree that provides a file system menu |
| 4 | +" Maintainer: Martin Grenfell <martin_grenfell at msn 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 | + |
| 18 | +call NERDTreeAddMenuItem({'text': '(a)dd a childnode', 'shortcut': 'a', 'callback': 'NERDTreeAddNode'}) |
| 19 | +call NERDTreeAddMenuItem({'text': '(m)ove the curent node', 'shortcut': 'm', 'callback': 'NERDTreeMoveNode'}) |
| 20 | +call NERDTreeAddMenuItem({'text': '(d)elete the curent node', 'shortcut': 'd', 'callback': 'NERDTreeDeleteNode'}) |
| 21 | +if g:NERDTreePath.CopyingSupported() |
| 22 | + call NERDTreeAddMenuItem({'text': '(c)copy the current node', 'shortcut': 'c', 'callback': 'NERDTreeCopyNode'}) |
| 23 | +endif |
| 24 | + |
| 25 | +"FUNCTION: s:echo(msg){{{1 |
| 26 | +function! s:echo(msg) |
| 27 | + redraw |
| 28 | + echomsg "NERDTree: " . a:msg |
| 29 | +endfunction |
| 30 | + |
| 31 | +"FUNCTION: s:echoWarning(msg){{{1 |
| 32 | +function! s:echoWarning(msg) |
| 33 | + echohl warningmsg |
| 34 | + call s:echo(a:msg) |
| 35 | + echohl normal |
| 36 | +endfunction |
| 37 | + |
| 38 | +"FUNCTION: s:promptToDelBuffer(bufnum, msg){{{1 |
| 39 | +"prints out the given msg and, if the user responds by pushing 'y' then the |
| 40 | +"buffer with the given bufnum is deleted |
| 41 | +" |
| 42 | +"Args: |
| 43 | +"bufnum: the buffer that may be deleted |
| 44 | +"msg: a message that will be echoed to the user asking them if they wish to |
| 45 | +" del the buffer |
| 46 | +function! s:promptToDelBuffer(bufnum, msg) |
| 47 | + echo a:msg |
| 48 | + if nr2char(getchar()) ==# 'y' |
| 49 | + exec "silent bdelete! " . a:bufnum |
| 50 | + endif |
| 51 | +endfunction |
| 52 | + |
| 53 | +"FUNCTION: NERDTreeAddNode(){{{1 |
| 54 | +function! NERDTreeAddNode() |
| 55 | + let curDirNode = g:NERDTreeDirNode.GetSelected() |
| 56 | + |
| 57 | + let newNodeName = input("Add a childnode\n". |
| 58 | + \ "==========================================================\n". |
| 59 | + \ "Enter the dir/file name to be created. Dirs end with a '/'\n" . |
| 60 | + \ "", curDirNode.path.strForGlob() . g:NERDTreePath.Slash()) |
| 61 | + |
| 62 | + if newNodeName ==# '' |
| 63 | + call s:echo("Node Creation Aborted.") |
| 64 | + return |
| 65 | + endif |
| 66 | + |
| 67 | + try |
| 68 | + let newPath = g:NERDTreePath.Create(newNodeName) |
| 69 | + let parentNode = b:NERDTreeRoot.findNode(newPath.getParent()) |
| 70 | + |
| 71 | + let newTreeNode = g:NERDTreeFileNode.New(newPath) |
| 72 | + if parentNode.isOpen || !empty(parentNode.children) |
| 73 | + call parentNode.addChild(newTreeNode, 1) |
| 74 | + call NERDTreeRender() |
| 75 | + call newTreeNode.putCursorHere(1, 0) |
| 76 | + endif |
| 77 | + catch /^NERDTree/ |
| 78 | + call s:echoWarning("Node Not Created.") |
| 79 | + endtry |
| 80 | +endfunction |
| 81 | + |
| 82 | +"FUNCTION: NERDTreeMoveNode(){{{1 |
| 83 | +function! NERDTreeMoveNode() |
| 84 | + let curNode = g:NERDTreeFileNode.GetSelected() |
| 85 | + if curNode ==# {} |
| 86 | + call s:echo("Put the cursor on a node first" ) |
| 87 | + return |
| 88 | + endif |
| 89 | + |
| 90 | + let newNodePath = input("Rename the current node\n" . |
| 91 | + \ "==========================================================\n" . |
| 92 | + \ "Enter the new path for the node: \n" . |
| 93 | + \ "", curNode.path.strForOS(0)) |
| 94 | + |
| 95 | + if newNodePath ==# '' |
| 96 | + call s:echo("Node Renaming Aborted.") |
| 97 | + return |
| 98 | + endif |
| 99 | + |
| 100 | + try |
| 101 | + let bufnum = bufnr(curNode.path.str(0)) |
| 102 | + |
| 103 | + call curNode.rename(newNodePath) |
| 104 | + call NERDTreeRender() |
| 105 | + |
| 106 | + "if the node is open in a buffer, ask the user if they want to |
| 107 | + "close that buffer |
| 108 | + if bufnum != -1 |
| 109 | + let prompt = "\nNode renamed.\n\nThe old file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)" |
| 110 | + call s:promptToDelBuffer(bufnum, prompt) |
| 111 | + endif |
| 112 | + |
| 113 | + call curNode.putCursorHere(1, 0) |
| 114 | + |
| 115 | + redraw |
| 116 | + catch /^NERDTree/ |
| 117 | + call s:echoWarning("Node Not Renamed.") |
| 118 | + endtry |
| 119 | +endfunction |
| 120 | + |
| 121 | +" FUNCTION: NERDTreeDeleteNode() {{{1 |
| 122 | +function! NERDTreeDeleteNode() |
| 123 | + let currentNode = g:NERDTreeFileNode.GetSelected() |
| 124 | + if currentNode ==# {} |
| 125 | + call s:echo("Put the cursor on a node first") |
| 126 | + return |
| 127 | + endif |
| 128 | + |
| 129 | + let confirmed = 0 |
| 130 | + |
| 131 | + if currentNode.path.isDirectory |
| 132 | + let choice =input("Delete the current node\n" . |
| 133 | + \ "==========================================================\n" . |
| 134 | + \ "STOP! To delete this entire directory, type 'yes'\n" . |
| 135 | + \ "" . currentNode.path.strForOS(0) . ": ") |
| 136 | + let confirmed = choice ==# 'yes' |
| 137 | + else |
| 138 | + echo "Delete the current node\n" . |
| 139 | + \ "==========================================================\n". |
| 140 | + \ "Are you sure you wish to delete the node:\n" . |
| 141 | + \ "" . currentNode.path.strForOS(0) . " (yN):" |
| 142 | + let choice = nr2char(getchar()) |
| 143 | + let confirmed = choice ==# 'y' |
| 144 | + endif |
| 145 | + |
| 146 | + |
| 147 | + if confirmed |
| 148 | + try |
| 149 | + call currentNode.delete() |
| 150 | + call NERDTreeRender() |
| 151 | + |
| 152 | + "if the node is open in a buffer, ask the user if they want to |
| 153 | + "close that buffer |
| 154 | + let bufnum = bufnr(currentNode.path.str(0)) |
| 155 | + if buflisted(bufnum) |
| 156 | + let prompt = "\nNode deleted.\n\nThe file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)" |
| 157 | + call s:promptToDelBuffer(bufnum, prompt) |
| 158 | + endif |
| 159 | + |
| 160 | + redraw |
| 161 | + catch /^NERDTree/ |
| 162 | + call s:echoWarning("Could not remove node") |
| 163 | + endtry |
| 164 | + else |
| 165 | + call s:echo("delete aborted") |
| 166 | + endif |
| 167 | + |
| 168 | +endfunction |
| 169 | + |
| 170 | +" FUNCTION: NERDTreeCopyNode() {{{1 |
| 171 | +function! NERDTreeCopyNode() |
| 172 | + let currentNode = g:NERDTreeFileNode.GetSelected() |
| 173 | + if currentNode ==# {} |
| 174 | + call s:echo("Put the cursor on a file node first") |
| 175 | + return |
| 176 | + endif |
| 177 | + |
| 178 | + let newNodePath = input("Copy the current node\n" . |
| 179 | + \ "==========================================================\n" . |
| 180 | + \ "Enter the new path to copy the node to: \n" . |
| 181 | + \ "", currentNode.path.str(0)) |
| 182 | + |
| 183 | + if newNodePath != "" |
| 184 | + "strip trailing slash |
| 185 | + let newNodePath = substitute(newNodePath, '\/$', '', '') |
| 186 | + |
| 187 | + let confirmed = 1 |
| 188 | + if currentNode.path.copyingWillOverwrite(newNodePath) |
| 189 | + call s:echo("Warning: copying may overwrite files! Continue? (yN)") |
| 190 | + let choice = nr2char(getchar()) |
| 191 | + let confirmed = choice ==# 'y' |
| 192 | + endif |
| 193 | + |
| 194 | + if confirmed |
| 195 | + try |
| 196 | + let newNode = currentNode.copy(newNodePath) |
| 197 | + call NERDTreeRender() |
| 198 | + call newNode.putCursorHere(0, 0) |
| 199 | + catch /^NERDTree/ |
| 200 | + call s:echoWarning("Could not copy node") |
| 201 | + endtry |
| 202 | + endif |
| 203 | + else |
| 204 | + call s:echo("Copy aborted.") |
| 205 | + endif |
| 206 | + redraw |
| 207 | +endfunction |
| 208 | + |
| 209 | +" vim: set sw=4 sts=4 et fdm=marker: |
0 commit comments