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-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
17 " ---------------------------------------------------------------------
21 if &cp || exists("g:loaded_zip") || v:version < 700
25 let g:loaded_zip = "v16"
26 let s:zipfile_escape = ' ?&;\'
31 " ---------------------------------------------------------------------
33 if !exists("g:zip_shq")
42 if !exists("g:zip_zipcmd")
43 let g:zip_zipcmd= "zip"
45 if !exists("g:zip_unzipcmd")
46 let g:zip_unzipcmd= "unzip"
53 " ---------------------------------------------------------------------
55 fun! zip#Browse(zipfile)
56 " call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
61 if !executable(g:zip_unzipcmd)
63 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
64 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
66 " call Dret("zip#Browse")
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
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()
77 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
80 " call Decho("passed sanity checks")
84 let b:zipfile= a:zipfile
87 setlocal buftype=nofile
88 setlocal bufhidden=hide
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"."'"
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
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()
109 set ei=BufReadCmd,FileReadCmd
113 " call Dret("zip#Browse")
116 " call Decho("line 6: ".getline(6))
117 let namecol= stridx(getline(6),'Name') + 1
118 " call Decho("namecol=".namecol)
123 exe 'silent 4,$s/^.*\%'.namecol.'c//'
126 setlocal noma nomod ro
127 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
130 " call Dret("zip#Browse")
133 " ---------------------------------------------------------------------
134 " ZipBrowseSelect: {{{2
135 fun! s:ZipBrowseSelect()
136 " call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
139 let fname= getline(".")
144 " call Dret("ZipBrowseSelect")
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()
152 " call Dret("ZipBrowseSelect")
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.">")
165 if !exists("g:zip_nomax") || g:zip_nomax == 0
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)
174 " call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
177 " ---------------------------------------------------------------------
179 fun! zip#Read(fname,mode)
180 " call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
185 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
186 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
188 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
189 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
190 let fname = substitute(fname, '[', '[[]', 'g')
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)
203 " call Dret("zip#Read")
206 " ---------------------------------------------------------------------
208 fun! zip#Write(fname)
209 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
214 if !executable(g:zip_zipcmd)
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()
219 " call Dret("zip#Write")
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()
227 " call Dret("zip#Write")
232 let tmpdir= tempname()
233 " call Decho("orig tempname<".tmpdir.">")
235 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
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")
243 " call Dret("zip#Write")
246 " call Decho("current directory now: ".getcwd())
248 " place temporary files under .../_ZIPVIM_/
249 if isdirectory("_ZIPVIM_")
250 call s:Rmdir("_ZIPVIM_")
252 call mkdir("_ZIPVIM_")
254 " call Decho("current directory now: ".getcwd())
257 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
258 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
260 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
261 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
263 " call Decho("zipfile<".zipfile.">")
264 " call Decho("fname <".fname.">")
267 let dirpath = substitute(fname,'/[^/]\+$','','e')
268 if executable("cygpath")
269 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
271 " call Decho("mkdir(dirpath<".dirpath.">,p)")
272 call mkdir(dirpath,"p")
275 let zipfile= curdir.'/'.zipfile
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')
284 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
285 let fname = substitute(fname, '[', '[[]', 'g')
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
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.">")
304 call netrw#NetWrite(netzipfile)
306 let &binary = binkeep
308 unlet s:zipfile_{winnr()}
311 " cleanup and restore current directory
313 call s:Rmdir("_ZIPVIM_")
314 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
319 " call Dret("zip#Write")
322 " ---------------------------------------------------------------------
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
330 " ---------------------------------------------------------------------
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,' ')
338 let newdir= escape(a:newdir,'\ ')
343 catch /^Vim\%((\a\+)\)\=:E344/
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
352 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
353 " call Dret("ChgDir 1")
357 " call Dret("ChgDir 0")
361 " ---------------------------------------------------------------------
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))
368 call system("/bin/rm -rf ".s:QuoteFileDir(a:fname))
373 " ------------------------------------------------------------------------
374 " Modelines And Restoration: {{{1
377 " vim:ts=8 fdm=marker