blob: 99bdd7960eff0707857160db981f3c85a26968a6 [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 Moolenaarc01140a2006-03-24 22:21:52 +000078" call Decho("exe silent r! unzip -l '".escape(a:zipfile,s:zipfile_escape)."'")
Bram Moolenaar2217cae2006-03-25 21:55:52 +000079 exe "silent r! unzip -l ".escape(a:zipfile,s:zipfile_escape)
Bram Moolenaar60a795a2005-09-16 21:55:43 +000080 $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
Bram Moolenaar36c31f72005-11-28 23:01:53 +000087 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000088" call Dret("zip#Browse")
89endfun
90
91" ---------------------------------------------------------------------
92" ZipBrowseSelect: {{{2
93fun! s:ZipBrowseSelect()
Bram Moolenaara5792f52005-11-23 21:25:05 +000094" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +000095 let repkeep= &report
96 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000097 let fname= getline(".")
98
99 " sanity check
100 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000101 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000102" 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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000108 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000109" 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')
Bram Moolenaardb552d602006-03-23 22:59:57 +0000117 let curfile= escape(expand("%"),s:zipfile_escape)
118" call Decho("zipfile<".zipfile.">")
119" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000120
121 new
122 wincmd _
Bram Moolenaara5792f52005-11-23 21:25:05 +0000123 let s:zipfile_{winnr()}= curfile
Bram Moolenaardb552d602006-03-23 22:59:57 +0000124" call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).':'.fname)
125 exe "e zipfile:".escape(zipfile,s:zipfile_escape).':'.fname
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000126 filetype detect
127
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000128 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000129" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000130endfun
131
132" ---------------------------------------------------------------------
133" zip#Read: {{{2
134fun! zip#Read(fname,mode)
135" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000136 let repkeep= &report
137 set report=10
138
Bram Moolenaardb552d602006-03-23 22:59:57 +0000139 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):[^\\].*$','\1','')
140 let fname = substitute(a:fname,'zipfile:.\{-}:\([^\\].*\)$','\1','')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000141" call Decho("zipfile<".zipfile."> fname<".fname.">")
142
Bram Moolenaardb552d602006-03-23 22:59:57 +0000143" call Decho("exe r! unzip -p '".escape(zipfile,s:zipfile_escape)."' ".fname)
Bram Moolenaar2217cae2006-03-25 21:55:52 +0000144 exe "r! unzip -p ".escape(zipfile,s:zipfile_escape)." ".fname
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000145
146 " cleanup
147 0d
148 set nomod
149
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000150 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000151" call Dret("zip#Read")
152endfun
153
154" ---------------------------------------------------------------------
155" zip#Write: {{{2
156fun! zip#Write(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000157" call Dfunc("zip#Write(fname<".a:fname.") zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000158 let repkeep= &report
159 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000160
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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000165 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000166" 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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000172 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000173" 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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000192 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000193" 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','')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000208
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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000216 if zipfile !~ '/'
217 let zipfile= curdir.'/'.zipfile
218 endif
219" call Decho("zipfile<".zipfile."> fname<".fname.">")
220
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000221 exe "w! ".fname
222 if executable("cygpath")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000223 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()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000231
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()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000246 endif
247
248 " cleanup and restore current directory
249 cd ..
250 call s:Rmdir("_ZIPVIM_")
251 exe "cd ".escape(curdir,' \')
252 setlocal nomod
253
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000254 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000255" call Dret("zip#Write")
256endfun
257
258" ---------------------------------------------------------------------
259" Rmdir: {{{2
260fun! 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")
272endfun
273
274" ------------------------------------------------------------------------
275" Modelines And Restoration: {{{1
276let &cpo= s:keepcpo
277unlet s:keepcpo
278" vim:ts=8 fdm=marker