blob: 41a006e9f6d23fcc2ff4a5ec1c121a43d82b6040 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +00003" Date: Dec 24, 2005
4" Version: 7
Bram Moolenaara5792f52005-11-23 21:25:05 +00005" 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
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +000027let g:loaded_tar= "v7"
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 Moolenaarbba577a2005-11-28 23:05:55 +000049 let repkeep= &report
50 set report=10
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000051
Bram Moolenaara5792f52005-11-23 21:25:05 +000052 " sanity checks
53 if !executable("tar")
54 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
55 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +000056 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +000057" call Dret("tar#Browse")
58 return
59 endif
60 if !filereadable(a:tarfile)
61 if a:tarfile !~# '^\a\+://'
62 " if its an url, don't complain, let url-handlers such as vim do its thing
63 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
64 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
65 endif
Bram Moolenaarbba577a2005-11-28 23:05:55 +000066 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +000067" call Dret("tar#Browse : file<".a:tarfile."> not readable")
68 return
69 endif
70 if &ma != 1
71 set ma
72 endif
73 let w:tarfile= a:tarfile
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000074
Bram Moolenaara5792f52005-11-23 21:25:05 +000075 setlocal noswapfile
76 setlocal buftype=nofile
77 setlocal bufhidden=hide
78 setlocal nobuflisted
79 setlocal nowrap
80 set ft=tar
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000081
Bram Moolenaara5792f52005-11-23 21:25:05 +000082 " give header
83 exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'"
84 exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'"
85 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
86 0d
87 $
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000088
Bram Moolenaara5792f52005-11-23 21:25:05 +000089 if a:tarfile =~# '\.\(gz\|tgz\)$'
90 exe "silent r! gzip -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
91 elseif a:tarfile =~# '\.bz2$'
92 exe "silent r! bzip2 -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - "
93 else
94 exe "silent r! tar -".g:tar_browseoptions." '".a:tarfile."'"
95 endif
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +000096 silent %g@/$@d
Bram Moolenaara5792f52005-11-23 21:25:05 +000097
98 setlocal noma nomod ro
99 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
100
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000101 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000102" call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
Bram Moolenaarab194812005-09-14 21:40:12 +0000103endfun
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000104
Bram Moolenaarab194812005-09-14 21:40:12 +0000105" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000106" TarBrowseSelect: {{{2
107fun! s:TarBrowseSelect()
108" call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000109 let repkeep= &report
110 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000111 let fname= getline(".")
112" call Decho("fname<".fname.">")
113
114 " sanity check
115 if fname =~ '^"'
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000116 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000117" call Dret("TarBrowseSelect")
118 return
119 endif
120
121 " about to make a new window, need to use w:tarfile
122 let tarfile= w:tarfile
123 let curfile= expand("%")
124
125 new
126 wincmd _
127 let s:tblfile_{winnr()}= curfile
128" call Decho("exe e tarfile:".tarfile.':'.fname)
129 exe "e tarfile:".tarfile.':'.fname
130 filetype detect
131
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000132 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000133" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
134endfun
135
136" ---------------------------------------------------------------------
137" tar#Read: {{{2
138fun! tar#Read(fname,mode)
139" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000140 let repkeep= &report
141 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000142 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
143 let fname = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
144" call Decho("tarfile<".tarfile."> fname<".fname.">")
145
146 if tarfile =~# '\.\(gz\|tgz\)$'
147" call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -OPxf - '".fname."'")
148 exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000149 elseif tarfile =~# '\.bz2$'
Bram Moolenaara5792f52005-11-23 21:25:05 +0000150" call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'")
151 exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'"
152 else
153" call Decho("exe silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'")
154 exe "silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'"
155 endif
156 let w:tarfile= a:fname
157 exe "file tarfile:".fname
158
159 " cleanup
160 0d
161 set nomod
162
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000163 let &report= repkeep
Bram Moolenaara5792f52005-11-23 21:25:05 +0000164" call Dret("tar#Read : w:tarfile<".w:tarfile.">")
165endfun
166
167" ---------------------------------------------------------------------
168" tar#Write: {{{2
169fun! tar#Write(fname)
170" call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000171 let repkeep= &report
172 set report=10
Bram Moolenaara5792f52005-11-23 21:25:05 +0000173
Bram Moolenaarab194812005-09-14 21:40:12 +0000174 " sanity checks
175 if !executable("tar")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000176 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system'
177 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000178 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000179" call Dret("tar#Write")
180 return
181 endif
182 if !exists("*mkdir")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000183 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
184 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000185 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000186" call Dret("tar#Write")
187 return
188 endif
189
190 let curdir= getcwd()
191 let tmpdir= tempname()
192" call Decho("orig tempname<".tmpdir.">")
193 if tmpdir =~ '\.'
194 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
195 endif
196" call Decho("tmpdir<".tmpdir.">")
197 call mkdir(tmpdir,"p")
198
199 " attempt to change to the indicated directory
200 try
201 exe "cd ".escape(tmpdir,' \')
202 catch /^Vim\%((\a\+)\)\=:E344/
Bram Moolenaara5792f52005-11-23 21:25:05 +0000203 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
204 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000205 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000206" call Dret("tar#Write")
207 return
208 endtry
209" call Decho("current directory now: ".getcwd())
210
Bram Moolenaara5792f52005-11-23 21:25:05 +0000211 " place temporary files under .../_ZIPVIM_/
212 if isdirectory("_ZIPVIM_")
213 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000214 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000215 call mkdir("_ZIPVIM_")
216 cd _ZIPVIM_
Bram Moolenaarab194812005-09-14 21:40:12 +0000217" call Decho("current directory now: ".getcwd())
218
Bram Moolenaara5792f52005-11-23 21:25:05 +0000219 let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
220 let fname = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
221
222 " handle compressed archives
223 if tarfile =~# '\.gz'
224 call system("gzip -d ".tarfile)
225 let tarfile = substitute(tarfile,'\.gz','','e')
226 let compress= "gzip '".tarfile."'"
227 elseif tarfile =~# '\.tgz'
228 call system("gzip -d ".tarfile)
229 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
230 let compress= "gzip '".tarfile."'"
231 let tgz = 1
232 elseif tarfile =~# '\.bz2'
233 call system("bzip2 -d ".tarfile)
234 let tarfile = substitute(tarfile,'\.bz2','','e')
235 let compress= "bzip2 '".tarfile."'"
Bram Moolenaarab194812005-09-14 21:40:12 +0000236 endif
237
Bram Moolenaarab194812005-09-14 21:40:12 +0000238 if v:shell_error != 0
Bram Moolenaara5792f52005-11-23 21:25:05 +0000239 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
240 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaarab194812005-09-14 21:40:12 +0000241 else
Bram Moolenaara5792f52005-11-23 21:25:05 +0000242
243" call Decho("tarfile<".tarfile."> fname<".fname.">")
244
Bram Moolenaar1cbe5f72005-12-29 22:51:09 +0000245 if fname =~ '/'
246 let dirpath = substitute(fname,'/[^/]\+$','','e')
247 if executable("cygpath")
248 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
249 endif
250 call mkdir(dirpath,"p")
251 endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000252 if tarfile !~ '/'
253 let tarfile= curdir.'/'.tarfile
254 endif
255" call Decho("tarfile<".tarfile."> fname<".fname.">")
256
Bram Moolenaara5792f52005-11-23 21:25:05 +0000257 exe "w! ".fname
258 if executable("cygpath")
Bram Moolenaara5792f52005-11-23 21:25:05 +0000259 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
260 endif
261
262 " delete old file from tarfile
263" call Decho("tar --delete -f '".tarfile."' '".fname."'")
264 call system("tar --delete -f '".tarfile."' '".fname."'")
265 if v:shell_error != 0
266 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
267 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
268 else
269
270 " update tarfile with new file
271" call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
272 call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
273 if v:shell_error != 0
274 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
275 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
276 elseif exists("compress")
277" call Decho("call system(".compress.")")
278 call system(compress)
279 if exists("tgz")
280" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
281 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
282 endif
283 endif
284 endif
285
286 " support writing tarfiles across a network
287 if s:tblfile_{winnr()} =~ '^\a\+://'
288" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
289 let tblfile= s:tblfile_{winnr()}
290 1split|enew
291 let binkeep= &binary
292 let eikeep = &ei
293 set binary ei=all
294 exe "e! ".tarfile
295 call netrw#NetWrite(tblfile)
296 let &ei = eikeep
297 let &binary = binkeep
298 q!
299 unlet s:tblfile_{winnr()}
300 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000301 endif
302
303 " cleanup and restore current directory
304 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000305 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000306 exe "cd ".escape(curdir,' \')
307 setlocal nomod
308
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000309 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000310" call Dret("tar#Write")
311endfun
312
313" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000314" Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000315fun! s:Rmdir(fname)
316" call Dfunc("Rmdir(fname<".a:fname.">)")
317 if has("unix")
318 call system("/bin/rm -rf ".a:fname)
319 elseif has("win32") || has("win95") || has("win64") || has("win16")
320 if &shell =~? "sh$"
321 call system("/bin/rm -rf ".a:fname)
322 else
323 call system("del /S ".a:fname)
324 endif
325 endif
326" call Dret("Rmdir")
327endfun
328
Bram Moolenaara5792f52005-11-23 21:25:05 +0000329" ------------------------------------------------------------------------
330" Modelines And Restoration: {{{1
331let &cpo= s:keepcpo
332unlet s:keepcpo
333" vim:ts=8 fdm=marker