1" zip.vim: Handles browsing zipfiles 2" AUTOLOAD PORTION 3" Date: Dec 21, 2005 4" Version: 6 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= "v6" 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 202 if fname =~ '/' 203 let dirpath = substitute(fname,'/[^/]\+$','','e') 204 if executable("cygpath") 205 let dirpath = substitute(system("cygpath ".dirpath),'\n','','e') 206 endif 207 call mkdir(dirpath,"p") 208 endif 209 if zipfile !~ '/' 210 let zipfile= curdir.'/'.zipfile 211 endif 212" call Decho("zipfile<".zipfile."> fname<".fname.">") 213 214 exe "w! ".fname 215 if executable("cygpath") 216 let zipfile = substitute(system("cygpath ".zipfile),'\n','','e') 217 endif 218 219" call Decho("zip -u ".zipfile.".zip ".fname) 220 call system("zip -u ".zipfile.".zip ".fname) 221 if v:shell_error != 0 222 echohl Error | echo "***error*** (zip#Write) sorry, unable to update ".zipfile." with ".fname | echohl None 223 call inputsave()|call input("Press <cr> to continue")|call inputrestore() 224 225 elseif s:zipfile_{winnr()} =~ '^\a\+://' 226 " support writing zipfiles across a network 227 let netzipfile= s:zipfile_{winnr()} 228" call Decho("handle writing <".zipfile.".zip> across network as <".netzipfile.">") 229 1split|enew 230 let binkeep= &binary 231 let eikeep = &ei 232 set binary ei=all 233 exe "e! ".zipfile.".zip" 234 call netrw#NetWrite(netzipfile) 235 let &ei = eikeep 236 let &binary = binkeep 237 q! 238 unlet s:zipfile_{winnr()} 239 endif 240 241 " cleanup and restore current directory 242 cd .. 243 call s:Rmdir("_ZIPVIM_") 244 exe "cd ".escape(curdir,' \') 245 setlocal nomod 246 247 let &report= repkeep 248" call Dret("zip#Write") 249endfun 250 251" --------------------------------------------------------------------- 252" Rmdir: {{{2 253fun! s:Rmdir(fname) 254" call Dfunc("Rmdir(fname<".a:fname.">)") 255 if has("unix") 256 call system("/bin/rm -rf ".a:fname) 257 elseif has("win32") || has("win95") || has("win64") || has("win16") 258 if &shell =~? "sh$" 259 call system("/bin/rm -rf ".a:fname) 260 else 261 call system("del /S ".a:fname) 262 endif 263 endif 264" call Dret("Rmdir") 265endfun 266 267" ------------------------------------------------------------------------ 268" Modelines And Restoration: {{{1 269let &cpo= s:keepcpo 270unlet s:keepcpo 271" vim:ts=8 fdm=marker 272