1" tar.vim: Handles browsing tarfiles 2" AUTOLOAD PORTION 3" Date: Nov 28, 2005 4" Version: 5 5" Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz> 6" License: Vim License (see vim's :help license) 7" 8" Contains many ideas from Michael Toren's <tar.vim> 9" 10" 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. 19 20" --------------------------------------------------------------------- 21" Initialization: {{{1 22let s:keepcpo= &cpo 23set cpo&vim 24if exists("g:loaded_tar") 25 finish 26endif 27let g:loaded_tar= "v5" 28 29" --------------------------------------------------------------------- 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 40 41" ---------------- 42" Functions: {{{1 43" ---------------- 44 45" --------------------------------------------------------------------- 46" tar#Browse: {{{2 47fun! tar#Browse(tarfile) 48" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)") 49 let repkeep= &report 50 set report=10 51 52 " 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() 56 let &report= repkeep 57" 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 66 let &report= repkeep 67" 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 74 75 setlocal noswapfile 76 setlocal buftype=nofile 77 setlocal bufhidden=hide 78 setlocal nobuflisted 79 setlocal nowrap 80 set ft=tar 81 82 " 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 $ 88 89 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 101 let &report= repkeep 102" call Dret("tar#Browse : w:tarfile<".w:tarfile.">") 103endfun 104 105" --------------------------------------------------------------------- 106" TarBrowseSelect: {{{2 107fun! s:TarBrowseSelect() 108" call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">") 109 let repkeep= &report 110 set report=10 111 let fname= getline(".") 112" call Decho("fname<".fname.">") 113 114 " sanity check 115 if fname =~ '^"' 116 let &report= repkeep 117" 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 132 let &report= repkeep 133" 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.")") 140 let repkeep= &report 141 set report=10 142 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 163 let &report= repkeep 164" 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()}.">") 171 let repkeep= &report 172 set report=10 173 174 " sanity checks 175 if !executable("tar") 176 echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system' 177 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 178 let &report= repkeep 179" call Dret("tar#Write") 180 return 181 endif 182 if !exists("*mkdir") 183 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() 185 let &report= repkeep 186" 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/ 203 echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None 204 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 205 let &report= repkeep 206" call Dret("tar#Write") 207 return 208 endtry 209" call Decho("current directory now: ".getcwd()) 210 211 " place temporary files under .../_ZIPVIM_/ 212 if isdirectory("_ZIPVIM_") 213 call s:Rmdir("_ZIPVIM_") 214 endif 215 call mkdir("_ZIPVIM_") 216 cd _ZIPVIM_ 217" call Decho("current directory now: ".getcwd()) 218 219 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."'" 236 endif 237 238 if v:shell_error != 0 239 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() 241 else 242 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 297 endif 298 299 " cleanup and restore current directory 300 cd .. 301 call s:Rmdir("_ZIPVIM_") 302 exe "cd ".escape(curdir,' \') 303 setlocal nomod 304 305 let &report= repkeep 306" call Dret("tar#Write") 307endfun 308 309" --------------------------------------------------------------------- 310" Rmdir: {{{2 311fun! 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 325" ------------------------------------------------------------------------ 326" Modelines And Restoration: {{{1 327let &cpo= s:keepcpo 328unlet s:keepcpo 329" vim:ts=8 fdm=marker 330