blob: 9c4f16d7f60a65498538edf3445dd517f88bac7b [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)
Christian Brabandtf9ca1392024-02-19 20:37:11 +01005" Maintainer: This runtime file is looking for a new maintainer.
6" Former Maintainer: Charles E Campbell
Bram Moolenaar29634562020-01-09 21:46:04 +01007" License: Vim License (see vim's :help license)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008"
Bram Moolenaara5792f52005-11-23 21:25:05 +00009" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000010"
Bram Moolenaar29634562020-01-09 21:46:04 +010011" Copyright: Copyright (C) 2005-2017 Charles E. Campbell {{{1
Bram Moolenaara5792f52005-11-23 21:25:05 +000012" Permission is hereby granted to use and distribute this code,
13" with or without modifications, provided that this copyright
14" notice is copied with it. Like anything else that's free,
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015" tar.vim and tarPlugin.vim are provided *as is* and comes
16" with no warranty of any kind, either expressed or implied.
17" By using this plugin, you agree that in no event will the
18" copyright holder be liable for any damages resulting from
19" the use of this software.
Bram Moolenaar5c736222010-01-06 20:54:52 +010020" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +000021" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000022" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010023if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000024 finish
25endif
Christian Brabandt3d372312023-11-05 17:44:05 +010026let g:loaded_tar= "v32a"
Bram Moolenaar5c736222010-01-06 20:54:52 +010027if v:version < 702
28 echohl WarningMsg
29 echo "***warning*** this version of tar needs vim 7.2"
30 echohl Normal
31 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000032endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010033let s:keepcpo= &cpo
34set cpo&vim
Bram Moolenaarff034192013-04-24 18:51:19 +020035"DechoTabOn
Bram Moolenaar5c736222010-01-06 20:54:52 +010036"call Decho("loading autoload/tar.vim")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000037
Bram Moolenaara5792f52005-11-23 21:25:05 +000038" ---------------------------------------------------------------------
39" Default Settings: {{{1
40if !exists("g:tar_browseoptions")
41 let g:tar_browseoptions= "Ptf"
42endif
43if !exists("g:tar_readoptions")
44 let g:tar_readoptions= "OPxf"
45endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000046if !exists("g:tar_cmd")
47 let g:tar_cmd= "tar"
48endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000049if !exists("g:tar_writeoptions")
50 let g:tar_writeoptions= "uf"
51endif
Bram Moolenaar29634562020-01-09 21:46:04 +010052if !exists("g:tar_delfile")
53 let g:tar_delfile="--delete -f"
54endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020055if !exists("g:netrw_cygwin")
56 if has("win32") || has("win95") || has("win64") || has("win16")
57 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
58 let g:netrw_cygwin= 1
59 else
60 let g:netrw_cygwin= 0
61 endif
62 else
63 let g:netrw_cygwin= 0
64 endif
65endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010066if !exists("g:tar_copycmd")
67 if !exists("g:netrw_localcopycmd")
68 if has("win32") || has("win95") || has("win64") || has("win16")
69 if g:netrw_cygwin
70 let g:netrw_localcopycmd= "cp"
71 else
72 let g:netrw_localcopycmd= "copy"
73 endif
74 elseif has("unix") || has("macunix")
75 let g:netrw_localcopycmd= "cp"
76 else
77 let g:netrw_localcopycmd= ""
78 endif
79 endif
80 let g:tar_copycmd= g:netrw_localcopycmd
81endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010082if !exists("g:tar_extractcmd")
83 let g:tar_extractcmd= "tar -xf"
84endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000085
Bram Moolenaar8c8de832008-06-24 22:58:06 +000086" set up shell quoting character
87if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020088 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000089 let g:tar_shq= &shq
90 elseif has("win32") || has("win95") || has("win64") || has("win16")
91 if exists("g:netrw_cygwin") && g:netrw_cygwin
92 let g:tar_shq= "'"
93 else
94 let g:tar_shq= '"'
95 endif
96 else
97 let g:tar_shq= "'"
98 endif
99" call Decho("g:tar_shq<".g:tar_shq.">")
100endif
101
Bram Moolenaara5792f52005-11-23 21:25:05 +0000102" ----------------
103" Functions: {{{1
104" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000105
Bram Moolenaara5792f52005-11-23 21:25:05 +0000106" ---------------------------------------------------------------------
107" tar#Browse: {{{2
108fun! tar#Browse(tarfile)
109" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000110 let repkeep= &report
111 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000112
Bram Moolenaara5792f52005-11-23 21:25:05 +0000113 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000114 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000115 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100116" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000117 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000118 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000119" call Dret("tar#Browse")
120 return
121 endif
122 if !filereadable(a:tarfile)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000123" call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000124 if a:tarfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100125 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000126 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100127" call Decho("***error*** (tar#Browse) File not readable<".a:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000128 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000129 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000130 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000131" call Dret("tar#Browse : file<".a:tarfile."> not readable")
132 return
133 endif
134 if &ma != 1
135 set ma
136 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200137 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000138
Bram Moolenaara5792f52005-11-23 21:25:05 +0000139 setlocal noswapfile
140 setlocal buftype=nofile
141 setlocal bufhidden=hide
142 setlocal nobuflisted
143 setlocal nowrap
144 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000145
Bram Moolenaara5792f52005-11-23 21:25:05 +0000146 " give header
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000147" call Decho("printing header")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000148 let lastline= line("$")
149 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
150 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
151 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200152 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100153 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200154 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000155
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000156 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200157 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000158 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100159 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000160 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000161 let curlast= line("$")
Bram Moolenaar29634562020-01-09 21:46:04 +0100162
163 if tarfile =~# '\.\(gz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100164" 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 +0100165 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
166
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100167 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' || tarfile =~# '\.\(tzst\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100168 if has("unix") && executable("file")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100169 let filekind= system("file ".shellescape(tarfile,1))
Bram Moolenaar29634562020-01-09 21:46:04 +0100170 else
171 let filekind= ""
172 endif
173
174 if filekind =~ "bzip2"
175 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
176 elseif filekind =~ "XZ"
177 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100178 elseif filekind =~ "Zstandard"
179 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100180 else
181 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
182 endif
183
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000184 elseif tarfile =~# '\.lrp'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100185" 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 +0200186 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100187 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100188" 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 +0200189 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100190 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100191" 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 +0200192 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200193 elseif tarfile =~# '\.\(xz\|txz\)$'
194" 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 +0200195 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100196 elseif tarfile =~# '\.\(zst\|tzst\)$'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100197 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000198 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000199 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100200 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000201 let tarfile = substitute(tarfile, '-', './-', '')
202 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100203" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
Bram Moolenaar251e1912011-06-19 05:09:16 +0200204 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000205 endif
206 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000207 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000208 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000209" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000210 return
211 endif
Christian Brabandt3d372312023-11-05 17:44:05 +0100212 "
Christian Brabandt67abf152023-11-14 17:15:17 +0100213 " The following should not be neccessary, since in case of errors the
214 " previous if statement should have caught the problem (because tar exited
215 " with a non-zero exit code).
216 " if line("$") == curlast || ( line("$") == (curlast + 1) &&
217 " \ getline("$") =~# '\c\<\%(warning\|error\|inappropriate\|unrecognized\)\>' &&
218 " \ getline("$") =~ '\s' )
219 " redraw!
220 " echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
221 " keepj sil! %d
222 " let eikeep= &ei
223 " set ei=BufReadCmd,FileReadCmd
224 " exe "r ".fnameescape(a:tarfile)
225 " let &ei= eikeep
226 " keepj sil! 1d
227 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
228 " return
229 " endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000230
Bram Moolenaar29634562020-01-09 21:46:04 +0100231 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000232 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100233 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100234 noremap <silent> <buffer> x :call tar#Extract()<cr>
Bram Moolenaar29634562020-01-09 21:46:04 +0100235 if &mouse != ""
236 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
237 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000238
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000239 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200240" call Dret("tar#Browse : b:tarfile<".b:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000241endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000242
Bram Moolenaarab194812005-09-14 21:40:12 +0000243" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000244" TarBrowseSelect: {{{2
245fun! s:TarBrowseSelect()
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200246" call Dfunc("TarBrowseSelect() b:tarfile<".b:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000247 let repkeep= &report
248 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000249 let fname= getline(".")
250" call Decho("fname<".fname.">")
251
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000252 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
253 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100254 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000255" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
256 return
257 endif
258
Bram Moolenaara5792f52005-11-23 21:25:05 +0000259 " sanity check
260 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000261 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000262" call Dret("TarBrowseSelect")
263 return
264 endif
265
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200266 " about to make a new window, need to use b:tarfile
267 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000268 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200269 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000270 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100271 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000272 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000273
Bram Moolenaar29634562020-01-09 21:46:04 +0100274 " open a new window (tar#Read will read a file into it)
275 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000276 if !exists("g:tar_nomax") || g:tar_nomax == 0
277 wincmd _
278 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000279 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000280 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000281 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200282 set nomod
283 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000284
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000285 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000286" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
287endfun
288
289" ---------------------------------------------------------------------
290" tar#Read: {{{2
291fun! tar#Read(fname,mode)
292" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000293 let repkeep= &report
294 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000295 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
296 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200297 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000298 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100299 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000300 endif
301" call Decho("tarfile<".tarfile.">")
302" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000303
Bram Moolenaar5c736222010-01-06 20:54:52 +0100304 if fname =~ '\.bz2$' && executable("bzcat")
305 let decmp= "|bzcat"
306 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100307 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000308 let decmp= "|zcat"
309 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100310 elseif fname =~ '\.lzma$' && executable("lzcat")
311 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000312 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200313 elseif fname =~ '\.xz$' && executable("xzcat")
314 let decmp= "|xzcat"
315 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100316 elseif fname =~ '\.zst$' && executable("zstdcat")
317 let decmp= "|zstdcat"
318 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000319 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000320 let decmp=""
321 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200322 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000323 setlocal bin
324 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000325 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000326
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000327 if exists("g:tar_secure")
328 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000329 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000330 let tar_secure= " "
331 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200332
Bram Moolenaar5c736222010-01-06 20:54:52 +0100333 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200334 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 +0100335 elseif tarfile =~# '\.\(gz\)$'
336 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
337
338 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
339 if has("unix") && executable("file")
340 let filekind= system("file ".shellescape(tarfile,1))
341 else
342 let filekind= ""
343 endif
344 if filekind =~ "bzip2"
345 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
346 elseif filekind =~ "XZ"
347 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 +0100348 elseif filekind =~ "Zstandard"
349 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 +0100350 else
351 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
352 endif
353
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000354 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200355 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 +0100356 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200357 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 +0200358 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200359 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 +0000360 else
361 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100362 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000363 let tarfile = substitute(tarfile, '-', './-', '')
364 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100365" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
366 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 +0000367 endif
368
369 if doro
370 " because the reverse process of compressing changed files back into the tarball is not currently supported
371 setlocal ro
372 endif
373
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200374 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000375 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000376
377 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200378 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000379 set nomod
380
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000381 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200382" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000383endfun
384
385" ---------------------------------------------------------------------
386" tar#Write: {{{2
387fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200388" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000389 let repkeep= &report
390 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000391
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000392 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
393 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100394 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000395" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
396 return
397 endif
398
Bram Moolenaarab194812005-09-14 21:40:12 +0000399 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000401 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100402" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000403 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000404" call Dret("tar#Write")
405 return
406 endif
407 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000408 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100409" call Decho("***error*** (tar#Write) sorry, mkdir() doesn't work on your system")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000410 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000411 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000412" call Dret("tar#Write")
413 return
414 endif
415
416 let curdir= getcwd()
417 let tmpdir= tempname()
418" call Decho("orig tempname<".tmpdir.">")
419 if tmpdir =~ '\.'
420 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
421 endif
422" call Decho("tmpdir<".tmpdir.">")
423 call mkdir(tmpdir,"p")
424
425 " attempt to change to the indicated directory
426 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000427 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000428 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000429 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100430" call Decho("***error*** (tar#Write) cannot cd to temporary directory")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000431 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000432 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000433" call Dret("tar#Write")
434 return
435 endtry
436" call Decho("current directory now: ".getcwd())
437
Bram Moolenaara5792f52005-11-23 21:25:05 +0000438 " place temporary files under .../_ZIPVIM_/
439 if isdirectory("_ZIPVIM_")
440 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000441 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000442 call mkdir("_ZIPVIM_")
443 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000444" call Decho("current directory now: ".getcwd())
445
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200446 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
447 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000448
449 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100450 if tarfile =~# '\.bz2'
451 call system("bzip2 -d -- ".shellescape(tarfile,0))
452 let tarfile = substitute(tarfile,'\.bz2','','e')
453 let compress= "bzip2 -- ".shellescape(tarfile,0)
454" call Decho("compress<".compress.">")
455 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100456 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000457 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100458 let compress= "gzip -- ".shellescape(tarfile,0)
459" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000460 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100461 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000462 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100463 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000464 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000465" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200466 elseif tarfile =~# '\.xz'
467 call system("xz -d -- ".shellescape(tarfile,0))
468 let tarfile = substitute(tarfile,'\.xz','','e')
469 let compress= "xz -- ".shellescape(tarfile,0)
470" call Decho("compress<".compress.">")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100471 elseif tarfile =~# '\.zst'
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100472 call system("zstd --decompress --rm -- ".shellescape(tarfile,0))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100473 let tarfile = substitute(tarfile,'\.zst','','e')
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100474 let compress= "zstd --rm -- ".shellescape(tarfile,0)
Bram Moolenaar477db062010-07-28 18:17:41 +0200475 elseif tarfile =~# '\.lzma'
476 call system("lzma -d -- ".shellescape(tarfile,0))
477 let tarfile = substitute(tarfile,'\.lzma','','e')
478 let compress= "lzma -- ".shellescape(tarfile,0)
479" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000480 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000482
Bram Moolenaarab194812005-09-14 21:40:12 +0000483 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000484 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100485" call Decho("***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000486 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000487 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000488
489" call Decho("tarfile<".tarfile."> fname<".fname.">")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100490
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000491 if fname =~ '/'
492 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200493 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100494 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000495 endif
496 call mkdir(dirpath,"p")
497 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000498 if tarfile !~ '/'
499 let tarfile= curdir.'/'.tarfile
500 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000501 if tarfile =~ '^\s*-'
502 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
503 let tarfile = substitute(tarfile, '-', './-', '')
504 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000505" call Decho("tarfile<".tarfile."> fname<".fname.">")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100506
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000507 if exists("g:tar_secure")
508 let tar_secure= " -- "
509 else
510 let tar_secure= " "
511 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200513 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100514 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000515 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100516
Bram Moolenaara5792f52005-11-23 21:25:05 +0000517 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100518" call Decho("system(".g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
519 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000520 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000521 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100522" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000524 else
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100525
526 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100527" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
528 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000529 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000530 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100531" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000533 elseif exists("compress")
534" call Decho("call system(".compress.")")
535 call system(compress)
536 if exists("tgz")
537" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
538 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
539 endif
540 endif
541 endif
542
543 " support writing tarfiles across a network
544 if s:tblfile_{winnr()} =~ '^\a\+://'
545" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
546 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100547 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100548 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000549 let eikeep = &ei
550 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100551 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000552 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100553 let &ei = eikeep
554 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000555 q!
556 unlet s:tblfile_{winnr()}
557 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000558 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100559
Bram Moolenaarab194812005-09-14 21:40:12 +0000560 " cleanup and restore current directory
561 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000562 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000563 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000564 setlocal nomod
565
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000566 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000567" call Dret("tar#Write")
568endfun
569
570" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200571" tar#Diff: {{{2
572fun! tar#Diff(userfname,fname)
573" call Dfunc("tar#Diff(userfname<".a:userfname."> fname<".a:fname.")")
574 let fname= a:fname
575 if a:userfname != ""
576 let fname= a:userfname
577 endif
578 if filereadable(fname)
579 " sets current file (from tarball) for diff'ing
580 " splits window vertically
581 " opens original file, sets it for diff'ing
582 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
583 diffthis
584 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100585 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200586 diffthis
587 else
588 redraw!
589 echo "***warning*** unable to read file<".fname.">"
590 endif
591" call Dret("tar#Diff")
592endfun
593
594" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100595" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
596fun! tar#Extract()
597" call Dfunc("tar#Extract()")
598
599 let repkeep= &report
600 set report=10
601 let fname= getline(".")
602" call Decho("fname<".fname.">")
603
604 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
605 redraw!
606 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
607" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
608 return
609 endif
610
611 " sanity check
612 if fname =~ '^"'
613 let &report= repkeep
614" call Dret("TarBrowseSelect")
615 return
616 endif
617
618 let tarball = expand("%")
619" call Decho("tarball<".tarball.">")
620 let tarbase = substitute(tarball,'\..*$','','')
621" call Decho("tarbase<".tarbase.">")
622
623 let extractcmd= netrw#WinPath(g:tar_extractcmd)
624 if filereadable(tarbase.".tar")
625" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname).")")
626 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
627 if v:shell_error != 0
628 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
629" call Decho("***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!")
630 else
631 echo "***note*** successfully extracted ".fname
632 endif
633
634 elseif filereadable(tarbase.".tgz")
635 let extractcmd= substitute(extractcmd,"-","-z","")
636" call Decho("system(".extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname).")")
637 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
638 if v:shell_error != 0
639 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
640" call Decho("***error*** ".extractcmd."t ".tarbase.".tgz ".fname.": failed!")
641 else
642 echo "***note*** successfully extracted ".fname
643 endif
644
645 elseif filereadable(tarbase.".tar.gz")
646 let extractcmd= substitute(extractcmd,"-","-z","")
647" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname).")")
648 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
649 if v:shell_error != 0
650 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
651" call Decho("***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!")
652 else
653 echo "***note*** successfully extracted ".fname
654 endif
655
656 elseif filereadable(tarbase.".tbz")
657 let extractcmd= substitute(extractcmd,"-","-j","")
658" call Decho("system(".extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname).")")
659 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
660 if v:shell_error != 0
661 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
662" call Decho("***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!")
663 else
664 echo "***note*** successfully extracted ".fname
665 endif
666
667 elseif filereadable(tarbase.".tar.bz2")
668 let extractcmd= substitute(extractcmd,"-","-j","")
669" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname).")")
670 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
671 if v:shell_error != 0
672 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
673" call Decho("***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!")
674 else
675 echo "***note*** successfully extracted ".fname
676 endif
677
678 elseif filereadable(tarbase.".txz")
679 let extractcmd= substitute(extractcmd,"-","-J","")
680" call Decho("system(".extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname).")")
681 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
682 if v:shell_error != 0
683 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
684" call Decho("***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!")
685 else
686 echo "***note*** successfully extracted ".fname
687 endif
688
689 elseif filereadable(tarbase.".tar.xz")
690 let extractcmd= substitute(extractcmd,"-","-J","")
691" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname).")")
692 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
693 if v:shell_error != 0
694 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
695" call Decho("***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!")
696 else
697 echo "***note*** successfully extracted ".fname
698 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100699
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100700 elseif filereadable(tarbase.".tzst")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100701 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100702" call Decho("system(".extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname).")")
703 call system(extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100704 if v:shell_error != 0
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100705 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!" | echohl NONE
706" call Decho("***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100707 else
708 echo "***note*** successfully extracted ".fname
709 endif
710
711 elseif filereadable(tarbase.".tar.zst")
712 let extractcmd= substitute(extractcmd,"-","--zstd","")
713" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname).")")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100714 call system(extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100715 if v:shell_error != 0
716 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
717" call Decho("***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!")
718 else
719 echo "***note*** successfully extracted ".fname
720 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100721 endif
722
723 " restore option
724 let &report= repkeep
725
726" call Dret("tar#Extract")
727endfun
728
729" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100730" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000731fun! s:Rmdir(fname)
732" call Dfunc("Rmdir(fname<".a:fname.">)")
733 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100734 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000735 elseif has("win32") || has("win95") || has("win64") || has("win16")
736 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100737 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000738 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100739 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000740 endif
741 endif
742" call Dret("Rmdir")
743endfun
744
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000745" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100746" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
747fun! tar#Vimuntar(...)
748" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
749 let tarball = expand("%")
750" call Decho("tarball<".tarball.">")
751 let tarbase = substitute(tarball,'\..*$','','')
752" call Decho("tarbase<".tarbase.">")
753 let tarhome = expand("%:p")
754 if has("win32") || has("win95") || has("win64") || has("win16")
755 let tarhome= substitute(tarhome,'\\','/','g')
756 endif
757 let tarhome= substitute(tarhome,'/[^/]*$','','')
758" call Decho("tarhome<".tarhome.">")
759 let tartail = expand("%:t")
760" call Decho("tartail<".tartail.">")
761 let curdir = getcwd()
762" call Decho("curdir <".curdir.">")
763 " set up vimhome
764 if a:0 > 0 && a:1 != ""
765 let vimhome= a:1
766 else
767 let vimhome= vimball#VimballHome()
768 endif
769" call Decho("vimhome<".vimhome.">")
770
771" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
772 if simplify(curdir) != simplify(vimhome)
773 " copy (possibly compressed) tarball to .vim/vimfiles
774" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
775 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
776" call Decho("exe cd ".fnameescape(vimhome))
777 exe "cd ".fnameescape(vimhome)
778 endif
779" call Decho("getcwd<".getcwd().">")
780
781 " if necessary, decompress the tarball; then, extract it
782 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100783 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100784 silent exe "!gunzip ".shellescape(tartail)
785 elseif executable("gzip")
786 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000787 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200788 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100789 if simplify(curdir) != simplify(tarhome)
790 " remove decompressed tarball, restore directory
791" call Decho("delete(".tartail.".tar)")
792 call delete(tartail.".tar")
793" call Decho("exe cd ".fnameescape(curdir))
794 exe "cd ".fnameescape(curdir)
795 endif
796" call Dret("tar#Vimuntar")
797 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000798 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100800 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100802 let extractcmd= netrw#WinPath(g:tar_extractcmd)
803" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
804 call system(extractcmd." ".shellescape(tarbase.".tar"))
805
806 " set up help
807 if filereadable("doc/".tarbase.".txt")
808" call Decho("exe helptags ".getcwd()."/doc")
809 exe "helptags ".getcwd()."/doc"
810 endif
811
812 if simplify(tarhome) != simplify(vimhome)
813 " remove decompressed tarball, restore directory
814 call delete(vimhome."/".tarbase.".tar")
815 exe "cd ".fnameescape(curdir)
816 endif
817
818" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000819endfun
820
Bram Moolenaar5c736222010-01-06 20:54:52 +0100821" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000822" Modelines And Restoration: {{{1
823let &cpo= s:keepcpo
824unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000825" vim:ts=8 fdm=marker