Runtime files update
[MacVim.git] / runtime / autoload / tar.vim
blob3fb8d97fa4440c838178b2f911071dc20ddfdc93
1 " tar.vim: Handles browsing tarfiles
2 "            AUTOLOAD PORTION
3 " Date:                 Jan 07, 2008
4 " Version:              13
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= "v13"
28 "call Decho("loading autoload/tar.vim")
30 " ---------------------------------------------------------------------
31 "  Default Settings: {{{1
32 if !exists("g:tar_browseoptions")
33  let g:tar_browseoptions= "Ptf"
34 endif
35 if !exists("g:tar_readoptions")
36  let g:tar_readoptions= "OPxf"
37 endif
38 if !exists("g:tar_cmd")
39  let g:tar_cmd= "tar"
40 endif
41 if !exists("g:tar_writeoptions")
42  let g:tar_writeoptions= "uf"
43 endif
44 if !exists("g:tar_shq")
45  if &shq != ""
46   let g:tar_shq= &shq
47  elseif has("unix")
48   let g:tar_shq= "'"
49  else
50   let g:tar_shq= '"'
51  endif
52 endif
54 " ----------------
55 "  Functions: {{{1
56 " ----------------
58 " ---------------------------------------------------------------------
59 " tar#Browse: {{{2
60 fun! tar#Browse(tarfile)
61 "  call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
62   let repkeep= &report
63   set report=10
65   " sanity checks
66   if !executable(g:tar_cmd)
67    redraw!
68    echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
69 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
70    let &report= repkeep
71 "   call Dret("tar#Browse")
72    return
73   endif
74   if !filereadable(a:tarfile)
75 "   call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
76    if a:tarfile !~# '^\a\+://'
77     " if its an url, don't complain, let url-handlers such as vim do its thing
78     redraw!
79     echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
80 "    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
81    endif
82    let &report= repkeep
83 "   call Dret("tar#Browse : file<".a:tarfile."> not readable")
84    return
85   endif
86   if &ma != 1
87    set ma
88   endif
89   let w:tarfile= a:tarfile
91   setlocal noswapfile
92   setlocal buftype=nofile
93   setlocal bufhidden=hide
94   setlocal nobuflisted
95   setlocal nowrap
96   set ft=tar
98   " give header
99 "  call Decho("printing header")
100   exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'"
101   exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'"
102   exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
103   0d
104   $
106   let tarfile= a:tarfile
107   if has("win32") && executable("cygpath")
108    " assuming cygwin
109    let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
110   endif
111   let curlast= line("$")
112   if tarfile =~# '\.\(gz\|tgz\)$'
113 "   call Decho("exe silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
114    exe "silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - "
115   elseif tarfile =~# '\.lrp'
116    exe "silent r! cat ".g:tar_shq.tarfile.g:tar_shq."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
117   elseif tarfile =~# '\.bz2$'
118 "   call Decho("exe silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
119    exe "silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - "
120   else
121 "   call Decho("exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".g:tar_shq.tarfile.g:tar_shq)
122    exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".g:tar_shq.tarfile.g:tar_shq
123   endif
124   if v:shell_error != 0
125    redraw!
126    echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
127 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
128 "   call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
129    return
130   endif
131   if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
132    redraw!
133    echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
134 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
135    silent %d
136    let eikeep= &ei
137    set ei=BufReadCmd,FileReadCmd
138    exe "r ".a:tarfile
139    let &ei= eikeep
140    1d
141 "   call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
142    return
143   endif
145   setlocal noma nomod ro
146   noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
148   let &report= repkeep
149 "  call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
150 endfun
152 " ---------------------------------------------------------------------
153 " TarBrowseSelect: {{{2
154 fun! s:TarBrowseSelect()
155 "  call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
156   let repkeep= &report
157   set report=10
158   let fname= getline(".")
159 "  call Decho("fname<".fname.">")
161   " sanity check
162   if fname =~ '^"'
163    let &report= repkeep
164 "   call Dret("TarBrowseSelect")
165    return
166   endif
168   " about to make a new window, need to use w:tarfile
169   let tarfile= w:tarfile
170   let curfile= expand("%")
171   if has("win32") && executable("cygpath")
172    " assuming cygwin
173    let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
174   endif
176   new
177   if !exists("g:tar_nomax") || g:tar_nomax == 0
178    wincmd _
179   endif
180   let s:tblfile_{winnr()}= curfile
181   call tar#Read("tarfile:".tarfile.':'.fname,1)
182   filetype detect
184   let &report= repkeep
185 "  call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
186 endfun
188 " ---------------------------------------------------------------------
189 " tar#Read: {{{2
190 fun! tar#Read(fname,mode)
191 "  call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
192   let repkeep= &report
193   set report=10
194   let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
195   let fname   = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
196   if has("win32") && executable("cygpath")
197    " assuming cygwin
198    let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
199   endif
200 "  call Decho("tarfile<".tarfile.">")
201 "  call Decho("fname<".fname.">")
203   if tarfile =~# '\.\(gz\|tgz\)$'
204 "   call Decho("exe silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq)
205    exe "silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq
206   elseif tarfile =~# '\.lrp$'
207 "   call Decho("exe silent r! cat ".g:tar_shq.tarfile.g:tar_shq." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq)
208    exe "silent r! cat ".g:tar_shq.tarfile.g:tar_shq." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq
209   elseif tarfile =~# '\.bz2$'
210 "   call Decho("exe silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq)
211    exe "silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq
212   else
213 "   call Decho("exe silent r! ".g:tar_cmd." -".g:tar_readoptions." ".g:tar_shq.tarfile.g:tar_shq." ".g:tar_shq.fname.g:tar_shq)
214    exe "silent r! ".g:tar_cmd." -".g:tar_readoptions." ".g:tar_shq.tarfile.g:tar_shq." ".g:tar_shq.fname.g:tar_shq
215   endif
216   let w:tarfile= a:fname
217   exe "file tarfile:".fname
219   " cleanup
220   0d
221   set nomod
223   let &report= repkeep
224 "  call Dret("tar#Read : w:tarfile<".w:tarfile.">")
225 endfun
227 " ---------------------------------------------------------------------
228 " tar#Write: {{{2
229 fun! tar#Write(fname)
230 "  call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
231   let repkeep= &report
232   set report=10
234   " sanity checks
235   if !executable(g:tar_cmd)
236    redraw!
237    echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
238 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
239    let &report= repkeep
240 "   call Dret("tar#Write")
241    return
242   endif
243   if !exists("*mkdir")
244    redraw!
245    echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
246 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
247    let &report= repkeep
248 "   call Dret("tar#Write")
249    return
250   endif
252   let curdir= getcwd()
253   let tmpdir= tempname()
254 "  call Decho("orig tempname<".tmpdir.">")
255   if tmpdir =~ '\.'
256    let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
257   endif
258 "  call Decho("tmpdir<".tmpdir.">")
259   call mkdir(tmpdir,"p")
261   " attempt to change to the indicated directory
262   try
263    exe "cd ".escape(tmpdir,' \')
264   catch /^Vim\%((\a\+)\)\=:E344/
265    redraw!
266    echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
267 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
268    let &report= repkeep
269 "   call Dret("tar#Write")
270    return
271   endtry
272 "  call Decho("current directory now: ".getcwd())
274   " place temporary files under .../_ZIPVIM_/
275   if isdirectory("_ZIPVIM_")
276    call s:Rmdir("_ZIPVIM_")
277   endif
278   call mkdir("_ZIPVIM_")
279   cd _ZIPVIM_
280 "  call Decho("current directory now: ".getcwd())
282   let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
283   let fname   = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
285   " handle compressed archives
286   if tarfile =~# '\.gz'
287    call system("gzip -d ".tarfile)
288    let tarfile = substitute(tarfile,'\.gz','','e')
289    let compress= "gzip '".tarfile."'"
290   elseif tarfile =~# '\.tgz'
291    call system("gzip -d ".tarfile)
292    let tarfile = substitute(tarfile,'\.tgz','.tar','e')
293    let compress= "gzip '".tarfile."'"
294    let tgz     = 1
295   elseif tarfile =~# '\.bz2'
296    call system("bzip2 -d ".tarfile)
297    let tarfile = substitute(tarfile,'\.bz2','','e')
298    let compress= "bzip2 '".tarfile."'"
299   endif
301   if v:shell_error != 0
302    redraw!
303    echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
304 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
305   else
307 "   call Decho("tarfile<".tarfile."> fname<".fname.">")
309    if fname =~ '/'
310     let dirpath = substitute(fname,'/[^/]\+$','','e')
311     if executable("cygpath")
312      let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
313     endif
314     call mkdir(dirpath,"p")
315    endif
316    if tarfile !~ '/'
317     let tarfile= curdir.'/'.tarfile
318    endif
319 "   call Decho("tarfile<".tarfile."> fname<".fname.">")
321    exe "w! ".fname
322    if executable("cygpath")
323     let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
324    endif
326    " delete old file from tarfile
327 "   call Decho("tar --delete -f '".tarfile."' '".fname."'")
328    call system("tar --delete -f '".tarfile."' '".fname."'")
329    if v:shell_error != 0
330     redraw!
331     echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
332 "    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
333    else
335     " update tarfile with new file 
336 "    call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
337     call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
338     if v:shell_error != 0
339      redraw!
340      echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
341 "     call inputsave()|call input("Press <cr> to continue")|call inputrestore()
342     elseif exists("compress")
343 "     call Decho("call system(".compress.")")
344      call system(compress)
345      if exists("tgz")
346 "      call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
347       call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
348      endif
349     endif
350    endif
352    " support writing tarfiles across a network
353    if s:tblfile_{winnr()} =~ '^\a\+://'
354 "    call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
355     let tblfile= s:tblfile_{winnr()}
356     1split|enew
357     let binkeep= &binary
358     let eikeep = &ei
359     set binary ei=all
360     exe "e! ".tarfile
361     call netrw#NetWrite(tblfile)
362     let &ei     = eikeep
363     let &binary = binkeep
364     q!
365     unlet s:tblfile_{winnr()}
366    endif
367   endif
368   
369   " cleanup and restore current directory
370   cd ..
371   call s:Rmdir("_ZIPVIM_")
372   exe "cd ".escape(curdir,' \')
373   setlocal nomod
375   let &report= repkeep
376 "  call Dret("tar#Write")
377 endfun
379 " ---------------------------------------------------------------------
380 " Rmdir: {{{2
381 fun! s:Rmdir(fname)
382 "  call Dfunc("Rmdir(fname<".a:fname.">)")
383   if has("unix")
384    call system("/bin/rm -rf ".a:fname)
385   elseif has("win32") || has("win95") || has("win64") || has("win16")
386    if &shell =~? "sh$"
387     call system("/bin/rm -rf ".a:fname)
388    else
389     call system("del /S ".a:fname)
390    endif
391   endif
392 "  call Dret("Rmdir")
393 endfun
395 " ------------------------------------------------------------------------
396 " Modelines And Restoration: {{{1
397 let &cpo= s:keepcpo
398 unlet s:keepcpo
399 " vim:ts=8 fdm=marker