blob: 3c4c9ccfb2e77a40b5d4f77c161aa00c029881d4 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Christian Brabandt67abf152023-11-14 17:15:17 +01003" Date: Nov 14, 2023
4" Version: 32b (with modifications from the Vim Project)
Bram Moolenaar29634562020-01-09 21:46:04 +01005" Maintainer: Charles E Campbell <NcampObell@SdrPchip.AorgM-NOSPAM>
6" 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 Moolenaar5c736222010-01-06 20:54:52 +010019" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +000020" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000021" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010022if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000023 finish
24endif
Christian Brabandt3d372312023-11-05 17:44:05 +010025let g:loaded_tar= "v32a"
Bram Moolenaar5c736222010-01-06 20:54:52 +010026if v:version < 702
27 echohl WarningMsg
28 echo "***warning*** this version of tar needs vim 7.2"
29 echohl Normal
30 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000031endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010032let s:keepcpo= &cpo
33set cpo&vim
Bram Moolenaarff034192013-04-24 18:51:19 +020034"DechoTabOn
Bram Moolenaar5c736222010-01-06 20:54:52 +010035"call Decho("loading autoload/tar.vim")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000036
Bram Moolenaara5792f52005-11-23 21:25:05 +000037" ---------------------------------------------------------------------
38" Default Settings: {{{1
39if !exists("g:tar_browseoptions")
40 let g:tar_browseoptions= "Ptf"
41endif
42if !exists("g:tar_readoptions")
43 let g:tar_readoptions= "OPxf"
44endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045if !exists("g:tar_cmd")
46 let g:tar_cmd= "tar"
47endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000048if !exists("g:tar_writeoptions")
49 let g:tar_writeoptions= "uf"
50endif
Bram Moolenaar29634562020-01-09 21:46:04 +010051if !exists("g:tar_delfile")
52 let g:tar_delfile="--delete -f"
53endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020054if !exists("g:netrw_cygwin")
55 if has("win32") || has("win95") || has("win64") || has("win16")
56 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
57 let g:netrw_cygwin= 1
58 else
59 let g:netrw_cygwin= 0
60 endif
61 else
62 let g:netrw_cygwin= 0
63 endif
64endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010065if !exists("g:tar_copycmd")
66 if !exists("g:netrw_localcopycmd")
67 if has("win32") || has("win95") || has("win64") || has("win16")
68 if g:netrw_cygwin
69 let g:netrw_localcopycmd= "cp"
70 else
71 let g:netrw_localcopycmd= "copy"
72 endif
73 elseif has("unix") || has("macunix")
74 let g:netrw_localcopycmd= "cp"
75 else
76 let g:netrw_localcopycmd= ""
77 endif
78 endif
79 let g:tar_copycmd= g:netrw_localcopycmd
80endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010081if !exists("g:tar_extractcmd")
82 let g:tar_extractcmd= "tar -xf"
83endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000084
Bram Moolenaar8c8de832008-06-24 22:58:06 +000085" set up shell quoting character
86if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020087 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000088 let g:tar_shq= &shq
89 elseif has("win32") || has("win95") || has("win64") || has("win16")
90 if exists("g:netrw_cygwin") && g:netrw_cygwin
91 let g:tar_shq= "'"
92 else
93 let g:tar_shq= '"'
94 endif
95 else
96 let g:tar_shq= "'"
97 endif
98" call Decho("g:tar_shq<".g:tar_shq.">")
99endif
100
Bram Moolenaara5792f52005-11-23 21:25:05 +0000101" ----------------
102" Functions: {{{1
103" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000104
Bram Moolenaara5792f52005-11-23 21:25:05 +0000105" ---------------------------------------------------------------------
106" tar#Browse: {{{2
107fun! tar#Browse(tarfile)
108" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000109 let repkeep= &report
110 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000111
Bram Moolenaara5792f52005-11-23 21:25:05 +0000112 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000113 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000114 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100115" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000116 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000117 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000118" call Dret("tar#Browse")
119 return
120 endif
121 if !filereadable(a:tarfile)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000122" call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000123 if a:tarfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100124 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000125 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100126" call Decho("***error*** (tar#Browse) File not readable<".a:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000127 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000128 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000129 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000130" call Dret("tar#Browse : file<".a:tarfile."> not readable")
131 return
132 endif
133 if &ma != 1
134 set ma
135 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200136 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000137
Bram Moolenaara5792f52005-11-23 21:25:05 +0000138 setlocal noswapfile
139 setlocal buftype=nofile
140 setlocal bufhidden=hide
141 setlocal nobuflisted
142 setlocal nowrap
143 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000144
Bram Moolenaara5792f52005-11-23 21:25:05 +0000145 " give header
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000146" call Decho("printing header")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000147 let lastline= line("$")
148 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
149 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
150 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200151 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100152 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200153 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000154
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000155 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200156 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000157 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100158 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000159 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000160 let curlast= line("$")
Bram Moolenaar29634562020-01-09 21:46:04 +0100161
162 if tarfile =~# '\.\(gz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100163" call Decho("1: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar29634562020-01-09 21:46:04 +0100164 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
165
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100166 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' || tarfile =~# '\.\(tzst\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100167 if has("unix") && executable("file")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100168 let filekind= system("file ".shellescape(tarfile,1))
Bram Moolenaar29634562020-01-09 21:46:04 +0100169 else
170 let filekind= ""
171 endif
172
173 if filekind =~ "bzip2"
174 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
175 elseif filekind =~ "XZ"
176 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100177 elseif filekind =~ "Zstandard"
178 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100179 else
180 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
181 endif
182
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 elseif tarfile =~# '\.lrp'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100184" call Decho("2: exe silent r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200185 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100186 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100187" call Decho("3: exe silent r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200188 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100189 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100190" call Decho("3: exe silent r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200191 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200192 elseif tarfile =~# '\.\(xz\|txz\)$'
193" call Decho("3: exe silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200194 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100195 elseif tarfile =~# '\.\(zst\|tzst\)$'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100196 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000197 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000198 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100199 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000200 let tarfile = substitute(tarfile, '-', './-', '')
201 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100202" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
Bram Moolenaar251e1912011-06-19 05:09:16 +0200203 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000204 endif
205 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000206 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000207 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000208" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000209 return
210 endif
Christian Brabandt3d372312023-11-05 17:44:05 +0100211 "
Christian Brabandt67abf152023-11-14 17:15:17 +0100212 " The following should not be neccessary, since in case of errors the
213 " previous if statement should have caught the problem (because tar exited
214 " with a non-zero exit code).
215 " if line("$") == curlast || ( line("$") == (curlast + 1) &&
216 " \ getline("$") =~# '\c\<\%(warning\|error\|inappropriate\|unrecognized\)\>' &&
217 " \ getline("$") =~ '\s' )
218 " redraw!
219 " echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
220 " keepj sil! %d
221 " let eikeep= &ei
222 " set ei=BufReadCmd,FileReadCmd
223 " exe "r ".fnameescape(a:tarfile)
224 " let &ei= eikeep
225 " keepj sil! 1d
226 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
227 " return
228 " endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000229
Bram Moolenaar29634562020-01-09 21:46:04 +0100230 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000231 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100232 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100233 noremap <silent> <buffer> x :call tar#Extract()<cr>
Bram Moolenaar29634562020-01-09 21:46:04 +0100234 if &mouse != ""
235 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
236 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000237
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000238 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200239" call Dret("tar#Browse : b:tarfile<".b:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000240endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000241
Bram Moolenaarab194812005-09-14 21:40:12 +0000242" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000243" TarBrowseSelect: {{{2
244fun! s:TarBrowseSelect()
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200245" call Dfunc("TarBrowseSelect() b:tarfile<".b:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000246 let repkeep= &report
247 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000248 let fname= getline(".")
249" call Decho("fname<".fname.">")
250
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000251 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
252 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100253 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000254" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
255 return
256 endif
257
Bram Moolenaara5792f52005-11-23 21:25:05 +0000258 " sanity check
259 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000260 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000261" call Dret("TarBrowseSelect")
262 return
263 endif
264
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200265 " about to make a new window, need to use b:tarfile
266 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000267 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200268 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000269 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100270 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000271 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000272
Bram Moolenaar29634562020-01-09 21:46:04 +0100273 " open a new window (tar#Read will read a file into it)
274 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000275 if !exists("g:tar_nomax") || g:tar_nomax == 0
276 wincmd _
277 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000278 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000279 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000280 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200281 set nomod
282 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000283
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000284 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000285" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
286endfun
287
288" ---------------------------------------------------------------------
289" tar#Read: {{{2
290fun! tar#Read(fname,mode)
291" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000292 let repkeep= &report
293 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000294 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
295 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200296 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000297 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100298 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000299 endif
300" call Decho("tarfile<".tarfile.">")
301" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000302
Bram Moolenaar5c736222010-01-06 20:54:52 +0100303 if fname =~ '\.bz2$' && executable("bzcat")
304 let decmp= "|bzcat"
305 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100306 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000307 let decmp= "|zcat"
308 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100309 elseif fname =~ '\.lzma$' && executable("lzcat")
310 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000311 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200312 elseif fname =~ '\.xz$' && executable("xzcat")
313 let decmp= "|xzcat"
314 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100315 elseif fname =~ '\.zst$' && executable("zstdcat")
316 let decmp= "|zstdcat"
317 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000318 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000319 let decmp=""
320 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200321 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000322 setlocal bin
323 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000324 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000325
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000326 if exists("g:tar_secure")
327 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000328 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000329 let tar_secure= " "
330 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200331
Bram Moolenaar5c736222010-01-06 20:54:52 +0100332 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200333 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar29634562020-01-09 21:46:04 +0100334 elseif tarfile =~# '\.\(gz\)$'
335 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
336
337 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
338 if has("unix") && executable("file")
339 let filekind= system("file ".shellescape(tarfile,1))
340 else
341 let filekind= ""
342 endif
343 if filekind =~ "bzip2"
344 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
345 elseif filekind =~ "XZ"
346 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar23515b42020-11-29 14:36:24 +0100347 elseif filekind =~ "Zstandard"
348 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar29634562020-01-09 21:46:04 +0100349 else
350 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
351 endif
352
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000353 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200354 exe "sil! r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar5c736222010-01-06 20:54:52 +0100355 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200356 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar477db062010-07-28 18:17:41 +0200357 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200358 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000359 else
360 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100361 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000362 let tarfile = substitute(tarfile, '-', './-', '')
363 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100364" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
365 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 endif
367
368 if doro
369 " because the reverse process of compressing changed files back into the tarball is not currently supported
370 setlocal ro
371 endif
372
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200373 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000374 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000375
376 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200377 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000378 set nomod
379
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000380 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200381" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000382endfun
383
384" ---------------------------------------------------------------------
385" tar#Write: {{{2
386fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200387" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000388 let repkeep= &report
389 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000390
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000391 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
392 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100393 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000394" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
395 return
396 endif
397
Bram Moolenaarab194812005-09-14 21:40:12 +0000398 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000400 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100401" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000402 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000403" call Dret("tar#Write")
404 return
405 endif
406 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000407 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100408" call Decho("***error*** (tar#Write) sorry, mkdir() doesn't work on your system")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000409 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000410 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000411" call Dret("tar#Write")
412 return
413 endif
414
415 let curdir= getcwd()
416 let tmpdir= tempname()
417" call Decho("orig tempname<".tmpdir.">")
418 if tmpdir =~ '\.'
419 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
420 endif
421" call Decho("tmpdir<".tmpdir.">")
422 call mkdir(tmpdir,"p")
423
424 " attempt to change to the indicated directory
425 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000426 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000427 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000428 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100429" call Decho("***error*** (tar#Write) cannot cd to temporary directory")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000430 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000431 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000432" call Dret("tar#Write")
433 return
434 endtry
435" call Decho("current directory now: ".getcwd())
436
Bram Moolenaara5792f52005-11-23 21:25:05 +0000437 " place temporary files under .../_ZIPVIM_/
438 if isdirectory("_ZIPVIM_")
439 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000440 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000441 call mkdir("_ZIPVIM_")
442 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000443" call Decho("current directory now: ".getcwd())
444
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200445 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
446 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000447
448 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100449 if tarfile =~# '\.bz2'
450 call system("bzip2 -d -- ".shellescape(tarfile,0))
451 let tarfile = substitute(tarfile,'\.bz2','','e')
452 let compress= "bzip2 -- ".shellescape(tarfile,0)
453" call Decho("compress<".compress.">")
454 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100455 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000456 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100457 let compress= "gzip -- ".shellescape(tarfile,0)
458" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000459 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100460 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000461 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100462 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000463 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000464" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200465 elseif tarfile =~# '\.xz'
466 call system("xz -d -- ".shellescape(tarfile,0))
467 let tarfile = substitute(tarfile,'\.xz','','e')
468 let compress= "xz -- ".shellescape(tarfile,0)
469" call Decho("compress<".compress.">")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100470 elseif tarfile =~# '\.zst'
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100471 call system("zstd --decompress --rm -- ".shellescape(tarfile,0))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100472 let tarfile = substitute(tarfile,'\.zst','','e')
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100473 let compress= "zstd --rm -- ".shellescape(tarfile,0)
Bram Moolenaar477db062010-07-28 18:17:41 +0200474 elseif tarfile =~# '\.lzma'
475 call system("lzma -d -- ".shellescape(tarfile,0))
476 let tarfile = substitute(tarfile,'\.lzma','','e')
477 let compress= "lzma -- ".shellescape(tarfile,0)
478" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000479 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000481
Bram Moolenaarab194812005-09-14 21:40:12 +0000482 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000483 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100484" call Decho("***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000485 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000486 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000487
488" call Decho("tarfile<".tarfile."> fname<".fname.">")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100489
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000490 if fname =~ '/'
491 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200492 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100493 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000494 endif
495 call mkdir(dirpath,"p")
496 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000497 if tarfile !~ '/'
498 let tarfile= curdir.'/'.tarfile
499 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000500 if tarfile =~ '^\s*-'
501 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
502 let tarfile = substitute(tarfile, '-', './-', '')
503 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000504" call Decho("tarfile<".tarfile."> fname<".fname.">")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100505
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000506 if exists("g:tar_secure")
507 let tar_secure= " -- "
508 else
509 let tar_secure= " "
510 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200512 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100513 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000514 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100515
Bram Moolenaara5792f52005-11-23 21:25:05 +0000516 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100517" call Decho("system(".g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
518 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000519 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000520 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100521" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000522 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000523 else
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100524
525 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100526" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
527 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000528 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000529 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100530" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000532 elseif exists("compress")
533" call Decho("call system(".compress.")")
534 call system(compress)
535 if exists("tgz")
536" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
537 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
538 endif
539 endif
540 endif
541
542 " support writing tarfiles across a network
543 if s:tblfile_{winnr()} =~ '^\a\+://'
544" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
545 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100546 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100547 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000548 let eikeep = &ei
549 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100550 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000551 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100552 let &ei = eikeep
553 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000554 q!
555 unlet s:tblfile_{winnr()}
556 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000557 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100558
Bram Moolenaarab194812005-09-14 21:40:12 +0000559 " cleanup and restore current directory
560 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000561 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000562 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000563 setlocal nomod
564
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000565 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000566" call Dret("tar#Write")
567endfun
568
569" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200570" tar#Diff: {{{2
571fun! tar#Diff(userfname,fname)
572" call Dfunc("tar#Diff(userfname<".a:userfname."> fname<".a:fname.")")
573 let fname= a:fname
574 if a:userfname != ""
575 let fname= a:userfname
576 endif
577 if filereadable(fname)
578 " sets current file (from tarball) for diff'ing
579 " splits window vertically
580 " opens original file, sets it for diff'ing
581 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
582 diffthis
583 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100584 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200585 diffthis
586 else
587 redraw!
588 echo "***warning*** unable to read file<".fname.">"
589 endif
590" call Dret("tar#Diff")
591endfun
592
593" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100594" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
595fun! tar#Extract()
596" call Dfunc("tar#Extract()")
597
598 let repkeep= &report
599 set report=10
600 let fname= getline(".")
601" call Decho("fname<".fname.">")
602
603 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
604 redraw!
605 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
606" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
607 return
608 endif
609
610 " sanity check
611 if fname =~ '^"'
612 let &report= repkeep
613" call Dret("TarBrowseSelect")
614 return
615 endif
616
617 let tarball = expand("%")
618" call Decho("tarball<".tarball.">")
619 let tarbase = substitute(tarball,'\..*$','','')
620" call Decho("tarbase<".tarbase.">")
621
622 let extractcmd= netrw#WinPath(g:tar_extractcmd)
623 if filereadable(tarbase.".tar")
624" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname).")")
625 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
626 if v:shell_error != 0
627 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
628" call Decho("***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!")
629 else
630 echo "***note*** successfully extracted ".fname
631 endif
632
633 elseif filereadable(tarbase.".tgz")
634 let extractcmd= substitute(extractcmd,"-","-z","")
635" call Decho("system(".extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname).")")
636 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
637 if v:shell_error != 0
638 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
639" call Decho("***error*** ".extractcmd."t ".tarbase.".tgz ".fname.": failed!")
640 else
641 echo "***note*** successfully extracted ".fname
642 endif
643
644 elseif filereadable(tarbase.".tar.gz")
645 let extractcmd= substitute(extractcmd,"-","-z","")
646" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname).")")
647 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
648 if v:shell_error != 0
649 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
650" call Decho("***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!")
651 else
652 echo "***note*** successfully extracted ".fname
653 endif
654
655 elseif filereadable(tarbase.".tbz")
656 let extractcmd= substitute(extractcmd,"-","-j","")
657" call Decho("system(".extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname).")")
658 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
659 if v:shell_error != 0
660 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
661" call Decho("***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!")
662 else
663 echo "***note*** successfully extracted ".fname
664 endif
665
666 elseif filereadable(tarbase.".tar.bz2")
667 let extractcmd= substitute(extractcmd,"-","-j","")
668" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname).")")
669 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
670 if v:shell_error != 0
671 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
672" call Decho("***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!")
673 else
674 echo "***note*** successfully extracted ".fname
675 endif
676
677 elseif filereadable(tarbase.".txz")
678 let extractcmd= substitute(extractcmd,"-","-J","")
679" call Decho("system(".extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname).")")
680 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
681 if v:shell_error != 0
682 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
683" call Decho("***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!")
684 else
685 echo "***note*** successfully extracted ".fname
686 endif
687
688 elseif filereadable(tarbase.".tar.xz")
689 let extractcmd= substitute(extractcmd,"-","-J","")
690" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname).")")
691 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
692 if v:shell_error != 0
693 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
694" call Decho("***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!")
695 else
696 echo "***note*** successfully extracted ".fname
697 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100698
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100699 elseif filereadable(tarbase.".tzst")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100700 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100701" call Decho("system(".extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname).")")
702 call system(extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100703 if v:shell_error != 0
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100704 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!" | echohl NONE
705" call Decho("***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100706 else
707 echo "***note*** successfully extracted ".fname
708 endif
709
710 elseif filereadable(tarbase.".tar.zst")
711 let extractcmd= substitute(extractcmd,"-","--zstd","")
712" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname).")")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100713 call system(extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100714 if v:shell_error != 0
715 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
716" call Decho("***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!")
717 else
718 echo "***note*** successfully extracted ".fname
719 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100720 endif
721
722 " restore option
723 let &report= repkeep
724
725" call Dret("tar#Extract")
726endfun
727
728" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100729" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000730fun! s:Rmdir(fname)
731" call Dfunc("Rmdir(fname<".a:fname.">)")
732 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100733 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000734 elseif has("win32") || has("win95") || has("win64") || has("win16")
735 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100736 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000737 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100738 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000739 endif
740 endif
741" call Dret("Rmdir")
742endfun
743
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000744" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100745" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
746fun! tar#Vimuntar(...)
747" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
748 let tarball = expand("%")
749" call Decho("tarball<".tarball.">")
750 let tarbase = substitute(tarball,'\..*$','','')
751" call Decho("tarbase<".tarbase.">")
752 let tarhome = expand("%:p")
753 if has("win32") || has("win95") || has("win64") || has("win16")
754 let tarhome= substitute(tarhome,'\\','/','g')
755 endif
756 let tarhome= substitute(tarhome,'/[^/]*$','','')
757" call Decho("tarhome<".tarhome.">")
758 let tartail = expand("%:t")
759" call Decho("tartail<".tartail.">")
760 let curdir = getcwd()
761" call Decho("curdir <".curdir.">")
762 " set up vimhome
763 if a:0 > 0 && a:1 != ""
764 let vimhome= a:1
765 else
766 let vimhome= vimball#VimballHome()
767 endif
768" call Decho("vimhome<".vimhome.">")
769
770" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
771 if simplify(curdir) != simplify(vimhome)
772 " copy (possibly compressed) tarball to .vim/vimfiles
773" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
774 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
775" call Decho("exe cd ".fnameescape(vimhome))
776 exe "cd ".fnameescape(vimhome)
777 endif
778" call Decho("getcwd<".getcwd().">")
779
780 " if necessary, decompress the tarball; then, extract it
781 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100782 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100783 silent exe "!gunzip ".shellescape(tartail)
784 elseif executable("gzip")
785 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000786 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200787 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100788 if simplify(curdir) != simplify(tarhome)
789 " remove decompressed tarball, restore directory
790" call Decho("delete(".tartail.".tar)")
791 call delete(tartail.".tar")
792" call Decho("exe cd ".fnameescape(curdir))
793 exe "cd ".fnameescape(curdir)
794 endif
795" call Dret("tar#Vimuntar")
796 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000797 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000798 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100799 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000800 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100801 let extractcmd= netrw#WinPath(g:tar_extractcmd)
802" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
803 call system(extractcmd." ".shellescape(tarbase.".tar"))
804
805 " set up help
806 if filereadable("doc/".tarbase.".txt")
807" call Decho("exe helptags ".getcwd()."/doc")
808 exe "helptags ".getcwd()."/doc"
809 endif
810
811 if simplify(tarhome) != simplify(vimhome)
812 " remove decompressed tarball, restore directory
813 call delete(vimhome."/".tarbase.".tar")
814 exe "cd ".fnameescape(curdir)
815 endif
816
817" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000818endfun
819
Bram Moolenaar5c736222010-01-06 20:54:52 +0100820" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000821" Modelines And Restoration: {{{1
822let &cpo= s:keepcpo
823unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000824" vim:ts=8 fdm=marker