blob: e61293c357cfd61214b1881cd0d74519977f998e [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaar71badf92023-04-22 22:40:14 +01003" Date: Mar 12, 2023
4" Version: 33
Bram Moolenaar29634562020-01-09 21:46:04 +01005" 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 Moolenaar71badf92023-04-22 22:40:14 +010023let g:loaded_zip= "v33"
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
D. Ben Knoblecd8a3ea2023-11-04 05:11:17 -040060if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd)
Christian Brabandt816fbcc2023-08-31 23:52:30 +020061 echoerr "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!"
62 finish
63endif
Anton Sharonov67c951d2023-09-05 21:03:27 +020064
Bram Moolenaar60a795a2005-09-16 21:55:43 +000065" ----------------
66" Functions: {{{1
67" ----------------
68
69" ---------------------------------------------------------------------
70" zip#Browse: {{{2
71fun! zip#Browse(zipfile)
72" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
Bram Moolenaarcb80aa22020-10-26 21:12:46 +010073 " sanity check: insure that the zipfile has "PK" as its first two letters
Bram Moolenaar94f76b72013-07-04 22:50:40 +020074 " (zipped files have a leading PK as a "magic cookie")
75 if !filereadable(a:zipfile) || readfile(a:zipfile, "", 1)[0] !~ '^PK'
Bram Moolenaar29634562020-01-09 21:46:04 +010076 exe "noswapfile noautocmd noswapfile e ".fnameescape(a:zipfile)
Bram Moolenaar94f76b72013-07-04 22:50:40 +020077" call Dret("zip#Browse : not a zipfile<".a:zipfile.">")
78 return
79" else " Decho
Bram Moolenaar8024f932020-01-14 19:29:13 +010080" call Decho("zip#Browse: a:zipfile<".a:zipfile."> passed PK test - it's a zip file")
Bram Moolenaar94f76b72013-07-04 22:50:40 +020081 endif
82
Bram Moolenaar36c31f72005-11-28 23:01:53 +000083 let repkeep= &report
84 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000085
86 " sanity checks
Bram Moolenaare37d50a2008-08-06 17:06:04 +000087 if !exists("*fnameescape")
88 if &verbose > 1
Bram Moolenaar6c391a72021-09-09 21:55:11 +020089 echoerr "the zip plugin is not available (your vim doesn't support fnameescape())"
Bram Moolenaare37d50a2008-08-06 17:06:04 +000090 endif
91 return
92 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000093 if !executable(g:zip_unzipcmd)
Bram Moolenaar9964e462007-05-05 17:54:07 +000094 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +000095 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
Bram Moolenaar9964e462007-05-05 17:54:07 +000096" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +000097 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000098" call Dret("zip#Browse")
99 return
100 endif
101 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000102 if a:zipfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100103 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaar9964e462007-05-05 17:54:07 +0000104 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000105 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000106" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000107 endif
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000108 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000109" call Dret("zip#Browse : file<".a:zipfile."> not readable")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000110 return
111 endif
Bram Moolenaardb552d602006-03-23 22:59:57 +0000112" call Decho("passed sanity checks")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000113 if &ma != 1
114 set ma
115 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +0000116 let b:zipfile= a:zipfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000117
118 setlocal noswapfile
119 setlocal buftype=nofile
120 setlocal bufhidden=hide
121 setlocal nobuflisted
122 setlocal nowrap
Bram Moolenaar519cc552021-11-16 19:18:26 +0000123
124 " Oct 12, 2021: need to re-use Bram's syntax/tar.vim.
125 " Setting the filetype to zip doesn't do anything (currently),
126 " but it is perhaps less confusing to curious perusers who do
127 " a :echo &ft
128 setf zip
129 run! syntax/tar.vim
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000130
131 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200132 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100133 \ '" Browsing zipfile '.a:zipfile,
134 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200135 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000136
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000137" call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100138 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000139 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000140 redraw!
Bram Moolenaarc236c162008-07-13 17:41:49 +0000141 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000142" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200143 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000144 let eikeep= &ei
145 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200146 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000147 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200148 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000149" call Dret("zip#Browse")
150 return
151 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000152
Bram Moolenaard0796902016-09-16 20:02:31 +0200153 " Maps associated with zip plugin
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000154 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100155 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
156 noremap <silent> <buffer> x :call zip#Extract()<cr>
157 if &mouse != ""
158 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
159 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000160
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000161 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000162" call Dret("zip#Browse")
163endfun
164
165" ---------------------------------------------------------------------
166" ZipBrowseSelect: {{{2
167fun! s:ZipBrowseSelect()
Bram Moolenaar71badf92023-04-22 22:40:14 +0100168 " call Dfunc("ZipBrowseSelect() zipfile<".((exists("b:zipfile"))? b:zipfile : "n/a")."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000169 let repkeep= &report
170 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000171 let fname= getline(".")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100172 if !exists("b:zipfile")
173" call Dret("ZipBrowseSelect : b:zipfile doesn't exist!")
174 return
175 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000176
177 " sanity check
178 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000179 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000180" call Dret("ZipBrowseSelect")
181 return
182 endif
183 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000184 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000185 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000186" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000187 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000188" call Dret("ZipBrowseSelect")
189 return
190 endif
191
192" call Decho("fname<".fname.">")
193
194 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000195 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100196 let curfile = expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000197" call Decho("zipfile<".zipfile.">")
198" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000199
Bram Moolenaar29634562020-01-09 21:46:04 +0100200 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000201 if !exists("g:zip_nomax") || g:zip_nomax == 0
202 wincmd _
203 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000204 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000205" call Decho("exe e ".fnameescape("zipfile://".zipfile.'::'.fname))
206 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000207 filetype detect
208
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000209 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000210" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000211endfun
212
213" ---------------------------------------------------------------------
214" zip#Read: {{{2
215fun! zip#Read(fname,mode)
216" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000217 let repkeep= &report
218 set report=10
219
Bram Moolenaard68071d2006-05-02 22:08:30 +0000220 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000221 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
222 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000223 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000224 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
225 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200226 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000227 endif
228" call Decho("zipfile<".zipfile.">")
229" call Decho("fname <".fname.">")
Bram Moolenaard0796902016-09-16 20:02:31 +0200230 " sanity check
231 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
232 redraw!
233 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
234" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
235 let &report= repkeep
236" call Dret("zip#Write")
237 return
238 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000239
Bram Moolenaarff034192013-04-24 18:51:19 +0200240 " the following code does much the same thing as
241 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000242 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200243 let temp = tempname()
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200244" call Decho("using temp file<".temp.">")
Bram Moolenaarff034192013-04-24 18:51:19 +0200245 let fn = expand('%:p')
246 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp
247" call Decho("exe sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp)
248 sil exe 'keepalt file '.temp
249 sil keepj e!
250 sil exe 'keepalt file '.fnameescape(fn)
251 call delete(temp)
252
Bram Moolenaar251e1912011-06-19 05:09:16 +0200253 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000254
255 " cleanup
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200256 " keepj 0d " used to be needed for the ...r! ... method
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000257 set nomod
258
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000259 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000260" call Dret("zip#Read")
261endfun
262
263" ---------------------------------------------------------------------
264" zip#Write: {{{2
265fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000266" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000267 let repkeep= &report
268 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000269
270 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200271 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000272 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200273 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 +0000274" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000275 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000276" call Dret("zip#Write")
277 return
278 endif
279 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000280 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000281 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000282" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000283 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000284" call Dret("zip#Write")
285 return
286 endif
287
288 let curdir= getcwd()
289 let tmpdir= tempname()
290" call Decho("orig tempname<".tmpdir.">")
291 if tmpdir =~ '\.'
292 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
293 endif
294" call Decho("tmpdir<".tmpdir.">")
295 call mkdir(tmpdir,"p")
296
297 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000298 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000299 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000300" call Dret("zip#Write")
301 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000302 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000303" call Decho("current directory now: ".getcwd())
304
305 " place temporary files under .../_ZIPVIM_/
306 if isdirectory("_ZIPVIM_")
307 call s:Rmdir("_ZIPVIM_")
308 endif
309 call mkdir("_ZIPVIM_")
310 cd _ZIPVIM_
311" call Decho("current directory now: ".getcwd())
312
Bram Moolenaard68071d2006-05-02 22:08:30 +0000313 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000314 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
315 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000316 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000317 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
318 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000319 endif
320" call Decho("zipfile<".zipfile.">")
321" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000322
323 if fname =~ '/'
324 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200325 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000326 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000327 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000329 call mkdir(dirpath,"p")
330 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000331 if zipfile !~ '/'
332 let zipfile= curdir.'/'.zipfile
333 endif
334" call Decho("zipfile<".zipfile."> fname<".fname.">")
335
Bram Moolenaarc236c162008-07-13 17:41:49 +0000336 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200337 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000338 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000339 endif
340
Bram Moolenaar9964e462007-05-05 17:54:07 +0000341 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
342 let fname = substitute(fname, '[', '[[]', 'g')
343 endif
344
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000345" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
346 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000347 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000348 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000349 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000350" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000351
352 elseif s:zipfile_{winnr()} =~ '^\a\+://'
353 " support writing zipfiles across a network
354 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000355" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000356 1split|enew
357 let binkeep= &binary
358 let eikeep = &ei
359 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100360 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000361 call netrw#NetWrite(netzipfile)
362 let &ei = eikeep
363 let &binary = binkeep
364 q!
365 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000366 endif
367
368 " cleanup and restore current directory
369 cd ..
370 call s:Rmdir("_ZIPVIM_")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000371 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
372 call s:Rmdir(tmpdir)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000373 setlocal nomod
374
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000375 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000376" call Dret("zip#Write")
377endfun
378
379" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200380" zip#Extract: extract a file from a zip archive {{{2
381fun! zip#Extract()
382" call Dfunc("zip#Extract()")
383
384 let repkeep= &report
385 set report=10
386 let fname= getline(".")
387" call Decho("fname<".fname.">")
388
389 " sanity check
390 if fname =~ '^"'
391 let &report= repkeep
392" call Dret("zip#Extract")
393 return
394 endif
395 if fname =~ '/$'
396 redraw!
397 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
398 let &report= repkeep
399" call Dret("zip#Extract")
400 return
401 endif
402
403 " extract the file mentioned under the cursor
404" call Decho("system(".g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell).")")
405 call system(g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell))
406" call Decho("zipfile<".b:zipfile.">")
407 if v:shell_error != 0
408 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
409 elseif !filereadable(fname)
410 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
411 else
412 echo "***note*** successfully extracted ".fname
413 endif
414
415 " restore option
416 let &report= repkeep
417
418" call Dret("zip#Extract")
419endfun
420
421" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000422" s:Escape: {{{2
423fun! s:Escape(fname,isfilt)
424" call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
425 if exists("*shellescape")
426 if a:isfilt
427 let qnameq= shellescape(a:fname,1)
428 else
429 let qnameq= shellescape(a:fname)
430 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000431 else
432 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
433 endif
434" call Dret("QuoteFileDir <".qnameq.">")
435 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000436endfun
437
438" ---------------------------------------------------------------------
439" ChgDir: {{{2
440fun! s:ChgDir(newdir,errlvl,errmsg)
441" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
442
Bram Moolenaar9964e462007-05-05 17:54:07 +0000443 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000444 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000445 catch /^Vim\%((\a\+)\)\=:E344/
446 redraw!
447 if a:errlvl == s:NOTE
448 echo "***note*** ".a:errmsg
449 elseif a:errlvl == s:WARNING
450 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
451 elseif a:errlvl == s:ERROR
452 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
453 endif
454" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
455" call Dret("ChgDir 1")
456 return 1
457 endtry
458
459" call Dret("ChgDir 0")
460 return 0
461endfun
462
463" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000464" s:Rmdir: {{{2
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000465fun! s:Rmdir(fname)
466" call Dfunc("Rmdir(fname<".a:fname.">)")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000467 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
Bram Moolenaarc236c162008-07-13 17:41:49 +0000468 call system("rmdir /S/Q ".s:Escape(a:fname,0))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000469 else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000470 call system("/bin/rm -rf ".s:Escape(a:fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000471 endif
472" call Dret("Rmdir")
473endfun
474
475" ------------------------------------------------------------------------
476" Modelines And Restoration: {{{1
477let &cpo= s:keepcpo
478unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000479" vim:ts=8 fdm=marker