cf9f5ab
" zip.vim: Handles browsing zipfiles
cf9f5ab
"            AUTOLOAD PORTION
cf9f5ab
" Date:		Jul 30, 2008
cf9f5ab
" Version:	22
cf9f5ab
" Maintainer:	Charles E Campbell, Jr <NdrOchip@ScampbellPfamily.AbizM-NOSPAM>
cf9f5ab
" License:	Vim License  (see vim's :help license)
cf9f5ab
" Copyright:    Copyright (C) 2005-2008 Charles E. Campbell, Jr. {{{1
cf9f5ab
"               Permission is hereby granted to use and distribute this code,
cf9f5ab
"               with or without modifications, provided that this copyright
cf9f5ab
"               notice is copied with it. Like anything else that's free,
cf9f5ab
"               zip.vim and zipPlugin.vim are provided *as is* and comes with
cf9f5ab
"               no warranty of any kind, either expressed or implied. By using
cf9f5ab
"               this plugin, you agree that in no event will the copyright
cf9f5ab
"               holder be liable for any damages resulting from the use
cf9f5ab
"               of this software.
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" Load Once: {{{1
cf9f5ab
let s:keepcpo= &cpo
cf9f5ab
set cpo&vim
cf9f5ab
if &cp || exists("g:loaded_zip") || v:version < 700
cf9f5ab
 finish
cf9f5ab
endif
cf9f5ab
cf9f5ab
let g:loaded_zip     = "v22"
cf9f5ab
let s:zipfile_escape = ' ?&;\'
cf9f5ab
let s:ERROR          = 2
cf9f5ab
let s:WARNING        = 1
cf9f5ab
let s:NOTE           = 0
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
"  Global Values: {{{1
cf9f5ab
if !exists("g:zip_shq")
cf9f5ab
 if &shq != ""
cf9f5ab
  let g:zip_shq= &shq
cf9f5ab
 elseif has("unix")
cf9f5ab
  let g:zip_shq= "'"
cf9f5ab
 else
cf9f5ab
  let g:zip_shq= '"'
cf9f5ab
 endif
cf9f5ab
endif
cf9f5ab
if !exists("g:zip_zipcmd")
cf9f5ab
 let g:zip_zipcmd= "zip"
cf9f5ab
endif
cf9f5ab
if !exists("g:zip_unzipcmd")
cf9f5ab
 let g:zip_unzipcmd= "unzip"
cf9f5ab
endif
cf9f5ab
cf9f5ab
" ----------------
cf9f5ab
"  Functions: {{{1
cf9f5ab
" ----------------
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" zip#Browse: {{{2
cf9f5ab
fun! zip#Browse(zipfile)
cf9f5ab
"  call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
cf9f5ab
  let repkeep= &report
cf9f5ab
  set report=10
cf9f5ab
cf9f5ab
  " sanity checks
cf9f5ab
  if !exists("*fnameescape")
cf9f5ab
   if &verbose > 1
cf9f5ab
    echoerr "the zip plugin is not available (your vim doens't support fnameescape())"
cf9f5ab
   endif
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
  if !executable(g:zip_unzipcmd)
cf9f5ab
   redraw!
cf9f5ab
   echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
cf9f5ab
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
   let &report= repkeep
cf9f5ab
"   call Dret("zip#Browse")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
  if !filereadable(a:zipfile)
cf9f5ab
   if a:zipfile !~# '^\a\+://'
cf9f5ab
    " if its an url, don't complain, let url-handlers such as vim do its thing
cf9f5ab
    redraw!
cf9f5ab
    echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
cf9f5ab
"    call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
   endif
cf9f5ab
   let &report= repkeep
cf9f5ab
"   call Dret("zip#Browse : file<".a:zipfile."> not readable")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
"  call Decho("passed sanity checks")
cf9f5ab
  if &ma != 1
cf9f5ab
   set ma
cf9f5ab
  endif
cf9f5ab
  let b:zipfile= a:zipfile
cf9f5ab
cf9f5ab
  setlocal noswapfile
cf9f5ab
  setlocal buftype=nofile
cf9f5ab
  setlocal bufhidden=hide
cf9f5ab
  setlocal nobuflisted
cf9f5ab
  setlocal nowrap
cf9f5ab
  set ft=tar
cf9f5ab
cf9f5ab
  " give header
cf9f5ab
  let lastline= line("$")
cf9f5ab
  call setline(lastline+1,'" zip.vim version '.g:loaded_zip)
cf9f5ab
  call setline(lastline+2,'" Browsing zipfile '.a:zipfile)
cf9f5ab
  call setline(lastline+3,'" Select a file with cursor and press ENTER')
cf9f5ab
  $put =''
cf9f5ab
  0d
cf9f5ab
  $
cf9f5ab
cf9f5ab
"  call Decho("exe silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1))
cf9f5ab
  exe "silent r! ".g:zip_unzipcmd." -l -- ".s:Escape(a:zipfile,1)
cf9f5ab
  if v:shell_error != 0
cf9f5ab
   redraw!
cf9f5ab
   echohl WarningMsg | echo "***warning*** (zip#Browse) ".fnameescape(a:zipfile)." is not a zip file" | echohl None
cf9f5ab
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
   silent %d
cf9f5ab
   let eikeep= &ei
cf9f5ab
   set ei=BufReadCmd,FileReadCmd
cf9f5ab
   exe "r ".fnameescape(a:zipfile)
cf9f5ab
   let &ei= eikeep
cf9f5ab
   1d
cf9f5ab
"   call Dret("zip#Browse")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
"  call Decho("line 6: ".getline(6))
cf9f5ab
  let namecol= stridx(getline(6),'Name') + 1
cf9f5ab
"  call Decho("namecol=".namecol)
cf9f5ab
  4,$g/^\s*----/d
cf9f5ab
  4,$g/^\s*\a/d
cf9f5ab
  $d
cf9f5ab
  if namecol > 0
cf9f5ab
   exe 'silent 4,$s/^.*\%'.namecol.'c//'
cf9f5ab
  endif
cf9f5ab
cf9f5ab
  setlocal noma nomod ro
cf9f5ab
  noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
cf9f5ab
cf9f5ab
  let &report= repkeep
cf9f5ab
"  call Dret("zip#Browse")
cf9f5ab
endfun
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" ZipBrowseSelect: {{{2
cf9f5ab
fun! s:ZipBrowseSelect()
cf9f5ab
"  call Dfunc("ZipBrowseSelect() zipfile<".b:zipfile."> curfile<".expand("%").">")
cf9f5ab
  let repkeep= &report
cf9f5ab
  set report=10
cf9f5ab
  let fname= getline(".")
cf9f5ab
cf9f5ab
  " sanity check
cf9f5ab
  if fname =~ '^"'
cf9f5ab
   let &report= repkeep
cf9f5ab
"   call Dret("ZipBrowseSelect")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
  if fname =~ '/$'
cf9f5ab
   redraw!
cf9f5ab
   echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
cf9f5ab
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
   let &report= repkeep
cf9f5ab
"   call Dret("ZipBrowseSelect")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
cf9f5ab
"  call Decho("fname<".fname.">")
cf9f5ab
cf9f5ab
  " get zipfile to the new-window
cf9f5ab
  let zipfile = b:zipfile
cf9f5ab
  let curfile= expand("%")
cf9f5ab
"  call Decho("zipfile<".zipfile.">")
cf9f5ab
"  call Decho("curfile<".curfile.">")
cf9f5ab
cf9f5ab
  new
cf9f5ab
  if !exists("g:zip_nomax") || g:zip_nomax == 0
cf9f5ab
   wincmd _
cf9f5ab
  endif
cf9f5ab
  let s:zipfile_{winnr()}= curfile
cf9f5ab
"  call Decho("exe e ".fnameescape("zipfile:".zipfile.'::'.fname))
cf9f5ab
  exe "e ".fnameescape("zipfile:".zipfile.'::'.fname)
cf9f5ab
  filetype detect
cf9f5ab
cf9f5ab
  let &report= repkeep
cf9f5ab
"  call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
cf9f5ab
endfun
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" zip#Read: {{{2
cf9f5ab
fun! zip#Read(fname,mode)
cf9f5ab
"  call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
cf9f5ab
  let repkeep= &report
cf9f5ab
  set report=10
cf9f5ab
cf9f5ab
  if has("unix")
cf9f5ab
   let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
cf9f5ab
   let fname   = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
cf9f5ab
  else
cf9f5ab
   let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
cf9f5ab
   let fname   = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
cf9f5ab
   let fname = substitute(fname, '[', '[[]', 'g')
cf9f5ab
  endif
cf9f5ab
"  call Decho("zipfile<".zipfile.">")
cf9f5ab
"  call Decho("fname  <".fname.">")
cf9f5ab
cf9f5ab
"  call Decho("exe r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1))
cf9f5ab
  exe "silent r! ".g:zip_unzipcmd." -p -- ".s:Escape(zipfile,1)." ".s:Escape(fname,1)
cf9f5ab
cf9f5ab
  " cleanup
cf9f5ab
  0d
cf9f5ab
  set nomod
cf9f5ab
cf9f5ab
  let &report= repkeep
cf9f5ab
"  call Dret("zip#Read")
cf9f5ab
endfun
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" zip#Write: {{{2
cf9f5ab
fun! zip#Write(fname)
cf9f5ab
"  call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
cf9f5ab
  let repkeep= &report
cf9f5ab
  set report=10
cf9f5ab
cf9f5ab
  " sanity checks
cf9f5ab
  if !executable(g:zip_zipcmd)
cf9f5ab
   redraw!
cf9f5ab
   echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
cf9f5ab
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
   let &report= repkeep
cf9f5ab
"   call Dret("zip#Write")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
  if !exists("*mkdir")
cf9f5ab
   redraw!
cf9f5ab
   echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
cf9f5ab
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
   let &report= repkeep
cf9f5ab
"   call Dret("zip#Write")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
cf9f5ab
  let curdir= getcwd()
cf9f5ab
  let tmpdir= tempname()
cf9f5ab
"  call Decho("orig tempname<".tmpdir.">")
cf9f5ab
  if tmpdir =~ '\.'
cf9f5ab
   let tmpdir= substitute(tmpdir,'\.[^.]*$','','e')
cf9f5ab
  endif
cf9f5ab
"  call Decho("tmpdir<".tmpdir.">")
cf9f5ab
  call mkdir(tmpdir,"p")
cf9f5ab
cf9f5ab
  " attempt to change to the indicated directory
cf9f5ab
  if s:ChgDir(tmpdir,s:ERROR,"(zip#Write) cannot cd to temporary directory")
cf9f5ab
   let &report= repkeep
cf9f5ab
"   call Dret("zip#Write")
cf9f5ab
   return
cf9f5ab
  endif
cf9f5ab
"  call Decho("current directory now: ".getcwd())
cf9f5ab
cf9f5ab
  " place temporary files under .../_ZIPVIM_/
cf9f5ab
  if isdirectory("_ZIPVIM_")
cf9f5ab
   call s:Rmdir("_ZIPVIM_")
cf9f5ab
  endif
cf9f5ab
  call mkdir("_ZIPVIM_")
cf9f5ab
  cd _ZIPVIM_
cf9f5ab
"  call Decho("current directory now: ".getcwd())
cf9f5ab
cf9f5ab
  if has("unix")
cf9f5ab
   let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
cf9f5ab
   let fname   = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
cf9f5ab
  else
cf9f5ab
   let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
cf9f5ab
   let fname   = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
cf9f5ab
  endif
cf9f5ab
"  call Decho("zipfile<".zipfile.">")
cf9f5ab
"  call Decho("fname  <".fname.">")
cf9f5ab
cf9f5ab
  if fname =~ '/'
cf9f5ab
   let dirpath = substitute(fname,'/[^/]\+$','','e')
cf9f5ab
   if executable("cygpath")
cf9f5ab
    let dirpath = substitute(system("cygpath ".s:Escape(dirpath,0)),'\n','','e')
cf9f5ab
   endif
cf9f5ab
"   call Decho("mkdir(dirpath<".dirpath.">,p)")
cf9f5ab
   call mkdir(dirpath,"p")
cf9f5ab
  endif
cf9f5ab
  if zipfile !~ '/'
cf9f5ab
   let zipfile= curdir.'/'.zipfile
cf9f5ab
  endif
cf9f5ab
"  call Decho("zipfile<".zipfile."> fname<".fname.">")
cf9f5ab
cf9f5ab
  exe "w! ".fnameescape(fname)
cf9f5ab
  if executable("cygpath")
cf9f5ab
   let zipfile = substitute(system("cygpath ".s:Escape(zipfile,0)),'\n','','e')
cf9f5ab
  endif
cf9f5ab
cf9f5ab
  if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
cf9f5ab
    let fname = substitute(fname, '[', '[[]', 'g')
cf9f5ab
  endif
cf9f5ab
cf9f5ab
"  call Decho(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
cf9f5ab
  call system(g:zip_zipcmd." -u ".s:Escape(fnamemodify(zipfile,":p"),0)." ".s:Escape(fname,0))
cf9f5ab
  if v:shell_error != 0
cf9f5ab
   redraw!
cf9f5ab
   echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
cf9f5ab
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
cf9f5ab
  elseif s:zipfile_{winnr()} =~ '^\a\+://'
cf9f5ab
   " support writing zipfiles across a network
cf9f5ab
   let netzipfile= s:zipfile_{winnr()}
cf9f5ab
"   call Decho("handle writing <".zipfile."> across network as <".netzipfile.">")
cf9f5ab
   1split|enew
cf9f5ab
   let binkeep= &binary
cf9f5ab
   let eikeep = &ei
cf9f5ab
   set binary ei=all
cf9f5ab
   exe "e! ".fnameescape(zipfile)
cf9f5ab
   call netrw#NetWrite(netzipfile)
cf9f5ab
   let &ei     = eikeep
cf9f5ab
   let &binary = binkeep
cf9f5ab
   q!
cf9f5ab
   unlet s:zipfile_{winnr()}
cf9f5ab
  endif
cf9f5ab
  
cf9f5ab
  " cleanup and restore current directory
cf9f5ab
  cd ..
cf9f5ab
  call s:Rmdir("_ZIPVIM_")
cf9f5ab
  call s:ChgDir(curdir,s:WARNING,"(zip#Write) unable to return to ".curdir."!")
cf9f5ab
  call s:Rmdir(tmpdir)
cf9f5ab
  setlocal nomod
cf9f5ab
cf9f5ab
  let &report= repkeep
cf9f5ab
"  call Dret("zip#Write")
cf9f5ab
endfun
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" s:Escape: {{{2
cf9f5ab
fun! s:Escape(fname,isfilt)
cf9f5ab
"  call Dfunc("QuoteFileDir(fname<".a:fname."> isfilt=".a:isfilt.")")
cf9f5ab
  if exists("*shellescape")
cf9f5ab
   if a:isfilt
cf9f5ab
    let qnameq= shellescape(a:fname,1)
cf9f5ab
   else
cf9f5ab
    let qnameq= shellescape(a:fname)
cf9f5ab
   endif
cf9f5ab
  else
cf9f5ab
   let qnameq= g:zip_shq.escape(a:fname,g:zip_shq).g:zip_shq
cf9f5ab
  endif
cf9f5ab
"  call Dret("QuoteFileDir <".qnameq.">")
cf9f5ab
  return qnameq
cf9f5ab
endfun
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" ChgDir: {{{2
cf9f5ab
fun! s:ChgDir(newdir,errlvl,errmsg)
cf9f5ab
"  call Dfunc("ChgDir(newdir<".a:newdir."> errlvl=".a:errlvl."  errmsg<".a:errmsg.">)")
cf9f5ab
cf9f5ab
  try
cf9f5ab
   exe "cd ".fnameescape(a:newdir)
cf9f5ab
  catch /^Vim\%((\a\+)\)\=:E344/
cf9f5ab
   redraw!
cf9f5ab
   if a:errlvl == s:NOTE
cf9f5ab
    echo "***note*** ".a:errmsg
cf9f5ab
   elseif a:errlvl == s:WARNING
cf9f5ab
    echohl WarningMsg | echo "***warning*** ".a:errmsg | echohl NONE
cf9f5ab
   elseif a:errlvl == s:ERROR
cf9f5ab
    echohl Error | echo "***error*** ".a:errmsg | echohl NONE
cf9f5ab
   endif
cf9f5ab
"   call inputsave()|call input("Press <cr> to continue")|call inputrestore()
cf9f5ab
"   call Dret("ChgDir 1")
cf9f5ab
   return 1
cf9f5ab
  endtry
cf9f5ab
cf9f5ab
"  call Dret("ChgDir 0")
cf9f5ab
  return 0
cf9f5ab
endfun
cf9f5ab
cf9f5ab
" ---------------------------------------------------------------------
cf9f5ab
" s:Rmdir: {{{2
cf9f5ab
fun! s:Rmdir(fname)
cf9f5ab
"  call Dfunc("Rmdir(fname<".a:fname.">)")
cf9f5ab
  if (has("win32") || has("win95") || has("win64") || has("win16")) && &shell !~? 'sh$'
cf9f5ab
   call system("rmdir /S/Q ".s:Escape(a:fname,0))
cf9f5ab
  else
cf9f5ab
   call system("/bin/rm -rf ".s:Escape(a:fname,0))
cf9f5ab
  endif
cf9f5ab
"  call Dret("Rmdir")
cf9f5ab
endfun
cf9f5ab
cf9f5ab
" ------------------------------------------------------------------------
cf9f5ab
" Modelines And Restoration: {{{1
cf9f5ab
let &cpo= s:keepcpo
cf9f5ab
unlet s:keepcpo
cf9f5ab
" vim:ts=8 fdm=marker