blob: 5c4499fa6bcfb5438cf6d1b971fa20d50b24afe8 [file] [log] [blame]
Corpulent Robinb69cd522025-02-06 21:10:49 +01001" tar.vim: Handles browsing tarfiles - AUTOLOAD PORTION
2" Date: Feb 06, 2025
Christian Brabandt67abf152023-11-14 17:15:17 +01003" Version: 32b (with modifications from the Vim Project)
Christian Brabandtf9ca1392024-02-19 20:37:11 +01004" Maintainer: This runtime file is looking for a new maintainer.
5" Former Maintainer: Charles E Campbell
Bram Moolenaar29634562020-01-09 21:46:04 +01006" License: Vim License (see vim's :help license)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007"
Bram Moolenaara5792f52005-11-23 21:25:05 +00008" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009"
Bram Moolenaar29634562020-01-09 21:46:04 +010010" Copyright: Copyright (C) 2005-2017 Charles E. Campbell {{{1
Bram Moolenaara5792f52005-11-23 21:25:05 +000011" Permission is hereby granted to use and distribute this code,
12" with or without modifications, provided that this copyright
13" notice is copied with it. Like anything else that's free,
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014" tar.vim and tarPlugin.vim are provided *as is* and comes
15" with no warranty of any kind, either expressed or implied.
16" By using this plugin, you agree that in no event will the
17" copyright holder be liable for any damages resulting from
18" the use of this software.
Bram Moolenaarab194812005-09-14 21:40:12 +000019" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000020" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010021if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000022 finish
23endif
Lennart00129a8442024-11-11 22:39:30 +010024let g:loaded_tar= "v32b"
Bram Moolenaar5c736222010-01-06 20:54:52 +010025if v:version < 702
26 echohl WarningMsg
27 echo "***warning*** this version of tar needs vim 7.2"
28 echohl Normal
29 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010031let s:keepcpo= &cpo
32set cpo&vim
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000033
Bram Moolenaara5792f52005-11-23 21:25:05 +000034" ---------------------------------------------------------------------
35" Default Settings: {{{1
36if !exists("g:tar_browseoptions")
37 let g:tar_browseoptions= "Ptf"
38endif
39if !exists("g:tar_readoptions")
Lennart00129a8442024-11-11 22:39:30 +010040 let g:tar_readoptions= "pPxf"
Bram Moolenaara5792f52005-11-23 21:25:05 +000041endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000042if !exists("g:tar_cmd")
43 let g:tar_cmd= "tar"
44endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000045if !exists("g:tar_writeoptions")
46 let g:tar_writeoptions= "uf"
47endif
Bram Moolenaar29634562020-01-09 21:46:04 +010048if !exists("g:tar_delfile")
49 let g:tar_delfile="--delete -f"
50endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020051if !exists("g:netrw_cygwin")
52 if has("win32") || has("win95") || has("win64") || has("win16")
53 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
54 let g:netrw_cygwin= 1
55 else
56 let g:netrw_cygwin= 0
57 endif
58 else
59 let g:netrw_cygwin= 0
60 endif
61endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010062if !exists("g:tar_copycmd")
63 if !exists("g:netrw_localcopycmd")
64 if has("win32") || has("win95") || has("win64") || has("win16")
65 if g:netrw_cygwin
66 let g:netrw_localcopycmd= "cp"
67 else
68 let g:netrw_localcopycmd= "copy"
69 endif
70 elseif has("unix") || has("macunix")
71 let g:netrw_localcopycmd= "cp"
72 else
73 let g:netrw_localcopycmd= ""
74 endif
75 endif
76 let g:tar_copycmd= g:netrw_localcopycmd
77endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010078if !exists("g:tar_extractcmd")
Lennart00129a8442024-11-11 22:39:30 +010079 let g:tar_extractcmd= "tar -pxf"
Bram Moolenaar5c736222010-01-06 20:54:52 +010080endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000081
Bram Moolenaar8c8de832008-06-24 22:58:06 +000082" set up shell quoting character
83if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020084 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000085 let g:tar_shq= &shq
86 elseif has("win32") || has("win95") || has("win64") || has("win16")
87 if exists("g:netrw_cygwin") && g:netrw_cygwin
88 let g:tar_shq= "'"
89 else
90 let g:tar_shq= '"'
91 endif
92 else
93 let g:tar_shq= "'"
94 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000095endif
96
Bram Moolenaara5792f52005-11-23 21:25:05 +000097" ----------------
98" Functions: {{{1
99" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000100
Bram Moolenaara5792f52005-11-23 21:25:05 +0000101" ---------------------------------------------------------------------
102" tar#Browse: {{{2
103fun! tar#Browse(tarfile)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000104 let repkeep= &report
105 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000106
Bram Moolenaara5792f52005-11-23 21:25:05 +0000107 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000108 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000109 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000110 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000111 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000112 return
113 endif
114 if !filereadable(a:tarfile)
115 if a:tarfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100116 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000117 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000118 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000119 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000120 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000121 return
122 endif
123 if &ma != 1
124 set ma
125 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200126 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000127
Bram Moolenaara5792f52005-11-23 21:25:05 +0000128 setlocal noswapfile
129 setlocal buftype=nofile
130 setlocal bufhidden=hide
131 setlocal nobuflisted
132 setlocal nowrap
133 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000134
Bram Moolenaara5792f52005-11-23 21:25:05 +0000135 " give header
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000136 let lastline= line("$")
137 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
138 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
139 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200140 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100141 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200142 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000143
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000144 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200145 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000146 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100147 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000148 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000149 let curlast= line("$")
Bram Moolenaar29634562020-01-09 21:46:04 +0100150
151 if tarfile =~# '\.\(gz\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100152 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
153
Corpulent Robinb69cd522025-02-06 21:10:49 +0100154 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' ||
155 \ tarfile =~# '\.\(tzst\)$' || tarfile =~# '\.\(tlz4\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100156 if has("unix") && executable("file")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100157 let filekind= system("file ".shellescape(tarfile,1))
Bram Moolenaar29634562020-01-09 21:46:04 +0100158 else
159 let filekind= ""
160 endif
161
162 if filekind =~ "bzip2"
163 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
164 elseif filekind =~ "XZ"
165 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100166 elseif filekind =~ "Zstandard"
167 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100168 elseif filekind =~ "LZ4"
169 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100170 else
171 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
172 endif
173
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000174 elseif tarfile =~# '\.lrp'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200175 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100176 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200177 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100178 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200179 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200180 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200181 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100182 elseif tarfile =~# '\.\(zst\|tzst\)$'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100183 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100184 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
185 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000186 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000187 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100188 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000189 let tarfile = substitute(tarfile, '-', './-', '')
190 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200191 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000192 endif
193 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000194 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000195 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000196 return
197 endif
Christian Brabandt3d372312023-11-05 17:44:05 +0100198 "
Christian Brabandt67abf152023-11-14 17:15:17 +0100199 " The following should not be neccessary, since in case of errors the
200 " previous if statement should have caught the problem (because tar exited
201 " with a non-zero exit code).
202 " if line("$") == curlast || ( line("$") == (curlast + 1) &&
203 " \ getline("$") =~# '\c\<\%(warning\|error\|inappropriate\|unrecognized\)\>' &&
204 " \ getline("$") =~ '\s' )
205 " redraw!
206 " echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
207 " keepj sil! %d
208 " let eikeep= &ei
209 " set ei=BufReadCmd,FileReadCmd
210 " exe "r ".fnameescape(a:tarfile)
211 " let &ei= eikeep
212 " keepj sil! 1d
213 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
214 " return
215 " endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000216
Bram Moolenaar29634562020-01-09 21:46:04 +0100217 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000218 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100219 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100220 noremap <silent> <buffer> x :call tar#Extract()<cr>
Bram Moolenaar29634562020-01-09 21:46:04 +0100221 if &mouse != ""
222 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
223 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000224
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000225 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000226endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000227
Bram Moolenaarab194812005-09-14 21:40:12 +0000228" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000229" TarBrowseSelect: {{{2
230fun! s:TarBrowseSelect()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000231 let repkeep= &report
232 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000233 let fname= getline(".")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000234
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000235 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
236 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100237 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000238 return
239 endif
240
Bram Moolenaara5792f52005-11-23 21:25:05 +0000241 " sanity check
242 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000243 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000244 return
245 endif
246
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200247 " about to make a new window, need to use b:tarfile
248 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000249 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200250 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000251 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100252 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000253 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000254
Bram Moolenaar29634562020-01-09 21:46:04 +0100255 " open a new window (tar#Read will read a file into it)
256 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000257 if !exists("g:tar_nomax") || g:tar_nomax == 0
258 wincmd _
259 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000260 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000261 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000262 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200263 set nomod
264 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000265
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000266 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000267endfun
268
269" ---------------------------------------------------------------------
270" tar#Read: {{{2
271fun! tar#Read(fname,mode)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000272 let repkeep= &report
273 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000274 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
275 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Lennart00129a8442024-11-11 22:39:30 +0100276
Corpulent Robinb69cd522025-02-06 21:10:49 +0100277 " changing the directory to the temporary earlier to allow tar to extract the file with permissions intact
Lennart00129a8442024-11-11 22:39:30 +0100278 if !exists("*mkdir")
279 redraw!
280 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
281 let &report= repkeep
282 return
283 endif
284
285 let curdir= getcwd()
286 let tmpdir= tempname()
287 let b:curdir= tmpdir
288 let b:tmpdir= curdir
289 if tmpdir =~ '\.'
290 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
291 endif
292 call mkdir(tmpdir,"p")
293
294 " attempt to change to the indicated directory
295 try
296 exe "cd ".fnameescape(tmpdir)
297 catch /^Vim\%((\a\+)\)\=:E344/
298 redraw!
299 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
300 let &report= repkeep
301 return
302 endtry
303
304 " place temporary files under .../_ZIPVIM_/
305 if isdirectory("_ZIPVIM_")
306 call s:Rmdir("_ZIPVIM_")
307 endif
308 call mkdir("_ZIPVIM_")
309 cd _ZIPVIM_
310
Bram Moolenaarff034192013-04-24 18:51:19 +0200311 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000312 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100313 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000314 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000315
Bram Moolenaar5c736222010-01-06 20:54:52 +0100316 if fname =~ '\.bz2$' && executable("bzcat")
317 let decmp= "|bzcat"
318 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100319 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000320 let decmp= "|zcat"
321 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100322 elseif fname =~ '\.lzma$' && executable("lzcat")
323 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000324 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200325 elseif fname =~ '\.xz$' && executable("xzcat")
326 let decmp= "|xzcat"
327 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100328 elseif fname =~ '\.zst$' && executable("zstdcat")
329 let decmp= "|zstdcat"
330 let doro = 1
Corpulent Robinb69cd522025-02-06 21:10:49 +0100331 elseif fname =~ '\.lz4$' && executable("lz4cat")
332 let decmp= "|lz4cat"
333 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000334 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000335 let decmp=""
336 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200337 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000338 setlocal bin
339 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000340 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000341
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000342 if exists("g:tar_secure")
343 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000344 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000345 let tar_secure= " "
346 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200347
Bram Moolenaar5c736222010-01-06 20:54:52 +0100348 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200349 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100350 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100351 elseif tarfile =~# '\.\(gz\)$'
352 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100353 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100354 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
355 if has("unix") && executable("file")
356 let filekind= system("file ".shellescape(tarfile,1))
357 else
358 let filekind= ""
359 endif
360 if filekind =~ "bzip2"
361 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100362 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100363 elseif filekind =~ "XZ"
364 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100365 exe "read ".fname
Bram Moolenaar23515b42020-11-29 14:36:24 +0100366 elseif filekind =~ "Zstandard"
367 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100368 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100369 else
370 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100371 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100372 endif
373
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000374 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200375 exe "sil! r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100376 exe "read ".fname
Bram Moolenaar5c736222010-01-06 20:54:52 +0100377 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200378 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100379 exe "read ".fname
Bram Moolenaar477db062010-07-28 18:17:41 +0200380 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200381 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100382 exe "read ".fname
Corpulent Robinb69cd522025-02-06 21:10:49 +0100383 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
384 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
385 exe "read ".fname
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000386 else
387 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100388 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000389 let tarfile = substitute(tarfile, '-', './-', '')
390 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100391 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100392 exe "read ".fname
393 endif
394
395 redraw!
396
397if v:shell_error != 0
398 cd ..
399 call s:Rmdir("_ZIPVIM_")
400 exe "cd ".fnameescape(curdir)
401 echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000402 endif
403
404 if doro
405 " because the reverse process of compressing changed files back into the tarball is not currently supported
406 setlocal ro
407 endif
408
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200409 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000410 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000411
412 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200413 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000414 set nomod
415
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000416 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000417endfun
418
419" ---------------------------------------------------------------------
420" tar#Write: {{{2
421fun! tar#Write(fname)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000422 let repkeep= &report
423 set report=10
Lennart00129a8442024-11-11 22:39:30 +0100424 " temporary buffer variable workaround because too fucking tired. but it works now
425 let curdir= b:curdir
426 let tmpdir= b:tmpdir
Bram Moolenaara5792f52005-11-23 21:25:05 +0000427
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000428 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
429 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100430 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000431 return
432 endif
433
Bram Moolenaarab194812005-09-14 21:40:12 +0000434 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000435 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000436 redraw!
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000437 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000438 return
439 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000440
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200441 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
442 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000443
444 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100445 if tarfile =~# '\.bz2'
446 call system("bzip2 -d -- ".shellescape(tarfile,0))
447 let tarfile = substitute(tarfile,'\.bz2','','e')
448 let compress= "bzip2 -- ".shellescape(tarfile,0)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100449 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100450 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000451 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100452 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000453 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100454 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000455 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100456 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000457 let tgz = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200458 elseif tarfile =~# '\.xz'
459 call system("xz -d -- ".shellescape(tarfile,0))
460 let tarfile = substitute(tarfile,'\.xz','','e')
461 let compress= "xz -- ".shellescape(tarfile,0)
Bram Moolenaar23515b42020-11-29 14:36:24 +0100462 elseif tarfile =~# '\.zst'
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100463 call system("zstd --decompress --rm -- ".shellescape(tarfile,0))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100464 let tarfile = substitute(tarfile,'\.zst','','e')
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100465 let compress= "zstd --rm -- ".shellescape(tarfile,0)
Corpulent Robinb69cd522025-02-06 21:10:49 +0100466 elseif tarfile =~# '\.lz4'
467 call system("lz4 --decompress --rm -- ".shellescape(tarfile,0))
468 let tarfile = substitute(tarfile,'\.lz4','','e')
469 let compress= "lz4 --rm -- ".shellescape(tarfile,0)
Bram Moolenaar477db062010-07-28 18:17:41 +0200470 elseif tarfile =~# '\.lzma'
471 call system("lzma -d -- ".shellescape(tarfile,0))
472 let tarfile = substitute(tarfile,'\.lzma','','e')
473 let compress= "lzma -- ".shellescape(tarfile,0)
Bram Moolenaarab194812005-09-14 21:40:12 +0000474 endif
475
Bram Moolenaarab194812005-09-14 21:40:12 +0000476 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000477 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000478 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000479 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000480
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000481 if fname =~ '/'
482 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200483 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100484 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000485 endif
486 call mkdir(dirpath,"p")
487 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000488 if tarfile !~ '/'
489 let tarfile= curdir.'/'.tarfile
490 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000491 if tarfile =~ '^\s*-'
492 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
493 let tarfile = substitute(tarfile, '-', './-', '')
494 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100495
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000496 if exists("g:tar_secure")
497 let tar_secure= " -- "
498 else
499 let tar_secure= " "
500 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200502 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100503 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000504 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100505
Bram Moolenaara5792f52005-11-23 21:25:05 +0000506 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100507 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000508 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000509 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000511 else
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100512
513 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100514 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000515 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000516 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000518 elseif exists("compress")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000519 call system(compress)
520 if exists("tgz")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000521 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
522 endif
523 endif
524 endif
525
526 " support writing tarfiles across a network
527 if s:tblfile_{winnr()} =~ '^\a\+://'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000528 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100529 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100530 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000531 let eikeep = &ei
532 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100533 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000534 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100535 let &ei = eikeep
536 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000537 q!
538 unlet s:tblfile_{winnr()}
539 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000540 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100541
Bram Moolenaarab194812005-09-14 21:40:12 +0000542 " cleanup and restore current directory
543 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000544 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000545 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000546 setlocal nomod
547
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000548 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000549endfun
550
551" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200552" tar#Diff: {{{2
553fun! tar#Diff(userfname,fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200554 let fname= a:fname
555 if a:userfname != ""
556 let fname= a:userfname
557 endif
558 if filereadable(fname)
559 " sets current file (from tarball) for diff'ing
560 " splits window vertically
561 " opens original file, sets it for diff'ing
562 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
563 diffthis
564 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100565 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200566 diffthis
567 else
568 redraw!
569 echo "***warning*** unable to read file<".fname.">"
570 endif
Bram Moolenaarff034192013-04-24 18:51:19 +0200571endfun
572
573" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100574" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
575fun! tar#Extract()
Bram Moolenaar29634562020-01-09 21:46:04 +0100576
577 let repkeep= &report
578 set report=10
579 let fname= getline(".")
Bram Moolenaar29634562020-01-09 21:46:04 +0100580
581 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
582 redraw!
583 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaar29634562020-01-09 21:46:04 +0100584 return
585 endif
586
587 " sanity check
588 if fname =~ '^"'
589 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100590 return
591 endif
592
593 let tarball = expand("%")
Bram Moolenaar29634562020-01-09 21:46:04 +0100594 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar29634562020-01-09 21:46:04 +0100595
596 let extractcmd= netrw#WinPath(g:tar_extractcmd)
597 if filereadable(tarbase.".tar")
Bram Moolenaar29634562020-01-09 21:46:04 +0100598 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
599 if v:shell_error != 0
600 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100601 else
602 echo "***note*** successfully extracted ".fname
603 endif
604
605 elseif filereadable(tarbase.".tgz")
606 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100607 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
608 if v:shell_error != 0
609 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100610 else
611 echo "***note*** successfully extracted ".fname
612 endif
613
614 elseif filereadable(tarbase.".tar.gz")
615 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100616 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
617 if v:shell_error != 0
618 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100619 else
620 echo "***note*** successfully extracted ".fname
621 endif
622
623 elseif filereadable(tarbase.".tbz")
624 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100625 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
626 if v:shell_error != 0
627 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100628 else
629 echo "***note*** successfully extracted ".fname
630 endif
631
632 elseif filereadable(tarbase.".tar.bz2")
633 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100634 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
635 if v:shell_error != 0
636 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100637 else
638 echo "***note*** successfully extracted ".fname
639 endif
640
641 elseif filereadable(tarbase.".txz")
642 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100643 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
644 if v:shell_error != 0
645 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100646 else
647 echo "***note*** successfully extracted ".fname
648 endif
649
650 elseif filereadable(tarbase.".tar.xz")
651 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100652 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
653 if v:shell_error != 0
654 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100655 else
656 echo "***note*** successfully extracted ".fname
657 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100658
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100659 elseif filereadable(tarbase.".tzst")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100660 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100661 call system(extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100662 if v:shell_error != 0
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100663 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100664 else
665 echo "***note*** successfully extracted ".fname
666 endif
667
668 elseif filereadable(tarbase.".tar.zst")
669 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100670 call system(extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100671 if v:shell_error != 0
672 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
Corpulent Robinb69cd522025-02-06 21:10:49 +0100673 else
674 echo "***note*** successfully extracted ".fname
675 endif
676
677 elseif filereadable(tarbase.".tlz4")
678 let extractcmd= substitute(extractcmd,"-","-I lz4","")
679 call system(extractcmd." ".shellescape(tarbase).".tlz4 ".shellescape(fname))
680 if v:shell_error != 0
681 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tlz4 ".fname.": failed!" | echohl NONE
682 else
683 echo "***note*** successfully extracted ".fname
684 endif
685
686 elseif filereadable(tarbase.".tar.lz4")
687 let extractcmd= substitute(extractcmd,"-","-I lz4","")
688 call system(extractcmd." ".shellescape(tarbase).".tar.lz4".shellescape(fname))
689 if v:shell_error != 0
690 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.lz4 ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100691 else
692 echo "***note*** successfully extracted ".fname
693 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100694 endif
695
696 " restore option
697 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100698endfun
699
700" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100701" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000702fun! s:Rmdir(fname)
Bram Moolenaarab194812005-09-14 21:40:12 +0000703 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100704 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000705 elseif has("win32") || has("win95") || has("win64") || has("win16")
706 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100707 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000708 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100709 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000710 endif
711 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000712endfun
713
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000714" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100715" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
716fun! tar#Vimuntar(...)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100717 let tarball = expand("%")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100718 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100719 let tarhome = expand("%:p")
720 if has("win32") || has("win95") || has("win64") || has("win16")
721 let tarhome= substitute(tarhome,'\\','/','g')
722 endif
723 let tarhome= substitute(tarhome,'/[^/]*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100724 let tartail = expand("%:t")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100725 let curdir = getcwd()
Bram Moolenaar5c736222010-01-06 20:54:52 +0100726 " set up vimhome
727 if a:0 > 0 && a:1 != ""
728 let vimhome= a:1
729 else
730 let vimhome= vimball#VimballHome()
731 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100732
Bram Moolenaar5c736222010-01-06 20:54:52 +0100733 if simplify(curdir) != simplify(vimhome)
734 " copy (possibly compressed) tarball to .vim/vimfiles
Bram Moolenaar5c736222010-01-06 20:54:52 +0100735 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100736 exe "cd ".fnameescape(vimhome)
737 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100738
739 " if necessary, decompress the tarball; then, extract it
740 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100741 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100742 silent exe "!gunzip ".shellescape(tartail)
743 elseif executable("gzip")
744 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000745 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200746 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100747 if simplify(curdir) != simplify(tarhome)
748 " remove decompressed tarball, restore directory
Bram Moolenaar5c736222010-01-06 20:54:52 +0100749 call delete(tartail.".tar")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100750 exe "cd ".fnameescape(curdir)
751 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100752 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000753 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100755 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100757 let extractcmd= netrw#WinPath(g:tar_extractcmd)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100758 call system(extractcmd." ".shellescape(tarbase.".tar"))
759
760 " set up help
761 if filereadable("doc/".tarbase.".txt")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100762 exe "helptags ".getcwd()."/doc"
763 endif
764
765 if simplify(tarhome) != simplify(vimhome)
766 " remove decompressed tarball, restore directory
767 call delete(vimhome."/".tarbase.".tar")
768 exe "cd ".fnameescape(curdir)
769 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770endfun
771
Bram Moolenaar5c736222010-01-06 20:54:52 +0100772" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000773" Modelines And Restoration: {{{1
774let &cpo= s:keepcpo
775unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776" vim:ts=8 fdm=marker