blob: 8b39c91c3ab04f23a9ec9be5cb87d03c6d96e397 [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaar71badf92023-04-22 22:40:14 +01003" Date: Mar 12, 2023
4" Version: 33
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 Moolenaar71badf92023-04-22 22:40:14 +010023let g:loaded_zip= "v33"
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
Anton Sharonov67c951d2023-09-05 21:03:27 +020060let s:tmp_cwd = getcwd()
61if (fnamemodify(exepath(g:zip_unzipcmd), ":p:h") ==# getcwd()
62 \ && (index(split($PATH,has("win32")? ';' : ':'), s:tmp_cwd) == -1 || s:tmp_cwd == '.'))
63 unlet s:tmp_cwd
Christian Brabandt816fbcc2023-08-31 23:52:30 +020064 echoerr "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!"
65 finish
66endif
Anton Sharonov67c951d2023-09-05 21:03:27 +020067unlet s:tmp_cwd
68
Bram Moolenaar60a795a2005-09-16 21:55:43 +000069" ----------------
70" Functions: {{{1
71" ----------------
72
73" ---------------------------------------------------------------------
74" zip#Browse: {{{2
75fun! zip#Browse(zipfile)
76" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
Bram Moolenaarcb80aa22020-10-26 21:12:46 +010077 " sanity check: insure that the zipfile has "PK" as its first two letters
Bram Moolenaar94f76b72013-07-04 22:50:40 +020078 " (zipped files have a leading PK as a "magic cookie")
79 if !filereadable(a:zipfile) || readfile(a:zipfile, "", 1)[0] !~ '^PK'
Bram Moolenaar29634562020-01-09 21:46:04 +010080 exe "noswapfile noautocmd noswapfile e ".fnameescape(a:zipfile)
Bram Moolenaar94f76b72013-07-04 22:50:40 +020081" call Dret("zip#Browse : not a zipfile<".a:zipfile.">")
82 return
83" else " Decho
Bram Moolenaar8024f932020-01-14 19:29:13 +010084" call Decho("zip#Browse: a:zipfile<".a:zipfile."> passed PK test - it's a zip file")
Bram Moolenaar94f76b72013-07-04 22:50:40 +020085 endif
86
Bram Moolenaar36c31f72005-11-28 23:01:53 +000087 let repkeep= &report
88 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000089
90 " sanity checks
Bram Moolenaare37d50a2008-08-06 17:06:04 +000091 if !exists("*fnameescape")
92 if &verbose > 1
Bram Moolenaar6c391a72021-09-09 21:55:11 +020093 echoerr "the zip plugin is not available (your vim doesn't support fnameescape())"
Bram Moolenaare37d50a2008-08-06 17:06:04 +000094 endif
95 return
96 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000097 if !executable(g:zip_unzipcmd)
Bram Moolenaar9964e462007-05-05 17:54:07 +000098 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +000099 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
Bram Moolenaar9964e462007-05-05 17:54:07 +0000100" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000101 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000102" call Dret("zip#Browse")
103 return
104 endif
105 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000106 if a:zipfile !~# '^\a\+://'
Bram Moolenaar8024f932020-01-14 19:29:13 +0100107 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaar9964e462007-05-05 17:54:07 +0000108 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000109 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000110" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000111 endif
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000112 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000113" call Dret("zip#Browse : file<".a:zipfile."> not readable")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000114 return
115 endif
Bram Moolenaardb552d602006-03-23 22:59:57 +0000116" call Decho("passed sanity checks")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000117 if &ma != 1
118 set ma
119 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +0000120 let b:zipfile= a:zipfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000121
122 setlocal noswapfile
123 setlocal buftype=nofile
124 setlocal bufhidden=hide
125 setlocal nobuflisted
126 setlocal nowrap
Bram Moolenaar519cc552021-11-16 19:18:26 +0000127
128 " Oct 12, 2021: need to re-use Bram's syntax/tar.vim.
129 " Setting the filetype to zip doesn't do anything (currently),
130 " but it is perhaps less confusing to curious perusers who do
131 " a :echo &ft
132 setf zip
133 run! syntax/tar.vim
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000134
135 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200136 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100137 \ '" Browsing zipfile '.a:zipfile,
138 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200139 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000140
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000141" call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100142 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000143 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000144 redraw!
Bram Moolenaarc236c162008-07-13 17:41:49 +0000145 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000146" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200147 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000148 let eikeep= &ei
149 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200150 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000151 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200152 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000153" call Dret("zip#Browse")
154 return
155 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000156
Bram Moolenaard0796902016-09-16 20:02:31 +0200157 " Maps associated with zip plugin
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000158 setlocal noma nomod ro
Bram Moolenaar29634562020-01-09 21:46:04 +0100159 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
160 noremap <silent> <buffer> x :call zip#Extract()<cr>
161 if &mouse != ""
162 noremap <silent> <buffer> <leftmouse> <leftmouse>:call <SID>ZipBrowseSelect()<cr>
163 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000164
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000165 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000166" call Dret("zip#Browse")
167endfun
168
169" ---------------------------------------------------------------------
170" ZipBrowseSelect: {{{2
171fun! s:ZipBrowseSelect()
Bram Moolenaar71badf92023-04-22 22:40:14 +0100172 " call Dfunc("ZipBrowseSelect() zipfile<".((exists("b:zipfile"))? b:zipfile : "n/a")."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000173 let repkeep= &report
174 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000175 let fname= getline(".")
Bram Moolenaar71badf92023-04-22 22:40:14 +0100176 if !exists("b:zipfile")
177" call Dret("ZipBrowseSelect : b:zipfile doesn't exist!")
178 return
179 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000180
181 " sanity check
182 if fname =~ '^"'
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 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000188 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000189 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000190" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000191 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000192" call Dret("ZipBrowseSelect")
193 return
194 endif
195
196" call Decho("fname<".fname.">")
197
198 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000199 let zipfile = b:zipfile
Bram Moolenaar29634562020-01-09 21:46:04 +0100200 let curfile = expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000201" call Decho("zipfile<".zipfile.">")
202" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000203
Bram Moolenaar29634562020-01-09 21:46:04 +0100204 noswapfile new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000205 if !exists("g:zip_nomax") || g:zip_nomax == 0
206 wincmd _
207 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000208 let s:zipfile_{winnr()}= curfile
Bram Moolenaar519cc552021-11-16 19:18:26 +0000209" call Decho("exe e ".fnameescape("zipfile://".zipfile.'::'.fname))
210 exe "noswapfile e ".fnameescape("zipfile://".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000211 filetype detect
212
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000213 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000214" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000215endfun
216
217" ---------------------------------------------------------------------
218" zip#Read: {{{2
219fun! zip#Read(fname,mode)
220" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000221 let repkeep= &report
222 set report=10
223
Bram Moolenaard68071d2006-05-02 22:08:30 +0000224 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000225 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
226 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000227 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000228 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
229 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200230 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000231 endif
232" call Decho("zipfile<".zipfile.">")
233" call Decho("fname <".fname.">")
Bram Moolenaard0796902016-09-16 20:02:31 +0200234 " sanity check
235 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
236 redraw!
237 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
238" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
239 let &report= repkeep
240" call Dret("zip#Write")
241 return
242 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000243
Bram Moolenaarff034192013-04-24 18:51:19 +0200244 " the following code does much the same thing as
245 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
Bram Moolenaar519cc552021-11-16 19:18:26 +0000246 " but allows zipfile://... entries in quickfix lists
Bram Moolenaarff034192013-04-24 18:51:19 +0200247 let temp = tempname()
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200248" call Decho("using temp file<".temp.">")
Bram Moolenaarff034192013-04-24 18:51:19 +0200249 let fn = expand('%:p')
250 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp
251" call Decho("exe sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp)
252 sil exe 'keepalt file '.temp
253 sil keepj e!
254 sil exe 'keepalt file '.fnameescape(fn)
255 call delete(temp)
256
Bram Moolenaar251e1912011-06-19 05:09:16 +0200257 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000258
259 " cleanup
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200260 " keepj 0d " used to be needed for the ...r! ... method
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000261 set nomod
262
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000263 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000264" call Dret("zip#Read")
265endfun
266
267" ---------------------------------------------------------------------
268" zip#Write: {{{2
269fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000270" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000271 let repkeep= &report
272 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000273
274 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200275 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000276 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200277 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 +0000278" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000279 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000280" call Dret("zip#Write")
281 return
282 endif
283 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000284 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000285 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000286" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
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
290 endif
291
292 let curdir= getcwd()
293 let tmpdir= tempname()
294" call Decho("orig tempname<".tmpdir.">")
295 if tmpdir =~ '\.'
296 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
297 endif
298" call Decho("tmpdir<".tmpdir.">")
299 call mkdir(tmpdir,"p")
300
301 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000302 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000303 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000304" call Dret("zip#Write")
305 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000306 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000307" call Decho("current directory now: ".getcwd())
308
309 " place temporary files under .../_ZIPVIM_/
310 if isdirectory("_ZIPVIM_")
311 call s:Rmdir("_ZIPVIM_")
312 endif
313 call mkdir("_ZIPVIM_")
314 cd _ZIPVIM_
315" call Decho("current directory now: ".getcwd())
316
Bram Moolenaard68071d2006-05-02 22:08:30 +0000317 if has("unix")
Bram Moolenaar519cc552021-11-16 19:18:26 +0000318 let zipfile = substitute(a:fname,'zipfile://\(.\{-}\)::[^\\].*$','\1','')
319 let fname = substitute(a:fname,'zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000320 else
Bram Moolenaar519cc552021-11-16 19:18:26 +0000321 let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
322 let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000323 endif
324" call Decho("zipfile<".zipfile.">")
325" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000326
327 if fname =~ '/'
328 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200329 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000330 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000331 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000332" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000333 call mkdir(dirpath,"p")
334 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000335 if zipfile !~ '/'
336 let zipfile= curdir.'/'.zipfile
337 endif
338" call Decho("zipfile<".zipfile."> fname<".fname.">")
339
Bram Moolenaarc236c162008-07-13 17:41:49 +0000340 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200341 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000342 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000343 endif
344
Bram Moolenaar9964e462007-05-05 17:54:07 +0000345 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
346 let fname = substitute(fname, '[', '[[]', 'g')
347 endif
348
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000349" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
350 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000351 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000352 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000353 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000354" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000355
356 elseif s:zipfile_{winnr()} =~ '^\a\+://'
357 " support writing zipfiles across a network
358 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000359" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000360 1split|enew
361 let binkeep= &binary
362 let eikeep = &ei
363 set binary ei=all
Bram Moolenaar29634562020-01-09 21:46:04 +0100364 exe "noswapfile e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000365 call netrw#NetWrite(netzipfile)
366 let &ei = eikeep
367 let &binary = binkeep
368 q!
369 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000370 endif
371
372 " cleanup and restore current directory
373 cd ..
374 call s:Rmdir("_ZIPVIM_")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000375 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
376 call s:Rmdir(tmpdir)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000377 setlocal nomod
378
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000379 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000380" call Dret("zip#Write")
381endfun
382
383" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200384" zip#Extract: extract a file from a zip archive {{{2
385fun! zip#Extract()
386" call Dfunc("zip#Extract()")
387
388 let repkeep= &report
389 set report=10
390 let fname= getline(".")
391" call Decho("fname<".fname.">")
392
393 " sanity check
394 if fname =~ '^"'
395 let &report= repkeep
396" call Dret("zip#Extract")
397 return
398 endif
399 if fname =~ '/$'
400 redraw!
401 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
402 let &report= repkeep
403" call Dret("zip#Extract")
404 return
405 endif
406
407 " extract the file mentioned under the cursor
408" call Decho("system(".g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell).")")
409 call system(g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell))
410" call Decho("zipfile<".b:zipfile.">")
411 if v:shell_error != 0
412 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
413 elseif !filereadable(fname)
414 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
415 else
416 echo "***note*** successfully extracted ".fname
417 endif
418
419 " restore option
420 let &report= repkeep
421
422" call Dret("zip#Extract")
423endfun
424
425" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000426" s:Escape: {{{2
427fun! s:Escape(fname,isfilt)
428" call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
429 if exists("*shellescape")
430 if a:isfilt
431 let qnameq= shellescape(a:fname,1)
432 else
433 let qnameq= shellescape(a:fname)
434 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000435 else
436 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
437 endif
438" call Dret("QuoteFileDir <".qnameq.">")
439 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000440endfun
441
442" ---------------------------------------------------------------------
443" ChgDir: {{{2
444fun! s:ChgDir(newdir,errlvl,errmsg)
445" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
446
Bram Moolenaar9964e462007-05-05 17:54:07 +0000447 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000448 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000449 catch /^Vim\%((\a\+)\)\=:E344/
450 redraw!
451 if a:errlvl == s:NOTE
452 echo "***note*** ".a:errmsg
453 elseif a:errlvl == s:WARNING
454 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
455 elseif a:errlvl == s:ERROR
456 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
457 endif
458" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
459" call Dret("ChgDir 1")
460 return 1
461 endtry
462
463" call Dret("ChgDir 0")
464 return 0
465endfun
466
467" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000468" s:Rmdir: {{{2
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000469fun! s:Rmdir(fname)
470" call Dfunc("Rmdir(fname<".a:fname.">)")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000471 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
Bram Moolenaarc236c162008-07-13 17:41:49 +0000472 call system("rmdir /S/Q ".s:Escape(a:fname,0))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000473 else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000474 call system("/bin/rm -rf ".s:Escape(a:fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000475 endif
476" call Dret("Rmdir")
477endfun
478
479" ------------------------------------------------------------------------
480" Modelines And Restoration: {{{1
481let &cpo= s:keepcpo
482unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000483" vim:ts=8 fdm=marker