blob: d92fe1b1807f90ff2b9cf18bf001632b1402f7e9 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaarff034192013-04-24 18:51:19 +02003" Date: Apr 17, 2013
4" Version: 29
5" Maintainer: Charles E Campbell <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
Bram Moolenaara5792f52005-11-23 21:25:05 +00006" 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 Moolenaarff034192013-04-24 18:51:19 +020010" Copyright: Copyright (C) 2005-2011 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 Moolenaarff034192013-04-24 18:51:19 +020025let g:loaded_tar= "v29"
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 Moolenaar251e1912011-06-19 05:09:16 +020051if !exists("g:netrw_cygwin")
52 if has("win32") || has("win95") || has("win64") || has("win16")
53 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
54 let g:netrw_cygwin= 1
55 else
56 let g:netrw_cygwin= 0
57 endif
58 else
59 let g:netrw_cygwin= 0
60 endif
61endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010062if !exists("g:tar_copycmd")
63 if !exists("g:netrw_localcopycmd")
64 if has("win32") || has("win95") || has("win64") || has("win16")
65 if g:netrw_cygwin
66 let g:netrw_localcopycmd= "cp"
67 else
68 let g:netrw_localcopycmd= "copy"
69 endif
70 elseif has("unix") || has("macunix")
71 let g:netrw_localcopycmd= "cp"
72 else
73 let g:netrw_localcopycmd= ""
74 endif
75 endif
76 let g:tar_copycmd= g:netrw_localcopycmd
77endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010078if !exists("g:tar_extractcmd")
79 let g:tar_extractcmd= "tar -xf"
80endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000081
Bram Moolenaar8c8de832008-06-24 22:58:06 +000082" set up shell quoting character
83if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020084 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000085 let g:tar_shq= &shq
86 elseif has("win32") || has("win95") || has("win64") || has("win16")
87 if exists("g:netrw_cygwin") && g:netrw_cygwin
88 let g:tar_shq= "'"
89 else
90 let g:tar_shq= '"'
91 endif
92 else
93 let g:tar_shq= "'"
94 endif
95" call Decho("g:tar_shq<".g:tar_shq.">")
96endif
97
Bram Moolenaara5792f52005-11-23 21:25:05 +000098" ----------------
99" Functions: {{{1
100" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000101
Bram Moolenaara5792f52005-11-23 21:25:05 +0000102" ---------------------------------------------------------------------
103" tar#Browse: {{{2
104fun! tar#Browse(tarfile)
105" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000106 let repkeep= &report
107 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000108
Bram Moolenaara5792f52005-11-23 21:25:05 +0000109 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000110 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000111 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000112 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000113 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000114" call Dret("tar#Browse")
115 return
116 endif
117 if !filereadable(a:tarfile)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000118" call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000119 if a:tarfile !~# '^\a\+://'
Bram Moolenaar3e496b02016-09-25 22:11:48 +0200120 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000121 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000122 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000123 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000124 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000125" call Dret("tar#Browse : file<".a:tarfile."> not readable")
126 return
127 endif
128 if &ma != 1
129 set ma
130 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200131 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000132
Bram Moolenaara5792f52005-11-23 21:25:05 +0000133 setlocal noswapfile
134 setlocal buftype=nofile
135 setlocal bufhidden=hide
136 setlocal nobuflisted
137 setlocal nowrap
138 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000139
Bram Moolenaara5792f52005-11-23 21:25:05 +0000140 " give header
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000141" call Decho("printing header")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000142 let lastline= line("$")
143 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
144 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
145 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200146 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100147 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200148 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000149
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000150 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200151 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000152 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100153 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000154 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200155
156 let gzip_command = s:get_gzip_command(tarfile)
157
Bram Moolenaard68071d2006-05-02 22:08:30 +0000158 let curlast= line("$")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000159 if tarfile =~# '\.\(gz\|tgz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100160" call Decho("1: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200161 exe "sil! r! " . gzip_command . " -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000162 elseif tarfile =~# '\.lrp'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100163" call Decho("2: exe silent r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200164 exe "sil! r! cat -- ".shellescape(tarfile,1)."|" . gzip_command . " -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100165 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100166" 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 +0200167 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100168 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100169" 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 +0200170 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200171 elseif tarfile =~# '\.\(xz\|txz\)$'
172" 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 +0200173 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000174 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000175 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100176 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000177 let tarfile = substitute(tarfile, '-', './-', '')
178 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100179" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
Bram Moolenaar251e1912011-06-19 05:09:16 +0200180 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000181 endif
182 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000183 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000184 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000185" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000186 return
187 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000188 if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
189 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000190 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 +0200191 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000192 let eikeep= &ei
193 set ei=BufReadCmd,FileReadCmd
Bram Moolenaarc236c162008-07-13 17:41:49 +0000194 exe "r ".fnameescape(a:tarfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000195 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200196 keepj sil! 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000197" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000198 return
199 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000200
201 setlocal noma nomod ro
202 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
203
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000204 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200205" call Dret("tar#Browse : b:tarfile<".b:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000206endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000207
Bram Moolenaarab194812005-09-14 21:40:12 +0000208" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000209" TarBrowseSelect: {{{2
210fun! s:TarBrowseSelect()
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200211" call Dfunc("TarBrowseSelect() b:tarfile<".b:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000212 let repkeep= &report
213 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000214 let fname= getline(".")
215" call Decho("fname<".fname.">")
216
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000217 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
218 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100219 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000220" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
221 return
222 endif
223
Bram Moolenaara5792f52005-11-23 21:25:05 +0000224 " sanity check
225 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000226 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000227" call Dret("TarBrowseSelect")
228 return
229 endif
230
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200231 " about to make a new window, need to use b:tarfile
232 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000233 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200234 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000235 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100236 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000237 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000238
239 new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000240 if !exists("g:tar_nomax") || g:tar_nomax == 0
241 wincmd _
242 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000243 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000244 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000245 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200246 set nomod
247 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000248
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000249 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000250" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
251endfun
252
253" ---------------------------------------------------------------------
254" tar#Read: {{{2
255fun! tar#Read(fname,mode)
256" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000257 let repkeep= &report
258 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000259 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
260 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200261 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000262 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100263 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000264 endif
265" call Decho("tarfile<".tarfile.">")
266" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000267
Bram Moolenaar5c736222010-01-06 20:54:52 +0100268 if fname =~ '\.bz2$' && executable("bzcat")
269 let decmp= "|bzcat"
270 let doro = 1
271 elseif fname =~ '\.gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000272 let decmp= "|zcat"
273 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100274 elseif fname =~ '\.lzma$' && executable("lzcat")
275 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000276 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200277 elseif fname =~ '\.xz$' && executable("xzcat")
278 let decmp= "|xzcat"
279 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000280 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000281 let decmp=""
282 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200283 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000284 setlocal bin
285 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000286 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000287
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000288 if exists("g:tar_secure")
289 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000290 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000291 let tar_secure= " "
292 endif
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200293
294 let gzip_command = s:get_gzip_command(tarfile)
295
Bram Moolenaar5c736222010-01-06 20:54:52 +0100296 if tarfile =~# '\.bz2$'
297" call Decho("7: exe silent r! bzip2 -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200298 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar5c736222010-01-06 20:54:52 +0100299 elseif tarfile =~# '\.\(gz\|tgz\)$'
300" call Decho("5: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd.' -'.g:tar_readoptions.' - '.tar_secure.shellescape(fname,1))
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200301 exe "sil! r! " . gzip_command . " -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000302 elseif tarfile =~# '\.lrp$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100303" call Decho("6: exe silent r! cat ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200304 exe "sil! r! cat -- ".shellescape(tarfile,1)." | " . gzip_command . " -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar5c736222010-01-06 20:54:52 +0100305 elseif tarfile =~# '\.lzma$'
306" call Decho("7: exe silent r! lzma -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200307 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 +0200308 elseif tarfile =~# '\.\(xz\|txz\)$'
309" call Decho("3: exe silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200310 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 +0000311 else
312 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100313 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000314 let tarfile = substitute(tarfile, '-', './-', '')
315 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100316" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
317 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 +0000318 endif
319
320 if doro
321 " because the reverse process of compressing changed files back into the tarball is not currently supported
322 setlocal ro
323 endif
324
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200325 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000326 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000327
328 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200329 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000330 set nomod
331
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000332 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200333" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000334endfun
335
336" ---------------------------------------------------------------------
337" tar#Write: {{{2
338fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200339" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000340 let repkeep= &report
341 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000342
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000343 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
344 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100345 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000346" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
347 return
348 endif
349
Bram Moolenaarab194812005-09-14 21:40:12 +0000350 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000351 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000352 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000353 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000354 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000355" call Dret("tar#Write")
356 return
357 endif
358 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000359 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000360 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000361 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000362" call Dret("tar#Write")
363 return
364 endif
365
366 let curdir= getcwd()
367 let tmpdir= tempname()
368" call Decho("orig tempname<".tmpdir.">")
369 if tmpdir =~ '\.'
370 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
371 endif
372" call Decho("tmpdir<".tmpdir.">")
373 call mkdir(tmpdir,"p")
374
375 " attempt to change to the indicated directory
376 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000378 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000379 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000380 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000381 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000382" call Dret("tar#Write")
383 return
384 endtry
385" call Decho("current directory now: ".getcwd())
386
Bram Moolenaara5792f52005-11-23 21:25:05 +0000387 " place temporary files under .../_ZIPVIM_/
388 if isdirectory("_ZIPVIM_")
389 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000390 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000391 call mkdir("_ZIPVIM_")
392 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000393" call Decho("current directory now: ".getcwd())
394
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200395 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
396 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000397
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200398 let gzip_command = s:get_gzip_command(tarfile)
399
Bram Moolenaara5792f52005-11-23 21:25:05 +0000400 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100401 if tarfile =~# '\.bz2'
402 call system("bzip2 -d -- ".shellescape(tarfile,0))
403 let tarfile = substitute(tarfile,'\.bz2','','e')
404 let compress= "bzip2 -- ".shellescape(tarfile,0)
405" call Decho("compress<".compress.">")
406 elseif tarfile =~# '\.gz'
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200407 call system(gzip_command . " -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000408 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100409 let compress= "gzip -- ".shellescape(tarfile,0)
410" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000411 elseif tarfile =~# '\.tgz'
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200412 call system(gzip_command . " -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000413 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100414 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000415 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000416" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200417 elseif tarfile =~# '\.xz'
418 call system("xz -d -- ".shellescape(tarfile,0))
419 let tarfile = substitute(tarfile,'\.xz','','e')
420 let compress= "xz -- ".shellescape(tarfile,0)
421" call Decho("compress<".compress.">")
422 elseif tarfile =~# '\.lzma'
423 call system("lzma -d -- ".shellescape(tarfile,0))
424 let tarfile = substitute(tarfile,'\.lzma','','e')
425 let compress= "lzma -- ".shellescape(tarfile,0)
426" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000427 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000428" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000429
Bram Moolenaarab194812005-09-14 21:40:12 +0000430 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000431 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000432 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000433 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000434
435" call Decho("tarfile<".tarfile."> fname<".fname.">")
436
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000437 if fname =~ '/'
438 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200439 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100440 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000441 endif
442 call mkdir(dirpath,"p")
443 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000444 if tarfile !~ '/'
445 let tarfile= curdir.'/'.tarfile
446 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000447 if tarfile =~ '^\s*-'
448 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
449 let tarfile = substitute(tarfile, '-', './-', '')
450 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000451" call Decho("tarfile<".tarfile."> fname<".fname.">")
452
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000453 if exists("g:tar_secure")
454 let tar_secure= " -- "
455 else
456 let tar_secure= " "
457 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000458 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200459 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100460 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000461 endif
462
463 " delete old file from tarfile
Bram Moolenaar5c736222010-01-06 20:54:52 +0100464" call Decho("system(".g:tar_cmd." --delete -f ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
465 call system(g:tar_cmd." --delete -f ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000466 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000467 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000469 else
470
471 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100472" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
473 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000474 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000475 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000477 elseif exists("compress")
478" call Decho("call system(".compress.")")
479 call system(compress)
480 if exists("tgz")
481" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
482 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
483 endif
484 endif
485 endif
486
487 " support writing tarfiles across a network
488 if s:tblfile_{winnr()} =~ '^\a\+://'
489" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
490 let tblfile= s:tblfile_{winnr()}
491 1split|enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100492 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000493 let eikeep = &ei
494 set binary ei=all
Bram Moolenaarc236c162008-07-13 17:41:49 +0000495 exe "e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000496 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100497 let &ei = eikeep
498 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000499 q!
500 unlet s:tblfile_{winnr()}
501 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000502 endif
503
504 " cleanup and restore current directory
505 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000506 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000507 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000508 setlocal nomod
509
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000510 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000511" call Dret("tar#Write")
512endfun
513
514" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200515" tar#Diff: {{{2
516fun! tar#Diff(userfname,fname)
517" call Dfunc("tar#Diff(userfname<".a:userfname."> fname<".a:fname.")")
518 let fname= a:fname
519 if a:userfname != ""
520 let fname= a:userfname
521 endif
522 if filereadable(fname)
523 " sets current file (from tarball) for diff'ing
524 " splits window vertically
525 " opens original file, sets it for diff'ing
526 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
527 diffthis
528 wincmd v
529 exe "e ".fnameescape(fname)
530 diffthis
531 else
532 redraw!
533 echo "***warning*** unable to read file<".fname.">"
534 endif
535" call Dret("tar#Diff")
536endfun
537
538" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100539" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000540fun! s:Rmdir(fname)
541" call Dfunc("Rmdir(fname<".a:fname.">)")
542 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100543 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000544 elseif has("win32") || has("win95") || has("win64") || has("win16")
545 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100546 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000547 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100548 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000549 endif
550 endif
551" call Dret("Rmdir")
552endfun
553
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100555" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
556fun! tar#Vimuntar(...)
557" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
558 let tarball = expand("%")
559" call Decho("tarball<".tarball.">")
560 let tarbase = substitute(tarball,'\..*$','','')
561" call Decho("tarbase<".tarbase.">")
562 let tarhome = expand("%:p")
563 if has("win32") || has("win95") || has("win64") || has("win16")
564 let tarhome= substitute(tarhome,'\\','/','g')
565 endif
566 let tarhome= substitute(tarhome,'/[^/]*$','','')
567" call Decho("tarhome<".tarhome.">")
568 let tartail = expand("%:t")
569" call Decho("tartail<".tartail.">")
570 let curdir = getcwd()
571" call Decho("curdir <".curdir.">")
572 " set up vimhome
573 if a:0 > 0 && a:1 != ""
574 let vimhome= a:1
575 else
576 let vimhome= vimball#VimballHome()
577 endif
578" call Decho("vimhome<".vimhome.">")
579
580" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
581 if simplify(curdir) != simplify(vimhome)
582 " copy (possibly compressed) tarball to .vim/vimfiles
583" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
584 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
585" call Decho("exe cd ".fnameescape(vimhome))
586 exe "cd ".fnameescape(vimhome)
587 endif
588" call Decho("getcwd<".getcwd().">")
589
590 " if necessary, decompress the tarball; then, extract it
591 if tartail =~ '\.tgz'
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200592 if executable("bzip2")
593 silent exe "!bzip2 -d ".shellescape(tartail)
594 elseif executable("gunzip")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100595 silent exe "!gunzip ".shellescape(tartail)
596 elseif executable("gzip")
597 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000598 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100599 echoerr "unable to decompress<".tartail."> on this sytem"
600 if simplify(curdir) != simplify(tarhome)
601 " remove decompressed tarball, restore directory
602" call Decho("delete(".tartail.".tar)")
603 call delete(tartail.".tar")
604" call Decho("exe cd ".fnameescape(curdir))
605 exe "cd ".fnameescape(curdir)
606 endif
607" call Dret("tar#Vimuntar")
608 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000609 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100611 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100613 let extractcmd= netrw#WinPath(g:tar_extractcmd)
614" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
615 call system(extractcmd." ".shellescape(tarbase.".tar"))
616
617 " set up help
618 if filereadable("doc/".tarbase.".txt")
619" call Decho("exe helptags ".getcwd()."/doc")
620 exe "helptags ".getcwd()."/doc"
621 endif
622
623 if simplify(tarhome) != simplify(vimhome)
624 " remove decompressed tarball, restore directory
625 call delete(vimhome."/".tarbase.".tar")
626 exe "cd ".fnameescape(curdir)
627 endif
628
629" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000630endfun
631
Bram Moolenaar20aac6c2018-09-02 21:07:30 +0200632func s:get_gzip_command(file)
633 if a:file =~# 'z$' && executable('bzip2')
634 " Some .tgz files are actually compressed with bzip2. Since bzip2 can
635 " handle the format from gzip, use it if the command exists.
636 return 'bzip2'
637 endif
638 return 'gzip'
639endfunc
640
Bram Moolenaar5c736222010-01-06 20:54:52 +0100641" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000642" Modelines And Restoration: {{{1
643let &cpo= s:keepcpo
644unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645" vim:ts=8 fdm=marker