Runtime files update
[MacVim.git] / runtime / autoload / zip.vim
blob5bb41d63a412f641179b9b49cdfc0cda539ce552
1 " zip.vim: Handles browsing zipfiles
2 "            AUTOLOAD PORTION
3 " Date:         Jan 07, 2008
4 " Version:      16
5 " Maintainer:   Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
6 " License:      Vim License  (see vim's :help license)
7 " Copyright:    Copyright (C) 2005-2008 Charles E. Campbell, Jr. {{{1
8 "               Permission is hereby granted to use and distribute this code,
9 "               with or without modifications, provided that this copyright
10 "               notice is copied with it. Like anything else that's free,
11 "               zip.vim and zipPlugin.vim are provided *as is* and comes with
12 "               no warranty of any kind, either expressed or implied. By using
13 "               this plugin, you agree that in no event will the copyright
14 "               holder be liable for any damages resulting from the use
15 "               of this software.
17 " ---------------------------------------------------------------------
18 " Load Once: {{{1
19 let s:keepcpo= &cpo
20 set cpo&vim
21 if &cp || exists("g:loaded_zip") || v:version < 700
22  finish
23 endif
25 let g:loaded_zip     = "v16"
26 let s:zipfile_escape = ' ?&;\'
27 let s:ERROR          = 2
28 let s:WARNING        = 1
29 let s:NOTE           = 0
31 " ---------------------------------------------------------------------
32 "  Global Values: {{{1
33 if !exists("g:zip_shq")
34  if &shq != ""
35   let g:zip_shq= &shq
36  elseif has("unix")
37   let g:zip_shq= "'"
38  else
39   let g:zip_shq= '"'
40  endif
41 endif
42 if !exists("g:zip_zipcmd")
43  let g:zip_zipcmd= "zip"
44 endif
45 if !exists("g:zip_unzipcmd")
46  let g:zip_unzipcmd= "unzip"
47 endif
49 " ----------------
50 "  Functions: {{{1
51 " ----------------
53 " ---------------------------------------------------------------------
54 " zip#Browse: {{{2
55 fun! zip#Browse(zipfile)
56 "  call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
57   let repkeep= &report
58   set report=10
60   " sanity checks
61   if !executable(g:zip_unzipcmd)
62    redraw!
63    echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
64 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
65    let &report= repkeep
66 "   call Dret("zip#Browse")
67    return
68   endif
69   if !filereadable(a:zipfile)
70    if a:zipfile !~# '^\a\+://'
71     " if its an url, don't complain, let url-handlers such as vim do its thing
72     redraw!
73     echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
74 "    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
75    endif
76    let &report= repkeep
77 "   call Dret("zip#Browse : file<".a:zipfile."> not readable")
78    return
79   endif
80 "  call Decho("passed sanity checks")
81   if &ma != 1
82    set ma
83   endif
84   let b:zipfile= a:zipfile
86   setlocal noswapfile
87   setlocal buftype=nofile
88   setlocal bufhidden=hide
89   setlocal nobuflisted
90   setlocal nowrap
91   set ft=tar
93   " give header
94   exe "$put ='".'\"'." zip.vim version ".g:loaded_zip."'"
95   exe "$put ='".'\"'." Browsing zipfile ".a:zipfile."'"
96   exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
97   $put =''
98   0d
99   $
101 "  call Decho("exe silent r! ".g:zip_unzipcmd." -l ".s:QuoteFileDir(a:zipfile))
102   exe "silent r! ".g:zip_unzipcmd." -l ".s:QuoteFileDir(a:zipfile)
103   if v:shell_error != 0
104    redraw!
105    echohl WarningMsg | echo "***warning*** (zip#Browse) ".a:zipfile." is not a zip file" | echohl None
106 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
107    silent %d
108    let eikeep= &ei
109    set ei=BufReadCmd,FileReadCmd
110    exe "r ".a:zipfile
111    let &ei= eikeep
112    1d
113 "   call Dret("zip#Browse")
114    return
115   endif
116 "  call Decho("line 6: ".getline(6))
117   let namecol= stridx(getline(6),'Name') + 1
118 "  call Decho("namecol=".namecol)
119   4,$g/^\s*----/d
120   4,$g/^\s*\a/d
121   $d
122   if namecol > 0
123    exe 'silent 4,$s/^.*\%'.namecol.'c//'
124   endif
126   setlocal noma nomod ro
127   noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
129   let &report= repkeep
130 "  call Dret("zip#Browse")
131 endfun
133 " ---------------------------------------------------------------------
134 " ZipBrowseSelect: {{{2
135 fun! s:ZipBrowseSelect()
136 "  call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
137   let repkeep= &report
138   set report=10
139   let fname= getline(".")
141   " sanity check
142   if fname =~ '^"'
143    let &report= repkeep
144 "   call Dret("ZipBrowseSelect")
145    return
146   endif
147   if fname =~ '/$'
148    redraw!
149    echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
150 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
151    let &report= repkeep
152 "   call Dret("ZipBrowseSelect")
153    return
154   endif
156 "  call Decho("fname<".fname.">")
158   " get zipfile to the new-window
159   let zipfile = b:zipfile
160   let curfile= expand("%")
161 "  call Decho("zipfile<".zipfile.">")
162 "  call Decho("curfile<".curfile.">")
164   new
165   if !exists("g:zip_nomax") || g:zip_nomax == 0
166    wincmd _
167   endif
168   let s:zipfile_{winnr()}= curfile
169 "  call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape))
170   exe "e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape)
171   filetype detect
173   let &report= repkeep
174 "  call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
175 endfun
177 " ---------------------------------------------------------------------
178 " zip#Read: {{{2
179 fun! zip#Read(fname,mode)
180 "  call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
181   let repkeep= &report
182   set report=10
184   if has("unix")
185    let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
186    let fname   = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
187   else
188    let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
189    let fname   = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
190    let fname = substitute(fname, '[', '[[]', 'g')
191   endif
192 "  call Decho("zipfile<".zipfile.">")
193 "  call Decho("fname  <".fname.">")
195 "  call Decho("exe r! ".g:zip_unzipcmd." -p ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname))
196   exe "silent r! ".g:zip_unzipcmd." -p ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname)
198   " cleanup
199   0d
200   set nomod
202   let &report= repkeep
203 "  call Dret("zip#Read")
204 endfun
206 " ---------------------------------------------------------------------
207 " zip#Write: {{{2
208 fun! zip#Write(fname)
209 "  call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
210   let repkeep= &report
211   set report=10
213   " sanity checks
214   if !executable(g:zip_zipcmd)
215    redraw!
216    echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
217 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
218    let &report= repkeep
219 "   call Dret("zip#Write")
220    return
221   endif
222   if !exists("*mkdir")
223    redraw!
224    echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
225 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
226    let &report= repkeep
227 "   call Dret("zip#Write")
228    return
229   endif
231   let curdir= getcwd()
232   let tmpdir= tempname()
233 "  call Decho("orig tempname<".tmpdir.">")
234   if tmpdir =~ '\.'
235    let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
236   endif
237 "  call Decho("tmpdir<".tmpdir.">")
238   call mkdir(tmpdir,"p")
240   " attempt to change to the indicated directory
241   if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
242    let &report= repkeep
243 "   call Dret("zip#Write")
244    return
245   endif
246 "  call Decho("current directory now: ".getcwd())
248   " place temporary files under .../_ZIPVIM_/
249   if isdirectory("_ZIPVIM_")
250    call s:Rmdir("_ZIPVIM_")
251   endif
252   call mkdir("_ZIPVIM_")
253   cd _ZIPVIM_
254 "  call Decho("current directory now: ".getcwd())
256   if has("unix")
257    let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
258    let fname   = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
259   else
260    let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
261    let fname   = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
262   endif
263 "  call Decho("zipfile<".zipfile.">")
264 "  call Decho("fname  <".fname.">")
266   if fname =~ '/'
267    let dirpath = substitute(fname,'/[^/]\+$','','e')
268    if executable("cygpath")
269     let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
270    endif
271 "   call Decho("mkdir(dirpath<".dirpath.">,p)")
272    call mkdir(dirpath,"p")
273   endif
274   if zipfile !~ '/'
275    let zipfile= curdir.'/'.zipfile
276   endif
277 "  call Decho("zipfile<".zipfile."> fname<".fname.">")
279   exe "w! ".escape(fname,s:zipfile_escape)
280   if executable("cygpath")
281    let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
282   endif
284   if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
285     let fname = substitute(fname, '[', '[[]', 'g')
286   endif
288 "  call Decho(g:zip_zipcmd." -u ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname))
289   call system(g:zip_zipcmd." -u ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname))
290   if v:shell_error != 0
291    redraw!
292    echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
293 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
295   elseif s:zipfile_{winnr()} =~ '^\a\+://'
296    " support writing zipfiles across a network
297    let netzipfile= s:zipfile_{winnr()}
298 "   call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
299    1split|enew
300    let binkeep= &binary
301    let eikeep = &ei
302    set binary ei=all
303    exe "e! ".zipfile
304    call netrw#NetWrite(netzipfile)
305    let &ei     = eikeep
306    let &binary = binkeep
307    q!
308    unlet s:zipfile_{winnr()}
309   endif
310   
311   " cleanup and restore current directory
312   cd ..
313   call s:Rmdir("_ZIPVIM_")
314   call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
315   call s:Rmdir(tmpdir)
316   setlocal nomod
318   let &report= repkeep
319 "  call Dret("zip#Write")
320 endfun
322 " ---------------------------------------------------------------------
323 " QuoteFileDir: {{{2
324 fun! s:QuoteFileDir(fname)
325 "  call Dfunc("QuoteFileDir(fname<".a:fname.">)")
326 "  call Dret("QuoteFileDir")
327   return g:zip_shq.a:fname.g:zip_shq
328 endfun
330 " ---------------------------------------------------------------------
331 " ChgDir: {{{2
332 fun! s:ChgDir(newdir,errlvl,errmsg)
333 "  call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl."  errmsg<".a:errmsg.">)")
335   if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
336    let newdir= escape(a:newdir,' ')
337   else
338    let newdir= escape(a:newdir,'\ ')
339   endif
341   try
342    exe "cd ".newdir
343   catch /^Vim\%((\a\+)\)\=:E344/
344    redraw!
345    if a:errlvl == s:NOTE
346     echo "***note*** ".a:errmsg
347    elseif a:errlvl == s:WARNING
348     echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
349    elseif a:errlvl == s:ERROR
350     echohl Error | echo "***error*** ".a:errmsg | echohl NONE
351    endif
352 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
353 "   call Dret("ChgDir 1")
354    return 1
355   endtry
357 "  call Dret("ChgDir 0")
358   return 0
359 endfun
361 " ---------------------------------------------------------------------
362 " Rmdir: {{{2
363 fun! s:Rmdir(fname)
364 "  call Dfunc("Rmdir(fname<".a:fname.">)")
365   if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
366    call system("rmdir /S/Q ".s:QuoteFileDir(a:fname))
367   else
368    call system("/bin/rm -rf ".s:QuoteFileDir(a:fname))
369   endif
370 "  call Dret("Rmdir")
371 endfun
373 " ------------------------------------------------------------------------
374 " Modelines And Restoration: {{{1
375 let &cpo= s:keepcpo
376 unlet s:keepcpo
377 " vim:ts=8 fdm=marker