blob: 9d8f138ac03aef419b1c277a9da7a82f9c48648e [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001" tar.vim: Handles browsing tarfiles
2" AUTOLOAD PORTION
Bram Moolenaarbba577a2005-11-28 23:05:55 +00003" Date: Nov 28, 2005
4" Version: 5
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 Moolenaarbba577a2005-11-28 23:05:55 +000027let g:loaded_tar= "v5"
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
96 %g@/$@d
97
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."'"
149 elseif a:fname =~# '\.bz2$'
150" 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
245 let dirpath = substitute(fname,'/[^/]\+$','','e')
246 if tarfile !~ '/'
247 let tarfile= curdir.'/'.tarfile
248 endif
249" call Decho("tarfile<".tarfile."> fname<".fname.">")
250
251 call mkdir(dirpath,"p")
252 exe "w! ".fname
253 if executable("cygpath")
254 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
255 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
256 endif
257
258 " delete old file from tarfile
259" call Decho("tar --delete -f '".tarfile."' '".fname."'")
260 call system("tar --delete -f '".tarfile."' '".fname."'")
261 if v:shell_error != 0
262 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
263 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
264 else
265
266 " update tarfile with new file
267" call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
268 call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
269 if v:shell_error != 0
270 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
271 call inputsave()|call input("Press <cr> to continue")|call inputrestore()
272 elseif exists("compress")
273" call Decho("call system(".compress.")")
274 call system(compress)
275 if exists("tgz")
276" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
277 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
278 endif
279 endif
280 endif
281
282 " support writing tarfiles across a network
283 if s:tblfile_{winnr()} =~ '^\a\+://'
284" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
285 let tblfile= s:tblfile_{winnr()}
286 1split|enew
287 let binkeep= &binary
288 let eikeep = &ei
289 set binary ei=all
290 exe "e! ".tarfile
291 call netrw#NetWrite(tblfile)
292 let &ei = eikeep
293 let &binary = binkeep
294 q!
295 unlet s:tblfile_{winnr()}
296 endif
Bram Moolenaarab194812005-09-14 21:40:12 +0000297 endif
298
299 " cleanup and restore current directory
300 cd ..
Bram Moolenaara5792f52005-11-23 21:25:05 +0000301 call s:Rmdir("_ZIPVIM_")
Bram Moolenaarab194812005-09-14 21:40:12 +0000302 exe "cd ".escape(curdir,' \')
303 setlocal nomod
304
Bram Moolenaarbba577a2005-11-28 23:05:55 +0000305 let &report= repkeep
Bram Moolenaarab194812005-09-14 21:40:12 +0000306" call Dret("tar#Write")
307endfun
308
309" ---------------------------------------------------------------------
Bram Moolenaara5792f52005-11-23 21:25:05 +0000310" Rmdir: {{{2
Bram Moolenaarab194812005-09-14 21:40:12 +0000311fun! s:Rmdir(fname)
312" call Dfunc("Rmdir(fname<".a:fname.">)")
313 if has("unix")
314 call system("/bin/rm -rf ".a:fname)
315 elseif has("win32") || has("win95") || has("win64") || has("win16")
316 if &shell =~? "sh$"
317 call system("/bin/rm -rf ".a:fname)
318 else
319 call system("del /S ".a:fname)
320 endif
321 endif
322" call Dret("Rmdir")
323endfun
324
Bram Moolenaara5792f52005-11-23 21:25:05 +0000325" ------------------------------------------------------------------------
326" Modelines And Restoration: {{{1
327let &cpo= s:keepcpo
328unlet s:keepcpo
329" vim:ts=8 fdm=marker