2009-07-19 01:21:57 -04:00
" ============================================================================
2009-08-21 09:54:28 -04:00
" File: fs_menu.vim
2009-07-19 01:21:57 -04:00
" Description: plugin for the NERD Tree that provides a file system menu
2009-10-08 21:46:40 -04:00
" Maintainer: Martin Grenfell <martin.grenfell at gmail dot com>
2009-07-19 01:21:57 -04:00
" License: This program is free software. It comes without any warranty,
" to the extent permitted by applicable law. You can redistribute
" it and/or modify it under the terms of the Do What The Fuck You
" Want To Public License, Version 2, as published by Sam Hocevar.
" See http://sam.zoy.org/wtfpl/COPYING for more details.
"
" ============================================================================
if exists ( "g:loaded_nerdtree_fs_menu" )
finish
endif
let g :loaded_nerdtree_fs_menu = 1
2012-08-26 04:51:26 -04:00
"Automatically delete the buffer after deleting or renaming a file
if ! exists ( "g:NERDTreeAutoDeleteBuffer" )
let g :NERDTreeAutoDeleteBuffer = 0
endif
2009-07-21 03:26:48 -04:00
call NERDTreeAddMenuItem ( {'text' : '(a)dd a childnode' , 'shortcut' : 'a' , 'callback' : 'NERDTreeAddNode' })
2011-02-07 23:16:30 -05:00
call NERDTreeAddMenuItem ( {'text' : '(m)ove the current node' , 'shortcut' : 'm' , 'callback' : 'NERDTreeMoveNode' })
call NERDTreeAddMenuItem ( {'text' : '(d)elete the current node' , 'shortcut' : 'd' , 'callback' : 'NERDTreeDeleteNode' })
2011-09-06 20:01:57 -04:00
2015-10-03 20:54:15 -04:00
if has ( "gui_mac" ) | | has ( "gui_macvim" ) | | has ( "mac" )
2011-09-06 20:01:57 -04:00
call NERDTreeAddMenuItem ( {'text' : '(r)eveal in Finder the current node' , 'shortcut' : 'r' , 'callback' : 'NERDTreeRevealInFinder' })
call NERDTreeAddMenuItem ( {'text' : '(o)pen the current node with system editor' , 'shortcut' : 'o' , 'callback' : 'NERDTreeExecuteFile' })
call NERDTreeAddMenuItem ( {'text' : '(q)uicklook the current node' , 'shortcut' : 'q' , 'callback' : 'NERDTreeQuickLook' })
endif
2009-07-19 01:34:28 -04:00
if g :NERDTreePath .CopyingSupported ( )
2012-10-03 15:39:56 -04:00
call NERDTreeAddMenuItem ( {'text' : '(c)opy the current node' , 'shortcut' : 'c' , 'callback' : 'NERDTreeCopyNode' })
2009-07-19 01:34:28 -04:00
endif
2009-07-19 01:21:57 -04:00
2015-11-02 14:30:16 -05:00
if has ( "unix" ) | | has ( "osx" )
call NERDTreeAddMenuItem ( {'text' : '(l)ist the current node' , 'shortcut' : 'l' , 'callback' : 'NERDTreeListNode' })
2015-11-12 16:20:20 -05:00
else
call NERDTreeAddMenuItem ( {'text' : '(l)ist the current node' , 'shortcut' : 'l' , 'callback' : 'NERDTreeListNodeWin32' })
2015-11-02 14:30:16 -05:00
endif
2009-07-19 01:21:57 -04:00
"FUNCTION: s:promptToDelBuffer(bufnum, msg){{{1
"prints out the given msg and, if the user responds by pushing 'y' then the
"buffer with the given bufnum is deleted
"
"Args:
"bufnum: the buffer that may be deleted
"msg: a message that will be echoed to the user asking them if they wish to
" del the buffer
function ! s :promptToDelBuffer ( bufnum , msg )
echo a :msg
2012-08-26 04:51:26 -04:00
if g :NERDTreeAutoDeleteBuffer | | nr2char ( getchar ( ) ) = = # 'y'
2012-08-26 09:19:52 -04:00
" 1. ensure that all windows which display the just deleted filename
" now display an empty buffer (so a layout is preserved).
" Is not it better to close single tabs with this file only ?
let s :originalTabNumber = tabpagenr ( )
let s :originalWindowNumber = winnr ( )
2017-11-02 08:26:48 -04:00
" Go to the next buffer in buffer list if at least one extra buffer is listed
" Otherwise open a new empty buffer
if v :version > = 800
let l :listedBufferCount = len ( getbufinfo ( {'buflisted' :1 }) )
elseif v :version > = 702
let l :listedBufferCount = len ( filter ( range ( 1 , bufnr ( '$' ) ) , 'buflisted(v:val)' ) )
else
" Ignore buffer count in this case to make sure we keep the old
" behavior
let l :listedBufferCount = 0
endif
if l :listedBufferCount > 1
exec "tabdo windo if winbufnr(0) == " . a :bufnum . " | exec ':bnext! ' | endif"
else
exec "tabdo windo if winbufnr(0) == " . a :bufnum . " | exec ':enew! ' | endif"
endif
2012-08-26 09:19:52 -04:00
exec "tabnext " . s :originalTabNumber
exec s :originalWindowNumber . "wincmd w"
" 3. We don't need a previous buffer anymore
exec "bwipeout! " . a :bufnum
2009-07-19 01:21:57 -04:00
endif
endfunction
2012-08-26 09:19:52 -04:00
"FUNCTION: s:promptToRenameBuffer(bufnum, msg){{{1
"prints out the given msg and, if the user responds by pushing 'y' then the
"buffer with the given bufnum is replaced with a new one
"
"Args:
"bufnum: the buffer that may be deleted
"msg: a message that will be echoed to the user asking them if they wish to
" del the buffer
function ! s :promptToRenameBuffer ( bufnum , msg , newFileName )
echo a :msg
if g :NERDTreeAutoDeleteBuffer | | nr2char ( getchar ( ) ) = = # 'y'
2016-10-03 18:04:50 -04:00
let quotedFileName = fnameescape ( a :newFileName )
2012-08-26 09:19:52 -04:00
" 1. ensure that a new buffer is loaded
2014-05-30 07:05:37 -04:00
exec "badd " . quotedFileName
2012-08-26 09:19:52 -04:00
" 2. ensure that all windows which display the just deleted filename
2014-09-25 05:08:53 -04:00
" display a buffer for a new filename.
2012-08-26 09:19:52 -04:00
let s :originalTabNumber = tabpagenr ( )
let s :originalWindowNumber = winnr ( )
2014-10-29 14:35:53 -04:00
let editStr = g :NERDTreePath .New ( a :newFileName ) .str ( {'format' : 'Edit' })
exec "tabdo windo if winbufnr(0) == " . a :bufnum . " | exec ':e! " . editStr . "' | endif"
2012-08-26 09:19:52 -04:00
exec "tabnext " . s :originalTabNumber
exec s :originalWindowNumber . "wincmd w"
" 3. We don't need a previous buffer anymore
exec "bwipeout! " . a :bufnum
2009-07-19 01:21:57 -04:00
endif
endfunction
2009-07-19 01:34:28 -04:00
"FUNCTION: NERDTreeAddNode(){{{1
function ! NERDTreeAddNode ( )
2009-07-19 01:21:57 -04:00
let curDirNode = g :NERDTreeDirNode .GetSelected ( )
let newNodeName = input ( "Add a childnode\n" .
\ "==========================================================\n" .
\ "Enter the dir/file name to be created. Dirs end with a '/'\n" .
2011-07-04 08:56:39 -04:00
\ "" , curDirNode .path .str ( ) . g :NERDTreePath .Slash ( ) , "file" )
2009-07-19 01:21:57 -04:00
if newNodeName = = # ''
2015-11-13 05:53:42 -05:00
call nerdtree #echo ( "Node Creation Aborted." )
2009-07-19 01:21:57 -04:00
return
endif
try
let newPath = g :NERDTreePath .Create ( newNodeName )
2015-11-16 06:28:24 -05:00
let parentNode = b :NERDTree .root .findNode ( newPath .getParent ( ) )
2009-07-19 01:21:57 -04:00
2015-11-19 20:44:12 -05:00
let newTreeNode = g :NERDTreeFileNode .New ( newPath , b :NERDTree )
2013-10-08 00:25:27 -04:00
if empty ( parentNode )
2015-11-16 06:28:24 -05:00
call b :NERDTree .root .refresh ( )
2014-07-07 17:59:28 -04:00
call b :NERDTree .render ( )
2013-10-08 00:25:27 -04:00
elseif parentNode .isOpen | | ! empty ( parentNode .children )
2009-07-19 01:21:57 -04:00
call parentNode .addChild ( newTreeNode , 1 )
call NERDTreeRender ( )
call newTreeNode .putCursorHere ( 1 , 0 )
endif
catch /^NERDTree/
2015-11-13 05:53:42 -05:00
call nerdtree #echoWarning ( "Node Not Created." )
2009-07-19 01:21:57 -04:00
endtry
endfunction
2009-07-19 01:34:28 -04:00
"FUNCTION: NERDTreeMoveNode(){{{1
function ! NERDTreeMoveNode ( )
2009-07-19 01:21:57 -04:00
let curNode = g :NERDTreeFileNode .GetSelected ( )
let newNodePath = input ( "Rename the current node\n" .
\ "==========================================================\n" .
\ "Enter the new path for the node: \n" .
2011-07-04 08:56:39 -04:00
\ "" , curNode .path .str ( ) , "file" )
2009-07-19 01:21:57 -04:00
if newNodePath = = # ''
2015-11-13 05:53:42 -05:00
call nerdtree #echo ( "Node Renaming Aborted." )
2009-07-19 01:21:57 -04:00
return
endif
try
2014-02-20 19:46:15 -05:00
let bufnum = bufnr ( "^" .curNode .path .str ( ) ."$" )
2009-07-19 01:21:57 -04:00
call curNode .rename ( newNodePath )
call NERDTreeRender ( )
"if the node is open in a buffer, ask the user if they want to
"close that buffer
if bufnum ! = -1
2012-08-26 09:19:52 -04:00
let prompt = "\nNode renamed.\n\nThe old file is open in buffer " . bufnum . ( bufwinnr ( bufnum ) = = # -1 ? " (hidden)" : "" ) .". Replace this buffer with a new file? (yN)"
call s :promptToRenameBuffer ( bufnum , prompt , newNodePath )
2009-07-19 01:21:57 -04:00
endif
call curNode .putCursorHere ( 1 , 0 )
redraw
catch /^NERDTree/
2015-11-13 05:53:42 -05:00
call nerdtree #echoWarning ( "Node Not Renamed." )
2009-07-19 01:21:57 -04:00
endtry
endfunction
2009-07-19 01:34:28 -04:00
" FUNCTION: NERDTreeDeleteNode() {{{1
function ! NERDTreeDeleteNode ( )
2009-07-19 01:21:57 -04:00
let currentNode = g :NERDTreeFileNode .GetSelected ( )
let confirmed = 0
2016-01-07 10:53:28 -05:00
if currentNode .path .isDirectory && currentNode .getChildCount ( ) > 0
2009-07-19 01:21:57 -04:00
let choice = input ( "Delete the current node\n" .
\ "==========================================================\n" .
2016-09-22 16:42:17 -04:00
\ "STOP! Directory is not empty! To delete, type 'yes'\n" .
2009-09-05 06:29:11 -04:00
\ "" . currentNode .path .str ( ) . ": " )
2009-07-19 01:21:57 -04:00
let confirmed = choice = = # 'yes'
else
echo "Delete the current node\n" .
\ "==========================================================\n" .
\ "Are you sure you wish to delete the node:\n" .
2009-09-05 06:29:11 -04:00
\ "" . currentNode .path .str ( ) . " (yN):"
2009-07-19 01:21:57 -04:00
let choice = nr2char ( getchar ( ) )
let confirmed = choice = = # 'y'
endif
if confirmed
try
call currentNode .delete ( )
call NERDTreeRender ( )
"if the node is open in a buffer, ask the user if they want to
"close that buffer
2014-02-20 19:46:15 -05:00
let bufnum = bufnr ( "^" .currentNode .path .str ( ) ."$" )
2009-07-19 01:21:57 -04:00
if buflisted ( bufnum )
let prompt = "\nNode deleted.\n\nThe file is open in buffer " . bufnum . ( bufwinnr ( bufnum ) = = # -1 ? " (hidden)" : "" ) .". Delete this buffer? (yN)"
call s :promptToDelBuffer ( bufnum , prompt )
endif
redraw
catch /^NERDTree/
2015-11-13 05:53:42 -05:00
call nerdtree #echoWarning ( "Could not remove node" )
2009-07-19 01:21:57 -04:00
endtry
else
2015-11-13 05:53:42 -05:00
call nerdtree #echo ( "delete aborted" )
2009-07-19 01:21:57 -04:00
endif
endfunction
2015-11-13 05:44:27 -05:00
" FUNCTION: NERDTreeListNode() {{{1
function ! NERDTreeListNode ( )
let treenode = g :NERDTreeFileNode .GetSelected ( )
2017-11-02 08:36:07 -04:00
if ! empty ( treenode )
if has ( "osx" )
let stat_cmd = 'stat -f "%z" '
else
let stat_cmd = 'stat -c "%s" '
endif
let cmd = 'size=$(' . stat_cmd . shellescape ( treenode .path .str ( ) ) . ') && ' .
\ 'size_with_commas=$(echo $size | sed -e :a -e "s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta") && ' .
\ 'ls -ld ' . shellescape ( treenode .path .str ( ) ) . ' | sed -e "s/ $size / $size_with_commas /"'
let metadata = split ( system ( cmd ) , '\n' )
2015-11-13 05:53:42 -05:00
call nerdtree #echo ( metadata [0 ])
2015-11-13 05:44:27 -05:00
else
2017-11-02 08:36:07 -04:00
call nerdtree #echo ( "No information available" )
2015-11-13 05:44:27 -05:00
endif
endfunction
2015-11-12 16:20:20 -05:00
" FUNCTION: NERDTreeListNodeWin32() {{{1
2015-11-12 21:29:34 -05:00
function ! NERDTreeListNodeWin32 ( )
2017-08-19 08:58:57 -04:00
let l :node = g :NERDTreeFileNode .GetSelected ( )
if ! empty ( l :node )
let l :save_shell = &shell
set shell &
if exists ( '+shellslash' )
let l :save_shellslash = &shellslash
set noshellslash
endif
let l :command = 'DIR /Q '
\ . shellescape ( l :node .path .str ( ) )
\ . ' | FINDSTR "^[012][0-9]/[0-3][0-9]/[12][0-9][0-9][0-9]"'
2017-08-19 09:29:46 -04:00
let l :metadata = split ( system ( l :command ) , "\n" )
2017-08-19 08:58:57 -04:00
if v :shell_error = = 0
call nerdtree #echo ( l :metadata [0 ])
else
call nerdtree #echoError ( 'shell command failed' )
endif
let &shell = l :save_shell
2017-08-19 09:29:46 -04:00
if exists ( 'l:save_shellslash' )
2017-08-19 08:58:57 -04:00
let &shellslash = l :save_shellslash
endif
return
2015-11-12 21:29:34 -05:00
endif
2017-08-19 08:58:57 -04:00
call nerdtree #echo ( 'node not recognized' )
2014-09-15 11:14:44 -04:00
endfunction
2009-07-19 01:34:28 -04:00
" FUNCTION: NERDTreeCopyNode() {{{1
function ! NERDTreeCopyNode ( )
2009-07-19 01:21:57 -04:00
let currentNode = g :NERDTreeFileNode .GetSelected ( )
let newNodePath = input ( "Copy the current node\n" .
\ "==========================================================\n" .
\ "Enter the new path to copy the node to: \n" .
2011-07-04 08:56:39 -04:00
\ "" , currentNode .path .str ( ) , "file" )
2009-07-19 01:21:57 -04:00
if newNodePath ! = ""
"strip trailing slash
let newNodePath = substitute ( newNodePath , '\/$' , '' , '' )
let confirmed = 1
if currentNode .path .copyingWillOverwrite ( newNodePath )
2015-11-13 05:53:42 -05:00
call nerdtree #echo ( "Warning: copying may overwrite files! Continue? (yN)" )
2009-07-19 01:21:57 -04:00
let choice = nr2char ( getchar ( ) )
let confirmed = choice = = # 'y'
endif
if confirmed
try
let newNode = currentNode .copy ( newNodePath )
2013-10-08 00:25:27 -04:00
if empty ( newNode )
2015-11-16 06:28:24 -05:00
call b :NERDTree .root .refresh ( )
2014-07-07 17:59:28 -04:00
call b :NERDTree .render ( )
2013-10-08 00:25:27 -04:00
else
2011-07-09 02:44:03 -04:00
call NERDTreeRender ( )
call newNode .putCursorHere ( 0 , 0 )
endif
2009-07-19 01:21:57 -04:00
catch /^NERDTree/
2015-11-13 05:53:42 -05:00
call nerdtree #echoWarning ( "Could not copy node" )
2009-07-19 01:21:57 -04:00
endtry
endif
else
2015-11-13 05:53:42 -05:00
call nerdtree #echo ( "Copy aborted." )
2009-07-19 01:21:57 -04:00
endif
redraw
endfunction
2015-11-12 21:29:34 -05:00
" FUNCTION: NERDTreeQuickLook() {{{1
2011-09-06 20:01:57 -04:00
function ! NERDTreeQuickLook ( )
let treenode = g :NERDTreeFileNode .GetSelected ( )
if treenode ! = {}
call system ( "qlmanage -p 2>/dev/null '" . treenode .path .str ( ) . "'" )
endif
endfunction
2015-11-12 21:29:34 -05:00
" FUNCTION: NERDTreeRevealInFinder() {{{1
2011-09-06 20:01:57 -04:00
function ! NERDTreeRevealInFinder ( )
let treenode = g :NERDTreeFileNode .GetSelected ( )
if treenode ! = {}
2015-11-13 05:46:12 -05:00
call system ( "open -R '" . treenode .path .str ( ) . "'" )
2011-09-06 20:01:57 -04:00
endif
endfunction
2015-11-12 21:29:34 -05:00
" FUNCTION: NERDTreeExecuteFile() {{{1
2011-09-06 20:01:57 -04:00
function ! NERDTreeExecuteFile ( )
let treenode = g :NERDTreeFileNode .GetSelected ( )
if treenode ! = {}
2015-11-13 05:46:12 -05:00
call system ( "open '" . treenode .path .str ( ) . "'" )
2011-09-06 20:01:57 -04:00
endif
endfunction
2009-07-19 01:21:57 -04:00
" vim: set sw=4 sts=4 et fdm=marker: