blob: ea086e0882cea4608e1b9febfa4ec60d1525149a [file] [log] [blame]
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001" zip.vim: Handles browsing zipfiles
2" AUTOLOAD PORTION
Bram Moolenaard0796902016-09-16 20:02:31 +02003" Date: Sep 13, 2016
4" Version: 28
Bram Moolenaarff034192013-04-24 18:51:19 +02005" Maintainer: Charles E Campbell <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
Bram Moolenaar9964e462007-05-05 17:54:07 +00006" License: Vim License (see vim's :help license)
Bram Moolenaar94f76b72013-07-04 22:50:40 +02007" Copyright: Copyright (C) 2005-2013 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 Moolenaard0796902016-09-16 20:02:31 +020023let g:loaded_zip= "v28"
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
60" ----------------
61" Functions: {{{1
62" ----------------
63
64" ---------------------------------------------------------------------
65" zip#Browse: {{{2
66fun! zip#Browse(zipfile)
67" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
Bram Moolenaar3e496b02016-09-25 22:11:48 +020068 " sanity check: ensure that the zipfile has "PK" as its first two letters
Bram Moolenaar94f76b72013-07-04 22:50:40 +020069 " (zipped files have a leading PK as a "magic cookie")
70 if !filereadable(a:zipfile) || readfile(a:zipfile, "", 1)[0] !~ '^PK'
71 exe "noautocmd e ".fnameescape(a:zipfile)
72" call Dret("zip#Browse : not a zipfile<".a:zipfile.">")
73 return
74" else " Decho
Bram Moolenaar3e496b02016-09-25 22:11:48 +020075" call Decho("zip#Browse: a:zipfile<".a:zipfile."> passed PK test - it's a zip file")
Bram Moolenaar94f76b72013-07-04 22:50:40 +020076 endif
77
Bram Moolenaar36c31f72005-11-28 23:01:53 +000078 let repkeep= &report
79 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +000080
81 " sanity checks
Bram Moolenaare37d50a2008-08-06 17:06:04 +000082 if !exists("*fnameescape")
83 if &verbose > 1
84 echoerr "the zip plugin is not available (your vim doens't support fnameescape())"
85 endif
86 return
87 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +000088 if !executable(g:zip_unzipcmd)
Bram Moolenaar9964e462007-05-05 17:54:07 +000089 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +000090 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
Bram Moolenaar9964e462007-05-05 17:54:07 +000091" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +000092 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +000093" call Dret("zip#Browse")
94 return
95 endif
96 if !filereadable(a:zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +000097 if a:zipfile !~# '^\a\+://'
Bram Moolenaar3e496b02016-09-25 22:11:48 +020098 " 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!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000100 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000101" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000102 endif
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000103 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000104" call Dret("zip#Browse : file<".a:zipfile."> not readable")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000105 return
106 endif
Bram Moolenaardb552d602006-03-23 22:59:57 +0000107" call Decho("passed sanity checks")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000108 if &ma != 1
109 set ma
110 endif
Bram Moolenaarccc18222007-05-10 18:25:20 +0000111 let b:zipfile= a:zipfile
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000112
113 setlocal noswapfile
114 setlocal buftype=nofile
115 setlocal bufhidden=hide
116 setlocal nobuflisted
117 setlocal nowrap
118 set ft=tar
119
120 " give header
Bram Moolenaar251e1912011-06-19 05:09:16 +0200121 call append(0, ['" zip.vim version '.g:loaded_zip,
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100122 \ '" Browsing zipfile '.a:zipfile,
123 \ '" Select a file with cursor and press ENTER'])
Bram Moolenaar251e1912011-06-19 05:09:16 +0200124 keepj $
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000125
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000126" call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100127 exe "keepj sil! r! ".g:zip_unzipcmd." -Z -1 -- ".s:Escape(a:zipfile,1)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000128 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000129 redraw!
Bram Moolenaarc236c162008-07-13 17:41:49 +0000130 echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000131" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200132 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000133 let eikeep= &ei
134 set ei=BufReadCmd,FileReadCmd
Bram Moolenaar251e1912011-06-19 05:09:16 +0200135 exe "keepj r ".fnameescape(a:zipfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000136 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200137 keepj 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000138" call Dret("zip#Browse")
139 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 Moolenaard0796902016-09-16 20:02:31 +0200144 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
145 noremap <silent> <buffer> x :call zip#Extract()<cr>
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000146
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000147 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000148" call Dret("zip#Browse")
149endfun
150
151" ---------------------------------------------------------------------
152" ZipBrowseSelect: {{{2
153fun! s:ZipBrowseSelect()
Bram Moolenaarccc18222007-05-10 18:25:20 +0000154" call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000155 let repkeep= &report
156 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000157 let fname= getline(".")
158
159 " sanity check
160 if fname =~ '^"'
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000161 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000162" call Dret("ZipBrowseSelect")
163 return
164 endif
165 if fname =~ '/$'
Bram Moolenaar9964e462007-05-05 17:54:07 +0000166 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000167 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000168" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000169 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000170" call Dret("ZipBrowseSelect")
171 return
172 endif
173
174" call Decho("fname<".fname.">")
175
176 " get zipfile to the new-window
Bram Moolenaarccc18222007-05-10 18:25:20 +0000177 let zipfile = b:zipfile
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000178 let curfile= expand("%")
Bram Moolenaardb552d602006-03-23 22:59:57 +0000179" call Decho("zipfile<".zipfile.">")
180" call Decho("curfile<".curfile.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000181
182 new
Bram Moolenaar446cb832008-06-24 21:56:24 +0000183 if !exists("g:zip_nomax") || g:zip_nomax == 0
184 wincmd _
185 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000186 let s:zipfile_{winnr()}= curfile
Bram Moolenaarc236c162008-07-13 17:41:49 +0000187" call Decho("exe e ".fnameescape("zipfile:".zipfile.'::'.fname))
188 exe "e ".fnameescape("zipfile:".zipfile.'::'.fname)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000189 filetype detect
190
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000191 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000192" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000193endfun
194
195" ---------------------------------------------------------------------
196" zip#Read: {{{2
197fun! zip#Read(fname,mode)
198" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a: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")
203 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
204 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
205 else
206 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
207 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200208 let fname = substitute(fname, '[', '[[]', 'g')
Bram Moolenaard68071d2006-05-02 22:08:30 +0000209 endif
210" call Decho("zipfile<".zipfile.">")
211" call Decho("fname <".fname.">")
Bram Moolenaard0796902016-09-16 20:02:31 +0200212 " sanity check
213 if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
214 redraw!
215 echohl Error | echo "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program" | echohl None
216" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
217 let &report= repkeep
218" call Dret("zip#Write")
219 return
220 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000221
Bram Moolenaarff034192013-04-24 18:51:19 +0200222 " the following code does much the same thing as
223 " exe "keepj sil! r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1)
224 " but allows zipfile:... entries in quickfix lists
225 let temp = tempname()
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200226" call Decho("using temp file<".temp.">")
Bram Moolenaarff034192013-04-24 18:51:19 +0200227 let fn = expand('%:p')
228 exe "sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp
229" call Decho("exe sil! !".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fnameescape(fname),1).' > '.temp)
230 sil exe 'keepalt file '.temp
231 sil keepj e!
232 sil exe 'keepalt file '.fnameescape(fn)
233 call delete(temp)
234
Bram Moolenaar251e1912011-06-19 05:09:16 +0200235 filetype detect
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000236
237 " cleanup
Bram Moolenaar94f76b72013-07-04 22:50:40 +0200238 " keepj 0d " used to be needed for the ...r! ... method
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000239 set nomod
240
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000241 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000242" call Dret("zip#Read")
243endfun
244
245" ---------------------------------------------------------------------
246" zip#Write: {{{2
247fun! zip#Write(fname)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000248" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000249 let repkeep= &report
250 set report=10
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000251
252 " sanity checks
Bram Moolenaard0796902016-09-16 20:02:31 +0200253 if !executable(substitute(g:zip_zipcmd,'\s\+.*$','',''))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000254 redraw!
Bram Moolenaard0796902016-09-16 20:02:31 +0200255 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 +0000256" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000257 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000258" call Dret("zip#Write")
259 return
260 endif
261 if !exists("*mkdir")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000262 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000263 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000264" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000265 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000266" call Dret("zip#Write")
267 return
268 endif
269
270 let curdir= getcwd()
271 let tmpdir= tempname()
272" call Decho("orig tempname<".tmpdir.">")
273 if tmpdir =~ '\.'
274 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
275 endif
276" call Decho("tmpdir<".tmpdir.">")
277 call mkdir(tmpdir,"p")
278
279 " attempt to change to the indicated directory
Bram Moolenaar9964e462007-05-05 17:54:07 +0000280 if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000281 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000282" call Dret("zip#Write")
283 return
Bram Moolenaar9964e462007-05-05 17:54:07 +0000284 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000285" call Decho("current directory now: ".getcwd())
286
287 " place temporary files under .../_ZIPVIM_/
288 if isdirectory("_ZIPVIM_")
289 call s:Rmdir("_ZIPVIM_")
290 endif
291 call mkdir("_ZIPVIM_")
292 cd _ZIPVIM_
293" call Decho("current directory now: ".getcwd())
294
Bram Moolenaard68071d2006-05-02 22:08:30 +0000295 if has("unix")
296 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
297 let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
298 else
299 let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
300 let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
301 endif
302" call Decho("zipfile<".zipfile.">")
303" call Decho("fname <".fname.">")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000304
305 if fname =~ '/'
306 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200307 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000308 let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000309 endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000310" call Decho("mkdir(dirpath<".dirpath.">,p)")
Bram Moolenaarc7486e02005-12-29 22:48:26 +0000311 call mkdir(dirpath,"p")
312 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000313 if zipfile !~ '/'
314 let zipfile= curdir.'/'.zipfile
315 endif
316" call Decho("zipfile<".zipfile."> fname<".fname.">")
317
Bram Moolenaarc236c162008-07-13 17:41:49 +0000318 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200319 if has("win32unix") && executable("cygpath")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000320 let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000321 endif
322
Bram Moolenaar9964e462007-05-05 17:54:07 +0000323 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
324 let fname = substitute(fname, '[', '[[]', 'g')
325 endif
326
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000327" call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
328 call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000329 if v:shell_error != 0
Bram Moolenaar9964e462007-05-05 17:54:07 +0000330 redraw!
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000331 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
Bram Moolenaar9964e462007-05-05 17:54:07 +0000332" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaara5792f52005-11-23 21:25:05 +0000333
334 elseif s:zipfile_{winnr()} =~ '^\a\+://'
335 " support writing zipfiles across a network
336 let netzipfile= s:zipfile_{winnr()}
Bram Moolenaar9964e462007-05-05 17:54:07 +0000337" call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000338 1split|enew
339 let binkeep= &binary
340 let eikeep = &ei
341 set binary ei=all
Bram Moolenaarc236c162008-07-13 17:41:49 +0000342 exe "e! ".fnameescape(zipfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000343 call netrw#NetWrite(netzipfile)
344 let &ei = eikeep
345 let &binary = binkeep
346 q!
347 unlet s:zipfile_{winnr()}
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000348 endif
349
350 " cleanup and restore current directory
351 cd ..
352 call s:Rmdir("_ZIPVIM_")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000353 call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
354 call s:Rmdir(tmpdir)
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000355 setlocal nomod
356
Bram Moolenaar36c31f72005-11-28 23:01:53 +0000357 let &report= repkeep
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000358" call Dret("zip#Write")
359endfun
360
361" ---------------------------------------------------------------------
Bram Moolenaard0796902016-09-16 20:02:31 +0200362" zip#Extract: extract a file from a zip archive {{{2
363fun! zip#Extract()
364" call Dfunc("zip#Extract()")
365
366 let repkeep= &report
367 set report=10
368 let fname= getline(".")
369" call Decho("fname<".fname.">")
370
371 " sanity check
372 if fname =~ '^"'
373 let &report= repkeep
374" call Dret("zip#Extract")
375 return
376 endif
377 if fname =~ '/$'
378 redraw!
379 echohl Error | echo "***error*** (zip#Extract) Please specify a file, not a directory" | echohl None
380 let &report= repkeep
381" call Dret("zip#Extract")
382 return
383 endif
384
385 " extract the file mentioned under the cursor
386" call Decho("system(".g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell).")")
387 call system(g:zip_extractcmd." ".shellescape(b:zipfile)." ".shellescape(shell))
388" call Decho("zipfile<".b:zipfile.">")
389 if v:shell_error != 0
390 echohl Error | echo "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!" | echohl NONE
391 elseif !filereadable(fname)
392 echohl Error | echo "***error*** attempted to extract ".fname." but it doesn't appear to be present!"
393 else
394 echo "***note*** successfully extracted ".fname
395 endif
396
397 " restore option
398 let &report= repkeep
399
400" call Dret("zip#Extract")
401endfun
402
403" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000404" s:Escape: {{{2
405fun! s:Escape(fname,isfilt)
406" call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
407 if exists("*shellescape")
408 if a:isfilt
409 let qnameq= shellescape(a:fname,1)
410 else
411 let qnameq= shellescape(a:fname)
412 endif
Bram Moolenaar446cb832008-06-24 21:56:24 +0000413 else
414 let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
415 endif
416" call Dret("QuoteFileDir <".qnameq.">")
417 return qnameq
Bram Moolenaar9964e462007-05-05 17:54:07 +0000418endfun
419
420" ---------------------------------------------------------------------
421" ChgDir: {{{2
422fun! s:ChgDir(newdir,errlvl,errmsg)
423" call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl." errmsg<".a:errmsg.">)")
424
Bram Moolenaar9964e462007-05-05 17:54:07 +0000425 try
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000426 exe "cd ".fnameescape(a:newdir)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000427 catch /^Vim\%((\a\+)\)\=:E344/
428 redraw!
429 if a:errlvl == s:NOTE
430 echo "***note*** ".a:errmsg
431 elseif a:errlvl == s:WARNING
432 echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
433 elseif a:errlvl == s:ERROR
434 echohl Error | echo "***error*** ".a:errmsg | echohl NONE
435 endif
436" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
437" call Dret("ChgDir 1")
438 return 1
439 endtry
440
441" call Dret("ChgDir 0")
442 return 0
443endfun
444
445" ---------------------------------------------------------------------
Bram Moolenaarc236c162008-07-13 17:41:49 +0000446" s:Rmdir: {{{2
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000447fun! s:Rmdir(fname)
448" call Dfunc("Rmdir(fname<".a:fname.">)")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000449 if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
Bram Moolenaarc236c162008-07-13 17:41:49 +0000450 call system("rmdir /S/Q ".s:Escape(a:fname,0))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000451 else
Bram Moolenaarc236c162008-07-13 17:41:49 +0000452 call system("/bin/rm -rf ".s:Escape(a:fname,0))
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000453 endif
454" call Dret("Rmdir")
455endfun
456
457" ------------------------------------------------------------------------
458" Modelines And Restoration: {{{1
459let &cpo= s:keepcpo
460unlet s:keepcpo
Bram Moolenaarccc18222007-05-10 18:25:20 +0000461" vim:ts=8 fdm=marker