1 " zip.vim: Handles browsing zipfiles
5 " Maintainer: Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
6 " License: Vim License (see vim's :help license)
7 " Copyright: Copyright (C) 2005 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 " zipPlugin.vim is provided *as is* and comes with no warranty
12 " of any kind, either expressed or implied. By using this
13 " plugin, you agree that in no event will the copyright
14 " holder be liable for any damages resulting from the use
17 " ---------------------------------------------------------------------
21 if &cp || exists("g:loaded_zip") || v:version < 700
25 let g:loaded_zip = "v14"
26 let s:zipfile_escape = ' ?&;\'
31 " ---------------------------------------------------------------------
33 if !exists("g:zip_shq")
40 if !exists("g:zip_zipcmd")
41 let g:zip_zipcmd= "zip"
43 if !exists("g:zip_unzipcmd")
44 let g:zip_unzipcmd= "unzip"
51 " ---------------------------------------------------------------------
53 fun! zip#Browse(zipfile)
54 " call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
59 if !executable(g:zip_unzipcmd)
61 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
62 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
64 " call Dret("zip#Browse")
67 if !filereadable(a:zipfile)
68 if a:zipfile !~# '^\a\+://'
69 " if its an url, don't complain, let url-handlers such as vim do its thing
71 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
72 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
75 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
78 " call Decho("passed sanity checks")
82 let b:zipfile= a:zipfile
85 setlocal buftype=nofile
86 setlocal bufhidden=hide
92 exe "$put ='".'\"'." zip.vim version ".g:loaded_zip."'"
93 exe "$put ='".'\"'." Browsing zipfile ".a:zipfile."'"
94 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
99 " call Decho("exe silent r! ".g:zip_unzipcmd." -l ".s:QuoteFileDir(a:zipfile))
100 exe "silent r! ".g:zip_unzipcmd." -l ".s:QuoteFileDir(a:zipfile)
101 if v:shell_error != 0
103 echohl WarningMsg | echo "***warning*** (zip#Browse) ".a:zipfile." is not a zip file" | echohl None
104 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
107 set ei=BufReadCmd,FileReadCmd
111 " call Dret("zip#Browse")
114 " call Decho("line 6: ".getline(6))
115 let namecol= stridx(getline(6),'Name') + 1
116 " call Decho("namecol=".namecol)
121 exe 'silent 4,$s/^.*\%'.namecol.'c//'
124 setlocal noma nomod ro
125 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
128 " call Dret("zip#Browse")
131 " ---------------------------------------------------------------------
132 " ZipBrowseSelect: {{{2
133 fun! s:ZipBrowseSelect()
134 " call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
137 let fname= getline(".")
142 " call Dret("ZipBrowseSelect")
147 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
148 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
150 " call Dret("ZipBrowseSelect")
154 " call Decho("fname<".fname.">")
156 " get zipfile to the new-window
157 let zipfile = b:zipfile
158 let curfile= expand("%")
159 " call Decho("zipfile<".zipfile.">")
160 " call Decho("curfile<".curfile.">")
164 let s:zipfile_{winnr()}= curfile
165 " call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape))
166 exe "e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape)
170 " call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
173 " ---------------------------------------------------------------------
175 fun! zip#Read(fname,mode)
176 " call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
181 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
182 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
184 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
185 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
186 let fname = substitute(fname, '[', '[[]', 'g')
188 " call Decho("zipfile<".zipfile.">")
189 " call Decho("fname <".fname.">")
191 " call Decho("exe r! ".g:zip_unzipcmd." -p ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname))
192 exe "silent r! ".g:zip_unzipcmd." -p ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname)
199 " call Dret("zip#Read")
202 " ---------------------------------------------------------------------
204 fun! zip#Write(fname)
205 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
210 if !executable(g:zip_zipcmd)
212 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
213 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
215 " call Dret("zip#Write")
220 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
221 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
223 " call Dret("zip#Write")
228 let tmpdir= tempname()
229 " call Decho("orig tempname<".tmpdir.">")
231 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
233 " call Decho("tmpdir<".tmpdir.">")
234 call mkdir(tmpdir,"p")
236 " attempt to change to the indicated directory
237 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
239 " call Dret("zip#Write")
242 " call Decho("current directory now: ".getcwd())
244 " place temporary files under .../_ZIPVIM_/
245 if isdirectory("_ZIPVIM_")
246 call s:Rmdir("_ZIPVIM_")
248 call mkdir("_ZIPVIM_")
250 " call Decho("current directory now: ".getcwd())
253 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
254 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
256 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
257 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
259 " call Decho("zipfile<".zipfile.">")
260 " call Decho("fname <".fname.">")
263 let dirpath = substitute(fname,'/[^/]\+$','','e')
264 if executable("cygpath")
265 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
267 " call Decho("mkdir(dirpath<".dirpath.">,p)")
268 call mkdir(dirpath,"p")
271 let zipfile= curdir.'/'.zipfile
273 " call Decho("zipfile<".zipfile."> fname<".fname.">")
275 exe "w! ".escape(fname,s:zipfile_escape)
276 if executable("cygpath")
277 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
280 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
281 let fname = substitute(fname, '[', '[[]', 'g')
284 " call Decho(g:zip_zipcmd." -u ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname))
285 call system(g:zip_zipcmd." -u ".s:QuoteFileDir(zipfile)." ".s:QuoteFileDir(fname))
286 if v:shell_error != 0
288 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
289 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
291 elseif s:zipfile_{winnr()} =~ '^\a\+://'
292 " support writing zipfiles across a network
293 let netzipfile= s:zipfile_{winnr()}
294 " call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
300 call netrw#NetWrite(netzipfile)
302 let &binary = binkeep
304 unlet s:zipfile_{winnr()}
307 " cleanup and restore current directory
309 call s:Rmdir("_ZIPVIM_")
310 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
315 " call Dret("zip#Write")
318 " ---------------------------------------------------------------------
320 fun! s:QuoteFileDir(fname)
321 " call Dfunc("QuoteFileDir(fname<".a:fname.">)")
322 " call Dret("QuoteFileDir")
323 return g:zip_shq.a:fname.g:zip_shq
326 " ---------------------------------------------------------------------
328 fun! s:ChgDir(newdir,errlvl,errmsg)
329 " call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
331 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
332 let newdir= escape(a:newdir,' ')
334 let newdir= escape(a:newdir,'\ ')
339 catch /^Vim\%((\a\+)\)\=:E344/
341 if a:errlvl == s:NOTE
342 echo "***note*** ".a:errmsg
343 elseif a:errlvl == s:WARNING
344 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
345 elseif a:errlvl == s:ERROR
346 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
348 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
349 " call Dret("ChgDir 1")
353 " call Dret("ChgDir 0")
357 " ---------------------------------------------------------------------
360 " call Dfunc("Rmdir(fname<".a:fname.">)")
361 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
362 call system("rmdir /S/Q ".s:QuoteFileDir(a:fname))
364 call system("/bin/rm -rf ".s:QuoteFileDir(a:fname))
369 " ------------------------------------------------------------------------
370 " Modelines And Restoration: {{{1
373 " vim:ts=8 fdm=marker