blob: 436e24eb67496d7eefd674d346bac3ca48315776 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Lennart00129a8442024-11-11 22:39:30 +01003" Date: Nov 11, 2024
Christian Brabandt67abf152023-11-14 17:15:17 +01004" 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
Lennart00129a8442024-11-11 22:39:30 +010026let g:loaded_tar= "v32b"
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")
Lennart00129a8442024-11-11 22:39:30 +010044 let g:tar_readoptions= "pPxf"
Bram Moolenaara5792f52005-11-23 21:25:05 +000045endif
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")
Lennart00129a8442024-11-11 22:39:30 +010083 let g:tar_extractcmd= "tar -pxf"
Bram Moolenaar5c736222010-01-06 20:54:52 +010084endif
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','')
Lennart00129a8442024-11-11 22:39:30 +0100297
298" changing the directory to the temporary earlier to allow tar to extract the file with permissions intact
299 if !exists("*mkdir")
300 redraw!
301 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
302 let &report= repkeep
303 return
304 endif
305
306 let curdir= getcwd()
307 let tmpdir= tempname()
308 let b:curdir= tmpdir
309 let b:tmpdir= curdir
310 if tmpdir =~ '\.'
311 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
312 endif
313 call mkdir(tmpdir,"p")
314
315 " attempt to change to the indicated directory
316 try
317 exe "cd ".fnameescape(tmpdir)
318 catch /^Vim\%((\a\+)\)\=:E344/
319 redraw!
320 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
321 let &report= repkeep
322 return
323 endtry
324
325 " place temporary files under .../_ZIPVIM_/
326 if isdirectory("_ZIPVIM_")
327 call s:Rmdir("_ZIPVIM_")
328 endif
329 call mkdir("_ZIPVIM_")
330 cd _ZIPVIM_
331
Bram Moolenaarff034192013-04-24 18:51:19 +0200332 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000333 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100334 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000335 endif
336" call Decho("tarfile<".tarfile.">")
337" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000338
Bram Moolenaar5c736222010-01-06 20:54:52 +0100339 if fname =~ '\.bz2$' && executable("bzcat")
340 let decmp= "|bzcat"
341 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100342 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000343 let decmp= "|zcat"
344 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100345 elseif fname =~ '\.lzma$' && executable("lzcat")
346 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000347 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200348 elseif fname =~ '\.xz$' && executable("xzcat")
349 let decmp= "|xzcat"
350 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100351 elseif fname =~ '\.zst$' && executable("zstdcat")
352 let decmp= "|zstdcat"
353 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000354 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355 let decmp=""
356 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200357 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 setlocal bin
359 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000360 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000362 if exists("g:tar_secure")
363 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000365 let tar_secure= " "
366 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200367
Bram Moolenaar5c736222010-01-06 20:54:52 +0100368 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200369 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100370 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100371 elseif tarfile =~# '\.\(gz\)$'
372 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100373 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100374 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
375 if has("unix") && executable("file")
376 let filekind= system("file ".shellescape(tarfile,1))
377 else
378 let filekind= ""
379 endif
380 if filekind =~ "bzip2"
381 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100382 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100383 elseif filekind =~ "XZ"
384 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100385 exe "read ".fname
Bram Moolenaar23515b42020-11-29 14:36:24 +0100386 elseif filekind =~ "Zstandard"
387 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100388 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100389 else
390 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100391 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100392 endif
393
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000394 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200395 exe "sil! r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100396 exe "read ".fname
Bram Moolenaar5c736222010-01-06 20:54:52 +0100397 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200398 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100399 exe "read ".fname
Bram Moolenaar477db062010-07-28 18:17:41 +0200400 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200401 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100402 exe "read ".fname
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000403 else
404 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100405 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000406 let tarfile = substitute(tarfile, '-', './-', '')
407 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100408" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
409 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100410 exe "read ".fname
411 endif
412
413 redraw!
414
415if v:shell_error != 0
416 cd ..
417 call s:Rmdir("_ZIPVIM_")
418 exe "cd ".fnameescape(curdir)
419 echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000420 endif
421
422 if doro
423 " because the reverse process of compressing changed files back into the tarball is not currently supported
424 setlocal ro
425 endif
426
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200427 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000428 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000429
430 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200431 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000432 set nomod
433
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000434 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200435" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000436endfun
437
438" ---------------------------------------------------------------------
439" tar#Write: {{{2
440fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200441" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000442 let repkeep= &report
443 set report=10
Lennart00129a8442024-11-11 22:39:30 +0100444
445 " temporary buffer variable workaround because too fucking tired. but it works now
446 let curdir= b:curdir
447 let tmpdir= b:tmpdir
Bram Moolenaara5792f52005-11-23 21:25:05 +0000448
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000449 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
450 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100451 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000452" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
453 return
454 endif
455
Bram Moolenaarab194812005-09-14 21:40:12 +0000456 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000457 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000458 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100459" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000460 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000461" call Dret("tar#Write")
462 return
463 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000464
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200465 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
466 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000467
468 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100469 if tarfile =~# '\.bz2'
470 call system("bzip2 -d -- ".shellescape(tarfile,0))
471 let tarfile = substitute(tarfile,'\.bz2','','e')
472 let compress= "bzip2 -- ".shellescape(tarfile,0)
473" call Decho("compress<".compress.">")
474 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100475 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000476 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100477 let compress= "gzip -- ".shellescape(tarfile,0)
478" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000479 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100480 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000481 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100482 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000483 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000484" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200485 elseif tarfile =~# '\.xz'
486 call system("xz -d -- ".shellescape(tarfile,0))
487 let tarfile = substitute(tarfile,'\.xz','','e')
488 let compress= "xz -- ".shellescape(tarfile,0)
489" call Decho("compress<".compress.">")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100490 elseif tarfile =~# '\.zst'
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100491 call system("zstd --decompress --rm -- ".shellescape(tarfile,0))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100492 let tarfile = substitute(tarfile,'\.zst','','e')
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100493 let compress= "zstd --rm -- ".shellescape(tarfile,0)
Bram Moolenaar477db062010-07-28 18:17:41 +0200494 elseif tarfile =~# '\.lzma'
495 call system("lzma -d -- ".shellescape(tarfile,0))
496 let tarfile = substitute(tarfile,'\.lzma','','e')
497 let compress= "lzma -- ".shellescape(tarfile,0)
498" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000499 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000501
Bram Moolenaarab194812005-09-14 21:40:12 +0000502 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000503 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100504" call Decho("***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000505 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000506 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000507
508" call Decho("tarfile<".tarfile."> fname<".fname.">")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100509
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000510 if fname =~ '/'
511 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200512 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100513 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000514 endif
515 call mkdir(dirpath,"p")
516 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000517 if tarfile !~ '/'
518 let tarfile= curdir.'/'.tarfile
519 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000520 if tarfile =~ '^\s*-'
521 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
522 let tarfile = substitute(tarfile, '-', './-', '')
523 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000524" call Decho("tarfile<".tarfile."> fname<".fname.">")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100525
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000526 if exists("g:tar_secure")
527 let tar_secure= " -- "
528 else
529 let tar_secure= " "
530 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200532 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100533 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000534 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100535
Bram Moolenaara5792f52005-11-23 21:25:05 +0000536 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100537" call Decho("system(".g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
538 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000539 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000540 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100541" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000543 else
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100544
545 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100546" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
547 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000548 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000549 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100550" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000551 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000552 elseif exists("compress")
553" call Decho("call system(".compress.")")
554 call system(compress)
555 if exists("tgz")
556" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
557 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
558 endif
559 endif
560 endif
561
562 " support writing tarfiles across a network
563 if s:tblfile_{winnr()} =~ '^\a\+://'
564" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
565 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100566 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100567 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000568 let eikeep = &ei
569 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100570 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000571 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100572 let &ei = eikeep
573 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000574 q!
575 unlet s:tblfile_{winnr()}
576 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000577 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100578
Bram Moolenaarab194812005-09-14 21:40:12 +0000579 " cleanup and restore current directory
580 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000581 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000582 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000583 setlocal nomod
584
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000585 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000586" call Dret("tar#Write")
587endfun
588
589" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200590" tar#Diff: {{{2
591fun! tar#Diff(userfname,fname)
592" call Dfunc("tar#Diff(userfname<".a:userfname."> fname<".a:fname.")")
593 let fname= a:fname
594 if a:userfname != ""
595 let fname= a:userfname
596 endif
597 if filereadable(fname)
598 " sets current file (from tarball) for diff'ing
599 " splits window vertically
600 " opens original file, sets it for diff'ing
601 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
602 diffthis
603 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100604 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200605 diffthis
606 else
607 redraw!
608 echo "***warning*** unable to read file<".fname.">"
609 endif
610" call Dret("tar#Diff")
611endfun
612
613" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100614" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
615fun! tar#Extract()
616" call Dfunc("tar#Extract()")
617
618 let repkeep= &report
619 set report=10
620 let fname= getline(".")
621" call Decho("fname<".fname.">")
622
623 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
624 redraw!
625 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
626" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
627 return
628 endif
629
630 " sanity check
631 if fname =~ '^"'
632 let &report= repkeep
633" call Dret("TarBrowseSelect")
634 return
635 endif
636
637 let tarball = expand("%")
638" call Decho("tarball<".tarball.">")
639 let tarbase = substitute(tarball,'\..*$','','')
640" call Decho("tarbase<".tarbase.">")
641
642 let extractcmd= netrw#WinPath(g:tar_extractcmd)
643 if filereadable(tarbase.".tar")
644" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname).")")
645 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
646 if v:shell_error != 0
647 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
648" call Decho("***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!")
649 else
650 echo "***note*** successfully extracted ".fname
651 endif
652
653 elseif filereadable(tarbase.".tgz")
654 let extractcmd= substitute(extractcmd,"-","-z","")
655" call Decho("system(".extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname).")")
656 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
657 if v:shell_error != 0
658 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
659" call Decho("***error*** ".extractcmd."t ".tarbase.".tgz ".fname.": failed!")
660 else
661 echo "***note*** successfully extracted ".fname
662 endif
663
664 elseif filereadable(tarbase.".tar.gz")
665 let extractcmd= substitute(extractcmd,"-","-z","")
666" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname).")")
667 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
668 if v:shell_error != 0
669 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
670" call Decho("***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!")
671 else
672 echo "***note*** successfully extracted ".fname
673 endif
674
675 elseif filereadable(tarbase.".tbz")
676 let extractcmd= substitute(extractcmd,"-","-j","")
677" call Decho("system(".extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname).")")
678 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
679 if v:shell_error != 0
680 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
681" call Decho("***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!")
682 else
683 echo "***note*** successfully extracted ".fname
684 endif
685
686 elseif filereadable(tarbase.".tar.bz2")
687 let extractcmd= substitute(extractcmd,"-","-j","")
688" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname).")")
689 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
690 if v:shell_error != 0
691 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
692" call Decho("***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!")
693 else
694 echo "***note*** successfully extracted ".fname
695 endif
696
697 elseif filereadable(tarbase.".txz")
698 let extractcmd= substitute(extractcmd,"-","-J","")
699" call Decho("system(".extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname).")")
700 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
701 if v:shell_error != 0
702 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
703" call Decho("***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!")
704 else
705 echo "***note*** successfully extracted ".fname
706 endif
707
708 elseif filereadable(tarbase.".tar.xz")
709 let extractcmd= substitute(extractcmd,"-","-J","")
710" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname).")")
711 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
712 if v:shell_error != 0
713 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
714" call Decho("***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!")
715 else
716 echo "***note*** successfully extracted ".fname
717 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100718
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100719 elseif filereadable(tarbase.".tzst")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100720 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100721" call Decho("system(".extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname).")")
722 call system(extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100723 if v:shell_error != 0
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100724 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!" | echohl NONE
725" call Decho("***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100726 else
727 echo "***note*** successfully extracted ".fname
728 endif
729
730 elseif filereadable(tarbase.".tar.zst")
731 let extractcmd= substitute(extractcmd,"-","--zstd","")
732" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname).")")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100733 call system(extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100734 if v:shell_error != 0
735 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
736" call Decho("***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!")
737 else
738 echo "***note*** successfully extracted ".fname
739 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100740 endif
741
742 " restore option
743 let &report= repkeep
744
745" call Dret("tar#Extract")
746endfun
747
748" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100749" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000750fun! s:Rmdir(fname)
751" call Dfunc("Rmdir(fname<".a:fname.">)")
752 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100753 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000754 elseif has("win32") || has("win95") || has("win64") || has("win16")
755 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100756 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000757 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100758 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000759 endif
760 endif
761" call Dret("Rmdir")
762endfun
763
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100765" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
766fun! tar#Vimuntar(...)
767" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
768 let tarball = expand("%")
769" call Decho("tarball<".tarball.">")
770 let tarbase = substitute(tarball,'\..*$','','')
771" call Decho("tarbase<".tarbase.">")
772 let tarhome = expand("%:p")
773 if has("win32") || has("win95") || has("win64") || has("win16")
774 let tarhome= substitute(tarhome,'\\','/','g')
775 endif
776 let tarhome= substitute(tarhome,'/[^/]*$','','')
777" call Decho("tarhome<".tarhome.">")
778 let tartail = expand("%:t")
779" call Decho("tartail<".tartail.">")
780 let curdir = getcwd()
781" call Decho("curdir <".curdir.">")
782 " set up vimhome
783 if a:0 > 0 && a:1 != ""
784 let vimhome= a:1
785 else
786 let vimhome= vimball#VimballHome()
787 endif
788" call Decho("vimhome<".vimhome.">")
789
790" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
791 if simplify(curdir) != simplify(vimhome)
792 " copy (possibly compressed) tarball to .vim/vimfiles
793" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
794 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
795" call Decho("exe cd ".fnameescape(vimhome))
796 exe "cd ".fnameescape(vimhome)
797 endif
798" call Decho("getcwd<".getcwd().">")
799
800 " if necessary, decompress the tarball; then, extract it
801 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100802 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100803 silent exe "!gunzip ".shellescape(tartail)
804 elseif executable("gzip")
805 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000806 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200807 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100808 if simplify(curdir) != simplify(tarhome)
809 " remove decompressed tarball, restore directory
810" call Decho("delete(".tartail.".tar)")
811 call delete(tartail.".tar")
812" call Decho("exe cd ".fnameescape(curdir))
813 exe "cd ".fnameescape(curdir)
814 endif
815" call Dret("tar#Vimuntar")
816 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000817 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000818 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100819 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000820 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100821 let extractcmd= netrw#WinPath(g:tar_extractcmd)
822" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
823 call system(extractcmd." ".shellescape(tarbase.".tar"))
824
825 " set up help
826 if filereadable("doc/".tarbase.".txt")
827" call Decho("exe helptags ".getcwd()."/doc")
828 exe "helptags ".getcwd()."/doc"
829 endif
830
831 if simplify(tarhome) != simplify(vimhome)
832 " remove decompressed tarball, restore directory
833 call delete(vimhome."/".tarbase.".tar")
834 exe "cd ".fnameescape(curdir)
835 endif
836
837" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000838endfun
839
Bram Moolenaar5c736222010-01-06 20:54:52 +0100840" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000841" Modelines And Restoration: {{{1
842let &cpo= s:keepcpo
843unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000844" vim:ts=8 fdm=marker