blob: e0ae9b8b7c8714fa5d11be43f0a0ac9b5ffda2fc [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaard68071d2006-05-02 22:08:30 +00003" Date: May 01, 2006
4" Version: 9
Bram Moolenaar60a795a2005-09-16 21:55:43 +00005" 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
19let s:keepcpo= &cpo
20set cpo&vim
21if exists("g:loaded_zip")
22 finish
23endif
24
Bram Moolenaard68071d2006-05-02 22:08:30 +000025let g:loaded_zip = "v9"
Bram Moolenaardb552d602006-03-23 22:59:57 +000026let s:zipfile_escape = ' ?&;\'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000027
28" ----------------
29" Functions: {{{1
30" ----------------
31
32" ---------------------------------------------------------------------
33" zip#Browse: {{{2
34fun! zip#Browse(zipfile)
35" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
Bram Moolenaar36c31f72005-11-28 23:01:53 +000036 let repkeep= &report
37 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000038
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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +000043 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000044" call Dret("zip#Browse")
45 return
46 endif
47 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +000048 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
Bram Moolenaar36c31f72005-11-28 23:01:53 +000053 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +000054" call Dret("zip#Browse : file<".a:zipfile."> not readable")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000055 return
56 endif
Bram Moolenaardb552d602006-03-23 22:59:57 +000057" call Decho("passed sanity checks")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000058 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
Bram Moolenaar910f66f2006-04-05 20:41:53 +000078" call Decho("exe silent r! unzip -l '".a:zipfile."'")
79 exe "silent r! unzip -l '".a:zipfile."'"
Bram Moolenaard68071d2006-05-02 22:08:30 +000080 if v:shell_error != 0
81 echohl WarningMsg | echo "***warning*** (zip#Browse) ".a:zipfile." is not a zip file" | echohl None
82 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
83 silent %d
84 let eikeep= &ei
85 set ei=BufReadCmd,FileReadCmd
86 exe "r ".a:zipfile
87 let &ei= eikeep
88 1d
89" call Dret("zip#Browse")
90 return
91 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000092" call Decho("line 6: ".getline(6))
93 let namecol= stridx(getline(6),'Name') + 1
94" call Decho("namecol=".namecol)
95 4,$g/^\s*----/d
96 4,$g/^\s*\a/d
Bram Moolenaar60a795a2005-09-16 21:55:43 +000097 $d
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000098 if namecol > 0
99 exe 'silent 4,$s/^.*\%'.namecol.'c//'
100 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000101
102 setlocal noma nomod ro
103 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
104
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000105 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000106" call Dret("zip#Browse")
107endfun
108
109" ---------------------------------------------------------------------
110" ZipBrowseSelect: {{{2
111fun! s:ZipBrowseSelect()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000112" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000113 let repkeep= &report
114 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000115 let fname= getline(".")
116
117 " sanity check
118 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000119 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000120" call Dret("ZipBrowseSelect")
121 return
122 endif
123 if fname =~ '/$'
124 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
125 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000126 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000127" call Dret("ZipBrowseSelect")
128 return
129 endif
130
131" call Decho("fname<".fname.">")
132
133 " get zipfile to the new-window
134 let zipfile= substitute(w:zipfile,'.zip$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000135 let curfile= expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000136" call Decho("zipfile<".zipfile.">")
137" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000138
139 new
140 wincmd _
Bram Moolenaara5792f52005-11-23 21:25:05 +0000141 let s:zipfile_{winnr()}= curfile
Bram Moolenaard68071d2006-05-02 22:08:30 +0000142" call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape))
143 exe "e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000144 filetype detect
145
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000146 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000147" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000148endfun
149
150" ---------------------------------------------------------------------
151" zip#Read: {{{2
152fun! zip#Read(fname,mode)
153" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000154 let repkeep= &report
155 set report=10
156
Bram Moolenaard68071d2006-05-02 22:08:30 +0000157 if has("unix")
158 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
159 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
160 else
161 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
162 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
163 endif
164" call Decho("zipfile<".zipfile.">")
165" call Decho("fname <".fname.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000166
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000167" call Decho("exe r! unzip -p '".zipfile."' '".fname."'")
168 exe "silent r! unzip -p '".zipfile."' '".fname."'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000169
170 " cleanup
171 0d
172 set nomod
173
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000174 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000175" call Dret("zip#Read")
176endfun
177
178" ---------------------------------------------------------------------
179" zip#Write: {{{2
180fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000181" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000182 let repkeep= &report
183 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000184
185 " sanity checks
186 if !executable("zip")
187 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
188 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000189 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000190" call Dret("zip#Write")
191 return
192 endif
193 if !exists("*mkdir")
194 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
195 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000196 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000197" call Dret("zip#Write")
198 return
199 endif
200
201 let curdir= getcwd()
202 let tmpdir= tempname()
203" call Decho("orig tempname<".tmpdir.">")
204 if tmpdir =~ '\.'
205 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
206 endif
207" call Decho("tmpdir<".tmpdir.">")
208 call mkdir(tmpdir,"p")
209
210 " attempt to change to the indicated directory
211 try
212 exe "cd ".escape(tmpdir,' \')
213 catch /^Vim\%((\a\+)\)\=:E344/
214 echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | Echohl None
215 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000216 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000217" call Dret("zip#Write")
218 return
219 endtry
220" call Decho("current directory now: ".getcwd())
221
222 " place temporary files under .../_ZIPVIM_/
223 if isdirectory("_ZIPVIM_")
224 call s:Rmdir("_ZIPVIM_")
225 endif
226 call mkdir("_ZIPVIM_")
227 cd _ZIPVIM_
228" call Decho("current directory now: ".getcwd())
229
Bram Moolenaard68071d2006-05-02 22:08:30 +0000230 if has("unix")
231 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
232 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
233 else
234 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
235 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
236 endif
237" call Decho("zipfile<".zipfile.">")
238" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000239
240 if fname =~ '/'
241 let dirpath = substitute(fname,'/[^/]\+$','','e')
242 if executable("cygpath")
243 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
244 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000245" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000246 call mkdir(dirpath,"p")
247 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000248 if zipfile !~ '/'
249 let zipfile= curdir.'/'.zipfile
250 endif
251" call Decho("zipfile<".zipfile."> fname<".fname.">")
252
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000253 exe "w! ".escape(fname,s:zipfile_escape)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000254 if executable("cygpath")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000255 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
256 endif
257
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000258" call Decho("zip -u '".zipfile.".zip' '".fname."'")
259 call system("zip -u '".zipfile.".zip' '".fname."'")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000260 if v:shell_error != 0
261 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
262 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000263
264 elseif s:zipfile_{winnr()} =~ '^\a\+://'
265 " support writing zipfiles across a network
266 let netzipfile= s:zipfile_{winnr()}
267" call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">")
268 1split|enew
269 let binkeep= &binary
270 let eikeep = &ei
271 set binary ei=all
272 exe "e! ".zipfile.".zip"
273 call netrw#NetWrite(netzipfile)
274 let &ei = eikeep
275 let &binary = binkeep
276 q!
277 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000278 endif
279
280 " cleanup and restore current directory
281 cd ..
282 call s:Rmdir("_ZIPVIM_")
283 exe "cd ".escape(curdir,' \')
284 setlocal nomod
285
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000286 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000287" call Dret("zip#Write")
288endfun
289
290" ---------------------------------------------------------------------
291" Rmdir: {{{2
292fun! s:Rmdir(fname)
293" call Dfunc("Rmdir(fname<".a:fname.">)")
294 if has("unix")
295 call system("/bin/rm -rf ".a:fname)
296 elseif has("win32") || has("win95") || has("win64") || has("win16")
297 if &shell =~? "sh$"
298 call system("/bin/rm -rf ".a:fname)
299 else
300 call system("del /S ".a:fname)
301 endif
302 endif
303" call Dret("Rmdir")
304endfun
305
306" ------------------------------------------------------------------------
307" Modelines And Restoration: {{{1
308let &cpo= s:keepcpo
309unlet s:keepcpo
310" vim:ts=8 fdm=marker