530
|
1 " zip.vim: Handles browsing zipfiles
|
|
2 " AUTOLOAD PORTION
|
798
|
3 " Date: Mar 22, 2006
|
|
4 " Version: 7
|
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
|
798
|
25 let g:loaded_zip = "v7"
|
|
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
|
798
|
78 call Decho("exe silent r! unzip -l '".escape(a:zipfile,s:zipfile_escape)."'")
|
|
79 exe "silent r! unzip -l '".escape(a:zipfile,s:zipfile_escape)."'"
|
530
|
80 $d
|
|
81 silent 4,$v/^\s\+\d\+\s\{0,5}\d/d
|
|
82 silent 4,$s/^\%(.*\)\s\+\(\S\)/\1/
|
|
83
|
|
84 setlocal noma nomod ro
|
|
85 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
|
|
86
|
568
|
87 let &report= repkeep
|
530
|
88 " call Dret("zip#Browse")
|
|
89 endfun
|
|
90
|
|
91 " ---------------------------------------------------------------------
|
|
92 " ZipBrowseSelect: {{{2
|
|
93 fun! s:ZipBrowseSelect()
|
557
|
94 " call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
|
568
|
95 let repkeep= &report
|
|
96 set report=10
|
530
|
97 let fname= getline(".")
|
|
98
|
|
99 " sanity check
|
|
100 if fname =~ '^"'
|
568
|
101 let &report= repkeep
|
530
|
102 " call Dret("ZipBrowseSelect")
|
|
103 return
|
|
104 endif
|
|
105 if fname =~ '/$'
|
|
106 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
|
|
107 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
108 let &report= repkeep
|
530
|
109 " call Dret("ZipBrowseSelect")
|
|
110 return
|
|
111 endif
|
|
112
|
|
113 " call Decho("fname<".fname.">")
|
|
114
|
|
115 " get zipfile to the new-window
|
|
116 let zipfile= substitute(w:zipfile,'.zip$','','e')
|
798
|
117 let curfile= escape(expand("%"),s:zipfile_escape)
|
|
118 " call Decho("zipfile<".zipfile.">")
|
|
119 " call Decho("curfile<".curfile.">")
|
530
|
120
|
|
121 new
|
|
122 wincmd _
|
557
|
123 let s:zipfile_{winnr()}= curfile
|
798
|
124 " call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).':'.fname)
|
|
125 exe "e zipfile:".escape(zipfile,s:zipfile_escape).':'.fname
|
530
|
126 filetype detect
|
|
127
|
568
|
128 let &report= repkeep
|
557
|
129 " call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
530
|
130 endfun
|
|
131
|
|
132 " ---------------------------------------------------------------------
|
|
133 " zip#Read: {{{2
|
|
134 fun! zip#Read(fname,mode)
|
|
135 " call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
|
568
|
136 let repkeep= &report
|
|
137 set report=10
|
|
138
|
798
|
139 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):[^\\].*$','\1','')
|
|
140 let fname = substitute(a:fname,'zipfile:.\{-}:\([^\\].*\)$','\1','')
|
530
|
141 " call Decho("zipfile<".zipfile."> fname<".fname.">")
|
|
142
|
798
|
143 " call Decho("exe r! unzip -p '".escape(zipfile,s:zipfile_escape)."' ".fname)
|
|
144 exe "r! unzip -p '".escape(zipfile,s:zipfile_escape)."' ".fname
|
530
|
145
|
|
146 " cleanup
|
|
147 0d
|
|
148 set nomod
|
|
149
|
568
|
150 let &report= repkeep
|
530
|
151 " call Dret("zip#Read")
|
|
152 endfun
|
|
153
|
|
154 " ---------------------------------------------------------------------
|
|
155 " zip#Write: {{{2
|
|
156 fun! zip#Write(fname)
|
557
|
157 " call Dfunc("zip#Write(fname<".a:fname.") zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
|
568
|
158 let repkeep= &report
|
|
159 set report=10
|
530
|
160
|
|
161 " sanity checks
|
|
162 if !executable("zip")
|
|
163 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
|
|
164 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
165 let &report= repkeep
|
530
|
166 " call Dret("zip#Write")
|
|
167 return
|
|
168 endif
|
|
169 if !exists("*mkdir")
|
|
170 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
|
|
171 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
172 let &report= repkeep
|
530
|
173 " call Dret("zip#Write")
|
|
174 return
|
|
175 endif
|
|
176
|
|
177 let curdir= getcwd()
|
|
178 let tmpdir= tempname()
|
|
179 " call Decho("orig tempname<".tmpdir.">")
|
|
180 if tmpdir =~ '\.'
|
|
181 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
|
|
182 endif
|
|
183 " call Decho("tmpdir<".tmpdir.">")
|
|
184 call mkdir(tmpdir,"p")
|
|
185
|
|
186 " attempt to change to the indicated directory
|
|
187 try
|
|
188 exe "cd ".escape(tmpdir,' \')
|
|
189 catch /^Vim\%((\a\+)\)\=:E344/
|
|
190 echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | Echohl None
|
|
191 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
568
|
192 let &report= repkeep
|
530
|
193 " call Dret("zip#Write")
|
|
194 return
|
|
195 endtry
|
|
196 " call Decho("current directory now: ".getcwd())
|
|
197
|
|
198 " place temporary files under .../_ZIPVIM_/
|
|
199 if isdirectory("_ZIPVIM_")
|
|
200 call s:Rmdir("_ZIPVIM_")
|
|
201 endif
|
|
202 call mkdir("_ZIPVIM_")
|
|
203 cd _ZIPVIM_
|
|
204 " call Decho("current directory now: ".getcwd())
|
|
205
|
|
206 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','')
|
|
207 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','')
|
623
|
208
|
|
209 if fname =~ '/'
|
|
210 let dirpath = substitute(fname,'/[^/]\+$','','e')
|
|
211 if executable("cygpath")
|
|
212 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
|
|
213 endif
|
|
214 call mkdir(dirpath,"p")
|
|
215 endif
|
530
|
216 if zipfile !~ '/'
|
|
217 let zipfile= curdir.'/'.zipfile
|
|
218 endif
|
|
219 " call Decho("zipfile<".zipfile."> fname<".fname.">")
|
|
220
|
|
221 exe "w! ".fname
|
|
222 if executable("cygpath")
|
|
223 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
|
|
224 endif
|
|
225
|
|
226 " call Decho("zip -u ".zipfile.".zip ".fname)
|
|
227 call system("zip -u ".zipfile.".zip ".fname)
|
|
228 if v:shell_error != 0
|
|
229 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
|
|
230 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
|
557
|
231
|
|
232 elseif s:zipfile_{winnr()} =~ '^\a\+://'
|
|
233 " support writing zipfiles across a network
|
|
234 let netzipfile= s:zipfile_{winnr()}
|
|
235 " call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">")
|
|
236 1split|enew
|
|
237 let binkeep= &binary
|
|
238 let eikeep = &ei
|
|
239 set binary ei=all
|
|
240 exe "e! ".zipfile.".zip"
|
|
241 call netrw#NetWrite(netzipfile)
|
|
242 let &ei = eikeep
|
|
243 let &binary = binkeep
|
|
244 q!
|
|
245 unlet s:zipfile_{winnr()}
|
530
|
246 endif
|
|
247
|
|
248 " cleanup and restore current directory
|
|
249 cd ..
|
|
250 call s:Rmdir("_ZIPVIM_")
|
|
251 exe "cd ".escape(curdir,' \')
|
|
252 setlocal nomod
|
|
253
|
568
|
254 let &report= repkeep
|
530
|
255 " call Dret("zip#Write")
|
|
256 endfun
|
|
257
|
|
258 " ---------------------------------------------------------------------
|
|
259 " Rmdir: {{{2
|
|
260 fun! s:Rmdir(fname)
|
|
261 " call Dfunc("Rmdir(fname<".a:fname.">)")
|
|
262 if has("unix")
|
|
263 call system("/bin/rm -rf ".a:fname)
|
|
264 elseif has("win32") || has("win95") || has("win64") || has("win16")
|
|
265 if &shell =~? "sh$"
|
|
266 call system("/bin/rm -rf ".a:fname)
|
|
267 else
|
|
268 call system("del /S ".a:fname)
|
|
269 endif
|
|
270 endif
|
|
271 " call Dret("Rmdir")
|
|
272 endfun
|
|
273
|
|
274 " ------------------------------------------------------------------------
|
|
275 " Modelines And Restoration: {{{1
|
|
276 let &cpo= s:keepcpo
|
|
277 unlet s:keepcpo
|
|
278 " vim:ts=8 fdm=marker
|