blob: 8dda30c418838daf421780c5988bcf5fff2df873 [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
60" ----------------
61" Functions: {{{1
62" ----------------
63
64" ---------------------------------------------------------------------
65" zip#Browse: {{{2
66fun! zip#Browse(zipfile)
67" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
Bram Moolenaarcb80aa22020-10-26 21:12:46 +010068 " sanity check: insure 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
Bram Moolenaar6c391a72021-09-09 21:55:11 +020084 echoerr "the zip plugin is not available (your vim doesn't support fnameescape())"
Bram Moolenaare37d50a2008-08-06 17:06:04 +000085 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
Bram Moolenaar519cc552021-11-16 19:18:26 +0000118
119 " Oct 12, 2021: need to re-use Bram's syntax/tar.vim.
120 " Setting the filetype to zip doesn't do anything (currently),
121 " but it is perhaps less confusing to curious perusers who do
122 " a :echo &ft
123 setf zip
124 run! syntax/tar.vim
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000125
126 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200127 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100128 \ '" Browsing zipfile '.a:zipfile,
129 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200130 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000131
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000132" call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100133 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000134 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000135 redraw!
Bram Moolenaarc236c162008-07-13 17:41:49 +0000136 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000137" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200138 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000139 let eikeep= &ei
140 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200141 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000142 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200143 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000144" call Dret("zip#Browse")
145 return
146 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000147
Bram Moolenaard0796902016-09-16 20:02:31 +0200148 " Maps associated with zip plugin
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000149 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100150 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
151 noremap <silent> <buffer> x :call zip#Extract()<cr>
152 if &mouse != ""
153 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
154 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000155
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000156 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000157" call Dret("zip#Browse")
158endfun
159
160" ---------------------------------------------------------------------
161" ZipBrowseSelect: {{{2
162fun! s:ZipBrowseSelect()
Bram Moolenaar71badf92023-04-22 22:40:14 +0100163 " call Dfunc("ZipBrowseSelect() zipfile<".((exists("b:zipfile"))? b:zipfile : "n/a")."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000164 let repkeep= &report
165 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000166 let fname= getline(".")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100167 if !exists("b:zipfile")
168" call Dret("ZipBrowseSelect : b:zipfile doesn't exist!")
169 return
170 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000171
172 " sanity check
173 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000174 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000175" call Dret("ZipBrowseSelect")
176 return
177 endif
178 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000179 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000180 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000181" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000182 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000183" call Dret("ZipBrowseSelect")
184 return
185 endif
186
187" call Decho("fname<".fname.">")
188
189 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000190 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100191 let curfile = expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000192" call Decho("zipfile<".zipfile.">")
193" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000194
Bram Moolenaar29634562020-01-09 21:46:04 +0100195 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000196 if !exists("g:zip_nomax") || g:zip_nomax == 0
197 wincmd _
198 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000199 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000200" call Decho("exe e ".fnameescape("zipfile://".zipfile.'::'.fname))
201 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000202 filetype detect
203
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000204 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000205" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000206endfun
207
208" ---------------------------------------------------------------------
209" zip#Read: {{{2
210fun! zip#Read(fname,mode)
211" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000212 let repkeep= &report
213 set report=10
214
Bram Moolenaard68071d2006-05-02 22:08:30 +0000215 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000216 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
217 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000218 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000219 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
220 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200221 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000222 endif
223" call Decho("zipfile<".zipfile.">")
224" call Decho("fname <".fname.">")
Bram Moolenaard0796902016-09-16 20:02:31 +0200225 " sanity check
226 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
227 redraw!
228 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
229" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
230 let &report= repkeep
231" call Dret("zip#Write")
232 return
233 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000234
Bram Moolenaarff034192013-04-24 18:51:19 +0200235 " the following code does much the same thing as
236 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000237 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200238 let temp = tempname()
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200239" call Decho("using temp file<".temp.">")
Bram Moolenaarff034192013-04-24 18:51:19 +0200240 let fn = expand('%:p')
241 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp
242" call Decho("exe sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp)
243 sil exe 'keepalt file '.temp
244 sil keepj e!
245 sil exe 'keepalt file '.fnameescape(fn)
246 call delete(temp)
247
Bram Moolenaar251e1912011-06-19 05:09:16 +0200248 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000249
250 " cleanup
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200251 " keepj 0d " used to be needed for the ...r! ... method
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000252 set nomod
253
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000254 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000255" call Dret("zip#Read")
256endfun
257
258" ---------------------------------------------------------------------
259" zip#Write: {{{2
260fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000261" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000262 let repkeep= &report
263 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000264
265 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200266 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000267 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200268 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 +0000269" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000270 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000271" call Dret("zip#Write")
272 return
273 endif
274 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000275 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000276 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000277" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000278 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000279" call Dret("zip#Write")
280 return
281 endif
282
283 let curdir= getcwd()
284 let tmpdir= tempname()
285" call Decho("orig tempname<".tmpdir.">")
286 if tmpdir =~ '\.'
287 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
288 endif
289" call Decho("tmpdir<".tmpdir.">")
290 call mkdir(tmpdir,"p")
291
292 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000293 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000294 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000295" call Dret("zip#Write")
296 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000297 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000298" call Decho("current directory now: ".getcwd())
299
300 " place temporary files under .../_ZIPVIM_/
301 if isdirectory("_ZIPVIM_")
302 call s:Rmdir("_ZIPVIM_")
303 endif
304 call mkdir("_ZIPVIM_")
305 cd _ZIPVIM_
306" call Decho("current directory now: ".getcwd())
307
Bram Moolenaard68071d2006-05-02 22:08:30 +0000308 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000309 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
310 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000311 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000312 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
313 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000314 endif
315" call Decho("zipfile<".zipfile.">")
316" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000317
318 if fname =~ '/'
319 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200320 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000321 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000322 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000323" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000324 call mkdir(dirpath,"p")
325 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000326 if zipfile !~ '/'
327 let zipfile= curdir.'/'.zipfile
328 endif
329" call Decho("zipfile<".zipfile."> fname<".fname.">")
330
Bram Moolenaarc236c162008-07-13 17:41:49 +0000331 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200332 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000333 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000334 endif
335
Bram Moolenaar9964e462007-05-05 17:54:07 +0000336 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
337 let fname = substitute(fname, '[', '[[]', 'g')
338 endif
339
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000340" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
341 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000342 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000343 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000344 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000345" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000346
347 elseif s:zipfile_{winnr()} =~ '^\a\+://'
348 " support writing zipfiles across a network
349 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000350" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000351 1split|enew
352 let binkeep= &binary
353 let eikeep = &ei
354 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100355 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000356 call netrw#NetWrite(netzipfile)
357 let &ei = eikeep
358 let &binary = binkeep
359 q!
360 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000361 endif
362
363 " cleanup and restore current directory
364 cd ..
365 call s:Rmdir("_ZIPVIM_")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000366 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
367 call s:Rmdir(tmpdir)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000368 setlocal nomod
369
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000370 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000371" call Dret("zip#Write")
372endfun
373
374" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200375" zip#Extract: extract a file from a zip archive {{{2
376fun! zip#Extract()
377" call Dfunc("zip#Extract()")
378
379 let repkeep= &report
380 set report=10
381 let fname= getline(".")
382" call Decho("fname<".fname.">")
383
384 " sanity check
385 if fname =~ '^"'
386 let &report= repkeep
387" call Dret("zip#Extract")
388 return
389 endif
390 if fname =~ '/$'
391 redraw!
392 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
393 let &report= repkeep
394" call Dret("zip#Extract")
395 return
396 endif
397
398 " extract the file mentioned under the cursor
399" call Decho("system(".g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell).")")
400 call system(g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell))
401" call Decho("zipfile<".b:zipfile.">")
402 if v:shell_error != 0
403 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
404 elseif !filereadable(fname)
405 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
406 else
407 echo "***note*** successfully extracted ".fname
408 endif
409
410 " restore option
411 let &report= repkeep
412
413" call Dret("zip#Extract")
414endfun
415
416" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000417" s:Escape: {{{2
418fun! s:Escape(fname,isfilt)
419" call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
420 if exists("*shellescape")
421 if a:isfilt
422 let qnameq= shellescape(a:fname,1)
423 else
424 let qnameq= shellescape(a:fname)
425 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000426 else
427 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
428 endif
429" call Dret("QuoteFileDir <".qnameq.">")
430 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000431endfun
432
433" ---------------------------------------------------------------------
434" ChgDir: {{{2
435fun! s:ChgDir(newdir,errlvl,errmsg)
436" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
437
Bram Moolenaar9964e462007-05-05 17:54:07 +0000438 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000439 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000440 catch /^Vim\%((\a\+)\)\=:E344/
441 redraw!
442 if a:errlvl == s:NOTE
443 echo "***note*** ".a:errmsg
444 elseif a:errlvl == s:WARNING
445 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
446 elseif a:errlvl == s:ERROR
447 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
448 endif
449" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
450" call Dret("ChgDir 1")
451 return 1
452 endtry
453
454" call Dret("ChgDir 0")
455 return 0
456endfun
457
458" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000459" s:Rmdir: {{{2
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000460fun! s:Rmdir(fname)
461" call Dfunc("Rmdir(fname<".a:fname.">)")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000462 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
Bram Moolenaarc236c162008-07-13 17:41:49 +0000463 call system("rmdir /S/Q ".s:Escape(a:fname,0))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000464 else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000465 call system("/bin/rm -rf ".s:Escape(a:fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000466 endif
467" call Dret("Rmdir")
468endfun
469
470" ------------------------------------------------------------------------
471" Modelines And Restoration: {{{1
472let &cpo= s:keepcpo
473unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000474" vim:ts=8 fdm=marker