mirror of
https://github.com/vim/vim
synced 2025-07-16 01:01:58 +00:00
runtime(tar): preserve pwd when reading and writing tar files
While at it, use `:lcd` to temporarily set the window local directory instead of `:cd` for the global working directory. fixes: #17334 closes: #17339 Signed-off-by: Michele Sorcinelli <michelesr@autistici.org> Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
5a8f9958e2
commit
719ec0fe15
@ -15,6 +15,7 @@
|
|||||||
" 2025 Mar 02 by Vim Project: determine the compression using readblob()
|
" 2025 Mar 02 by Vim Project: determine the compression using readblob()
|
||||||
" instead of shelling out to file(1)
|
" instead of shelling out to file(1)
|
||||||
" 2025 Apr 16 by Vim Project: decouple from netrw by adding s:WinPath()
|
" 2025 Apr 16 by Vim Project: decouple from netrw by adding s:WinPath()
|
||||||
|
" 2025 May 19 by Vim Project: restore working directory after read/write
|
||||||
"
|
"
|
||||||
" Contains many ideas from Michael Toren's <tar.vim>
|
" Contains many ideas from Michael Toren's <tar.vim>
|
||||||
"
|
"
|
||||||
@ -306,10 +307,10 @@ fun! tar#Read(fname,mode)
|
|||||||
|
|
||||||
" attempt to change to the indicated directory
|
" attempt to change to the indicated directory
|
||||||
try
|
try
|
||||||
exe "cd ".fnameescape(tmpdir)
|
exe "lcd ".fnameescape(tmpdir)
|
||||||
catch /^Vim\%((\a\+)\)\=:E344/
|
catch /^Vim\%((\a\+)\)\=:E344/
|
||||||
redraw!
|
redraw!
|
||||||
echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
|
echohl Error | echo "***error*** (tar#Write) cannot lcd to temporary directory" | Echohl None
|
||||||
let &report= repkeep
|
let &report= repkeep
|
||||||
return
|
return
|
||||||
endtry
|
endtry
|
||||||
@ -319,7 +320,7 @@ fun! tar#Read(fname,mode)
|
|||||||
call s:Rmdir("_ZIPVIM_")
|
call s:Rmdir("_ZIPVIM_")
|
||||||
endif
|
endif
|
||||||
call mkdir("_ZIPVIM_")
|
call mkdir("_ZIPVIM_")
|
||||||
cd _ZIPVIM_
|
lcd _ZIPVIM_
|
||||||
|
|
||||||
if has("win32unix") && executable("cygpath")
|
if has("win32unix") && executable("cygpath")
|
||||||
" assuming cygwin
|
" assuming cygwin
|
||||||
@ -413,9 +414,9 @@ fun! tar#Read(fname,mode)
|
|||||||
redraw!
|
redraw!
|
||||||
|
|
||||||
if v:shell_error != 0
|
if v:shell_error != 0
|
||||||
cd ..
|
lcd ..
|
||||||
call s:Rmdir("_ZIPVIM_")
|
call s:Rmdir("_ZIPVIM_")
|
||||||
exe "cd ".fnameescape(curdir)
|
exe "lcd ".fnameescape(curdir)
|
||||||
echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
|
echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@ -432,14 +433,16 @@ if v:shell_error != 0
|
|||||||
set nomod
|
set nomod
|
||||||
|
|
||||||
let &report= repkeep
|
let &report= repkeep
|
||||||
|
exe "lcd ".fnameescape(curdir)
|
||||||
|
silent exe "file tarfile::".escape_file
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
" ---------------------------------------------------------------------
|
" ---------------------------------------------------------------------
|
||||||
" tar#Write: {{{2
|
" tar#Write: {{{2
|
||||||
fun! tar#Write(fname)
|
fun! tar#Write(fname)
|
||||||
|
let pwdkeep= getcwd()
|
||||||
let repkeep= &report
|
let repkeep= &report
|
||||||
set report=10
|
set report=10
|
||||||
" temporary buffer variable workaround because too fucking tired. but it works now
|
|
||||||
let curdir= b:curdir
|
let curdir= b:curdir
|
||||||
let tmpdir= b:tmpdir
|
let tmpdir= b:tmpdir
|
||||||
|
|
||||||
@ -563,9 +566,9 @@ fun! tar#Write(fname)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
" cleanup and restore current directory
|
" cleanup and restore current directory
|
||||||
cd ..
|
lcd ..
|
||||||
call s:Rmdir("_ZIPVIM_")
|
call s:Rmdir("_ZIPVIM_")
|
||||||
exe "cd ".fnameescape(curdir)
|
exe "lcd ".fnameescape(pwdkeep)
|
||||||
setlocal nomod
|
setlocal nomod
|
||||||
|
|
||||||
let &report= repkeep
|
let &report= repkeep
|
||||||
@ -807,7 +810,7 @@ fun! tar#Vimuntar(...)
|
|||||||
if simplify(curdir) != simplify(vimhome)
|
if simplify(curdir) != simplify(vimhome)
|
||||||
" copy (possibly compressed) tarball to .vim/vimfiles
|
" copy (possibly compressed) tarball to .vim/vimfiles
|
||||||
call system(s:WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
|
call system(s:WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
|
||||||
exe "cd ".fnameescape(vimhome)
|
exe "lcd ".fnameescape(vimhome)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" if necessary, decompress the tarball; then, extract it
|
" if necessary, decompress the tarball; then, extract it
|
||||||
@ -821,7 +824,7 @@ fun! tar#Vimuntar(...)
|
|||||||
if simplify(curdir) != simplify(tarhome)
|
if simplify(curdir) != simplify(tarhome)
|
||||||
" remove decompressed tarball, restore directory
|
" remove decompressed tarball, restore directory
|
||||||
call delete(tartail.".tar")
|
call delete(tartail.".tar")
|
||||||
exe "cd ".fnameescape(curdir)
|
exe "lcd ".fnameescape(curdir)
|
||||||
endif
|
endif
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
@ -839,7 +842,7 @@ fun! tar#Vimuntar(...)
|
|||||||
if simplify(tarhome) != simplify(vimhome)
|
if simplify(tarhome) != simplify(vimhome)
|
||||||
" remove decompressed tarball, restore directory
|
" remove decompressed tarball, restore directory
|
||||||
call delete(vimhome."/".tarbase.".tar")
|
call delete(vimhome."/".tarbase.".tar")
|
||||||
exe "cd ".fnameescape(curdir)
|
exe "lcd ".fnameescape(curdir)
|
||||||
endif
|
endif
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user