blob: 8876ef08e6050088c8200478181758f83251afc1 [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
Damien2cad9412024-07-24 20:07:00 +02002" AUTOLOAD PORTION
Damienf0e9b722024-08-05 20:21:18 +02003" Date: Aug 05, 2024
Bram Moolenaar71badf92023-04-22 22:40:14 +01004" Version: 33
Christian Brabandtf9ca1392024-02-19 20:37:11 +01005" Maintainer: This runtime file is looking for a new maintainer.
6" Former Maintainer: Charles E Campbell
Christian Brabandt1c673422024-06-15 14:49:24 +02007" Last Change:
Damien2cad9412024-07-24 20:07:00 +02008" 2024 Jun 16 by Vim Project: handle whitespace on Windows properly (#14998)
9" 2024 Jul 23 by Vim Project: fix 'x' command
10" 2024 Jul 24 by Vim Project: use delete() function
Damienf0e9b722024-08-05 20:21:18 +020011" 2024 Jul 30 by Vim Project: fix opening remote zipfile
zeertzjqc5bdd662024-08-04 18:35:50 +020012" 2024 Aug 04 by Vim Project: escape '[' in name of file to be extracted
Damienf0e9b722024-08-05 20:21:18 +020013" 2024 Aug 05 by Vim Project: workaround for the FreeBSD's unzip
Bram Moolenaar9964e462007-05-05 17:54:07 +000014" License: Vim License (see vim's :help license)
Christian Brabandt1c673422024-06-15 14:49:24 +020015" Copyright: Copyright (C) 2005-2019 Charles E. Campbell {{{1
16" Permission is hereby granted to use and distribute this code,
17" with or without modifications, provided that this copyright
18" notice is copied with it. Like anything else that's free,
19" zip.vim and zipPlugin.vim are provided *as is* and comes with
20" no warranty of any kind, either expressed or implied. By using
21" this plugin, you agree that in no event will the copyright
22" holder be liable for any damages resulting from the use
23" of this software.
Bram Moolenaar60a795a2005-09-16 21:55:43 +000024
25" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +000026" Load Once: {{{1
Bram Moolenaar00a927d2010-05-14 23:24:24 +020027if &cp || exists("g:loaded_zip")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000028 finish
29endif
Bram Moolenaar71badf92023-04-22 22:40:14 +010030let g:loaded_zip= "v33"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020031if v:version < 702
32 echohl WarningMsg
Bram Moolenaard0796902016-09-16 20:02:31 +020033 echo "***warning*** this version of zip needs vim 7.2 or later"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020034 echohl Normal
35 finish
36endif
37let s:keepcpo= &cpo
38set cpo&vim
Bram Moolenaar94f76b72013-07-04 22:50:40 +020039"DechoTabOn
Bram Moolenaar60a795a2005-09-16 21:55:43 +000040
Bram Moolenaardb552d602006-03-23 22:59:57 +000041let s:zipfile_escape = ' ?&;\'
Bram Moolenaar9964e462007-05-05 17:54:07 +000042let s:ERROR = 2
43let s:WARNING = 1
44let s:NOTE = 0
45
46" ---------------------------------------------------------------------
47" Global Values: {{{1
48if !exists("g:zip_shq")
Bram Moolenaar446cb832008-06-24 21:56:24 +000049 if &shq != ""
50 let g:zip_shq= &shq
51 elseif has("unix")
Bram Moolenaar9964e462007-05-05 17:54:07 +000052 let g:zip_shq= "'"
53 else
54 let g:zip_shq= '"'
55 endif
56endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000057if !exists("g:zip_zipcmd")
58 let g:zip_zipcmd= "zip"
59endif
60if !exists("g:zip_unzipcmd")
61 let g:zip_unzipcmd= "unzip"
62endif
Bram Moolenaard0796902016-09-16 20:02:31 +020063if !exists("g:zip_extractcmd")
64 let g:zip_extractcmd= g:zip_unzipcmd
65endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000066
D. Ben Knoblecd8a3ea2023-11-04 05:11:17 -040067if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd)
Christian Brabandt816fbcc2023-08-31 23:52:30 +020068 echoerr "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!"
69 finish
70endif
Anton Sharonov67c951d2023-09-05 21:03:27 +020071
Bram Moolenaar60a795a2005-09-16 21:55:43 +000072" ----------------
73" Functions: {{{1
74" ----------------
75
76" ---------------------------------------------------------------------
77" zip#Browse: {{{2
78fun! zip#Browse(zipfile)
Damienc4be0662024-07-30 19:14:35 +020079 " sanity check: ensure that the zipfile has "PK" as its first two letters
80 " (zip files have a leading PK as a "magic cookie")
81 if filereadable(a:zipfile) && readblob(a:zipfile, 0, 2) != 0z50.4B
82 exe "noswapfile noautocmd e " .. fnameescape(a:zipfile)
Bram Moolenaar94f76b72013-07-04 22:50:40 +020083 return
Bram Moolenaar94f76b72013-07-04 22:50:40 +020084 endif
85
Bram Moolenaar36c31f72005-11-28 23:01:53 +000086 let repkeep= &report
87 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000088
89 " sanity checks
Bram Moolenaare37d50a2008-08-06 17:06:04 +000090 if !exists("*fnameescape")
91 if &verbose > 1
Bram Moolenaar6c391a72021-09-09 21:55:11 +020092 echoerr "the zip plugin is not available (your vim doesn't support fnameescape())"
Bram Moolenaare37d50a2008-08-06 17:06:04 +000093 endif
94 return
95 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000096 if !executable(g:zip_unzipcmd)
Bram Moolenaar9964e462007-05-05 17:54:07 +000097 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +000098 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
Bram Moolenaar36c31f72005-11-28 23:01:53 +000099 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000100 return
101 endif
102 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000103 if a:zipfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100104 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaar9964e462007-05-05 17:54:07 +0000105 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000106 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000107 endif
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000108 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000109 return
110 endif
111 if &ma != 1
112 set ma
113 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +0000114 let b:zipfile= a:zipfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000115
116 setlocal noswapfile
117 setlocal buftype=nofile
118 setlocal bufhidden=hide
119 setlocal nobuflisted
120 setlocal nowrap
Bram Moolenaar519cc552021-11-16 19:18:26 +0000121
122 " Oct 12, 2021: need to re-use Bram's syntax/tar.vim.
123 " Setting the filetype to zip doesn't do anything (currently),
124 " but it is perhaps less confusing to curious perusers who do
125 " a :echo &ft
126 setf zip
127 run! syntax/tar.vim
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000128
129 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200130 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100131 \ '" Browsing zipfile '.a:zipfile,
132 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200133 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000134
Damienf0e9b722024-08-05 20:21:18 +0200135 exe $"keepj sil r! {g:zip_unzipcmd} -Z1 -- {s:Escape(a:zipfile, 1)}"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000136 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000137 redraw!
Bram Moolenaarc236c162008-07-13 17:41:49 +0000138 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000139" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200140 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000141 let eikeep= &ei
142 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200143 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000144 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200145 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000146" call Dret("zip#Browse")
147 return
148 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000149
Bram Moolenaard0796902016-09-16 20:02:31 +0200150 " Maps associated with zip plugin
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000151 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100152 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
153 noremap <silent> <buffer> x :call zip#Extract()<cr>
154 if &mouse != ""
155 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
156 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000157
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000158 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000159" call Dret("zip#Browse")
160endfun
161
162" ---------------------------------------------------------------------
163" ZipBrowseSelect: {{{2
164fun! s:ZipBrowseSelect()
Bram Moolenaar71badf92023-04-22 22:40:14 +0100165 " call Dfunc("ZipBrowseSelect() zipfile<".((exists("b:zipfile"))? b:zipfile : "n/a")."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000166 let repkeep= &report
167 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000168 let fname= getline(".")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100169 if !exists("b:zipfile")
170" call Dret("ZipBrowseSelect : b:zipfile doesn't exist!")
171 return
172 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000173
174 " sanity check
175 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000176 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000177" call Dret("ZipBrowseSelect")
178 return
179 endif
180 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000181 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000182 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000183" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000184 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000185" call Dret("ZipBrowseSelect")
186 return
187 endif
188
189" call Decho("fname<".fname.">")
190
191 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000192 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100193 let curfile = expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000194" call Decho("zipfile<".zipfile.">")
195" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000196
Bram Moolenaar29634562020-01-09 21:46:04 +0100197 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000198 if !exists("g:zip_nomax") || g:zip_nomax == 0
199 wincmd _
200 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000201 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000202" call Decho("exe e ".fnameescape("zipfile://".zipfile.'::'.fname))
203 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000204 filetype detect
205
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000206 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000207" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000208endfun
209
210" ---------------------------------------------------------------------
211" zip#Read: {{{2
212fun! zip#Read(fname,mode)
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000213 let repkeep= &report
214 set report=10
215
Bram Moolenaard68071d2006-05-02 22:08:30 +0000216 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000217 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
218 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000219 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000220 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
221 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000222 endif
zeertzjqc5bdd662024-08-04 18:35:50 +0200223 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard0796902016-09-16 20:02:31 +0200224 " sanity check
225 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
226 redraw!
227 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
228" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
229 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200230 return
231 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000232
Bram Moolenaarff034192013-04-24 18:51:19 +0200233 " the following code does much the same thing as
zeertzjqc5bdd662024-08-04 18:35:50 +0200234 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000235 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200236 let temp = tempname()
237 let fn = expand('%:p')
Damienf0e9b722024-08-05 20:21:18 +0200238 exe "sil !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1).' > '.temp
Bram Moolenaarff034192013-04-24 18:51:19 +0200239 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 Moolenaar60a795a2005-09-16 21:55:43 +0000247 set nomod
248
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000249 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000250endfun
251
252" ---------------------------------------------------------------------
253" zip#Write: {{{2
254fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000255" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000256 let repkeep= &report
257 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000258
259 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200260 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000261 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200262 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 +0000263" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000264 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000265" call Dret("zip#Write")
266 return
267 endif
268 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000269 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000270 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000271" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000272 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000273" call Dret("zip#Write")
274 return
275 endif
276
277 let curdir= getcwd()
278 let tmpdir= tempname()
279" call Decho("orig tempname<".tmpdir.">")
280 if tmpdir =~ '\.'
281 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
282 endif
283" call Decho("tmpdir<".tmpdir.">")
284 call mkdir(tmpdir,"p")
285
286 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000287 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000288 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000289" call Dret("zip#Write")
290 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000291 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000292" call Decho("current directory now: ".getcwd())
293
294 " place temporary files under .../_ZIPVIM_/
295 if isdirectory("_ZIPVIM_")
Damien2cad9412024-07-24 20:07:00 +0200296 call delete("_ZIPVIM_", "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000297 endif
298 call mkdir("_ZIPVIM_")
299 cd _ZIPVIM_
300" call Decho("current directory now: ".getcwd())
301
Bram Moolenaard68071d2006-05-02 22:08:30 +0000302 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000303 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
304 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000305 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000306 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
307 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000308 endif
309" call Decho("zipfile<".zipfile.">")
310" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000311
312 if fname =~ '/'
313 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200314 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000315 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000316 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000317" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000318 call mkdir(dirpath,"p")
319 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000320 if zipfile !~ '/'
321 let zipfile= curdir.'/'.zipfile
322 endif
323" call Decho("zipfile<".zipfile."> fname<".fname.">")
324
Bram Moolenaarc236c162008-07-13 17:41:49 +0000325 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200326 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000327 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000328 endif
329
Bram Moolenaar9964e462007-05-05 17:54:07 +0000330 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
331 let fname = substitute(fname, '[', '[[]', 'g')
332 endif
333
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000334" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
335 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000336 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000337 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000338 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000339" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000340
341 elseif s:zipfile_{winnr()} =~ '^\a\+://'
342 " support writing zipfiles across a network
343 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000344" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000345 1split|enew
346 let binkeep= &binary
347 let eikeep = &ei
348 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100349 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000350 call netrw#NetWrite(netzipfile)
351 let &ei = eikeep
352 let &binary = binkeep
353 q!
354 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000355 endif
Damien2cad9412024-07-24 20:07:00 +0200356
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000357 " cleanup and restore current directory
358 cd ..
Damien2cad9412024-07-24 20:07:00 +0200359 call delete("_ZIPVIM_", "rf")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000360 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
Damien2cad9412024-07-24 20:07:00 +0200361 call delete(tmpdir, "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000362 setlocal nomod
363
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000364 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000365" call Dret("zip#Write")
366endfun
367
368" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200369" zip#Extract: extract a file from a zip archive {{{2
370fun! zip#Extract()
371" call Dfunc("zip#Extract()")
372
373 let repkeep= &report
374 set report=10
375 let fname= getline(".")
376" call Decho("fname<".fname.">")
377
378 " sanity check
379 if fname =~ '^"'
380 let &report= repkeep
381" call Dret("zip#Extract")
382 return
383 endif
384 if fname =~ '/$'
385 redraw!
386 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
387 let &report= repkeep
388" call Dret("zip#Extract")
389 return
390 endif
391
392 " extract the file mentioned under the cursor
Damien38ce71c2024-07-23 19:56:54 +0200393 call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}")
Bram Moolenaard0796902016-09-16 20:02:31 +0200394" call Decho("zipfile<".b:zipfile.">")
395 if v:shell_error != 0
396 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
397 elseif !filereadable(fname)
398 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
399 else
400 echo "***note*** successfully extracted ".fname
401 endif
402
403 " restore option
404 let &report= repkeep
405
406" call Dret("zip#Extract")
407endfun
408
409" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000410" s:Escape: {{{2
411fun! s:Escape(fname,isfilt)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000412 if exists("*shellescape")
413 if a:isfilt
414 let qnameq= shellescape(a:fname,1)
415 else
416 let qnameq= shellescape(a:fname)
417 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000418 else
419 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
420 endif
Christian Brabandt1c673422024-06-15 14:49:24 +0200421 if exists("+shellslash") && &shellslash && &shell =~ "cmd.exe"
422 " renormalize directory separator on Windows
423 let qnameq=substitute(qnameq, '/', '\\', 'g')
424 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000425 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000426endfun
427
428" ---------------------------------------------------------------------
429" ChgDir: {{{2
430fun! s:ChgDir(newdir,errlvl,errmsg)
431" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
432
Bram Moolenaar9964e462007-05-05 17:54:07 +0000433 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000434 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000435 catch /^Vim\%((\a\+)\)\=:E344/
436 redraw!
437 if a:errlvl == s:NOTE
438 echo "***note*** ".a:errmsg
439 elseif a:errlvl == s:WARNING
440 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
441 elseif a:errlvl == s:ERROR
442 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
443 endif
444" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
445" call Dret("ChgDir 1")
446 return 1
447 endtry
448
449" call Dret("ChgDir 0")
450 return 0
451endfun
452
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000453" ------------------------------------------------------------------------
454" Modelines And Restoration: {{{1
455let &cpo= s:keepcpo
456unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000457" vim:ts=8 fdm=marker