1 " tar.vim: Handles browsing tarfiles
5 " Maintainer: Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
6 " License: Vim License (see vim's :help license)
8 " Contains many ideas from Michael Toren's <tar.vim>
10 " Copyright: Copyright (C) 2005-2008 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 " 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.
20 " ---------------------------------------------------------------------
24 if &cp || exists("g:loaded_tar") || v:version < 700
27 let g:loaded_tar= "v13"
28 "call Decho("loading autoload/tar.vim")
30 " ---------------------------------------------------------------------
31 " Default Settings: {{{1
32 if !exists("g:tar_browseoptions")
33 let g:tar_browseoptions= "Ptf"
35 if !exists("g:tar_readoptions")
36 let g:tar_readoptions= "OPxf"
38 if !exists("g:tar_cmd")
41 if !exists("g:tar_writeoptions")
42 let g:tar_writeoptions= "uf"
44 if !exists("g:tar_shq")
58 " ---------------------------------------------------------------------
60 fun! tar#Browse(tarfile)
61 " call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
66 if !executable(g:tar_cmd)
68 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
69 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
71 " call Dret("tar#Browse")
74 if !filereadable(a:tarfile)
75 " call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
76 if a:tarfile !~# '^\a\+://'
77 " if its an url, don't complain, let url-handlers such as vim do its thing
79 echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
80 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
83 " call Dret("tar#Browse : file<".a:tarfile."> not readable")
89 let w:tarfile= a:tarfile
92 setlocal buftype=nofile
93 setlocal bufhidden=hide
99 " call Decho("printing header")
100 exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'"
101 exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'"
102 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
106 let tarfile= a:tarfile
107 if has("win32") && executable("cygpath")
109 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
111 let curlast= line("$")
112 if tarfile =~# '\.\(gz\|tgz\)$'
113 " call Decho("exe silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
114 exe "silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - "
115 elseif tarfile =~# '\.lrp'
116 exe "silent r! cat ".g:tar_shq.tarfile.g:tar_shq."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
117 elseif tarfile =~# '\.bz2$'
118 " call Decho("exe silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
119 exe "silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq." | ".g:tar_cmd." -".g:tar_browseoptions." - "
121 " call Decho("exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".g:tar_shq.tarfile.g:tar_shq)
122 exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".g:tar_shq.tarfile.g:tar_shq
124 if v:shell_error != 0
126 echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
127 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
128 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
131 if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
133 echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
134 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
137 set ei=BufReadCmd,FileReadCmd
141 " call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
145 setlocal noma nomod ro
146 noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
149 " call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
152 " ---------------------------------------------------------------------
153 " TarBrowseSelect: {{{2
154 fun! s:TarBrowseSelect()
155 " call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
158 let fname= getline(".")
159 " call Decho("fname<".fname.">")
164 " call Dret("TarBrowseSelect")
168 " about to make a new window, need to use w:tarfile
169 let tarfile= w:tarfile
170 let curfile= expand("%")
171 if has("win32") && executable("cygpath")
173 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
177 if !exists("g:tar_nomax") || g:tar_nomax == 0
180 let s:tblfile_{winnr()}= curfile
181 call tar#Read("tarfile:".tarfile.':'.fname,1)
185 " call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
188 " ---------------------------------------------------------------------
190 fun! tar#Read(fname,mode)
191 " call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
194 let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','')
195 let fname = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','')
196 if has("win32") && executable("cygpath")
198 let tarfile=substitute(system("cygpath -u ".tarfile),'\n$','','e')
200 " call Decho("tarfile<".tarfile.">")
201 " call Decho("fname<".fname.">")
203 if tarfile =~# '\.\(gz\|tgz\)$'
204 " call Decho("exe silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq)
205 exe "silent r! gzip -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq
206 elseif tarfile =~# '\.lrp$'
207 " call Decho("exe silent r! cat ".g:tar_shq.tarfile.g:tar_shq." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq)
208 exe "silent r! cat ".g:tar_shq.tarfile.g:tar_shq." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq
209 elseif tarfile =~# '\.bz2$'
210 " call Decho("exe silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq)
211 exe "silent r! bzip2 -d -c ".g:tar_shq.tarfile.g:tar_shq."| ".g:tar_cmd." -".g:tar_readoptions." - ".g:tar_shq.fname.g:tar_shq
213 " call Decho("exe silent r! ".g:tar_cmd." -".g:tar_readoptions." ".g:tar_shq.tarfile.g:tar_shq." ".g:tar_shq.fname.g:tar_shq)
214 exe "silent r! ".g:tar_cmd." -".g:tar_readoptions." ".g:tar_shq.tarfile.g:tar_shq." ".g:tar_shq.fname.g:tar_shq
216 let w:tarfile= a:fname
217 exe "file tarfile:".fname
224 " call Dret("tar#Read : w:tarfile<".w:tarfile.">")
227 " ---------------------------------------------------------------------
229 fun! tar#Write(fname)
230 " call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
235 if !executable(g:tar_cmd)
237 echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
238 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
240 " call Dret("tar#Write")
245 echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
246 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
248 " call Dret("tar#Write")
253 let tmpdir= tempname()
254 " call Decho("orig tempname<".tmpdir.">")
256 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
258 " call Decho("tmpdir<".tmpdir.">")
259 call mkdir(tmpdir,"p")
261 " attempt to change to the indicated directory
263 exe "cd ".escape(tmpdir,' \')
264 catch /^Vim\%((\a\+)\)\=:E344/
266 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
267 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
269 " call Dret("tar#Write")
272 " call Decho("current directory now: ".getcwd())
274 " place temporary files under .../_ZIPVIM_/
275 if isdirectory("_ZIPVIM_")
276 call s:Rmdir("_ZIPVIM_")
278 call mkdir("_ZIPVIM_")
280 " call Decho("current directory now: ".getcwd())
282 let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','')
283 let fname = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','')
285 " handle compressed archives
286 if tarfile =~# '\.gz'
287 call system("gzip -d ".tarfile)
288 let tarfile = substitute(tarfile,'\.gz','','e')
289 let compress= "gzip '".tarfile."'"
290 elseif tarfile =~# '\.tgz'
291 call system("gzip -d ".tarfile)
292 let tarfile = substitute(tarfile,'\.tgz','.tar','e')
293 let compress= "gzip '".tarfile."'"
295 elseif tarfile =~# '\.bz2'
296 call system("bzip2 -d ".tarfile)
297 let tarfile = substitute(tarfile,'\.bz2','','e')
298 let compress= "bzip2 '".tarfile."'"
301 if v:shell_error != 0
303 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
304 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
307 " call Decho("tarfile<".tarfile."> fname<".fname.">")
310 let dirpath = substitute(fname,'/[^/]\+$','','e')
311 if executable("cygpath")
312 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
314 call mkdir(dirpath,"p")
317 let tarfile= curdir.'/'.tarfile
319 " call Decho("tarfile<".tarfile."> fname<".fname.">")
322 if executable("cygpath")
323 let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
326 " delete old file from tarfile
327 " call Decho("tar --delete -f '".tarfile."' '".fname."'")
328 call system("tar --delete -f '".tarfile."' '".fname."'")
329 if v:shell_error != 0
331 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
332 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
335 " update tarfile with new file
336 " call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
337 call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'")
338 if v:shell_error != 0
340 echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
341 " call inputsave()|call input("Press <cr> to continue")|call inputrestore()
342 elseif exists("compress")
343 " call Decho("call system(".compress.")")
344 call system(compress)
346 " call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
347 call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
352 " support writing tarfiles across a network
353 if s:tblfile_{winnr()} =~ '^\a\+://'
354 " call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
355 let tblfile= s:tblfile_{winnr()}
361 call netrw#NetWrite(tblfile)
363 let &binary = binkeep
365 unlet s:tblfile_{winnr()}
369 " cleanup and restore current directory
371 call s:Rmdir("_ZIPVIM_")
372 exe "cd ".escape(curdir,' \')
376 " call Dret("tar#Write")
379 " ---------------------------------------------------------------------
382 " call Dfunc("Rmdir(fname<".a:fname.">)")
384 call system("/bin/rm -rf ".a:fname)
385 elseif has("win32") || has("win95") || has("win64") || has("win16")
387 call system("/bin/rm -rf ".a:fname)
389 call system("del /S ".a:fname)
395 " ------------------------------------------------------------------------
396 " Modelines And Restoration: {{{1
399 " vim:ts=8 fdm=marker