blob: 1c2ed78c1907b4256d54839e196b4f369e6e89c8 [file] [log] [blame]
Corpulent Robinb69cd522025-02-06 21:10:49 +01001" tar.vim: Handles browsing tarfiles - AUTOLOAD PORTION
Christian Brabandt8ac975d2025-03-01 17:13:40 +01002" Date: Mar 01, 2025
Christian Brabandt67abf152023-11-14 17:15:17 +01003" Version: 32b (with modifications from the Vim Project)
Christian Brabandtf9ca1392024-02-19 20:37:11 +01004" Maintainer: This runtime file is looking for a new maintainer.
5" Former Maintainer: Charles E Campbell
Bram Moolenaar29634562020-01-09 21:46:04 +01006" License: Vim License (see vim's :help license)
Jim Zhou56957ed2025-02-28 18:06:14 +01007" Last Change:
8" 2024 Jan 08 by Vim Project: fix a few problems (#138331, #12637, #8109)
9" 2024 Feb 19 by Vim Project: announce adoption
10" 2024 Nov 11 by Vim Project: support permissions (#7379)
11" 2025 Feb 06 by Vim Project: add support for lz4 (#16591)
12" 2025 Feb 28 by Vim Project: add support for bzip3 (#16755)
Christian Brabandt8ac975d2025-03-01 17:13:40 +010013" 2025 Mar 01 by Vim Project: fix syntax error in tar#Read()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000014"
Bram Moolenaara5792f52005-11-23 21:25:05 +000015" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016"
Bram Moolenaar29634562020-01-09 21:46:04 +010017" Copyright: Copyright (C) 2005-2017 Charles E. Campbell {{{1
Bram Moolenaara5792f52005-11-23 21:25:05 +000018" Permission is hereby granted to use and distribute this code,
19" with or without modifications, provided that this copyright
20" notice is copied with it. Like anything else that's free,
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021" tar.vim and tarPlugin.vim are provided *as is* and comes
22" with no warranty of any kind, either expressed or implied.
23" By using this plugin, you agree that in no event will the
24" copyright holder be liable for any damages resulting from
25" the use of this software.
Bram Moolenaarab194812005-09-14 21:40:12 +000026" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000027" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010028if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000029 finish
30endif
Lennart00129a8442024-11-11 22:39:30 +010031let g:loaded_tar= "v32b"
Bram Moolenaar5c736222010-01-06 20:54:52 +010032if v:version < 702
33 echohl WarningMsg
34 echo "***warning*** this version of tar needs vim 7.2"
35 echohl Normal
36 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000037endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010038let s:keepcpo= &cpo
39set cpo&vim
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000040
Bram Moolenaara5792f52005-11-23 21:25:05 +000041" ---------------------------------------------------------------------
42" Default Settings: {{{1
43if !exists("g:tar_browseoptions")
44 let g:tar_browseoptions= "Ptf"
45endif
46if !exists("g:tar_readoptions")
Lennart00129a8442024-11-11 22:39:30 +010047 let g:tar_readoptions= "pPxf"
Bram Moolenaara5792f52005-11-23 21:25:05 +000048endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000049if !exists("g:tar_cmd")
50 let g:tar_cmd= "tar"
51endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000052if !exists("g:tar_writeoptions")
53 let g:tar_writeoptions= "uf"
54endif
Bram Moolenaar29634562020-01-09 21:46:04 +010055if !exists("g:tar_delfile")
56 let g:tar_delfile="--delete -f"
57endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020058if !exists("g:netrw_cygwin")
59 if has("win32") || has("win95") || has("win64") || has("win16")
60 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
61 let g:netrw_cygwin= 1
62 else
63 let g:netrw_cygwin= 0
64 endif
65 else
66 let g:netrw_cygwin= 0
67 endif
68endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010069if !exists("g:tar_copycmd")
70 if !exists("g:netrw_localcopycmd")
71 if has("win32") || has("win95") || has("win64") || has("win16")
72 if g:netrw_cygwin
73 let g:netrw_localcopycmd= "cp"
74 else
75 let g:netrw_localcopycmd= "copy"
76 endif
77 elseif has("unix") || has("macunix")
78 let g:netrw_localcopycmd= "cp"
79 else
80 let g:netrw_localcopycmd= ""
81 endif
82 endif
83 let g:tar_copycmd= g:netrw_localcopycmd
84endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010085if !exists("g:tar_extractcmd")
Lennart00129a8442024-11-11 22:39:30 +010086 let g:tar_extractcmd= "tar -pxf"
Bram Moolenaar5c736222010-01-06 20:54:52 +010087endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000088
Bram Moolenaar8c8de832008-06-24 22:58:06 +000089" set up shell quoting character
90if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020091 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000092 let g:tar_shq= &shq
93 elseif has("win32") || has("win95") || has("win64") || has("win16")
94 if exists("g:netrw_cygwin") && g:netrw_cygwin
95 let g:tar_shq= "'"
96 else
97 let g:tar_shq= '"'
98 endif
99 else
100 let g:tar_shq= "'"
101 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000102endif
103
Bram Moolenaara5792f52005-11-23 21:25:05 +0000104" ----------------
105" Functions: {{{1
106" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000107
Bram Moolenaara5792f52005-11-23 21:25:05 +0000108" ---------------------------------------------------------------------
109" tar#Browse: {{{2
110fun! tar#Browse(tarfile)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000111 let repkeep= &report
112 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000113
Bram Moolenaara5792f52005-11-23 21:25:05 +0000114 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000115 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000116 redraw!
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 return
120 endif
121 if !filereadable(a:tarfile)
122 if a:tarfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100123 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000124 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000125 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000126 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000127 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000128 return
129 endif
130 if &ma != 1
131 set ma
132 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200133 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000134
Bram Moolenaara5792f52005-11-23 21:25:05 +0000135 setlocal noswapfile
136 setlocal buftype=nofile
137 setlocal bufhidden=hide
138 setlocal nobuflisted
139 setlocal nowrap
140 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000141
Bram Moolenaara5792f52005-11-23 21:25:05 +0000142 " give header
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000143 let lastline= line("$")
144 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
145 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
146 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200147 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100148 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200149 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000150
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000151 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200152 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000153 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100154 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000155 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000156 let curlast= line("$")
Bram Moolenaar29634562020-01-09 21:46:04 +0100157
158 if tarfile =~# '\.\(gz\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100159 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
160
Corpulent Robinb69cd522025-02-06 21:10:49 +0100161 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' ||
162 \ tarfile =~# '\.\(tzst\)$' || tarfile =~# '\.\(tlz4\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100163 if has("unix") && executable("file")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100164 let filekind= system("file ".shellescape(tarfile,1))
Bram Moolenaar29634562020-01-09 21:46:04 +0100165 else
166 let filekind= ""
167 endif
168
169 if filekind =~ "bzip2"
170 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Jim Zhou56957ed2025-02-28 18:06:14 +0100171 elseif filekind =~ "bzip3"
172 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100173 elseif filekind =~ "XZ"
174 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100175 elseif filekind =~ "Zstandard"
176 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100177 elseif filekind =~ "LZ4"
178 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100179 else
180 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
181 endif
182
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 elseif tarfile =~# '\.lrp'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200184 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100185 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200186 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Jim Zhou56957ed2025-02-28 18:06:14 +0100187 elseif tarfile =~# '\.\(bz3\|tb3\)$'
188 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100189 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200190 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200191 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200192 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100193 elseif tarfile =~# '\.\(zst\|tzst\)$'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100194 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100195 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
196 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000197 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000198 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100199 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000200 let tarfile = substitute(tarfile, '-', './-', '')
201 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200202 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000203 endif
204 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000205 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000206 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000207 return
208 endif
Christian Brabandt3d372312023-11-05 17:44:05 +0100209 "
Christian Brabandt67abf152023-11-14 17:15:17 +0100210 " The following should not be neccessary, since in case of errors the
211 " previous if statement should have caught the problem (because tar exited
212 " with a non-zero exit code).
213 " if line("$") == curlast || ( line("$") == (curlast + 1) &&
214 " \ getline("$") =~# '\c\<\%(warning\|error\|inappropriate\|unrecognized\)\>' &&
215 " \ getline("$") =~ '\s' )
216 " redraw!
217 " echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
218 " keepj sil! %d
219 " let eikeep= &ei
220 " set ei=BufReadCmd,FileReadCmd
221 " exe "r ".fnameescape(a:tarfile)
222 " let &ei= eikeep
223 " keepj sil! 1d
224 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
225 " return
226 " endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000227
Bram Moolenaar29634562020-01-09 21:46:04 +0100228 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000229 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100230 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100231 noremap <silent> <buffer> x :call tar#Extract()<cr>
Bram Moolenaar29634562020-01-09 21:46:04 +0100232 if &mouse != ""
233 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
234 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000235
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000236 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000237endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000238
Bram Moolenaarab194812005-09-14 21:40:12 +0000239" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000240" TarBrowseSelect: {{{2
241fun! s:TarBrowseSelect()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000242 let repkeep= &report
243 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000244 let fname= getline(".")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000245
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000246 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
247 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100248 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000249 return
250 endif
251
Bram Moolenaara5792f52005-11-23 21:25:05 +0000252 " sanity check
253 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000254 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000255 return
256 endif
257
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200258 " about to make a new window, need to use b:tarfile
259 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000260 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200261 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000262 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100263 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000264 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000265
Bram Moolenaar29634562020-01-09 21:46:04 +0100266 " open a new window (tar#Read will read a file into it)
267 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000268 if !exists("g:tar_nomax") || g:tar_nomax == 0
269 wincmd _
270 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000271 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000272 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000273 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200274 set nomod
275 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000276
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000277 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000278endfun
279
280" ---------------------------------------------------------------------
281" tar#Read: {{{2
282fun! tar#Read(fname,mode)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000283 let repkeep= &report
284 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000285 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
286 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Lennart00129a8442024-11-11 22:39:30 +0100287
Corpulent Robinb69cd522025-02-06 21:10:49 +0100288 " changing the directory to the temporary earlier to allow tar to extract the file with permissions intact
Lennart00129a8442024-11-11 22:39:30 +0100289 if !exists("*mkdir")
290 redraw!
291 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
292 let &report= repkeep
293 return
294 endif
295
296 let curdir= getcwd()
297 let tmpdir= tempname()
298 let b:curdir= tmpdir
299 let b:tmpdir= curdir
300 if tmpdir =~ '\.'
301 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
302 endif
303 call mkdir(tmpdir,"p")
304
305 " attempt to change to the indicated directory
306 try
307 exe "cd ".fnameescape(tmpdir)
308 catch /^Vim\%((\a\+)\)\=:E344/
309 redraw!
310 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
311 let &report= repkeep
312 return
313 endtry
314
315 " place temporary files under .../_ZIPVIM_/
316 if isdirectory("_ZIPVIM_")
317 call s:Rmdir("_ZIPVIM_")
318 endif
319 call mkdir("_ZIPVIM_")
320 cd _ZIPVIM_
321
Bram Moolenaarff034192013-04-24 18:51:19 +0200322 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000323 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100324 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000325 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000326
Bram Moolenaar5c736222010-01-06 20:54:52 +0100327 if fname =~ '\.bz2$' && executable("bzcat")
328 let decmp= "|bzcat"
329 let doro = 1
Jim Zhou56957ed2025-02-28 18:06:14 +0100330 elseif fname =~ '\.bz3$' && executable("bz3cat")
331 let decmp= "|bz3cat"
332 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100333 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000334 let decmp= "|zcat"
335 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100336 elseif fname =~ '\.lzma$' && executable("lzcat")
337 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000338 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200339 elseif fname =~ '\.xz$' && executable("xzcat")
340 let decmp= "|xzcat"
341 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100342 elseif fname =~ '\.zst$' && executable("zstdcat")
343 let decmp= "|zstdcat"
344 let doro = 1
Corpulent Robinb69cd522025-02-06 21:10:49 +0100345 elseif fname =~ '\.lz4$' && executable("lz4cat")
346 let decmp= "|lz4cat"
347 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000348 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000349 let decmp=""
350 let doro = 0
Jim Zhou56957ed2025-02-28 18:06:14 +0100351 if fname =~ '\.bz2$\|\.bz3$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000352 setlocal bin
353 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000354 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000356 if exists("g:tar_secure")
357 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000359 let tar_secure= " "
360 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200361
Bram Moolenaar5c736222010-01-06 20:54:52 +0100362 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200363 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 +0100364 exe "read ".fname
Christian Brabandt8ac975d2025-03-01 17:13:40 +0100365 elseif tarfile =~# '\.bz3$'
Jim Zhou56957ed2025-02-28 18:06:14 +0100366 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
367 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100368 elseif tarfile =~# '\.\(gz\)$'
369 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 +0100370 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100371 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
372 if has("unix") && executable("file")
373 let filekind= system("file ".shellescape(tarfile,1))
374 else
375 let filekind= ""
376 endif
377 if filekind =~ "bzip2"
378 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 +0100379 exe "read ".fname
Jim Zhou56957ed2025-02-28 18:06:14 +0100380 elseif filekind =~ "bzip3"
381 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
382 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
Corpulent Robinb69cd522025-02-06 21:10:49 +0100403 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
404 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
405 exe "read ".fname
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000406 else
407 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100408 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000409 let tarfile = substitute(tarfile, '-', './-', '')
410 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100411 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100412 exe "read ".fname
413 endif
414
415 redraw!
416
417if v:shell_error != 0
418 cd ..
419 call s:Rmdir("_ZIPVIM_")
420 exe "cd ".fnameescape(curdir)
421 echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000422 endif
423
424 if doro
425 " because the reverse process of compressing changed files back into the tarball is not currently supported
426 setlocal ro
427 endif
428
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200429 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000430 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000431
432 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200433 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000434 set nomod
435
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000436 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000437endfun
438
439" ---------------------------------------------------------------------
440" tar#Write: {{{2
441fun! tar#Write(fname)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000442 let repkeep= &report
443 set report=10
Lennart00129a8442024-11-11 22:39:30 +0100444 " temporary buffer variable workaround because too fucking tired. but it works now
445 let curdir= b:curdir
446 let tmpdir= b:tmpdir
Bram Moolenaara5792f52005-11-23 21:25:05 +0000447
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000448 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
449 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100450 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000451 return
452 endif
453
Bram Moolenaarab194812005-09-14 21:40:12 +0000454 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000455 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000456 redraw!
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000457 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000458 return
459 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000460
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200461 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
462 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000463
464 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100465 if tarfile =~# '\.bz2'
466 call system("bzip2 -d -- ".shellescape(tarfile,0))
467 let tarfile = substitute(tarfile,'\.bz2','','e')
468 let compress= "bzip2 -- ".shellescape(tarfile,0)
Jim Zhou56957ed2025-02-28 18:06:14 +0100469 elseif tarfile =~# '\.bz3'
470 call system("bzip3 -d -- ".shellescape(tarfile,0))
471 let tarfile = substitute(tarfile,'\.bz3','','e')
472 let compress= "bzip3 -- ".shellescape(tarfile,0)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100473 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100474 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000475 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100476 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000477 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100478 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000479 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100480 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000481 let tgz = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200482 elseif tarfile =~# '\.xz'
483 call system("xz -d -- ".shellescape(tarfile,0))
484 let tarfile = substitute(tarfile,'\.xz','','e')
485 let compress= "xz -- ".shellescape(tarfile,0)
Bram Moolenaar23515b42020-11-29 14:36:24 +0100486 elseif tarfile =~# '\.zst'
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100487 call system("zstd --decompress --rm -- ".shellescape(tarfile,0))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100488 let tarfile = substitute(tarfile,'\.zst','','e')
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100489 let compress= "zstd --rm -- ".shellescape(tarfile,0)
Corpulent Robinb69cd522025-02-06 21:10:49 +0100490 elseif tarfile =~# '\.lz4'
491 call system("lz4 --decompress --rm -- ".shellescape(tarfile,0))
492 let tarfile = substitute(tarfile,'\.lz4','','e')
493 let compress= "lz4 --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)
Bram Moolenaarab194812005-09-14 21:40:12 +0000498 endif
499
Bram Moolenaarab194812005-09-14 21:40:12 +0000500 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000501 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000502 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000503 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000504
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000505 if fname =~ '/'
506 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200507 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100508 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000509 endif
510 call mkdir(dirpath,"p")
511 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000512 if tarfile !~ '/'
513 let tarfile= curdir.'/'.tarfile
514 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000515 if tarfile =~ '^\s*-'
516 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
517 let tarfile = substitute(tarfile, '-', './-', '')
518 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100519
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000520 if exists("g:tar_secure")
521 let tar_secure= " -- "
522 else
523 let tar_secure= " "
524 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000525 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200526 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100527 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000528 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100529
Bram Moolenaara5792f52005-11-23 21:25:05 +0000530 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100531 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000532 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000533 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000535 else
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100536
537 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100538 call system(g:tar_cmd." -".g:tar_writeoptions." ".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 Moolenaar8c8de832008-06-24 22:58:06 +0000541 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000542 elseif exists("compress")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000543 call system(compress)
544 if exists("tgz")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000545 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
546 endif
547 endif
548 endif
549
550 " support writing tarfiles across a network
551 if s:tblfile_{winnr()} =~ '^\a\+://'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000552 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100553 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100554 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000555 let eikeep = &ei
556 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100557 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000558 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100559 let &ei = eikeep
560 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000561 q!
562 unlet s:tblfile_{winnr()}
563 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000564 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100565
Bram Moolenaarab194812005-09-14 21:40:12 +0000566 " cleanup and restore current directory
567 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000568 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000569 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000570 setlocal nomod
571
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000572 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000573endfun
574
575" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200576" tar#Diff: {{{2
577fun! tar#Diff(userfname,fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200578 let fname= a:fname
579 if a:userfname != ""
580 let fname= a:userfname
581 endif
582 if filereadable(fname)
583 " sets current file (from tarball) for diff'ing
584 " splits window vertically
585 " opens original file, sets it for diff'ing
586 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
587 diffthis
588 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100589 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200590 diffthis
591 else
592 redraw!
593 echo "***warning*** unable to read file<".fname.">"
594 endif
Bram Moolenaarff034192013-04-24 18:51:19 +0200595endfun
596
597" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100598" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
599fun! tar#Extract()
Bram Moolenaar29634562020-01-09 21:46:04 +0100600
601 let repkeep= &report
602 set report=10
603 let fname= getline(".")
Bram Moolenaar29634562020-01-09 21:46:04 +0100604
605 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
606 redraw!
607 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaar29634562020-01-09 21:46:04 +0100608 return
609 endif
610
611 " sanity check
612 if fname =~ '^"'
613 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100614 return
615 endif
616
617 let tarball = expand("%")
Bram Moolenaar29634562020-01-09 21:46:04 +0100618 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar29634562020-01-09 21:46:04 +0100619
620 let extractcmd= netrw#WinPath(g:tar_extractcmd)
621 if filereadable(tarbase.".tar")
Bram Moolenaar29634562020-01-09 21:46:04 +0100622 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
623 if v:shell_error != 0
624 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100625 else
626 echo "***note*** successfully extracted ".fname
627 endif
628
629 elseif filereadable(tarbase.".tgz")
630 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100631 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
632 if v:shell_error != 0
633 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100634 else
635 echo "***note*** successfully extracted ".fname
636 endif
637
638 elseif filereadable(tarbase.".tar.gz")
639 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100640 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
641 if v:shell_error != 0
642 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100643 else
644 echo "***note*** successfully extracted ".fname
645 endif
646
647 elseif filereadable(tarbase.".tbz")
648 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100649 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
650 if v:shell_error != 0
651 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100652 else
653 echo "***note*** successfully extracted ".fname
654 endif
655
656 elseif filereadable(tarbase.".tar.bz2")
657 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100658 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
659 if v:shell_error != 0
660 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100661 else
662 echo "***note*** successfully extracted ".fname
663 endif
664
Jim Zhou56957ed2025-02-28 18:06:14 +0100665 elseif filereadable(tarbase.".tar.bz3")
666 let extractcmd= substitute(extractcmd,"-","-j","")
667 call system(extractcmd." ".shellescape(tarbase).".tar.bz3 ".shellescape(fname))
668 if v:shell_error != 0
669 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz3 ".fname.": failed!" | echohl NONE
670 else
671 echo "***note*** successfully extracted ".fname
672 endif
673
Bram Moolenaar29634562020-01-09 21:46:04 +0100674 elseif filereadable(tarbase.".txz")
675 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100676 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
677 if v:shell_error != 0
678 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100679 else
680 echo "***note*** successfully extracted ".fname
681 endif
682
683 elseif filereadable(tarbase.".tar.xz")
684 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100685 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
686 if v:shell_error != 0
687 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100688 else
689 echo "***note*** successfully extracted ".fname
690 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100691
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100692 elseif filereadable(tarbase.".tzst")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100693 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100694 call system(extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100695 if v:shell_error != 0
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100696 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100697 else
698 echo "***note*** successfully extracted ".fname
699 endif
700
701 elseif filereadable(tarbase.".tar.zst")
702 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100703 call system(extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100704 if v:shell_error != 0
705 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
Corpulent Robinb69cd522025-02-06 21:10:49 +0100706 else
707 echo "***note*** successfully extracted ".fname
708 endif
709
710 elseif filereadable(tarbase.".tlz4")
711 let extractcmd= substitute(extractcmd,"-","-I lz4","")
712 call system(extractcmd." ".shellescape(tarbase).".tlz4 ".shellescape(fname))
713 if v:shell_error != 0
714 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tlz4 ".fname.": failed!" | echohl NONE
715 else
716 echo "***note*** successfully extracted ".fname
717 endif
718
719 elseif filereadable(tarbase.".tar.lz4")
720 let extractcmd= substitute(extractcmd,"-","-I lz4","")
721 call system(extractcmd." ".shellescape(tarbase).".tar.lz4".shellescape(fname))
722 if v:shell_error != 0
723 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.lz4 ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100724 else
725 echo "***note*** successfully extracted ".fname
726 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100727 endif
728
729 " restore option
730 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100731endfun
732
733" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100734" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000735fun! s:Rmdir(fname)
Bram Moolenaarab194812005-09-14 21:40:12 +0000736 if has("unix")
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 elseif has("win32") || has("win95") || has("win64") || has("win16")
739 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100740 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000741 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100742 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000743 endif
744 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000745endfun
746
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000747" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100748" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
749fun! tar#Vimuntar(...)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100750 let tarball = expand("%")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100751 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100752 let tarhome = expand("%:p")
753 if has("win32") || has("win95") || has("win64") || has("win16")
754 let tarhome= substitute(tarhome,'\\','/','g')
755 endif
756 let tarhome= substitute(tarhome,'/[^/]*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100757 let tartail = expand("%:t")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100758 let curdir = getcwd()
Bram Moolenaar5c736222010-01-06 20:54:52 +0100759 " set up vimhome
760 if a:0 > 0 && a:1 != ""
761 let vimhome= a:1
762 else
763 let vimhome= vimball#VimballHome()
764 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100765
Bram Moolenaar5c736222010-01-06 20:54:52 +0100766 if simplify(curdir) != simplify(vimhome)
767 " copy (possibly compressed) tarball to .vim/vimfiles
Bram Moolenaar5c736222010-01-06 20:54:52 +0100768 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100769 exe "cd ".fnameescape(vimhome)
770 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100771
772 " if necessary, decompress the tarball; then, extract it
773 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100774 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100775 silent exe "!gunzip ".shellescape(tartail)
776 elseif executable("gzip")
777 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000778 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200779 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100780 if simplify(curdir) != simplify(tarhome)
781 " remove decompressed tarball, restore directory
Bram Moolenaar5c736222010-01-06 20:54:52 +0100782 call delete(tartail.".tar")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100783 exe "cd ".fnameescape(curdir)
784 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100785 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000786 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000787 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100788 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000789 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100790 let extractcmd= netrw#WinPath(g:tar_extractcmd)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100791 call system(extractcmd." ".shellescape(tarbase.".tar"))
792
793 " set up help
794 if filereadable("doc/".tarbase.".txt")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100795 exe "helptags ".getcwd()."/doc"
796 endif
797
798 if simplify(tarhome) != simplify(vimhome)
799 " remove decompressed tarball, restore directory
800 call delete(vimhome."/".tarbase.".tar")
801 exe "cd ".fnameescape(curdir)
802 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000803endfun
804
Bram Moolenaar5c736222010-01-06 20:54:52 +0100805" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000806" Modelines And Restoration: {{{1
807let &cpo= s:keepcpo
808unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000809" vim:ts=8 fdm=marker