0
0
mirror of https://github.com/vim/vim.git synced 2025-07-04 23:07:33 -04:00
vim/runtime/autoload/zip.vim

311 lines
9.4 KiB
VimL
Raw Normal View History

2005-09-16 21:55:43 +00:00
" zip.vim: Handles browsing zipfiles
" AUTOLOAD PORTION
2006-05-02 22:08:30 +00:00
" Date: May 01, 2006
" Version: 9
2005-09-16 21:55:43 +00:00
" Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz>
" License: Vim License (see vim's :help license)
" 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,
" zipPlugin.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_zip")
finish
endif
2006-05-02 22:08:30 +00:00
let g:loaded_zip = "v9"
2006-03-23 22:59:57 +00:00
let s:zipfile_escape = ' ?&;\'
2005-09-16 21:55:43 +00:00
" ----------------
" Functions: {{{1
" ----------------
" ---------------------------------------------------------------------
" zip#Browse: {{{2
fun! zip#Browse(zipfile)
" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)")
2005-11-28 23:01:53 +00:00
let repkeep= &report
set report=10
2005-09-16 21:55:43 +00:00
" sanity checks
if !executable("unzip")
echohl Error | echo "***error*** (zip#Browse) unzip not available on your system"
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("zip#Browse")
return
endif
if !filereadable(a:zipfile)
2005-11-23 21:25:05 +00:00
if a:zipfile !~# '^\a\+://'
" if its an url, don't complain, let url-handlers such as vim do its thing
echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
endif
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-11-23 21:25:05 +00:00
" call Dret("zip#Browse : file<".a:zipfile."> not readable")
2005-09-16 21:55:43 +00:00
return
endif
2006-03-23 22:59:57 +00:00
" call Decho("passed sanity checks")
2005-09-16 21:55:43 +00:00
if &ma != 1
set ma
endif
let w:zipfile= a:zipfile
setlocal noswapfile
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal nobuflisted
setlocal nowrap
set ft=tar
" give header
exe "$put ='".'\"'." zip.vim version ".g:loaded_zip."'"
exe "$put ='".'\"'." Browsing zipfile ".a:zipfile."'"
exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'"
$put =''
0d
$
2006-04-05 20:41:53 +00:00
" call Decho("exe silent r! unzip -l '".a:zipfile."'")
exe "silent r! unzip -l '".a:zipfile."'"
2006-05-02 22:08:30 +00:00
if v:shell_error != 0
echohl WarningMsg | echo "***warning*** (zip#Browse) ".a:zipfile." is not a zip file" | echohl None
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
silent %d
let eikeep= &ei
set ei=BufReadCmd,FileReadCmd
exe "r ".a:zipfile
let &ei= eikeep
1d
" call Dret("zip#Browse")
return
endif
2006-04-05 20:41:53 +00:00
" call Decho("line 6: ".getline(6))
let namecol= stridx(getline(6),'Name') + 1
" call Decho("namecol=".namecol)
4,$g/^\s*----/d
4,$g/^\s*\a/d
2005-09-16 21:55:43 +00:00
$d
2006-04-11 21:38:50 +00:00
if namecol > 0
exe 'silent 4,$s/^.*\%'.namecol.'c//'
endif
2005-09-16 21:55:43 +00:00
setlocal noma nomod ro
noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr>
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("zip#Browse")
endfun
" ---------------------------------------------------------------------
" ZipBrowseSelect: {{{2
fun! s:ZipBrowseSelect()
2005-11-23 21:25:05 +00:00
" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">")
2005-11-28 23:01:53 +00:00
let repkeep= &report
set report=10
2005-09-16 21:55:43 +00:00
let fname= getline(".")
" sanity check
if fname =~ '^"'
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("ZipBrowseSelect")
return
endif
if fname =~ '/$'
echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("ZipBrowseSelect")
return
endif
" call Decho("fname<".fname.">")
" get zipfile to the new-window
let zipfile= substitute(w:zipfile,'.zip$','','e')
2006-04-05 20:41:53 +00:00
let curfile= expand("%")
2006-03-23 22:59:57 +00:00
" call Decho("zipfile<".zipfile.">")
" call Decho("curfile<".curfile.">")
2005-09-16 21:55:43 +00:00
new
wincmd _
2005-11-23 21:25:05 +00:00
let s:zipfile_{winnr()}= curfile
2006-05-02 22:08:30 +00:00
" call Decho("exe e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape))
exe "e zipfile:".escape(zipfile,s:zipfile_escape).'::'.escape(fname,s:zipfile_escape)
2005-09-16 21:55:43 +00:00
filetype detect
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-11-23 21:25:05 +00:00
" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
2005-09-16 21:55:43 +00:00
endfun
" ---------------------------------------------------------------------
" zip#Read: {{{2
fun! zip#Read(fname,mode)
" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")")
2005-11-28 23:01:53 +00:00
let repkeep= &report
set report=10
2006-05-02 22:08:30 +00:00
if has("unix")
let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
else
let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
endif
" call Decho("zipfile<".zipfile.">")
" call Decho("fname <".fname.">")
2005-09-16 21:55:43 +00:00
2006-04-05 20:41:53 +00:00
" call Decho("exe r! unzip -p '".zipfile."' '".fname."'")
exe "silent r! unzip -p '".zipfile."' '".fname."'"
2005-09-16 21:55:43 +00:00
" cleanup
0d
set nomod
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("zip#Read")
endfun
" ---------------------------------------------------------------------
" zip#Write: {{{2
fun! zip#Write(fname)
2006-04-05 20:41:53 +00:00
" call Dfunc("zip#Write(fname<".a:fname.">) zipfile_".winnr()."<".s:zipfile_{winnr()}.">")
2005-11-28 23:01:53 +00:00
let repkeep= &report
set report=10
2005-09-16 21:55:43 +00:00
" sanity checks
if !executable("zip")
echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("zip#Write")
return
endif
if !exists("*mkdir")
echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("zip#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/
echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | Echohl None
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("zip#Write")
return
endtry
" call Decho("current directory now: ".getcwd())
" place temporary files under .../_ZIPVIM_/
if isdirectory("_ZIPVIM_")
call s:Rmdir("_ZIPVIM_")
endif
call mkdir("_ZIPVIM_")
cd _ZIPVIM_
" call Decho("current directory now: ".getcwd())
2006-05-02 22:08:30 +00:00
if has("unix")
let zipfile = substitute(a:fname,'zipfile:\(.\{-}\)::[^\\].*$','\1','')
let fname = substitute(a:fname,'zipfile:.\{-}::\([^\\].*\)$','\1','')
else
let zipfile = substitute(a:fname,'^.\{-}zipfile:\(.\{-}\)::[^\\].*$','\1','')
let fname = substitute(a:fname,'^.\{-}zipfile:.\{-}::\([^\\].*\)$','\1','')
endif
" call Decho("zipfile<".zipfile.">")
" call Decho("fname <".fname.">")
2005-12-29 22:48:26 +00:00
if fname =~ '/'
let dirpath = substitute(fname,'/[^/]\+$','','e')
if executable("cygpath")
let dirpath = substitute(system("cygpath ".dirpath),'\n','','e')
endif
2006-04-05 20:41:53 +00:00
" call Decho("mkdir(dirpath<".dirpath.">,p)")
2005-12-29 22:48:26 +00:00
call mkdir(dirpath,"p")
endif
2005-09-16 21:55:43 +00:00
if zipfile !~ '/'
let zipfile= curdir.'/'.zipfile
endif
" call Decho("zipfile<".zipfile."> fname<".fname.">")
2006-04-05 20:41:53 +00:00
exe "w! ".escape(fname,s:zipfile_escape)
2005-09-16 21:55:43 +00:00
if executable("cygpath")
let zipfile = substitute(system("cygpath ".zipfile),'\n','','e')
endif
2006-04-05 20:41:53 +00:00
" call Decho("zip -u '".zipfile.".zip' '".fname."'")
call system("zip -u '".zipfile.".zip' '".fname."'")
2005-09-16 21:55:43 +00:00
if v:shell_error != 0
echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None
call inputsave()|call input("Press <cr> to continue")|call inputrestore()
2005-11-23 21:25:05 +00:00
elseif s:zipfile_{winnr()} =~ '^\a\+://'
" support writing zipfiles across a network
let netzipfile= s:zipfile_{winnr()}
" call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">")
1split|enew
let binkeep= &binary
let eikeep = &ei
set binary ei=all
exe "e! ".zipfile.".zip"
call netrw#NetWrite(netzipfile)
let &ei = eikeep
let &binary = binkeep
q!
unlet s:zipfile_{winnr()}
2005-09-16 21:55:43 +00:00
endif
" cleanup and restore current directory
cd ..
call s:Rmdir("_ZIPVIM_")
exe "cd ".escape(curdir,' \')
setlocal nomod
2005-11-28 23:01:53 +00:00
let &report= repkeep
2005-09-16 21:55:43 +00:00
" call Dret("zip#Write")
endfun
" ---------------------------------------------------------------------
" Rmdir: {{{2
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 And Restoration: {{{1
let &cpo= s:keepcpo
unlet s:keepcpo
" vim:ts=8 fdm=marker