blob: 9004f39359b8840a2a319c0885d9334677c08e26 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaard68071d2006-05-02 22:08:30 +00003" Date: May 02, 2006
4" Version: 9
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 Moolenaard68071d2006-05-02 22:08:30 +000027let g:loaded_tar= "v9"
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 Moolenaard68071d2006-05-02 22:08:30 +0000100 let curlast= line("$")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000101 if tarfile =~# '\.\(gz\|tgz\)$'
102" call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -".g:tar_browseoptions." - ")
103 exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_browseoptions." - "
104 elseif tarfile =~# '\.bz2$'
105" call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_browseoptions." - ")
106 exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_browseoptions." - "
107 else
108" call Decho("exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." '".tarfile."'")
109 exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." '".tarfile."'"
110 endif
111 if v:shell_error != 0
Bram Moolenaard68071d2006-05-02 22:08:30 +0000112 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
113 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
114" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
115 silent %d
116 let eikeep= &ei
117 set ei=BufReadCmd,FileReadCmd
118 exe "r ".a:tarfile
119 let &ei= eikeep
120 1d
121 return
122 endif
123 if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~? '\c\%(warning\|error\|inappropriate\|unrecognized\)')
124 echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
125 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
126 silent %d
127 let eikeep= &ei
128 set ei=BufReadCmd,FileReadCmd
129 exe "r ".a:tarfile
130 let &ei= eikeep
131 1d
132" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000133 return
134 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000135
136 setlocal noma nomod ro
137 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
138
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000139 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000140" call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000141endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000142
Bram Moolenaarab194812005-09-14 21:40:12 +0000143" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000144" TarBrowseSelect: {{{2
145fun! s:TarBrowseSelect()
146" call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000147 let repkeep= &report
148 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000149 let fname= getline(".")
150" call Decho("fname<".fname.">")
151
152 " sanity check
153 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000154 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000155" call Dret("TarBrowseSelect")
156 return
157 endif
158
159 " about to make a new window, need to use w:tarfile
160 let tarfile= w:tarfile
161 let curfile= expand("%")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000162 if has("win32") && executable("cygpath")
163 " assuming cygwin
164 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
165 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000166
167 new
168 wincmd _
169 let s:tblfile_{winnr()}= curfile
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000170 call tar#Read("tarfile:".tarfile.':'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000171 filetype detect
172
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000173 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000174" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
175endfun
176
177" ---------------------------------------------------------------------
178" tar#Read: {{{2
179fun! tar#Read(fname,mode)
180" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000181 let repkeep= &report
182 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000183 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
184 let fname = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000185 if has("win32") && executable("cygpath")
186 " assuming cygwin
187 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
188 endif
189" call Decho("tarfile<".tarfile.">")
190" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000191
192 if tarfile =~# '\.\(gz\|tgz\)$'
193" call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -OPxf - '".fname."'")
194 exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000195 elseif tarfile =~# '\.bz2$'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000196" call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'")
197 exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
198 else
199" call Decho("exe silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000200 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions." '".tarfile."' '".fname."'"
Bram Moolenaara5792f52005-11-23 21:25:05 +0000201 endif
202 let w:tarfile= a:fname
203 exe "file tarfile:".fname
204
205 " cleanup
206 0d
207 set nomod
208
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000209 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000210" call Dret("tar#Read : w:tarfile<".w:tarfile.">")
211endfun
212
213" ---------------------------------------------------------------------
214" tar#Write: {{{2
215fun! tar#Write(fname)
216" call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000217 let repkeep= &report
218 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000219
Bram Moolenaarab194812005-09-14 21:40:12 +0000220 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000221 if !executable(g:tar_cmd)
222 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000223 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000224 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000225" call Dret("tar#Write")
226 return
227 endif
228 if !exists("*mkdir")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000229 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
230 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000231 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000232" call Dret("tar#Write")
233 return
234 endif
235
236 let curdir= getcwd()
237 let tmpdir= tempname()
238" call Decho("orig tempname<".tmpdir.">")
239 if tmpdir =~ '\.'
240 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
241 endif
242" call Decho("tmpdir<".tmpdir.">")
243 call mkdir(tmpdir,"p")
244
245 " attempt to change to the indicated directory
246 try
247 exe "cd ".escape(tmpdir,' \')
248 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaara5792f52005-11-23 21:25:05 +0000249 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
250 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000251 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000252" call Dret("tar#Write")
253 return
254 endtry
255" call Decho("current directory now: ".getcwd())
256
Bram Moolenaara5792f52005-11-23 21:25:05 +0000257 " place temporary files under .../_ZIPVIM_/
258 if isdirectory("_ZIPVIM_")
259 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000260 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000261 call mkdir("_ZIPVIM_")
262 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000263" call Decho("current directory now: ".getcwd())
264
Bram Moolenaara5792f52005-11-23 21:25:05 +0000265 let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
266 let fname = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
267
268 " handle compressed archives
269 if tarfile =~# '\.gz'
270 call system("gzip -d ".tarfile)
271 let tarfile = substitute(tarfile,'\.gz','','e')
272 let compress= "gzip '".tarfile."'"
273 elseif tarfile =~# '\.tgz'
274 call system("gzip -d ".tarfile)
275 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
276 let compress= "gzip '".tarfile."'"
277 let tgz = 1
278 elseif tarfile =~# '\.bz2'
279 call system("bzip2 -d ".tarfile)
280 let tarfile = substitute(tarfile,'\.bz2','','e')
281 let compress= "bzip2 '".tarfile."'"
Bram Moolenaarab194812005-09-14 21:40:12 +0000282 endif
283
Bram Moolenaarab194812005-09-14 21:40:12 +0000284 if v:shell_error != 0
Bram Moolenaara5792f52005-11-23 21:25:05 +0000285 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
286 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +0000287 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000288
289" call Decho("tarfile<".tarfile."> fname<".fname.">")
290
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000291 if fname =~ '/'
292 let dirpath = substitute(fname,'/[^/]\+$','','e')
293 if executable("cygpath")
294 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
295 endif
296 call mkdir(dirpath,"p")
297 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000298 if tarfile !~ '/'
299 let tarfile= curdir.'/'.tarfile
300 endif
301" call Decho("tarfile<".tarfile."> fname<".fname.">")
302
Bram Moolenaara5792f52005-11-23 21:25:05 +0000303 exe "w! ".fname
304 if executable("cygpath")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000305 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
306 endif
307
308 " delete old file from tarfile
309" call Decho("tar --delete -f '".tarfile."' '".fname."'")
310 call system("tar --delete -f '".tarfile."' '".fname."'")
311 if v:shell_error != 0
312 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
313 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
314 else
315
316 " update tarfile with new file
317" call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
318 call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
319 if v:shell_error != 0
320 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
321 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
322 elseif exists("compress")
323" call Decho("call system(".compress.")")
324 call system(compress)
325 if exists("tgz")
326" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
327 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
328 endif
329 endif
330 endif
331
332 " support writing tarfiles across a network
333 if s:tblfile_{winnr()} =~ '^\a\+://'
334" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
335 let tblfile= s:tblfile_{winnr()}
336 1split|enew
337 let binkeep= &binary
338 let eikeep = &ei
339 set binary ei=all
340 exe "e! ".tarfile
341 call netrw#NetWrite(tblfile)
342 let &ei = eikeep
343 let &binary = binkeep
344 q!
345 unlet s:tblfile_{winnr()}
346 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000347 endif
348
349 " cleanup and restore current directory
350 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000351 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000352 exe "cd ".escape(curdir,' \')
353 setlocal nomod
354
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000355 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000356" call Dret("tar#Write")
357endfun
358
359" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000360" Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000361fun! s:Rmdir(fname)
362" call Dfunc("Rmdir(fname<".a:fname.">)")
363 if has("unix")
364 call system("/bin/rm -rf ".a:fname)
365 elseif has("win32") || has("win95") || has("win64") || has("win16")
366 if &shell =~? "sh$"
367 call system("/bin/rm -rf ".a:fname)
368 else
369 call system("del /S ".a:fname)
370 endif
371 endif
372" call Dret("Rmdir")
373endfun
374
Bram Moolenaara5792f52005-11-23 21:25:05 +0000375" ------------------------------------------------------------------------
376" Modelines And Restoration: {{{1
377let &cpo= s:keepcpo
378unlet s:keepcpo
379" vim:ts=8 fdm=marker