blob: 66d2eb5ced0e1c79a50eb453289f334457e4466e [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
3" Date: Nov 18, 2005
4" Version: 4
5" Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz>
6" 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 Moolenaara5792f52005-11-23 21:25:05 +000010" Copyright: Copyright (C) 2005 Charles E. Campbell, Jr. {{{1
11" 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,
14" tarPlugin.vim is provided *as is* and comes with no warranty
15" of any kind, either expressed or implied. By using this
16" plugin, you agree that in no event will the copyright
17" holder be liable for any damages resulting from the use
18" of this software.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019
Bram Moolenaarab194812005-09-14 21:40:12 +000020" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +000021" Initialization: {{{1
22let s:keepcpo= &cpo
23set cpo&vim
24if exists("g:loaded_tar")
25 finish
26endif
27let g:loaded_tar= "v4"
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000028
Bram Moolenaara5792f52005-11-23 21:25:05 +000029" ---------------------------------------------------------------------
30" Default Settings: {{{1
31if !exists("g:tar_browseoptions")
32 let g:tar_browseoptions= "Ptf"
33endif
34if !exists("g:tar_readoptions")
35 let g:tar_readoptions= "OPxf"
36endif
37if !exists("g:tar_writeoptions")
38 let g:tar_writeoptions= "uf"
39endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000040
Bram Moolenaara5792f52005-11-23 21:25:05 +000041" ----------------
42" Functions: {{{1
43" ----------------
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000044
Bram Moolenaara5792f52005-11-23 21:25:05 +000045" ---------------------------------------------------------------------
46" tar#Browse: {{{2
47fun! tar#Browse(tarfile)
48" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000049
Bram Moolenaara5792f52005-11-23 21:25:05 +000050 " sanity checks
51 if !executable("tar")
52 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
53 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
54" call Dret("tar#Browse")
55 return
56 endif
57 if !filereadable(a:tarfile)
58 if a:tarfile !~# '^\a\+://'
59 " if its an url, don't complain, let url-handlers such as vim do its thing
60 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
61 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
62 endif
63" call Dret("tar#Browse : file<".a:tarfile."> not readable")
64 return
65 endif
66 if &ma != 1
67 set ma
68 endif
69 let w:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000070
Bram Moolenaara5792f52005-11-23 21:25:05 +000071 setlocal noswapfile
72 setlocal buftype=nofile
73 setlocal bufhidden=hide
74 setlocal nobuflisted
75 setlocal nowrap
76 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000077
Bram Moolenaara5792f52005-11-23 21:25:05 +000078 " give header
79 exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'"
80 exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'"
81 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
82 0d
83 $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000084
Bram Moolenaara5792f52005-11-23 21:25:05 +000085 if a:tarfile =~# '\.\(gz\|tgz\)$'
86 exe "silent r! gzip -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
87 elseif a:tarfile =~# '\.bz2$'
88 exe "silent r! bzip2 -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
89 else
90 exe "silent r! tar -".g:tar_browseoptions." '".a:tarfile."'"
91 endif
92 %g@/$@d
93
94 setlocal noma nomod ro
95 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
96
97" call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +000098endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000099
Bram Moolenaarab194812005-09-14 21:40:12 +0000100" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000101" TarBrowseSelect: {{{2
102fun! s:TarBrowseSelect()
103" call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
104 let fname= getline(".")
105" call Decho("fname<".fname.">")
106
107 " sanity check
108 if fname =~ '^"'
109" call Dret("TarBrowseSelect")
110 return
111 endif
112
113 " about to make a new window, need to use w:tarfile
114 let tarfile= w:tarfile
115 let curfile= expand("%")
116
117 new
118 wincmd _
119 let s:tblfile_{winnr()}= curfile
120" call Decho("exe e tarfile:".tarfile.':'.fname)
121 exe "e tarfile:".tarfile.':'.fname
122 filetype detect
123
124" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
125endfun
126
127" ---------------------------------------------------------------------
128" tar#Read: {{{2
129fun! tar#Read(fname,mode)
130" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
131 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
132 let fname = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
133" call Decho("tarfile<".tarfile."> fname<".fname.">")
134
135 if tarfile =~# '\.\(gz\|tgz\)$'
136" call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -OPxf - '".fname."'")
137 exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
138 elseif a:fname =~# '\.bz2$'
139" call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'")
140 exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
141 else
142" call Decho("exe silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'")
143 exe "silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'"
144 endif
145 let w:tarfile= a:fname
146 exe "file tarfile:".fname
147
148 " cleanup
149 0d
150 set nomod
151
152" call Dret("tar#Read : w:tarfile<".w:tarfile.">")
153endfun
154
155" ---------------------------------------------------------------------
156" tar#Write: {{{2
157fun! tar#Write(fname)
158" call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
159
Bram Moolenaarab194812005-09-14 21:40:12 +0000160 " sanity checks
161 if !executable("tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000162 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
163 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +0000164" call Dret("tar#Write")
165 return
166 endif
167 if !exists("*mkdir")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000168 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
169 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +0000170" call Dret("tar#Write")
171 return
172 endif
173
174 let curdir= getcwd()
175 let tmpdir= tempname()
176" call Decho("orig tempname<".tmpdir.">")
177 if tmpdir =~ '\.'
178 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
179 endif
180" call Decho("tmpdir<".tmpdir.">")
181 call mkdir(tmpdir,"p")
182
183 " attempt to change to the indicated directory
184 try
185 exe "cd ".escape(tmpdir,' \')
186 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaara5792f52005-11-23 21:25:05 +0000187 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
188 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +0000189" call Dret("tar#Write")
190 return
191 endtry
192" call Decho("current directory now: ".getcwd())
193
Bram Moolenaara5792f52005-11-23 21:25:05 +0000194 " place temporary files under .../_ZIPVIM_/
195 if isdirectory("_ZIPVIM_")
196 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000197 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000198 call mkdir("_ZIPVIM_")
199 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000200" call Decho("current directory now: ".getcwd())
201
Bram Moolenaara5792f52005-11-23 21:25:05 +0000202 let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
203 let fname = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
204
205 " handle compressed archives
206 if tarfile =~# '\.gz'
207 call system("gzip -d ".tarfile)
208 let tarfile = substitute(tarfile,'\.gz','','e')
209 let compress= "gzip '".tarfile."'"
210 elseif tarfile =~# '\.tgz'
211 call system("gzip -d ".tarfile)
212 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
213 let compress= "gzip '".tarfile."'"
214 let tgz = 1
215 elseif tarfile =~# '\.bz2'
216 call system("bzip2 -d ".tarfile)
217 let tarfile = substitute(tarfile,'\.bz2','','e')
218 let compress= "bzip2 '".tarfile."'"
Bram Moolenaarab194812005-09-14 21:40:12 +0000219 endif
220
Bram Moolenaarab194812005-09-14 21:40:12 +0000221 if v:shell_error != 0
Bram Moolenaara5792f52005-11-23 21:25:05 +0000222 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
223 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +0000224 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000225
226" call Decho("tarfile<".tarfile."> fname<".fname.">")
227
228 let dirpath = substitute(fname,'/[^/]\+$','','e')
229 if tarfile !~ '/'
230 let tarfile= curdir.'/'.tarfile
231 endif
232" call Decho("tarfile<".tarfile."> fname<".fname.">")
233
234 call mkdir(dirpath,"p")
235 exe "w! ".fname
236 if executable("cygpath")
237 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
238 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
239 endif
240
241 " delete old file from tarfile
242" call Decho("tar --delete -f '".tarfile."' '".fname."'")
243 call system("tar --delete -f '".tarfile."' '".fname."'")
244 if v:shell_error != 0
245 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
246 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
247 else
248
249 " update tarfile with new file
250" call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
251 call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
252 if v:shell_error != 0
253 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
254 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
255 elseif exists("compress")
256" call Decho("call system(".compress.")")
257 call system(compress)
258 if exists("tgz")
259" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
260 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
261 endif
262 endif
263 endif
264
265 " support writing tarfiles across a network
266 if s:tblfile_{winnr()} =~ '^\a\+://'
267" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
268 let tblfile= s:tblfile_{winnr()}
269 1split|enew
270 let binkeep= &binary
271 let eikeep = &ei
272 set binary ei=all
273 exe "e! ".tarfile
274 call netrw#NetWrite(tblfile)
275 let &ei = eikeep
276 let &binary = binkeep
277 q!
278 unlet s:tblfile_{winnr()}
279 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000280 endif
281
282 " cleanup and restore current directory
283 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000284 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000285 exe "cd ".escape(curdir,' \')
286 setlocal nomod
287
288" call Dret("tar#Write")
289endfun
290
291" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000292" Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000293fun! s:Rmdir(fname)
294" call Dfunc("Rmdir(fname<".a:fname.">)")
295 if has("unix")
296 call system("/bin/rm -rf ".a:fname)
297 elseif has("win32") || has("win95") || has("win64") || has("win16")
298 if &shell =~? "sh$"
299 call system("/bin/rm -rf ".a:fname)
300 else
301 call system("del /S ".a:fname)
302 endif
303 endif
304" call Dret("Rmdir")
305endfun
306
Bram Moolenaara5792f52005-11-23 21:25:05 +0000307" ------------------------------------------------------------------------
308" Modelines And Restoration: {{{1
309let &cpo= s:keepcpo
310unlet s:keepcpo
311" vim:ts=8 fdm=marker