530
|
1 " zip.vim: Handles browsing zipfiles
|
|
2 " AUTOLOAD PORTION
|
826
|
3 " Date: Apr 10, 2006
|
|
4 " Version: 8
|
530
|
5 " Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz>
|
|
6 " License: Vim License (see vim's :help license)
|
|
7 " Copyright: Copyright (C) 2005 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 " zipPlugin.vim is provided *as is* and comes with no warranty
|
|
12 " of any kind, either expressed or implied. By using this
|
|
13 " plugin, you agree that in no event will the copyright
|
|
14 " holder be liable for any damages resulting from the use
|
|
15 " of this software.
|
|
16
|
|
17 " ---------------------------------------------------------------------
|
|
18 " Initialization: {{{1
|
|
19 let s:keepcpo= &cpo
|
|
20 set cpo&vim
|
|
21 if exists("g:loaded_zip")
|
|
22 finish
|
|
23 endif
|
|
24
|
826
|
25 let g:loaded_zip = "v8"
|
798
|
26 let s:zipfile_escape = ' ?&;\'
|
530
|
27
|
|
28 " ----------------
|
|
29 " Functions: {{{1
|
|
30 " ----------------
|
|
31
|
|
32 " ---------------------------------------------------------------------
|
|
33 " zip#Browse: {{{2
|
|
34 fun! zip#Browse(zipfile)
|
|
35 " call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
|
568
|
36 let repkeep= &report
|
|
37 set report=10
|
530
|
38
|
|
39 " sanity checks
|
|
40 if !executable("unzip")
|
|
41 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
|
|
42 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
43 let &report= repkeep
|
530
|
44 " call Dret("zip#Browse")
|
|
45 return
|
|
46 endif
|
|
47 if !filereadable(a:zipfile)
|
557
|
48 if a:zipfile !~# '^\a\+://'
|
|
49 " if its an url, don't complain, let url-handlers such as vim do its thing
|
|
50 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
|
|
51 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
|
52 endif
|
568
|
53 let &report= repkeep
|
557
|
54 " call Dret("zip#Browse : file<".a:zipfile."> not readable")
|
530
|
55 return
|
|
56 endif
|
798
|
57 " call Decho("passed sanity checks")
|
530
|
58 if &ma != 1
|
|
59 set ma
|
|
60 endif
|
|
61 let w:zipfile= a:zipfile
|
|
62
|
|
63 setlocal noswapfile
|
|
64 setlocal buftype=nofile
|
|
65 setlocal bufhidden=hide
|
|
66 setlocal nobuflisted
|
|
67 setlocal nowrap
|
|
68 set ft=tar
|
|
69
|
|
70 " give header
|
|
71 exe "$put ='".'\"'." zip.vim version ".g:loaded_zip."'"
|
|
72 exe "$put ='".'\"'." Browsing zipfile ".a:zipfile."'"
|
|
73 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
|
|
74 $put =''
|
|
75 0d
|
|
76 $
|
|
77
|
819
|
78 " call Decho("exe silent r! unzip -l '".a:zipfile."'")
|
|
79 exe "silent r! unzip -l '".a:zipfile."'"
|
|
80 " call Decho("line 6: ".getline(6))
|
|
81 let namecol= stridx(getline(6),'Name') + 1
|
|
82 " call Decho("namecol=".namecol)
|
|
83 4,$g/^\s*----/d
|
|
84 4,$g/^\s*\a/d
|
530
|
85 $d
|
826
|
86 if namecol > 0
|
|
87 exe 'silent 4,$s/^.*\%'.namecol.'c//'
|
|
88 endif
|
530
|
89
|
|
90 setlocal noma nomod ro
|
|
91 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
|
|
92
|
568
|
93 let &report= repkeep
|
530
|
94 " call Dret("zip#Browse")
|
|
95 endfun
|
|
96
|
|
97 " ---------------------------------------------------------------------
|
|
98 " ZipBrowseSelect: {{{2
|
|
99 fun! s:ZipBrowseSelect()
|
557
|
100 " call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
|
568
|
101 let repkeep= &report
|
|
102 set report=10
|
530
|
103 let fname= getline(".")
|
|
104
|
|
105 " sanity check
|
|
106 if fname =~ '^"'
|
568
|
107 let &report= repkeep
|
530
|
108 " call Dret("ZipBrowseSelect")
|
|
109 return
|
|
110 endif
|
|
111 if fname =~ '/$'
|
|
112 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
|
|
113 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
114 let &report= repkeep
|
530
|
115 " call Dret("ZipBrowseSelect")
|
|
116 return
|
|
117 endif
|
|
118
|
|
119 " call Decho("fname<".fname.">")
|
|
120
|
|
121 " get zipfile to the new-window
|
|
122 let zipfile= substitute(w:zipfile,'.zip$','','e')
|
819
|
123 let curfile= expand("%")
|
798
|
124 " call Decho("zipfile<".zipfile.">")
|
|
125 " call Decho("curfile<".curfile.">")
|
530
|
126
|
|
127 new
|
|
128 wincmd _
|
557
|
129 let s:zipfile_{winnr()}= curfile
|
819
|
130 " call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).':'.escape(fname,s:zipfile_escape))
|
|
131 exe "e zipfile:".escape(zipfile,s:zipfile_escape).':'.escape(fname,s:zipfile_escape)
|
530
|
132 filetype detect
|
|
133
|
568
|
134 let &report= repkeep
|
557
|
135 " call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
530
|
136 endfun
|
|
137
|
|
138 " ---------------------------------------------------------------------
|
|
139 " zip#Read: {{{2
|
|
140 fun! zip#Read(fname,mode)
|
|
141 " call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
|
568
|
142 let repkeep= &report
|
|
143 set report=10
|
|
144
|
798
|
145 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):[^\\].*$','\1','')
|
|
146 let fname = substitute(a:fname,'zipfile:.\{-}:\([^\\].*\)$','\1','')
|
530
|
147 " call Decho("zipfile<".zipfile."> fname<".fname.">")
|
|
148
|
819
|
149 " call Decho("exe r! unzip -p '".zipfile."' '".fname."'")
|
|
150 exe "silent r! unzip -p '".zipfile."' '".fname."'"
|
530
|
151
|
|
152 " cleanup
|
|
153 0d
|
|
154 set nomod
|
|
155
|
568
|
156 let &report= repkeep
|
530
|
157 " call Dret("zip#Read")
|
|
158 endfun
|
|
159
|
|
160 " ---------------------------------------------------------------------
|
|
161 " zip#Write: {{{2
|
|
162 fun! zip#Write(fname)
|
819
|
163 " call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
568
|
164 let repkeep= &report
|
|
165 set report=10
|
530
|
166
|
|
167 " sanity checks
|
|
168 if !executable("zip")
|
|
169 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
|
|
170 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
171 let &report= repkeep
|
530
|
172 " call Dret("zip#Write")
|
|
173 return
|
|
174 endif
|
|
175 if !exists("*mkdir")
|
|
176 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
|
|
177 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
178 let &report= repkeep
|
530
|
179 " call Dret("zip#Write")
|
|
180 return
|
|
181 endif
|
|
182
|
|
183 let curdir= getcwd()
|
|
184 let tmpdir= tempname()
|
|
185 " call Decho("orig tempname<".tmpdir.">")
|
|
186 if tmpdir =~ '\.'
|
|
187 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
|
|
188 endif
|
|
189 " call Decho("tmpdir<".tmpdir.">")
|
|
190 call mkdir(tmpdir,"p")
|
|
191
|
|
192 " attempt to change to the indicated directory
|
|
193 try
|
|
194 exe "cd ".escape(tmpdir,' \')
|
|
195 catch /^Vim\%((\a\+)\)\=:E344/
|
|
196 echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | Echohl None
|
|
197 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
198 let &report= repkeep
|
530
|
199 " call Dret("zip#Write")
|
|
200 return
|
|
201 endtry
|
|
202 " call Decho("current directory now: ".getcwd())
|
|
203
|
|
204 " place temporary files under .../_ZIPVIM_/
|
|
205 if isdirectory("_ZIPVIM_")
|
|
206 call s:Rmdir("_ZIPVIM_")
|
|
207 endif
|
|
208 call mkdir("_ZIPVIM_")
|
|
209 cd _ZIPVIM_
|
|
210 " call Decho("current directory now: ".getcwd())
|
|
211
|
|
212 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','')
|
|
213 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','')
|
623
|
214
|
|
215 if fname =~ '/'
|
|
216 let dirpath = substitute(fname,'/[^/]\+$','','e')
|
|
217 if executable("cygpath")
|
|
218 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
|
|
219 endif
|
819
|
220 " call Decho("mkdir(dirpath<".dirpath.">,p)")
|
623
|
221 call mkdir(dirpath,"p")
|
|
222 endif
|
530
|
223 if zipfile !~ '/'
|
|
224 let zipfile= curdir.'/'.zipfile
|
|
225 endif
|
|
226 " call Decho("zipfile<".zipfile."> fname<".fname.">")
|
|
227
|
819
|
228 exe "w! ".escape(fname,s:zipfile_escape)
|
530
|
229 if executable("cygpath")
|
|
230 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
|
|
231 endif
|
|
232
|
819
|
233 " call Decho("zip -u '".zipfile.".zip' '".fname."'")
|
|
234 call system("zip -u '".zipfile.".zip' '".fname."'")
|
530
|
235 if v:shell_error != 0
|
|
236 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
|
|
237 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
238
|
|
239 elseif s:zipfile_{winnr()} =~ '^\a\+://'
|
|
240 " support writing zipfiles across a network
|
|
241 let netzipfile= s:zipfile_{winnr()}
|
|
242 " call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">")
|
|
243 1split|enew
|
|
244 let binkeep= &binary
|
|
245 let eikeep = &ei
|
|
246 set binary ei=all
|
|
247 exe "e! ".zipfile.".zip"
|
|
248 call netrw#NetWrite(netzipfile)
|
|
249 let &ei = eikeep
|
|
250 let &binary = binkeep
|
|
251 q!
|
|
252 unlet s:zipfile_{winnr()}
|
530
|
253 endif
|
|
254
|
|
255 " cleanup and restore current directory
|
|
256 cd ..
|
|
257 call s:Rmdir("_ZIPVIM_")
|
|
258 exe "cd ".escape(curdir,' \')
|
|
259 setlocal nomod
|
|
260
|
568
|
261 let &report= repkeep
|
530
|
262 " call Dret("zip#Write")
|
|
263 endfun
|
|
264
|
|
265 " ---------------------------------------------------------------------
|
|
266 " Rmdir: {{{2
|
|
267 fun! s:Rmdir(fname)
|
|
268 " call Dfunc("Rmdir(fname<".a:fname.">)")
|
|
269 if has("unix")
|
|
270 call system("/bin/rm -rf ".a:fname)
|
|
271 elseif has("win32") || has("win95") || has("win64") || has("win16")
|
|
272 if &shell =~? "sh$"
|
|
273 call system("/bin/rm -rf ".a:fname)
|
|
274 else
|
|
275 call system("del /S ".a:fname)
|
|
276 endif
|
|
277 endif
|
|
278 " call Dret("Rmdir")
|
|
279 endfun
|
|
280
|
|
281 " ------------------------------------------------------------------------
|
|
282 " Modelines And Restoration: {{{1
|
|
283 let &cpo= s:keepcpo
|
|
284 unlet s:keepcpo
|
|
285 " vim:ts=8 fdm=marker
|