blob: 6790f4ff4ef5341ea2400f8e0402fe0650a44fdb [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaardb552d602006-03-23 22:59:57 +00003" Date: Mar 22, 2006
4" Version: 7
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 Moolenaardb552d602006-03-23 22:59:57 +000025let g:loaded_zip = "v7"
26let 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."'"
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
Bram Moolenaar60a795a2005-09-16 21:55:43 +000085 $d
Bram Moolenaar910f66f2006-04-05 20:41:53 +000086 exe 'silent 4,$s/^.*\%'.namecol.'c//'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000087
88 setlocal noma nomod ro
89 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
90
Bram Moolenaar36c31f72005-11-28 23:01:53 +000091 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000092" call Dret("zip#Browse")
93endfun
94
95" ---------------------------------------------------------------------
96" ZipBrowseSelect: {{{2
97fun! s:ZipBrowseSelect()
Bram Moolenaara5792f52005-11-23 21:25:05 +000098" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +000099 let repkeep= &report
100 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000101 let fname= getline(".")
102
103 " sanity check
104 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000105 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000106" call Dret("ZipBrowseSelect")
107 return
108 endif
109 if fname =~ '/$'
110 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
111 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000112 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000113" call Dret("ZipBrowseSelect")
114 return
115 endif
116
117" call Decho("fname<".fname.">")
118
119 " get zipfile to the new-window
120 let zipfile= substitute(w:zipfile,'.zip$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000121 let curfile= expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000122" call Decho("zipfile<".zipfile.">")
123" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000124
125 new
126 wincmd _
Bram Moolenaara5792f52005-11-23 21:25:05 +0000127 let s:zipfile_{winnr()}= curfile
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000128" call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).':'.escape(fname,s:zipfile_escape))
129 exe "e zipfile:".escape(zipfile,s:zipfile_escape).':'.escape(fname,s:zipfile_escape)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000130 filetype detect
131
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000132 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000133" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000134endfun
135
136" ---------------------------------------------------------------------
137" zip#Read: {{{2
138fun! zip#Read(fname,mode)
139" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000140 let repkeep= &report
141 set report=10
142
Bram Moolenaardb552d602006-03-23 22:59:57 +0000143 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):[^\\].*$','\1','')
144 let fname = substitute(a:fname,'zipfile:.\{-}:\([^\\].*\)$','\1','')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000145" call Decho("zipfile<".zipfile."> fname<".fname.">")
146
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000147" call Decho("exe r! unzip -p '".zipfile."' '".fname."'")
148 exe "silent r! unzip -p '".zipfile."' '".fname."'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000149
150 " cleanup
151 0d
152 set nomod
153
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000154 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000155" call Dret("zip#Read")
156endfun
157
158" ---------------------------------------------------------------------
159" zip#Write: {{{2
160fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000161" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000162 let repkeep= &report
163 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000164
165 " sanity checks
166 if !executable("zip")
167 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
168 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000169 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000170" call Dret("zip#Write")
171 return
172 endif
173 if !exists("*mkdir")
174 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
175 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000176 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000177" call Dret("zip#Write")
178 return
179 endif
180
181 let curdir= getcwd()
182 let tmpdir= tempname()
183" call Decho("orig tempname<".tmpdir.">")
184 if tmpdir =~ '\.'
185 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
186 endif
187" call Decho("tmpdir<".tmpdir.">")
188 call mkdir(tmpdir,"p")
189
190 " attempt to change to the indicated directory
191 try
192 exe "cd ".escape(tmpdir,' \')
193 catch /^Vim\%((\a\+)\)\=:E344/
194 echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | 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 endtry
200" call Decho("current directory now: ".getcwd())
201
202 " place temporary files under .../_ZIPVIM_/
203 if isdirectory("_ZIPVIM_")
204 call s:Rmdir("_ZIPVIM_")
205 endif
206 call mkdir("_ZIPVIM_")
207 cd _ZIPVIM_
208" call Decho("current directory now: ".getcwd())
209
210 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','')
211 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000212
213 if fname =~ '/'
214 let dirpath = substitute(fname,'/[^/]\+$','','e')
215 if executable("cygpath")
216 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
217 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000218" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000219 call mkdir(dirpath,"p")
220 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000221 if zipfile !~ '/'
222 let zipfile= curdir.'/'.zipfile
223 endif
224" call Decho("zipfile<".zipfile."> fname<".fname.">")
225
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000226 exe "w! ".escape(fname,s:zipfile_escape)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000227 if executable("cygpath")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000228 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
229 endif
230
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000231" call Decho("zip -u '".zipfile.".zip' '".fname."'")
232 call system("zip -u '".zipfile.".zip' '".fname."'")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000233 if v:shell_error != 0
234 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
235 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000236
237 elseif s:zipfile_{winnr()} =~ '^\a\+://'
238 " support writing zipfiles across a network
239 let netzipfile= s:zipfile_{winnr()}
240" call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">")
241 1split|enew
242 let binkeep= &binary
243 let eikeep = &ei
244 set binary ei=all
245 exe "e! ".zipfile.".zip"
246 call netrw#NetWrite(netzipfile)
247 let &ei = eikeep
248 let &binary = binkeep
249 q!
250 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000251 endif
252
253 " cleanup and restore current directory
254 cd ..
255 call s:Rmdir("_ZIPVIM_")
256 exe "cd ".escape(curdir,' \')
257 setlocal nomod
258
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000259 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000260" call Dret("zip#Write")
261endfun
262
263" ---------------------------------------------------------------------
264" Rmdir: {{{2
265fun! s:Rmdir(fname)
266" call Dfunc("Rmdir(fname<".a:fname.">)")
267 if has("unix")
268 call system("/bin/rm -rf ".a:fname)
269 elseif has("win32") || has("win95") || has("win64") || has("win16")
270 if &shell =~? "sh$"
271 call system("/bin/rm -rf ".a:fname)
272 else
273 call system("del /S ".a:fname)
274 endif
275 endif
276" call Dret("Rmdir")
277endfun
278
279" ------------------------------------------------------------------------
280" Modelines And Restoration: {{{1
281let &cpo= s:keepcpo
282unlet s:keepcpo
283" vim:ts=8 fdm=marker