From dcecb762b059983452c7b1cb30fea4df9d886932 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Fri, 4 May 2018 21:39:38 -0400 Subject: [PATCH] Add :Grename I'll rip out the weird current directory stuff later. --- doc/fugitive.txt | 8 +++++++- plugin/fugitive.vim | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/doc/fugitive.txt b/doc/fugitive.txt index 6c27c6f..c3a9ffb 100644 --- a/doc/fugitive.txt +++ b/doc/fugitive.txt @@ -196,7 +196,13 @@ that are part of Git repositories). :Gmove {destination} Wrapper around git-mv that renames the buffer afterward. The destination is relative to the current directory except when started with a /, in which case - it is relative to the work tree. Add a ! to pass -f. + it is relative to the work tree. (This is a holdover + from before |:Grename| and will be removed.) Add a ! + to pass -f. + + *fugitive-:Grename* +:Grename {destination} Like |:Gmove| but operates relative to the parent + directory of the current file. *fugitive-:Gdelete* :Gdelete Wrapper around git-rm that deletes the buffer diff --git a/plugin/fugitive.vim b/plugin/fugitive.vim index d543dd3..cdd0563 100644 --- a/plugin/fugitive.vim +++ b/plugin/fugitive.vim @@ -1883,9 +1883,11 @@ endfunction " Section: Gmove, Gremove -function! s:Move(force,destination) abort +function! s:Move(force, rename, destination) abort if a:destination =~# '^/' let destination = a:destination[1:-1] + elseif a:rename + let destination = fnamemodify(s:buffer().path(), ':h') . '/' . a:destination else let destination = s:shellslash(fnamemodify(s:sub(a:destination,'[%#]%(:\w)*','\=expand(submatch(0))'),':p')) if destination[0:strlen(s:repo().tree())] ==# s:repo().tree('') @@ -1906,7 +1908,7 @@ function! s:Move(force,destination) abort let destination = fnamemodify(s:sub(destination,'/$','').'/'.expand('%:t'),':.') endif call fugitive#reload_status() - if s:buffer().commit() == '' + if empty(s:buffer().commit()) if isdirectory(destination) return 'keepalt edit '.s:fnameescape(destination) else @@ -1918,15 +1920,24 @@ function! s:Move(force,destination) abort endfunction function! s:MoveComplete(A,L,P) abort - if a:A =~ '^/' + if a:A =~# '^/' return s:repo().superglob(a:A) else let matches = split(glob(a:A.'*'),"\n") - call map(matches,'v:val !~ "/$" && isdirectory(v:val) ? v:val."/" : v:val') + call map(matches,'v:val !~# "/$" && isdirectory(v:val) ? v:val."/" : v:val') return matches endif endfunction +function! s:RenameComplete(A,L,P) abort + if a:A =~# '^/' + return s:repo().superglob(a:A) + else + let pre = '/'. fnamemodify(s:buffer().path(), ':h') . '/' + return map(s:repo().superglob(pre.a:A), 'strpart(v:val, len(pre))') + endif +endfunction + function! s:Remove(after, force) abort if s:buffer().commit() ==# '' let cmd = ['rm'] @@ -1952,7 +1963,8 @@ endfunction augroup fugitive_remove autocmd! autocmd User Fugitive if s:buffer().commit() =~# '^0\=$' | - \ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,s:MoveComplete Gmove :execute s:Move(0,)" | + \ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,s:MoveComplete Gmove :execute s:Move(0,0,)" | + \ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,s:RenameComplete Grename :execute s:Move(0,1,)" | \ exe "command! -buffer -bar -bang Gremove :execute s:Remove('edit',0)" | \ exe "command! -buffer -bar -bang Gdelete :execute s:Remove('bdelete',0)" | \ endif