blob: 50a6002a909f92525e239f028b73f5d9f1b56711 [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaara5792f52005-11-23 21:25:05 +00003" Date: Nov 14, 2005
4" Version: 4
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 Moolenaara5792f52005-11-23 21:25:05 +000025let g:loaded_zip= "v4"
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.">)")
35
36 " sanity checks
37 if !executable("unzip")
38 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
39 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
40" call Dret("zip#Browse")
41 return
42 endif
43 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +000044 if a:zipfile !~# '^\a\+://'
45 " if its an url, don't complain, let url-handlers such as vim do its thing
46 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
47 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
48 endif
49" call Dret("zip#Browse : file<".a:zipfile."> not readable")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000050 return
51 endif
52 if &ma != 1
53 set ma
54 endif
55 let w:zipfile= a:zipfile
56
57 setlocal noswapfile
58 setlocal buftype=nofile
59 setlocal bufhidden=hide
60 setlocal nobuflisted
61 setlocal nowrap
62 set ft=tar
63
64 " give header
65 exe "$put ='".'\"'." zip.vim version ".g:loaded_zip."'"
66 exe "$put ='".'\"'." Browsing zipfile ".a:zipfile."'"
67 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
68 $put =''
69 0d
70 $
71
72 exe "silent r! unzip -l ".a:zipfile
73 $d
74 silent 4,$v/^\s\+\d\+\s\{0,5}\d/d
75 silent 4,$s/^\%(.*\)\s\+\(\S\)/\1/
76
77 setlocal noma nomod ro
78 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
79
80" call Dret("zip#Browse")
81endfun
82
83" ---------------------------------------------------------------------
84" ZipBrowseSelect: {{{2
85fun! s:ZipBrowseSelect()
Bram Moolenaara5792f52005-11-23 21:25:05 +000086" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000087 let fname= getline(".")
88
89 " sanity check
90 if fname =~ '^"'
91" call Dret("ZipBrowseSelect")
92 return
93 endif
94 if fname =~ '/$'
95 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
96 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
97" call Dret("ZipBrowseSelect")
98 return
99 endif
100
101" call Decho("fname<".fname.">")
102
103 " get zipfile to the new-window
104 let zipfile= substitute(w:zipfile,'.zip$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000105 let curfile= expand("%")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000106
107 new
108 wincmd _
Bram Moolenaara5792f52005-11-23 21:25:05 +0000109 let s:zipfile_{winnr()}= curfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000110 exe "e zipfile:".zipfile.':'.fname
111 filetype detect
112
Bram Moolenaara5792f52005-11-23 21:25:05 +0000113" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000114endfun
115
116" ---------------------------------------------------------------------
117" zip#Read: {{{2
118fun! zip#Read(fname,mode)
119" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
120 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','')
121 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','')
122" call Decho("zipfile<".zipfile."> fname<".fname.">")
123
124 exe "r! unzip -p ".zipfile." ".fname
125
126 " cleanup
127 0d
128 set nomod
129
130" call Dret("zip#Read")
131endfun
132
133" ---------------------------------------------------------------------
134" zip#Write: {{{2
135fun! zip#Write(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000136" call Dfunc("zip#Write(fname<".a:fname.") zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000137
138 " sanity checks
139 if !executable("zip")
140 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
141 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
142" call Dret("zip#Write")
143 return
144 endif
145 if !exists("*mkdir")
146 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
147 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
148" call Dret("zip#Write")
149 return
150 endif
151
152 let curdir= getcwd()
153 let tmpdir= tempname()
154" call Decho("orig tempname<".tmpdir.">")
155 if tmpdir =~ '\.'
156 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
157 endif
158" call Decho("tmpdir<".tmpdir.">")
159 call mkdir(tmpdir,"p")
160
161 " attempt to change to the indicated directory
162 try
163 exe "cd ".escape(tmpdir,' \')
164 catch /^Vim\%((\a\+)\)\=:E344/
165 echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | Echohl None
166 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
167" call Dret("zip#Write")
168 return
169 endtry
170" call Decho("current directory now: ".getcwd())
171
172 " place temporary files under .../_ZIPVIM_/
173 if isdirectory("_ZIPVIM_")
174 call s:Rmdir("_ZIPVIM_")
175 endif
176 call mkdir("_ZIPVIM_")
177 cd _ZIPVIM_
178" call Decho("current directory now: ".getcwd())
179
180 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','')
181 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','')
182 let dirpath = substitute(fname,'/[^/]\+$','','e')
183 if zipfile !~ '/'
184 let zipfile= curdir.'/'.zipfile
185 endif
186" call Decho("zipfile<".zipfile."> fname<".fname.">")
187
188 call mkdir(dirpath,"p")
189 exe "w! ".fname
190 if executable("cygpath")
191 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
192 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
193 endif
194
195" call Decho("zip -u ".zipfile.".zip ".fname)
196 call system("zip -u ".zipfile.".zip ".fname)
197 if v:shell_error != 0
198 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
199 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000200
201 elseif s:zipfile_{winnr()} =~ '^\a\+://'
202 " support writing zipfiles across a network
203 let netzipfile= s:zipfile_{winnr()}
204" call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">")
205 1split|enew
206 let binkeep= &binary
207 let eikeep = &ei
208 set binary ei=all
209 exe "e! ".zipfile.".zip"
210 call netrw#NetWrite(netzipfile)
211 let &ei = eikeep
212 let &binary = binkeep
213 q!
214 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000215 endif
216
217 " cleanup and restore current directory
218 cd ..
219 call s:Rmdir("_ZIPVIM_")
220 exe "cd ".escape(curdir,' \')
221 setlocal nomod
222
223" call Dret("zip#Write")
224endfun
225
226" ---------------------------------------------------------------------
227" Rmdir: {{{2
228fun! s:Rmdir(fname)
229" call Dfunc("Rmdir(fname<".a:fname.">)")
230 if has("unix")
231 call system("/bin/rm -rf ".a:fname)
232 elseif has("win32") || has("win95") || has("win64") || has("win16")
233 if &shell =~? "sh$"
234 call system("/bin/rm -rf ".a:fname)
235 else
236 call system("del /S ".a:fname)
237 endif
238 endif
239" call Dret("Rmdir")
240endfun
241
242" ------------------------------------------------------------------------
243" Modelines And Restoration: {{{1
244let &cpo= s:keepcpo
245unlet s:keepcpo
246" vim:ts=8 fdm=marker