1 " zip.vim: Handles browsing zipfiles
5 " Maintainer: Charles E Campbell <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
6 " License: Vim License (see vim's :help license)
7 " Copyright: Copyright (C) 2005-2013 Charles E. Campbell {{{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
16 "redraw!|call DechoSep()|call inputsave()|call input("Press <cr> to continue")|call inputrestore()
18 " ---------------------------------------------------------------------
20 if &cp || exists("g:loaded_zip")
23 let g:loaded_zip= "v27"
26 echo "***warning*** this version of zip needs vim 7.2"
34 let s:zipfile_escape = ' ?&;\'
39 " ---------------------------------------------------------------------
41 if !exists("g:zip_shq")
50 if !exists("g:zip_zipcmd")
51 let g:zip_zipcmd= "zip"
53 if !exists("g:zip_unzipcmd")
54 let g:zip_unzipcmd= "unzip"
61 " ---------------------------------------------------------------------
63 fun! zip#Browse(zipfile)
64 " call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
65 " sanity check: insure that the zipfile has "PK" as its first two letters
66 " (zipped files have a leading PK as a "magic cookie")
67 if !filereadable(a:zipfile) || readfile(a:zipfile, "", 1)[0] !~ '^PK'
68 exe "noautocmd e ".fnameescape(a:zipfile)
69 " call Dret("zip#Browse : not a zipfile<".a:zipfile.">")
72 " call Decho("zip#Browse: a:zipfile<".a:zipfile."> passed PK test - its a zip file")
79 if !exists("*fnameescape")
81 echoerr "the zip plugin is not available (your vim doens't support fnameescape())"
85 if !executable(g:zip_unzipcmd)
87 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
88 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
90 " call Dret("zip#Browse")
93 if !filereadable(a:zipfile)
94 if a:zipfile !~# '^\a\+://'
95 " if its an url, don't complain, let url-handlers such as vim do its thing
97 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
98 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
101 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
104 " call Decho("passed sanity checks")
108 let b:zipfile= a:zipfile
111 setlocal buftype=nofile
112 setlocal bufhidden=hide
118 call append(0, ['" zip.vim version '.g:loaded_zip,
119 \ '" Browsing zipfile '.a:zipfile,
120 \ '" Select a file with cursor and press ENTER'])
123 " call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
124 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
125 if v:shell_error != 0
127 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
128 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
131 set ei=BufReadCmd,FileReadCmd
132 exe "keepj r ".fnameescape(a:zipfile)
135 " call Dret("zip#Browse")
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 " the following code does much the same thing as
209 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
210 " but allows zipfile:... entries in quickfix lists
211 let temp = tempname()
212 " call Decho("using temp file<".temp.">")
213 let fn = expand('%:p')
214 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp
215 " call Decho("exe sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp)
216 sil exe 'keepalt file '.temp
218 sil exe 'keepalt file '.fnameescape(fn)
224 " keepj 0d " used to be needed for the ...r! ... method
228 " call Dret("zip#Read")
231 " ---------------------------------------------------------------------
233 fun! zip#Write(fname)
234 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
239 if !executable(g:zip_zipcmd)
241 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
242 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
244 " call Dret("zip#Write")
249 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
250 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
252 " call Dret("zip#Write")
257 let tmpdir= tempname()
258 " call Decho("orig tempname<".tmpdir.">")
260 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
262 " call Decho("tmpdir<".tmpdir.">")
263 call mkdir(tmpdir,"p")
265 " attempt to change to the indicated directory
266 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
268 " call Dret("zip#Write")
271 " call Decho("current directory now: ".getcwd())
273 " place temporary files under .../_ZIPVIM_/
274 if isdirectory("_ZIPVIM_")
275 call s:Rmdir("_ZIPVIM_")
277 call mkdir("_ZIPVIM_")
279 " call Decho("current directory now: ".getcwd())
282 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
283 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
285 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
286 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
288 " call Decho("zipfile<".zipfile.">")
289 " call Decho("fname <".fname.">")
292 let dirpath = substitute(fname,'/[^/]\+$','','e')
293 if has("win32unix") && executable("cygpath")
294 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
296 " call Decho("mkdir(dirpath<".dirpath.">,p)")
297 call mkdir(dirpath,"p")
300 let zipfile= curdir.'/'.zipfile
302 " call Decho("zipfile<".zipfile."> fname<".fname.">")
304 exe "w! ".fnameescape(fname)
305 if has("win32unix") && executable("cygpath")
306 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
309 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
310 let fname = substitute(fname, '[', '[[]', 'g')
313 " call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
314 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
315 if v:shell_error != 0
317 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
318 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
320 elseif s:zipfile_{winnr()} =~ '^\a\+://'
321 " support writing zipfiles across a network
322 let netzipfile= s:zipfile_{winnr()}
323 " call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
328 exe "e! ".fnameescape(zipfile)
329 call netrw#NetWrite(netzipfile)
331 let &binary = binkeep
333 unlet s:zipfile_{winnr()}
336 " cleanup and restore current directory
338 call s:Rmdir("_ZIPVIM_")
339 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
344 " call Dret("zip#Write")
347 " ---------------------------------------------------------------------
349 fun! s:Escape(fname,isfilt)
350 " call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
351 if exists("*shellescape")
353 let qnameq= shellescape(a:fname,1)
355 let qnameq= shellescape(a:fname)
358 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
360 " call Dret("QuoteFileDir <".qnameq.">")
364 " ---------------------------------------------------------------------
366 fun! s:ChgDir(newdir,errlvl,errmsg)
367 " call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
370 exe "cd ".fnameescape(a:newdir)
371 catch /^Vim\%((\a\+)\)\=:E344/
373 if a:errlvl == s:NOTE
374 echo "***note*** ".a:errmsg
375 elseif a:errlvl == s:WARNING
376 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
377 elseif a:errlvl == s:ERROR
378 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
380 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
381 " call Dret("ChgDir 1")
385 " call Dret("ChgDir 0")
389 " ---------------------------------------------------------------------
392 " call Dfunc("Rmdir(fname<".a:fname.">)")
393 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
394 call system("rmdir /S/Q ".s:Escape(a:fname,0))
396 call system("/bin/rm -rf ".s:Escape(a:fname,0))
401 " ------------------------------------------------------------------------
402 " Modelines And Restoration: {{{1
405 " vim:ts=8 fdm=marker