blob: bc9b62ddb0c35760b9c1aa4221f2c194f02003d7 [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaar519cc552021-11-16 19:18:26 +00003" Date: Nov 08, 2021
4" Version: 32
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 Moolenaar519cc552021-11-16 19:18:26 +000023let g:loaded_zip= "v32"
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 Moolenaarccc18222007-05-10 18:25:20 +0000163" call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> 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(".")
167
168 " sanity check
169 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000170 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000171" call Dret("ZipBrowseSelect")
172 return
173 endif
174 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000175 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000176 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000177" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000178 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000179" call Dret("ZipBrowseSelect")
180 return
181 endif
182
183" call Decho("fname<".fname.">")
184
185 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000186 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100187 let curfile = expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000188" call Decho("zipfile<".zipfile.">")
189" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000190
Bram Moolenaar29634562020-01-09 21:46:04 +0100191 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000192 if !exists("g:zip_nomax") || g:zip_nomax == 0
193 wincmd _
194 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000195 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000196" call Decho("exe e ".fnameescape("zipfile://".zipfile.'::'.fname))
197 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000198 filetype detect
199
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000200 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000201" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000202endfun
203
204" ---------------------------------------------------------------------
205" zip#Read: {{{2
206fun! zip#Read(fname,mode)
207" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000208 let repkeep= &report
209 set report=10
210
Bram Moolenaard68071d2006-05-02 22:08:30 +0000211 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000212 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
213 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000214 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000215 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
216 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200217 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000218 endif
219" call Decho("zipfile<".zipfile.">")
220" call Decho("fname <".fname.">")
Bram Moolenaard0796902016-09-16 20:02:31 +0200221 " sanity check
222 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
223 redraw!
224 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
225" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
226 let &report= repkeep
227" call Dret("zip#Write")
228 return
229 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000230
Bram Moolenaarff034192013-04-24 18:51:19 +0200231 " the following code does much the same thing as
232 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000233 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200234 let temp = tempname()
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200235" call Decho("using temp file<".temp.">")
Bram Moolenaarff034192013-04-24 18:51:19 +0200236 let fn = expand('%:p')
237 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp
238" call Decho("exe sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp)
239 sil exe 'keepalt file '.temp
240 sil keepj e!
241 sil exe 'keepalt file '.fnameescape(fn)
242 call delete(temp)
243
Bram Moolenaar251e1912011-06-19 05:09:16 +0200244 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000245
246 " cleanup
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200247 " keepj 0d " used to be needed for the ...r! ... method
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000248 set nomod
249
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000250 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000251" call Dret("zip#Read")
252endfun
253
254" ---------------------------------------------------------------------
255" zip#Write: {{{2
256fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000257" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000258 let repkeep= &report
259 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000260
261 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200262 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000263 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200264 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 +0000265" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000266 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000267" call Dret("zip#Write")
268 return
269 endif
270 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000271 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000272 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000273" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000274 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000275" call Dret("zip#Write")
276 return
277 endif
278
279 let curdir= getcwd()
280 let tmpdir= tempname()
281" call Decho("orig tempname<".tmpdir.">")
282 if tmpdir =~ '\.'
283 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
284 endif
285" call Decho("tmpdir<".tmpdir.">")
286 call mkdir(tmpdir,"p")
287
288 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000289 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000290 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000291" call Dret("zip#Write")
292 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000293 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000294" call Decho("current directory now: ".getcwd())
295
296 " place temporary files under .../_ZIPVIM_/
297 if isdirectory("_ZIPVIM_")
298 call s:Rmdir("_ZIPVIM_")
299 endif
300 call mkdir("_ZIPVIM_")
301 cd _ZIPVIM_
302" call Decho("current directory now: ".getcwd())
303
Bram Moolenaard68071d2006-05-02 22:08:30 +0000304 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000305 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
306 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000307 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000308 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
309 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000310 endif
311" call Decho("zipfile<".zipfile.">")
312" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000313
314 if fname =~ '/'
315 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200316 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000317 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000318 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000319" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000320 call mkdir(dirpath,"p")
321 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000322 if zipfile !~ '/'
323 let zipfile= curdir.'/'.zipfile
324 endif
325" call Decho("zipfile<".zipfile."> fname<".fname.">")
326
Bram Moolenaarc236c162008-07-13 17:41:49 +0000327 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200328 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000329 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000330 endif
331
Bram Moolenaar9964e462007-05-05 17:54:07 +0000332 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
333 let fname = substitute(fname, '[', '[[]', 'g')
334 endif
335
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000336" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
337 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000338 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000339 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000340 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000341" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000342
343 elseif s:zipfile_{winnr()} =~ '^\a\+://'
344 " support writing zipfiles across a network
345 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000346" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000347 1split|enew
348 let binkeep= &binary
349 let eikeep = &ei
350 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100351 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000352 call netrw#NetWrite(netzipfile)
353 let &ei = eikeep
354 let &binary = binkeep
355 q!
356 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000357 endif
358
359 " cleanup and restore current directory
360 cd ..
361 call s:Rmdir("_ZIPVIM_")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000362 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
363 call s:Rmdir(tmpdir)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000364 setlocal nomod
365
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000366 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000367" call Dret("zip#Write")
368endfun
369
370" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200371" zip#Extract: extract a file from a zip archive {{{2
372fun! zip#Extract()
373" call Dfunc("zip#Extract()")
374
375 let repkeep= &report
376 set report=10
377 let fname= getline(".")
378" call Decho("fname<".fname.">")
379
380 " sanity check
381 if fname =~ '^"'
382 let &report= repkeep
383" call Dret("zip#Extract")
384 return
385 endif
386 if fname =~ '/$'
387 redraw!
388 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
389 let &report= repkeep
390" call Dret("zip#Extract")
391 return
392 endif
393
394 " extract the file mentioned under the cursor
395" call Decho("system(".g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell).")")
396 call system(g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell))
397" call Decho("zipfile<".b:zipfile.">")
398 if v:shell_error != 0
399 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
400 elseif !filereadable(fname)
401 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
402 else
403 echo "***note*** successfully extracted ".fname
404 endif
405
406 " restore option
407 let &report= repkeep
408
409" call Dret("zip#Extract")
410endfun
411
412" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000413" s:Escape: {{{2
414fun! s:Escape(fname,isfilt)
415" call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
416 if exists("*shellescape")
417 if a:isfilt
418 let qnameq= shellescape(a:fname,1)
419 else
420 let qnameq= shellescape(a:fname)
421 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000422 else
423 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
424 endif
425" call Dret("QuoteFileDir <".qnameq.">")
426 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000427endfun
428
429" ---------------------------------------------------------------------
430" ChgDir: {{{2
431fun! s:ChgDir(newdir,errlvl,errmsg)
432" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
433
Bram Moolenaar9964e462007-05-05 17:54:07 +0000434 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000435 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000436 catch /^Vim\%((\a\+)\)\=:E344/
437 redraw!
438 if a:errlvl == s:NOTE
439 echo "***note*** ".a:errmsg
440 elseif a:errlvl == s:WARNING
441 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
442 elseif a:errlvl == s:ERROR
443 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
444 endif
445" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
446" call Dret("ChgDir 1")
447 return 1
448 endtry
449
450" call Dret("ChgDir 0")
451 return 0
452endfun
453
454" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000455" s:Rmdir: {{{2
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000456fun! s:Rmdir(fname)
457" call Dfunc("Rmdir(fname<".a:fname.">)")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000458 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
Bram Moolenaarc236c162008-07-13 17:41:49 +0000459 call system("rmdir /S/Q ".s:Escape(a:fname,0))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000460 else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000461 call system("/bin/rm -rf ".s:Escape(a:fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000462 endif
463" call Dret("Rmdir")
464endfun
465
466" ------------------------------------------------------------------------
467" Modelines And Restoration: {{{1
468let &cpo= s:keepcpo
469unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000470" vim:ts=8 fdm=marker