blob: 2fc7ef62742388381ce6ec81fd0cf3303e7a31a0 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003" Date: Mar 27, 2006
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +00004" Version: 7
Bram Moolenaara5792f52005-11-23 21:25:05 +00005" Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz>
6" License: Vim License (see vim's :help license)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007"
Bram Moolenaara5792f52005-11-23 21:25:05 +00008" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009"
Bram Moolenaara5792f52005-11-23 21:25:05 +000010" Copyright: Copyright (C) 2005 Charles E. Campbell, Jr. {{{1
11" 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,
14" tarPlugin.vim is provided *as is* and comes with no warranty
15" of any kind, either expressed or implied. By using this
16" plugin, you agree that in no event will the copyright
17" holder be liable for any damages resulting from the use
18" of this software.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019
Bram Moolenaarab194812005-09-14 21:40:12 +000020" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +000021" Initialization: {{{1
22let s:keepcpo= &cpo
23set cpo&vim
24if exists("g:loaded_tar")
25 finish
26endif
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +000027let g:loaded_tar= "v7"
Bram Moolenaar910f66f2006-04-05 20:41:53 +000028"call Decho("loading autoload/tar.vim")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000029
Bram Moolenaara5792f52005-11-23 21:25:05 +000030" ---------------------------------------------------------------------
31" Default Settings: {{{1
32if !exists("g:tar_browseoptions")
33 let g:tar_browseoptions= "Ptf"
34endif
35if !exists("g:tar_readoptions")
36 let g:tar_readoptions= "OPxf"
37endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000038if !exists("g:tar_cmd")
39 let g:tar_cmd= "tar"
40endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000041if !exists("g:tar_writeoptions")
42 let g:tar_writeoptions= "uf"
43endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000044
Bram Moolenaara5792f52005-11-23 21:25:05 +000045" ----------------
46" Functions: {{{1
47" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000048
Bram Moolenaara5792f52005-11-23 21:25:05 +000049" ---------------------------------------------------------------------
50" tar#Browse: {{{2
51fun! tar#Browse(tarfile)
52" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaarbba577a2005-11-28 23:05:55 +000053 let repkeep= &report
54 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000055
Bram Moolenaara5792f52005-11-23 21:25:05 +000056 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +000057 if !executable(g:tar_cmd)
58 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaara5792f52005-11-23 21:25:05 +000059 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +000060 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +000061" call Dret("tar#Browse")
62 return
63 endif
64 if !filereadable(a:tarfile)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000065" call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Bram Moolenaara5792f52005-11-23 21:25:05 +000066 if a:tarfile !~# '^\a\+://'
67 " if its an url, don't complain, let url-handlers such as vim do its thing
68 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
69 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
70 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +000071 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +000072" call Dret("tar#Browse : file<".a:tarfile."> not readable")
73 return
74 endif
75 if &ma != 1
76 set ma
77 endif
78 let w:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000079
Bram Moolenaara5792f52005-11-23 21:25:05 +000080 setlocal noswapfile
81 setlocal buftype=nofile
82 setlocal bufhidden=hide
83 setlocal nobuflisted
84 setlocal nowrap
85 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000086
Bram Moolenaara5792f52005-11-23 21:25:05 +000087 " give header
Bram Moolenaar910f66f2006-04-05 20:41:53 +000088" call Decho("printing header")
Bram Moolenaara5792f52005-11-23 21:25:05 +000089 exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'"
90 exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'"
91 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
92 0d
93 $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000094
Bram Moolenaar910f66f2006-04-05 20:41:53 +000095 let tarfile= a:tarfile
96 if has("win32") && executable("cygpath")
97 " assuming cygwin
98 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +000099 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000100 if tarfile =~# '\.\(gz\|tgz\)$'
101" call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -".g:tar_browseoptions." - ")
102 exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_browseoptions." - "
103 elseif tarfile =~# '\.bz2$'
104" call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_browseoptions." - ")
105 exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_browseoptions." - "
106 else
107" call Decho("exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." '".tarfile."'")
108 exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." '".tarfile."'"
109 endif
110 if v:shell_error != 0
111 echohl Error | echo '***error*** (tar#Browse) while browsing; check your g:tar_browseoptions<".g:tar_browseoptions.">"
112" call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
113 return
114 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000115
116 setlocal noma nomod ro
117 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
118
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000119 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000120" call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000121endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000122
Bram Moolenaarab194812005-09-14 21:40:12 +0000123" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000124" TarBrowseSelect: {{{2
125fun! s:TarBrowseSelect()
126" call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000127 let repkeep= &report
128 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000129 let fname= getline(".")
130" call Decho("fname<".fname.">")
131
132 " sanity check
133 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000134 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000135" call Dret("TarBrowseSelect")
136 return
137 endif
138
139 " about to make a new window, need to use w:tarfile
140 let tarfile= w:tarfile
141 let curfile= expand("%")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000142 if has("win32") && executable("cygpath")
143 " assuming cygwin
144 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
145 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000146
147 new
148 wincmd _
149 let s:tblfile_{winnr()}= curfile
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000150" if has("unix")
151"" call Decho("exe e tarfile:".tarfile.':'.fname)
152" exe "e tarfile:".tarfile.':'.fname
153" elseif has("win32")
154" call tar#Read("tarfile:".tarfile.':'.fname,1)
155" endif
156 call tar#Read("tarfile:".tarfile.':'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000157 filetype detect
158
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000159 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000160" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
161endfun
162
163" ---------------------------------------------------------------------
164" tar#Read: {{{2
165fun! tar#Read(fname,mode)
166" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000167 let repkeep= &report
168 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000169 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
170 let fname = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000171 if has("win32") && executable("cygpath")
172 " assuming cygwin
173 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
174 endif
175" call Decho("tarfile<".tarfile.">")
176" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000177
178 if tarfile =~# '\.\(gz\|tgz\)$'
179" call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -OPxf - '".fname."'")
180 exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000181 elseif tarfile =~# '\.bz2$'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000182" call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'")
183 exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
184 else
185" call Decho("exe silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000186 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions." '".tarfile."' '".fname."'"
Bram Moolenaara5792f52005-11-23 21:25:05 +0000187 endif
188 let w:tarfile= a:fname
189 exe "file tarfile:".fname
190
191 " cleanup
192 0d
193 set nomod
194
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000195 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000196" call Dret("tar#Read : w:tarfile<".w:tarfile.">")
197endfun
198
199" ---------------------------------------------------------------------
200" tar#Write: {{{2
201fun! tar#Write(fname)
202" call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000203 let repkeep= &report
204 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000205
Bram Moolenaarab194812005-09-14 21:40:12 +0000206 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000207 if !executable(g:tar_cmd)
208 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000209 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000210 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000211" call Dret("tar#Write")
212 return
213 endif
214 if !exists("*mkdir")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000215 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
216 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000217 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000218" call Dret("tar#Write")
219 return
220 endif
221
222 let curdir= getcwd()
223 let tmpdir= tempname()
224" call Decho("orig tempname<".tmpdir.">")
225 if tmpdir =~ '\.'
226 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
227 endif
228" call Decho("tmpdir<".tmpdir.">")
229 call mkdir(tmpdir,"p")
230
231 " attempt to change to the indicated directory
232 try
233 exe "cd ".escape(tmpdir,' \')
234 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaara5792f52005-11-23 21:25:05 +0000235 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
236 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000237 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000238" call Dret("tar#Write")
239 return
240 endtry
241" call Decho("current directory now: ".getcwd())
242
Bram Moolenaara5792f52005-11-23 21:25:05 +0000243 " place temporary files under .../_ZIPVIM_/
244 if isdirectory("_ZIPVIM_")
245 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000246 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000247 call mkdir("_ZIPVIM_")
248 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000249" call Decho("current directory now: ".getcwd())
250
Bram Moolenaara5792f52005-11-23 21:25:05 +0000251 let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
252 let fname = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
253
254 " handle compressed archives
255 if tarfile =~# '\.gz'
256 call system("gzip -d ".tarfile)
257 let tarfile = substitute(tarfile,'\.gz','','e')
258 let compress= "gzip '".tarfile."'"
259 elseif tarfile =~# '\.tgz'
260 call system("gzip -d ".tarfile)
261 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
262 let compress= "gzip '".tarfile."'"
263 let tgz = 1
264 elseif tarfile =~# '\.bz2'
265 call system("bzip2 -d ".tarfile)
266 let tarfile = substitute(tarfile,'\.bz2','','e')
267 let compress= "bzip2 '".tarfile."'"
Bram Moolenaarab194812005-09-14 21:40:12 +0000268 endif
269
Bram Moolenaarab194812005-09-14 21:40:12 +0000270 if v:shell_error != 0
Bram Moolenaara5792f52005-11-23 21:25:05 +0000271 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
272 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +0000273 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000274
275" call Decho("tarfile<".tarfile."> fname<".fname.">")
276
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000277 if fname =~ '/'
278 let dirpath = substitute(fname,'/[^/]\+$','','e')
279 if executable("cygpath")
280 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
281 endif
282 call mkdir(dirpath,"p")
283 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000284 if tarfile !~ '/'
285 let tarfile= curdir.'/'.tarfile
286 endif
287" call Decho("tarfile<".tarfile."> fname<".fname.">")
288
Bram Moolenaara5792f52005-11-23 21:25:05 +0000289 exe "w! ".fname
290 if executable("cygpath")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000291 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
292 endif
293
294 " delete old file from tarfile
295" call Decho("tar --delete -f '".tarfile."' '".fname."'")
296 call system("tar --delete -f '".tarfile."' '".fname."'")
297 if v:shell_error != 0
298 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
299 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
300 else
301
302 " update tarfile with new file
303" call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
304 call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
305 if v:shell_error != 0
306 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
307 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
308 elseif exists("compress")
309" call Decho("call system(".compress.")")
310 call system(compress)
311 if exists("tgz")
312" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
313 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
314 endif
315 endif
316 endif
317
318 " support writing tarfiles across a network
319 if s:tblfile_{winnr()} =~ '^\a\+://'
320" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
321 let tblfile= s:tblfile_{winnr()}
322 1split|enew
323 let binkeep= &binary
324 let eikeep = &ei
325 set binary ei=all
326 exe "e! ".tarfile
327 call netrw#NetWrite(tblfile)
328 let &ei = eikeep
329 let &binary = binkeep
330 q!
331 unlet s:tblfile_{winnr()}
332 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000333 endif
334
335 " cleanup and restore current directory
336 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000337 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000338 exe "cd ".escape(curdir,' \')
339 setlocal nomod
340
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000341 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000342" call Dret("tar#Write")
343endfun
344
345" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000346" Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000347fun! s:Rmdir(fname)
348" call Dfunc("Rmdir(fname<".a:fname.">)")
349 if has("unix")
350 call system("/bin/rm -rf ".a:fname)
351 elseif has("win32") || has("win95") || has("win64") || has("win16")
352 if &shell =~? "sh$"
353 call system("/bin/rm -rf ".a:fname)
354 else
355 call system("del /S ".a:fname)
356 endif
357 endif
358" call Dret("Rmdir")
359endfun
360
Bram Moolenaara5792f52005-11-23 21:25:05 +0000361" ------------------------------------------------------------------------
362" Modelines And Restoration: {{{1
363let &cpo= s:keepcpo
364unlet s:keepcpo
365" vim:ts=8 fdm=marker