blob: 16deae0c03eeb08626143fcdaadf719074c933d4 [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaar36c31f72005-11-28 23:01:53 +00003" Date: Nov 28, 2005
4" Version: 5
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 Moolenaar36c31f72005-11-28 23:01:53 +000025let g:loaded_zip= "v5"
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','')
201 let dirpath = substitute(fname,'/[^/]\+$','','e')
202 if zipfile !~ '/'
203 let zipfile= curdir.'/'.zipfile
204 endif
205" call Decho("zipfile<".zipfile."> fname<".fname.">")
206
207 call mkdir(dirpath,"p")
208 exe "w! ".fname
209 if executable("cygpath")
210 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
211 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
212 endif
213
214" call Decho("zip -u ".zipfile.".zip ".fname)
215 call system("zip -u ".zipfile.".zip ".fname)
216 if v:shell_error != 0
217 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
218 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000219
220 elseif s:zipfile_{winnr()} =~ '^\a\+://'
221 " support writing zipfiles across a network
222 let netzipfile= s:zipfile_{winnr()}
223" call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">")
224 1split|enew
225 let binkeep= &binary
226 let eikeep = &ei
227 set binary ei=all
228 exe "e! ".zipfile.".zip"
229 call netrw#NetWrite(netzipfile)
230 let &ei = eikeep
231 let &binary = binkeep
232 q!
233 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000234 endif
235
236 " cleanup and restore current directory
237 cd ..
238 call s:Rmdir("_ZIPVIM_")
239 exe "cd ".escape(curdir,' \')
240 setlocal nomod
241
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000242 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000243" call Dret("zip#Write")
244endfun
245
246" ---------------------------------------------------------------------
247" Rmdir: {{{2
248fun! s:Rmdir(fname)
249" call Dfunc("Rmdir(fname<".a:fname.">)")
250 if has("unix")
251 call system("/bin/rm -rf ".a:fname)
252 elseif has("win32") || has("win95") || has("win64") || has("win16")
253 if &shell =~? "sh$"
254 call system("/bin/rm -rf ".a:fname)
255 else
256 call system("del /S ".a:fname)
257 endif
258 endif
259" call Dret("Rmdir")
260endfun
261
262" ------------------------------------------------------------------------
263" Modelines And Restoration: {{{1
264let &cpo= s:keepcpo
265unlet s:keepcpo
266" vim:ts=8 fdm=marker