blob: becb7909253bf2f7cffa713e0e26c4d546666bff [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaar7fc0c062010-08-10 21:43:35 +02003" Date: Aug 09, 2010
4" Version: 26
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00005" Maintainer: Charles E Campbell, Jr <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 Moolenaar5c736222010-01-06 20:54:52 +010010" Copyright: Copyright (C) 2005-2009 Charles E. Campbell, Jr. {{{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 Moolenaar7fc0c062010-08-10 21:43:35 +020025let g:loaded_tar= "v26"
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
34"call Decho("loading autoload/tar.vim")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000035
Bram Moolenaara5792f52005-11-23 21:25:05 +000036" ---------------------------------------------------------------------
37" Default Settings: {{{1
38if !exists("g:tar_browseoptions")
39 let g:tar_browseoptions= "Ptf"
40endif
41if !exists("g:tar_readoptions")
42 let g:tar_readoptions= "OPxf"
43endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000044if !exists("g:tar_cmd")
45 let g:tar_cmd= "tar"
46endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000047if !exists("g:tar_writeoptions")
48 let g:tar_writeoptions= "uf"
49endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010050if !exists("g:tar_copycmd")
51 if !exists("g:netrw_localcopycmd")
52 if has("win32") || has("win95") || has("win64") || has("win16")
53 if g:netrw_cygwin
54 let g:netrw_localcopycmd= "cp"
55 else
56 let g:netrw_localcopycmd= "copy"
57 endif
58 elseif has("unix") || has("macunix")
59 let g:netrw_localcopycmd= "cp"
60 else
61 let g:netrw_localcopycmd= ""
62 endif
63 endif
64 let g:tar_copycmd= g:netrw_localcopycmd
65endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000066if !exists("g:netrw_cygwin")
67 if has("win32") || has("win95") || has("win64") || has("win16")
68 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
69 let g:netrw_cygwin= 1
70 else
71 let g:netrw_cygwin= 0
72 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000074 let g:netrw_cygwin= 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000075 endif
76endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010077if !exists("g:tar_extractcmd")
78 let g:tar_extractcmd= "tar -xf"
79endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000080
Bram Moolenaar8c8de832008-06-24 22:58:06 +000081" set up shell quoting character
82if !exists("g:tar_shq")
83 if exists("&shq") && &shq != ""
84 let g:tar_shq= &shq
85 elseif has("win32") || has("win95") || has("win64") || has("win16")
86 if exists("g:netrw_cygwin") && g:netrw_cygwin
87 let g:tar_shq= "'"
88 else
89 let g:tar_shq= '"'
90 endif
91 else
92 let g:tar_shq= "'"
93 endif
94" call Decho("g:tar_shq<".g:tar_shq.">")
95endif
96
Bram Moolenaara5792f52005-11-23 21:25:05 +000097" ----------------
98" Functions: {{{1
99" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000100
Bram Moolenaara5792f52005-11-23 21:25:05 +0000101" ---------------------------------------------------------------------
102" tar#Browse: {{{2
103fun! tar#Browse(tarfile)
104" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000105 let repkeep= &report
106 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000107
Bram Moolenaara5792f52005-11-23 21:25:05 +0000108 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000109 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000110 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000111 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000112 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000113" call Dret("tar#Browse")
114 return
115 endif
116 if !filereadable(a:tarfile)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000117" call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000118 if a:tarfile !~# '^\a\+://'
119 " if its an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000120 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000121 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000122 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000123 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000124" call Dret("tar#Browse : file<".a:tarfile."> not readable")
125 return
126 endif
127 if &ma != 1
128 set ma
129 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200130 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000131
Bram Moolenaara5792f52005-11-23 21:25:05 +0000132 setlocal noswapfile
133 setlocal buftype=nofile
134 setlocal bufhidden=hide
135 setlocal nobuflisted
136 setlocal nowrap
137 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000138
Bram Moolenaara5792f52005-11-23 21:25:05 +0000139 " give header
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000140" call Decho("printing header")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000141 let lastline= line("$")
142 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
143 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
144 call setline(lastline+3,'" Select a file with cursor and press ENTER')
145 $put =''
Bram Moolenaara5792f52005-11-23 21:25:05 +0000146 0d
147 $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000148
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000149 let tarfile= a:tarfile
150 if has("win32") && executable("cygpath")
151 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100152 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000153 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000154 let curlast= line("$")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000155 if tarfile =~# '\.\(gz\|tgz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100156" call Decho("1: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
157 exe "silent r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000158 elseif tarfile =~# '\.lrp'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100159" call Decho("2: exe silent r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - ")
160 exe "silent r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000161 elseif tarfile =~# '\.bz2$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100162" call Decho("3: exe silent r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
163 exe "silent r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
164 elseif tarfile =~# '\.lzma$'
165" call Decho("3: exe silent r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
166 exe "silent r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200167 elseif tarfile =~# '\.\(xz\|txz\)$'
168" call Decho("3: exe silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
169 exe "silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000170 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000171 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100172 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000173 let tarfile = substitute(tarfile, '-', './-', '')
174 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100175" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
176 exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000177 endif
178 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000179 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000180 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000181" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000182 return
183 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000184 if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
185 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000186 echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
Bram Moolenaard68071d2006-05-02 22:08:30 +0000187 silent %d
188 let eikeep= &ei
189 set ei=BufReadCmd,FileReadCmd
Bram Moolenaarc236c162008-07-13 17:41:49 +0000190 exe "r ".fnameescape(a:tarfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000191 let &ei= eikeep
192 1d
193" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000194 return
195 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000196
197 setlocal noma nomod ro
198 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
199
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000200 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200201" call Dret("tar#Browse : b:tarfile<".b:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000202endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000203
Bram Moolenaarab194812005-09-14 21:40:12 +0000204" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000205" TarBrowseSelect: {{{2
206fun! s:TarBrowseSelect()
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200207" call Dfunc("TarBrowseSelect() b:tarfile<".b:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000208 let repkeep= &report
209 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000210 let fname= getline(".")
211" call Decho("fname<".fname.">")
212
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000213 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
214 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100215 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000216" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
217 return
218 endif
219
Bram Moolenaara5792f52005-11-23 21:25:05 +0000220 " sanity check
221 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000222 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000223" call Dret("TarBrowseSelect")
224 return
225 endif
226
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200227 " about to make a new window, need to use b:tarfile
228 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000229 let curfile= expand("%")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000230 if has("win32") && executable("cygpath")
231 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100232 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000233 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000234
235 new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000236 if !exists("g:tar_nomax") || g:tar_nomax == 0
237 wincmd _
238 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000239 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000240 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000241 filetype detect
242
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000243 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000244" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
245endfun
246
247" ---------------------------------------------------------------------
248" tar#Read: {{{2
249fun! tar#Read(fname,mode)
250" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000251 let repkeep= &report
252 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000253 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
254 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000255 if has("win32") && executable("cygpath")
256 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100257 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000258 endif
259" call Decho("tarfile<".tarfile.">")
260" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000261
Bram Moolenaar5c736222010-01-06 20:54:52 +0100262 if fname =~ '\.bz2$' && executable("bzcat")
263 let decmp= "|bzcat"
264 let doro = 1
265 elseif fname =~ '\.gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000266 let decmp= "|zcat"
267 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100268 elseif fname =~ '\.lzma$' && executable("lzcat")
269 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000270 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200271 elseif fname =~ '\.xz$' && executable("xzcat")
272 let decmp= "|xzcat"
273 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000274 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000275 let decmp=""
276 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200277 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000278 setlocal bin
279 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000280 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000281
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000282 if exists("g:tar_secure")
283 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000284 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000285 let tar_secure= " "
286 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100287 if tarfile =~# '\.bz2$'
288" call Decho("7: exe silent r! bzip2 -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
289 exe "silent r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
290 elseif tarfile =~# '\.\(gz\|tgz\)$'
291" call Decho("5: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd.' -'.g:tar_readoptions.' - '.tar_secure.shellescape(fname,1))
292 exe "silent r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000293 elseif tarfile =~# '\.lrp$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100294" 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)
295 exe "silent r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
296 elseif tarfile =~# '\.lzma$'
297" call Decho("7: exe silent r! lzma -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
298 exe "silent 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 +0200299 elseif tarfile =~# '\.\(xz\|txz\)$'
300" call Decho("3: exe silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
301 exe "silent 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 +0000302 else
303 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100304 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000305 let tarfile = substitute(tarfile, '-', './-', '')
306 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100307" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
308 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 +0000309 endif
310
311 if doro
312 " because the reverse process of compressing changed files back into the tarball is not currently supported
313 setlocal ro
314 endif
315
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200316 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000317 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000318
319 " cleanup
320 0d
321 set nomod
322
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000323 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200324" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000325endfun
326
327" ---------------------------------------------------------------------
328" tar#Write: {{{2
329fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200330" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000331 let repkeep= &report
332 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000333
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000334 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
335 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100336 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000337" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
338 return
339 endif
340
Bram Moolenaarab194812005-09-14 21:40:12 +0000341 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000342 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000343 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000344 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000345 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000346" call Dret("tar#Write")
347 return
348 endif
349 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000350 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000351 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000352 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000353" call Dret("tar#Write")
354 return
355 endif
356
357 let curdir= getcwd()
358 let tmpdir= tempname()
359" call Decho("orig tempname<".tmpdir.">")
360 if tmpdir =~ '\.'
361 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
362 endif
363" call Decho("tmpdir<".tmpdir.">")
364 call mkdir(tmpdir,"p")
365
366 " attempt to change to the indicated directory
367 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000369 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000370 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000371 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000372 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000373" call Dret("tar#Write")
374 return
375 endtry
376" call Decho("current directory now: ".getcwd())
377
Bram Moolenaara5792f52005-11-23 21:25:05 +0000378 " place temporary files under .../_ZIPVIM_/
379 if isdirectory("_ZIPVIM_")
380 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000381 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000382 call mkdir("_ZIPVIM_")
383 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000384" call Decho("current directory now: ".getcwd())
385
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200386 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
387 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000388
389 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100390 if tarfile =~# '\.bz2'
391 call system("bzip2 -d -- ".shellescape(tarfile,0))
392 let tarfile = substitute(tarfile,'\.bz2','','e')
393 let compress= "bzip2 -- ".shellescape(tarfile,0)
394" call Decho("compress<".compress.">")
395 elseif tarfile =~# '\.gz'
396 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000397 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100398 let compress= "gzip -- ".shellescape(tarfile,0)
399" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000400 elseif tarfile =~# '\.tgz'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100401 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000402 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100403 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000404 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000405" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200406 elseif tarfile =~# '\.xz'
407 call system("xz -d -- ".shellescape(tarfile,0))
408 let tarfile = substitute(tarfile,'\.xz','','e')
409 let compress= "xz -- ".shellescape(tarfile,0)
410" call Decho("compress<".compress.">")
411 elseif tarfile =~# '\.lzma'
412 call system("lzma -d -- ".shellescape(tarfile,0))
413 let tarfile = substitute(tarfile,'\.lzma','','e')
414 let compress= "lzma -- ".shellescape(tarfile,0)
415" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000416 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000417" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000418
Bram Moolenaarab194812005-09-14 21:40:12 +0000419 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000420 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000421 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000422 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000423
424" call Decho("tarfile<".tarfile."> fname<".fname.">")
425
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000426 if fname =~ '/'
427 let dirpath = substitute(fname,'/[^/]\+$','','e')
428 if executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100429 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000430 endif
431 call mkdir(dirpath,"p")
432 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000433 if tarfile !~ '/'
434 let tarfile= curdir.'/'.tarfile
435 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000436 if tarfile =~ '^\s*-'
437 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
438 let tarfile = substitute(tarfile, '-', './-', '')
439 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000440" call Decho("tarfile<".tarfile."> fname<".fname.">")
441
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000442 if exists("g:tar_secure")
443 let tar_secure= " -- "
444 else
445 let tar_secure= " "
446 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000447 exe "w! ".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000448 if executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100449 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000450 endif
451
452 " delete old file from tarfile
Bram Moolenaar5c736222010-01-06 20:54:52 +0100453" call Decho("system(".g:tar_cmd." --delete -f ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
454 call system(g:tar_cmd." --delete -f ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000455 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000456 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000458 else
459
460 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100461" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
462 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000463 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000464 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000466 elseif exists("compress")
467" call Decho("call system(".compress.")")
468 call system(compress)
469 if exists("tgz")
470" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
471 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
472 endif
473 endif
474 endif
475
476 " support writing tarfiles across a network
477 if s:tblfile_{winnr()} =~ '^\a\+://'
478" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
479 let tblfile= s:tblfile_{winnr()}
480 1split|enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100481 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000482 let eikeep = &ei
483 set binary ei=all
Bram Moolenaarc236c162008-07-13 17:41:49 +0000484 exe "e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000485 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100486 let &ei = eikeep
487 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000488 q!
489 unlet s:tblfile_{winnr()}
490 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000491 endif
492
493 " cleanup and restore current directory
494 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000495 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000496 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000497 setlocal nomod
498
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000499 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000500" call Dret("tar#Write")
501endfun
502
503" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100504" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000505fun! s:Rmdir(fname)
506" call Dfunc("Rmdir(fname<".a:fname.">)")
507 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100508 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000509 elseif has("win32") || has("win95") || has("win64") || has("win16")
510 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100511 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000512 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100513 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000514 endif
515 endif
516" call Dret("Rmdir")
517endfun
518
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100520" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
521fun! tar#Vimuntar(...)
522" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
523 let tarball = expand("%")
524" call Decho("tarball<".tarball.">")
525 let tarbase = substitute(tarball,'\..*$','','')
526" call Decho("tarbase<".tarbase.">")
527 let tarhome = expand("%:p")
528 if has("win32") || has("win95") || has("win64") || has("win16")
529 let tarhome= substitute(tarhome,'\\','/','g')
530 endif
531 let tarhome= substitute(tarhome,'/[^/]*$','','')
532" call Decho("tarhome<".tarhome.">")
533 let tartail = expand("%:t")
534" call Decho("tartail<".tartail.">")
535 let curdir = getcwd()
536" call Decho("curdir <".curdir.">")
537 " set up vimhome
538 if a:0 > 0 && a:1 != ""
539 let vimhome= a:1
540 else
541 let vimhome= vimball#VimballHome()
542 endif
543" call Decho("vimhome<".vimhome.">")
544
545" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
546 if simplify(curdir) != simplify(vimhome)
547 " copy (possibly compressed) tarball to .vim/vimfiles
548" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
549 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
550" call Decho("exe cd ".fnameescape(vimhome))
551 exe "cd ".fnameescape(vimhome)
552 endif
553" call Decho("getcwd<".getcwd().">")
554
555 " if necessary, decompress the tarball; then, extract it
556 if tartail =~ '\.tgz'
557 if executable("gunzip")
558 silent exe "!gunzip ".shellescape(tartail)
559 elseif executable("gzip")
560 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000561 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100562 echoerr "unable to decompress<".tartail."> on this sytem"
563 if simplify(curdir) != simplify(tarhome)
564 " remove decompressed tarball, restore directory
565" call Decho("delete(".tartail.".tar)")
566 call delete(tartail.".tar")
567" call Decho("exe cd ".fnameescape(curdir))
568 exe "cd ".fnameescape(curdir)
569 endif
570" call Dret("tar#Vimuntar")
571 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000572 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100574 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000575 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100576 let extractcmd= netrw#WinPath(g:tar_extractcmd)
577" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
578 call system(extractcmd." ".shellescape(tarbase.".tar"))
579
580 " set up help
581 if filereadable("doc/".tarbase.".txt")
582" call Decho("exe helptags ".getcwd()."/doc")
583 exe "helptags ".getcwd()."/doc"
584 endif
585
586 if simplify(tarhome) != simplify(vimhome)
587 " remove decompressed tarball, restore directory
588 call delete(vimhome."/".tarbase.".tar")
589 exe "cd ".fnameescape(curdir)
590 endif
591
592" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000593endfun
594
Bram Moolenaar5c736222010-01-06 20:54:52 +0100595" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000596" Modelines And Restoration: {{{1
597let &cpo= s:keepcpo
598unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000599" vim:ts=8 fdm=marker