530
|
1 " zip.vim: Handles browsing zipfiles
|
|
2 " AUTOLOAD PORTION
|
1698
|
3 " Date: Jul 30, 2008
|
|
4 " Version: 22
|
1121
|
5 " Maintainer: Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
|
|
6 " License: Vim License (see vim's :help license)
|
1621
|
7 " Copyright: Copyright (C) 2005-2008 Charles E. Campbell, Jr. {{{1
|
530
|
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,
|
1621
|
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
|
530
|
14 " holder be liable for any damages resulting from the use
|
|
15 " of this software.
|
|
16
|
|
17 " ---------------------------------------------------------------------
|
1121
|
18 " Load Once: {{{1
|
530
|
19 let s:keepcpo= &cpo
|
|
20 set cpo&vim
|
1121
|
21 if &cp || exists("g:loaded_zip") || v:version < 700
|
530
|
22 finish
|
|
23 endif
|
|
24
|
1698
|
25 let g:loaded_zip = "v22"
|
798
|
26 let s:zipfile_escape = ' ?&;\'
|
1121
|
27 let s:ERROR = 2
|
|
28 let s:WARNING = 1
|
|
29 let s:NOTE = 0
|
|
30
|
|
31 " ---------------------------------------------------------------------
|
|
32 " Global Values: {{{1
|
|
33 if !exists("g:zip_shq")
|
1621
|
34 if &shq != ""
|
|
35 let g:zip_shq= &shq
|
|
36 elseif has("unix")
|
1121
|
37 let g:zip_shq= "'"
|
|
38 else
|
|
39 let g:zip_shq= '"'
|
|
40 endif
|
|
41 endif
|
1214
|
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
|
530
|
48
|
|
49 " ----------------
|
|
50 " Functions: {{{1
|
|
51 " ----------------
|
|
52
|
|
53 " ---------------------------------------------------------------------
|
|
54 " zip#Browse: {{{2
|
|
55 fun! zip#Browse(zipfile)
|
|
56 " call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
|
568
|
57 let repkeep= &report
|
|
58 set report=10
|
530
|
59
|
|
60 " sanity checks
|
1698
|
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
|
1214
|
67 if !executable(g:zip_unzipcmd)
|
1121
|
68 redraw!
|
530
|
69 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
|
1121
|
70 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
71 let &report= repkeep
|
530
|
72 " call Dret("zip#Browse")
|
|
73 return
|
|
74 endif
|
|
75 if !filereadable(a:zipfile)
|
557
|
76 if a:zipfile !~# '^\a\+://'
|
|
77 " if its an url, don't complain, let url-handlers such as vim do its thing
|
1121
|
78 redraw!
|
557
|
79 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
|
1121
|
80 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
81 endif
|
568
|
82 let &report= repkeep
|
557
|
83 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
|
530
|
84 return
|
|
85 endif
|
798
|
86 " call Decho("passed sanity checks")
|
530
|
87 if &ma != 1
|
|
88 set ma
|
|
89 endif
|
1214
|
90 let b:zipfile= a:zipfile
|
530
|
91
|
|
92 setlocal noswapfile
|
|
93 setlocal buftype=nofile
|
|
94 setlocal bufhidden=hide
|
|
95 setlocal nobuflisted
|
|
96 setlocal nowrap
|
|
97 set ft=tar
|
|
98
|
|
99 " give header
|
1668
|
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')
|
530
|
104 $put =''
|
|
105 0d
|
|
106 $
|
|
107
|
1698
|
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)
|
857
|
110 if v:shell_error != 0
|
1121
|
111 redraw!
|
1668
|
112 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
|
1121
|
113 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
857
|
114 silent %d
|
|
115 let eikeep= &ei
|
|
116 set ei=BufReadCmd,FileReadCmd
|
1668
|
117 exe "r ".fnameescape(a:zipfile)
|
857
|
118 let &ei= eikeep
|
|
119 1d
|
|
120 " call Dret("zip#Browse")
|
|
121 return
|
|
122 endif
|
819
|
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
|
530
|
128 $d
|
826
|
129 if namecol > 0
|
|
130 exe 'silent 4,$s/^.*\%'.namecol.'c//'
|
|
131 endif
|
530
|
132
|
|
133 setlocal noma nomod ro
|
|
134 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
|
|
135
|
568
|
136 let &report= repkeep
|
530
|
137 " call Dret("zip#Browse")
|
|
138 endfun
|
|
139
|
|
140 " ---------------------------------------------------------------------
|
|
141 " ZipBrowseSelect: {{{2
|
|
142 fun! s:ZipBrowseSelect()
|
1214
|
143 " call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
|
568
|
144 let repkeep= &report
|
|
145 set report=10
|
530
|
146 let fname= getline(".")
|
|
147
|
|
148 " sanity check
|
|
149 if fname =~ '^"'
|
568
|
150 let &report= repkeep
|
530
|
151 " call Dret("ZipBrowseSelect")
|
|
152 return
|
|
153 endif
|
|
154 if fname =~ '/$'
|
1121
|
155 redraw!
|
530
|
156 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
|
1121
|
157 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
158 let &report= repkeep
|
530
|
159 " call Dret("ZipBrowseSelect")
|
|
160 return
|
|
161 endif
|
|
162
|
|
163 " call Decho("fname<".fname.">")
|
|
164
|
|
165 " get zipfile to the new-window
|
1214
|
166 let zipfile = b:zipfile
|
819
|
167 let curfile= expand("%")
|
798
|
168 " call Decho("zipfile<".zipfile.">")
|
|
169 " call Decho("curfile<".curfile.">")
|
530
|
170
|
|
171 new
|
1621
|
172 if !exists("g:zip_nomax") || g:zip_nomax == 0
|
|
173 wincmd _
|
|
174 endif
|
557
|
175 let s:zipfile_{winnr()}= curfile
|
1668
|
176 " call Decho("exe e ".fnameescape("zipfile:".zipfile.'::'.fname))
|
|
177 exe "e ".fnameescape("zipfile:".zipfile.'::'.fname)
|
530
|
178 filetype detect
|
|
179
|
568
|
180 let &report= repkeep
|
557
|
181 " call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
530
|
182 endfun
|
|
183
|
|
184 " ---------------------------------------------------------------------
|
|
185 " zip#Read: {{{2
|
|
186 fun! zip#Read(fname,mode)
|
|
187 " call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
|
568
|
188 let repkeep= &report
|
|
189 set report=10
|
|
190
|
857
|
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','')
|
1121
|
197 let fname = substitute(fname, '[', '[[]', 'g')
|
857
|
198 endif
|
|
199 " call Decho("zipfile<".zipfile.">")
|
|
200 " call Decho("fname <".fname.">")
|
530
|
201
|
1698
|
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)
|
530
|
204
|
|
205 " cleanup
|
|
206 0d
|
|
207 set nomod
|
|
208
|
568
|
209 let &report= repkeep
|
530
|
210 " call Dret("zip#Read")
|
|
211 endfun
|
|
212
|
|
213 " ---------------------------------------------------------------------
|
|
214 " zip#Write: {{{2
|
|
215 fun! zip#Write(fname)
|
819
|
216 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
568
|
217 let repkeep= &report
|
|
218 set report=10
|
530
|
219
|
|
220 " sanity checks
|
1214
|
221 if !executable(g:zip_zipcmd)
|
1121
|
222 redraw!
|
530
|
223 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
|
1121
|
224 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
225 let &report= repkeep
|
530
|
226 " call Dret("zip#Write")
|
|
227 return
|
|
228 endif
|
|
229 if !exists("*mkdir")
|
1121
|
230 redraw!
|
530
|
231 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
|
1121
|
232 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
233 let &report= repkeep
|
530
|
234 " call Dret("zip#Write")
|
|
235 return
|
|
236 endif
|
|
237
|
|
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")
|
|
246
|
|
247 " attempt to change to the indicated directory
|
1121
|
248 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
|
568
|
249 let &report= repkeep
|
530
|
250 " call Dret("zip#Write")
|
|
251 return
|
1121
|
252 endif
|
530
|
253 " call Decho("current directory now: ".getcwd())
|
|
254
|
|
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())
|
|
262
|
857
|
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.">")
|
623
|
272
|
|
273 if fname =~ '/'
|
|
274 let dirpath = substitute(fname,'/[^/]\+$','','e')
|
|
275 if executable("cygpath")
|
1668
|
276 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
|
623
|
277 endif
|
819
|
278 " call Decho("mkdir(dirpath<".dirpath.">,p)")
|
623
|
279 call mkdir(dirpath,"p")
|
|
280 endif
|
530
|
281 if zipfile !~ '/'
|
|
282 let zipfile= curdir.'/'.zipfile
|
|
283 endif
|
|
284 " call Decho("zipfile<".zipfile."> fname<".fname.">")
|
|
285
|
1668
|
286 exe "w! ".fnameescape(fname)
|
530
|
287 if executable("cygpath")
|
1668
|
288 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
|
530
|
289 endif
|
|
290
|
1121
|
291 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
|
|
292 let fname = substitute(fname, '[', '[[]', 'g')
|
|
293 endif
|
|
294
|
1698
|
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))
|
530
|
297 if v:shell_error != 0
|
1121
|
298 redraw!
|
530
|
299 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
|
1121
|
300 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
301
|
|
302 elseif s:zipfile_{winnr()} =~ '^\a\+://'
|
|
303 " support writing zipfiles across a network
|
|
304 let netzipfile= s:zipfile_{winnr()}
|
1121
|
305 " call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
|
557
|
306 1split|enew
|
|
307 let binkeep= &binary
|
|
308 let eikeep = &ei
|
|
309 set binary ei=all
|
1668
|
310 exe "e! ".fnameescape(zipfile)
|
557
|
311 call netrw#NetWrite(netzipfile)
|
|
312 let &ei = eikeep
|
|
313 let &binary = binkeep
|
|
314 q!
|
|
315 unlet s:zipfile_{winnr()}
|
530
|
316 endif
|
|
317
|
|
318 " cleanup and restore current directory
|
|
319 cd ..
|
|
320 call s:Rmdir("_ZIPVIM_")
|
1121
|
321 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
|
|
322 call s:Rmdir(tmpdir)
|
530
|
323 setlocal nomod
|
|
324
|
568
|
325 let &report= repkeep
|
530
|
326 " call Dret("zip#Write")
|
|
327 endfun
|
|
328
|
|
329 " ---------------------------------------------------------------------
|
1668
|
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
|
1621
|
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
|
1121
|
344 endfun
|
|
345
|
|
346 " ---------------------------------------------------------------------
|
|
347 " ChgDir: {{{2
|
|
348 fun! s:ChgDir(newdir,errlvl,errmsg)
|
|
349 " call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
|
|
350
|
|
351 try
|
1698
|
352 exe "cd ".fnameescape(a:newdir)
|
1121
|
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
|
|
366
|
|
367 " call Dret("ChgDir 0")
|
|
368 return 0
|
|
369 endfun
|
|
370
|
|
371 " ---------------------------------------------------------------------
|
1668
|
372 " s:Rmdir: {{{2
|
530
|
373 fun! s:Rmdir(fname)
|
|
374 " call Dfunc("Rmdir(fname<".a:fname.">)")
|
1121
|
375 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
|
1668
|
376 call system("rmdir /S/Q ".s:Escape(a:fname,0))
|
1121
|
377 else
|
1668
|
378 call system("/bin/rm -rf ".s:Escape(a:fname,0))
|
530
|
379 endif
|
|
380 " call Dret("Rmdir")
|
|
381 endfun
|
|
382
|
|
383 " ------------------------------------------------------------------------
|
|
384 " Modelines And Restoration: {{{1
|
|
385 let &cpo= s:keepcpo
|
|
386 unlet s:keepcpo
|
1214
|
387 " vim:ts=8 fdm=marker
|