blob: 14e68b7899c728b4ac93dda9ddcb430e403f8c9f [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()
Christian Brabandt334a13b2025-03-02 19:33:51 +010014" 2025 Mar 02 by Vim Project: escape the filename before using :read
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015"
Bram Moolenaara5792f52005-11-23 21:25:05 +000016" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017"
Bram Moolenaar29634562020-01-09 21:46:04 +010018" Copyright: Copyright (C) 2005-2017 Charles E. Campbell {{{1
Bram Moolenaara5792f52005-11-23 21:25:05 +000019" Permission is hereby granted to use and distribute this code,
20" with or without modifications, provided that this copyright
21" notice is copied with it. Like anything else that's free,
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022" tar.vim and tarPlugin.vim are provided *as is* and comes
23" with no warranty of any kind, either expressed or implied.
24" By using this plugin, you agree that in no event will the
25" copyright holder be liable for any damages resulting from
26" the use of this software.
Bram Moolenaarab194812005-09-14 21:40:12 +000027" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000028" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010029if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000030 finish
31endif
Lennart00129a8442024-11-11 22:39:30 +010032let g:loaded_tar= "v32b"
Bram Moolenaar5c736222010-01-06 20:54:52 +010033if v:version < 702
34 echohl WarningMsg
35 echo "***warning*** this version of tar needs vim 7.2"
36 echohl Normal
37 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000038endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010039let s:keepcpo= &cpo
40set cpo&vim
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000041
Bram Moolenaara5792f52005-11-23 21:25:05 +000042" ---------------------------------------------------------------------
43" Default Settings: {{{1
44if !exists("g:tar_browseoptions")
45 let g:tar_browseoptions= "Ptf"
46endif
47if !exists("g:tar_readoptions")
Lennart00129a8442024-11-11 22:39:30 +010048 let g:tar_readoptions= "pPxf"
Bram Moolenaara5792f52005-11-23 21:25:05 +000049endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000050if !exists("g:tar_cmd")
51 let g:tar_cmd= "tar"
52endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000053if !exists("g:tar_writeoptions")
54 let g:tar_writeoptions= "uf"
55endif
Bram Moolenaar29634562020-01-09 21:46:04 +010056if !exists("g:tar_delfile")
57 let g:tar_delfile="--delete -f"
58endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020059if !exists("g:netrw_cygwin")
60 if has("win32") || has("win95") || has("win64") || has("win16")
61 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
62 let g:netrw_cygwin= 1
63 else
64 let g:netrw_cygwin= 0
65 endif
66 else
67 let g:netrw_cygwin= 0
68 endif
69endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010070if !exists("g:tar_copycmd")
71 if !exists("g:netrw_localcopycmd")
72 if has("win32") || has("win95") || has("win64") || has("win16")
73 if g:netrw_cygwin
74 let g:netrw_localcopycmd= "cp"
75 else
76 let g:netrw_localcopycmd= "copy"
77 endif
78 elseif has("unix") || has("macunix")
79 let g:netrw_localcopycmd= "cp"
80 else
81 let g:netrw_localcopycmd= ""
82 endif
83 endif
84 let g:tar_copycmd= g:netrw_localcopycmd
85endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010086if !exists("g:tar_extractcmd")
Lennart00129a8442024-11-11 22:39:30 +010087 let g:tar_extractcmd= "tar -pxf"
Bram Moolenaar5c736222010-01-06 20:54:52 +010088endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000089
Bram Moolenaar8c8de832008-06-24 22:58:06 +000090" set up shell quoting character
91if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020092 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000093 let g:tar_shq= &shq
94 elseif has("win32") || has("win95") || has("win64") || has("win16")
95 if exists("g:netrw_cygwin") && g:netrw_cygwin
96 let g:tar_shq= "'"
97 else
98 let g:tar_shq= '"'
99 endif
100 else
101 let g:tar_shq= "'"
102 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000103endif
104
Bram Moolenaara5792f52005-11-23 21:25:05 +0000105" ----------------
106" Functions: {{{1
107" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000108
Bram Moolenaara5792f52005-11-23 21:25:05 +0000109" ---------------------------------------------------------------------
110" tar#Browse: {{{2
111fun! tar#Browse(tarfile)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000112 let repkeep= &report
113 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000114
Bram Moolenaara5792f52005-11-23 21:25:05 +0000115 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000116 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000117 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000118 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000119 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000120 return
121 endif
122 if !filereadable(a:tarfile)
123 if a:tarfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100124 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000125 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000126 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000127 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000128 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000129 return
130 endif
131 if &ma != 1
132 set ma
133 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200134 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000135
Bram Moolenaara5792f52005-11-23 21:25:05 +0000136 setlocal noswapfile
137 setlocal buftype=nofile
138 setlocal bufhidden=hide
139 setlocal nobuflisted
140 setlocal nowrap
141 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000142
Bram Moolenaara5792f52005-11-23 21:25:05 +0000143 " give header
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000144 let lastline= line("$")
145 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
146 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
147 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200148 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100149 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200150 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000151
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000152 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200153 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000154 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100155 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000156 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000157 let curlast= line("$")
Bram Moolenaar29634562020-01-09 21:46:04 +0100158
159 if tarfile =~# '\.\(gz\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100160 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
161
Corpulent Robinb69cd522025-02-06 21:10:49 +0100162 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' ||
163 \ tarfile =~# '\.\(tzst\)$' || tarfile =~# '\.\(tlz4\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100164 if has("unix") && executable("file")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100165 let filekind= system("file ".shellescape(tarfile,1))
Bram Moolenaar29634562020-01-09 21:46:04 +0100166 else
167 let filekind= ""
168 endif
169
170 if filekind =~ "bzip2"
171 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Jim Zhou56957ed2025-02-28 18:06:14 +0100172 elseif filekind =~ "bzip3"
173 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100174 elseif filekind =~ "XZ"
175 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100176 elseif filekind =~ "Zstandard"
177 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100178 elseif filekind =~ "LZ4"
179 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100180 else
181 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
182 endif
183
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000184 elseif tarfile =~# '\.lrp'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200185 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100186 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200187 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Jim Zhou56957ed2025-02-28 18:06:14 +0100188 elseif tarfile =~# '\.\(bz3\|tb3\)$'
189 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100190 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200191 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200192 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200193 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100194 elseif tarfile =~# '\.\(zst\|tzst\)$'
Bram Moolenaar23515b42020-11-29 14:36:24 +0100195 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Corpulent Robinb69cd522025-02-06 21:10:49 +0100196 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
197 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000198 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000199 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100200 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000201 let tarfile = substitute(tarfile, '-', './-', '')
202 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200203 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000204 endif
205 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000206 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000207 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000208 return
209 endif
Christian Brabandt3d372312023-11-05 17:44:05 +0100210 "
Christian Brabandt67abf152023-11-14 17:15:17 +0100211 " The following should not be neccessary, since in case of errors the
212 " previous if statement should have caught the problem (because tar exited
213 " with a non-zero exit code).
214 " if line("$") == curlast || ( line("$") == (curlast + 1) &&
215 " \ getline("$") =~# '\c\<\%(warning\|error\|inappropriate\|unrecognized\)\>' &&
216 " \ getline("$") =~ '\s' )
217 " redraw!
218 " echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
219 " keepj sil! %d
220 " let eikeep= &ei
221 " set ei=BufReadCmd,FileReadCmd
222 " exe "r ".fnameescape(a:tarfile)
223 " let &ei= eikeep
224 " keepj sil! 1d
225 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
226 " return
227 " endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000228
Bram Moolenaar29634562020-01-09 21:46:04 +0100229 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000230 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100231 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100232 noremap <silent> <buffer> x :call tar#Extract()<cr>
Bram Moolenaar29634562020-01-09 21:46:04 +0100233 if &mouse != ""
234 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
235 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000236
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000237 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000238endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000239
Bram Moolenaarab194812005-09-14 21:40:12 +0000240" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000241" TarBrowseSelect: {{{2
242fun! s:TarBrowseSelect()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000243 let repkeep= &report
244 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000245 let fname= getline(".")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000246
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000247 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
248 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100249 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000250 return
251 endif
252
Bram Moolenaara5792f52005-11-23 21:25:05 +0000253 " sanity check
254 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000255 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000256 return
257 endif
258
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200259 " about to make a new window, need to use b:tarfile
260 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000261 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200262 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000263 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100264 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000265 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000266
Bram Moolenaar29634562020-01-09 21:46:04 +0100267 " open a new window (tar#Read will read a file into it)
268 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000269 if !exists("g:tar_nomax") || g:tar_nomax == 0
270 wincmd _
271 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000272 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000273 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000274 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200275 set nomod
276 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000277
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000278 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000279endfun
280
281" ---------------------------------------------------------------------
282" tar#Read: {{{2
283fun! tar#Read(fname,mode)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000284 let repkeep= &report
285 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000286 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
287 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Christian Brabandt334a13b2025-03-02 19:33:51 +0100288 " be careful not to execute special crafted files
289 let escape_file = fname->fnameescape()
Lennart00129a8442024-11-11 22:39:30 +0100290
Corpulent Robinb69cd522025-02-06 21:10:49 +0100291 " changing the directory to the temporary earlier to allow tar to extract the file with permissions intact
Lennart00129a8442024-11-11 22:39:30 +0100292 if !exists("*mkdir")
293 redraw!
294 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
295 let &report= repkeep
296 return
297 endif
298
299 let curdir= getcwd()
300 let tmpdir= tempname()
301 let b:curdir= tmpdir
302 let b:tmpdir= curdir
303 if tmpdir =~ '\.'
304 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
305 endif
306 call mkdir(tmpdir,"p")
307
308 " attempt to change to the indicated directory
309 try
310 exe "cd ".fnameescape(tmpdir)
311 catch /^Vim\%((\a\+)\)\=:E344/
312 redraw!
313 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
314 let &report= repkeep
315 return
316 endtry
317
318 " place temporary files under .../_ZIPVIM_/
319 if isdirectory("_ZIPVIM_")
320 call s:Rmdir("_ZIPVIM_")
321 endif
322 call mkdir("_ZIPVIM_")
323 cd _ZIPVIM_
324
Bram Moolenaarff034192013-04-24 18:51:19 +0200325 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000326 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100327 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000329
Bram Moolenaar5c736222010-01-06 20:54:52 +0100330 if fname =~ '\.bz2$' && executable("bzcat")
331 let decmp= "|bzcat"
332 let doro = 1
Jim Zhou56957ed2025-02-28 18:06:14 +0100333 elseif fname =~ '\.bz3$' && executable("bz3cat")
334 let decmp= "|bz3cat"
335 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100336 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000337 let decmp= "|zcat"
338 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100339 elseif fname =~ '\.lzma$' && executable("lzcat")
340 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000341 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200342 elseif fname =~ '\.xz$' && executable("xzcat")
343 let decmp= "|xzcat"
344 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100345 elseif fname =~ '\.zst$' && executable("zstdcat")
346 let decmp= "|zstdcat"
347 let doro = 1
Corpulent Robinb69cd522025-02-06 21:10:49 +0100348 elseif fname =~ '\.lz4$' && executable("lz4cat")
349 let decmp= "|lz4cat"
350 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000351 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000352 let decmp=""
353 let doro = 0
Jim Zhou56957ed2025-02-28 18:06:14 +0100354 if fname =~ '\.bz2$\|\.bz3$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355 setlocal bin
356 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000357 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000359 if exists("g:tar_secure")
360 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000362 let tar_secure= " "
363 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200364
Bram Moolenaar5c736222010-01-06 20:54:52 +0100365 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200366 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100367 exe "read ".escape_file
Christian Brabandt8ac975d2025-03-01 17:13:40 +0100368 elseif tarfile =~# '\.bz3$'
Jim Zhou56957ed2025-02-28 18:06:14 +0100369 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100370 exe "read ".escape_file
Bram Moolenaar29634562020-01-09 21:46:04 +0100371 elseif tarfile =~# '\.\(gz\)$'
372 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100373 exe "read ".escape_file
Bram Moolenaar29634562020-01-09 21:46:04 +0100374 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
375 if has("unix") && executable("file")
376 let filekind= system("file ".shellescape(tarfile,1))
377 else
378 let filekind= ""
379 endif
380 if filekind =~ "bzip2"
381 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100382 exe "read ".escape_file
Jim Zhou56957ed2025-02-28 18:06:14 +0100383 elseif filekind =~ "bzip3"
384 exe "sil! r! bzip3 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100385 exe "read ".escape_file
Bram Moolenaar29634562020-01-09 21:46:04 +0100386 elseif filekind =~ "XZ"
387 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100388 exe "read ".escape_file
Bram Moolenaar23515b42020-11-29 14:36:24 +0100389 elseif filekind =~ "Zstandard"
390 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100391 exe "read ".escape_file
Bram Moolenaar29634562020-01-09 21:46:04 +0100392 else
393 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100394 exe "read ".escape_file
Bram Moolenaar29634562020-01-09 21:46:04 +0100395 endif
396
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000397 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200398 exe "sil! r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100399 exe "read ".escape_file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100400 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200401 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100402 exe "read ".escape_file
Bram Moolenaar477db062010-07-28 18:17:41 +0200403 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200404 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100405 exe "read ".escape_file
Corpulent Robinb69cd522025-02-06 21:10:49 +0100406 elseif tarfile =~# '\.\(lz4\|tlz4\)$'
407 exe "sil! r! lz4 --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100408 exe "read ".escape_file
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000409 else
410 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100411 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000412 let tarfile = substitute(tarfile, '-', './-', '')
413 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100414 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Christian Brabandt334a13b2025-03-02 19:33:51 +0100415 exe "read ".escape_file
Lennart00129a8442024-11-11 22:39:30 +0100416 endif
417
418 redraw!
419
420if v:shell_error != 0
421 cd ..
422 call s:Rmdir("_ZIPVIM_")
423 exe "cd ".fnameescape(curdir)
424 echohl Error | echo "***error*** (tar#Read) sorry, unable to open or extract ".tarfile." with ".fname | echohl None
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000425 endif
426
427 if doro
428 " because the reverse process of compressing changed files back into the tarball is not currently supported
429 setlocal ro
430 endif
431
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200432 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000433 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000434
435 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200436 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000437 set nomod
438
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000439 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000440endfun
441
442" ---------------------------------------------------------------------
443" tar#Write: {{{2
444fun! tar#Write(fname)
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000445 let repkeep= &report
446 set report=10
Lennart00129a8442024-11-11 22:39:30 +0100447 " temporary buffer variable workaround because too fucking tired. but it works now
448 let curdir= b:curdir
449 let tmpdir= b:tmpdir
Bram Moolenaara5792f52005-11-23 21:25:05 +0000450
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000451 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
452 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100453 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000454 return
455 endif
456
Bram Moolenaarab194812005-09-14 21:40:12 +0000457 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000458 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000459 redraw!
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000460 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000461 return
462 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000463
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200464 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
465 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000466
467 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100468 if tarfile =~# '\.bz2'
469 call system("bzip2 -d -- ".shellescape(tarfile,0))
470 let tarfile = substitute(tarfile,'\.bz2','','e')
471 let compress= "bzip2 -- ".shellescape(tarfile,0)
Jim Zhou56957ed2025-02-28 18:06:14 +0100472 elseif tarfile =~# '\.bz3'
473 call system("bzip3 -d -- ".shellescape(tarfile,0))
474 let tarfile = substitute(tarfile,'\.bz3','','e')
475 let compress= "bzip3 -- ".shellescape(tarfile,0)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100476 elseif tarfile =~# '\.gz'
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,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100479 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000480 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100481 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000482 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100483 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000484 let tgz = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200485 elseif tarfile =~# '\.xz'
486 call system("xz -d -- ".shellescape(tarfile,0))
487 let tarfile = substitute(tarfile,'\.xz','','e')
488 let compress= "xz -- ".shellescape(tarfile,0)
Bram Moolenaar23515b42020-11-29 14:36:24 +0100489 elseif tarfile =~# '\.zst'
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100490 call system("zstd --decompress --rm -- ".shellescape(tarfile,0))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100491 let tarfile = substitute(tarfile,'\.zst','','e')
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100492 let compress= "zstd --rm -- ".shellescape(tarfile,0)
Corpulent Robinb69cd522025-02-06 21:10:49 +0100493 elseif tarfile =~# '\.lz4'
494 call system("lz4 --decompress --rm -- ".shellescape(tarfile,0))
495 let tarfile = substitute(tarfile,'\.lz4','','e')
496 let compress= "lz4 --rm -- ".shellescape(tarfile,0)
Bram Moolenaar477db062010-07-28 18:17:41 +0200497 elseif tarfile =~# '\.lzma'
498 call system("lzma -d -- ".shellescape(tarfile,0))
499 let tarfile = substitute(tarfile,'\.lzma','','e')
500 let compress= "lzma -- ".shellescape(tarfile,0)
Bram Moolenaarab194812005-09-14 21:40:12 +0000501 endif
502
Bram Moolenaarab194812005-09-14 21:40:12 +0000503 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000504 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000505 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000506 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000507
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000508 if fname =~ '/'
509 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200510 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100511 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000512 endif
513 call mkdir(dirpath,"p")
514 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000515 if tarfile !~ '/'
516 let tarfile= curdir.'/'.tarfile
517 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000518 if tarfile =~ '^\s*-'
519 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
520 let tarfile = substitute(tarfile, '-', './-', '')
521 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100522
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000523 if exists("g:tar_secure")
524 let tar_secure= " -- "
525 else
526 let tar_secure= " "
527 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200529 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100530 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000531 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100532
Bram Moolenaara5792f52005-11-23 21:25:05 +0000533 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100534 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000535 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000536 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000538 else
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100539
540 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100541 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000542 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000543 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000545 elseif exists("compress")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000546 call system(compress)
547 if exists("tgz")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000548 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
549 endif
550 endif
551 endif
552
553 " support writing tarfiles across a network
554 if s:tblfile_{winnr()} =~ '^\a\+://'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000555 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100556 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100557 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000558 let eikeep = &ei
559 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100560 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000561 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100562 let &ei = eikeep
563 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000564 q!
565 unlet s:tblfile_{winnr()}
566 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000567 endif
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100568
Bram Moolenaarab194812005-09-14 21:40:12 +0000569 " cleanup and restore current directory
570 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000571 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000572 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000573 setlocal nomod
574
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000575 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000576endfun
577
578" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200579" tar#Diff: {{{2
580fun! tar#Diff(userfname,fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200581 let fname= a:fname
582 if a:userfname != ""
583 let fname= a:userfname
584 endif
585 if filereadable(fname)
586 " sets current file (from tarball) for diff'ing
587 " splits window vertically
588 " opens original file, sets it for diff'ing
589 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
590 diffthis
591 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100592 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200593 diffthis
594 else
595 redraw!
596 echo "***warning*** unable to read file<".fname.">"
597 endif
Bram Moolenaarff034192013-04-24 18:51:19 +0200598endfun
599
600" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100601" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
602fun! tar#Extract()
Bram Moolenaar29634562020-01-09 21:46:04 +0100603
604 let repkeep= &report
605 set report=10
606 let fname= getline(".")
Bram Moolenaar29634562020-01-09 21:46:04 +0100607
608 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
609 redraw!
610 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaar29634562020-01-09 21:46:04 +0100611 return
612 endif
613
614 " sanity check
615 if fname =~ '^"'
616 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100617 return
618 endif
619
620 let tarball = expand("%")
Bram Moolenaar29634562020-01-09 21:46:04 +0100621 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar29634562020-01-09 21:46:04 +0100622
623 let extractcmd= netrw#WinPath(g:tar_extractcmd)
624 if filereadable(tarbase.".tar")
Bram Moolenaar29634562020-01-09 21:46:04 +0100625 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
626 if v:shell_error != 0
627 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100628 else
629 echo "***note*** successfully extracted ".fname
630 endif
631
632 elseif filereadable(tarbase.".tgz")
633 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100634 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
635 if v:shell_error != 0
636 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100637 else
638 echo "***note*** successfully extracted ".fname
639 endif
640
641 elseif filereadable(tarbase.".tar.gz")
642 let extractcmd= substitute(extractcmd,"-","-z","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100643 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
644 if v:shell_error != 0
645 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100646 else
647 echo "***note*** successfully extracted ".fname
648 endif
649
650 elseif filereadable(tarbase.".tbz")
651 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100652 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
653 if v:shell_error != 0
654 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100655 else
656 echo "***note*** successfully extracted ".fname
657 endif
658
659 elseif filereadable(tarbase.".tar.bz2")
660 let extractcmd= substitute(extractcmd,"-","-j","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100661 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
662 if v:shell_error != 0
663 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100664 else
665 echo "***note*** successfully extracted ".fname
666 endif
667
Jim Zhou56957ed2025-02-28 18:06:14 +0100668 elseif filereadable(tarbase.".tar.bz3")
669 let extractcmd= substitute(extractcmd,"-","-j","")
670 call system(extractcmd." ".shellescape(tarbase).".tar.bz3 ".shellescape(fname))
671 if v:shell_error != 0
672 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz3 ".fname.": failed!" | echohl NONE
673 else
674 echo "***note*** successfully extracted ".fname
675 endif
676
Bram Moolenaar29634562020-01-09 21:46:04 +0100677 elseif filereadable(tarbase.".txz")
678 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100679 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
680 if v:shell_error != 0
681 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100682 else
683 echo "***note*** successfully extracted ".fname
684 endif
685
686 elseif filereadable(tarbase.".tar.xz")
687 let extractcmd= substitute(extractcmd,"-","-J","")
Bram Moolenaar29634562020-01-09 21:46:04 +0100688 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
689 if v:shell_error != 0
690 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
Bram Moolenaar29634562020-01-09 21:46:04 +0100691 else
692 echo "***note*** successfully extracted ".fname
693 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100694
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100695 elseif filereadable(tarbase.".tzst")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100696 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100697 call system(extractcmd." ".shellescape(tarbase).".tzst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100698 if v:shell_error != 0
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100699 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzst ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100700 else
701 echo "***note*** successfully extracted ".fname
702 endif
703
704 elseif filereadable(tarbase.".tar.zst")
705 let extractcmd= substitute(extractcmd,"-","--zstd","")
Christian Brabandt3a5b3df2024-01-08 20:02:14 +0100706 call system(extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname))
Bram Moolenaar23515b42020-11-29 14:36:24 +0100707 if v:shell_error != 0
708 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
Corpulent Robinb69cd522025-02-06 21:10:49 +0100709 else
710 echo "***note*** successfully extracted ".fname
711 endif
712
713 elseif filereadable(tarbase.".tlz4")
714 let extractcmd= substitute(extractcmd,"-","-I lz4","")
715 call system(extractcmd." ".shellescape(tarbase).".tlz4 ".shellescape(fname))
716 if v:shell_error != 0
717 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tlz4 ".fname.": failed!" | echohl NONE
718 else
719 echo "***note*** successfully extracted ".fname
720 endif
721
722 elseif filereadable(tarbase.".tar.lz4")
723 let extractcmd= substitute(extractcmd,"-","-I lz4","")
724 call system(extractcmd." ".shellescape(tarbase).".tar.lz4".shellescape(fname))
725 if v:shell_error != 0
726 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.lz4 ".fname.": failed!" | echohl NONE
Bram Moolenaar23515b42020-11-29 14:36:24 +0100727 else
728 echo "***note*** successfully extracted ".fname
729 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100730 endif
731
732 " restore option
733 let &report= repkeep
Bram Moolenaar29634562020-01-09 21:46:04 +0100734endfun
735
736" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100737" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000738fun! s:Rmdir(fname)
Bram Moolenaarab194812005-09-14 21:40:12 +0000739 if has("unix")
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 elseif has("win32") || has("win95") || has("win64") || has("win16")
742 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100743 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000744 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100745 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000746 endif
747 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000748endfun
749
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100751" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
752fun! tar#Vimuntar(...)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100753 let tarball = expand("%")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100754 let tarbase = substitute(tarball,'\..*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100755 let tarhome = expand("%:p")
756 if has("win32") || has("win95") || has("win64") || has("win16")
757 let tarhome= substitute(tarhome,'\\','/','g')
758 endif
759 let tarhome= substitute(tarhome,'/[^/]*$','','')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100760 let tartail = expand("%:t")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100761 let curdir = getcwd()
Bram Moolenaar5c736222010-01-06 20:54:52 +0100762 " set up vimhome
763 if a:0 > 0 && a:1 != ""
764 let vimhome= a:1
765 else
766 let vimhome= vimball#VimballHome()
767 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100768
Bram Moolenaar5c736222010-01-06 20:54:52 +0100769 if simplify(curdir) != simplify(vimhome)
770 " copy (possibly compressed) tarball to .vim/vimfiles
Bram Moolenaar5c736222010-01-06 20:54:52 +0100771 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100772 exe "cd ".fnameescape(vimhome)
773 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100774
775 " if necessary, decompress the tarball; then, extract it
776 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100777 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100778 silent exe "!gunzip ".shellescape(tartail)
779 elseif executable("gzip")
780 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000781 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200782 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100783 if simplify(curdir) != simplify(tarhome)
784 " remove decompressed tarball, restore directory
Bram Moolenaar5c736222010-01-06 20:54:52 +0100785 call delete(tartail.".tar")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100786 exe "cd ".fnameescape(curdir)
787 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100788 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000789 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000790 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100791 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000792 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100793 let extractcmd= netrw#WinPath(g:tar_extractcmd)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100794 call system(extractcmd." ".shellescape(tarbase.".tar"))
795
796 " set up help
797 if filereadable("doc/".tarbase.".txt")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100798 exe "helptags ".getcwd()."/doc"
799 endif
800
801 if simplify(tarhome) != simplify(vimhome)
802 " remove decompressed tarball, restore directory
803 call delete(vimhome."/".tarbase.".tar")
804 exe "cd ".fnameescape(curdir)
805 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000806endfun
807
Bram Moolenaar5c736222010-01-06 20:54:52 +0100808" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000809" Modelines And Restoration: {{{1
810let &cpo= s:keepcpo
811unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000812" vim:ts=8 fdm=marker