blob: d6bde99a7d48a05b989a7773ee69ae6c11fc1e6c [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaar29634562020-01-09 21:46:04 +01003" Date: Jan 07, 2020
4" Version: 30
5" Maintainer: Charles E Campbell <NcampObell@SdrPchip.AorgM-NOSPAM>
Bram Moolenaar9964e462007-05-05 17:54:07 +00006" License: Vim License (see vim's :help license)
Bram Moolenaar29634562020-01-09 21:46:04 +01007" Copyright: Copyright (C) 2005-2019 Charles E. Campbell {{{1
Bram Moolenaar60a795a2005-09-16 21:55:43 +00008" 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,
Bram Moolenaar446cb832008-06-24 21:56:24 +000011" zip.vim and zipPlugin.vim are provided *as is* and comes with
12" no warranty of any kind, either expressed or implied. By using
13" this plugin, you agree that in no event will the copyright
Bram Moolenaar60a795a2005-09-16 21:55:43 +000014" holder be liable for any damages resulting from the use
15" of this software.
Bram Moolenaar94f76b72013-07-04 22:50:40 +020016"redraw!|call DechoSep()|call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar60a795a2005-09-16 21:55:43 +000017
18" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +000019" Load Once: {{{1
Bram Moolenaar00a927d2010-05-14 23:24:24 +020020if &cp || exists("g:loaded_zip")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000021 finish
22endif
Bram Moolenaar29634562020-01-09 21:46:04 +010023let g:loaded_zip= "v30"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020024if v:version < 702
25 echohl WarningMsg
Bram Moolenaard0796902016-09-16 20:02:31 +020026 echo "***warning*** this version of zip needs vim 7.2 or later"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020027 echohl Normal
28 finish
29endif
30let s:keepcpo= &cpo
31set cpo&vim
Bram Moolenaar94f76b72013-07-04 22:50:40 +020032"DechoTabOn
Bram Moolenaar60a795a2005-09-16 21:55:43 +000033
Bram Moolenaardb552d602006-03-23 22:59:57 +000034let s:zipfile_escape = ' ?&;\'
Bram Moolenaar9964e462007-05-05 17:54:07 +000035let s:ERROR = 2
36let s:WARNING = 1
37let s:NOTE = 0
38
39" ---------------------------------------------------------------------
40" Global Values: {{{1
41if !exists("g:zip_shq")
Bram Moolenaar446cb832008-06-24 21:56:24 +000042 if &shq != ""
43 let g:zip_shq= &shq
44 elseif has("unix")
Bram Moolenaar9964e462007-05-05 17:54:07 +000045 let g:zip_shq= "'"
46 else
47 let g:zip_shq= '"'
48 endif
49endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000050if !exists("g:zip_zipcmd")
51 let g:zip_zipcmd= "zip"
52endif
53if !exists("g:zip_unzipcmd")
54 let g:zip_unzipcmd= "unzip"
55endif
Bram Moolenaard0796902016-09-16 20:02:31 +020056if !exists("g:zip_extractcmd")
57 let g:zip_extractcmd= g:zip_unzipcmd
58endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000059
60" ----------------
61" Functions: {{{1
62" ----------------
63
64" ---------------------------------------------------------------------
65" zip#Browse: {{{2
66fun! zip#Browse(zipfile)
67" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
Bram Moolenaar8024f932020-01-14 19:29:13 +010068 " sanity check: ensure that the zipfile has "PK" as its first two letters
Bram Moolenaar94f76b72013-07-04 22:50:40 +020069 " (zipped files have a leading PK as a "magic cookie")
70 if !filereadable(a:zipfile) || readfile(a:zipfile, "", 1)[0] !~ '^PK'
Bram Moolenaar29634562020-01-09 21:46:04 +010071 exe "noswapfile noautocmd noswapfile e ".fnameescape(a:zipfile)
Bram Moolenaar94f76b72013-07-04 22:50:40 +020072" call Dret("zip#Browse : not a zipfile<".a:zipfile.">")
73 return
74" else " Decho
Bram Moolenaar8024f932020-01-14 19:29:13 +010075" call Decho("zip#Browse: a:zipfile<".a:zipfile."> passed PK test - it's a zip file")
Bram Moolenaar94f76b72013-07-04 22:50:40 +020076 endif
77
Bram Moolenaar36c31f72005-11-28 23:01:53 +000078 let repkeep= &report
79 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000080
81 " sanity checks
Bram Moolenaare37d50a2008-08-06 17:06:04 +000082 if !exists("*fnameescape")
83 if &verbose > 1
84 echoerr "the zip plugin is not available (your vim doens't support fnameescape())"
85 endif
86 return
87 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000088 if !executable(g:zip_unzipcmd)
Bram Moolenaar9964e462007-05-05 17:54:07 +000089 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +000090 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
Bram Moolenaar9964e462007-05-05 17:54:07 +000091" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +000092 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000093" call Dret("zip#Browse")
94 return
95 endif
96 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +000097 if a:zipfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +010098 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaar9964e462007-05-05 17:54:07 +000099 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000100 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000101" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000102 endif
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000103 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000104" call Dret("zip#Browse : file<".a:zipfile."> not readable")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000105 return
106 endif
Bram Moolenaardb552d602006-03-23 22:59:57 +0000107" call Decho("passed sanity checks")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000108 if &ma != 1
109 set ma
110 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +0000111 let b:zipfile= a:zipfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000112
113 setlocal noswapfile
114 setlocal buftype=nofile
115 setlocal bufhidden=hide
116 setlocal nobuflisted
117 setlocal nowrap
118 set ft=tar
119
120 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200121 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100122 \ '" Browsing zipfile '.a:zipfile,
123 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200124 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000125
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000126" call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100127 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000128 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000129 redraw!
Bram Moolenaarc236c162008-07-13 17:41:49 +0000130 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000131" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200132 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000133 let eikeep= &ei
134 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200135 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000136 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200137 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000138" call Dret("zip#Browse")
139 return
140 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000141
Bram Moolenaard0796902016-09-16 20:02:31 +0200142 " Maps associated with zip plugin
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000143 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100144 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
145 noremap <silent> <buffer> x :call zip#Extract()<cr>
146 if &mouse != ""
147 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
148 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000149
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000150 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000151" call Dret("zip#Browse")
152endfun
153
154" ---------------------------------------------------------------------
155" ZipBrowseSelect: {{{2
156fun! s:ZipBrowseSelect()
Bram Moolenaarccc18222007-05-10 18:25:20 +0000157" call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000158 let repkeep= &report
159 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000160 let fname= getline(".")
161
162 " sanity check
163 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000164 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000165" call Dret("ZipBrowseSelect")
166 return
167 endif
168 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000169 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000170 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000171" 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("ZipBrowseSelect")
174 return
175 endif
176
177" call Decho("fname<".fname.">")
178
179 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000180 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100181 let curfile = expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000182" call Decho("zipfile<".zipfile.">")
183" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000184
Bram Moolenaar29634562020-01-09 21:46:04 +0100185 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000186 if !exists("g:zip_nomax") || g:zip_nomax == 0
187 wincmd _
188 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000189 let s:zipfile_{winnr()}= curfile
Bram Moolenaarc236c162008-07-13 17:41:49 +0000190" call Decho("exe e ".fnameescape("zipfile:".zipfile.'::'.fname))
Bram Moolenaar29634562020-01-09 21:46:04 +0100191 exe "noswapfile e ".fnameescape("zipfile:".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000192 filetype detect
193
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000194 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000195" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000196endfun
197
198" ---------------------------------------------------------------------
199" zip#Read: {{{2
200fun! zip#Read(fname,mode)
201" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000202 let repkeep= &report
203 set report=10
204
Bram Moolenaard68071d2006-05-02 22:08:30 +0000205 if has("unix")
206 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
207 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
208 else
209 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
210 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200211 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000212 endif
213" call Decho("zipfile<".zipfile.">")
214" call Decho("fname <".fname.">")
Bram Moolenaard0796902016-09-16 20:02:31 +0200215 " sanity check
216 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
217 redraw!
218 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
219" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
220 let &report= repkeep
221" call Dret("zip#Write")
222 return
223 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000224
Bram Moolenaarff034192013-04-24 18:51:19 +0200225 " the following code does much the same thing as
226 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
227 " but allows zipfile:... entries in quickfix lists
228 let temp = tempname()
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200229" call Decho("using temp file<".temp.">")
Bram Moolenaarff034192013-04-24 18:51:19 +0200230 let fn = expand('%:p')
231 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp
232" call Decho("exe sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp)
233 sil exe 'keepalt file '.temp
234 sil keepj e!
235 sil exe 'keepalt file '.fnameescape(fn)
236 call delete(temp)
237
Bram Moolenaar251e1912011-06-19 05:09:16 +0200238 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000239
240 " cleanup
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200241 " keepj 0d " used to be needed for the ...r! ... method
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000242 set nomod
243
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000244 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000245" call Dret("zip#Read")
246endfun
247
248" ---------------------------------------------------------------------
249" zip#Write: {{{2
250fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000251" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000252 let repkeep= &report
253 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000254
255 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200256 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000257 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200258 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000259" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000260 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000261" call Dret("zip#Write")
262 return
263 endif
264 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000265 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000266 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000267" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000268 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000269" call Dret("zip#Write")
270 return
271 endif
272
273 let curdir= getcwd()
274 let tmpdir= tempname()
275" call Decho("orig tempname<".tmpdir.">")
276 if tmpdir =~ '\.'
277 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
278 endif
279" call Decho("tmpdir<".tmpdir.">")
280 call mkdir(tmpdir,"p")
281
282 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000283 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000284 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000285" call Dret("zip#Write")
286 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000287 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000288" call Decho("current directory now: ".getcwd())
289
290 " place temporary files under .../_ZIPVIM_/
291 if isdirectory("_ZIPVIM_")
292 call s:Rmdir("_ZIPVIM_")
293 endif
294 call mkdir("_ZIPVIM_")
295 cd _ZIPVIM_
296" call Decho("current directory now: ".getcwd())
297
Bram Moolenaard68071d2006-05-02 22:08:30 +0000298 if has("unix")
299 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
300 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
301 else
302 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
303 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
304 endif
305" call Decho("zipfile<".zipfile.">")
306" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000307
308 if fname =~ '/'
309 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200310 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000311 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000312 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000313" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000314 call mkdir(dirpath,"p")
315 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000316 if zipfile !~ '/'
317 let zipfile= curdir.'/'.zipfile
318 endif
319" call Decho("zipfile<".zipfile."> fname<".fname.">")
320
Bram Moolenaarc236c162008-07-13 17:41:49 +0000321 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200322 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000323 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000324 endif
325
Bram Moolenaar9964e462007-05-05 17:54:07 +0000326 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
327 let fname = substitute(fname, '[', '[[]', 'g')
328 endif
329
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000330" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
331 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000332 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000333 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000334 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000335" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000336
337 elseif s:zipfile_{winnr()} =~ '^\a\+://'
338 " support writing zipfiles across a network
339 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000340" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000341 1split|enew
342 let binkeep= &binary
343 let eikeep = &ei
344 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100345 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000346 call netrw#NetWrite(netzipfile)
347 let &ei = eikeep
348 let &binary = binkeep
349 q!
350 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000351 endif
352
353 " cleanup and restore current directory
354 cd ..
355 call s:Rmdir("_ZIPVIM_")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000356 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
357 call s:Rmdir(tmpdir)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000358 setlocal nomod
359
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000360 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000361" call Dret("zip#Write")
362endfun
363
364" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200365" zip#Extract: extract a file from a zip archive {{{2
366fun! zip#Extract()
367" call Dfunc("zip#Extract()")
368
369 let repkeep= &report
370 set report=10
371 let fname= getline(".")
372" call Decho("fname<".fname.">")
373
374 " sanity check
375 if fname =~ '^"'
376 let &report= repkeep
377" call Dret("zip#Extract")
378 return
379 endif
380 if fname =~ '/$'
381 redraw!
382 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
383 let &report= repkeep
384" call Dret("zip#Extract")
385 return
386 endif
387
388 " extract the file mentioned under the cursor
389" call Decho("system(".g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell).")")
390 call system(g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell))
391" call Decho("zipfile<".b:zipfile.">")
392 if v:shell_error != 0
393 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
394 elseif !filereadable(fname)
395 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
396 else
397 echo "***note*** successfully extracted ".fname
398 endif
399
400 " restore option
401 let &report= repkeep
402
403" call Dret("zip#Extract")
404endfun
405
406" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000407" s:Escape: {{{2
408fun! s:Escape(fname,isfilt)
409" call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
410 if exists("*shellescape")
411 if a:isfilt
412 let qnameq= shellescape(a:fname,1)
413 else
414 let qnameq= shellescape(a:fname)
415 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000416 else
417 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
418 endif
419" call Dret("QuoteFileDir <".qnameq.">")
420 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000421endfun
422
423" ---------------------------------------------------------------------
424" ChgDir: {{{2
425fun! s:ChgDir(newdir,errlvl,errmsg)
426" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
427
Bram Moolenaar9964e462007-05-05 17:54:07 +0000428 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000429 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000430 catch /^Vim\%((\a\+)\)\=:E344/
431 redraw!
432 if a:errlvl == s:NOTE
433 echo "***note*** ".a:errmsg
434 elseif a:errlvl == s:WARNING
435 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
436 elseif a:errlvl == s:ERROR
437 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
438 endif
439" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
440" call Dret("ChgDir 1")
441 return 1
442 endtry
443
444" call Dret("ChgDir 0")
445 return 0
446endfun
447
448" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000449" s:Rmdir: {{{2
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000450fun! s:Rmdir(fname)
451" call Dfunc("Rmdir(fname<".a:fname.">)")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000452 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
Bram Moolenaarc236c162008-07-13 17:41:49 +0000453 call system("rmdir /S/Q ".s:Escape(a:fname,0))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000454 else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000455 call system("/bin/rm -rf ".s:Escape(a:fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000456 endif
457" call Dret("Rmdir")
458endfun
459
460" ------------------------------------------------------------------------
461" Modelines And Restoration: {{{1
462let &cpo= s:keepcpo
463unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000464" vim:ts=8 fdm=marker