Add :Grename

I'll rip out the weird current directory stuff later.
This commit is contained in:
Tim Pope 2018-05-04 21:39:38 -04:00
parent fa1b8652f1
commit dcecb762b0
2 changed files with 24 additions and 6 deletions

View File

@ -196,7 +196,13 @@ that are part of Git repositories).
:Gmove {destination} Wrapper around git-mv that renames the buffer :Gmove {destination} Wrapper around git-mv that renames the buffer
afterward. The destination is relative to the current afterward. The destination is relative to the current
directory except when started with a /, in which case 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* *fugitive-:Gdelete*
:Gdelete Wrapper around git-rm that deletes the buffer :Gdelete Wrapper around git-rm that deletes the buffer

View File

@ -1883,9 +1883,11 @@ endfunction
" Section: Gmove, Gremove " Section: Gmove, Gremove
function! s:Move(force,destination) abort function! s:Move(force, rename, destination) abort
if a:destination =~# '^/' if a:destination =~# '^/'
let destination = a:destination[1:-1] let destination = a:destination[1:-1]
elseif a:rename
let destination = fnamemodify(s:buffer().path(), ':h') . '/' . a:destination
else else
let destination = s:shellslash(fnamemodify(s:sub(a:destination,'[%#]%(:\w)*','\=expand(submatch(0))'),':p')) let destination = s:shellslash(fnamemodify(s:sub(a:destination,'[%#]%(:\w)*','\=expand(submatch(0))'),':p'))
if destination[0:strlen(s:repo().tree())] ==# s:repo().tree('') 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'),':.') let destination = fnamemodify(s:sub(destination,'/$','').'/'.expand('%:t'),':.')
endif endif
call fugitive#reload_status() call fugitive#reload_status()
if s:buffer().commit() == '' if empty(s:buffer().commit())
if isdirectory(destination) if isdirectory(destination)
return 'keepalt edit '.s:fnameescape(destination) return 'keepalt edit '.s:fnameescape(destination)
else else
@ -1918,15 +1920,24 @@ function! s:Move(force,destination) abort
endfunction endfunction
function! s:MoveComplete(A,L,P) abort function! s:MoveComplete(A,L,P) abort
if a:A =~ '^/' if a:A =~# '^/'
return s:repo().superglob(a:A) return s:repo().superglob(a:A)
else else
let matches = split(glob(a:A.'*'),"\n") 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 return matches
endif endif
endfunction 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 function! s:Remove(after, force) abort
if s:buffer().commit() ==# '' if s:buffer().commit() ==# ''
let cmd = ['rm'] let cmd = ['rm']
@ -1952,7 +1963,8 @@ endfunction
augroup fugitive_remove augroup fugitive_remove
autocmd! autocmd!
autocmd User Fugitive if s:buffer().commit() =~# '^0\=$' | autocmd User Fugitive if s:buffer().commit() =~# '^0\=$' |
\ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,s:MoveComplete Gmove :execute s:Move(<bang>0,<q-args>)" | \ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,s:MoveComplete Gmove :execute s:Move(<bang>0,0,<q-args>)" |
\ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,s:RenameComplete Grename :execute s:Move(<bang>0,1,<q-args>)" |
\ exe "command! -buffer -bar -bang Gremove :execute s:Remove('edit',<bang>0)" | \ exe "command! -buffer -bar -bang Gremove :execute s:Remove('edit',<bang>0)" |
\ exe "command! -buffer -bar -bang Gdelete :execute s:Remove('bdelete',<bang>0)" | \ exe "command! -buffer -bar -bang Gdelete :execute s:Remove('bdelete',<bang>0)" |
\ endif \ endif