blob: 3e44579ce19ee779f488dcb6fd9a4a4a698c812c [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaar5c736222010-01-06 20:54:52 +01003" Date: Dec 28, 2009
4" Version: 24
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 Moolenaar5c736222010-01-06 20:54:52 +010025let g:loaded_tar= "v24"
26if 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
130 let w: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 Moolenaar910f66f2006-04-05 20:41:53 +0000167 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000168 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100169 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000170 let tarfile = substitute(tarfile, '-', './-', '')
171 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100172" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
173 exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000174 endif
175 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000176 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000177 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000178" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000179 return
180 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000181 if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
182 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000183 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 +0000184 silent %d
185 let eikeep= &ei
186 set ei=BufReadCmd,FileReadCmd
Bram Moolenaarc236c162008-07-13 17:41:49 +0000187 exe "r ".fnameescape(a:tarfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000188 let &ei= eikeep
189 1d
190" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000191 return
192 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000193
194 setlocal noma nomod ro
195 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
196
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000197 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000198" call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000199endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000200
Bram Moolenaarab194812005-09-14 21:40:12 +0000201" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000202" TarBrowseSelect: {{{2
203fun! s:TarBrowseSelect()
204" call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000205 let repkeep= &report
206 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000207 let fname= getline(".")
208" call Decho("fname<".fname.">")
209
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000210 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
211 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100212 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000213" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
214 return
215 endif
216
Bram Moolenaara5792f52005-11-23 21:25:05 +0000217 " sanity check
218 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000219 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000220" call Dret("TarBrowseSelect")
221 return
222 endif
223
224 " about to make a new window, need to use w:tarfile
225 let tarfile= w:tarfile
226 let curfile= expand("%")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000227 if has("win32") && executable("cygpath")
228 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100229 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000230 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000231
232 new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000233 if !exists("g:tar_nomax") || g:tar_nomax == 0
234 wincmd _
235 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000236 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000237 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000238 filetype detect
239
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000240 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000241" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
242endfun
243
244" ---------------------------------------------------------------------
245" tar#Read: {{{2
246fun! tar#Read(fname,mode)
247" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000248 let repkeep= &report
249 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000250 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
251 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000252 if has("win32") && executable("cygpath")
253 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100254 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000255 endif
256" call Decho("tarfile<".tarfile.">")
257" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000258
Bram Moolenaar5c736222010-01-06 20:54:52 +0100259 if fname =~ '\.bz2$' && executable("bzcat")
260 let decmp= "|bzcat"
261 let doro = 1
262 elseif fname =~ '\.gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000263 let decmp= "|zcat"
264 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100265 elseif fname =~ '\.lzma$' && executable("lzcat")
266 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000267 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000268 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000269 let decmp=""
270 let doro = 0
Bram Moolenaar5c736222010-01-06 20:54:52 +0100271 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000272 setlocal bin
273 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000274 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000275
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000276 if exists("g:tar_secure")
277 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000278 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000279 let tar_secure= " "
280 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100281 if tarfile =~# '\.bz2$'
282" call Decho("7: exe silent r! bzip2 -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
283 exe "silent r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
284 elseif tarfile =~# '\.\(gz\|tgz\)$'
285" call Decho("5: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd.' -'.g:tar_readoptions.' - '.tar_secure.shellescape(fname,1))
286 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 +0000287 elseif tarfile =~# '\.lrp$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100288" 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)
289 exe "silent r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
290 elseif tarfile =~# '\.lzma$'
291" call Decho("7: exe silent r! lzma -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
292 exe "silent r! lzma -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 else
294 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100295 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000296 let tarfile = substitute(tarfile, '-', './-', '')
297 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100298" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
299 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 +0000300 endif
301
302 if doro
303 " because the reverse process of compressing changed files back into the tarball is not currently supported
304 setlocal ro
305 endif
306
Bram Moolenaara5792f52005-11-23 21:25:05 +0000307 let w:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000308 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000309
310 " cleanup
311 0d
312 set nomod
313
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000314 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000315" call Dret("tar#Read : w:tarfile<".w:tarfile.">")
316endfun
317
318" ---------------------------------------------------------------------
319" tar#Write: {{{2
320fun! tar#Write(fname)
321" call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000322 let repkeep= &report
323 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000324
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000325 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
326 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100327 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000328" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
329 return
330 endif
331
Bram Moolenaarab194812005-09-14 21:40:12 +0000332 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000333 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000334 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000335 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000336 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000337" call Dret("tar#Write")
338 return
339 endif
340 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000341 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000342 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000343 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000344" call Dret("tar#Write")
345 return
346 endif
347
348 let curdir= getcwd()
349 let tmpdir= tempname()
350" call Decho("orig tempname<".tmpdir.">")
351 if tmpdir =~ '\.'
352 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
353 endif
354" call Decho("tmpdir<".tmpdir.">")
355 call mkdir(tmpdir,"p")
356
357 " attempt to change to the indicated directory
358 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000359 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000360 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000361 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000362 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000363 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000364" call Dret("tar#Write")
365 return
366 endtry
367" call Decho("current directory now: ".getcwd())
368
Bram Moolenaara5792f52005-11-23 21:25:05 +0000369 " place temporary files under .../_ZIPVIM_/
370 if isdirectory("_ZIPVIM_")
371 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000372 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000373 call mkdir("_ZIPVIM_")
374 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000375" call Decho("current directory now: ".getcwd())
376
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377 let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
378 let fname = substitute(w:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000379
380 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100381 if tarfile =~# '\.bz2'
382 call system("bzip2 -d -- ".shellescape(tarfile,0))
383 let tarfile = substitute(tarfile,'\.bz2','','e')
384 let compress= "bzip2 -- ".shellescape(tarfile,0)
385" call Decho("compress<".compress.">")
386 elseif tarfile =~# '\.gz'
387 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000388 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100389 let compress= "gzip -- ".shellescape(tarfile,0)
390" call Decho("compress<".compress.">")
391 elseif tarfile =~# '\.lzma'
392 call system("lzma -d -- ".shellescape(tarfile,0))
393 let tarfile = substitute(tarfile,'\.lzma','','e')
394 let compress= "lzma -- ".shellescape(tarfile,0)
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000395" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000396 elseif tarfile =~# '\.tgz'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100397 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000398 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100399 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000400 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000401" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000402 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000403" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000404
Bram Moolenaarab194812005-09-14 21:40:12 +0000405 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000406 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000407 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000408 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000409
410" call Decho("tarfile<".tarfile."> fname<".fname.">")
411
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000412 if fname =~ '/'
413 let dirpath = substitute(fname,'/[^/]\+$','','e')
414 if executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100415 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000416 endif
417 call mkdir(dirpath,"p")
418 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000419 if tarfile !~ '/'
420 let tarfile= curdir.'/'.tarfile
421 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000422 if tarfile =~ '^\s*-'
423 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
424 let tarfile = substitute(tarfile, '-', './-', '')
425 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000426" call Decho("tarfile<".tarfile."> fname<".fname.">")
427
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000428 if exists("g:tar_secure")
429 let tar_secure= " -- "
430 else
431 let tar_secure= " "
432 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000433 exe "w! ".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000434 if executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100435 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000436 endif
437
438 " delete old file from tarfile
Bram Moolenaar5c736222010-01-06 20:54:52 +0100439" call Decho("system(".g:tar_cmd." --delete -f ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
440 call system(g:tar_cmd." --delete -f ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000441 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000442 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000443 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000444 else
445
446 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100447" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
448 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000449 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000450 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000451 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000452 elseif exists("compress")
453" call Decho("call system(".compress.")")
454 call system(compress)
455 if exists("tgz")
456" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
457 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
458 endif
459 endif
460 endif
461
462 " support writing tarfiles across a network
463 if s:tblfile_{winnr()} =~ '^\a\+://'
464" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
465 let tblfile= s:tblfile_{winnr()}
466 1split|enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100467 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000468 let eikeep = &ei
469 set binary ei=all
Bram Moolenaarc236c162008-07-13 17:41:49 +0000470 exe "e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000471 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100472 let &ei = eikeep
473 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000474 q!
475 unlet s:tblfile_{winnr()}
476 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000477 endif
478
479 " cleanup and restore current directory
480 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000481 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000482 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000483 setlocal nomod
484
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000485 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000486" call Dret("tar#Write")
487endfun
488
489" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100490" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000491fun! s:Rmdir(fname)
492" call Dfunc("Rmdir(fname<".a:fname.">)")
493 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100494 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000495 elseif has("win32") || has("win95") || has("win64") || has("win16")
496 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100497 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000498 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100499 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000500 endif
501 endif
502" call Dret("Rmdir")
503endfun
504
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100506" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
507fun! tar#Vimuntar(...)
508" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
509 let tarball = expand("%")
510" call Decho("tarball<".tarball.">")
511 let tarbase = substitute(tarball,'\..*$','','')
512" call Decho("tarbase<".tarbase.">")
513 let tarhome = expand("%:p")
514 if has("win32") || has("win95") || has("win64") || has("win16")
515 let tarhome= substitute(tarhome,'\\','/','g')
516 endif
517 let tarhome= substitute(tarhome,'/[^/]*$','','')
518" call Decho("tarhome<".tarhome.">")
519 let tartail = expand("%:t")
520" call Decho("tartail<".tartail.">")
521 let curdir = getcwd()
522" call Decho("curdir <".curdir.">")
523 " set up vimhome
524 if a:0 > 0 && a:1 != ""
525 let vimhome= a:1
526 else
527 let vimhome= vimball#VimballHome()
528 endif
529" call Decho("vimhome<".vimhome.">")
530
531" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
532 if simplify(curdir) != simplify(vimhome)
533 " copy (possibly compressed) tarball to .vim/vimfiles
534" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
535 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
536" call Decho("exe cd ".fnameescape(vimhome))
537 exe "cd ".fnameescape(vimhome)
538 endif
539" call Decho("getcwd<".getcwd().">")
540
541 " if necessary, decompress the tarball; then, extract it
542 if tartail =~ '\.tgz'
543 if executable("gunzip")
544 silent exe "!gunzip ".shellescape(tartail)
545 elseif executable("gzip")
546 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000547 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100548 echoerr "unable to decompress<".tartail."> on this sytem"
549 if simplify(curdir) != simplify(tarhome)
550 " remove decompressed tarball, restore directory
551" call Decho("delete(".tartail.".tar)")
552 call delete(tartail.".tar")
553" call Decho("exe cd ".fnameescape(curdir))
554 exe "cd ".fnameescape(curdir)
555 endif
556" call Dret("tar#Vimuntar")
557 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000558 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000559 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100560 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100562 let extractcmd= netrw#WinPath(g:tar_extractcmd)
563" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
564 call system(extractcmd." ".shellescape(tarbase.".tar"))
565
566 " set up help
567 if filereadable("doc/".tarbase.".txt")
568" call Decho("exe helptags ".getcwd()."/doc")
569 exe "helptags ".getcwd()."/doc"
570 endif
571
572 if simplify(tarhome) != simplify(vimhome)
573 " remove decompressed tarball, restore directory
574 call delete(vimhome."/".tarbase.".tar")
575 exe "cd ".fnameescape(curdir)
576 endif
577
578" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000579endfun
580
Bram Moolenaar5c736222010-01-06 20:54:52 +0100581" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000582" Modelines And Restoration: {{{1
583let &cpo= s:keepcpo
584unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000585" vim:ts=8 fdm=marker