vim: update release.sh to vim 7.3
[msysgit/mtrensch.git] / share / vim / vim72 / autoload / tar.vim
blobaf6fbca2e1b313a89bae159944a6b3753dab64b3
1 " tar.vim: Handles browsing tarfiles
2 "            AUTOLOAD PORTION
3 " Date:                 Aug 08, 2008
4 " Version:              23 + modifications by Bram
5 " Maintainer:   Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
6 " License:              Vim License  (see vim's :help license)
8 "       Contains many ideas from Michael Toren's <tar.vim>
10 " Copyright:    Copyright (C) 2005-2008 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 "               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.
20 " ---------------------------------------------------------------------
21 " Load Once: {{{1
22 let s:keepcpo= &cpo
23 set cpo&vim
24 if &cp || exists("g:loaded_tar") || v:version < 700
25  finish
26 endif
27 let g:loaded_tar= "v23b"
28 "call Decho("loading autoload/tar.vim")
29 if v:version < 701 || (v:version == 701 && !has("patch299"))
30  echoerr "(autoload/tar.vim) need vim v7.1 with patchlevel 299"
31 endif
33 " ---------------------------------------------------------------------
34 "  Default Settings: {{{1
35 if !exists("g:tar_browseoptions")
36  let g:tar_browseoptions= "Ptf"
37 endif
38 if !exists("g:tar_readoptions")
39  let g:tar_readoptions= "OPxf"
40 endif
41 if !exists("g:tar_cmd")
42  let g:tar_cmd= "tar"
43 endif
44 if !exists("g:tar_writeoptions")
45  let g:tar_writeoptions= "uf"
46 endif
48 if !exists("g:netrw_cygwin")
49  if has("win32") || has("win95") || has("win64") || has("win16")
50   if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
51    let g:netrw_cygwin= 1
52   else
53    let g:netrw_cygwin= 0
54   endif
55  else
56   let g:netrw_cygwin= 0
57  endif
58 endif
60 " set up shell quoting character
61 if !exists("g:tar_shq")
62  if exists("&shq") && &shq != ""
63   let g:tar_shq= &shq
64  elseif has("win32") || has("win95") || has("win64") || has("win16")
65   if exists("g:netrw_cygwin") && g:netrw_cygwin
66    let g:tar_shq= "'"
67   else
68    let g:tar_shq= '"'
69   endif
70  else
71   let g:tar_shq= "'"
72  endif
73 " call Decho("g:tar_shq<".g:tar_shq.">")
74 endif
76 " ----------------
77 "  Functions: {{{1
78 " ----------------
80 " ---------------------------------------------------------------------
81 " tar#Browse: {{{2
82 fun! tar#Browse(tarfile)
83 "  call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
84   let repkeep= &report
85   set report=10
87   " sanity checks
88   if !executable(g:tar_cmd)
89    redraw!
90    echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
91 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
92    let &report= repkeep
93 "   call Dret("tar#Browse")
94    return
95   endif
96   if !filereadable(a:tarfile)
97 "   call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
98    if a:tarfile !~# '^\a\+://'
99     " if its an url, don't complain, let url-handlers such as vim do its thing
100     redraw!
101     echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
102 "    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
103    endif
104    let &report= repkeep
105 "   call Dret("tar#Browse : file<".a:tarfile."> not readable")
106    return
107   endif
108   if &ma != 1
109    set ma
110   endif
111   let w:tarfile= a:tarfile
113   setlocal noswapfile
114   setlocal buftype=nofile
115   setlocal bufhidden=hide
116   setlocal nobuflisted
117   setlocal nowrap
118   set ft=tar
120   " give header
121 "  call Decho("printing header")
122   let lastline= line("$")
123   call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
124   call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
125   call setline(lastline+3,'" Select a file with cursor and press ENTER')
126   $put =''
127   0d
128   $
130   let tarfile= a:tarfile
131   if has("win32") && executable("cygpath")
132    " assuming cygwin
133    let tarfile=substitute(system("cygpath -u ".s:Escape(tarfile,0)),'\n$','','e')
134   endif
135   let curlast= line("$")
136   if tarfile =~# '\.\(gz\|tgz\)$'
137 "   call Decho("1: exe silent r! gzip -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
138    exe "silent r! gzip -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
139   elseif tarfile =~# '\.lrp'
140 "   call Decho("2: exe silent r! cat -- ".s:Escape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - ")
141    exe "silent r! cat -- ".s:Escape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
142   elseif tarfile =~# '\.bz2$'
143 "   call Decho("3: exe silent r! bzip2 -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
144    exe "silent r! bzip2 -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
145   else
146    if tarfile =~ '^\s*-'
147     " A file name starting with a dash may be taken as an option.  Prepend ./ to avoid that.
148     let tarfile = substitute(tarfile, '-', './-', '')
149    endif
150 "   call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".s:Escape(tarfile,1))
151    exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".s:Escape(tarfile,1)
152   endif
153   if v:shell_error != 0
154    redraw!
155    echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
156 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
157 "   call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
158    return
159   endif
160   if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
161    redraw!
162    echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
163 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
164    silent %d
165    let eikeep= &ei
166    set ei=BufReadCmd,FileReadCmd
167    exe "r ".fnameescape(a:tarfile)
168    let &ei= eikeep
169    1d
170 "   call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
171    return
172   endif
174   setlocal noma nomod ro
175   noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
177   let &report= repkeep
178 "  call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
179 endfun
181 " ---------------------------------------------------------------------
182 " TarBrowseSelect: {{{2
183 fun! s:TarBrowseSelect()
184 "  call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
185   let repkeep= &report
186   set report=10
187   let fname= getline(".")
188 "  call Decho("fname<".fname.">")
190   if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
191    redraw!
192    echohl WarningMsg | echo '***error*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"; See :help tar-options'
193 "   call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
194    return
195   endif
197   " sanity check
198   if fname =~ '^"'
199    let &report= repkeep
200 "   call Dret("TarBrowseSelect")
201    return
202   endif
204   " about to make a new window, need to use w:tarfile
205   let tarfile= w:tarfile
206   let curfile= expand("%")
207   if has("win32") && executable("cygpath")
208    " assuming cygwin
209    let tarfile=substitute(system("cygpath -u ".s:Escape(tarfile,0)),'\n$','','e')
210   endif
212   new
213   if !exists("g:tar_nomax") || g:tar_nomax == 0
214    wincmd _
215   endif
216   let s:tblfile_{winnr()}= curfile
217   call tar#Read("tarfile:".tarfile.'::'.fname,1)
218   filetype detect
220   let &report= repkeep
221 "  call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
222 endfun
224 " ---------------------------------------------------------------------
225 " tar#Read: {{{2
226 fun! tar#Read(fname,mode)
227 "  call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
228   let repkeep= &report
229   set report=10
230   let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
231   let fname   = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
232   if has("win32") && executable("cygpath")
233    " assuming cygwin
234    let tarfile=substitute(system("cygpath -u ".s:Escape(tarfile,0)),'\n$','','e')
235   endif
236 "  call Decho("tarfile<".tarfile.">")
237 "  call Decho("fname<".fname.">")
239   if      fname =~ '\.gz$'  && executable("zcat")
240    let decmp= "|zcat"
241    let doro = 1
242   elseif  fname =~ '\.bz2$' && executable("bzcat")
243    let decmp= "|bzcat"
244    let doro = 1
245   else
246    let decmp=""
247    let doro = 0
248    if fname =~ '\.gz$\|\.bz2$\|\.Z$\|\.zip$'
249     setlocal bin
250    endif
251   endif
253   if exists("g:tar_secure")
254    let tar_secure= " -- "
255   else
256    let tar_secure= " "
257   endif
258   if tarfile =~# '\.\(gz\|tgz\)$'
259 "   call Decho("5: exe silent r! gzip -d -c -- ".s:Escape(tarfile,1)."| ".g:tar_cmd.' -'.g:tar_readoptions.' - '.tar_secure.s:Escape(fname,1))
260    exe "silent r! gzip -d -c -- ".s:Escape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp
261   elseif tarfile =~# '\.lrp$'
262 "   call Decho("6: exe silent r! cat ".s:Escape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp)
263    exe "silent r! cat -- ".s:Escape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp
264   elseif tarfile =~# '\.bz2$'
265 "   call Decho("7: exe silent r! bzip2 -d -c ".s:Escape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp)
266    exe "silent r! bzip2 -d -c -- ".s:Escape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp
267   else
268    if tarfile =~ '^\s*-'
269     " A file name starting with a dash may be taken as an option.  Prepend ./ to avoid that.
270     let tarfile = substitute(tarfile, '-', './-', '')
271    endif
272 "   call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions." "s:Escape(tarfile,1).tar_secure..s:Escape(fname,1).decmp)
273    exe "silent r! ".g:tar_cmd." -".g:tar_readoptions." ".s:Escape(tarfile,1).tar_secure.s:Escape(fname,1).decmp
274   endif
276   if doro
277    " because the reverse process of compressing changed files back into the tarball is not currently supported
278    setlocal ro
279   endif
281   let w:tarfile= a:fname
282   exe "file tarfile::".fnameescape(fname)
284   " cleanup
285   0d
286   set nomod
288   let &report= repkeep
289 "  call Dret("tar#Read : w:tarfile<".w:tarfile.">")
290 endfun
292 " ---------------------------------------------------------------------
293 " tar#Write: {{{2
294 fun! tar#Write(fname)
295 "  call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
296   let repkeep= &report
297   set report=10
299   if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
300    redraw!
301    echohl WarningMsg | echo '***error*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"; See :help tar-options'
302 "   call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
303    return
304   endif
306   " sanity checks
307   if !executable(g:tar_cmd)
308    redraw!
309    echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
310 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
311    let &report= repkeep
312 "   call Dret("tar#Write")
313    return
314   endif
315   if !exists("*mkdir")
316    redraw!
317    echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
318 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
319    let &report= repkeep
320 "   call Dret("tar#Write")
321    return
322   endif
324   let curdir= getcwd()
325   let tmpdir= tempname()
326 "  call Decho("orig tempname<".tmpdir.">")
327   if tmpdir =~ '\.'
328    let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
329   endif
330 "  call Decho("tmpdir<".tmpdir.">")
331   call mkdir(tmpdir,"p")
333   " attempt to change to the indicated directory
334   try
335    exe "cd ".fnameescape(tmpdir)
336   catch /^Vim\%((\a\+)\)\=:E344/
337    redraw!
338    echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
339 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
340    let &report= repkeep
341 "   call Dret("tar#Write")
342    return
343   endtry
344 "  call Decho("current directory now: ".getcwd())
346   " place temporary files under .../_ZIPVIM_/
347   if isdirectory("_ZIPVIM_")
348    call s:Rmdir("_ZIPVIM_")
349   endif
350   call mkdir("_ZIPVIM_")
351   cd _ZIPVIM_
352 "  call Decho("current directory now: ".getcwd())
354   let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
355   let fname   = substitute(w:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
357   " handle compressed archives
358   if tarfile =~# '\.gz'
359    call system("gzip -d -- ".s:Escape(tarfile,0))
360    let tarfile = substitute(tarfile,'\.gz','','e')
361    let compress= "gzip -- ".s:Escape(tarfile,0)
362 "   call Decho("compress<".compress.">")
363   elseif tarfile =~# '\.tgz'
364    call system("gzip -d -- ".s:Escape(tarfile,0))
365    let tarfile = substitute(tarfile,'\.tgz','.tar','e')
366    let compress= "gzip -- ".s:Escape(tarfile,0)
367    let tgz     = 1
368 "   call Decho("compress<".compress.">")
369   elseif tarfile =~# '\.bz2'
370    call system("bzip2 -d -- ".s:Escape(tarfile,0))
371    let tarfile = substitute(tarfile,'\.bz2','','e')
372    let compress= "bzip2 -- ".s:Escape(tarfile,0)
373 "   call Decho("compress<".compress.">")
374   endif
375 "  call Decho("tarfile<".tarfile.">")
377   if v:shell_error != 0
378    redraw!
379    echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
380 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
381   else
383 "   call Decho("tarfile<".tarfile."> fname<".fname.">")
385    if fname =~ '/'
386     let dirpath = substitute(fname,'/[^/]\+$','','e')
387     if executable("cygpath")
388      let dirpath = substitute(system("cygpath ".s:Escape(dirpath, 0)),'\n','','e')
389     endif
390     call mkdir(dirpath,"p")
391    endif
392    if tarfile !~ '/'
393     let tarfile= curdir.'/'.tarfile
394    endif
395    if tarfile =~ '^\s*-'
396     " A file name starting with a dash may be taken as an option.  Prepend ./ to avoid that.
397     let tarfile = substitute(tarfile, '-', './-', '')
398    endif
399 "   call Decho("tarfile<".tarfile."> fname<".fname.">")
401    if exists("g:tar_secure")
402     let tar_secure= " -- "
403    else
404     let tar_secure= " "
405    endif
406    exe "w! ".fnameescape(fname)
407    if executable("cygpath")
408     let tarfile = substitute(system("cygpath ".s:Escape(tarfile,0)),'\n','','e')
409    endif
411    " delete old file from tarfile
412 "   call Decho("system(".g:tar_cmd." --delete -f ".s:Escape(tarfile,0)." -- ".s:Escape(fname,0).")")
413    call system(g:tar_cmd." --delete -f ".s:Escape(tarfile,0).tar_secure.s:Escape(fname,0))
414    if v:shell_error != 0
415     redraw!
416     echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
417 "    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
418    else
420     " update tarfile with new file 
421 "    call Decho(g:tar_cmd." -".g:tar_writeoptions." ".s:Escape(tarfile,0).tar_secure.s:Escape(fname,0))
422     call system(g:tar_cmd." -".g:tar_writeoptions." ".s:Escape(tarfile,0).tar_secure.s:Escape(fname,0))
423     if v:shell_error != 0
424      redraw!
425      echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
426 "     call inputsave()|call input("Press <cr> to continue")|call inputrestore()
427     elseif exists("compress")
428 "     call Decho("call system(".compress.")")
429      call system(compress)
430      if exists("tgz")
431 "      call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
432       call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
433      endif
434     endif
435    endif
437    " support writing tarfiles across a network
438    if s:tblfile_{winnr()} =~ '^\a\+://'
439 "    call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
440     let tblfile= s:tblfile_{winnr()}
441     1split|enew
442     let binkeep= &binary
443     let eikeep = &ei
444     set binary ei=all
445     exe "e! ".fnameescape(tarfile)
446     call netrw#NetWrite(tblfile)
447     let &ei     = eikeep
448     let &binary = binkeep
449     q!
450     unlet s:tblfile_{winnr()}
451    endif
452   endif
453   
454   " cleanup and restore current directory
455   cd ..
456   call s:Rmdir("_ZIPVIM_")
457   exe "cd ".fnameescape(curdir)
458   setlocal nomod
460   let &report= repkeep
461 "  call Dret("tar#Write")
462 endfun
464 " ---------------------------------------------------------------------
465 " Rmdir: {{{2
466 fun! s:Rmdir(fname)
467 "  call Dfunc("Rmdir(fname<".a:fname.">)")
468   if has("unix")
469    call system("/bin/rm -rf -- ".s:Escape(a:fname,0))
470   elseif has("win32") || has("win95") || has("win64") || has("win16")
471    if &shell =~? "sh$"
472     call system("/bin/rm -rf -- ".s:Escape(a:fname,0))
473    else
474     call system("del /S ".s:Escape(a:fname,0))
475    endif
476   endif
477 "  call Dret("Rmdir")
478 endfun
480 " ---------------------------------------------------------------------
481 " s:Escape: {{{2
482 fun s:Escape(name,isfilt)
483   " shellescape() was added by patch 7.0.111
484   if exists("*shellescape")
485    if a:isfilt
486     let qnameq= shellescape(a:name,1)
487    else
488     let qnameq= shellescape(a:name)
489    endif
490   else
491    let qnameq= g:tar_shq . a:name . g:tar_shq
492   endif
493   return qnameq
494 endfun
496 " ---------------------------------------------------------------------
497 " Modelines And Restoration: {{{1
498 let &cpo= s:keepcpo
499 unlet s:keepcpo
500 " vim:ts=8 fdm=marker