Karsten Hopp cf9f5a
" tar.vim: Handles browsing tarfiles
Karsten Hopp cf9f5a
"            AUTOLOAD PORTION
Karsten Hopp cf9f5a
" Date:			Aug 08, 2008
Karsten Hopp cf9f5a
" Version:		23 + modifications by Bram
Karsten Hopp cf9f5a
" Maintainer:	Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
Karsten Hopp cf9f5a
" License:		Vim License  (see vim's :help license)
Karsten Hopp cf9f5a
"
Karsten Hopp cf9f5a
"	Contains many ideas from Michael Toren's <tar.vim>
Karsten Hopp cf9f5a
"
Karsten Hopp cf9f5a
" Copyright:    Copyright (C) 2005-2008 Charles E. Campbell, Jr. {{{1
Karsten Hopp cf9f5a
"               Permission is hereby granted to use and distribute this code,
Karsten Hopp cf9f5a
"               with or without modifications, provided that this copyright
Karsten Hopp cf9f5a
"               notice is copied with it. Like anything else that's free,
Karsten Hopp cf9f5a
"               tar.vim and tarPlugin.vim are provided *as is* and comes
Karsten Hopp cf9f5a
"               with no warranty of any kind, either expressed or implied.
Karsten Hopp cf9f5a
"               By using this plugin, you agree that in no event will the
Karsten Hopp cf9f5a
"               copyright holder be liable for any damages resulting from
Karsten Hopp cf9f5a
"               the use of this software.
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" Load Once: {{{1
Karsten Hopp cf9f5a
let s:keepcpo= &cpo
Karsten Hopp cf9f5a
set cpo&vim
Karsten Hopp cf9f5a
if &cp || exists("g:loaded_tar") || v:version < 700
Karsten Hopp cf9f5a
 finish
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
let g:loaded_tar= "v23b"
Karsten Hopp cf9f5a
"call Decho("loading autoload/tar.vim")
Karsten Hopp cf9f5a
if v:version < 701 || (v:version == 701 && !has("patch299"))
Karsten Hopp cf9f5a
 echoerr "(autoload/tar.vim) need vim v7.1 with patchlevel 299"
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
"  Default Settings: {{{1
Karsten Hopp cf9f5a
if !exists("g:tar_browseoptions")
Karsten Hopp cf9f5a
 let g:tar_browseoptions= "Ptf"
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
if !exists("g:tar_readoptions")
Karsten Hopp cf9f5a
 let g:tar_readoptions= "OPxf"
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
if !exists("g:tar_cmd")
Karsten Hopp cf9f5a
 let g:tar_cmd= "tar"
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
if !exists("g:tar_writeoptions")
Karsten Hopp cf9f5a
 let g:tar_writeoptions= "uf"
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
if !exists("g:netrw_cygwin")
Karsten Hopp cf9f5a
 if has("win32") || has("win95") || has("win64") || has("win16")
Karsten Hopp cf9f5a
  if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
Karsten Hopp cf9f5a
   let g:netrw_cygwin= 1
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
   let g:netrw_cygwin= 0
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
 else
Karsten Hopp cf9f5a
  let g:netrw_cygwin= 0
Karsten Hopp cf9f5a
 endif
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" set up shell quoting character
Karsten Hopp cf9f5a
if !exists("g:tar_shq")
Karsten Hopp cf9f5a
 if exists("&shq") && &shq != ""
Karsten Hopp cf9f5a
  let g:tar_shq= &shq
Karsten Hopp cf9f5a
 elseif has("win32") || has("win95") || has("win64") || has("win16")
Karsten Hopp cf9f5a
  if exists("g:netrw_cygwin") && g:netrw_cygwin
Karsten Hopp cf9f5a
   let g:tar_shq= "'"
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
   let g:tar_shq= '"'
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
 else
Karsten Hopp cf9f5a
  let g:tar_shq= "'"
Karsten Hopp cf9f5a
 endif
Karsten Hopp cf9f5a
" call Decho("g:tar_shq<".g:tar_shq.">")
Karsten Hopp cf9f5a
endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ----------------
Karsten Hopp cf9f5a
"  Functions: {{{1
Karsten Hopp cf9f5a
" ----------------
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" tar#Browse: {{{2
Karsten Hopp cf9f5a
fun! tar#Browse(tarfile)
Karsten Hopp cf9f5a
"  call Dfunc("tar#Browse(tarfile<".a:tarfile.">)")
Karsten Hopp cf9f5a
  let repkeep= &report
Karsten Hopp cf9f5a
  set report=10
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " sanity checks
Karsten Hopp cf9f5a
  if !executable(g:tar_cmd)
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Karsten Hopp cf9f5a
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
   let &report= repkeep
Karsten Hopp cf9f5a
"   call Dret("tar#Browse")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  if !filereadable(a:tarfile)
Karsten Hopp cf9f5a
"   call Decho('a:tarfile<'.a:tarfile.'> not filereadable')
Karsten Hopp cf9f5a
   if a:tarfile !~# '^\a\+://'
Karsten Hopp cf9f5a
    " if its an url, don't complain, let url-handlers such as vim do its thing
Karsten Hopp cf9f5a
    redraw!
Karsten Hopp cf9f5a
    echohl Error | echo "***error*** (tar#Browse) File not readable<".a:tarfile.">" | echohl None
Karsten Hopp cf9f5a
"    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
   let &report= repkeep
Karsten Hopp cf9f5a
"   call Dret("tar#Browse : file<".a:tarfile."> not readable")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  if &ma != 1
Karsten Hopp cf9f5a
   set ma
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  let w:tarfile= a:tarfile
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  setlocal noswapfile
Karsten Hopp cf9f5a
  setlocal buftype=nofile
Karsten Hopp cf9f5a
  setlocal bufhidden=hide
Karsten Hopp cf9f5a
  setlocal nobuflisted
Karsten Hopp cf9f5a
  setlocal nowrap
Karsten Hopp cf9f5a
  set ft=tar
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " give header
Karsten Hopp cf9f5a
"  call Decho("printing header")
Karsten Hopp cf9f5a
  let lastline= line("$")
Karsten Hopp cf9f5a
  call setline(lastline+1,'" tar.vim version '.g:loaded_tar)
Karsten Hopp cf9f5a
  call setline(lastline+2,'" Browsing tarfile '.a:tarfile)
Karsten Hopp cf9f5a
  call setline(lastline+3,'" Select a file with cursor and press ENTER')
Karsten Hopp cf9f5a
  $put =''
Karsten Hopp cf9f5a
  0d
Karsten Hopp cf9f5a
  $
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let tarfile= a:tarfile
Karsten Hopp cf9f5a
  if has("win32") && executable("cygpath")
Karsten Hopp cf9f5a
   " assuming cygwin
Karsten Hopp cf9f5a
   let tarfile=substitute(system("cygpath -u ".s:Escape(tarfile,0)),'\n$','','e')
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  let curlast= line("$")
Karsten Hopp cf9f5a
  if tarfile =~# '\.\(gz\|tgz\)$'
Karsten Hopp cf9f5a
"   call Decho("1: exe silent r! gzip -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Karsten Hopp cf9f5a
   exe "silent r! gzip -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Karsten Hopp cf9f5a
  elseif tarfile =~# '\.lrp'
Karsten Hopp cf9f5a
"   call Decho("2: exe silent r! cat -- ".s:Escape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - ")
Karsten Hopp cf9f5a
   exe "silent r! cat -- ".s:Escape(tarfile,1)."|gzip -d -c -|".g:tar_cmd." -".g:tar_browseoptions." - "
Karsten Hopp cf9f5a
  elseif tarfile =~# '\.bz2$'
Karsten Hopp cf9f5a
"   call Decho("3: exe silent r! bzip2 -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - ")
Karsten Hopp cf9f5a
   exe "silent r! bzip2 -d -c -- ".s:Escape(tarfile,1)." | ".g:tar_cmd." -".g:tar_browseoptions." - "
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
   if tarfile =~ '^\s*-'
Karsten Hopp cf9f5a
    " A file name starting with a dash may be taken as an option.  Prepend ./ to avoid that.
Karsten Hopp cf9f5a
    let tarfile = substitute(tarfile, '-', './-', '')
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
"   call Decho("4: exe silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".s:Escape(tarfile,1))
Karsten Hopp cf9f5a
   exe "silent r! ".g:tar_cmd." -".g:tar_browseoptions." ".s:Escape(tarfile,1)
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  if v:shell_error != 0
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl WarningMsg | echo "***warning*** (tar#Browse) please check your g:tar_browseoptions<".g:tar_browseoptions.">"
Karsten Hopp cf9f5a
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
"   call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  if line("$") == curlast || ( line("$") == (curlast + 1) && getline("$") =~ '\c\%(warning\|error\|inappropriate\|unrecognized\)')
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl WarningMsg | echo "***warning*** (tar#Browse) ".a:tarfile." doesn't appear to be a tar file" | echohl None
Karsten Hopp cf9f5a
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
   silent %d
Karsten Hopp cf9f5a
   let eikeep= &ei
Karsten Hopp cf9f5a
   set ei=BufReadCmd,FileReadCmd
Karsten Hopp cf9f5a
   exe "r ".fnameescape(a:tarfile)
Karsten Hopp cf9f5a
   let &ei= eikeep
Karsten Hopp cf9f5a
   1d
Karsten Hopp cf9f5a
"   call Dret("tar#Browse : a:tarfile<".a:tarfile.">")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  setlocal noma nomod ro
Karsten Hopp cf9f5a
  noremap <silent> <buffer> <cr> :call <SID>TarBrowseSelect()<cr>
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let &report= repkeep
Karsten Hopp cf9f5a
"  call Dret("tar#Browse : w:tarfile<".w:tarfile.">")
Karsten Hopp cf9f5a
endfun
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" TarBrowseSelect: {{{2
Karsten Hopp cf9f5a
fun! s:TarBrowseSelect()
Karsten Hopp cf9f5a
"  call Dfunc("TarBrowseSelect() w:tarfile<".w:tarfile."> curfile<".expand("%").">")
Karsten Hopp cf9f5a
  let repkeep= &report
Karsten Hopp cf9f5a
  set report=10
Karsten Hopp cf9f5a
  let fname= getline(".")
Karsten Hopp cf9f5a
"  call Decho("fname<".fname.">")
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  if !exists("g:tar_secure") && fname =~ '^\s*-\|\s\+-'
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl WarningMsg | echo '***error*** (tar#BrowseSelect) rejecting tarfile member<'.fname.'> because of embedded "-"; See :help tar-options'
Karsten Hopp cf9f5a
"   call Dret('tar#BrowseSelect : rejecting tarfile member<'.fname.'> because of embedded "-"')
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " sanity check
Karsten Hopp cf9f5a
  if fname =~ '^"'
Karsten Hopp cf9f5a
   let &report= repkeep
Karsten Hopp cf9f5a
"   call Dret("TarBrowseSelect")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " about to make a new window, need to use w:tarfile
Karsten Hopp cf9f5a
  let tarfile= w:tarfile
Karsten Hopp cf9f5a
  let curfile= expand("%")
Karsten Hopp cf9f5a
  if has("win32") && executable("cygpath")
Karsten Hopp cf9f5a
   " assuming cygwin
Karsten Hopp cf9f5a
   let tarfile=substitute(system("cygpath -u ".s:Escape(tarfile,0)),'\n$','','e')
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  new
Karsten Hopp cf9f5a
  if !exists("g:tar_nomax") || g:tar_nomax == 0
Karsten Hopp cf9f5a
   wincmd _
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  let s:tblfile_{winnr()}= curfile
Karsten Hopp cf9f5a
  call tar#Read("tarfile:".tarfile.'::'.fname,1)
Karsten Hopp cf9f5a
  filetype detect
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let &report= repkeep
Karsten Hopp cf9f5a
"  call Dret("TarBrowseSelect : s:tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Karsten Hopp cf9f5a
endfun
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" tar#Read: {{{2
Karsten Hopp cf9f5a
fun! tar#Read(fname,mode)
Karsten Hopp cf9f5a
"  call Dfunc("tar#Read(fname<".a:fname.">,mode=".a:mode.")")
Karsten Hopp cf9f5a
  let repkeep= &report
Karsten Hopp cf9f5a
  set report=10
Karsten Hopp cf9f5a
  let tarfile = substitute(a:fname,'tarfile:\(.\{-}\)::.*$','\1','')
Karsten Hopp cf9f5a
  let fname   = substitute(a:fname,'tarfile:.\{-}::\(.*\)$','\1','')
Karsten Hopp cf9f5a
  if has("win32") && executable("cygpath")
Karsten Hopp cf9f5a
   " assuming cygwin
Karsten Hopp cf9f5a
   let tarfile=substitute(system("cygpath -u ".s:Escape(tarfile,0)),'\n$','','e')
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
"  call Decho("tarfile<".tarfile.">")
Karsten Hopp cf9f5a
"  call Decho("fname<".fname.">")
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  if      fname =~ '\.gz$'  && executable("zcat")
Karsten Hopp cf9f5a
   let decmp= "|zcat"
Karsten Hopp cf9f5a
   let doro = 1
Karsten Hopp cf9f5a
  elseif  fname =~ '\.bz2$' && executable("bzcat")
Karsten Hopp cf9f5a
   let decmp= "|bzcat"
Karsten Hopp cf9f5a
   let doro = 1
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
   let decmp=""
Karsten Hopp cf9f5a
   let doro = 0
Karsten Hopp cf9f5a
   if fname =~ '\.gz$\|\.bz2$\|\.Z$\|\.zip$'
Karsten Hopp cf9f5a
    setlocal bin
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  if exists("g:tar_secure")
Karsten Hopp cf9f5a
   let tar_secure= " -- "
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
   let tar_secure= " "
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  if tarfile =~# '\.\(gz\|tgz\)$'
Karsten Hopp cf9f5a
"   call Decho("5: exe silent r! gzip -d -c -- ".s:Escape(tarfile,1)."| ".g:tar_cmd.' -'.g:tar_readoptions.' - '.tar_secure.s:Escape(fname,1))
Karsten Hopp cf9f5a
   exe "silent r! gzip -d -c -- ".s:Escape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp
Karsten Hopp cf9f5a
  elseif tarfile =~# '\.lrp$'
Karsten Hopp cf9f5a
"   call Decho("6: exe silent r! cat ".s:Escape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp)
Karsten Hopp cf9f5a
   exe "silent r! cat -- ".s:Escape(tarfile,1)." | gzip -d -c - | ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp
Karsten Hopp cf9f5a
  elseif tarfile =~# '\.bz2$'
Karsten Hopp cf9f5a
"   call Decho("7: exe silent r! bzip2 -d -c ".s:Escape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp)
Karsten Hopp cf9f5a
   exe "silent r! bzip2 -d -c -- ".s:Escape(tarfile,1)."| ".g:tar_cmd." -".g:tar_readoptions." - ".tar_secure.s:Escape(fname,1).decmp
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
   if tarfile =~ '^\s*-'
Karsten Hopp cf9f5a
    " A file name starting with a dash may be taken as an option.  Prepend ./ to avoid that.
Karsten Hopp cf9f5a
    let tarfile = substitute(tarfile, '-', './-', '')
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
"   call Decho("8: exe silent r! ".g:tar_cmd." -".g:tar_readoptions." "s:Escape(tarfile,1).tar_secure..s:Escape(fname,1).decmp)
Karsten Hopp cf9f5a
   exe "silent r! ".g:tar_cmd." -".g:tar_readoptions." ".s:Escape(tarfile,1).tar_secure.s:Escape(fname,1).decmp
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  if doro
Karsten Hopp cf9f5a
   " because the reverse process of compressing changed files back into the tarball is not currently supported
Karsten Hopp cf9f5a
   setlocal ro
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let w:tarfile= a:fname
Karsten Hopp cf9f5a
  exe "file tarfile::".fnameescape(fname)
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " cleanup
Karsten Hopp cf9f5a
  0d
Karsten Hopp cf9f5a
  set nomod
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let &report= repkeep
Karsten Hopp cf9f5a
"  call Dret("tar#Read : w:tarfile<".w:tarfile.">")
Karsten Hopp cf9f5a
endfun
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" tar#Write: {{{2
Karsten Hopp cf9f5a
fun! tar#Write(fname)
Karsten Hopp cf9f5a
"  call Dfunc("tar#Write(fname<".a:fname.">) w:tarfile<".w:tarfile."> tblfile_".winnr()."<".s:tblfile_{winnr()}.">")
Karsten Hopp cf9f5a
  let repkeep= &report
Karsten Hopp cf9f5a
  set report=10
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  if !exists("g:tar_secure") && a:fname =~ '^\s*-\|\s\+-'
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl WarningMsg | echo '***error*** (tar#Write) rejecting tarfile member<'.a:fname.'> because of embedded "-"; See :help tar-options'
Karsten Hopp cf9f5a
"   call Dret('tar#Write : rejecting tarfile member<'.fname.'> because of embedded "-"')
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " sanity checks
Karsten Hopp cf9f5a
  if !executable(g:tar_cmd)
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl Error | echo '***error*** (tar#Browse) "'.g:tar_cmd.'" not available on your system'
Karsten Hopp cf9f5a
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
   let &report= repkeep
Karsten Hopp cf9f5a
"   call Dret("tar#Write")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  if !exists("*mkdir")
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl Error | echo "***error*** (tar#Write) sorry, mkdir() doesn't work on your system" | echohl None
Karsten Hopp cf9f5a
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
   let &report= repkeep
Karsten Hopp cf9f5a
"   call Dret("tar#Write")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let curdir= getcwd()
Karsten Hopp cf9f5a
  let tmpdir= tempname()
Karsten Hopp cf9f5a
"  call Decho("orig tempname<".tmpdir.">")
Karsten Hopp cf9f5a
  if tmpdir =~ '\.'
Karsten Hopp cf9f5a
   let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
"  call Decho("tmpdir<".tmpdir.">")
Karsten Hopp cf9f5a
  call mkdir(tmpdir,"p")
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " attempt to change to the indicated directory
Karsten Hopp cf9f5a
  try
Karsten Hopp cf9f5a
   exe "cd ".fnameescape(tmpdir)
Karsten Hopp cf9f5a
  catch /^Vim\%((\a\+)\)\=:E344/
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl Error | echo "***error*** (tar#Write) cannot cd to temporary directory" | Echohl None
Karsten Hopp cf9f5a
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
   let &report= repkeep
Karsten Hopp cf9f5a
"   call Dret("tar#Write")
Karsten Hopp cf9f5a
   return
Karsten Hopp cf9f5a
  endtry
Karsten Hopp cf9f5a
"  call Decho("current directory now: ".getcwd())
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " place temporary files under .../_ZIPVIM_/
Karsten Hopp cf9f5a
  if isdirectory("_ZIPVIM_")
Karsten Hopp cf9f5a
   call s:Rmdir("_ZIPVIM_")
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  call mkdir("_ZIPVIM_")
Karsten Hopp cf9f5a
  cd _ZIPVIM_
Karsten Hopp cf9f5a
"  call Decho("current directory now: ".getcwd())
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let tarfile = substitute(w:tarfile,'tarfile:\(.\{-}\)::.*$','\1','')
Karsten Hopp cf9f5a
  let fname   = substitute(w:tarfile,'tarfile:.\{-}::\(.*\)$','\1','')
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  " handle compressed archives
Karsten Hopp cf9f5a
  if tarfile =~# '\.gz'
Karsten Hopp cf9f5a
   call system("gzip -d -- ".s:Escape(tarfile,0))
Karsten Hopp cf9f5a
   let tarfile = substitute(tarfile,'\.gz','','e')
Karsten Hopp cf9f5a
   let compress= "gzip -- ".s:Escape(tarfile,0)
Karsten Hopp cf9f5a
"   call Decho("compress<".compress.">")
Karsten Hopp cf9f5a
  elseif tarfile =~# '\.tgz'
Karsten Hopp cf9f5a
   call system("gzip -d -- ".s:Escape(tarfile,0))
Karsten Hopp cf9f5a
   let tarfile = substitute(tarfile,'\.tgz','.tar','e')
Karsten Hopp cf9f5a
   let compress= "gzip -- ".s:Escape(tarfile,0)
Karsten Hopp cf9f5a
   let tgz     = 1
Karsten Hopp cf9f5a
"   call Decho("compress<".compress.">")
Karsten Hopp cf9f5a
  elseif tarfile =~# '\.bz2'
Karsten Hopp cf9f5a
   call system("bzip2 -d -- ".s:Escape(tarfile,0))
Karsten Hopp cf9f5a
   let tarfile = substitute(tarfile,'\.bz2','','e')
Karsten Hopp cf9f5a
   let compress= "bzip2 -- ".s:Escape(tarfile,0)
Karsten Hopp cf9f5a
"   call Decho("compress<".compress.">")
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
"  call Decho("tarfile<".tarfile.">")
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  if v:shell_error != 0
Karsten Hopp cf9f5a
   redraw!
Karsten Hopp cf9f5a
   echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".tarfile." with ".fname | echohl None
Karsten Hopp cf9f5a
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
"   call Decho("tarfile<".tarfile."> fname<".fname.">")
Karsten Hopp cf9f5a
 
Karsten Hopp cf9f5a
   if fname =~ '/'
Karsten Hopp cf9f5a
    let dirpath = substitute(fname,'/[^/]\+$','','e')
Karsten Hopp cf9f5a
    if executable("cygpath")
Karsten Hopp cf9f5a
     let dirpath = substitute(system("cygpath ".s:Escape(dirpath, 0)),'\n','','e')
Karsten Hopp cf9f5a
    endif
Karsten Hopp cf9f5a
    call mkdir(dirpath,"p")
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
   if tarfile !~ '/'
Karsten Hopp cf9f5a
    let tarfile= curdir.'/'.tarfile
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
   if tarfile =~ '^\s*-'
Karsten Hopp cf9f5a
    " A file name starting with a dash may be taken as an option.  Prepend ./ to avoid that.
Karsten Hopp cf9f5a
    let tarfile = substitute(tarfile, '-', './-', '')
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
"   call Decho("tarfile<".tarfile."> fname<".fname.">")
Karsten Hopp cf9f5a
 
Karsten Hopp cf9f5a
   if exists("g:tar_secure")
Karsten Hopp cf9f5a
    let tar_secure= " -- "
Karsten Hopp cf9f5a
   else
Karsten Hopp cf9f5a
    let tar_secure= " "
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
   exe "w! ".fnameescape(fname)
Karsten Hopp cf9f5a
   if executable("cygpath")
Karsten Hopp cf9f5a
    let tarfile = substitute(system("cygpath ".s:Escape(tarfile,0)),'\n','','e')
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
 
Karsten Hopp cf9f5a
   " delete old file from tarfile
Karsten Hopp cf9f5a
"   call Decho("system(".g:tar_cmd." --delete -f ".s:Escape(tarfile,0)." -- ".s:Escape(fname,0).")")
Karsten Hopp cf9f5a
   call system(g:tar_cmd." --delete -f ".s:Escape(tarfile,0).tar_secure.s:Escape(fname,0))
Karsten Hopp cf9f5a
   if v:shell_error != 0
Karsten Hopp cf9f5a
    redraw!
Karsten Hopp cf9f5a
    echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Karsten Hopp cf9f5a
"    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
   else
Karsten Hopp cf9f5a
 
Karsten Hopp cf9f5a
    " update tarfile with new file 
Karsten Hopp cf9f5a
"    call Decho(g:tar_cmd." -".g:tar_writeoptions." ".s:Escape(tarfile,0).tar_secure.s:Escape(fname,0))
Karsten Hopp cf9f5a
    call system(g:tar_cmd." -".g:tar_writeoptions." ".s:Escape(tarfile,0).tar_secure.s:Escape(fname,0))
Karsten Hopp cf9f5a
    if v:shell_error != 0
Karsten Hopp cf9f5a
     redraw!
Karsten Hopp cf9f5a
     echohl Error | echo "***error*** (tar#Write) sorry, unable to update ".fnameescape(tarfile)." with ".fnameescape(fname) | echohl None
Karsten Hopp cf9f5a
"     call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Karsten Hopp cf9f5a
    elseif exists("compress")
Karsten Hopp cf9f5a
"     call Decho("call system(".compress.")")
Karsten Hopp cf9f5a
     call system(compress)
Karsten Hopp cf9f5a
     if exists("tgz")
Karsten Hopp cf9f5a
"      call Decho("rename(".tarfile.".gz,".substitute(tarfile,'\.tar$','.tgz','e').")")
Karsten Hopp cf9f5a
      call rename(tarfile.".gz",substitute(tarfile,'\.tar$','.tgz','e'))
Karsten Hopp cf9f5a
     endif
Karsten Hopp cf9f5a
    endif
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
   " support writing tarfiles across a network
Karsten Hopp cf9f5a
   if s:tblfile_{winnr()} =~ '^\a\+://'
Karsten Hopp cf9f5a
"    call Decho("handle writing <".tarfile."> across network to <".s:tblfile_{winnr()}.">")
Karsten Hopp cf9f5a
    let tblfile= s:tblfile_{winnr()}
Karsten Hopp cf9f5a
    1split|enew
Karsten Hopp cf9f5a
    let binkeep= &binary
Karsten Hopp cf9f5a
    let eikeep = &ei
Karsten Hopp cf9f5a
    set binary ei=all
Karsten Hopp cf9f5a
    exe "e! ".fnameescape(tarfile)
Karsten Hopp cf9f5a
    call netrw#NetWrite(tblfile)
Karsten Hopp cf9f5a
    let &ei     = eikeep
Karsten Hopp cf9f5a
    let &binary = binkeep
Karsten Hopp cf9f5a
    q!
Karsten Hopp cf9f5a
    unlet s:tblfile_{winnr()}
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  
Karsten Hopp cf9f5a
  " cleanup and restore current directory
Karsten Hopp cf9f5a
  cd ..
Karsten Hopp cf9f5a
  call s:Rmdir("_ZIPVIM_")
Karsten Hopp cf9f5a
  exe "cd ".fnameescape(curdir)
Karsten Hopp cf9f5a
  setlocal nomod
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
  let &report= repkeep
Karsten Hopp cf9f5a
"  call Dret("tar#Write")
Karsten Hopp cf9f5a
endfun
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" Rmdir: {{{2
Karsten Hopp cf9f5a
fun! s:Rmdir(fname)
Karsten Hopp cf9f5a
"  call Dfunc("Rmdir(fname<".a:fname.">)")
Karsten Hopp cf9f5a
  if has("unix")
Karsten Hopp cf9f5a
   call system("/bin/rm -rf -- ".s:Escape(a:fname,0))
Karsten Hopp cf9f5a
  elseif has("win32") || has("win95") || has("win64") || has("win16")
Karsten Hopp cf9f5a
   if &shell =~? "sh$"
Karsten Hopp cf9f5a
    call system("/bin/rm -rf -- ".s:Escape(a:fname,0))
Karsten Hopp cf9f5a
   else
Karsten Hopp cf9f5a
    call system("del /S ".s:Escape(a:fname,0))
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
"  call Dret("Rmdir")
Karsten Hopp cf9f5a
endfun
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" s:Escape: {{{2
Karsten Hopp cf9f5a
fun s:Escape(name,isfilt)
Karsten Hopp cf9f5a
  " shellescape() was added by patch 7.0.111
Karsten Hopp cf9f5a
  if exists("*shellescape")
Karsten Hopp cf9f5a
   if a:isfilt
Karsten Hopp cf9f5a
    let qnameq= shellescape(a:name,1)
Karsten Hopp cf9f5a
   else
Karsten Hopp cf9f5a
    let qnameq= shellescape(a:name)
Karsten Hopp cf9f5a
   endif
Karsten Hopp cf9f5a
  else
Karsten Hopp cf9f5a
   let qnameq= g:tar_shq . a:name . g:tar_shq
Karsten Hopp cf9f5a
  endif
Karsten Hopp cf9f5a
  return qnameq
Karsten Hopp cf9f5a
endfun
Karsten Hopp cf9f5a
Karsten Hopp cf9f5a
" ---------------------------------------------------------------------
Karsten Hopp cf9f5a
" Modelines And Restoration: {{{1
Karsten Hopp cf9f5a
let &cpo= s:keepcpo
Karsten Hopp cf9f5a
unlet s:keepcpo
Karsten Hopp cf9f5a
" vim:ts=8 fdm=marker