Skip to content

Commit 719ec0f

Browse files
michelesrchrisbra
authored andcommitted
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: vim#17334 closes: vim#17339 Signed-off-by: Michele Sorcinelli <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 5a8f995 commit 719ec0f

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

runtime/autoload/tar.vim

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
" 2025 Mar 02 by Vim Project: determine the compression using readblob()
1616
" instead of shelling out to file(1)
1717
" 2025 Apr 16 by Vim Project: decouple from netrw by adding s:WinPath()
18+
" 2025 May 19 by Vim Project: restore working directory after read/write
1819
"
1920
" Contains many ideas from Michael Toren's <tar.vim>
2021
"
@@ -306,10 +307,10 @@ fun! tar#Read(fname,mode)
306307

307308
" attempt to change to the indicated directory
308309
try
309-
exe "cd ".fnameescape(tmpdir)
310+
exe "lcd ".fnameescape(tmpdir)
310311
catch /^Vim\%((\a\+)\)\=:E344/
311312
redraw!
312-
echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
313+
echohl Error | echo "***error*** (tar#Write) cannot lcd to temporary directory" | Echohl None
313314
let &report= repkeep
314315
return
315316
endtry
@@ -319,7 +320,7 @@ fun! tar#Read(fname,mode)
319320
call s:Rmdir("_ZIPVIM_")
320321
endif
321322
call mkdir("_ZIPVIM_")
322-
cd _ZIPVIM_
323+
lcd _ZIPVIM_
323324

324325
if has("win32unix") && executable("cygpath")
325326
" assuming cygwin
@@ -413,9 +414,9 @@ fun! tar#Read(fname,mode)
413414
redraw!
414415

415416
if v:shell_error != 0
416-
cd ..
417+
lcd ..
417418
call s:Rmdir("_ZIPVIM_")
418-
exe "cd ".fnameescape(curdir)
419+
exe "lcd ".fnameescape(curdir)
419420
echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
420421
endif
421422

@@ -432,14 +433,16 @@ if v:shell_error != 0
432433
set nomod
433434

434435
let &report= repkeep
436+
exe "lcd ".fnameescape(curdir)
437+
silent exe "file tarfile::".escape_file
435438
endfun
436439

437440
" ---------------------------------------------------------------------
438441
" tar#Write: {{{2
439442
fun! tar#Write(fname)
443+
let pwdkeep= getcwd()
440444
let repkeep= &report
441445
set report=10
442-
" temporary buffer variable workaround because too fucking tired. but it works now
443446
let curdir= b:curdir
444447
let tmpdir= b:tmpdir
445448

@@ -563,9 +566,9 @@ fun! tar#Write(fname)
563566
endif
564567

565568
" cleanup and restore current directory
566-
cd ..
569+
lcd ..
567570
call s:Rmdir("_ZIPVIM_")
568-
exe "cd ".fnameescape(curdir)
571+
exe "lcd ".fnameescape(pwdkeep)
569572
setlocal nomod
570573

571574
let &report= repkeep
@@ -807,7 +810,7 @@ fun! tar#Vimuntar(...)
807810
if simplify(curdir) != simplify(vimhome)
808811
" copy (possibly compressed) tarball to .vim/vimfiles
809812
call system(s:WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
810-
exe "cd ".fnameescape(vimhome)
813+
exe "lcd ".fnameescape(vimhome)
811814
endif
812815

813816
" if necessary, decompress the tarball; then, extract it
@@ -821,7 +824,7 @@ fun! tar#Vimuntar(...)
821824
if simplify(curdir) != simplify(tarhome)
822825
" remove decompressed tarball, restore directory
823826
call delete(tartail.".tar")
824-
exe "cd ".fnameescape(curdir)
827+
exe "lcd ".fnameescape(curdir)
825828
endif
826829
return
827830
endif
@@ -839,7 +842,7 @@ fun! tar#Vimuntar(...)
839842
if simplify(tarhome) != simplify(vimhome)
840843
" remove decompressed tarball, restore directory
841844
call delete(vimhome."/".tarbase.".tar")
842-
exe "cd ".fnameescape(curdir)
845+
exe "lcd ".fnameescape(curdir)
843846
endif
844847
endfun
845848

0 commit comments

Comments
 (0)