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 " ---------------------------------------------------------------------
19 if &cp || exists("g:loaded_zip")
22 let g:loaded_zip= "v23"
25 echo "***warning*** this version of zip needs vim 7.2"
32 let s:zipfile_escape = ' ?&;\'
37 " ---------------------------------------------------------------------
39 if !exists("g:zip_shq")
48 if !exists("g:zip_zipcmd")
49 let g:zip_zipcmd= "zip"
51 if !exists("g:zip_unzipcmd")
52 let g:zip_unzipcmd= "unzip"
59 " ---------------------------------------------------------------------
61 fun! zip#Browse(zipfile)
62 " call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
67 if !exists("*fnameescape")
69 echoerr "the zip plugin is not available (your vim doens't support fnameescape())"
73 if !executable(g:zip_unzipcmd)
75 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
76 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
78 " call Dret("zip#Browse")
81 if !filereadable(a:zipfile)
82 if a:zipfile !~# '^\a\+://'
83 " if its an url, don't complain, let url-handlers such as vim do its thing
85 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
86 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
89 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
92 " call Decho("passed sanity checks")
96 let b:zipfile= a:zipfile
99 setlocal buftype=nofile
100 setlocal bufhidden=hide
106 let lastline= line("$")
107 call setline(lastline+1,'" zip.vim version '.g:loaded_zip)
108 call setline(lastline+2,'" Browsing zipfile '.a:zipfile)
109 call setline(lastline+3,'" Select a file with cursor and press ENTER')
114 " call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
115 exe "silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1)
116 if v:shell_error != 0
118 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
119 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
122 set ei=BufReadCmd,FileReadCmd
123 exe "r ".fnameescape(a:zipfile)
126 " call Dret("zip#Browse")
129 " call Decho("line 6: ".getline(6))
130 let namecol= stridx(getline(6),'Name') + 1
131 " call Decho("namecol=".namecol)
136 exe 'silent 4,$s/^.*\%'.namecol.'c//'
139 setlocal noma nomod ro
140 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
143 " call Dret("zip#Browse")
146 " ---------------------------------------------------------------------
147 " ZipBrowseSelect: {{{2
148 fun! s:ZipBrowseSelect()
149 " call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
152 let fname= getline(".")
157 " call Dret("ZipBrowseSelect")
162 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
163 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
165 " call Dret("ZipBrowseSelect")
169 " call Decho("fname<".fname.">")
171 " get zipfile to the new-window
172 let zipfile = b:zipfile
173 let curfile= expand("%")
174 " call Decho("zipfile<".zipfile.">")
175 " call Decho("curfile<".curfile.">")
178 if !exists("g:zip_nomax") || g:zip_nomax == 0
181 let s:zipfile_{winnr()}= curfile
182 " call Decho("exe e ".fnameescape("zipfile:".zipfile.'::'.fname))
183 exe "e ".fnameescape("zipfile:".zipfile.'::'.fname)
187 " call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
190 " ---------------------------------------------------------------------
192 fun! zip#Read(fname,mode)
193 " call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
198 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
199 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
201 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
202 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
203 let fname = substitute(fname, '[', '[[]', 'g')
205 " call Decho("zipfile<".zipfile.">")
206 " call Decho("fname <".fname.">")
208 " call Decho("exe r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1))
209 exe "silent r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
216 " call Dret("zip#Read")
219 " ---------------------------------------------------------------------
221 fun! zip#Write(fname)
222 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
227 if !executable(g:zip_zipcmd)
229 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
230 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
232 " call Dret("zip#Write")
237 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
238 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
240 " call Dret("zip#Write")
245 let tmpdir= tempname()
246 " call Decho("orig tempname<".tmpdir.">")
248 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
250 " call Decho("tmpdir<".tmpdir.">")
251 call mkdir(tmpdir,"p")
253 " attempt to change to the indicated directory
254 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
256 " call Dret("zip#Write")
259 " call Decho("current directory now: ".getcwd())
261 " place temporary files under .../_ZIPVIM_/
262 if isdirectory("_ZIPVIM_")
263 call s:Rmdir("_ZIPVIM_")
265 call mkdir("_ZIPVIM_")
267 " call Decho("current directory now: ".getcwd())
270 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
271 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
273 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
274 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
276 " call Decho("zipfile<".zipfile.">")
277 " call Decho("fname <".fname.">")
280 let dirpath = substitute(fname,'/[^/]\+$','','e')
281 if executable("cygpath")
282 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
284 " call Decho("mkdir(dirpath<".dirpath.">,p)")
285 call mkdir(dirpath,"p")
288 let zipfile= curdir.'/'.zipfile
290 " call Decho("zipfile<".zipfile."> fname<".fname.">")
292 exe "w! ".fnameescape(fname)
293 if executable("cygpath")
294 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
297 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
298 let fname = substitute(fname, '[', '[[]', 'g')
301 " call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
302 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
303 if v:shell_error != 0
305 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
306 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
308 elseif s:zipfile_{winnr()} =~ '^\a\+://'
309 " support writing zipfiles across a network
310 let netzipfile= s:zipfile_{winnr()}
311 " call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
316 exe "e! ".fnameescape(zipfile)
317 call netrw#NetWrite(netzipfile)
319 let &binary = binkeep
321 unlet s:zipfile_{winnr()}
324 " cleanup and restore current directory
326 call s:Rmdir("_ZIPVIM_")
327 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
332 " call Dret("zip#Write")
335 " ---------------------------------------------------------------------
337 fun! s:Escape(fname,isfilt)
338 " call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
339 if exists("*shellescape")
341 let qnameq= shellescape(a:fname,1)
343 let qnameq= shellescape(a:fname)
346 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
348 " call Dret("QuoteFileDir <".qnameq.">")
352 " ---------------------------------------------------------------------
354 fun! s:ChgDir(newdir,errlvl,errmsg)
355 " call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
358 exe "cd ".fnameescape(a:newdir)
359 catch /^Vim\%((\a\+)\)\=:E344/
361 if a:errlvl == s:NOTE
362 echo "***note*** ".a:errmsg
363 elseif a:errlvl == s:WARNING
364 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
365 elseif a:errlvl == s:ERROR
366 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
368 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
369 " call Dret("ChgDir 1")
373 " call Dret("ChgDir 0")
377 " ---------------------------------------------------------------------
380 " call Dfunc("Rmdir(fname<".a:fname.">)")
381 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
382 call system("rmdir /S/Q ".s:Escape(a:fname,0))
384 call system("/bin/rm -rf ".s:Escape(a:fname,0))
389 " ------------------------------------------------------------------------
390 " Modelines And Restoration: {{{1
393 " vim:ts=8 fdm=marker