]> git.r.bdr.sh - rbdr/dotfiles/blob - vim/nerdtree_plugin/fs_menu.vim
Update the stuff
[rbdr/dotfiles] / vim / nerdtree_plugin / fs_menu.vim
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
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.str({'format': 'Glob'}) . 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 let newNodePath = input("Rename the current node\n" .
86 \ "==========================================================\n" .
87 \ "Enter the new path for the node: \n" .
88 \ "", curNode.path.str())
89
90 if newNodePath ==# ''
91 call s:echo("Node Renaming Aborted.")
92 return
93 endif
94
95 try
96 let bufnum = bufnr(curNode.path.str())
97
98 call curNode.rename(newNodePath)
99 call NERDTreeRender()
100
101 "if the node is open in a buffer, ask the user if they want to
102 "close that buffer
103 if bufnum != -1
104 let prompt = "\nNode renamed.\n\nThe old file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)"
105 call s:promptToDelBuffer(bufnum, prompt)
106 endif
107
108 call curNode.putCursorHere(1, 0)
109
110 redraw
111 catch /^NERDTree/
112 call s:echoWarning("Node Not Renamed.")
113 endtry
114 endfunction
115
116 " FUNCTION: NERDTreeDeleteNode() {{{1
117 function! NERDTreeDeleteNode()
118 let currentNode = g:NERDTreeFileNode.GetSelected()
119 let confirmed = 0
120
121 if currentNode.path.isDirectory
122 let choice =input("Delete the current node\n" .
123 \ "==========================================================\n" .
124 \ "STOP! To delete this entire directory, type 'yes'\n" .
125 \ "" . currentNode.path.str() . ": ")
126 let confirmed = choice ==# 'yes'
127 else
128 echo "Delete the current node\n" .
129 \ "==========================================================\n".
130 \ "Are you sure you wish to delete the node:\n" .
131 \ "" . currentNode.path.str() . " (yN):"
132 let choice = nr2char(getchar())
133 let confirmed = choice ==# 'y'
134 endif
135
136
137 if confirmed
138 try
139 call currentNode.delete()
140 call NERDTreeRender()
141
142 "if the node is open in a buffer, ask the user if they want to
143 "close that buffer
144 let bufnum = bufnr(currentNode.path.str())
145 if buflisted(bufnum)
146 let prompt = "\nNode deleted.\n\nThe file is open in buffer ". bufnum . (bufwinnr(bufnum) ==# -1 ? " (hidden)" : "") .". Delete this buffer? (yN)"
147 call s:promptToDelBuffer(bufnum, prompt)
148 endif
149
150 redraw
151 catch /^NERDTree/
152 call s:echoWarning("Could not remove node")
153 endtry
154 else
155 call s:echo("delete aborted")
156 endif
157
158 endfunction
159
160 " FUNCTION: NERDTreeCopyNode() {{{1
161 function! NERDTreeCopyNode()
162 let currentNode = g:NERDTreeFileNode.GetSelected()
163 let newNodePath = input("Copy the current node\n" .
164 \ "==========================================================\n" .
165 \ "Enter the new path to copy the node to: \n" .
166 \ "", currentNode.path.str())
167
168 if newNodePath != ""
169 "strip trailing slash
170 let newNodePath = substitute(newNodePath, '\/$', '', '')
171
172 let confirmed = 1
173 if currentNode.path.copyingWillOverwrite(newNodePath)
174 call s:echo("Warning: copying may overwrite files! Continue? (yN)")
175 let choice = nr2char(getchar())
176 let confirmed = choice ==# 'y'
177 endif
178
179 if confirmed
180 try
181 let newNode = currentNode.copy(newNodePath)
182 call NERDTreeRender()
183 call newNode.putCursorHere(0, 0)
184 catch /^NERDTree/
185 call s:echoWarning("Could not copy node")
186 endtry
187 endif
188 else
189 call s:echo("Copy aborted.")
190 endif
191 redraw
192 endfunction
193
194 " vim: set sw=4 sts=4 et fdm=marker: