530
|
1 " zip.vim: Handles browsing zipfiles
|
|
2 " AUTOLOAD PORTION
|
3281
|
3 " Date: Jan 17, 2012
|
|
4 " Version: 25
|
1121
|
5 " Maintainer: Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
|
|
6 " License: Vim License (see vim's :help license)
|
3281
|
7 " Copyright: Copyright (C) 2005-2012 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
|
2152
|
19 if &cp || exists("g:loaded_zip")
|
|
20 finish
|
|
21 endif
|
3281
|
22 let g:loaded_zip= "v25"
|
2152
|
23 if v:version < 702
|
|
24 echohl WarningMsg
|
|
25 echo "***warning*** this version of zip needs vim 7.2"
|
|
26 echohl Normal
|
|
27 finish
|
|
28 endif
|
530
|
29 let s:keepcpo= &cpo
|
|
30 set cpo&vim
|
|
31
|
798
|
32 let s:zipfile_escape = ' ?&;\'
|
1121
|
33 let s:ERROR = 2
|
|
34 let s:WARNING = 1
|
|
35 let s:NOTE = 0
|
|
36
|
|
37 " ---------------------------------------------------------------------
|
|
38 " Global Values: {{{1
|
|
39 if !exists("g:zip_shq")
|
1621
|
40 if &shq != ""
|
|
41 let g:zip_shq= &shq
|
|
42 elseif has("unix")
|
1121
|
43 let g:zip_shq= "'"
|
|
44 else
|
|
45 let g:zip_shq= '"'
|
|
46 endif
|
|
47 endif
|
1214
|
48 if !exists("g:zip_zipcmd")
|
|
49 let g:zip_zipcmd= "zip"
|
|
50 endif
|
|
51 if !exists("g:zip_unzipcmd")
|
|
52 let g:zip_unzipcmd= "unzip"
|
|
53 endif
|
530
|
54
|
|
55 " ----------------
|
|
56 " Functions: {{{1
|
|
57 " ----------------
|
|
58
|
|
59 " ---------------------------------------------------------------------
|
|
60 " zip#Browse: {{{2
|
|
61 fun! zip#Browse(zipfile)
|
|
62 " call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
|
568
|
63 let repkeep= &report
|
|
64 set report=10
|
530
|
65
|
|
66 " sanity checks
|
1698
|
67 if !exists("*fnameescape")
|
|
68 if &verbose > 1
|
|
69 echoerr "the zip plugin is not available (your vim doens't support fnameescape())"
|
|
70 endif
|
|
71 return
|
|
72 endif
|
1214
|
73 if !executable(g:zip_unzipcmd)
|
1121
|
74 redraw!
|
530
|
75 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
|
1121
|
76 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
77 let &report= repkeep
|
530
|
78 " call Dret("zip#Browse")
|
|
79 return
|
|
80 endif
|
|
81 if !filereadable(a:zipfile)
|
557
|
82 if a:zipfile !~# '^\a\+://'
|
|
83 " if its an url, don't complain, let url-handlers such as vim do its thing
|
1121
|
84 redraw!
|
557
|
85 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
|
1121
|
86 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
87 endif
|
568
|
88 let &report= repkeep
|
557
|
89 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
|
530
|
90 return
|
|
91 endif
|
798
|
92 " call Decho("passed sanity checks")
|
530
|
93 if &ma != 1
|
|
94 set ma
|
|
95 endif
|
1214
|
96 let b:zipfile= a:zipfile
|
530
|
97
|
|
98 setlocal noswapfile
|
|
99 setlocal buftype=nofile
|
|
100 setlocal bufhidden=hide
|
|
101 setlocal nobuflisted
|
|
102 setlocal nowrap
|
|
103 set ft=tar
|
|
104
|
|
105 " give header
|
2908
|
106 call append(0, ['" zip.vim version '.g:loaded_zip,
|
3281
|
107 \ '" Browsing zipfile '.a:zipfile,
|
|
108 \ '" Select a file with cursor and press ENTER'])
|
2908
|
109 keepj $
|
530
|
110
|
1698
|
111 " call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
|
3281
|
112 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
|
857
|
113 if v:shell_error != 0
|
1121
|
114 redraw!
|
1668
|
115 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
|
1121
|
116 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
2908
|
117 keepj sil! %d
|
857
|
118 let eikeep= &ei
|
|
119 set ei=BufReadCmd,FileReadCmd
|
2908
|
120 exe "keepj r ".fnameescape(a:zipfile)
|
857
|
121 let &ei= eikeep
|
2908
|
122 keepj 1d
|
857
|
123 " call Dret("zip#Browse")
|
|
124 return
|
|
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
|
2908
|
196 " call Decho("exe r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1))
|
|
197 exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
|
|
198 filetype detect
|
530
|
199
|
|
200 " cleanup
|
2908
|
201 keepj 0d
|
530
|
202 set nomod
|
|
203
|
568
|
204 let &report= repkeep
|
530
|
205 " call Dret("zip#Read")
|
|
206 endfun
|
|
207
|
|
208 " ---------------------------------------------------------------------
|
|
209 " zip#Write: {{{2
|
|
210 fun! zip#Write(fname)
|
819
|
211 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
568
|
212 let repkeep= &report
|
|
213 set report=10
|
530
|
214
|
|
215 " sanity checks
|
1214
|
216 if !executable(g:zip_zipcmd)
|
1121
|
217 redraw!
|
530
|
218 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
|
1121
|
219 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
220 let &report= repkeep
|
530
|
221 " call Dret("zip#Write")
|
|
222 return
|
|
223 endif
|
|
224 if !exists("*mkdir")
|
1121
|
225 redraw!
|
530
|
226 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
|
1121
|
227 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
228 let &report= repkeep
|
530
|
229 " call Dret("zip#Write")
|
|
230 return
|
|
231 endif
|
|
232
|
|
233 let curdir= getcwd()
|
|
234 let tmpdir= tempname()
|
|
235 " call Decho("orig tempname<".tmpdir.">")
|
|
236 if tmpdir =~ '\.'
|
|
237 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
|
|
238 endif
|
|
239 " call Decho("tmpdir<".tmpdir.">")
|
|
240 call mkdir(tmpdir,"p")
|
|
241
|
|
242 " attempt to change to the indicated directory
|
1121
|
243 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
|
568
|
244 let &report= repkeep
|
530
|
245 " call Dret("zip#Write")
|
|
246 return
|
1121
|
247 endif
|
530
|
248 " call Decho("current directory now: ".getcwd())
|
|
249
|
|
250 " place temporary files under .../_ZIPVIM_/
|
|
251 if isdirectory("_ZIPVIM_")
|
|
252 call s:Rmdir("_ZIPVIM_")
|
|
253 endif
|
|
254 call mkdir("_ZIPVIM_")
|
|
255 cd _ZIPVIM_
|
|
256 " call Decho("current directory now: ".getcwd())
|
|
257
|
857
|
258 if has("unix")
|
|
259 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
|
|
260 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
|
|
261 else
|
|
262 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
|
|
263 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
|
|
264 endif
|
|
265 " call Decho("zipfile<".zipfile.">")
|
|
266 " call Decho("fname <".fname.">")
|
623
|
267
|
|
268 if fname =~ '/'
|
|
269 let dirpath = substitute(fname,'/[^/]\+$','','e')
|
|
270 if executable("cygpath")
|
1668
|
271 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
|
623
|
272 endif
|
819
|
273 " call Decho("mkdir(dirpath<".dirpath.">,p)")
|
623
|
274 call mkdir(dirpath,"p")
|
|
275 endif
|
530
|
276 if zipfile !~ '/'
|
|
277 let zipfile= curdir.'/'.zipfile
|
|
278 endif
|
|
279 " call Decho("zipfile<".zipfile."> fname<".fname.">")
|
|
280
|
1668
|
281 exe "w! ".fnameescape(fname)
|
530
|
282 if executable("cygpath")
|
1668
|
283 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
|
530
|
284 endif
|
|
285
|
1121
|
286 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
|
|
287 let fname = substitute(fname, '[', '[[]', 'g')
|
|
288 endif
|
|
289
|
1698
|
290 " call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
|
|
291 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
|
530
|
292 if v:shell_error != 0
|
1121
|
293 redraw!
|
530
|
294 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
|
1121
|
295 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
296
|
|
297 elseif s:zipfile_{winnr()} =~ '^\a\+://'
|
|
298 " support writing zipfiles across a network
|
|
299 let netzipfile= s:zipfile_{winnr()}
|
1121
|
300 " call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
|
557
|
301 1split|enew
|
|
302 let binkeep= &binary
|
|
303 let eikeep = &ei
|
|
304 set binary ei=all
|
1668
|
305 exe "e! ".fnameescape(zipfile)
|
557
|
306 call netrw#NetWrite(netzipfile)
|
|
307 let &ei = eikeep
|
|
308 let &binary = binkeep
|
|
309 q!
|
|
310 unlet s:zipfile_{winnr()}
|
530
|
311 endif
|
|
312
|
|
313 " cleanup and restore current directory
|
|
314 cd ..
|
|
315 call s:Rmdir("_ZIPVIM_")
|
1121
|
316 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
|
|
317 call s:Rmdir(tmpdir)
|
530
|
318 setlocal nomod
|
|
319
|
568
|
320 let &report= repkeep
|
530
|
321 " call Dret("zip#Write")
|
|
322 endfun
|
|
323
|
|
324 " ---------------------------------------------------------------------
|
1668
|
325 " s:Escape: {{{2
|
|
326 fun! s:Escape(fname,isfilt)
|
|
327 " call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
|
|
328 if exists("*shellescape")
|
|
329 if a:isfilt
|
|
330 let qnameq= shellescape(a:fname,1)
|
|
331 else
|
|
332 let qnameq= shellescape(a:fname)
|
|
333 endif
|
1621
|
334 else
|
|
335 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
|
|
336 endif
|
|
337 " call Dret("QuoteFileDir <".qnameq.">")
|
|
338 return qnameq
|
1121
|
339 endfun
|
|
340
|
|
341 " ---------------------------------------------------------------------
|
|
342 " ChgDir: {{{2
|
|
343 fun! s:ChgDir(newdir,errlvl,errmsg)
|
|
344 " call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
|
|
345
|
|
346 try
|
1698
|
347 exe "cd ".fnameescape(a: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
|