Partially synced with the branch.
[MacVim.git] / runtime / autoload / zip.vim
blobaf56944ce314a36de0a4771cb39e1ef6451d5791
1 " zip.vim: Handles browsing zipfiles
2 "            AUTOLOAD PORTION
3 " Date:         Jul 30, 2008
4 " Version:      22
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     = "v22"
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 !exists("*fnameescape")
62    if &verbose > 1
63     echoerr "the zip plugin is not available (your vim doens't support fnameescape())"
64    endif
65    return
66   endif
67   if !executable(g:zip_unzipcmd)
68    redraw!
69    echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
70 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
71    let &report= repkeep
72 "   call Dret("zip#Browse")
73    return
74   endif
75   if !filereadable(a:zipfile)
76    if a:zipfile !~# '^\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*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
80 "    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
81    endif
82    let &report= repkeep
83 "   call Dret("zip#Browse : file<".a:zipfile."> not readable")
84    return
85   endif
86 "  call Decho("passed sanity checks")
87   if &ma != 1
88    set ma
89   endif
90   let b:zipfile= a:zipfile
92   setlocal noswapfile
93   setlocal buftype=nofile
94   setlocal bufhidden=hide
95   setlocal nobuflisted
96   setlocal nowrap
97   set ft=tar
99   " give header
100   let lastline= line("$")
101   call setline(lastline+1,'" zip.vim version '.g:loaded_zip)
102   call setline(lastline+2,'" Browsing zipfile '.a:zipfile)
103   call setline(lastline+3,'" Select a file with cursor and press ENTER')
104   $put =''
105   0d
106   $
108 "  call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
109   exe "silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1)
110   if v:shell_error != 0
111    redraw!
112    echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
113 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
114    silent %d
115    let eikeep= &ei
116    set ei=BufReadCmd,FileReadCmd
117    exe "r ".fnameescape(a:zipfile)
118    let &ei= eikeep
119    1d
120 "   call Dret("zip#Browse")
121    return
122   endif
123 "  call Decho("line 6: ".getline(6))
124   let namecol= stridx(getline(6),'Name') + 1
125 "  call Decho("namecol=".namecol)
126   4,$g/^\s*----/d
127   4,$g/^\s*\a/d
128   $d
129   if namecol > 0
130    exe 'silent 4,$s/^.*\%'.namecol.'c//'
131   endif
133   setlocal noma nomod ro
134   noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
136   let &report= repkeep
137 "  call Dret("zip#Browse")
138 endfun
140 " ---------------------------------------------------------------------
141 " ZipBrowseSelect: {{{2
142 fun! s:ZipBrowseSelect()
143 "  call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
144   let repkeep= &report
145   set report=10
146   let fname= getline(".")
148   " sanity check
149   if fname =~ '^"'
150    let &report= repkeep
151 "   call Dret("ZipBrowseSelect")
152    return
153   endif
154   if fname =~ '/$'
155    redraw!
156    echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
157 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
158    let &report= repkeep
159 "   call Dret("ZipBrowseSelect")
160    return
161   endif
163 "  call Decho("fname<".fname.">")
165   " get zipfile to the new-window
166   let zipfile = b:zipfile
167   let curfile= expand("%")
168 "  call Decho("zipfile<".zipfile.">")
169 "  call Decho("curfile<".curfile.">")
171   new
172   if !exists("g:zip_nomax") || g:zip_nomax == 0
173    wincmd _
174   endif
175   let s:zipfile_{winnr()}= curfile
176 "  call Decho("exe e ".fnameescape("zipfile:".zipfile.'::'.fname))
177   exe "e ".fnameescape("zipfile:".zipfile.'::'.fname)
178   filetype detect
180   let &report= repkeep
181 "  call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
182 endfun
184 " ---------------------------------------------------------------------
185 " zip#Read: {{{2
186 fun! zip#Read(fname,mode)
187 "  call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
188   let repkeep= &report
189   set report=10
191   if has("unix")
192    let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
193    let fname   = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
194   else
195    let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
196    let fname   = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
197    let fname = substitute(fname, '[', '[[]', 'g')
198   endif
199 "  call Decho("zipfile<".zipfile.">")
200 "  call Decho("fname  <".fname.">")
202 "  call Decho("exe r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1))
203   exe "silent r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
205   " cleanup
206   0d
207   set nomod
209   let &report= repkeep
210 "  call Dret("zip#Read")
211 endfun
213 " ---------------------------------------------------------------------
214 " zip#Write: {{{2
215 fun! zip#Write(fname)
216 "  call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
217   let repkeep= &report
218   set report=10
220   " sanity checks
221   if !executable(g:zip_zipcmd)
222    redraw!
223    echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
224 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
225    let &report= repkeep
226 "   call Dret("zip#Write")
227    return
228   endif
229   if !exists("*mkdir")
230    redraw!
231    echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
232 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
233    let &report= repkeep
234 "   call Dret("zip#Write")
235    return
236   endif
238   let curdir= getcwd()
239   let tmpdir= tempname()
240 "  call Decho("orig tempname<".tmpdir.">")
241   if tmpdir =~ '\.'
242    let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
243   endif
244 "  call Decho("tmpdir<".tmpdir.">")
245   call mkdir(tmpdir,"p")
247   " attempt to change to the indicated directory
248   if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
249    let &report= repkeep
250 "   call Dret("zip#Write")
251    return
252   endif
253 "  call Decho("current directory now: ".getcwd())
255   " place temporary files under .../_ZIPVIM_/
256   if isdirectory("_ZIPVIM_")
257    call s:Rmdir("_ZIPVIM_")
258   endif
259   call mkdir("_ZIPVIM_")
260   cd _ZIPVIM_
261 "  call Decho("current directory now: ".getcwd())
263   if has("unix")
264    let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
265    let fname   = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
266   else
267    let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
268    let fname   = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
269   endif
270 "  call Decho("zipfile<".zipfile.">")
271 "  call Decho("fname  <".fname.">")
273   if fname =~ '/'
274    let dirpath = substitute(fname,'/[^/]\+$','','e')
275    if executable("cygpath")
276     let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
277    endif
278 "   call Decho("mkdir(dirpath<".dirpath.">,p)")
279    call mkdir(dirpath,"p")
280   endif
281   if zipfile !~ '/'
282    let zipfile= curdir.'/'.zipfile
283   endif
284 "  call Decho("zipfile<".zipfile."> fname<".fname.">")
286   exe "w! ".fnameescape(fname)
287   if executable("cygpath")
288    let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
289   endif
291   if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
292     let fname = substitute(fname, '[', '[[]', 'g')
293   endif
295 "  call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
296   call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
297   if v:shell_error != 0
298    redraw!
299    echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
300 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
302   elseif s:zipfile_{winnr()} =~ '^\a\+://'
303    " support writing zipfiles across a network
304    let netzipfile= s:zipfile_{winnr()}
305 "   call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
306    1split|enew
307    let binkeep= &binary
308    let eikeep = &ei
309    set binary ei=all
310    exe "e! ".fnameescape(zipfile)
311    call netrw#NetWrite(netzipfile)
312    let &ei     = eikeep
313    let &binary = binkeep
314    q!
315    unlet s:zipfile_{winnr()}
316   endif
317   
318   " cleanup and restore current directory
319   cd ..
320   call s:Rmdir("_ZIPVIM_")
321   call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
322   call s:Rmdir(tmpdir)
323   setlocal nomod
325   let &report= repkeep
326 "  call Dret("zip#Write")
327 endfun
329 " ---------------------------------------------------------------------
330 " s:Escape: {{{2
331 fun! s:Escape(fname,isfilt)
332 "  call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
333   if exists("*shellescape")
334    if a:isfilt
335     let qnameq= shellescape(a:fname,1)
336    else
337     let qnameq= shellescape(a:fname)
338    endif
339   else
340    let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
341   endif
342 "  call Dret("QuoteFileDir <".qnameq.">")
343   return qnameq
344 endfun
346 " ---------------------------------------------------------------------
347 " ChgDir: {{{2
348 fun! s:ChgDir(newdir,errlvl,errmsg)
349 "  call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl."  errmsg<".a:errmsg.">)")
351   try
352    exe "cd ".fnameescape(a:newdir)
353   catch /^Vim\%((\a\+)\)\=:E344/
354    redraw!
355    if a:errlvl == s:NOTE
356     echo "***note*** ".a:errmsg
357    elseif a:errlvl == s:WARNING
358     echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
359    elseif a:errlvl == s:ERROR
360     echohl Error | echo "***error*** ".a:errmsg | echohl NONE
361    endif
362 "   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
363 "   call Dret("ChgDir 1")
364    return 1
365   endtry
367 "  call Dret("ChgDir 0")
368   return 0
369 endfun
371 " ---------------------------------------------------------------------
372 " s:Rmdir: {{{2
373 fun! s:Rmdir(fname)
374 "  call Dfunc("Rmdir(fname<".a:fname.">)")
375   if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
376    call system("rmdir /S/Q ".s:Escape(a:fname,0))
377   else
378    call system("/bin/rm -rf ".s:Escape(a:fname,0))
379   endif
380 "  call Dret("Rmdir")
381 endfun
383 " ------------------------------------------------------------------------
384 " Modelines And Restoration: {{{1
385 let &cpo= s:keepcpo
386 unlet s:keepcpo
387 " vim:ts=8 fdm=marker