Make :Gmove and :Gremove global

This commit is contained in:
Tim Pope
2019-09-03 21:29:55 -04:00
parent 2464b0c29a
commit cf88ce96c9
2 changed files with 38 additions and 21 deletions

View File

@@ -4513,26 +4513,31 @@ endfunction
" Section: :Gmove, :Gremove " Section: :Gmove, :Gremove
function! s:Move(force, rename, destination) abort function! s:Move(force, rename, destination) abort
let dir = s:Dir()
exe s:DirCheck(dir)
if s:DirCommitFile(@%)[1] !~# '^0\=$' || empty(@%)
return 'echoerr ' . string('fugitive: mv not supported for this buffer')
endif
if a:destination =~# '^\.\.\=\%(/\|$\)' if a:destination =~# '^\.\.\=\%(/\|$\)'
let destination = simplify(getcwd() . '/' . a:destination) let destination = simplify(getcwd() . '/' . a:destination)
elseif a:destination =~# '^\a\+:\|^/' elseif a:destination =~# '^\a\+:\|^/'
let destination = a:destination let destination = a:destination
elseif a:destination =~# '^:/:\=' elseif a:destination =~# '^:/:\='
let destination = s:Tree() . substitute(a:destination, '^:/:\=', '', '') let destination = s:Tree(dir) . substitute(a:destination, '^:/:\=', '', '')
elseif a:destination =~# '^:(\%(top\|top,literal\|literal,top\))' elseif a:destination =~# '^:(\%(top\|top,literal\|literal,top\))'
let destination = s:Tree() . matchstr(a:destination, ')\zs.*') let destination = s:Tree(dir) . matchstr(a:destination, ')\zs.*')
elseif a:destination =~# '^:(literal)' elseif a:destination =~# '^:(literal)'
let destination = simplify(getcwd() . '/' . matchstr(a:destination, ')\zs.*')) let destination = simplify(getcwd() . '/' . matchstr(a:destination, ')\zs.*'))
elseif a:rename elseif a:rename
let destination = expand('%:p:s?[\/]$??:h') . '/' . a:destination let destination = expand('%:p:s?[\/]$??:h') . '/' . a:destination
else else
let destination = s:Tree() . '/' . a:destination let destination = s:Tree(dir) . '/' . a:destination
endif endif
let destination = s:Slash(destination) let destination = s:Slash(destination)
if isdirectory(@%) if isdirectory(@%)
setlocal noswapfile setlocal noswapfile
endif endif
let [message, exec_error] = s:ChompError(['mv'] + (a:force ? ['-f'] : []) + ['--', expand('%:p'), destination]) let [message, exec_error] = s:ChompError(['mv'] + (a:force ? ['-f'] : []) + ['--', expand('%:p'), destination], dir)
if exec_error if exec_error
let v:errmsg = 'fugitive: '.message let v:errmsg = 'fugitive: '.message
return 'echoerr v:errmsg' return 'echoerr v:errmsg'
@@ -4540,7 +4545,7 @@ function! s:Move(force, rename, destination) abort
if isdirectory(destination) if isdirectory(destination)
let destination = fnamemodify(s:sub(destination,'/$','').'/'.expand('%:t'),':.') let destination = fnamemodify(s:sub(destination,'/$','').'/'.expand('%:t'),':.')
endif endif
call fugitive#ReloadStatus() call fugitive#ReloadStatus(dir)
if empty(s:DirCommitFile(@%)[1]) if empty(s:DirCommitFile(@%)[1])
if isdirectory(destination) if isdirectory(destination)
return 'keepalt edit '.s:fnameescape(destination) return 'keepalt edit '.s:fnameescape(destination)
@@ -4548,11 +4553,11 @@ function! s:Move(force, rename, destination) abort
return 'keepalt saveas! '.s:fnameescape(destination) return 'keepalt saveas! '.s:fnameescape(destination)
endif endif
else else
return 'file '.s:fnameescape(s:Generate(':0:'.destination)) return 'file '.s:fnameescape(fugitive#Find(':0:'.destination, dir))
endif endif
endfunction endfunction
function! s:RenameComplete(A,L,P) abort function! fugitive#RenameComplete(A,L,P) abort
if a:A =~# '^[.:]\=/' if a:A =~# '^[.:]\=/'
return fugitive#CompletePath(a:A) return fugitive#CompletePath(a:A)
else else
@@ -4561,37 +4566,44 @@ function! s:RenameComplete(A,L,P) abort
endif endif
endfunction endfunction
function! fugitive#MoveCommand(line1, line2, range, bang, mods, arg, args) abort
return s:Move(a:bang, 0, a:arg)
endfunction
function! fugitive#RenameCommand(line1, line2, range, bang, mods, arg, args) abort
return s:Move(a:bang, 1, a:arg)
endfunction
function! s:Remove(after, force) abort function! s:Remove(after, force) abort
if s:DirCommitFile(@%)[1] ==# '' let dir = s:Dir()
exe s:DirCheck(dir)
if len(@%) && s:DirCommitFile(@%)[1] ==# ''
let cmd = ['rm'] let cmd = ['rm']
elseif s:DirCommitFile(@%)[1] ==# '0' elseif s:DirCommitFile(@%)[1] ==# '0'
let cmd = ['rm','--cached'] let cmd = ['rm','--cached']
else else
let v:errmsg = 'fugitive: rm not supported here' return 'echoerr ' . string('fugitive: rm not supported for this buffer')
return 'echoerr v:errmsg'
endif endif
if a:force if a:force
let cmd += ['--force'] let cmd += ['--force']
endif endif
let [message, exec_error] = s:ChompError(cmd + ['--', expand('%:p')]) let [message, exec_error] = s:ChompError(cmd + ['--', expand('%:p')], dir)
if exec_error if exec_error
let v:errmsg = 'fugitive: '.s:sub(message,'error:.*\zs\n\(.*-f.*',' (add ! to force)') let v:errmsg = 'fugitive: '.s:sub(message,'error:.*\zs\n\(.*-f.*',' (add ! to force)')
return 'echoerr '.string(v:errmsg) return 'echoerr '.string(v:errmsg)
else else
call fugitive#ReloadStatus() call fugitive#ReloadStatus(dir)
return a:after . (a:force ? '!' : '') return a:after . (a:force ? '!' : '')
endif endif
endfunction endfunction
augroup fugitive_remove function! fugitive#RemoveCommand(line1, line2, range, bang, mods, arg, args) abort
autocmd! return s:Remove('edit', a:bang)
autocmd User Fugitive if s:DirCommitFile(@%)[1] =~# '^0\=$' | endfunction
\ exe "command! -buffer -bar -bang -nargs=1 -complete=customlist,fugitive#CompletePath 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>)" | function! fugitive#DeleteCommand(line1, line2, range, bang, mods, arg, args) abort
\ exe "command! -buffer -bar -bang Gremove :execute s:Remove('edit',<bang>0)" | return s:Remove('bdelete', a:bang)
\ exe "command! -buffer -bar -bang Gdelete :execute s:Remove('bdelete',<bang>0)" | endfunction
\ endif
augroup END
" Section: :Gblame " Section: :Gblame

View File

@@ -384,3 +384,8 @@ exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject G
exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gw exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gw exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwrite exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwrite exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwq exe fugitive#WqCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwq exe fugitive#WqCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
exe 'command! -bar -bang -nargs=0 -complete=customlist,fugitive#CompleteObject Gremove exe fugitive#RemoveCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
exe 'command! -bar -bang -nargs=0 -complete=customlist,fugitive#CompleteObject Gdelete exe fugitive#DeleteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#CompleteObject Gmove exe fugitive#MoveCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'
exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#RenameComplete Grename exe fugitive#RenameCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])'