blob: 52369a42c1c1bfd016c56eb53e62ef815efe1e28 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Christian Brabandt3d372312023-11-05 17:44:05 +01003" Date: Nov 05, 2023
4" Version: 32a (with modifications from the Vim Project)
Bram Moolenaar29634562020-01-09 21:46:04 +01005" Maintainer: Charles E Campbell <NcampObell@SdrPchip.AorgM-NOSPAM>
6" License: Vim License (see vim's :help license)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007"
Bram Moolenaara5792f52005-11-23 21:25:05 +00008" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009"
Bram Moolenaar29634562020-01-09 21:46:04 +010010" Copyright: Copyright (C) 2005-2017 Charles E. Campbell {{{1
Bram Moolenaara5792f52005-11-23 21:25:05 +000011" Permission is hereby granted to use and distribute this code,
12" with or without modifications, provided that this copyright
13" notice is copied with it. Like anything else that's free,
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014" tar.vim and tarPlugin.vim are provided *as is* and comes
15" with no warranty of any kind, either expressed or implied.
16" By using this plugin, you agree that in no event will the
17" copyright holder be liable for any damages resulting from
18" the use of this software.
Bram Moolenaar5c736222010-01-06 20:54:52 +010019" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +000020" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000021" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010022if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000023 finish
24endif
Christian Brabandt3d372312023-11-05 17:44:05 +010025let g:loaded_tar= "v32a"
Bram Moolenaar5c736222010-01-06 20:54:52 +010026if v:version < 702
27 echohl WarningMsg
28 echo "***warning*** this version of tar needs vim 7.2"
29 echohl Normal
30 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000031endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010032let s:keepcpo= &cpo
33set cpo&vim
Bram Moolenaarff034192013-04-24 18:51:19 +020034"DechoTabOn
Bram Moolenaar5c736222010-01-06 20:54:52 +010035"call Decho("loading autoload/tar.vim")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000036
Bram Moolenaara5792f52005-11-23 21:25:05 +000037" ---------------------------------------------------------------------
38" Default Settings: {{{1
39if !exists("g:tar_browseoptions")
40 let g:tar_browseoptions= "Ptf"
41endif
42if !exists("g:tar_readoptions")
43 let g:tar_readoptions= "OPxf"
44endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045if !exists("g:tar_cmd")
46 let g:tar_cmd= "tar"
47endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000048if !exists("g:tar_writeoptions")
49 let g:tar_writeoptions= "uf"
50endif
Bram Moolenaar29634562020-01-09 21:46:04 +010051if !exists("g:tar_delfile")
52 let g:tar_delfile="--delete -f"
53endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020054if !exists("g:netrw_cygwin")
55 if has("win32") || has("win95") || has("win64") || has("win16")
56 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
57 let g:netrw_cygwin= 1
58 else
59 let g:netrw_cygwin= 0
60 endif
61 else
62 let g:netrw_cygwin= 0
63 endif
64endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010065if !exists("g:tar_copycmd")
66 if !exists("g:netrw_localcopycmd")
67 if has("win32") || has("win95") || has("win64") || has("win16")
68 if g:netrw_cygwin
69 let g:netrw_localcopycmd= "cp"
70 else
71 let g:netrw_localcopycmd= "copy"
72 endif
73 elseif has("unix") || has("macunix")
74 let g:netrw_localcopycmd= "cp"
75 else
76 let g:netrw_localcopycmd= ""
77 endif
78 endif
79 let g:tar_copycmd= g:netrw_localcopycmd
80endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010081if !exists("g:tar_extractcmd")
82 let g:tar_extractcmd= "tar -xf"
83endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000084
Bram Moolenaar8c8de832008-06-24 22:58:06 +000085" set up shell quoting character
86if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020087 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000088 let g:tar_shq= &shq
89 elseif has("win32") || has("win95") || has("win64") || has("win16")
90 if exists("g:netrw_cygwin") && g:netrw_cygwin
91 let g:tar_shq= "'"
92 else
93 let g:tar_shq= '"'
94 endif
95 else
96 let g:tar_shq= "'"
97 endif
98" call Decho("g:tar_shq<".g:tar_shq.">")
99endif
100
Bram Moolenaara5792f52005-11-23 21:25:05 +0000101" ----------------
102" Functions: {{{1
103" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000104
Bram Moolenaara5792f52005-11-23 21:25:05 +0000105" ---------------------------------------------------------------------
106" tar#Browse: {{{2
107fun! tar#Browse(tarfile)
108" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000109 let repkeep= &report
110 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000111
Bram Moolenaara5792f52005-11-23 21:25:05 +0000112 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000113 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000114 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100115" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
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" call Dret("tar#Browse")
119 return
120 endif
121 if !filereadable(a:tarfile)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000122" call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000123 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 Moolenaar29634562020-01-09 21:46:04 +0100126" call Decho("***error*** (tar#Browse) File not readable<".a:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000127 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000128 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000129 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000130" call Dret("tar#Browse : file<".a:tarfile."> not readable")
131 return
132 endif
133 if &ma != 1
134 set ma
135 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200136 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000137
Bram Moolenaara5792f52005-11-23 21:25:05 +0000138 setlocal noswapfile
139 setlocal buftype=nofile
140 setlocal bufhidden=hide
141 setlocal nobuflisted
142 setlocal nowrap
143 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000144
Bram Moolenaara5792f52005-11-23 21:25:05 +0000145 " give header
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000146" call Decho("printing header")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000147 let lastline= line("$")
148 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
149 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
150 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200151 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100152 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200153 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000154
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000155 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200156 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000157 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100158 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000159 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000160 let curlast= line("$")
Bram Moolenaar29634562020-01-09 21:46:04 +0100161
162 if tarfile =~# '\.\(gz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100163" call Decho("1: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar29634562020-01-09 21:46:04 +0100164 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
165
Bram Moolenaar23515b42020-11-29 14:36:24 +0100166 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' || tarfile =~# '\.\(tzs\)$'
Bram Moolenaar29634562020-01-09 21:46:04 +0100167 if has("unix") && executable("file")
168 let filekind= system("file ".shellescape(tarfile,1)) =~ "bzip2"
169 else
170 let filekind= ""
171 endif
172
173 if filekind =~ "bzip2"
174 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
175 elseif filekind =~ "XZ"
176 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100177 elseif filekind =~ "Zstandard"
178 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar29634562020-01-09 21:46:04 +0100179 else
180 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
181 endif
182
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 elseif tarfile =~# '\.lrp'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100184" call Decho("2: exe silent r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaard4a1aab2018-09-08 15:10:34 +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 Moolenaar5c736222010-01-06 20:54:52 +0100187" call Decho("3: exe silent r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200188 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100189 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100190" call Decho("3: exe silent r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +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\)$'
193" call Decho("3: exe silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200194 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar23515b42020-11-29 14:36:24 +0100195 elseif tarfile =~# '\.\(zst\|tzs\)$'
196 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000197 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000198 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100199 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000200 let tarfile = substitute(tarfile, '-', './-', '')
201 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100202" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
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" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000209 return
210 endif
Christian Brabandt3d372312023-11-05 17:44:05 +0100211 " If there was an error message, the last line probably matches some keywords but
212 " should also contain whitespace for readability. Make sure not to match a
213 " filename that contains the keyword (error/warning/unrecognized/inappropriate, etc)
214 "
215 " FIXME:is this actually necessary? In case of an error, we should probably
216 " have noticed in the if statement above since tar should have exited
217 " with a non-zero exit code.
218 if line("$") == curlast || ( line("$") == (curlast + 1) &&
219 \ getline("$") =~# '\c\<\%(warning\|error\|inappropriate\|unrecognized\)\>' &&
220 \ getline("$") =~ '\s' )
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000221 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000222 echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
Bram Moolenaar251e1912011-06-19 05:09:16 +0200223 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000224 let eikeep= &ei
225 set ei=BufReadCmd,FileReadCmd
Bram Moolenaarc236c162008-07-13 17:41:49 +0000226 exe "r ".fnameescape(a:tarfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000227 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200228 keepj sil! 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000229" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000230 return
231 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000232
Bram Moolenaar29634562020-01-09 21:46:04 +0100233 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000234 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100235 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
236 noremap <silent> <buffer> x :call tar#Extract()<cr>
237 if &mouse != ""
238 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
239 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000240
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000241 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200242" call Dret("tar#Browse : b:tarfile<".b:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000243endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000244
Bram Moolenaarab194812005-09-14 21:40:12 +0000245" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000246" TarBrowseSelect: {{{2
247fun! s:TarBrowseSelect()
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200248" call Dfunc("TarBrowseSelect() b:tarfile<".b:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000249 let repkeep= &report
250 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000251 let fname= getline(".")
252" call Decho("fname<".fname.">")
253
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000254 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
255 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100256 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000257" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
258 return
259 endif
260
Bram Moolenaara5792f52005-11-23 21:25:05 +0000261 " sanity check
262 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000263 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000264" call Dret("TarBrowseSelect")
265 return
266 endif
267
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200268 " about to make a new window, need to use b:tarfile
269 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000270 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200271 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000272 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100273 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000274 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000275
Bram Moolenaar29634562020-01-09 21:46:04 +0100276 " open a new window (tar#Read will read a file into it)
277 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000278 if !exists("g:tar_nomax") || g:tar_nomax == 0
279 wincmd _
280 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000281 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000282 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000283 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200284 set nomod
285 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000286
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000287 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000288" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
289endfun
290
291" ---------------------------------------------------------------------
292" tar#Read: {{{2
293fun! tar#Read(fname,mode)
294" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000295 let repkeep= &report
296 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000297 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
298 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200299 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000300 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100301 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000302 endif
303" call Decho("tarfile<".tarfile.">")
304" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000305
Bram Moolenaar5c736222010-01-06 20:54:52 +0100306 if fname =~ '\.bz2$' && executable("bzcat")
307 let decmp= "|bzcat"
308 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100309 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000310 let decmp= "|zcat"
311 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100312 elseif fname =~ '\.lzma$' && executable("lzcat")
313 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000314 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200315 elseif fname =~ '\.xz$' && executable("xzcat")
316 let decmp= "|xzcat"
317 let doro = 1
Bram Moolenaar23515b42020-11-29 14:36:24 +0100318 elseif fname =~ '\.zst$' && executable("zstdcat")
319 let decmp= "|zstdcat"
320 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000321 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000322 let decmp=""
323 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200324 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000325 setlocal bin
326 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000327 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000328
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000329 if exists("g:tar_secure")
330 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000331 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000332 let tar_secure= " "
333 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200334
Bram Moolenaar5c736222010-01-06 20:54:52 +0100335 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200336 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar29634562020-01-09 21:46:04 +0100337 elseif tarfile =~# '\.\(gz\)$'
338 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
339
340 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
341 if has("unix") && executable("file")
342 let filekind= system("file ".shellescape(tarfile,1))
343 else
344 let filekind= ""
345 endif
346 if filekind =~ "bzip2"
347 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
348 elseif filekind =~ "XZ"
349 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar23515b42020-11-29 14:36:24 +0100350 elseif filekind =~ "Zstandard"
351 exe "sil! r! zstd --decompress --stdout -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar29634562020-01-09 21:46:04 +0100352 else
353 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
354 endif
355
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000356 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200357 exe "sil! r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar5c736222010-01-06 20:54:52 +0100358 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200359 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar477db062010-07-28 18:17:41 +0200360 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200361 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000362 else
363 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100364 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000365 let tarfile = substitute(tarfile, '-', './-', '')
366 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100367" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
368 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369 endif
370
371 if doro
372 " because the reverse process of compressing changed files back into the tarball is not currently supported
373 setlocal ro
374 endif
375
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200376 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000377 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000378
379 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200380 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000381 set nomod
382
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000383 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200384" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000385endfun
386
387" ---------------------------------------------------------------------
388" tar#Write: {{{2
389fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200390" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000391 let repkeep= &report
392 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000393
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000394 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
395 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100396 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000397" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
398 return
399 endif
400
Bram Moolenaarab194812005-09-14 21:40:12 +0000401 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000403 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100404" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000405 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000406" call Dret("tar#Write")
407 return
408 endif
409 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000410 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100411" call Decho("***error*** (tar#Write) sorry, mkdir() doesn't work on your system")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000412 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000413 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000414" call Dret("tar#Write")
415 return
416 endif
417
418 let curdir= getcwd()
419 let tmpdir= tempname()
420" call Decho("orig tempname<".tmpdir.">")
421 if tmpdir =~ '\.'
422 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
423 endif
424" call Decho("tmpdir<".tmpdir.">")
425 call mkdir(tmpdir,"p")
426
427 " attempt to change to the indicated directory
428 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000429 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000430 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000431 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100432" call Decho("***error*** (tar#Write) cannot cd to temporary directory")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000433 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000434 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000435" call Dret("tar#Write")
436 return
437 endtry
438" call Decho("current directory now: ".getcwd())
439
Bram Moolenaara5792f52005-11-23 21:25:05 +0000440 " place temporary files under .../_ZIPVIM_/
441 if isdirectory("_ZIPVIM_")
442 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000443 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000444 call mkdir("_ZIPVIM_")
445 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000446" call Decho("current directory now: ".getcwd())
447
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200448 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
449 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000450
451 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100452 if tarfile =~# '\.bz2'
453 call system("bzip2 -d -- ".shellescape(tarfile,0))
454 let tarfile = substitute(tarfile,'\.bz2','','e')
455 let compress= "bzip2 -- ".shellescape(tarfile,0)
456" call Decho("compress<".compress.">")
457 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100458 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000459 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100460 let compress= "gzip -- ".shellescape(tarfile,0)
461" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000462 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100463 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000464 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100465 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000466 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000467" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200468 elseif tarfile =~# '\.xz'
469 call system("xz -d -- ".shellescape(tarfile,0))
470 let tarfile = substitute(tarfile,'\.xz','','e')
471 let compress= "xz -- ".shellescape(tarfile,0)
472" call Decho("compress<".compress.">")
Bram Moolenaar23515b42020-11-29 14:36:24 +0100473 elseif tarfile =~# '\.zst'
474 call system("zstd --decompress -- ".shellescape(tarfile,0))
475 let tarfile = substitute(tarfile,'\.zst','','e')
476 let compress= "zstd -- ".shellescape(tarfile,0)
Bram Moolenaar477db062010-07-28 18:17:41 +0200477 elseif tarfile =~# '\.lzma'
478 call system("lzma -d -- ".shellescape(tarfile,0))
479 let tarfile = substitute(tarfile,'\.lzma','','e')
480 let compress= "lzma -- ".shellescape(tarfile,0)
481" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000482 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000484
Bram Moolenaarab194812005-09-14 21:40:12 +0000485 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000486 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100487" call Decho("***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000488 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000489 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000490
491" call Decho("tarfile<".tarfile."> fname<".fname.">")
492
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000493 if fname =~ '/'
494 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200495 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100496 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000497 endif
498 call mkdir(dirpath,"p")
499 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000500 if tarfile !~ '/'
501 let tarfile= curdir.'/'.tarfile
502 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000503 if tarfile =~ '^\s*-'
504 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
505 let tarfile = substitute(tarfile, '-', './-', '')
506 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000507" call Decho("tarfile<".tarfile."> fname<".fname.">")
508
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000509 if exists("g:tar_secure")
510 let tar_secure= " -- "
511 else
512 let tar_secure= " "
513 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200515 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100516 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000517 endif
518
519 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100520" call Decho("system(".g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
521 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000522 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000523 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100524" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000525 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000526 else
527
528 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100529" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
530 call system(g:tar_cmd." -".g:tar_writeoptions." ".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 Moolenaar29634562020-01-09 21:46:04 +0100533" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000535 elseif exists("compress")
536" call Decho("call system(".compress.")")
537 call system(compress)
538 if exists("tgz")
539" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
540 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
541 endif
542 endif
543 endif
544
545 " support writing tarfiles across a network
546 if s:tblfile_{winnr()} =~ '^\a\+://'
547" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
548 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100549 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100550 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000551 let eikeep = &ei
552 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100553 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000554 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100555 let &ei = eikeep
556 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000557 q!
558 unlet s:tblfile_{winnr()}
559 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000560 endif
561
562 " cleanup and restore current directory
563 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000564 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000565 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000566 setlocal nomod
567
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000568 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000569" call Dret("tar#Write")
570endfun
571
572" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200573" tar#Diff: {{{2
574fun! tar#Diff(userfname,fname)
575" call Dfunc("tar#Diff(userfname<".a:userfname."> fname<".a:fname.")")
576 let fname= a:fname
577 if a:userfname != ""
578 let fname= a:userfname
579 endif
580 if filereadable(fname)
581 " sets current file (from tarball) for diff'ing
582 " splits window vertically
583 " opens original file, sets it for diff'ing
584 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
585 diffthis
586 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100587 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200588 diffthis
589 else
590 redraw!
591 echo "***warning*** unable to read file<".fname.">"
592 endif
593" call Dret("tar#Diff")
594endfun
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()
599" call Dfunc("tar#Extract()")
600
601 let repkeep= &report
602 set report=10
603 let fname= getline(".")
604" call Decho("fname<".fname.">")
605
606 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
607 redraw!
608 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
609" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
610 return
611 endif
612
613 " sanity check
614 if fname =~ '^"'
615 let &report= repkeep
616" call Dret("TarBrowseSelect")
617 return
618 endif
619
620 let tarball = expand("%")
621" call Decho("tarball<".tarball.">")
622 let tarbase = substitute(tarball,'\..*$','','')
623" call Decho("tarbase<".tarbase.">")
624
625 let extractcmd= netrw#WinPath(g:tar_extractcmd)
626 if filereadable(tarbase.".tar")
627" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname).")")
628 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
629 if v:shell_error != 0
630 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
631" call Decho("***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!")
632 else
633 echo "***note*** successfully extracted ".fname
634 endif
635
636 elseif filereadable(tarbase.".tgz")
637 let extractcmd= substitute(extractcmd,"-","-z","")
638" call Decho("system(".extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname).")")
639 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
640 if v:shell_error != 0
641 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
642" call Decho("***error*** ".extractcmd."t ".tarbase.".tgz ".fname.": failed!")
643 else
644 echo "***note*** successfully extracted ".fname
645 endif
646
647 elseif filereadable(tarbase.".tar.gz")
648 let extractcmd= substitute(extractcmd,"-","-z","")
649" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname).")")
650 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
651 if v:shell_error != 0
652 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
653" call Decho("***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!")
654 else
655 echo "***note*** successfully extracted ".fname
656 endif
657
658 elseif filereadable(tarbase.".tbz")
659 let extractcmd= substitute(extractcmd,"-","-j","")
660" call Decho("system(".extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname).")")
661 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
662 if v:shell_error != 0
663 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
664" call Decho("***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!")
665 else
666 echo "***note*** successfully extracted ".fname
667 endif
668
669 elseif filereadable(tarbase.".tar.bz2")
670 let extractcmd= substitute(extractcmd,"-","-j","")
671" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname).")")
672 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
673 if v:shell_error != 0
674 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
675" call Decho("***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!")
676 else
677 echo "***note*** successfully extracted ".fname
678 endif
679
680 elseif filereadable(tarbase.".txz")
681 let extractcmd= substitute(extractcmd,"-","-J","")
682" call Decho("system(".extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname).")")
683 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
684 if v:shell_error != 0
685 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
686" call Decho("***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!")
687 else
688 echo "***note*** successfully extracted ".fname
689 endif
690
691 elseif filereadable(tarbase.".tar.xz")
692 let extractcmd= substitute(extractcmd,"-","-J","")
693" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname).")")
694 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
695 if v:shell_error != 0
696 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
697" call Decho("***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!")
698 else
699 echo "***note*** successfully extracted ".fname
700 endif
Bram Moolenaar23515b42020-11-29 14:36:24 +0100701
702 elseif filereadable(tarbase.".tzs")
703 let extractcmd= substitute(extractcmd,"-","--zstd","")
704" call Decho("system(".extractcmd." ".shellescape(tarbase).".tzs ".shellescape(fname).")")
705 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
706 if v:shell_error != 0
707 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tzs ".fname.": failed!" | echohl NONE
708" call Decho("***error*** ".extractcmd." ".tarbase.".tzs ".fname.": failed!")
709 else
710 echo "***note*** successfully extracted ".fname
711 endif
712
713 elseif filereadable(tarbase.".tar.zst")
714 let extractcmd= substitute(extractcmd,"-","--zstd","")
715" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.zst ".shellescape(fname).")")
716 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
717 if v:shell_error != 0
718 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!" | echohl NONE
719" call Decho("***error*** ".extractcmd." ".tarbase.".tar.zst ".fname.": failed!")
720 else
721 echo "***note*** successfully extracted ".fname
722 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100723 endif
724
725 " restore option
726 let &report= repkeep
727
728" call Dret("tar#Extract")
729endfun
730
731" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100732" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000733fun! s:Rmdir(fname)
734" call Dfunc("Rmdir(fname<".a:fname.">)")
735 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
744" call Dret("Rmdir")
745endfun
746
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000747" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100748" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
749fun! tar#Vimuntar(...)
750" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
751 let tarball = expand("%")
752" call Decho("tarball<".tarball.">")
753 let tarbase = substitute(tarball,'\..*$','','')
754" call Decho("tarbase<".tarbase.">")
755 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,'/[^/]*$','','')
760" call Decho("tarhome<".tarhome.">")
761 let tartail = expand("%:t")
762" call Decho("tartail<".tartail.">")
763 let curdir = getcwd()
764" call Decho("curdir <".curdir.">")
765 " set up vimhome
766 if a:0 > 0 && a:1 != ""
767 let vimhome= a:1
768 else
769 let vimhome= vimball#VimballHome()
770 endif
771" call Decho("vimhome<".vimhome.">")
772
773" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
774 if simplify(curdir) != simplify(vimhome)
775 " copy (possibly compressed) tarball to .vim/vimfiles
776" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
777 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
778" call Decho("exe cd ".fnameescape(vimhome))
779 exe "cd ".fnameescape(vimhome)
780 endif
781" call Decho("getcwd<".getcwd().">")
782
783 " if necessary, decompress the tarball; then, extract it
784 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100785 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100786 silent exe "!gunzip ".shellescape(tartail)
787 elseif executable("gzip")
788 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000789 else
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200790 echoerr "unable to decompress<".tartail."> on this system"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100791 if simplify(curdir) != simplify(tarhome)
792 " remove decompressed tarball, restore directory
793" call Decho("delete(".tartail.".tar)")
794 call delete(tartail.".tar")
795" call Decho("exe cd ".fnameescape(curdir))
796 exe "cd ".fnameescape(curdir)
797 endif
798" call Dret("tar#Vimuntar")
799 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000800 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100802 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000803 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100804 let extractcmd= netrw#WinPath(g:tar_extractcmd)
805" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
806 call system(extractcmd." ".shellescape(tarbase.".tar"))
807
808 " set up help
809 if filereadable("doc/".tarbase.".txt")
810" call Decho("exe helptags ".getcwd()."/doc")
811 exe "helptags ".getcwd()."/doc"
812 endif
813
814 if simplify(tarhome) != simplify(vimhome)
815 " remove decompressed tarball, restore directory
816 call delete(vimhome."/".tarbase.".tar")
817 exe "cd ".fnameescape(curdir)
818 endif
819
820" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000821endfun
822
Bram Moolenaar5c736222010-01-06 20:54:52 +0100823" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000824" Modelines And Restoration: {{{1
825let &cpo= s:keepcpo
826unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000827" vim:ts=8 fdm=marker