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