blob: ea1525c45450d98080c90db6b82405f0a6bd8683 [file] [log] [blame]
Corpulent Robinb69cd522025-02-06 21:10:49 +01001" tar.vim: Handles browsing tarfiles - AUTOLOAD PORTION
Jim Zhou56957ed2025-02-28 18:06:14 +01002" Date: Feb 28, 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)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000013"
Bram Moolenaara5792f52005-11-23 21:25:05 +000014" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015"
Bram Moolenaar29634562020-01-09 21:46:04 +010016" Copyright: Copyright (C) 2005-2017 Charles E. Campbell {{{1
Bram Moolenaara5792f52005-11-23 21:25:05 +000017" Permission is hereby granted to use and distribute this code,
18" with or without modifications, provided that this copyright
19" notice is copied with it. Like anything else that's free,
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020" tar.vim and tarPlugin.vim are provided *as is* and comes
21" with no warranty of any kind, either expressed or implied.
22" By using this plugin, you agree that in no event will the
23" copyright holder be liable for any damages resulting from
24" the use of this software.
Bram Moolenaarab194812005-09-14 21:40:12 +000025" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000026" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010027if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000028 finish
29endif
Lennart00129a8442024-11-11 22:39:30 +010030let g:loaded_tar= "v32b"
Bram Moolenaar5c736222010-01-06 20:54:52 +010031if v:version < 702
32 echohl WarningMsg
33 echo "***warning*** this version of tar needs vim 7.2"
34 echohl Normal
35 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000036endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010037let s:keepcpo= &cpo
38set cpo&vim
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000039
Bram Moolenaara5792f52005-11-23 21:25:05 +000040" ---------------------------------------------------------------------
41" Default Settings: {{{1
42if !exists("g:tar_browseoptions")
43 let g:tar_browseoptions= "Ptf"
44endif
45if !exists("g:tar_readoptions")
Lennart00129a8442024-11-11 22:39:30 +010046 let g:tar_readoptions= "pPxf"
Bram Moolenaara5792f52005-11-23 21:25:05 +000047endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000048if !exists("g:tar_cmd")
49 let g:tar_cmd= "tar"
50endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000051if !exists("g:tar_writeoptions")
52 let g:tar_writeoptions= "uf"
53endif
Bram Moolenaar29634562020-01-09 21:46:04 +010054if !exists("g:tar_delfile")
55 let g:tar_delfile="--delete -f"
56endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020057if !exists("g:netrw_cygwin")
58 if has("win32") || has("win95") || has("win64") || has("win16")
59 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
60 let g:netrw_cygwin= 1
61 else
62 let g:netrw_cygwin= 0
63 endif
64 else
65 let g:netrw_cygwin= 0
66 endif
67endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010068if !exists("g:tar_copycmd")
69 if !exists("g:netrw_localcopycmd")
70 if has("win32") || has("win95") || has("win64") || has("win16")
71 if g:netrw_cygwin
72 let g:netrw_localcopycmd= "cp"
73 else
74 let g:netrw_localcopycmd= "copy"
75 endif
76 elseif has("unix") || has("macunix")
77 let g:netrw_localcopycmd= "cp"
78 else
79 let g:netrw_localcopycmd= ""
80 endif
81 endif
82 let g:tar_copycmd= g:netrw_localcopycmd
83endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010084if !exists("g:tar_extractcmd")
Lennart00129a8442024-11-11 22:39:30 +010085 let g:tar_extractcmd= "tar -pxf"
Bram Moolenaar5c736222010-01-06 20:54:52 +010086endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000087
Bram Moolenaar8c8de832008-06-24 22:58:06 +000088" set up shell quoting character
89if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020090 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000091 let g:tar_shq= &shq
92 elseif has("win32") || has("win95") || has("win64") || has("win16")
93 if exists("g:netrw_cygwin") && g:netrw_cygwin
94 let g:tar_shq= "'"
95 else
96 let g:tar_shq= '"'
97 endif
98 else
99 let g:tar_shq= "'"
100 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000101endif
102
Bram Moolenaara5792f52005-11-23 21:25:05 +0000103" ----------------
104" Functions: {{{1
105" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000106
Bram Moolenaara5792f52005-11-23 21:25:05 +0000107" ---------------------------------------------------------------------
108" tar#Browse: {{{2
109fun! tar#Browse(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 Moolenaar910f66f2006-04-05 20:41:53 +0000116 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000117 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000118 return
119 endif
120 if !filereadable(a:tarfile)
121 if a:tarfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100122 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000123 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000124 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000125 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000126 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000127 return
128 endif
129 if &ma != 1
130 set ma
131 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200132 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000133
Bram Moolenaara5792f52005-11-23 21:25:05 +0000134 setlocal noswapfile
135 setlocal buftype=nofile
136 setlocal bufhidden=hide
137 setlocal nobuflisted
138 setlocal nowrap
139 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000140
Bram Moolenaara5792f52005-11-23 21:25:05 +0000141 " give header
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000142 let lastline= line("$")
143 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
144 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
145 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200146 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100147 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200148 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000149
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000150 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200151 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000152 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100153 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000154 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000155 let curlast= line("$")
Bram Moolenaar29634562020-01-09 21:46:04 +0100156
157 if tarfile =~# '\.\(gz\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100158 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
159
Corpulent Robinb69cd522025-02-06 21:10:49 +0100160 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' ||
161 \ tarfile =~# '\.\(tzst\)$' || tarfile =~# '\.\(tlz4\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100162 if has("unix") && executable("file")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100163 let filekind= system("file ".shellescape(tarfile,1))
Bram Moolenaar29634562020-01-09 21:46:04 +0100164 else
165 let filekind= ""
166 endif
167
168 if filekind =~ "bzip2"
169 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Jim Zhou56957ed2025-02-28 18:06:14 +0100170 elseif filekind =~ "bzip3"
171 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100172 elseif filekind =~ "XZ"
173 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100174 elseif filekind =~ "Zstandard"
175 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100176 elseif filekind =~ "LZ4"
177 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100178 else
179 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
180 endif
181
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 elseif tarfile =~# '\.lrp'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200183 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100184 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200185 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Jim Zhou56957ed2025-02-28 18:06:14 +0100186 elseif tarfile =~# '\.\(bz3\|tb3\)$'
187 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100188 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200189 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200190 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200191 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100192 elseif tarfile =~# '\.\(zst\|tzst\)$'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100193 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100194 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
195 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000196 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000197 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100198 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000199 let tarfile = substitute(tarfile, '-', './-', '')
200 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200201 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000202 endif
203 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000204 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000205 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000206 return
207 endif
Christian Brabandt3d372312023-11-05 17:44:05 +0100208 "
Christian Brabandt67abf152023-11-14 17:15:17 +0100209 " The following should not be neccessary, since in case of errors the
210 " previous if statement should have caught the problem (because tar exited
211 " with a non-zero exit code).
212 " if line("$") == curlast || ( line("$") == (curlast + 1) &&
213 " \ getline("$") =~# '\c\<\%(warning\|error\|inappropriate\|unrecognized\)\>' &&
214 " \ getline("$") =~ '\s' )
215 " redraw!
216 " echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
217 " keepj sil! %d
218 " let eikeep= &ei
219 " set ei=BufReadCmd,FileReadCmd
220 " exe "r ".fnameescape(a:tarfile)
221 " let &ei= eikeep
222 " keepj sil! 1d
223 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
224 " return
225 " endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000226
Bram Moolenaar29634562020-01-09 21:46:04 +0100227 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000228 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100229 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100230 noremap <silent> <buffer> x :call tar#Extract()<cr>
Bram Moolenaar29634562020-01-09 21:46:04 +0100231 if &mouse != ""
232 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
233 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000234
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000235 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000236endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000237
Bram Moolenaarab194812005-09-14 21:40:12 +0000238" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000239" TarBrowseSelect: {{{2
240fun! s:TarBrowseSelect()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000241 let repkeep= &report
242 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000243 let fname= getline(".")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000244
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000245 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
246 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100247 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000248 return
249 endif
250
Bram Moolenaara5792f52005-11-23 21:25:05 +0000251 " sanity check
252 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000253 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000254 return
255 endif
256
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200257 " about to make a new window, need to use b:tarfile
258 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000259 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200260 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000261 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100262 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000263 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000264
Bram Moolenaar29634562020-01-09 21:46:04 +0100265 " open a new window (tar#Read will read a file into it)
266 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000267 if !exists("g:tar_nomax") || g:tar_nomax == 0
268 wincmd _
269 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000270 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000271 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000272 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200273 set nomod
274 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000275
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000276 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000277endfun
278
279" ---------------------------------------------------------------------
280" tar#Read: {{{2
281fun! tar#Read(fname,mode)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000282 let repkeep= &report
283 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000284 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
285 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Lennart00129a8442024-11-11 22:39:30 +0100286
Corpulent Robinb69cd522025-02-06 21:10:49 +0100287 " changing the directory to the temporary earlier to allow tar to extract the file with permissions intact
Lennart00129a8442024-11-11 22:39:30 +0100288 if !exists("*mkdir")
289 redraw!
290 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
291 let &report= repkeep
292 return
293 endif
294
295 let curdir= getcwd()
296 let tmpdir= tempname()
297 let b:curdir= tmpdir
298 let b:tmpdir= curdir
299 if tmpdir =~ '\.'
300 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
301 endif
302 call mkdir(tmpdir,"p")
303
304 " attempt to change to the indicated directory
305 try
306 exe "cd ".fnameescape(tmpdir)
307 catch /^Vim\%((\a\+)\)\=:E344/
308 redraw!
309 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
310 let &report= repkeep
311 return
312 endtry
313
314 " place temporary files under .../_ZIPVIM_/
315 if isdirectory("_ZIPVIM_")
316 call s:Rmdir("_ZIPVIM_")
317 endif
318 call mkdir("_ZIPVIM_")
319 cd _ZIPVIM_
320
Bram Moolenaarff034192013-04-24 18:51:19 +0200321 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000322 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100323 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000324 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000325
Bram Moolenaar5c736222010-01-06 20:54:52 +0100326 if fname =~ '\.bz2$' && executable("bzcat")
327 let decmp= "|bzcat"
328 let doro = 1
Jim Zhou56957ed2025-02-28 18:06:14 +0100329 elseif fname =~ '\.bz3$' && executable("bz3cat")
330 let decmp= "|bz3cat"
331 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100332 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000333 let decmp= "|zcat"
334 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100335 elseif fname =~ '\.lzma$' && executable("lzcat")
336 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000337 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200338 elseif fname =~ '\.xz$' && executable("xzcat")
339 let decmp= "|xzcat"
340 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100341 elseif fname =~ '\.zst$' && executable("zstdcat")
342 let decmp= "|zstdcat"
343 let doro = 1
Corpulent Robinb69cd522025-02-06 21:10:49 +0100344 elseif fname =~ '\.lz4$' && executable("lz4cat")
345 let decmp= "|lz4cat"
346 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000347 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000348 let decmp=""
349 let doro = 0
Jim Zhou56957ed2025-02-28 18:06:14 +0100350 if fname =~ '\.bz2$\|\.bz3$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000351 setlocal bin
352 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000353 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000354
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000355 if exists("g:tar_secure")
356 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000357 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000358 let tar_secure= " "
359 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200360
Bram Moolenaar5c736222010-01-06 20:54:52 +0100361 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200362 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 +0100363 exe "read ".fname
Jim Zhou56957ed2025-02-28 18:06:14 +0100364 if tarfile =~# '\.bz3$'
365 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
366 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100367 elseif tarfile =~# '\.\(gz\)$'
368 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 +0100369 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100370 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
371 if has("unix") && executable("file")
372 let filekind= system("file ".shellescape(tarfile,1))
373 else
374 let filekind= ""
375 endif
376 if filekind =~ "bzip2"
377 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 +0100378 exe "read ".fname
Jim Zhou56957ed2025-02-28 18:06:14 +0100379 elseif filekind =~ "bzip3"
380 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
381 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100382 elseif filekind =~ "XZ"
383 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 +0100384 exe "read ".fname
Bram Moolenaar23515b42020-11-29 14:36:24 +0100385 elseif filekind =~ "Zstandard"
386 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 +0100387 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100388 else
389 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 +0100390 exe "read ".fname
Bram Moolenaar29634562020-01-09 21:46:04 +0100391 endif
392
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000393 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200394 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 +0100395 exe "read ".fname
Bram Moolenaar5c736222010-01-06 20:54:52 +0100396 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200397 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 +0100398 exe "read ".fname
Bram Moolenaar477db062010-07-28 18:17:41 +0200399 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200400 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 +0100401 exe "read ".fname
Corpulent Robinb69cd522025-02-06 21:10:49 +0100402 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
403 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
404 exe "read ".fname
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000405 else
406 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100407 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000408 let tarfile = substitute(tarfile, '-', './-', '')
409 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100410 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Lennart00129a8442024-11-11 22:39:30 +0100411 exe "read ".fname
412 endif
413
414 redraw!
415
416if v:shell_error != 0
417 cd ..
418 call s:Rmdir("_ZIPVIM_")
419 exe "cd ".fnameescape(curdir)
420 echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000421 endif
422
423 if doro
424 " because the reverse process of compressing changed files back into the tarball is not currently supported
425 setlocal ro
426 endif
427
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200428 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000429 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000430
431 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200432 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000433 set nomod
434
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000435 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000436endfun
437
438" ---------------------------------------------------------------------
439" tar#Write: {{{2
440fun! tar#Write(fname)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000441 let repkeep= &report
442 set report=10
Lennart00129a8442024-11-11 22:39:30 +0100443 " temporary buffer variable workaround because too fucking tired. but it works now
444 let curdir= b:curdir
445 let tmpdir= b:tmpdir
Bram Moolenaara5792f52005-11-23 21:25:05 +0000446
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000447 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
448 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100449 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000450 return
451 endif
452
Bram Moolenaarab194812005-09-14 21:40:12 +0000453 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000454 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000455 redraw!
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000456 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000457 return
458 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000459
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200460 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
461 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000462
463 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100464 if tarfile =~# '\.bz2'
465 call system("bzip2 -d -- ".shellescape(tarfile,0))
466 let tarfile = substitute(tarfile,'\.bz2','','e')
467 let compress= "bzip2 -- ".shellescape(tarfile,0)
Jim Zhou56957ed2025-02-28 18:06:14 +0100468 elseif tarfile =~# '\.bz3'
469 call system("bzip3 -d -- ".shellescape(tarfile,0))
470 let tarfile = substitute(tarfile,'\.bz3','','e')
471 let compress= "bzip3 -- ".shellescape(tarfile,0)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100472 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100473 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000474 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100475 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000476 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100477 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000478 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100479 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000480 let tgz = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200481 elseif tarfile =~# '\.xz'
482 call system("xz -d -- ".shellescape(tarfile,0))
483 let tarfile = substitute(tarfile,'\.xz','','e')
484 let compress= "xz -- ".shellescape(tarfile,0)
Bram Moolenaar23515b42020-11-29 14:36:24 +0100485 elseif tarfile =~# '\.zst'
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100486 call system("zstd --decompress --rm -- ".shellescape(tarfile,0))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100487 let tarfile = substitute(tarfile,'\.zst','','e')
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100488 let compress= "zstd --rm -- ".shellescape(tarfile,0)
Corpulent Robinb69cd522025-02-06 21:10:49 +0100489 elseif tarfile =~# '\.lz4'
490 call system("lz4 --decompress --rm -- ".shellescape(tarfile,0))
491 let tarfile = substitute(tarfile,'\.lz4','','e')
492 let compress= "lz4 --rm -- ".shellescape(tarfile,0)
Bram Moolenaar477db062010-07-28 18:17:41 +0200493 elseif tarfile =~# '\.lzma'
494 call system("lzma -d -- ".shellescape(tarfile,0))
495 let tarfile = substitute(tarfile,'\.lzma','','e')
496 let compress= "lzma -- ".shellescape(tarfile,0)
Bram Moolenaarab194812005-09-14 21:40:12 +0000497 endif
498
Bram Moolenaarab194812005-09-14 21:40:12 +0000499 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000500 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000501 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000502 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000503
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000504 if fname =~ '/'
505 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200506 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100507 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000508 endif
509 call mkdir(dirpath,"p")
510 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000511 if tarfile !~ '/'
512 let tarfile= curdir.'/'.tarfile
513 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000514 if tarfile =~ '^\s*-'
515 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
516 let tarfile = substitute(tarfile, '-', './-', '')
517 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100518
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000519 if exists("g:tar_secure")
520 let tar_secure= " -- "
521 else
522 let tar_secure= " "
523 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000524 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200525 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100526 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000527 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100528
Bram Moolenaara5792f52005-11-23 21:25:05 +0000529 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100530 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000531 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000532 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000534 else
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100535
536 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100537 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000538 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000539 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000541 elseif exists("compress")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000542 call system(compress)
543 if exists("tgz")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000544 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
545 endif
546 endif
547 endif
548
549 " support writing tarfiles across a network
550 if s:tblfile_{winnr()} =~ '^\a\+://'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000551 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100552 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100553 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000554 let eikeep = &ei
555 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100556 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000557 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100558 let &ei = eikeep
559 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000560 q!
561 unlet s:tblfile_{winnr()}
562 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000563 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100564
Bram Moolenaarab194812005-09-14 21:40:12 +0000565 " cleanup and restore current directory
566 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000567 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000568 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000569 setlocal nomod
570
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000571 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000572endfun
573
574" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200575" tar#Diff: {{{2
576fun! tar#Diff(userfname,fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200577 let fname= a:fname
578 if a:userfname != ""
579 let fname= a:userfname
580 endif
581 if filereadable(fname)
582 " sets current file (from tarball) for diff'ing
583 " splits window vertically
584 " opens original file, sets it for diff'ing
585 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
586 diffthis
587 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100588 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200589 diffthis
590 else
591 redraw!
592 echo "***warning*** unable to read file<".fname.">"
593 endif
Bram Moolenaarff034192013-04-24 18:51:19 +0200594endfun
595
596" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100597" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
598fun! tar#Extract()
Bram Moolenaar29634562020-01-09 21:46:04 +0100599
600 let repkeep= &report
601 set report=10
602 let fname= getline(".")
Bram Moolenaar29634562020-01-09 21:46:04 +0100603
604 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
605 redraw!
606 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaar29634562020-01-09 21:46:04 +0100607 return
608 endif
609
610 " sanity check
611 if fname =~ '^"'
612 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100613 return
614 endif
615
616 let tarball = expand("%")
Bram Moolenaar29634562020-01-09 21:46:04 +0100617 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar29634562020-01-09 21:46:04 +0100618
619 let extractcmd= netrw#WinPath(g:tar_extractcmd)
620 if filereadable(tarbase.".tar")
Bram Moolenaar29634562020-01-09 21:46:04 +0100621 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
622 if v:shell_error != 0
623 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100624 else
625 echo "***note*** successfully extracted ".fname
626 endif
627
628 elseif filereadable(tarbase.".tgz")
629 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100630 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
631 if v:shell_error != 0
632 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100633 else
634 echo "***note*** successfully extracted ".fname
635 endif
636
637 elseif filereadable(tarbase.".tar.gz")
638 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100639 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
640 if v:shell_error != 0
641 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100642 else
643 echo "***note*** successfully extracted ".fname
644 endif
645
646 elseif filereadable(tarbase.".tbz")
647 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100648 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
649 if v:shell_error != 0
650 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100651 else
652 echo "***note*** successfully extracted ".fname
653 endif
654
655 elseif filereadable(tarbase.".tar.bz2")
656 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100657 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
658 if v:shell_error != 0
659 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100660 else
661 echo "***note*** successfully extracted ".fname
662 endif
663
Jim Zhou56957ed2025-02-28 18:06:14 +0100664 elseif filereadable(tarbase.".tar.bz3")
665 let extractcmd= substitute(extractcmd,"-","-j","")
666 call system(extractcmd." ".shellescape(tarbase).".tar.bz3 ".shellescape(fname))
667 if v:shell_error != 0
668 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz3 ".fname.": failed!" | echohl NONE
669 else
670 echo "***note*** successfully extracted ".fname
671 endif
672
Bram Moolenaar29634562020-01-09 21:46:04 +0100673 elseif filereadable(tarbase.".txz")
674 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100675 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
676 if v:shell_error != 0
677 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100678 else
679 echo "***note*** successfully extracted ".fname
680 endif
681
682 elseif filereadable(tarbase.".tar.xz")
683 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100684 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
685 if v:shell_error != 0
686 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100687 else
688 echo "***note*** successfully extracted ".fname
689 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100690
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100691 elseif filereadable(tarbase.".tzst")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100692 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100693 call system(extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100694 if v:shell_error != 0
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100695 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100696 else
697 echo "***note*** successfully extracted ".fname
698 endif
699
700 elseif filereadable(tarbase.".tar.zst")
701 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100702 call system(extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100703 if v:shell_error != 0
704 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
Corpulent Robinb69cd522025-02-06 21:10:49 +0100705 else
706 echo "***note*** successfully extracted ".fname
707 endif
708
709 elseif filereadable(tarbase.".tlz4")
710 let extractcmd= substitute(extractcmd,"-","-I lz4","")
711 call system(extractcmd." ".shellescape(tarbase).".tlz4 ".shellescape(fname))
712 if v:shell_error != 0
713 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tlz4 ".fname.": failed!" | echohl NONE
714 else
715 echo "***note*** successfully extracted ".fname
716 endif
717
718 elseif filereadable(tarbase.".tar.lz4")
719 let extractcmd= substitute(extractcmd,"-","-I lz4","")
720 call system(extractcmd." ".shellescape(tarbase).".tar.lz4".shellescape(fname))
721 if v:shell_error != 0
722 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.lz4 ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100723 else
724 echo "***note*** successfully extracted ".fname
725 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100726 endif
727
728 " restore option
729 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100730endfun
731
732" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100733" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000734fun! s:Rmdir(fname)
Bram Moolenaarab194812005-09-14 21:40:12 +0000735 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100736 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000737 elseif has("win32") || has("win95") || has("win64") || has("win16")
738 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100739 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000740 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100741 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000742 endif
743 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000744endfun
745
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000746" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100747" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
748fun! tar#Vimuntar(...)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100749 let tarball = expand("%")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100750 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100751 let tarhome = expand("%:p")
752 if has("win32") || has("win95") || has("win64") || has("win16")
753 let tarhome= substitute(tarhome,'\\','/','g')
754 endif
755 let tarhome= substitute(tarhome,'/[^/]*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100756 let tartail = expand("%:t")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100757 let curdir = getcwd()
Bram Moolenaar5c736222010-01-06 20:54:52 +0100758 " set up vimhome
759 if a:0 > 0 && a:1 != ""
760 let vimhome= a:1
761 else
762 let vimhome= vimball#VimballHome()
763 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100764
Bram Moolenaar5c736222010-01-06 20:54:52 +0100765 if simplify(curdir) != simplify(vimhome)
766 " copy (possibly compressed) tarball to .vim/vimfiles
Bram Moolenaar5c736222010-01-06 20:54:52 +0100767 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100768 exe "cd ".fnameescape(vimhome)
769 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100770
771 " if necessary, decompress the tarball; then, extract it
772 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100773 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100774 silent exe "!gunzip ".shellescape(tartail)
775 elseif executable("gzip")
776 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000777 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200778 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100779 if simplify(curdir) != simplify(tarhome)
780 " remove decompressed tarball, restore directory
Bram Moolenaar5c736222010-01-06 20:54:52 +0100781 call delete(tartail.".tar")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100782 exe "cd ".fnameescape(curdir)
783 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100784 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000785 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000786 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100787 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000788 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100789 let extractcmd= netrw#WinPath(g:tar_extractcmd)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100790 call system(extractcmd." ".shellescape(tarbase.".tar"))
791
792 " set up help
793 if filereadable("doc/".tarbase.".txt")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100794 exe "helptags ".getcwd()."/doc"
795 endif
796
797 if simplify(tarhome) != simplify(vimhome)
798 " remove decompressed tarball, restore directory
799 call delete(vimhome."/".tarbase.".tar")
800 exe "cd ".fnameescape(curdir)
801 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000802endfun
803
Bram Moolenaar5c736222010-01-06 20:54:52 +0100804" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000805" Modelines And Restoration: {{{1
806let &cpo= s:keepcpo
807unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000808" vim:ts=8 fdm=marker