blob: cf7013518668085b75389d5b9d84d7c7a3a942d9 [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
Christian Brabandt120c0dd2024-08-05 20:51:47 +020034 echomsg "***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 Moolenaarccc18222007-05-10 18:25:20 +000090 if !executable(g:zip_unzipcmd)
Bram Moolenaar9964e462007-05-05 17:54:07 +000091 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +020092 echohl Error | echomsg "***error*** (zip#Browse) unzip not available on your system"
Bram Moolenaar36c31f72005-11-28 23:01:53 +000093 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000094 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!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200100 echohl Error | echomsg "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000101 endif
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000102 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000103 return
104 endif
105 if &ma != 1
106 set ma
107 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +0000108 let b:zipfile= a:zipfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000109
110 setlocal noswapfile
111 setlocal buftype=nofile
112 setlocal bufhidden=hide
113 setlocal nobuflisted
114 setlocal nowrap
Bram Moolenaar519cc552021-11-16 19:18:26 +0000115
116 " Oct 12, 2021: need to re-use Bram's syntax/tar.vim.
117 " Setting the filetype to zip doesn't do anything (currently),
118 " but it is perhaps less confusing to curious perusers who do
119 " a :echo &ft
120 setf zip
121 run! syntax/tar.vim
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000122
123 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200124 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100125 \ '" Browsing zipfile '.a:zipfile,
126 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200127 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000128
Damienf0e9b722024-08-05 20:21:18 +0200129 exe $"keepj sil r! {g:zip_unzipcmd} -Z1 -- {s:Escape(a:zipfile, 1)}"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000130 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000131 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200132 echohl WarningMsg | echomsg "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar251e1912011-06-19 05:09:16 +0200133 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000134 let eikeep= &ei
135 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200136 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000137 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200138 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000139 return
140 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000141
Bram Moolenaard0796902016-09-16 20:02:31 +0200142 " Maps associated with zip plugin
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000143 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100144 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
145 noremap <silent> <buffer> x :call zip#Extract()<cr>
146 if &mouse != ""
147 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
148 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000149
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000150 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000151endfun
152
153" ---------------------------------------------------------------------
154" ZipBrowseSelect: {{{2
155fun! s:ZipBrowseSelect()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000156 let repkeep= &report
157 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000158 let fname= getline(".")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100159 if !exists("b:zipfile")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100160 return
161 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000162
163 " sanity check
164 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000165 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000166 return
167 endif
168 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000169 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200170 echohl Error | echomsg "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000171 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000172 return
173 endif
174
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000175 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000176 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100177 let curfile = expand("%")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000178
Bram Moolenaar29634562020-01-09 21:46:04 +0100179 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000180 if !exists("g:zip_nomax") || g:zip_nomax == 0
181 wincmd _
182 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000183 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000184 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000185 filetype detect
186
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000187 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000188endfun
189
190" ---------------------------------------------------------------------
191" zip#Read: {{{2
192fun! zip#Read(fname,mode)
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000193 let repkeep= &report
194 set report=10
195
Bram Moolenaard68071d2006-05-02 22:08:30 +0000196 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000197 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
198 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000199 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000200 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
201 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000202 endif
zeertzjqc5bdd662024-08-04 18:35:50 +0200203 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard0796902016-09-16 20:02:31 +0200204 " sanity check
205 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
206 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200207 echohl Error | echomsg "***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 +0200208 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200209 return
210 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000211
Bram Moolenaarff034192013-04-24 18:51:19 +0200212 " the following code does much the same thing as
zeertzjqc5bdd662024-08-04 18:35:50 +0200213 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000214 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200215 let temp = tempname()
216 let fn = expand('%:p')
Damienf0e9b722024-08-05 20:21:18 +0200217 exe "sil !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1).' > '.temp
Bram Moolenaarff034192013-04-24 18:51:19 +0200218 sil exe 'keepalt file '.temp
219 sil keepj e!
220 sil exe 'keepalt file '.fnameescape(fn)
221 call delete(temp)
222
Bram Moolenaar251e1912011-06-19 05:09:16 +0200223 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000224
225 " cleanup
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000226 set nomod
227
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000228 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000229endfun
230
231" ---------------------------------------------------------------------
232" zip#Write: {{{2
233fun! zip#Write(fname)
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000234 let repkeep= &report
235 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000236
237 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200238 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000239 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200240 echohl Error | echomsg "***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 +0000241 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000242 return
243 endif
244 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000245 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200246 echohl Error | echomsg "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | 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
251 let curdir= getcwd()
252 let tmpdir= tempname()
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000253 if tmpdir =~ '\.'
254 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
255 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000256 call mkdir(tmpdir,"p")
257
258 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000259 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000260 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000261 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000262 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000263
264 " place temporary files under .../_ZIPVIM_/
265 if isdirectory("_ZIPVIM_")
Damien2cad9412024-07-24 20:07:00 +0200266 call delete("_ZIPVIM_", "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000267 endif
268 call mkdir("_ZIPVIM_")
269 cd _ZIPVIM_
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000270
Bram Moolenaard68071d2006-05-02 22:08:30 +0000271 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000272 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
273 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000274 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000275 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
276 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000277 endif
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000278
279 if fname =~ '/'
280 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200281 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000282 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000283 endif
284 call mkdir(dirpath,"p")
285 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000286 if zipfile !~ '/'
287 let zipfile= curdir.'/'.zipfile
288 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000289
Bram Moolenaarc236c162008-07-13 17:41:49 +0000290 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200291 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000292 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000293 endif
294
Bram Moolenaar9964e462007-05-05 17:54:07 +0000295 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
296 let fname = substitute(fname, '[', '[[]', 'g')
297 endif
298
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000299 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000300 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000301 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200302 echohl Error | echomsg "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000303
304 elseif s:zipfile_{winnr()} =~ '^\a\+://'
305 " support writing zipfiles across a network
306 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaara5792f52005-11-23 21:25:05 +0000307 1split|enew
308 let binkeep= &binary
309 let eikeep = &ei
310 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100311 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000312 call netrw#NetWrite(netzipfile)
313 let &ei = eikeep
314 let &binary = binkeep
315 q!
316 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000317 endif
Damien2cad9412024-07-24 20:07:00 +0200318
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000319 " cleanup and restore current directory
320 cd ..
Damien2cad9412024-07-24 20:07:00 +0200321 call delete("_ZIPVIM_", "rf")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000322 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
Damien2cad9412024-07-24 20:07:00 +0200323 call delete(tmpdir, "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000324 setlocal nomod
325
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000326 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000327endfun
328
329" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200330" zip#Extract: extract a file from a zip archive {{{2
331fun! zip#Extract()
Bram Moolenaard0796902016-09-16 20:02:31 +0200332
333 let repkeep= &report
334 set report=10
335 let fname= getline(".")
Bram Moolenaard0796902016-09-16 20:02:31 +0200336
337 " sanity check
338 if fname =~ '^"'
339 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200340 return
341 endif
342 if fname =~ '/$'
343 redraw!
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200344 echohl Error | echomsg "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
Bram Moolenaard0796902016-09-16 20:02:31 +0200345 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200346 return
347 endif
348
349 " extract the file mentioned under the cursor
Damien38ce71c2024-07-23 19:56:54 +0200350 call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}")
Bram Moolenaard0796902016-09-16 20:02:31 +0200351 if v:shell_error != 0
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200352 echohl Error | echomsg "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
Bram Moolenaard0796902016-09-16 20:02:31 +0200353 elseif !filereadable(fname)
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200354 echohl Error | echomsg "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
Bram Moolenaard0796902016-09-16 20:02:31 +0200355 else
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200356 echomsg "***note*** successfully extracted ".fname
Bram Moolenaard0796902016-09-16 20:02:31 +0200357 endif
358
359 " restore option
360 let &report= repkeep
361
Bram Moolenaard0796902016-09-16 20:02:31 +0200362endfun
363
364" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000365" s:Escape: {{{2
366fun! s:Escape(fname,isfilt)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000367 if exists("*shellescape")
368 if a:isfilt
369 let qnameq= shellescape(a:fname,1)
370 else
371 let qnameq= shellescape(a:fname)
372 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000373 else
374 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
375 endif
Christian Brabandt1c673422024-06-15 14:49:24 +0200376 if exists("+shellslash") && &shellslash && &shell =~ "cmd.exe"
377 " renormalize directory separator on Windows
378 let qnameq=substitute(qnameq, '/', '\\', 'g')
379 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000380 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000381endfun
382
383" ---------------------------------------------------------------------
384" ChgDir: {{{2
385fun! s:ChgDir(newdir,errlvl,errmsg)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000386 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000387 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000388 catch /^Vim\%((\a\+)\)\=:E344/
389 redraw!
390 if a:errlvl == s:NOTE
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200391 echomsg "***note*** ".a:errmsg
Bram Moolenaar9964e462007-05-05 17:54:07 +0000392 elseif a:errlvl == s:WARNING
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200393 echohl WarningMsg | echomsg "***warning*** ".a:errmsg | echohl NONE
Bram Moolenaar9964e462007-05-05 17:54:07 +0000394 elseif a:errlvl == s:ERROR
Christian Brabandt120c0dd2024-08-05 20:51:47 +0200395 echohl Error | echomsg "***error*** ".a:errmsg | echohl NONE
Bram Moolenaar9964e462007-05-05 17:54:07 +0000396 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000397 return 1
398 endtry
399
Bram Moolenaar9964e462007-05-05 17:54:07 +0000400 return 0
401endfun
402
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000403" ------------------------------------------------------------------------
404" Modelines And Restoration: {{{1
405let &cpo= s:keepcpo
406unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000407" vim:ts=8 fdm=marker