blob: de88b55907ecdc7d0fbbe0f790834206c8abbbfc [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
Christian Brabandta63f66e2024-08-05 20:47:34 +020014" 2024 Aug 05 by Vim Project: clean-up and make it work with shellslash on Windows
Bram Moolenaar9964e462007-05-05 17:54:07 +000015" License: Vim License (see vim's :help license)
Christian Brabandt1c673422024-06-15 14:49:24 +020016" Copyright: Copyright (C) 2005-2019 Charles E. Campbell {{{1
17" Permission is hereby granted to use and distribute this code,
18" with or without modifications, provided that this copyright
19" notice is copied with it. Like anything else that's free,
20" zip.vim and zipPlugin.vim are provided *as is* and comes with
21" no warranty of any kind, either expressed or implied. By using
22" this plugin, you agree that in no event will the copyright
23" holder be liable for any damages resulting from the use
24" of this software.
Bram Moolenaar60a795a2005-09-16 21:55:43 +000025
26" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +000027" Load Once: {{{1
Bram Moolenaar00a927d2010-05-14 23:24:24 +020028if &cp || exists("g:loaded_zip")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000029 finish
30endif
Bram Moolenaar71badf92023-04-22 22:40:14 +010031let g:loaded_zip= "v33"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020032if v:version < 702
33 echohl WarningMsg
Bram Moolenaard0796902016-09-16 20:02:31 +020034 echo "***warning*** this version of zip needs vim 7.2 or later"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020035 echohl Normal
36 finish
37endif
38let s:keepcpo= &cpo
39set cpo&vim
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 Moolenaar251e1912011-06-19 05:09:16 +0200139 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000140 let eikeep= &ei
141 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200142 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000143 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200144 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000145 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 +0000157endfun
158
159" ---------------------------------------------------------------------
160" ZipBrowseSelect: {{{2
161fun! s:ZipBrowseSelect()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000162 let repkeep= &report
163 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000164 let fname= getline(".")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100165 if !exists("b:zipfile")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100166 return
167 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000168
169 " sanity check
170 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000171 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000172 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 Moolenaar36c31f72005-11-28 23:01:53 +0000177 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000178 return
179 endif
180
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000181 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000182 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100183 let curfile = expand("%")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000184
Bram Moolenaar29634562020-01-09 21:46:04 +0100185 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000186 if !exists("g:zip_nomax") || g:zip_nomax == 0
187 wincmd _
188 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000189 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000190 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000191 filetype detect
192
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000193 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000194endfun
195
196" ---------------------------------------------------------------------
197" zip#Read: {{{2
198fun! zip#Read(fname,mode)
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000199 let repkeep= &report
200 set report=10
201
Bram Moolenaard68071d2006-05-02 22:08:30 +0000202 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000203 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
204 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000205 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000206 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
207 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000208 endif
zeertzjqc5bdd662024-08-04 18:35:50 +0200209 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard0796902016-09-16 20:02:31 +0200210 " sanity check
211 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
212 redraw!
213 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
Bram Moolenaard0796902016-09-16 20:02:31 +0200214 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200215 return
216 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000217
Bram Moolenaarff034192013-04-24 18:51:19 +0200218 " the following code does much the same thing as
zeertzjqc5bdd662024-08-04 18:35:50 +0200219 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000220 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200221 let temp = tempname()
222 let fn = expand('%:p')
Damienf0e9b722024-08-05 20:21:18 +0200223 exe "sil !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1).' > '.temp
Bram Moolenaarff034192013-04-24 18:51:19 +0200224 sil exe 'keepalt file '.temp
225 sil keepj e!
226 sil exe 'keepalt file '.fnameescape(fn)
227 call delete(temp)
228
Bram Moolenaar251e1912011-06-19 05:09:16 +0200229 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000230
231 " cleanup
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000232 set nomod
233
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000234 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000235endfun
236
237" ---------------------------------------------------------------------
238" zip#Write: {{{2
239fun! zip#Write(fname)
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000240 let repkeep= &report
241 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000242
243 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200244 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000245 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200246 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the ".g:zip_zipcmd." program" | echohl None
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000247 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000248 return
249 endif
250 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000251 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000252 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000253 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000254 return
255 endif
256
257 let curdir= getcwd()
258 let tmpdir= tempname()
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000259 if tmpdir =~ '\.'
260 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
261 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000262 call mkdir(tmpdir,"p")
263
264 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000265 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000266 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000267 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000268 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000269
270 " place temporary files under .../_ZIPVIM_/
271 if isdirectory("_ZIPVIM_")
Damien2cad9412024-07-24 20:07:00 +0200272 call delete("_ZIPVIM_", "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000273 endif
274 call mkdir("_ZIPVIM_")
275 cd _ZIPVIM_
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000276
Bram Moolenaard68071d2006-05-02 22:08:30 +0000277 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000278 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
279 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000280 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000281 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
282 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000283 endif
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000284
285 if fname =~ '/'
286 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200287 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000288 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000289 endif
290 call mkdir(dirpath,"p")
291 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000292 if zipfile !~ '/'
293 let zipfile= curdir.'/'.zipfile
294 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000295
Bram Moolenaarc236c162008-07-13 17:41:49 +0000296 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200297 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000298 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000299 endif
300
Bram Moolenaar9964e462007-05-05 17:54:07 +0000301 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
302 let fname = substitute(fname, '[', '[[]', 'g')
303 endif
304
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000305 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000306 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000307 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000308 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000309
310 elseif s:zipfile_{winnr()} =~ '^\a\+://'
311 " support writing zipfiles across a network
312 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaara5792f52005-11-23 21:25:05 +0000313 1split|enew
314 let binkeep= &binary
315 let eikeep = &ei
316 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100317 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000318 call netrw#NetWrite(netzipfile)
319 let &ei = eikeep
320 let &binary = binkeep
321 q!
322 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000323 endif
Damien2cad9412024-07-24 20:07:00 +0200324
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000325 " cleanup and restore current directory
326 cd ..
Damien2cad9412024-07-24 20:07:00 +0200327 call delete("_ZIPVIM_", "rf")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000328 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
Damien2cad9412024-07-24 20:07:00 +0200329 call delete(tmpdir, "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000330 setlocal nomod
331
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000332 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000333endfun
334
335" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200336" zip#Extract: extract a file from a zip archive {{{2
337fun! zip#Extract()
Bram Moolenaard0796902016-09-16 20:02:31 +0200338
339 let repkeep= &report
340 set report=10
341 let fname= getline(".")
Bram Moolenaard0796902016-09-16 20:02:31 +0200342
343 " sanity check
344 if fname =~ '^"'
345 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200346 return
347 endif
348 if fname =~ '/$'
349 redraw!
350 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
351 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200352 return
353 endif
354
355 " extract the file mentioned under the cursor
Damien38ce71c2024-07-23 19:56:54 +0200356 call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}")
Bram Moolenaard0796902016-09-16 20:02:31 +0200357 if v:shell_error != 0
358 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
359 elseif !filereadable(fname)
360 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
361 else
362 echo "***note*** successfully extracted ".fname
363 endif
364
365 " restore option
366 let &report= repkeep
367
Bram Moolenaard0796902016-09-16 20:02:31 +0200368endfun
369
370" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000371" s:Escape: {{{2
372fun! s:Escape(fname,isfilt)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000373 if exists("*shellescape")
374 if a:isfilt
375 let qnameq= shellescape(a:fname,1)
376 else
377 let qnameq= shellescape(a:fname)
378 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000379 else
380 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
381 endif
Christian Brabandt1c673422024-06-15 14:49:24 +0200382 if exists("+shellslash") && &shellslash && &shell =~ "cmd.exe"
383 " renormalize directory separator on Windows
384 let qnameq=substitute(qnameq, '/', '\\', 'g')
385 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000386 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000387endfun
388
389" ---------------------------------------------------------------------
390" ChgDir: {{{2
391fun! s:ChgDir(newdir,errlvl,errmsg)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000392 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000393 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000394 catch /^Vim\%((\a\+)\)\=:E344/
395 redraw!
396 if a:errlvl == s:NOTE
397 echo "***note*** ".a:errmsg
398 elseif a:errlvl == s:WARNING
399 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
400 elseif a:errlvl == s:ERROR
401 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
402 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000403 return 1
404 endtry
405
Bram Moolenaar9964e462007-05-05 17:54:07 +0000406 return 0
407endfun
408
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000409" ------------------------------------------------------------------------
410" Modelines And Restoration: {{{1
411let &cpo= s:keepcpo
412unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000413" vim:ts=8 fdm=marker