1" zip.vim: Handles browsing zipfiles 2" AUTOLOAD PORTION 3" Date: Nov 28, 2005 4" Version: 5 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= "v5" 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 let repkeep= &report 36 set report=10 37 38 " sanity checks 39 if !executable("unzip") 40 echohl Error | echo "***error*** (zip#Browse) unzip not available on your system" 41 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 42 let &report= repkeep 43" call Dret("zip#Browse") 44 return 45 endif 46 if !filereadable(a:zipfile) 47 if a:zipfile !~# '^\a\+://' 48 " if its an url, don't complain, let url-handlers such as vim do its thing 49 echohl Error | echo "***error*** (zip#Browse) File not readable<".a:zipfile.">" | echohl None 50 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 51 endif 52 let &report= repkeep 53" call Dret("zip#Browse : file<".a:zipfile."> not readable") 54 return 55 endif 56 if &ma != 1 57 set ma 58 endif 59 let w:zipfile= a:zipfile 60 61 setlocal noswapfile 62 setlocal buftype=nofile 63 setlocal bufhidden=hide 64 setlocal nobuflisted 65 setlocal nowrap 66 set ft=tar 67 68 " give header 69 exe "$put ='".'\"'." zip.vim version ".g:loaded_zip."'" 70 exe "$put ='".'\"'." Browsing zipfile ".a:zipfile."'" 71 exe "$put ='".'\"'." Select a file with cursor and press ENTER"."'" 72 $put ='' 73 0d 74 $ 75 76 exe "silent r! unzip -l ".a:zipfile 77 $d 78 silent 4,$v/^\s\+\d\+\s\{0,5}\d/d 79 silent 4,$s/^\%(.*\)\s\+\(\S\)/\1/ 80 81 setlocal noma nomod ro 82 noremap <silent> <buffer> <cr> :call <SID>ZipBrowseSelect()<cr> 83 84 let &report= repkeep 85" call Dret("zip#Browse") 86endfun 87 88" --------------------------------------------------------------------- 89" ZipBrowseSelect: {{{2 90fun! s:ZipBrowseSelect() 91" call Dfunc("ZipBrowseSelect() zipfile<".w:zipfile."> curfile<".expand("%").">") 92 let repkeep= &report 93 set report=10 94 let fname= getline(".") 95 96 " sanity check 97 if fname =~ '^"' 98 let &report= repkeep 99" call Dret("ZipBrowseSelect") 100 return 101 endif 102 if fname =~ '/$' 103 echohl Error | echo "***error*** (zip#Browse) Please specify a file, not a directory" | echohl None 104 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 105 let &report= repkeep 106" call Dret("ZipBrowseSelect") 107 return 108 endif 109 110" call Decho("fname<".fname.">") 111 112 " get zipfile to the new-window 113 let zipfile= substitute(w:zipfile,'.zip$','','e') 114 let curfile= expand("%") 115 116 new 117 wincmd _ 118 let s:zipfile_{winnr()}= curfile 119 exe "e zipfile:".zipfile.':'.fname 120 filetype detect 121 122 let &report= repkeep 123" call Dret("ZipBrowseSelect : s:zipfile_".winnr()."<".s:zipfile_{winnr()}.">") 124endfun 125 126" --------------------------------------------------------------------- 127" zip#Read: {{{2 128fun! zip#Read(fname,mode) 129" call Dfunc("zip#Read(fname<".a:fname.">,mode=".a:mode.")") 130 let repkeep= &report 131 set report=10 132 133 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','') 134 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','') 135" call Decho("zipfile<".zipfile."> fname<".fname.">") 136 137 exe "r! unzip -p ".zipfile." ".fname 138 139 " cleanup 140 0d 141 set nomod 142 143 let &report= repkeep 144" call Dret("zip#Read") 145endfun 146 147" --------------------------------------------------------------------- 148" zip#Write: {{{2 149fun! zip#Write(fname) 150" call Dfunc("zip#Write(fname<".a:fname.") zipfile_".winnr()."<".s:zipfile_{winnr()}.">") 151 let repkeep= &report 152 set report=10 153 154 " sanity checks 155 if !executable("zip") 156 echohl Error | echo "***error*** (zip#Write) sorry, your system doesn't appear to have the zip pgm" | echohl None 157 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 158 let &report= repkeep 159" call Dret("zip#Write") 160 return 161 endif 162 if !exists("*mkdir") 163 echohl Error | echo "***error*** (zip#Write) sorry, mkdir() doesn't work on your system" | echohl None 164 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 165 let &report= repkeep 166" call Dret("zip#Write") 167 return 168 endif 169 170 let curdir= getcwd() 171 let tmpdir= tempname() 172" call Decho("orig tempname<".tmpdir.">") 173 if tmpdir =~ '\.' 174 let tmpdir= substitute(tmpdir,'\.[^.]*$','','e') 175 endif 176" call Decho("tmpdir<".tmpdir.">") 177 call mkdir(tmpdir,"p") 178 179 " attempt to change to the indicated directory 180 try 181 exe "cd ".escape(tmpdir,' \') 182 catch /^Vim\%((\a\+)\)\=:E344/ 183 echohl Error | echo "***error*** (zip#Write) cannot cd to temporary directory" | Echohl None 184 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 185 let &report= repkeep 186" call Dret("zip#Write") 187 return 188 endtry 189" call Decho("current directory now: ".getcwd()) 190 191 " place temporary files under .../_ZIPVIM_/ 192 if isdirectory("_ZIPVIM_") 193 call s:Rmdir("_ZIPVIM_") 194 endif 195 call mkdir("_ZIPVIM_") 196 cd _ZIPVIM_ 197" call Decho("current directory now: ".getcwd()) 198 199 let zipfile = substitute(a:fname,'zipfile:\(.\{-}\):.*$','\1','') 200 let fname = substitute(a:fname,'zipfile:.\{-}:\(.*\)$','\1','') 201 let dirpath = substitute(fname,'/[^/]\+$','','e') 202 if zipfile !~ '/' 203 let zipfile= curdir.'/'.zipfile 204 endif 205" call Decho("zipfile<".zipfile."> fname<".fname.">") 206 207 call mkdir(dirpath,"p") 208 exe "w! ".fname 209 if executable("cygpath") 210 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e') 211 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e') 212 endif 213 214" call Decho("zip -u ".zipfile.".zip ".fname) 215 call system("zip -u ".zipfile.".zip ".fname) 216 if v:shell_error != 0 217 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None 218 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 219 220 elseif s:zipfile_{winnr()} =~ '^\a\+://' 221 " support writing zipfiles across a network 222 let netzipfile= s:zipfile_{winnr()} 223" call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">") 224 1split|enew 225 let binkeep= &binary 226 let eikeep = &ei 227 set binary ei=all 228 exe "e! ".zipfile.".zip" 229 call netrw#NetWrite(netzipfile) 230 let &ei = eikeep 231 let &binary = binkeep 232 q! 233 unlet s:zipfile_{winnr()} 234 endif 235 236 " cleanup and restore current directory 237 cd .. 238 call s:Rmdir("_ZIPVIM_") 239 exe "cd ".escape(curdir,' \') 240 setlocal nomod 241 242 let &report= repkeep 243" call Dret("zip#Write") 244endfun 245 246" --------------------------------------------------------------------- 247" Rmdir: {{{2 248fun! s:Rmdir(fname) 249" call Dfunc("Rmdir(fname<".a:fname.">)") 250 if has("unix") 251 call system("/bin/rm -rf ".a:fname) 252 elseif has("win32") || has("win95") || has("win64") || has("win16") 253 if &shell =~? "sh$" 254 call system("/bin/rm -rf ".a:fname) 255 else 256 call system("del /S ".a:fname) 257 endif 258 endif 259" call Dret("Rmdir") 260endfun 261 262" ------------------------------------------------------------------------ 263" Modelines And Restoration: {{{1 264let &cpo= s:keepcpo 265unlet s:keepcpo 266" vim:ts=8 fdm=marker 267