blob: f77d729f0365574983ede167ac1e6dc5141f79e2 [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
Damienc4be0662024-07-30 19:14:35 +02003" Date: Jul 30, 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
Damienc4be0662024-07-30 19:14:35 +020011" 2024 Jul 20 by Vim Project: fix opening remote zipfile
Bram Moolenaar9964e462007-05-05 17:54:07 +000012" License: Vim License (see vim's :help license)
Christian Brabandt1c673422024-06-15 14:49:24 +020013" Copyright: Copyright (C) 2005-2019 Charles E. Campbell {{{1
14" Permission is hereby granted to use and distribute this code,
15" with or without modifications, provided that this copyright
16" notice is copied with it. Like anything else that's free,
17" zip.vim and zipPlugin.vim are provided *as is* and comes with
18" no warranty of any kind, either expressed or implied. By using
19" this plugin, you agree that in no event will the copyright
20" holder be liable for any damages resulting from the use
21" of this software.
Bram Moolenaar60a795a2005-09-16 21:55:43 +000022
23" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +000024" Load Once: {{{1
Bram Moolenaar00a927d2010-05-14 23:24:24 +020025if &cp || exists("g:loaded_zip")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000026 finish
27endif
Bram Moolenaar71badf92023-04-22 22:40:14 +010028let g:loaded_zip= "v33"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020029if v:version < 702
30 echohl WarningMsg
Bram Moolenaard0796902016-09-16 20:02:31 +020031 echo "***warning*** this version of zip needs vim 7.2 or later"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020032 echohl Normal
33 finish
34endif
35let s:keepcpo= &cpo
36set cpo&vim
Bram Moolenaar94f76b72013-07-04 22:50:40 +020037"DechoTabOn
Bram Moolenaar60a795a2005-09-16 21:55:43 +000038
Bram Moolenaardb552d602006-03-23 22:59:57 +000039let s:zipfile_escape = ' ?&;\'
Bram Moolenaar9964e462007-05-05 17:54:07 +000040let s:ERROR = 2
41let s:WARNING = 1
42let s:NOTE = 0
43
44" ---------------------------------------------------------------------
45" Global Values: {{{1
46if !exists("g:zip_shq")
Bram Moolenaar446cb832008-06-24 21:56:24 +000047 if &shq != ""
48 let g:zip_shq= &shq
49 elseif has("unix")
Bram Moolenaar9964e462007-05-05 17:54:07 +000050 let g:zip_shq= "'"
51 else
52 let g:zip_shq= '"'
53 endif
54endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000055if !exists("g:zip_zipcmd")
56 let g:zip_zipcmd= "zip"
57endif
58if !exists("g:zip_unzipcmd")
59 let g:zip_unzipcmd= "unzip"
60endif
Bram Moolenaard0796902016-09-16 20:02:31 +020061if !exists("g:zip_extractcmd")
62 let g:zip_extractcmd= g:zip_unzipcmd
63endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000064
D. Ben Knoblecd8a3ea2023-11-04 05:11:17 -040065if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd)
Christian Brabandt816fbcc2023-08-31 23:52:30 +020066 echoerr "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!"
67 finish
68endif
Anton Sharonov67c951d2023-09-05 21:03:27 +020069
Bram Moolenaar60a795a2005-09-16 21:55:43 +000070" ----------------
71" Functions: {{{1
72" ----------------
73
74" ---------------------------------------------------------------------
75" zip#Browse: {{{2
76fun! zip#Browse(zipfile)
Damienc4be0662024-07-30 19:14:35 +020077 " sanity check: ensure that the zipfile has "PK" as its first two letters
78 " (zip files have a leading PK as a "magic cookie")
79 if filereadable(a:zipfile) && readblob(a:zipfile, 0, 2) != 0z50.4B
80 exe "noswapfile noautocmd e " .. fnameescape(a:zipfile)
Bram Moolenaar94f76b72013-07-04 22:50:40 +020081 return
Bram Moolenaar94f76b72013-07-04 22:50:40 +020082 endif
83
Bram Moolenaar36c31f72005-11-28 23:01:53 +000084 let repkeep= &report
85 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000086
87 " sanity checks
Bram Moolenaare37d50a2008-08-06 17:06:04 +000088 if !exists("*fnameescape")
89 if &verbose > 1
Bram Moolenaar6c391a72021-09-09 21:55:11 +020090 echoerr "the zip plugin is not available (your vim doesn't support fnameescape())"
Bram Moolenaare37d50a2008-08-06 17:06:04 +000091 endif
92 return
93 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000094 if !executable(g:zip_unzipcmd)
Bram Moolenaar9964e462007-05-05 17:54:07 +000095 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +000096 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
Bram Moolenaar36c31f72005-11-28 23:01:53 +000097 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000098 return
99 endif
100 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000101 if a:zipfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100102 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaar9964e462007-05-05 17:54:07 +0000103 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000104 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000105 endif
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000106 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000107 return
108 endif
109 if &ma != 1
110 set ma
111 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +0000112 let b:zipfile= a:zipfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000113
114 setlocal noswapfile
115 setlocal buftype=nofile
116 setlocal bufhidden=hide
117 setlocal nobuflisted
118 setlocal nowrap
Bram Moolenaar519cc552021-11-16 19:18:26 +0000119
120 " Oct 12, 2021: need to re-use Bram's syntax/tar.vim.
121 " Setting the filetype to zip doesn't do anything (currently),
122 " but it is perhaps less confusing to curious perusers who do
123 " a :echo &ft
124 setf zip
125 run! syntax/tar.vim
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000126
127 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200128 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100129 \ '" Browsing zipfile '.a:zipfile,
130 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200131 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000132
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000133" call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100134 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000135 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000136 redraw!
Bram Moolenaarc236c162008-07-13 17:41:49 +0000137 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000138" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
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" call Dret("zip#Browse")
146 return
147 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000148
Bram Moolenaard0796902016-09-16 20:02:31 +0200149 " Maps associated with zip plugin
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000150 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100151 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
152 noremap <silent> <buffer> x :call zip#Extract()<cr>
153 if &mouse != ""
154 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
155 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000156
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000157 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000158" call Dret("zip#Browse")
159endfun
160
161" ---------------------------------------------------------------------
162" ZipBrowseSelect: {{{2
163fun! s:ZipBrowseSelect()
Bram Moolenaar71badf92023-04-22 22:40:14 +0100164 " call Dfunc("ZipBrowseSelect() zipfile<".((exists("b:zipfile"))? b:zipfile : "n/a")."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000165 let repkeep= &report
166 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000167 let fname= getline(".")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100168 if !exists("b:zipfile")
169" call Dret("ZipBrowseSelect : b:zipfile doesn't exist!")
170 return
171 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000172
173 " sanity check
174 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000175 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000176" call Dret("ZipBrowseSelect")
177 return
178 endif
179 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000180 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000181 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000182" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000183 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000184" call Dret("ZipBrowseSelect")
185 return
186 endif
187
188" call Decho("fname<".fname.">")
189
190 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000191 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100192 let curfile = expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000193" call Decho("zipfile<".zipfile.">")
194" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000195
Bram Moolenaar29634562020-01-09 21:46:04 +0100196 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000197 if !exists("g:zip_nomax") || g:zip_nomax == 0
198 wincmd _
199 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000200 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000201" call Decho("exe e ".fnameescape("zipfile://".zipfile.'::'.fname))
202 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000203 filetype detect
204
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000205 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000206" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000207endfun
208
209" ---------------------------------------------------------------------
210" zip#Read: {{{2
211fun! zip#Read(fname,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
Bram Moolenaard0796902016-09-16 20:02:31 +0200223 " sanity check
224 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
225 redraw!
226 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
227" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
228 let &report= repkeep
Bram Moolenaard0796902016-09-16 20:02:31 +0200229 return
230 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000231
Bram Moolenaarff034192013-04-24 18:51:19 +0200232 " the following code does much the same thing as
233 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000234 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200235 let temp = tempname()
236 let fn = expand('%:p')
Christian Brabandt1c673422024-06-15 14:49:24 +0200237 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1).' > '.temp
Bram Moolenaarff034192013-04-24 18:51:19 +0200238 sil exe 'keepalt file '.temp
239 sil keepj e!
240 sil exe 'keepalt file '.fnameescape(fn)
241 call delete(temp)
242
Bram Moolenaar251e1912011-06-19 05:09:16 +0200243 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000244
245 " cleanup
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000246 set nomod
247
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000248 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000249endfun
250
251" ---------------------------------------------------------------------
252" zip#Write: {{{2
253fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000254" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000255 let repkeep= &report
256 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000257
258 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200259 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000260 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200261 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 +0000262" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000263 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000264" call Dret("zip#Write")
265 return
266 endif
267 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000268 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000269 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000270" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000271 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000272" call Dret("zip#Write")
273 return
274 endif
275
276 let curdir= getcwd()
277 let tmpdir= tempname()
278" call Decho("orig tempname<".tmpdir.">")
279 if tmpdir =~ '\.'
280 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
281 endif
282" call Decho("tmpdir<".tmpdir.">")
283 call mkdir(tmpdir,"p")
284
285 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000286 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000287 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000288" call Dret("zip#Write")
289 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000290 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000291" call Decho("current directory now: ".getcwd())
292
293 " place temporary files under .../_ZIPVIM_/
294 if isdirectory("_ZIPVIM_")
Damien2cad9412024-07-24 20:07:00 +0200295 call delete("_ZIPVIM_", "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000296 endif
297 call mkdir("_ZIPVIM_")
298 cd _ZIPVIM_
299" call Decho("current directory now: ".getcwd())
300
Bram Moolenaard68071d2006-05-02 22:08:30 +0000301 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000302 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
303 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000304 else
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 endif
308" call Decho("zipfile<".zipfile.">")
309" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000310
311 if fname =~ '/'
312 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200313 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000314 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000315 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000316" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000317 call mkdir(dirpath,"p")
318 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000319 if zipfile !~ '/'
320 let zipfile= curdir.'/'.zipfile
321 endif
322" call Decho("zipfile<".zipfile."> fname<".fname.">")
323
Bram Moolenaarc236c162008-07-13 17:41:49 +0000324 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200325 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000326 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000327 endif
328
Bram Moolenaar9964e462007-05-05 17:54:07 +0000329 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
330 let fname = substitute(fname, '[', '[[]', 'g')
331 endif
332
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000333" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
334 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000335 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000336 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000337 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000338" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000339
340 elseif s:zipfile_{winnr()} =~ '^\a\+://'
341 " support writing zipfiles across a network
342 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000343" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000344 1split|enew
345 let binkeep= &binary
346 let eikeep = &ei
347 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100348 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000349 call netrw#NetWrite(netzipfile)
350 let &ei = eikeep
351 let &binary = binkeep
352 q!
353 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000354 endif
Damien2cad9412024-07-24 20:07:00 +0200355
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000356 " cleanup and restore current directory
357 cd ..
Damien2cad9412024-07-24 20:07:00 +0200358 call delete("_ZIPVIM_", "rf")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000359 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
Damien2cad9412024-07-24 20:07:00 +0200360 call delete(tmpdir, "rf")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000361 setlocal nomod
362
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000363 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000364" call Dret("zip#Write")
365endfun
366
367" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200368" zip#Extract: extract a file from a zip archive {{{2
369fun! zip#Extract()
370" call Dfunc("zip#Extract()")
371
372 let repkeep= &report
373 set report=10
374 let fname= getline(".")
375" call Decho("fname<".fname.">")
376
377 " sanity check
378 if fname =~ '^"'
379 let &report= repkeep
380" call Dret("zip#Extract")
381 return
382 endif
383 if fname =~ '/$'
384 redraw!
385 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
386 let &report= repkeep
387" call Dret("zip#Extract")
388 return
389 endif
390
391 " extract the file mentioned under the cursor
Damien38ce71c2024-07-23 19:56:54 +0200392 call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}")
Bram Moolenaard0796902016-09-16 20:02:31 +0200393" call Decho("zipfile<".b:zipfile.">")
394 if v:shell_error != 0
395 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
396 elseif !filereadable(fname)
397 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
398 else
399 echo "***note*** successfully extracted ".fname
400 endif
401
402 " restore option
403 let &report= repkeep
404
405" call Dret("zip#Extract")
406endfun
407
408" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000409" s:Escape: {{{2
410fun! s:Escape(fname,isfilt)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000411 if exists("*shellescape")
412 if a:isfilt
413 let qnameq= shellescape(a:fname,1)
414 else
415 let qnameq= shellescape(a:fname)
416 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000417 else
418 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
419 endif
Christian Brabandt1c673422024-06-15 14:49:24 +0200420 if exists("+shellslash") && &shellslash && &shell =~ "cmd.exe"
421 " renormalize directory separator on Windows
422 let qnameq=substitute(qnameq, '/', '\\', 'g')
423 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000424 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000425endfun
426
427" ---------------------------------------------------------------------
428" ChgDir: {{{2
429fun! s:ChgDir(newdir,errlvl,errmsg)
430" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
431
Bram Moolenaar9964e462007-05-05 17:54:07 +0000432 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000433 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000434 catch /^Vim\%((\a\+)\)\=:E344/
435 redraw!
436 if a:errlvl == s:NOTE
437 echo "***note*** ".a:errmsg
438 elseif a:errlvl == s:WARNING
439 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
440 elseif a:errlvl == s:ERROR
441 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
442 endif
443" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
444" call Dret("ChgDir 1")
445 return 1
446 endtry
447
448" call Dret("ChgDir 0")
449 return 0
450endfun
451
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000452" ------------------------------------------------------------------------
453" Modelines And Restoration: {{{1
454let &cpo= s:keepcpo
455unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000456" vim:ts=8 fdm=marker