blob: be39740112e3fb6f00ba726878c73c4b9f30a7ce [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaarc7486e02005-12-29 22:48:26 +00003" Date: Dec 21, 2005
4" Version: 6
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 Moolenaarc7486e02005-12-29 22:48:26 +000025let g:loaded_zip= "v6"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000026
27" ----------------
28" Functions: {{{1
29" ----------------
30
31" ---------------------------------------------------------------------
32" zip#Browse: {{{2
33fun! zip#Browse(zipfile)
34" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
Bram Moolenaar36c31f72005-11-28 23:01:53 +000035 let repkeep= &report
36 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000037
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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +000042 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000043" call Dret("zip#Browse")
44 return
45 endif
46 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +000047 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
Bram Moolenaar36c31f72005-11-28 23:01:53 +000052 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +000053" call Dret("zip#Browse : file<".a:zipfile."> not readable")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000054 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
Bram Moolenaar36c31f72005-11-28 23:01:53 +000084 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000085" call Dret("zip#Browse")
86endfun
87
88" ---------------------------------------------------------------------
89" ZipBrowseSelect: {{{2
90fun! s:ZipBrowseSelect()
Bram Moolenaara5792f52005-11-23 21:25:05 +000091" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +000092 let repkeep= &report
93 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000094 let fname= getline(".")
95
96 " sanity check
97 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +000098 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000099" 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()
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
110" call Decho("fname<".fname.">")
111
112 " get zipfile to the new-window
113 let zipfile= substitute(w:zipfile,'.zip$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000114 let curfile= expand("%")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000115
116 new
117 wincmd _
Bram Moolenaara5792f52005-11-23 21:25:05 +0000118 let s:zipfile_{winnr()}= curfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000119 exe "e zipfile:".zipfile.':'.fname
120 filetype detect
121
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000122 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000123" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000124endfun
125
126" ---------------------------------------------------------------------
127" zip#Read: {{{2
128fun! zip#Read(fname,mode)
129" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000130 let repkeep= &report
131 set report=10
132
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000133 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
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000143 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000144" call Dret("zip#Read")
145endfun
146
147" ---------------------------------------------------------------------
148" zip#Write: {{{2
149fun! zip#Write(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000150" call Dfunc("zip#Write(fname<".a:fname.") zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000151 let repkeep= &report
152 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000153
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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000158 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000159" 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()
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
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()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000185 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000186" 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','')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000201
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
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000209 if zipfile !~ '/'
210 let zipfile= curdir.'/'.zipfile
211 endif
212" call Decho("zipfile<".zipfile."> fname<".fname.">")
213
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000214 exe "w! ".fname
215 if executable("cygpath")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000216 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()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000224
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()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000239 endif
240
241 " cleanup and restore current directory
242 cd ..
243 call s:Rmdir("_ZIPVIM_")
244 exe "cd ".escape(curdir,' \')
245 setlocal nomod
246
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000247 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000248" call Dret("zip#Write")
249endfun
250
251" ---------------------------------------------------------------------
252" Rmdir: {{{2
253fun! 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")
265endfun
266
267" ------------------------------------------------------------------------
268" Modelines And Restoration: {{{1
269let &cpo= s:keepcpo
270unlet s:keepcpo
271" vim:ts=8 fdm=marker