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