blob: 9f9609434feb77ac8d69fe6215e8611550eeda4c [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaar29634562020-01-09 21:46:04 +01003" Date: Jan 07, 2020
4" Version: 32
5" 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
Bram Moolenaar29634562020-01-09 21:46:04 +010025let g:loaded_tar= "v32"
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
166 elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$'
167 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." - "
177 else
178 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
179 endif
180
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 elseif tarfile =~# '\.lrp'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100182" 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 +0200183 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100184 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100185" 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 +0200186 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100187 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100188" 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 +0200189 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200190 elseif tarfile =~# '\.\(xz\|txz\)$'
191" 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 +0200192 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000193 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000194 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100195 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000196 let tarfile = substitute(tarfile, '-', './-', '')
197 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100198" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
Bram Moolenaar251e1912011-06-19 05:09:16 +0200199 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000200 endif
201 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000202 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000203 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000204" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000205 return
206 endif
Bram Moolenaar29634562020-01-09 21:46:04 +0100207 if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~# '\c\%(warning\|error\|inappropriate\|unrecognized\)')
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000208 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000209 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 +0200210 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000211 let eikeep= &ei
212 set ei=BufReadCmd,FileReadCmd
Bram Moolenaarc236c162008-07-13 17:41:49 +0000213 exe "r ".fnameescape(a:tarfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000214 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200215 keepj sil! 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000216" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000217 return
218 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000219
Bram Moolenaar29634562020-01-09 21:46:04 +0100220 " set up maps supported for tar
Bram Moolenaara5792f52005-11-23 21:25:05 +0000221 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100222 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
223 noremap <silent> <buffer> x :call tar#Extract()<cr>
224 if &mouse != ""
225 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>TarBrowseSelect()<cr>
226 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000227
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000228 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200229" call Dret("tar#Browse : b:tarfile<".b:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000230endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000231
Bram Moolenaarab194812005-09-14 21:40:12 +0000232" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000233" TarBrowseSelect: {{{2
234fun! s:TarBrowseSelect()
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200235" call Dfunc("TarBrowseSelect() b:tarfile<".b:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000236 let repkeep= &report
237 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000238 let fname= getline(".")
239" call Decho("fname<".fname.">")
240
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000241 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
242 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100243 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000244" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
245 return
246 endif
247
Bram Moolenaara5792f52005-11-23 21:25:05 +0000248 " sanity check
249 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000250 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000251" call Dret("TarBrowseSelect")
252 return
253 endif
254
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200255 " about to make a new window, need to use b:tarfile
256 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000257 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200258 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100260 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000261 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000262
Bram Moolenaar29634562020-01-09 21:46:04 +0100263 " open a new window (tar#Read will read a file into it)
264 noswapfile new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000265 if !exists("g:tar_nomax") || g:tar_nomax == 0
266 wincmd _
267 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000268 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000269 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000270 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200271 set nomod
272 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000273
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000274 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000275" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
276endfun
277
278" ---------------------------------------------------------------------
279" tar#Read: {{{2
280fun! tar#Read(fname,mode)
281" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000282 let repkeep= &report
283 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000284 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
285 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200286 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000287 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100288 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000289 endif
290" call Decho("tarfile<".tarfile.">")
291" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000292
Bram Moolenaar5c736222010-01-06 20:54:52 +0100293 if fname =~ '\.bz2$' && executable("bzcat")
294 let decmp= "|bzcat"
295 let doro = 1
Bram Moolenaar29634562020-01-09 21:46:04 +0100296 elseif fname =~ '\.t\=gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000297 let decmp= "|zcat"
298 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100299 elseif fname =~ '\.lzma$' && executable("lzcat")
300 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000301 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200302 elseif fname =~ '\.xz$' && executable("xzcat")
303 let decmp= "|xzcat"
304 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000305 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000306 let decmp=""
307 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200308 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000309 setlocal bin
310 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000311 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000312
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000313 if exists("g:tar_secure")
314 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000315 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000316 let tar_secure= " "
317 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200318
Bram Moolenaar5c736222010-01-06 20:54:52 +0100319 if tarfile =~# '\.bz2$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200320 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 +0100321 elseif tarfile =~# '\.\(gz\)$'
322 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
323
324 elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)'
325 if has("unix") && executable("file")
326 let filekind= system("file ".shellescape(tarfile,1))
327 else
328 let filekind= ""
329 endif
330 if filekind =~ "bzip2"
331 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
332 elseif filekind =~ "XZ"
333 exe "sil! r! xz -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
334 else
335 exe "sil! r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
336 endif
337
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000338 elseif tarfile =~# '\.lrp$'
Bram Moolenaard4a1aab2018-09-08 15:10:34 +0200339 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 +0100340 elseif tarfile =~# '\.lzma$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200341 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 +0200342 elseif tarfile =~# '\.\(xz\|txz\)$'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200343 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 +0000344 else
345 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100346 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000347 let tarfile = substitute(tarfile, '-', './-', '')
348 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100349" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
350 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 +0000351 endif
352
353 if doro
354 " because the reverse process of compressing changed files back into the tarball is not currently supported
355 setlocal ro
356 endif
357
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200358 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000359 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000360
361 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200362 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000363 set nomod
364
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000365 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200366" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000367endfun
368
369" ---------------------------------------------------------------------
370" tar#Write: {{{2
371fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200372" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000373 let repkeep= &report
374 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000375
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000376 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
377 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100378 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000379" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
380 return
381 endif
382
Bram Moolenaarab194812005-09-14 21:40:12 +0000383 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000384 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000385 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100386" call Decho('***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system')
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000387 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000388" call Dret("tar#Write")
389 return
390 endif
391 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000392 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100393" call Decho("***error*** (tar#Write) sorry, mkdir() doesn't work on your system")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000394 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000395 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000396" call Dret("tar#Write")
397 return
398 endif
399
400 let curdir= getcwd()
401 let tmpdir= tempname()
402" call Decho("orig tempname<".tmpdir.">")
403 if tmpdir =~ '\.'
404 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
405 endif
406" call Decho("tmpdir<".tmpdir.">")
407 call mkdir(tmpdir,"p")
408
409 " attempt to change to the indicated directory
410 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000411 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000412 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000413 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100414" call Decho("***error*** (tar#Write) cannot cd to temporary directory")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000415 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000416 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000417" call Dret("tar#Write")
418 return
419 endtry
420" call Decho("current directory now: ".getcwd())
421
Bram Moolenaara5792f52005-11-23 21:25:05 +0000422 " place temporary files under .../_ZIPVIM_/
423 if isdirectory("_ZIPVIM_")
424 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000425 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000426 call mkdir("_ZIPVIM_")
427 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000428" call Decho("current directory now: ".getcwd())
429
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200430 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
431 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000432
433 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100434 if tarfile =~# '\.bz2'
435 call system("bzip2 -d -- ".shellescape(tarfile,0))
436 let tarfile = substitute(tarfile,'\.bz2','','e')
437 let compress= "bzip2 -- ".shellescape(tarfile,0)
438" call Decho("compress<".compress.">")
439 elseif tarfile =~# '\.gz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100440 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000441 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100442 let compress= "gzip -- ".shellescape(tarfile,0)
443" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000444 elseif tarfile =~# '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100445 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000446 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100447 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000448 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000449" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200450 elseif tarfile =~# '\.xz'
451 call system("xz -d -- ".shellescape(tarfile,0))
452 let tarfile = substitute(tarfile,'\.xz','','e')
453 let compress= "xz -- ".shellescape(tarfile,0)
454" call Decho("compress<".compress.">")
455 elseif tarfile =~# '\.lzma'
456 call system("lzma -d -- ".shellescape(tarfile,0))
457 let tarfile = substitute(tarfile,'\.lzma','','e')
458 let compress= "lzma -- ".shellescape(tarfile,0)
459" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000460 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000462
Bram Moolenaarab194812005-09-14 21:40:12 +0000463 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000464 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100465" call Decho("***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000466 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000467 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000468
469" call Decho("tarfile<".tarfile."> fname<".fname.">")
470
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000471 if fname =~ '/'
472 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200473 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100474 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000475 endif
476 call mkdir(dirpath,"p")
477 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000478 if tarfile !~ '/'
479 let tarfile= curdir.'/'.tarfile
480 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000481 if tarfile =~ '^\s*-'
482 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
483 let tarfile = substitute(tarfile, '-', './-', '')
484 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000485" call Decho("tarfile<".tarfile."> fname<".fname.">")
486
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000487 if exists("g:tar_secure")
488 let tar_secure= " -- "
489 else
490 let tar_secure= " "
491 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200493 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100494 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000495 endif
496
497 " delete old file from tarfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100498" call Decho("system(".g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
499 call system(g:tar_cmd." ".g:tar_delfile." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000500 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000501 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100502" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000504 else
505
506 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100507" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
508 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000509 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000510 redraw!
Bram Moolenaar29634562020-01-09 21:46:04 +0100511" call Decho("***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname))
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000513 elseif exists("compress")
514" call Decho("call system(".compress.")")
515 call system(compress)
516 if exists("tgz")
517" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
518 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
519 endif
520 endif
521 endif
522
523 " support writing tarfiles across a network
524 if s:tblfile_{winnr()} =~ '^\a\+://'
525" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
526 let tblfile= s:tblfile_{winnr()}
Bram Moolenaar29634562020-01-09 21:46:04 +0100527 1split|noswapfile enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100528 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000529 let eikeep = &ei
530 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100531 exe "noswapfile e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000532 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100533 let &ei = eikeep
534 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000535 q!
536 unlet s:tblfile_{winnr()}
537 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000538 endif
539
540 " cleanup and restore current directory
541 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000542 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000543 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000544 setlocal nomod
545
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000546 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000547" call Dret("tar#Write")
548endfun
549
550" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200551" tar#Diff: {{{2
552fun! tar#Diff(userfname,fname)
553" call Dfunc("tar#Diff(userfname<".a:userfname."> fname<".a:fname.")")
554 let fname= a:fname
555 if a:userfname != ""
556 let fname= a:userfname
557 endif
558 if filereadable(fname)
559 " sets current file (from tarball) for diff'ing
560 " splits window vertically
561 " opens original file, sets it for diff'ing
562 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
563 diffthis
564 wincmd v
Bram Moolenaar29634562020-01-09 21:46:04 +0100565 exe "noswapfile e ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200566 diffthis
567 else
568 redraw!
569 echo "***warning*** unable to read file<".fname.">"
570 endif
571" call Dret("tar#Diff")
572endfun
573
574" ---------------------------------------------------------------------
Bram Moolenaar29634562020-01-09 21:46:04 +0100575" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2
576fun! tar#Extract()
577" call Dfunc("tar#Extract()")
578
579 let repkeep= &report
580 set report=10
581 let fname= getline(".")
582" call Decho("fname<".fname.">")
583
584 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
585 redraw!
586 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
587" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
588 return
589 endif
590
591 " sanity check
592 if fname =~ '^"'
593 let &report= repkeep
594" call Dret("TarBrowseSelect")
595 return
596 endif
597
598 let tarball = expand("%")
599" call Decho("tarball<".tarball.">")
600 let tarbase = substitute(tarball,'\..*$','','')
601" call Decho("tarbase<".tarbase.">")
602
603 let extractcmd= netrw#WinPath(g:tar_extractcmd)
604 if filereadable(tarbase.".tar")
605" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname).")")
606 call system(extractcmd." ".shellescape(tarbase).".tar ".shellescape(fname))
607 if v:shell_error != 0
608 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!" | echohl NONE
609" call Decho("***error*** ".extractcmd." ".tarbase.".tar ".fname.": failed!")
610 else
611 echo "***note*** successfully extracted ".fname
612 endif
613
614 elseif filereadable(tarbase.".tgz")
615 let extractcmd= substitute(extractcmd,"-","-z","")
616" call Decho("system(".extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname).")")
617 call system(extractcmd." ".shellescape(tarbase).".tgz ".shellescape(fname))
618 if v:shell_error != 0
619 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tgz ".fname.": failed!" | echohl NONE
620" call Decho("***error*** ".extractcmd."t ".tarbase.".tgz ".fname.": failed!")
621 else
622 echo "***note*** successfully extracted ".fname
623 endif
624
625 elseif filereadable(tarbase.".tar.gz")
626 let extractcmd= substitute(extractcmd,"-","-z","")
627" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname).")")
628 call system(extractcmd." ".shellescape(tarbase).".tar.gz ".shellescape(fname))
629 if v:shell_error != 0
630 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!" | echohl NONE
631" call Decho("***error*** ".extractcmd." ".tarbase.".tar.gz ".fname.": failed!")
632 else
633 echo "***note*** successfully extracted ".fname
634 endif
635
636 elseif filereadable(tarbase.".tbz")
637 let extractcmd= substitute(extractcmd,"-","-j","")
638" call Decho("system(".extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname).")")
639 call system(extractcmd." ".shellescape(tarbase).".tbz ".shellescape(fname))
640 if v:shell_error != 0
641 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!" | echohl NONE
642" call Decho("***error*** ".extractcmd."j ".tarbase.".tbz ".fname.": failed!")
643 else
644 echo "***note*** successfully extracted ".fname
645 endif
646
647 elseif filereadable(tarbase.".tar.bz2")
648 let extractcmd= substitute(extractcmd,"-","-j","")
649" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname).")")
650 call system(extractcmd." ".shellescape(tarbase).".tar.bz2 ".shellescape(fname))
651 if v:shell_error != 0
652 echohl Error | echo "***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!" | echohl NONE
653" call Decho("***error*** ".extractcmd."j ".tarbase.".tar.bz2 ".fname.": failed!")
654 else
655 echo "***note*** successfully extracted ".fname
656 endif
657
658 elseif filereadable(tarbase.".txz")
659 let extractcmd= substitute(extractcmd,"-","-J","")
660" call Decho("system(".extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname).")")
661 call system(extractcmd." ".shellescape(tarbase).".txz ".shellescape(fname))
662 if v:shell_error != 0
663 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!" | echohl NONE
664" call Decho("***error*** ".extractcmd." ".tarbase.".txz ".fname.": failed!")
665 else
666 echo "***note*** successfully extracted ".fname
667 endif
668
669 elseif filereadable(tarbase.".tar.xz")
670 let extractcmd= substitute(extractcmd,"-","-J","")
671" call Decho("system(".extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname).")")
672 call system(extractcmd." ".shellescape(tarbase).".tar.xz ".shellescape(fname))
673 if v:shell_error != 0
674 echohl Error | echo "***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!" | echohl NONE
675" call Decho("***error*** ".extractcmd." ".tarbase.".tar.xz ".fname.": failed!")
676 else
677 echo "***note*** successfully extracted ".fname
678 endif
679 endif
680
681 " restore option
682 let &report= repkeep
683
684" call Dret("tar#Extract")
685endfun
686
687" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100688" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000689fun! s:Rmdir(fname)
690" call Dfunc("Rmdir(fname<".a:fname.">)")
691 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100692 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000693 elseif has("win32") || has("win95") || has("win64") || has("win16")
694 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100695 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000696 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100697 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000698 endif
699 endif
700" call Dret("Rmdir")
701endfun
702
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100704" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
705fun! tar#Vimuntar(...)
706" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
707 let tarball = expand("%")
708" call Decho("tarball<".tarball.">")
709 let tarbase = substitute(tarball,'\..*$','','')
710" call Decho("tarbase<".tarbase.">")
711 let tarhome = expand("%:p")
712 if has("win32") || has("win95") || has("win64") || has("win16")
713 let tarhome= substitute(tarhome,'\\','/','g')
714 endif
715 let tarhome= substitute(tarhome,'/[^/]*$','','')
716" call Decho("tarhome<".tarhome.">")
717 let tartail = expand("%:t")
718" call Decho("tartail<".tartail.">")
719 let curdir = getcwd()
720" call Decho("curdir <".curdir.">")
721 " set up vimhome
722 if a:0 > 0 && a:1 != ""
723 let vimhome= a:1
724 else
725 let vimhome= vimball#VimballHome()
726 endif
727" call Decho("vimhome<".vimhome.">")
728
729" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
730 if simplify(curdir) != simplify(vimhome)
731 " copy (possibly compressed) tarball to .vim/vimfiles
732" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
733 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
734" call Decho("exe cd ".fnameescape(vimhome))
735 exe "cd ".fnameescape(vimhome)
736 endif
737" call Decho("getcwd<".getcwd().">")
738
739 " if necessary, decompress the tarball; then, extract it
740 if tartail =~ '\.tgz'
Bram Moolenaar29634562020-01-09 21:46:04 +0100741 if executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100742 silent exe "!gunzip ".shellescape(tartail)
743 elseif executable("gzip")
744 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000745 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100746 echoerr "unable to decompress<".tartail."> on this sytem"
747 if simplify(curdir) != simplify(tarhome)
748 " remove decompressed tarball, restore directory
749" call Decho("delete(".tartail.".tar)")
750 call delete(tartail.".tar")
751" call Decho("exe cd ".fnameescape(curdir))
752 exe "cd ".fnameescape(curdir)
753 endif
754" call Dret("tar#Vimuntar")
755 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000756 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100758 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100760 let extractcmd= netrw#WinPath(g:tar_extractcmd)
761" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
762 call system(extractcmd." ".shellescape(tarbase.".tar"))
763
764 " set up help
765 if filereadable("doc/".tarbase.".txt")
766" call Decho("exe helptags ".getcwd()."/doc")
767 exe "helptags ".getcwd()."/doc"
768 endif
769
770 if simplify(tarhome) != simplify(vimhome)
771 " remove decompressed tarball, restore directory
772 call delete(vimhome."/".tarbase.".tar")
773 exe "cd ".fnameescape(curdir)
774 endif
775
776" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777endfun
778
Bram Moolenaar5c736222010-01-06 20:54:52 +0100779" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000780" Modelines And Restoration: {{{1
781let &cpo= s:keepcpo
782unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000783" vim:ts=8 fdm=marker