1
0
Fork 0
mirror of synced 2024-11-17 14:35:34 -05:00
ultimate-vim/sources_non_forked/nerdtree/nerdtree_plugin/fs_menu.vim

426 lines
16 KiB
VimL
Raw Normal View History

2012-08-16 23:41:25 -04:00
" ============================================================================
" File: fs_menu.vim
" Description: plugin for the NERD Tree that provides a file system menu
" Maintainer: Martin Grenfell <martin.grenfell at gmail dot com>
" 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
"Automatically delete the buffer after deleting or renaming a file
if !exists("g:NERDTreeAutoDeleteBuffer")
let g:NERDTreeAutoDeleteBuffer = 0
endif
2012-08-16 23:41:25 -04:00
call NERDTreeAddMenuItem({'text': '(a)dd a childnode', 'shortcut': 'a', 'callback': 'NERDTreeAddNode'})
call NERDTreeAddMenuItem({'text': '(m)ove the current node', 'shortcut': 'm', 'callback': 'NERDTreeMoveNode'})
call NERDTreeAddMenuItem({'text': '(d)elete the current node', 'shortcut': 'd', 'callback': 'NERDTreeDeleteNode'})
2015-12-08 08:20:04 -05:00
if has("gui_mac") || has("gui_macvim") || has("mac")
2012-08-16 23:41:25 -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
2018-06-14 06:31:12 -04:00
if executable("xdg-open")
call NERDTreeAddMenuItem({'text': '(r)eveal the current node in file manager', 'shortcut': 'r', 'callback': 'NERDTreeRevealFileLinux'})
call NERDTreeAddMenuItem({'text': '(o)pen the current node with system editor', 'shortcut': 'o', 'callback': 'NERDTreeExecuteFileLinux'})
endif
2012-08-16 23:41:25 -04:00
if g:NERDTreePath.CopyingSupported()
call NERDTreeAddMenuItem({'text': '(c)opy the current node', 'shortcut': 'c', 'callback': 'NERDTreeCopyNode'})
2012-08-16 23:41:25 -04:00
endif
2019-08-22 11:36:17 -04:00
call NERDTreeAddMenuItem({'text': (has("clipboard")?'copy (p)ath to clipboard':'print (p)ath to screen'), 'shortcut': 'p', 'callback': 'NERDTreeCopyPath'})
2012-08-16 23:41:25 -04:00
2015-12-08 08:20:04 -05:00
if has("unix") || has("osx")
call NERDTreeAddMenuItem({'text': '(l)ist the current node', 'shortcut': 'l', 'callback': 'NERDTreeListNode'})
else
call NERDTreeAddMenuItem({'text': '(l)ist the current node', 'shortcut': 'l', 'callback': 'NERDTreeListNodeWin32'})
endif
2012-08-16 23:41:25 -04:00
2019-03-08 06:04:56 -05:00
"FUNCTION: s:inputPrompt(action){{{1
"returns the string that should be prompted to the user for the given action
"
"Args:
"action: the action that is being performed, e.g. 'delete'
function! s:inputPrompt(action)
if a:action == "add"
let title = "Add a childnode"
let info = "Enter the dir/file name to be created. Dirs end with a '/'"
let minimal = "Add node:"
elseif a:action == "copy"
let title = "Copy the current node"
let info = "Enter the new path to copy the node to:"
let minimal = "Copy to:"
elseif a:action == "delete"
let title = "Delete the current node"
let info = "Are you sure you wish to delete the node:"
let minimal = "Delete?"
elseif a:action == "deleteNonEmpty"
let title = "Delete the current node"
let info = "STOP! Directory is not empty! To delete, type 'yes'"
let minimal = "Delete directory?"
elseif a:action == "move"
let title = "Rename the current node"
let info = "Enter the new path for the node:"
let minimal = "Move to:"
endif
if g:NERDTreeMenuController.isMinimal()
redraw! " Clear the menu
return minimal . " "
else
let divider = "=========================================================="
return title . "\n" . divider . "\n" . info . "\n"
end
endfunction
2012-08-16 23:41:25 -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
if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
" 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()
" 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
2019-08-22 11:36:17 -04:00
call nerdtree#exec("tabdo windo if winbufnr(0) == " . a:bufnum . " | exec ':bnext! ' | endif", 1)
else
2019-08-22 11:36:17 -04:00
call nerdtree#exec("tabdo windo if winbufnr(0) == " . a:bufnum . " | exec ':enew! ' | endif", 1)
endif
2019-08-22 11:36:17 -04:00
call nerdtree#exec("tabnext " . s:originalTabNumber, 1)
call nerdtree#exec(s:originalWindowNumber . "wincmd w", 1)
" 3. We don't need a previous buffer anymore
2019-08-22 11:36:17 -04:00
call nerdtree#exec("bwipeout! " . a:bufnum, 0)
2012-08-16 23:41:25 -04:00
endif
endfunction
2018-11-01 06:03:42 -04:00
"FUNCTION: s:renameBuffer(bufNum, newNodeName, isDirectory){{{1
"The buffer with the given bufNum is replaced with a new one
"
"Args:
2018-11-01 06:03:42 -04:00
"bufNum: the buffer that may be deleted
"newNodeName: the name given to the renamed node
"isDirectory: determines how to do the create the new filenames
function! s:renameBuffer(bufNum, newNodeName, isDirectory)
if a:isDirectory
let quotedFileName = fnameescape(a:newNodeName . '/' . fnamemodify(bufname(a:bufNum),':t'))
let editStr = g:NERDTreePath.New(a:newNodeName . '/' . fnamemodify(bufname(a:bufNum),':t')).str({'format': 'Edit'})
else
let quotedFileName = fnameescape(a:newNodeName)
let editStr = g:NERDTreePath.New(a:newNodeName).str({'format': 'Edit'})
endif
2018-11-01 06:03:42 -04:00
" 1. ensure that a new buffer is loaded
2019-08-22 11:36:17 -04:00
call nerdtree#exec("badd " . quotedFileName, 1)
2018-11-01 06:03:42 -04:00
" 2. ensure that all windows which display the just deleted filename
" display a buffer for a new filename.
let s:originalTabNumber = tabpagenr()
let s:originalWindowNumber = winnr()
2019-08-22 11:36:17 -04:00
call nerdtree#exec("tabdo windo if winbufnr(0) == " . a:bufNum . " | exec ':e! " . editStr . "' | endif", 1)
call nerdtree#exec("tabnext " . s:originalTabNumber, 1)
call nerdtree#exec(s:originalWindowNumber . "wincmd w", 1)
2018-11-01 06:03:42 -04:00
" 3. We don't need a previous buffer anymore
2019-03-08 06:04:56 -05:00
try
2019-08-22 11:36:17 -04:00
call nerdtree#exec("confirm bwipeout " . a:bufNum, 0)
2019-03-08 06:04:56 -05:00
catch
" This happens when answering Cancel if confirmation is needed. Do nothing.
endtry
endfunction
2019-03-08 06:04:56 -05:00
2012-08-16 23:41:25 -04:00
"FUNCTION: NERDTreeAddNode(){{{1
function! NERDTreeAddNode()
let curDirNode = g:NERDTreeDirNode.GetSelected()
2019-03-08 06:04:56 -05:00
let prompt = s:inputPrompt("add")
let newNodeName = input(prompt, curDirNode.path.str() . g:NERDTreePath.Slash(), "file")
2012-08-16 23:41:25 -04:00
if newNodeName ==# ''
2015-12-08 08:20:04 -05:00
call nerdtree#echo("Node Creation Aborted.")
2012-08-16 23:41:25 -04:00
return
endif
try
let newPath = g:NERDTreePath.Create(newNodeName)
2015-12-08 08:20:04 -05:00
let parentNode = b:NERDTree.root.findNode(newPath.getParent())
2012-08-16 23:41:25 -04:00
2015-12-08 08:20:04 -05:00
let newTreeNode = g:NERDTreeFileNode.New(newPath, b:NERDTree)
2018-11-01 06:03:42 -04:00
" Emptying g:NERDTreeOldSortOrder forces the sort to
" recalculate the cached sortKey so nodes sort correctly.
let g:NERDTreeOldSortOrder = []
2014-07-02 07:18:18 -04:00
if empty(parentNode)
2015-12-08 08:20:04 -05:00
call b:NERDTree.root.refresh()
2014-08-03 18:02:51 -04:00
call b:NERDTree.render()
2014-07-02 07:18:18 -04:00
elseif parentNode.isOpen || !empty(parentNode.children)
2012-08-16 23:41:25 -04:00
call parentNode.addChild(newTreeNode, 1)
call NERDTreeRender()
call newTreeNode.putCursorHere(1, 0)
endif
2019-03-08 06:04:56 -05:00
redraw!
2012-08-16 23:41:25 -04:00
catch /^NERDTree/
2015-12-08 08:20:04 -05:00
call nerdtree#echoWarning("Node Not Created.")
2012-08-16 23:41:25 -04:00
endtry
endfunction
"FUNCTION: NERDTreeMoveNode(){{{1
function! NERDTreeMoveNode()
let curNode = g:NERDTreeFileNode.GetSelected()
2019-03-08 06:04:56 -05:00
let prompt = s:inputPrompt("move")
let newNodePath = input(prompt, curNode.path.str(), "file")
2012-08-16 23:41:25 -04:00
if newNodePath ==# ''
2015-12-08 08:20:04 -05:00
call nerdtree#echo("Node Renaming Aborted.")
2012-08-16 23:41:25 -04:00
return
endif
try
2018-11-01 06:03:42 -04:00
if curNode.path.isDirectory
let l:openBuffers = filter(range(1,bufnr("$")),'bufexists(v:val) && fnamemodify(bufname(v:val),":p") =~# curNode.path.str() . "/.*"')
else
let l:openBuffers = filter(range(1,bufnr("$")),'bufexists(v:val) && fnamemodify(bufname(v:val),":p") ==# curNode.path.str()')
endif
2012-08-16 23:41:25 -04:00
call curNode.rename(newNodePath)
2018-11-01 06:03:42 -04:00
" Emptying g:NERDTreeOldSortOrder forces the sort to
" recalculate the cached sortKey so nodes sort correctly.
let g:NERDTreeOldSortOrder = []
2018-06-14 06:31:12 -04:00
call b:NERDTree.root.refresh()
2012-08-16 23:41:25 -04:00
call NERDTreeRender()
2018-11-01 06:03:42 -04:00
" If the file node is open, or files under the directory node are
" open, ask the user if they want to replace the file(s) with the
" renamed files.
if !empty(l:openBuffers)
if curNode.path.isDirectory
echo "\nDirectory renamed.\n\nFiles with the old directory name are open in buffers " . join(l:openBuffers, ', ') . ". Replace these buffers with the new files? (yN)"
else
echo "\nFile renamed.\n\nThe old file is open in buffer " . l:openBuffers[0] . ". Replace this buffer with the new file? (yN)"
endif
if g:NERDTreeAutoDeleteBuffer || nr2char(getchar()) ==# 'y'
for bufNum in l:openBuffers
call s:renameBuffer(bufNum, newNodePath, curNode.path.isDirectory)
endfor
endif
2012-08-16 23:41:25 -04:00
endif
call curNode.putCursorHere(1, 0)
2019-03-08 06:04:56 -05:00
redraw!
2012-08-16 23:41:25 -04:00
catch /^NERDTree/
2015-12-08 08:20:04 -05:00
call nerdtree#echoWarning("Node Not Renamed.")
2012-08-16 23:41:25 -04:00
endtry
endfunction
" FUNCTION: NERDTreeDeleteNode() {{{1
function! NERDTreeDeleteNode()
2019-03-08 06:04:56 -05:00
let l:shellslash = &shellslash
let &shellslash = 0
2012-08-16 23:41:25 -04:00
let currentNode = g:NERDTreeFileNode.GetSelected()
let confirmed = 0
2018-06-14 06:31:12 -04:00
if currentNode.path.isDirectory && ((currentNode.isOpen && currentNode.getChildCount() > 0) ||
\ (len(currentNode._glob('*', 1)) > 0))
2019-03-08 06:04:56 -05:00
let prompt = s:inputPrompt("deleteNonEmpty") . currentNode.path.str() . ": "
let choice = input(prompt)
2012-08-16 23:41:25 -04:00
let confirmed = choice ==# 'yes'
else
2019-03-08 06:04:56 -05:00
let prompt = s:inputPrompt("delete") . currentNode.path.str() . " (yN): "
echo prompt
2012-08-16 23:41:25 -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-07-02 07:18:18 -04:00
let bufnum = bufnr("^".currentNode.path.str()."$")
2012-08-16 23:41:25 -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
2019-03-08 06:04:56 -05:00
redraw!
2012-08-16 23:41:25 -04:00
catch /^NERDTree/
2015-12-08 08:20:04 -05:00
call nerdtree#echoWarning("Could not remove node")
2012-08-16 23:41:25 -04:00
endtry
else
2015-12-08 08:20:04 -05:00
call nerdtree#echo("delete aborted")
endif
2019-03-08 06:04:56 -05:00
let &shellslash = l:shellslash
2015-12-08 08:20:04 -05:00
endfunction
" FUNCTION: NERDTreeListNode() {{{1
function! NERDTreeListNode()
let treenode = g:NERDTreeFileNode.GetSelected()
if !empty(treenode)
2018-07-04 06:53:25 -04:00
let s:uname = system("uname")
2018-11-01 06:03:42 -04:00
let stat_cmd = 'stat -c "%s" '
if s:uname =~? "Darwin"
let stat_cmd = 'stat -f "%z" '
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-12-08 08:20:04 -05:00
call nerdtree#echo(metadata[0])
else
call nerdtree#echo("No information available")
2015-12-08 08:20:04 -05:00
endif
endfunction
" FUNCTION: NERDTreeListNodeWin32() {{{1
function! NERDTreeListNodeWin32()
2017-09-02 06:43:18 -04:00
let l:node = g:NERDTreeFileNode.GetSelected()
if !empty(l:node)
2018-11-01 06:03:42 -04:00
let l:path = l:node.path.str()
call nerdtree#echo(printf("%s:%s MOD:%s BYTES:%d PERMISSIONS:%s",
\ toupper(getftype(l:path)),
\ fnamemodify(l:path, ':t'),
\ strftime("%c", getftime(l:path)),
\ getfsize(l:path),
\ getfperm(l:path)))
2017-09-02 06:43:18 -04:00
return
2012-08-16 23:41:25 -04:00
endif
2017-09-02 06:43:18 -04:00
call nerdtree#echo('node not recognized')
2012-08-16 23:41:25 -04:00
endfunction
" FUNCTION: NERDTreeCopyNode() {{{1
function! NERDTreeCopyNode()
2019-03-08 06:04:56 -05:00
let l:shellslash = &shellslash
let &shellslash = 0
2012-08-16 23:41:25 -04:00
let currentNode = g:NERDTreeFileNode.GetSelected()
2019-03-08 06:04:56 -05:00
let prompt = s:inputPrompt("copy")
let newNodePath = input(prompt, currentNode.path.str(), "file")
2012-08-16 23:41:25 -04:00
if newNodePath != ""
"strip trailing slash
let newNodePath = substitute(newNodePath, '\/$', '', '')
let confirmed = 1
if currentNode.path.copyingWillOverwrite(newNodePath)
2015-12-08 08:20:04 -05:00
call nerdtree#echo("Warning: copying may overwrite files! Continue? (yN)")
2012-08-16 23:41:25 -04:00
let choice = nr2char(getchar())
let confirmed = choice ==# 'y'
endif
if confirmed
try
let newNode = currentNode.copy(newNodePath)
2018-11-01 06:03:42 -04:00
" Emptying g:NERDTreeOldSortOrder forces the sort to
" recalculate the cached sortKey so nodes sort correctly.
let g:NERDTreeOldSortOrder = []
2014-07-02 07:18:18 -04:00
if empty(newNode)
2015-12-08 08:20:04 -05:00
call b:NERDTree.root.refresh()
2014-08-03 18:02:51 -04:00
call b:NERDTree.render()
2014-07-02 07:18:18 -04:00
else
2012-08-16 23:41:25 -04:00
call NERDTreeRender()
call newNode.putCursorHere(0, 0)
endif
catch /^NERDTree/
2015-12-08 08:20:04 -05:00
call nerdtree#echoWarning("Could not copy node")
2012-08-16 23:41:25 -04:00
endtry
endif
else
2015-12-08 08:20:04 -05:00
call nerdtree#echo("Copy aborted.")
2012-08-16 23:41:25 -04:00
endif
2019-03-08 06:04:56 -05:00
let &shellslash = l:shellslash
redraw!
2012-08-16 23:41:25 -04:00
endfunction
2019-08-22 11:36:17 -04:00
" FUNCTION: NERDTreeCopyPath() {{{1
function! NERDTreeCopyPath()
let l:nodePath = g:NERDTreeFileNode.GetSelected().path.str()
if has("clipboard")
2019-11-16 10:28:42 -05:00
if &clipboard == "unnamedplus"
let @+ = l:nodePath
else
let @* = l:nodePath
endif
2019-08-22 11:36:17 -04:00
call nerdtree#echo("The path [" . l:nodePath . "] was copied to your clipboard.")
else
call nerdtree#echo("The full path is: " . l:nodePath)
endif
endfunction
2015-12-08 08:20:04 -05:00
" FUNCTION: NERDTreeQuickLook() {{{1
2012-08-16 23:41:25 -04:00
function! NERDTreeQuickLook()
let treenode = g:NERDTreeFileNode.GetSelected()
if treenode != {}
call system("qlmanage -p 2>/dev/null '" . treenode.path.str() . "'")
endif
endfunction
2015-12-08 08:20:04 -05:00
" FUNCTION: NERDTreeRevealInFinder() {{{1
2012-08-16 23:41:25 -04:00
function! NERDTreeRevealInFinder()
let treenode = g:NERDTreeFileNode.GetSelected()
if treenode != {}
2015-12-08 08:20:04 -05:00
call system("open -R '" . treenode.path.str() . "'")
2012-08-16 23:41:25 -04:00
endif
endfunction
2015-12-08 08:20:04 -05:00
" FUNCTION: NERDTreeExecuteFile() {{{1
2012-08-16 23:41:25 -04:00
function! NERDTreeExecuteFile()
let treenode = g:NERDTreeFileNode.GetSelected()
if treenode != {}
2015-12-08 08:20:04 -05:00
call system("open '" . treenode.path.str() . "'")
2012-08-16 23:41:25 -04:00
endif
endfunction
2018-02-04 06:35:08 -05:00
2018-06-14 06:31:12 -04:00
" FUNCTION: NERDTreeRevealFileLinux() {{{1
function! NERDTreeRevealFileLinux()
let treenode = g:NERDTreeFileNode.GetSelected()
let parentnode = treenode.parent
if parentnode != {}
call system("xdg-open '" . parentnode.path.str() . "' &")
endif
endfunction
" FUNCTION: NERDTreeExecuteFileLinux() {{{1
function! NERDTreeExecuteFileLinux()
let treenode = g:NERDTreeFileNode.GetSelected()
if treenode != {}
call system("xdg-open '" . treenode.path.str() . "' &")
endif
endfunction
2012-08-16 23:41:25 -04:00
" vim: set sw=4 sts=4 et fdm=marker:
2018-06-14 06:31:12 -04:00