2005-07-27 21:13:01 +00:00
|
|
|
"
|
|
|
|
" tar.vim -- a Vim plugin for browsing tarfiles
|
|
|
|
" Copyright (c) 2002, Michael C. Toren <mct@toren.net>
|
|
|
|
" Distributed under the GNU General Public License.
|
|
|
|
"
|
2005-09-14 21:40:12 +00:00
|
|
|
" Version: 2
|
|
|
|
" Date: Sep 14, 2005
|
|
|
|
" Modified By: Charles E. Campbell, Jr.
|
2005-07-27 21:13:01 +00:00
|
|
|
"
|
|
|
|
" Updates are available from <http://michael.toren.net/code/>. If you
|
|
|
|
" find this script useful, or have suggestions for improvements, please
|
|
|
|
" let me know.
|
|
|
|
" Also look there for further comments and documentation.
|
|
|
|
"
|
|
|
|
" This part defines the functions. The autocommands are in plugin/tar.vim.
|
2005-09-14 21:40:12 +00:00
|
|
|
if exists("g:loaded_tar") || &cp
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_tar= "v2"
|
|
|
|
|
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" tar#Read: {{{1
|
|
|
|
fun! tar#Read(argument, cleanup)
|
|
|
|
" call Dfunc("tar#Read(argument<".a:argument."> cleanup=".a:cleanup.")")
|
2005-07-27 21:13:01 +00:00
|
|
|
let l:argument = a:argument
|
|
|
|
let l:argument = substitute(l:argument, '^tarfile:', '', '')
|
|
|
|
let l:argument = substitute(l:argument, '^\~', $HOME, '')
|
|
|
|
|
|
|
|
let l:tarfile = l:argument
|
|
|
|
while 1
|
|
|
|
if (l:tarfile == "" || l:tarfile == "/")
|
2005-09-14 21:40:12 +00:00
|
|
|
echo "***error*** (tar#Read) Could not find a readable tarfile in path:" l:argument
|
|
|
|
" call Dret("tar#Read")
|
2005-07-27 21:13:01 +00:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if filereadable(l:tarfile) " found it!
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:tarfile = fnamemodify(l:tarfile, ":h")
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
let l:toextract = strpart(l:argument, strlen(l:tarfile) + 1)
|
|
|
|
|
|
|
|
if (l:toextract == "")
|
2005-09-14 21:40:12 +00:00
|
|
|
" call Dret("tar#Read")
|
2005-07-27 21:13:01 +00:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:cat = s:TarCatCommand(l:tarfile)
|
|
|
|
execute "r !" . l:cat . " < '" . l:tarfile . "'"
|
|
|
|
\ " | tar OPxf - '" . l:toextract . "'"
|
|
|
|
|
|
|
|
if (a:cleanup)
|
|
|
|
0d "blank line
|
|
|
|
execute "doautocmd BufReadPost " . expand("%")
|
2005-09-14 21:40:12 +00:00
|
|
|
setlocal nomod
|
2005-07-27 21:13:01 +00:00
|
|
|
silent preserve
|
|
|
|
endif
|
2005-09-14 21:40:12 +00:00
|
|
|
" call Dret("tar#Read")
|
|
|
|
endfun
|
2005-07-27 21:13:01 +00:00
|
|
|
|
2005-09-14 21:40:12 +00:00
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" tar#Write: {{{1
|
|
|
|
fun! tar#Write(argument)
|
|
|
|
" call Dfunc("tar#Write(argument<".a:argument.">)")
|
|
|
|
"
|
|
|
|
" sanity checks
|
|
|
|
if !executable("tar")
|
|
|
|
echo "***error*** (TarWrite) sorry, your system doesn't appear to have the tar pgm"
|
|
|
|
" call Dret("tar#Write")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
if !exists("*mkdir")
|
|
|
|
echo "***error*** (TarWrite) sorry, mkdir() doesn't work on your system"
|
|
|
|
" call Dret("tar#Write")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let curdir= getcwd()
|
|
|
|
let tmpdir= tempname()
|
|
|
|
" call Decho("orig tempname<".tmpdir.">")
|
|
|
|
if tmpdir =~ '\.'
|
|
|
|
let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
|
|
|
|
endif
|
|
|
|
" call Decho("tmpdir<".tmpdir.">")
|
|
|
|
call mkdir(tmpdir,"p")
|
|
|
|
|
|
|
|
" attempt to change to the indicated directory
|
|
|
|
try
|
|
|
|
exe "cd ".escape(tmpdir,' \')
|
|
|
|
catch /^Vim\%((\a\+)\)\=:E344/
|
|
|
|
echo "***error*** (TarWrite) cannot cd to temporary directory"
|
|
|
|
" call Dret("tar#Write")
|
|
|
|
return
|
|
|
|
endtry
|
|
|
|
" call Decho("current directory now: ".getcwd())
|
|
|
|
|
|
|
|
" place temporary files under .../_TARVIM_/
|
|
|
|
if isdirectory("_TARVIM_")
|
|
|
|
call s:Rmdir("_TARVIM_")
|
|
|
|
endif
|
|
|
|
call mkdir("_TARVIM_")
|
|
|
|
cd _TARVIM_
|
|
|
|
" call Decho("current directory now: ".getcwd())
|
|
|
|
|
|
|
|
let tarfile = curdir."/".substitute(a:argument,'tarfile:\([^/]\{-}\)/.*$','\1','')
|
|
|
|
let path = substitute(a:argument,'^.\{-}/','','')
|
|
|
|
let dirpath = substitute(path,'/\=[^/]\+$','','')
|
|
|
|
" call Decho("path <".path.">")
|
|
|
|
" call Decho("dirpath<".dirpath.">")
|
|
|
|
call mkdir(dirpath,"p")
|
|
|
|
exe "w! ".path
|
|
|
|
if executable("cygpath")
|
|
|
|
let path = substitute(system("cygpath ".path),'\n','','e')
|
|
|
|
let tarfile = substitute(system("cygpath ".tarfile),'\n','','e')
|
|
|
|
endif
|
|
|
|
|
|
|
|
" call Decho("tar --delete -f ".tarfile." ".path)
|
|
|
|
call system("tar --delete -f ".tarfile." ".path)
|
|
|
|
if v:shell_error != 0
|
|
|
|
echo "***error*** (TarWrite) sorry, your tar pgm doesn't support deletion of ".path
|
|
|
|
else
|
|
|
|
" call Decho("tar -rf ".tarfile." ".path)
|
|
|
|
call system("tar -rf ".tarfile." ".path)
|
|
|
|
endif
|
|
|
|
|
|
|
|
" cleanup and restore current directory
|
|
|
|
cd ..
|
|
|
|
call s:Rmdir("_TARVIM_")
|
|
|
|
exe "cd ".escape(curdir,' \')
|
|
|
|
setlocal nomod
|
|
|
|
|
|
|
|
" call Dret("tar#Write")
|
|
|
|
endfun
|
|
|
|
|
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" tar#Browse: {{{1
|
|
|
|
fun! tar#Browse(tarfile)
|
|
|
|
" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
|
2005-07-27 21:13:01 +00:00
|
|
|
setlocal noswapfile
|
|
|
|
setlocal buftype=nofile
|
|
|
|
setlocal bufhidden=hide
|
|
|
|
setlocal filetype=
|
|
|
|
setlocal nobuflisted
|
|
|
|
setlocal buftype=nofile
|
|
|
|
setlocal wrap
|
|
|
|
setlocal syntax=tar
|
|
|
|
|
|
|
|
let l:tarfile = a:tarfile
|
|
|
|
let b:tarfile = l:tarfile
|
|
|
|
let l:cat = s:TarCatCommand(l:tarfile)
|
|
|
|
|
|
|
|
if ! filereadable(l:tarfile)
|
|
|
|
let l:tarfile = substitute(l:tarfile, '^tarfile:', '', '')
|
|
|
|
endif
|
|
|
|
|
|
|
|
if ! filereadable(l:tarfile)
|
2005-09-14 21:40:12 +00:00
|
|
|
echo "***error*** (tar#Browse) File not readable:" l:tarfile
|
|
|
|
" call Dret("tar#Browse")
|
2005-07-27 21:13:01 +00:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2005-09-14 21:40:12 +00:00
|
|
|
call s:Say("\" tar.vim version " . g:loaded_tar)
|
2005-07-27 21:13:01 +00:00
|
|
|
call s:Say("\" Browsing tarfile " . l:tarfile)
|
|
|
|
call s:Say("\" Hit ENTER to view a file in a new window")
|
|
|
|
call s:Say("")
|
|
|
|
|
|
|
|
silent execute "r!" . l:cat . "<'" . l:tarfile . "'| tar Ptf - "
|
|
|
|
0d "blank line
|
|
|
|
/^$/1
|
|
|
|
|
2005-09-14 21:40:12 +00:00
|
|
|
setlocal noma nomod ro
|
|
|
|
|
2005-07-27 21:13:01 +00:00
|
|
|
noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
|
2005-09-14 21:40:12 +00:00
|
|
|
" call Dret("tar#Browse")
|
|
|
|
endfun
|
2005-07-27 21:13:01 +00:00
|
|
|
|
2005-09-14 21:40:12 +00:00
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" TarBrowseSelect: {{{1
|
|
|
|
fun! s:TarBrowseSelect()
|
2005-07-27 21:13:01 +00:00
|
|
|
let l:line = getline(".")
|
|
|
|
|
|
|
|
if (l:line =~ '^" ')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if (l:line =~ '/$')
|
|
|
|
echo "Please specify a file, not a directory"
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:selection = "tarfile:" . b:tarfile . "/" . l:line
|
|
|
|
new
|
|
|
|
wincmd _
|
|
|
|
execute "e " . l:selection
|
2005-09-14 21:40:12 +00:00
|
|
|
endfun
|
2005-07-27 21:13:01 +00:00
|
|
|
|
2005-09-14 21:40:12 +00:00
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" TarCatCommand: kludge to deal with compressed archives {{{1
|
|
|
|
fun! s:TarCatCommand(tarfile)
|
|
|
|
" call Dfunc("s:TarCatCommand(tarfile<".a:tarfile.">)")
|
2005-07-27 21:13:01 +00:00
|
|
|
if a:tarfile =~# '\.\(gz\|tgz\|Z\)$'
|
|
|
|
let l:cat = "gzip -d -c"
|
|
|
|
elseif a:tarfile =~# '\.bz2$'
|
|
|
|
let l:cat = "bzip2 -d -c"
|
|
|
|
else
|
|
|
|
let l:cat = "cat"
|
|
|
|
endif
|
2005-09-14 21:40:12 +00:00
|
|
|
" call Dret("s:TarCatCommand ".l:cat)
|
2005-07-27 21:13:01 +00:00
|
|
|
return l:cat
|
2005-09-14 21:40:12 +00:00
|
|
|
endfun
|
2005-07-27 21:13:01 +00:00
|
|
|
|
2005-09-14 21:40:12 +00:00
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" Say: {{{1
|
|
|
|
fun! s:Say(string)
|
2005-07-27 21:13:01 +00:00
|
|
|
let @" = a:string
|
|
|
|
$ put
|
2005-09-14 21:40:12 +00:00
|
|
|
endfun
|
|
|
|
|
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" Rmdir: {{{1
|
|
|
|
fun! s:Rmdir(fname)
|
|
|
|
" call Dfunc("Rmdir(fname<".a:fname.">)")
|
|
|
|
if has("unix")
|
|
|
|
call system("/bin/rm -rf ".a:fname)
|
|
|
|
elseif has("win32") || has("win95") || has("win64") || has("win16")
|
|
|
|
if &shell =~? "sh$"
|
|
|
|
call system("/bin/rm -rf ".a:fname)
|
|
|
|
else
|
|
|
|
call system("del /S ".a:fname)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
" call Dret("Rmdir")
|
|
|
|
endfun
|
|
|
|
|
|
|
|
" ---------------------------------------------------------------------
|
|
|
|
" Modelines: {{{1
|
|
|
|
" vim:set ts=8 sts=4 sw=4 fdm=marker:
|