1" zip.vim: Handles browsing zipfiles 2" AUTOLOAD PORTION 3" Date: Sep 16, 2005 4" Version: 2 5" Maintainer: Charles E Campbell, Jr <drchipNOSPAM at campbellfamily dot biz> 6" License: Vim License (see vim's :help license) 7" Copyright: Copyright (C) 2005 Charles E. Campbell, Jr. {{{1 8" Permission is hereby granted to use and distribute this code, 9" with or without modifications, provided that this copyright 10" notice is copied with it. Like anything else that's free, 11" zipPlugin.vim is provided *as is* and comes with no warranty 12" of any kind, either expressed or implied. By using this 13" plugin, you agree that in no event will the copyright 14" holder be liable for any damages resulting from the use 15" of this software. 16 17" --------------------------------------------------------------------- 18" Initialization: {{{1 19let s:keepcpo= &cpo 20set cpo&vim 21if exists("g:loaded_zip") 22 finish 23endif 24 25let g:loaded_zip= "v2" 26 27" ---------------- 28" Functions: {{{1 29" ---------------- 30 31" --------------------------------------------------------------------- 32" zip#Browse: {{{2 33fun! zip#Browse(zipfile) 34" call Dfunc("zip#Browse(zipfile<".a:zipfile.">)") 35 36 " sanity checks 37 if !executable("unzip") 38 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system" 39 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 40" call Dret("zip#Browse") 41 return 42 endif 43 if !filereadable(a:zipfile) 44 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None 45 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 46" call Dret("zip#Browse") 47 return 48 endif 49 if &ma != 1 50 set ma 51 endif 52 let w:zipfile= a:zipfile 53 54 setlocal noswapfile 55 setlocal buftype=nofile 56 setlocal bufhidden=hide 57 setlocal nobuflisted 58 setlocal nowrap 59 set ft=tar 60 61 " give header 62 exe "$put ='".'\"'." zip.vim version ".g:loaded_zip."'" 63 exe "$put ='".'\"'." Browsing zipfile ".a:zipfile."'" 64 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'" 65 $put ='' 66 0d 67 $ 68 69 exe "silent r! unzip -l ".a:zipfile 70 $d 71 silent 4,$v/^\s\+\d\+\s\{0,5}\d/d 72 silent 4,$s/^\%(.*\)\s\+\(\S\)/\1/ 73 74 setlocal noma nomod ro 75 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr> 76 77" call Dret("zip#Browse") 78endfun 79 80" --------------------------------------------------------------------- 81" ZipBrowseSelect: {{{2 82fun! s:ZipBrowseSelect() 83" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile.">") 84 let fname= getline(".") 85 86 " sanity check 87 if fname =~ '^"' 88" call Dret("ZipBrowseSelect") 89 return 90 endif 91 if fname =~ '/$' 92 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None 93 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 94" call Dret("ZipBrowseSelect") 95 return 96 endif 97 98" call Decho("fname<".fname.">") 99 100 " get zipfile to the new-window 101 let zipfile= substitute(w:zipfile,'.zip$','','e') 102 103 new 104 wincmd _ 105 exe "e zipfile:".zipfile.':'.fname 106 filetype detect 107 108" call Dret("ZipBrowseSelect") 109endfun 110 111" --------------------------------------------------------------------- 112" zip#Read: {{{2 113fun! zip#Read(fname,mode) 114" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")") 115 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','') 116 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','') 117" call Decho("zipfile<".zipfile."> fname<".fname.">") 118 119 exe "r! unzip -p ".zipfile." ".fname 120 121 " cleanup 122 0d 123 set nomod 124 125" call Dret("zip#Read") 126endfun 127 128" --------------------------------------------------------------------- 129" zip#Write: {{{2 130fun! zip#Write(fname) 131" call Dfunc("zip#Write(fname<".a:fname.")") 132 133 " sanity checks 134 if !executable("zip") 135 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None 136 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 137" call Dret("zip#Write") 138 return 139 endif 140 if !exists("*mkdir") 141 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None 142 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 143" call Dret("zip#Write") 144 return 145 endif 146 147 let curdir= getcwd() 148 let tmpdir= tempname() 149" call Decho("orig tempname<".tmpdir.">") 150 if tmpdir =~ '\.' 151 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e') 152 endif 153" call Decho("tmpdir<".tmpdir.">") 154 call mkdir(tmpdir,"p") 155 156 " attempt to change to the indicated directory 157 try 158 exe "cd ".escape(tmpdir,' \') 159 catch /^Vim\%((\a\+)\)\=:E344/ 160 echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | Echohl None 161 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 162" call Dret("zip#Write") 163 return 164 endtry 165" call Decho("current directory now: ".getcwd()) 166 167 " place temporary files under .../_ZIPVIM_/ 168 if isdirectory("_ZIPVIM_") 169 call s:Rmdir("_ZIPVIM_") 170 endif 171 call mkdir("_ZIPVIM_") 172 cd _ZIPVIM_ 173" call Decho("current directory now: ".getcwd()) 174 175 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','') 176 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','') 177 let dirpath = substitute(fname,'/[^/]\+$','','e') 178 if zipfile !~ '/' 179 let zipfile= curdir.'/'.zipfile 180 endif 181" call Decho("zipfile<".zipfile."> fname<".fname.">") 182 183 call mkdir(dirpath,"p") 184 exe "w! ".fname 185 if executable("cygpath") 186 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e') 187 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e') 188 endif 189 190" call Decho("zip -u ".zipfile.".zip ".fname) 191 call system("zip -u ".zipfile.".zip ".fname) 192 if v:shell_error != 0 193 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None 194 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 195 endif 196 197 " cleanup and restore current directory 198 cd .. 199 call s:Rmdir("_ZIPVIM_") 200 exe "cd ".escape(curdir,' \') 201 setlocal nomod 202 203" call Dret("zip#Write") 204endfun 205 206" --------------------------------------------------------------------- 207" Rmdir: {{{2 208fun! s:Rmdir(fname) 209" call Dfunc("Rmdir(fname<".a:fname.">)") 210 if has("unix") 211 call system("/bin/rm -rf ".a:fname) 212 elseif has("win32") || has("win95") || has("win64") || has("win16") 213 if &shell =~? "sh$" 214 call system("/bin/rm -rf ".a:fname) 215 else 216 call system("del /S ".a:fname) 217 endif 218 endif 219" call Dret("Rmdir") 220endfun 221 222" ------------------------------------------------------------------------ 223" Modelines And Restoration: {{{1 224let &cpo= s:keepcpo 225unlet s:keepcpo 226" vim:ts=8 fdm=marker 227