530
|
1 " zip.vim: Handles browsing zipfiles
|
|
2 " AUTOLOAD PORTION
|
1668
|
3 " Date: Jul 12, 2008
|
|
4 " Version: 21 (modified by Bram)
|
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
|
1668
|
25 let g:loaded_zip = "v21+b"
|
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
|
1214
|
61 if !executable(g:zip_unzipcmd)
|
1121
|
62 redraw!
|
530
|
63 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
|
1121
|
64 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
65 let &report= repkeep
|
530
|
66 " call Dret("zip#Browse")
|
|
67 return
|
|
68 endif
|
|
69 if !filereadable(a:zipfile)
|
557
|
70 if a:zipfile !~# '^\a\+://'
|
|
71 " if its an url, don't complain, let url-handlers such as vim do its thing
|
1121
|
72 redraw!
|
557
|
73 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
|
1121
|
74 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
75 endif
|
568
|
76 let &report= repkeep
|
557
|
77 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
|
530
|
78 return
|
|
79 endif
|
798
|
80 " call Decho("passed sanity checks")
|
530
|
81 if &ma != 1
|
|
82 set ma
|
|
83 endif
|
1214
|
84 let b:zipfile= a:zipfile
|
530
|
85
|
|
86 setlocal noswapfile
|
|
87 setlocal buftype=nofile
|
|
88 setlocal bufhidden=hide
|
|
89 setlocal nobuflisted
|
|
90 setlocal nowrap
|
|
91 set ft=tar
|
|
92
|
|
93 " give header
|
1668
|
94 let lastline= line("$")
|
|
95 call setline(lastline+1,'" zip.vim version '.g:loaded_zip)
|
|
96 call setline(lastline+2,'" Browsing zipfile '.a:zipfile)
|
|
97 call setline(lastline+3,'" Select a file with cursor and press ENTER')
|
530
|
98 $put =''
|
|
99 0d
|
|
100 $
|
|
101
|
1668
|
102 " call Decho("exe silent r! ".g:zip_unzipcmd." -l ".s:Escape(a:zipfile,1))
|
|
103 exe "silent r! ".g:zip_unzipcmd." -l ".s:Escape(a:zipfile,1)
|
857
|
104 if v:shell_error != 0
|
1121
|
105 redraw!
|
1668
|
106 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
|
1121
|
107 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
857
|
108 silent %d
|
|
109 let eikeep= &ei
|
|
110 set ei=BufReadCmd,FileReadCmd
|
1668
|
111 exe "r ".fnameescape(a:zipfile)
|
857
|
112 let &ei= eikeep
|
|
113 1d
|
|
114 " call Dret("zip#Browse")
|
|
115 return
|
|
116 endif
|
819
|
117 " call Decho("line 6: ".getline(6))
|
|
118 let namecol= stridx(getline(6),'Name') + 1
|
|
119 " call Decho("namecol=".namecol)
|
|
120 4,$g/^\s*----/d
|
|
121 4,$g/^\s*\a/d
|
530
|
122 $d
|
826
|
123 if namecol > 0
|
|
124 exe 'silent 4,$s/^.*\%'.namecol.'c//'
|
|
125 endif
|
530
|
126
|
|
127 setlocal noma nomod ro
|
|
128 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
|
|
129
|
568
|
130 let &report= repkeep
|
530
|
131 " call Dret("zip#Browse")
|
|
132 endfun
|
|
133
|
|
134 " ---------------------------------------------------------------------
|
|
135 " ZipBrowseSelect: {{{2
|
|
136 fun! s:ZipBrowseSelect()
|
1214
|
137 " call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
|
568
|
138 let repkeep= &report
|
|
139 set report=10
|
530
|
140 let fname= getline(".")
|
|
141
|
|
142 " sanity check
|
|
143 if fname =~ '^"'
|
568
|
144 let &report= repkeep
|
530
|
145 " call Dret("ZipBrowseSelect")
|
|
146 return
|
|
147 endif
|
|
148 if fname =~ '/$'
|
1121
|
149 redraw!
|
530
|
150 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
|
1121
|
151 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
152 let &report= repkeep
|
530
|
153 " call Dret("ZipBrowseSelect")
|
|
154 return
|
|
155 endif
|
|
156
|
|
157 " call Decho("fname<".fname.">")
|
|
158
|
|
159 " get zipfile to the new-window
|
1214
|
160 let zipfile = b:zipfile
|
819
|
161 let curfile= expand("%")
|
798
|
162 " call Decho("zipfile<".zipfile.">")
|
|
163 " call Decho("curfile<".curfile.">")
|
530
|
164
|
|
165 new
|
1621
|
166 if !exists("g:zip_nomax") || g:zip_nomax == 0
|
|
167 wincmd _
|
|
168 endif
|
557
|
169 let s:zipfile_{winnr()}= curfile
|
1668
|
170 " call Decho("exe e ".fnameescape("zipfile:".zipfile.'::'.fname))
|
|
171 exe "e ".fnameescape("zipfile:".zipfile.'::'.fname)
|
530
|
172 filetype detect
|
|
173
|
568
|
174 let &report= repkeep
|
557
|
175 " call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
530
|
176 endfun
|
|
177
|
|
178 " ---------------------------------------------------------------------
|
|
179 " zip#Read: {{{2
|
|
180 fun! zip#Read(fname,mode)
|
|
181 " call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
|
568
|
182 let repkeep= &report
|
|
183 set report=10
|
|
184
|
857
|
185 if has("unix")
|
|
186 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
|
|
187 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
|
|
188 else
|
|
189 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
|
|
190 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
|
1121
|
191 let fname = substitute(fname, '[', '[[]', 'g')
|
857
|
192 endif
|
|
193 " call Decho("zipfile<".zipfile.">")
|
|
194 " call Decho("fname <".fname.">")
|
530
|
195
|
1668
|
196 " call Decho("exe r! ".g:zip_unzipcmd." -p ".s:Escape(zipfile,1)." ".s:Escape(fname,1))
|
|
197 exe "silent r! ".g:zip_unzipcmd." -p ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
|
530
|
198
|
|
199 " cleanup
|
|
200 0d
|
|
201 set nomod
|
|
202
|
568
|
203 let &report= repkeep
|
530
|
204 " call Dret("zip#Read")
|
|
205 endfun
|
|
206
|
|
207 " ---------------------------------------------------------------------
|
|
208 " zip#Write: {{{2
|
|
209 fun! zip#Write(fname)
|
819
|
210 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
568
|
211 let repkeep= &report
|
|
212 set report=10
|
530
|
213
|
|
214 " sanity checks
|
1214
|
215 if !executable(g:zip_zipcmd)
|
1121
|
216 redraw!
|
530
|
217 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
|
1121
|
218 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
219 let &report= repkeep
|
530
|
220 " call Dret("zip#Write")
|
|
221 return
|
|
222 endif
|
|
223 if !exists("*mkdir")
|
1121
|
224 redraw!
|
530
|
225 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
|
1121
|
226 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
227 let &report= repkeep
|
530
|
228 " call Dret("zip#Write")
|
|
229 return
|
|
230 endif
|
|
231
|
|
232 let curdir= getcwd()
|
|
233 let tmpdir= tempname()
|
|
234 " call Decho("orig tempname<".tmpdir.">")
|
|
235 if tmpdir =~ '\.'
|
|
236 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
|
|
237 endif
|
|
238 " call Decho("tmpdir<".tmpdir.">")
|
|
239 call mkdir(tmpdir,"p")
|
|
240
|
|
241 " attempt to change to the indicated directory
|
1121
|
242 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
|
568
|
243 let &report= repkeep
|
530
|
244 " call Dret("zip#Write")
|
|
245 return
|
1121
|
246 endif
|
530
|
247 " call Decho("current directory now: ".getcwd())
|
|
248
|
|
249 " place temporary files under .../_ZIPVIM_/
|
|
250 if isdirectory("_ZIPVIM_")
|
|
251 call s:Rmdir("_ZIPVIM_")
|
|
252 endif
|
|
253 call mkdir("_ZIPVIM_")
|
|
254 cd _ZIPVIM_
|
|
255 " call Decho("current directory now: ".getcwd())
|
|
256
|
857
|
257 if has("unix")
|
|
258 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
|
|
259 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
|
|
260 else
|
|
261 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
|
|
262 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
|
|
263 endif
|
|
264 " call Decho("zipfile<".zipfile.">")
|
|
265 " call Decho("fname <".fname.">")
|
623
|
266
|
|
267 if fname =~ '/'
|
|
268 let dirpath = substitute(fname,'/[^/]\+$','','e')
|
|
269 if executable("cygpath")
|
1668
|
270 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
|
623
|
271 endif
|
819
|
272 " call Decho("mkdir(dirpath<".dirpath.">,p)")
|
623
|
273 call mkdir(dirpath,"p")
|
|
274 endif
|
530
|
275 if zipfile !~ '/'
|
|
276 let zipfile= curdir.'/'.zipfile
|
|
277 endif
|
|
278 " call Decho("zipfile<".zipfile."> fname<".fname.">")
|
|
279
|
1668
|
280 exe "w! ".fnameescape(fname)
|
530
|
281 if executable("cygpath")
|
1668
|
282 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
|
530
|
283 endif
|
|
284
|
1121
|
285 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
|
|
286 let fname = substitute(fname, '[', '[[]', 'g')
|
|
287 endif
|
|
288
|
1668
|
289 " call Decho(g:zip_zipcmd." -u ".s:Escape(zipfile,0)." ".s:Escape(fname,0))
|
|
290 call system(g:zip_zipcmd." -u ".s:Escape(zipfile,0)." ".s:Escape(fname,0))
|
530
|
291 if v:shell_error != 0
|
1121
|
292 redraw!
|
530
|
293 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
|
1121
|
294 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
295
|
|
296 elseif s:zipfile_{winnr()} =~ '^\a\+://'
|
|
297 " support writing zipfiles across a network
|
|
298 let netzipfile= s:zipfile_{winnr()}
|
1121
|
299 " call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
|
557
|
300 1split|enew
|
|
301 let binkeep= &binary
|
|
302 let eikeep = &ei
|
|
303 set binary ei=all
|
1668
|
304 exe "e! ".fnameescape(zipfile)
|
557
|
305 call netrw#NetWrite(netzipfile)
|
|
306 let &ei = eikeep
|
|
307 let &binary = binkeep
|
|
308 q!
|
|
309 unlet s:zipfile_{winnr()}
|
530
|
310 endif
|
|
311
|
|
312 " cleanup and restore current directory
|
|
313 cd ..
|
|
314 call s:Rmdir("_ZIPVIM_")
|
1121
|
315 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
|
|
316 call s:Rmdir(tmpdir)
|
530
|
317 setlocal nomod
|
|
318
|
568
|
319 let &report= repkeep
|
530
|
320 " call Dret("zip#Write")
|
|
321 endfun
|
|
322
|
|
323 " ---------------------------------------------------------------------
|
1668
|
324 " s:Escape: {{{2
|
|
325 fun! s:Escape(fname,isfilt)
|
|
326 " call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
|
|
327 if exists("*shellescape")
|
|
328 if a:isfilt
|
|
329 let qnameq= shellescape(a:fname,1)
|
|
330 else
|
|
331 let qnameq= shellescape(a:fname)
|
|
332 endif
|
1621
|
333 else
|
|
334 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
|
|
335 endif
|
|
336 " call Dret("QuoteFileDir <".qnameq.">")
|
|
337 return qnameq
|
1121
|
338 endfun
|
|
339
|
|
340 " ---------------------------------------------------------------------
|
|
341 " ChgDir: {{{2
|
|
342 fun! s:ChgDir(newdir,errlvl,errmsg)
|
|
343 " call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
|
|
344
|
|
345
|
|
346 try
|
1668
|
347 exe "cd ".fnameescape(newdir)
|
1121
|
348 catch /^Vim\%((\a\+)\)\=:E344/
|
|
349 redraw!
|
|
350 if a:errlvl == s:NOTE
|
|
351 echo "***note*** ".a:errmsg
|
|
352 elseif a:errlvl == s:WARNING
|
|
353 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
|
|
354 elseif a:errlvl == s:ERROR
|
|
355 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
|
|
356 endif
|
|
357 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
|
358 " call Dret("ChgDir 1")
|
|
359 return 1
|
|
360 endtry
|
|
361
|
|
362 " call Dret("ChgDir 0")
|
|
363 return 0
|
|
364 endfun
|
|
365
|
|
366 " ---------------------------------------------------------------------
|
1668
|
367 " s:Rmdir: {{{2
|
530
|
368 fun! s:Rmdir(fname)
|
|
369 " call Dfunc("Rmdir(fname<".a:fname.">)")
|
1121
|
370 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
|
1668
|
371 call system("rmdir /S/Q ".s:Escape(a:fname,0))
|
1121
|
372 else
|
1668
|
373 call system("/bin/rm -rf ".s:Escape(a:fname,0))
|
530
|
374 endif
|
|
375 " call Dret("Rmdir")
|
|
376 endfun
|
|
377
|
|
378 " ------------------------------------------------------------------------
|
|
379 " Modelines And Restoration: {{{1
|
|
380 let &cpo= s:keepcpo
|
|
381 unlet s:keepcpo
|
1214
|
382 " vim:ts=8 fdm=marker
|