blob: 34eab9670946adba13e01dff814ec468af9d7ae9 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaarff034192013-04-24 18:51:19 +02003" Date: Apr 17, 2013
4" Version: 29
5" Maintainer: Charles E Campbell <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
Bram Moolenaara5792f52005-11-23 21:25:05 +00006" License: Vim License (see vim's :help license)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007"
Bram Moolenaara5792f52005-11-23 21:25:05 +00008" Contains many ideas from Michael Toren's <tar.vim>
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009"
Bram Moolenaarff034192013-04-24 18:51:19 +020010" Copyright: Copyright (C) 2005-2011 Charles E. Campbell {{{1
Bram Moolenaara5792f52005-11-23 21:25:05 +000011" Permission is hereby granted to use and distribute this code,
12" with or without modifications, provided that this copyright
13" notice is copied with it. Like anything else that's free,
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014" tar.vim and tarPlugin.vim are provided *as is* and comes
15" with no warranty of any kind, either expressed or implied.
16" By using this plugin, you agree that in no event will the
17" copyright holder be liable for any damages resulting from
18" the use of this software.
Bram Moolenaar5c736222010-01-06 20:54:52 +010019" call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +000020" ---------------------------------------------------------------------
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000021" Load Once: {{{1
Bram Moolenaar5c736222010-01-06 20:54:52 +010022if &cp || exists("g:loaded_tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +000023 finish
24endif
Bram Moolenaarff034192013-04-24 18:51:19 +020025let g:loaded_tar= "v29"
Bram Moolenaar5c736222010-01-06 20:54:52 +010026if v:version < 702
27 echohl WarningMsg
28 echo "***warning*** this version of tar needs vim 7.2"
29 echohl Normal
30 finish
Bram Moolenaar8c8de832008-06-24 22:58:06 +000031endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010032let s:keepcpo= &cpo
33set cpo&vim
Bram Moolenaarff034192013-04-24 18:51:19 +020034"DechoTabOn
Bram Moolenaar5c736222010-01-06 20:54:52 +010035"call Decho("loading autoload/tar.vim")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000036
Bram Moolenaara5792f52005-11-23 21:25:05 +000037" ---------------------------------------------------------------------
38" Default Settings: {{{1
39if !exists("g:tar_browseoptions")
40 let g:tar_browseoptions= "Ptf"
41endif
42if !exists("g:tar_readoptions")
43 let g:tar_readoptions= "OPxf"
44endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045if !exists("g:tar_cmd")
46 let g:tar_cmd= "tar"
47endif
Bram Moolenaara5792f52005-11-23 21:25:05 +000048if !exists("g:tar_writeoptions")
49 let g:tar_writeoptions= "uf"
50endif
Bram Moolenaar251e1912011-06-19 05:09:16 +020051if !exists("g:netrw_cygwin")
52 if has("win32") || has("win95") || has("win64") || has("win16")
53 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
54 let g:netrw_cygwin= 1
55 else
56 let g:netrw_cygwin= 0
57 endif
58 else
59 let g:netrw_cygwin= 0
60 endif
61endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010062if !exists("g:tar_copycmd")
63 if !exists("g:netrw_localcopycmd")
64 if has("win32") || has("win95") || has("win64") || has("win16")
65 if g:netrw_cygwin
66 let g:netrw_localcopycmd= "cp"
67 else
68 let g:netrw_localcopycmd= "copy"
69 endif
70 elseif has("unix") || has("macunix")
71 let g:netrw_localcopycmd= "cp"
72 else
73 let g:netrw_localcopycmd= ""
74 endif
75 endif
76 let g:tar_copycmd= g:netrw_localcopycmd
77endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010078if !exists("g:tar_extractcmd")
79 let g:tar_extractcmd= "tar -xf"
80endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000081
Bram Moolenaar8c8de832008-06-24 22:58:06 +000082" set up shell quoting character
83if !exists("g:tar_shq")
Bram Moolenaarff034192013-04-24 18:51:19 +020084 if exists("+shq") && exists("&shq") && &shq != ""
Bram Moolenaar8c8de832008-06-24 22:58:06 +000085 let g:tar_shq= &shq
86 elseif has("win32") || has("win95") || has("win64") || has("win16")
87 if exists("g:netrw_cygwin") && g:netrw_cygwin
88 let g:tar_shq= "'"
89 else
90 let g:tar_shq= '"'
91 endif
92 else
93 let g:tar_shq= "'"
94 endif
95" call Decho("g:tar_shq<".g:tar_shq.">")
96endif
97
Bram Moolenaara5792f52005-11-23 21:25:05 +000098" ----------------
99" Functions: {{{1
100" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000101
Bram Moolenaara5792f52005-11-23 21:25:05 +0000102" ---------------------------------------------------------------------
103" tar#Browse: {{{2
104fun! tar#Browse(tarfile)
105" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000106 let repkeep= &report
107 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000108
Bram Moolenaara5792f52005-11-23 21:25:05 +0000109 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000110 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000111 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000112 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000113 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000114" call Dret("tar#Browse")
115 return
116 endif
117 if !filereadable(a:tarfile)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000118" call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000119 if a:tarfile !~# '^\a\+://'
Bram Moolenaar3e496b02016-09-25 22:11:48 +0200120 " if it's an url, don't complain, let url-handlers such as vim do its thing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000121 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000122 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000123 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000124 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000125" call Dret("tar#Browse : file<".a:tarfile."> not readable")
126 return
127 endif
128 if &ma != 1
129 set ma
130 endif
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200131 let b:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000132
Bram Moolenaara5792f52005-11-23 21:25:05 +0000133 setlocal noswapfile
134 setlocal buftype=nofile
135 setlocal bufhidden=hide
136 setlocal nobuflisted
137 setlocal nowrap
138 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000139
Bram Moolenaara5792f52005-11-23 21:25:05 +0000140 " give header
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000141" call Decho("printing header")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000142 let lastline= line("$")
143 call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
144 call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
145 call setline(lastline+3,'" Select a file with cursor and press ENTER')
Bram Moolenaar251e1912011-06-19 05:09:16 +0200146 keepj $put =''
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100147 keepj sil! 0d
Bram Moolenaar251e1912011-06-19 05:09:16 +0200148 keepj $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000149
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000150 let tarfile= a:tarfile
Bram Moolenaarff034192013-04-24 18:51:19 +0200151 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000152 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100153 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000154 endif
Bram Moolenaard68071d2006-05-02 22:08:30 +0000155 let curlast= line("$")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000156 if tarfile =~# '\.\(gz\|tgz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100157" call Decho("1: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200158 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000159 elseif tarfile =~# '\.lrp'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100160" call Decho("2: exe silent r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200161 exe "sil! r! cat -- ".shellescape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100162 elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100163" call Decho("3: exe silent r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200164 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100165 elseif tarfile =~# '\.\(lzma\|tlz\)$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100166" call Decho("3: exe silent r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200167 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar477db062010-07-28 18:17:41 +0200168 elseif tarfile =~# '\.\(xz\|txz\)$'
169" call Decho("3: exe silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200170 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000171 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000172 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100173 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000174 let tarfile = substitute(tarfile, '-', './-', '')
175 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100176" call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,0))
Bram Moolenaar251e1912011-06-19 05:09:16 +0200177 exe "sil! r! ".g:tar_cmd." -".g:tar_browseoptions." ".shellescape(tarfile,1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000178 endif
179 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000180 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000181 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Bram Moolenaard68071d2006-05-02 22:08:30 +0000182" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaard68071d2006-05-02 22:08:30 +0000183 return
184 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000185 if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
186 redraw!
Bram Moolenaard68071d2006-05-02 22:08:30 +0000187 echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
Bram Moolenaar251e1912011-06-19 05:09:16 +0200188 keepj sil! %d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000189 let eikeep= &ei
190 set ei=BufReadCmd,FileReadCmd
Bram Moolenaarc236c162008-07-13 17:41:49 +0000191 exe "r ".fnameescape(a:tarfile)
Bram Moolenaard68071d2006-05-02 22:08:30 +0000192 let &ei= eikeep
Bram Moolenaar251e1912011-06-19 05:09:16 +0200193 keepj sil! 1d
Bram Moolenaard68071d2006-05-02 22:08:30 +0000194" call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000195 return
196 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000197
198 setlocal noma nomod ro
199 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
200
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000201 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200202" call Dret("tar#Browse : b:tarfile<".b:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000203endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000204
Bram Moolenaarab194812005-09-14 21:40:12 +0000205" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000206" TarBrowseSelect: {{{2
207fun! s:TarBrowseSelect()
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200208" call Dfunc("TarBrowseSelect() b:tarfile<".b:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000209 let repkeep= &report
210 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000211 let fname= getline(".")
212" call Decho("fname<".fname.">")
213
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000214 if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
215 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100216 echohl WarningMsg | echo '***warning*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000217" call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
218 return
219 endif
220
Bram Moolenaara5792f52005-11-23 21:25:05 +0000221 " sanity check
222 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000223 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000224" call Dret("TarBrowseSelect")
225 return
226 endif
227
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200228 " about to make a new window, need to use b:tarfile
229 let tarfile= b:tarfile
Bram Moolenaara5792f52005-11-23 21:25:05 +0000230 let curfile= expand("%")
Bram Moolenaarff034192013-04-24 18:51:19 +0200231 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000232 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100233 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000234 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000235
236 new
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000237 if !exists("g:tar_nomax") || g:tar_nomax == 0
238 wincmd _
239 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000240 let s:tblfile_{winnr()}= curfile
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000241 call tar#Read("tarfile:".tarfile.'::'.fname,1)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000242 filetype detect
Bram Moolenaarff034192013-04-24 18:51:19 +0200243 set nomod
244 exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"'.fnameescape(fname).'")'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000245
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000246 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000247" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
248endfun
249
250" ---------------------------------------------------------------------
251" tar#Read: {{{2
252fun! tar#Read(fname,mode)
253" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000254 let repkeep= &report
255 set report=10
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000256 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
257 let fname = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaarff034192013-04-24 18:51:19 +0200258 if has("win32unix") && executable("cygpath")
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 " assuming cygwin
Bram Moolenaar5c736222010-01-06 20:54:52 +0100260 let tarfile=substitute(system("cygpath -u ".shellescape(tarfile,0)),'\n$','','e')
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000261 endif
262" call Decho("tarfile<".tarfile.">")
263" call Decho("fname<".fname.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000264
Bram Moolenaar5c736222010-01-06 20:54:52 +0100265 if fname =~ '\.bz2$' && executable("bzcat")
266 let decmp= "|bzcat"
267 let doro = 1
268 elseif fname =~ '\.gz$' && executable("zcat")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000269 let decmp= "|zcat"
270 let doro = 1
Bram Moolenaar5c736222010-01-06 20:54:52 +0100271 elseif fname =~ '\.lzma$' && executable("lzcat")
272 let decmp= "|lzcat"
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000273 let doro = 1
Bram Moolenaar477db062010-07-28 18:17:41 +0200274 elseif fname =~ '\.xz$' && executable("xzcat")
275 let decmp= "|xzcat"
276 let doro = 1
Bram Moolenaara5792f52005-11-23 21:25:05 +0000277 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000278 let decmp=""
279 let doro = 0
Bram Moolenaar477db062010-07-28 18:17:41 +0200280 if fname =~ '\.bz2$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$'
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000281 setlocal bin
282 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000283 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000284
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000285 if exists("g:tar_secure")
286 let tar_secure= " -- "
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000287 else
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000288 let tar_secure= " "
289 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100290 if tarfile =~# '\.bz2$'
291" call Decho("7: exe silent r! bzip2 -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200292 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar5c736222010-01-06 20:54:52 +0100293 elseif tarfile =~# '\.\(gz\|tgz\)$'
294" call Decho("5: exe silent r! gzip -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd.' -'.g:tar_readoptions.' - '.tar_secure.shellescape(fname,1))
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200295 exe "sil! r! bzip2 -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000296 elseif tarfile =~# '\.lrp$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100297" call Decho("6: exe silent r! cat ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200298 exe "sil! r! cat -- ".shellescape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar5c736222010-01-06 20:54:52 +0100299 elseif tarfile =~# '\.lzma$'
300" call Decho("7: exe silent r! lzma -d -c ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200301 exe "sil! r! lzma -d -c -- ".shellescape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar477db062010-07-28 18:17:41 +0200302 elseif tarfile =~# '\.\(xz\|txz\)$'
303" call Decho("3: exe silent r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200304 exe "sil! r! xz --decompress --stdout -- ".shellescape(tarfile,1)." | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000305 else
306 if tarfile =~ '^\s*-'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100307 " A file name starting with a dash is taken as an option. Prepend ./ to avoid that.
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000308 let tarfile = substitute(tarfile, '-', './-', '')
309 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100310" call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions.tar_secure.shellescape(tarfile,1)." ".shellescape(fname,1).decmp)
311 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions.shellescape(tarfile,1)." ".tar_secure.shellescape(fname,1).decmp
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000312 endif
313
314 if doro
315 " because the reverse process of compressing changed files back into the tarball is not currently supported
316 setlocal ro
317 endif
318
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200319 let b:tarfile= a:fname
Bram Moolenaarc236c162008-07-13 17:41:49 +0000320 exe "file tarfile::".fnameescape(fname)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000321
322 " cleanup
Bram Moolenaar251e1912011-06-19 05:09:16 +0200323 keepj sil! 0d
Bram Moolenaara5792f52005-11-23 21:25:05 +0000324 set nomod
325
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000326 let &report= repkeep
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200327" call Dret("tar#Read : b:tarfile<".b:tarfile.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000328endfun
329
330" ---------------------------------------------------------------------
331" tar#Write: {{{2
332fun! tar#Write(fname)
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200333" call Dfunc("tar#Write(fname<".a:fname.">) b:tarfile<".b:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000334 let repkeep= &report
335 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000336
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000337 if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
338 redraw!
Bram Moolenaar5c736222010-01-06 20:54:52 +0100339 echohl WarningMsg | echo '***warning*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"'
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000340" call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
341 return
342 endif
343
Bram Moolenaarab194812005-09-14 21:40:12 +0000344 " sanity checks
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000345 if !executable(g:tar_cmd)
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000346 redraw!
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000347 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000348 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000349" call Dret("tar#Write")
350 return
351 endif
352 if !exists("*mkdir")
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000353 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000354 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000355 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000356" call Dret("tar#Write")
357 return
358 endif
359
360 let curdir= getcwd()
361 let tmpdir= tempname()
362" call Decho("orig tempname<".tmpdir.">")
363 if tmpdir =~ '\.'
364 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
365 endif
366" call Decho("tmpdir<".tmpdir.">")
367 call mkdir(tmpdir,"p")
368
369 " attempt to change to the indicated directory
370 try
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371 exe "cd ".fnameescape(tmpdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000372 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000373 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000374 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000375 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000376" call Dret("tar#Write")
377 return
378 endtry
379" call Decho("current directory now: ".getcwd())
380
Bram Moolenaara5792f52005-11-23 21:25:05 +0000381 " place temporary files under .../_ZIPVIM_/
382 if isdirectory("_ZIPVIM_")
383 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000384 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000385 call mkdir("_ZIPVIM_")
386 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000387" call Decho("current directory now: ".getcwd())
388
Bram Moolenaar7fc0c062010-08-10 21:43:35 +0200389 let tarfile = substitute(b:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
390 let fname = substitute(b:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000391
392 " handle compressed archives
Bram Moolenaar5c736222010-01-06 20:54:52 +0100393 if tarfile =~# '\.bz2'
394 call system("bzip2 -d -- ".shellescape(tarfile,0))
395 let tarfile = substitute(tarfile,'\.bz2','','e')
396 let compress= "bzip2 -- ".shellescape(tarfile,0)
397" call Decho("compress<".compress.">")
398 elseif tarfile =~# '\.gz'
399 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000400 let tarfile = substitute(tarfile,'\.gz','','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100401 let compress= "gzip -- ".shellescape(tarfile,0)
402" call Decho("compress<".compress.">")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000403 elseif tarfile =~# '\.tgz'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100404 call system("gzip -d -- ".shellescape(tarfile,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000405 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Bram Moolenaar5c736222010-01-06 20:54:52 +0100406 let compress= "gzip -- ".shellescape(tarfile,0)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000407 let tgz = 1
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000408" call Decho("compress<".compress.">")
Bram Moolenaar477db062010-07-28 18:17:41 +0200409 elseif tarfile =~# '\.xz'
410 call system("xz -d -- ".shellescape(tarfile,0))
411 let tarfile = substitute(tarfile,'\.xz','','e')
412 let compress= "xz -- ".shellescape(tarfile,0)
413" call Decho("compress<".compress.">")
414 elseif tarfile =~# '\.lzma'
415 call system("lzma -d -- ".shellescape(tarfile,0))
416 let tarfile = substitute(tarfile,'\.lzma','','e')
417 let compress= "lzma -- ".shellescape(tarfile,0)
418" call Decho("compress<".compress.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000419 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000420" call Decho("tarfile<".tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000421
Bram Moolenaarab194812005-09-14 21:40:12 +0000422 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000423 redraw!
Bram Moolenaara5792f52005-11-23 21:25:05 +0000424 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Bram Moolenaarab194812005-09-14 21:40:12 +0000425 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000426
427" call Decho("tarfile<".tarfile."> fname<".fname.">")
428
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000429 if fname =~ '/'
430 let dirpath = substitute(fname,'/[^/]\+$','','e')
Bram Moolenaarff034192013-04-24 18:51:19 +0200431 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100432 let dirpath = substitute(system("cygpath ".shellescape(dirpath, 0)),'\n','','e')
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000433 endif
434 call mkdir(dirpath,"p")
435 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000436 if tarfile !~ '/'
437 let tarfile= curdir.'/'.tarfile
438 endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000439 if tarfile =~ '^\s*-'
440 " A file name starting with a dash may be taken as an option. Prepend ./ to avoid that.
441 let tarfile = substitute(tarfile, '-', './-', '')
442 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000443" call Decho("tarfile<".tarfile."> fname<".fname.">")
444
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000445 if exists("g:tar_secure")
446 let tar_secure= " -- "
447 else
448 let tar_secure= " "
449 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000450 exe "w! ".fnameescape(fname)
Bram Moolenaarff034192013-04-24 18:51:19 +0200451 if has("win32unix") && executable("cygpath")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100452 let tarfile = substitute(system("cygpath ".shellescape(tarfile,0)),'\n','','e')
Bram Moolenaara5792f52005-11-23 21:25:05 +0000453 endif
454
455 " delete old file from tarfile
Bram Moolenaar5c736222010-01-06 20:54:52 +0100456" call Decho("system(".g:tar_cmd." --delete -f ".shellescape(tarfile,0)." -- ".shellescape(fname,0).")")
457 call system(g:tar_cmd." --delete -f ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000458 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000459 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000461 else
462
463 " update tarfile with new file
Bram Moolenaar5c736222010-01-06 20:54:52 +0100464" call Decho(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
465 call system(g:tar_cmd." -".g:tar_writeoptions." ".shellescape(tarfile,0).tar_secure.shellescape(fname,0))
Bram Moolenaara5792f52005-11-23 21:25:05 +0000466 if v:shell_error != 0
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000467 redraw!
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Bram Moolenaara5792f52005-11-23 21:25:05 +0000469 elseif exists("compress")
470" call Decho("call system(".compress.")")
471 call system(compress)
472 if exists("tgz")
473" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
474 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
475 endif
476 endif
477 endif
478
479 " support writing tarfiles across a network
480 if s:tblfile_{winnr()} =~ '^\a\+://'
481" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
482 let tblfile= s:tblfile_{winnr()}
483 1split|enew
Bram Moolenaar5c736222010-01-06 20:54:52 +0100484 let binkeep= &l:binary
Bram Moolenaara5792f52005-11-23 21:25:05 +0000485 let eikeep = &ei
486 set binary ei=all
Bram Moolenaarc236c162008-07-13 17:41:49 +0000487 exe "e! ".fnameescape(tarfile)
Bram Moolenaara5792f52005-11-23 21:25:05 +0000488 call netrw#NetWrite(tblfile)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100489 let &ei = eikeep
490 let &l:binary = binkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000491 q!
492 unlet s:tblfile_{winnr()}
493 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000494 endif
495
496 " cleanup and restore current directory
497 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000498 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarc236c162008-07-13 17:41:49 +0000499 exe "cd ".fnameescape(curdir)
Bram Moolenaarab194812005-09-14 21:40:12 +0000500 setlocal nomod
501
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000502 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000503" call Dret("tar#Write")
504endfun
505
506" ---------------------------------------------------------------------
Bram Moolenaarff034192013-04-24 18:51:19 +0200507" tar#Diff: {{{2
508fun! tar#Diff(userfname,fname)
509" call Dfunc("tar#Diff(userfname<".a:userfname."> fname<".a:fname.")")
510 let fname= a:fname
511 if a:userfname != ""
512 let fname= a:userfname
513 endif
514 if filereadable(fname)
515 " sets current file (from tarball) for diff'ing
516 " splits window vertically
517 " opens original file, sets it for diff'ing
518 " sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes)
519 diffthis
520 wincmd v
521 exe "e ".fnameescape(fname)
522 diffthis
523 else
524 redraw!
525 echo "***warning*** unable to read file<".fname.">"
526 endif
527" call Dret("tar#Diff")
528endfun
529
530" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100531" s:Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000532fun! s:Rmdir(fname)
533" call Dfunc("Rmdir(fname<".a:fname.">)")
534 if has("unix")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100535 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000536 elseif has("win32") || has("win95") || has("win64") || has("win16")
537 if &shell =~? "sh$"
Bram Moolenaar5c736222010-01-06 20:54:52 +0100538 call system("/bin/rm -rf -- ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000539 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100540 call system("del /S ".shellescape(a:fname,0))
Bram Moolenaarab194812005-09-14 21:40:12 +0000541 endif
542 endif
543" call Dret("Rmdir")
544endfun
545
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546" ---------------------------------------------------------------------
Bram Moolenaar5c736222010-01-06 20:54:52 +0100547" tar#Vimuntar: installs a tarball in the user's .vim / vimfiles directory {{{2
548fun! tar#Vimuntar(...)
549" call Dfunc("tar#Vimuntar() a:0=".a:0." a:1<".(exists("a:1")? a:1 : "-n/a-").">")
550 let tarball = expand("%")
551" call Decho("tarball<".tarball.">")
552 let tarbase = substitute(tarball,'\..*$','','')
553" call Decho("tarbase<".tarbase.">")
554 let tarhome = expand("%:p")
555 if has("win32") || has("win95") || has("win64") || has("win16")
556 let tarhome= substitute(tarhome,'\\','/','g')
557 endif
558 let tarhome= substitute(tarhome,'/[^/]*$','','')
559" call Decho("tarhome<".tarhome.">")
560 let tartail = expand("%:t")
561" call Decho("tartail<".tartail.">")
562 let curdir = getcwd()
563" call Decho("curdir <".curdir.">")
564 " set up vimhome
565 if a:0 > 0 && a:1 != ""
566 let vimhome= a:1
567 else
568 let vimhome= vimball#VimballHome()
569 endif
570" call Decho("vimhome<".vimhome.">")
571
572" call Decho("curdir<".curdir."> vimhome<".vimhome.">")
573 if simplify(curdir) != simplify(vimhome)
574 " copy (possibly compressed) tarball to .vim/vimfiles
575" call Decho(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
576 call system(netrw#WinPath(g:tar_copycmd)." ".shellescape(tartail)." ".shellescape(vimhome))
577" call Decho("exe cd ".fnameescape(vimhome))
578 exe "cd ".fnameescape(vimhome)
579 endif
580" call Decho("getcwd<".getcwd().">")
581
582 " if necessary, decompress the tarball; then, extract it
583 if tartail =~ '\.tgz'
584 if executable("gunzip")
585 silent exe "!gunzip ".shellescape(tartail)
586 elseif executable("gzip")
587 silent exe "!gzip -d ".shellescape(tartail)
Bram Moolenaarc236c162008-07-13 17:41:49 +0000588 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100589 echoerr "unable to decompress<".tartail."> on this sytem"
590 if simplify(curdir) != simplify(tarhome)
591 " remove decompressed tarball, restore directory
592" call Decho("delete(".tartail.".tar)")
593 call delete(tartail.".tar")
594" call Decho("exe cd ".fnameescape(curdir))
595 exe "cd ".fnameescape(curdir)
596 endif
597" call Dret("tar#Vimuntar")
598 return
Bram Moolenaarc236c162008-07-13 17:41:49 +0000599 endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000600 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100601 call vimball#Decompress(tartail,0)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000602 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100603 let extractcmd= netrw#WinPath(g:tar_extractcmd)
604" call Decho("system(".extractcmd." ".shellescape(tarbase.".tar").")")
605 call system(extractcmd." ".shellescape(tarbase.".tar"))
606
607 " set up help
608 if filereadable("doc/".tarbase.".txt")
609" call Decho("exe helptags ".getcwd()."/doc")
610 exe "helptags ".getcwd()."/doc"
611 endif
612
613 if simplify(tarhome) != simplify(vimhome)
614 " remove decompressed tarball, restore directory
615 call delete(vimhome."/".tarbase.".tar")
616 exe "cd ".fnameescape(curdir)
617 endif
618
619" call Dret("tar#Vimuntar")
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620endfun
621
Bram Moolenaar5c736222010-01-06 20:54:52 +0100622" =====================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +0000623" Modelines And Restoration: {{{1
624let &cpo= s:keepcpo
625unlet s:keepcpo
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626" vim:ts=8 fdm=marker