diff options
Diffstat (limited to 'runtime/autoload/tar.vim')
-rw-r--r-- | runtime/autoload/tar.vim | 409 |
1 files changed, 240 insertions, 169 deletions
diff --git a/runtime/autoload/tar.vim b/runtime/autoload/tar.vim index 0234059b0..66d2eb5ce 100644 --- a/runtime/autoload/tar.vim +++ b/runtime/autoload/tar.vim @@ -1,79 +1,172 @@ +" tar.vim: Handles browsing tarfiles +" AUTOLOAD PORTION +" Date: Nov 18, 2005 +" Version: 4 +" Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz> +" License: Vim License (see vim's :help license) " -" tar.vim -- a Vim plugin for browsing tarfiles -" Copyright (c) 2002, Michael C. Toren <mct@toren.net> -" Distributed under the GNU General Public License. +" Contains many ideas from Michael Toren's <tar.vim> " -" Version: 2 -" Date: Sep 14, 2005 -" Modified By: Charles E. Campbell, Jr. -" -" 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. -if exists("g:loaded_tar") || &cp +" Copyright: Copyright (C) 2005 Charles E. Campbell, Jr. {{{1 +" Permission is hereby granted to use and distribute this code, +" with or without modifications, provided that this copyright +" notice is copied with it. Like anything else that's free, +" tarPlugin.vim is provided *as is* and comes with no warranty +" of any kind, either expressed or implied. By using this +" plugin, you agree that in no event will the copyright +" holder be liable for any damages resulting from the use +" of this software. + +" --------------------------------------------------------------------- +" Initialization: {{{1 +let s:keepcpo= &cpo +set cpo&vim +if exists("g:loaded_tar") finish endif -let g:loaded_tar= "v2" +let g:loaded_tar= "v4" " --------------------------------------------------------------------- -" tar#Read: {{{1 -fun! tar#Read(argument, cleanup) -" call Dfunc("tar#Read(argument<".a:argument."> cleanup=".a:cleanup.")") - 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 == "/") - echo "***error*** (tar#Read) Could not find a readable tarfile in path:" l:argument -" call Dret("tar#Read") - 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 == "") -" call Dret("tar#Read") - return - endif +" Default Settings: {{{1 +if !exists("g:tar_browseoptions") + let g:tar_browseoptions= "Ptf" +endif +if !exists("g:tar_readoptions") + let g:tar_readoptions= "OPxf" +endif +if !exists("g:tar_writeoptions") + let g:tar_writeoptions= "uf" +endif - let l:cat = s:TarCatCommand(l:tarfile) - execute "r !" . l:cat . " < '" . l:tarfile . "'" - \ " | tar OPxf - '" . l:toextract . "'" +" ---------------- +" Functions: {{{1 +" ---------------- - if (a:cleanup) - 0d "blank line - execute "doautocmd BufReadPost " . expand("%") - setlocal nomod - silent preserve - endif -" call Dret("tar#Read") +" --------------------------------------------------------------------- +" tar#Browse: {{{2 +fun! tar#Browse(tarfile) +" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)") + + " sanity checks + if !executable("tar") + echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system' + call inputsave()|call input("Press <cr> to continue")|call inputrestore() +" call Dret("tar#Browse") + return + endif + if !filereadable(a:tarfile) + if a:tarfile !~# '^\a\+://' + " if its an url, don't complain, let url-handlers such as vim do its thing + echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None + call inputsave()|call input("Press <cr> to continue")|call inputrestore() + endif +" call Dret("tar#Browse : file<".a:tarfile."> not readable") + return + endif + if &ma != 1 + set ma + endif + let w:tarfile= a:tarfile + + setlocal noswapfile + setlocal buftype=nofile + setlocal bufhidden=hide + setlocal nobuflisted + setlocal nowrap + set ft=tar + + " give header + exe "$put ='".'\"'." tar.vim version ".g:loaded_tar."'" + exe "$put ='".'\"'." Browsing tarfile ".a:tarfile."'" + exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'" + 0d + $ + + if a:tarfile =~# '\.\(gz\|tgz\)$' + exe "silent r! gzip -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - " + elseif a:tarfile =~# '\.bz2$' + exe "silent r! bzip2 -d -c '".a:tarfile."'| tar -".g:tar_browseoptions." - " + else + exe "silent r! tar -".g:tar_browseoptions." '".a:tarfile."'" + endif + %g@/$@d + + setlocal noma nomod ro + noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr> + +" call Dret("tar#Browse : w:tarfile<".w:tarfile.">") endfun " --------------------------------------------------------------------- -" tar#Write: {{{1 -fun! tar#Write(argument) -" call Dfunc("tar#Write(argument<".a:argument.">)") -" +" TarBrowseSelect: {{{2 +fun! s:TarBrowseSelect() +" call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">") + let fname= getline(".") +" call Decho("fname<".fname.">") + + " sanity check + if fname =~ '^"' +" call Dret("TarBrowseSelect") + return + endif + + " about to make a new window, need to use w:tarfile + let tarfile= w:tarfile + let curfile= expand("%") + + new + wincmd _ + let s:tblfile_{winnr()}= curfile +" call Decho("exe e tarfile:".tarfile.':'.fname) + exe "e tarfile:".tarfile.':'.fname + filetype detect + +" call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">") +endfun + +" --------------------------------------------------------------------- +" tar#Read: {{{2 +fun! tar#Read(fname,mode) +" call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")") + let tarfile = substitute(a:fname,'tarfile:\(.\{-}\):.*$','\1','') + let fname = substitute(a:fname,'tarfile:.\{-}:\(.*\)$','\1','') +" call Decho("tarfile<".tarfile."> fname<".fname.">") + + if tarfile =~# '\.\(gz\|tgz\)$' +" call Decho("exe silent r! gzip -d -c '".tarfile."'| tar -OPxf - '".fname."'") + exe "silent r! gzip -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'" + elseif a:fname =~# '\.bz2$' +" call Decho("exe silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'") + exe "silent r! bzip2 -d -c '".tarfile."'| tar -".g:tar_readoptions." - '".fname."'" + else +" call Decho("exe silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'") + exe "silent r! tar -".g:tar_readoptions." '".tarfile."' '".fname."'" + endif + let w:tarfile= a:fname + exe "file tarfile:".fname + + " cleanup + 0d + set nomod + +" call Dret("tar#Read : w:tarfile<".w:tarfile.">") +endfun + +" --------------------------------------------------------------------- +" tar#Write: {{{2 +fun! tar#Write(fname) +" call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">") + " sanity checks if !executable("tar") - echo "***error*** (TarWrite) sorry, your system doesn't appear to have the tar pgm" + echohl Error | echo '***error*** (tar#Browse) "tar" not available on your system' + call inputsave()|call input("Press <cr> to continue")|call inputrestore() " call Dret("tar#Write") return endif if !exists("*mkdir") - echo "***error*** (TarWrite) sorry, mkdir() doesn't work on your system" + echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None + call inputsave()|call input("Press <cr> to continue")|call inputrestore() " call Dret("tar#Write") return endif @@ -91,44 +184,104 @@ fun! tar#Write(argument) try exe "cd ".escape(tmpdir,' \') catch /^Vim\%((\a\+)\)\=:E344/ - echo "***error*** (TarWrite) cannot cd to temporary directory" + echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None + call inputsave()|call input("Press <cr> to continue")|call inputrestore() " call Dret("tar#Write") return endtry " call Decho("current directory now: ".getcwd()) - " place temporary files under .../_TARVIM_/ - if isdirectory("_TARVIM_") - call s:Rmdir("_TARVIM_") + " place temporary files under .../_ZIPVIM_/ + if isdirectory("_ZIPVIM_") + call s:Rmdir("_ZIPVIM_") endif - call mkdir("_TARVIM_") - cd _TARVIM_ + call mkdir("_ZIPVIM_") + cd _ZIPVIM_ " 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') + let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\):.*$','\1','') + let fname = substitute(w:tarfile,'tarfile:.\{-}:\(.*\)$','\1','') + + " handle compressed archives + if tarfile =~# '\.gz' + call system("gzip -d ".tarfile) + let tarfile = substitute(tarfile,'\.gz','','e') + let compress= "gzip '".tarfile."'" + elseif tarfile =~# '\.tgz' + call system("gzip -d ".tarfile) + let tarfile = substitute(tarfile,'\.tgz','.tar','e') + let compress= "gzip '".tarfile."'" + let tgz = 1 + elseif tarfile =~# '\.bz2' + call system("bzip2 -d ".tarfile) + let tarfile = substitute(tarfile,'\.bz2','','e') + let compress= "bzip2 '".tarfile."'" 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 + echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None + call inputsave()|call input("Press <cr> to continue")|call inputrestore() else -" call Decho("tar -rf ".tarfile." ".path) - call system("tar -rf ".tarfile." ".path) + +" call Decho("tarfile<".tarfile."> fname<".fname.">") + + let dirpath = substitute(fname,'/[^/]\+$','','e') + if tarfile !~ '/' + let tarfile= curdir.'/'.tarfile + endif +" call Decho("tarfile<".tarfile."> fname<".fname.">") + + call mkdir(dirpath,"p") + exe "w! ".fname + if executable("cygpath") + let dirpath = substitute(system("cygpath ".dirpath),'\n','','e') + let tarfile = substitute(system("cygpath ".tarfile),'\n','','e') + endif + + " delete old file from tarfile +" call Decho("tar --delete -f '".tarfile."' '".fname."'") + call system("tar --delete -f '".tarfile."' '".fname."'") + if v:shell_error != 0 + echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None + call inputsave()|call input("Press <cr> to continue")|call inputrestore() + else + + " update tarfile with new file +" call Decho("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'") + call system("tar -".g:tar_writeoptions." '".tarfile."' '".fname."'") + if v:shell_error != 0 + echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None + call inputsave()|call input("Press <cr> to continue")|call inputrestore() + elseif exists("compress") +" call Decho("call system(".compress.")") + call system(compress) + if exists("tgz") +" call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")") + call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e')) + endif + endif + endif + + " support writing tarfiles across a network + if s:tblfile_{winnr()} =~ '^\a\+://' +" call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">") + let tblfile= s:tblfile_{winnr()} + 1split|enew + let binkeep= &binary + let eikeep = &ei + set binary ei=all + exe "e! ".tarfile + call netrw#NetWrite(tblfile) + let &ei = eikeep + let &binary = binkeep + q! + unlet s:tblfile_{winnr()} + endif endif " cleanup and restore current directory cd .. - call s:Rmdir("_TARVIM_") + call s:Rmdir("_ZIPVIM_") exe "cd ".escape(curdir,' \') setlocal nomod @@ -136,91 +289,7 @@ fun! tar#Write(argument) endfun " --------------------------------------------------------------------- -" tar#Browse: {{{1 -fun! tar#Browse(tarfile) -" call Dfunc("tar#Browse(tarfile<".a:tarfile.">)") - 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) - echo "***error*** (tar#Browse) File not readable:" l:tarfile -" call Dret("tar#Browse") - return - endif - - call s:Say("\" tar.vim version " . g:loaded_tar) - 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 - - setlocal noma nomod ro - - noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr> -" call Dret("tar#Browse") -endfun - -" --------------------------------------------------------------------- -" TarBrowseSelect: {{{1 -fun! s:TarBrowseSelect() - 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 -endfun - -" --------------------------------------------------------------------- -" TarCatCommand: kludge to deal with compressed archives {{{1 -fun! s:TarCatCommand(tarfile) -" call Dfunc("s:TarCatCommand(tarfile<".a:tarfile.">)") - 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 -" call Dret("s:TarCatCommand ".l:cat) - return l:cat -endfun - -" --------------------------------------------------------------------- -" Say: {{{1 -fun! s:Say(string) - let @" = a:string - $ put -endfun - -" --------------------------------------------------------------------- -" Rmdir: {{{1 +" Rmdir: {{{2 fun! s:Rmdir(fname) " call Dfunc("Rmdir(fname<".a:fname.">)") if has("unix") @@ -235,6 +304,8 @@ fun! s:Rmdir(fname) " call Dret("Rmdir") endfun -" --------------------------------------------------------------------- -" Modelines: {{{1 -" vim:set ts=8 sts=4 sw=4 fdm=marker: +" ------------------------------------------------------------------------ +" Modelines And Restoration: {{{1 +let &cpo= s:keepcpo +unlet s:keepcpo +" vim:ts=8 fdm=marker |