blob: c71a4dc3d32ee4432d380f888ed263f0b1fb0000 [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003" Date: Apr 10, 2006
4" Version: 8
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 Moolenaar4c3f5362006-04-11 21:38:50 +000025let g:loaded_zip = "v8"
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."'"
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 Moolenaar4c3f5362006-04-11 21:38:50 +000086 if namecol > 0
87 exe 'silent 4,$s/^.*\%'.namecol.'c//'
88 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000089
90 setlocal noma nomod ro
91 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
92
Bram Moolenaar36c31f72005-11-28 23:01:53 +000093 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000094" call Dret("zip#Browse")
95endfun
96
97" ---------------------------------------------------------------------
98" ZipBrowseSelect: {{{2
99fun! s:ZipBrowseSelect()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000100" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000101 let repkeep= &report
102 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000103 let fname= getline(".")
104
105 " sanity check
106 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000107 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000108" 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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000114 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000115" 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')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000123 let curfile= expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000124" call Decho("zipfile<".zipfile.">")
125" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000126
127 new
128 wincmd _
Bram Moolenaara5792f52005-11-23 21:25:05 +0000129 let s:zipfile_{winnr()}= curfile
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000130" 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)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000132 filetype detect
133
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000134 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000135" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000136endfun
137
138" ---------------------------------------------------------------------
139" zip#Read: {{{2
140fun! zip#Read(fname,mode)
141" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000142 let repkeep= &report
143 set report=10
144
Bram Moolenaardb552d602006-03-23 22:59:57 +0000145 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):[^\\].*$','\1','')
146 let fname = substitute(a:fname,'zipfile:.\{-}:\([^\\].*\)$','\1','')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000147" call Decho("zipfile<".zipfile."> fname<".fname.">")
148
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000149" call Decho("exe r! unzip -p '".zipfile."' '".fname."'")
150 exe "silent r! unzip -p '".zipfile."' '".fname."'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000151
152 " cleanup
153 0d
154 set nomod
155
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000156 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000157" call Dret("zip#Read")
158endfun
159
160" ---------------------------------------------------------------------
161" zip#Write: {{{2
162fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000163" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000164 let repkeep= &report
165 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000166
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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000171 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000172" 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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000178 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000179" 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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000198 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000199" 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','')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000214
215 if fname =~ '/'
216 let dirpath = substitute(fname,'/[^/]\+$','','e')
217 if executable("cygpath")
218 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
219 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000220" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000221 call mkdir(dirpath,"p")
222 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000223 if zipfile !~ '/'
224 let zipfile= curdir.'/'.zipfile
225 endif
226" call Decho("zipfile<".zipfile."> fname<".fname.">")
227
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000228 exe "w! ".escape(fname,s:zipfile_escape)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000229 if executable("cygpath")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000230 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
231 endif
232
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000233" call Decho("zip -u '".zipfile.".zip' '".fname."'")
234 call system("zip -u '".zipfile.".zip' '".fname."'")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000235 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()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000238
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()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000253 endif
254
255 " cleanup and restore current directory
256 cd ..
257 call s:Rmdir("_ZIPVIM_")
258 exe "cd ".escape(curdir,' \')
259 setlocal nomod
260
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000261 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000262" call Dret("zip#Write")
263endfun
264
265" ---------------------------------------------------------------------
266" Rmdir: {{{2
267fun! 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")
279endfun
280
281" ------------------------------------------------------------------------
282" Modelines And Restoration: {{{1
283let &cpo= s:keepcpo
284unlet s:keepcpo
285" vim:ts=8 fdm=marker