1" --------------------------------------------------------------------- 2" getscript.vim 3" Author: Charles E. Campbell, Jr. 4" Date: May 05, 2007 5" Version: 25 6" Installing: :help glvs-install 7" Usage: :help glvs 8" 9" GetLatestVimScripts: 642 1 :AutoInstall: getscript.vim 10" --------------------------------------------------------------------- 11" Initialization: {{{1 12" if you're sourcing this file, surely you can't be 13" expecting vim to be in its vi-compatible mode 14if &cp 15 echoerr "GetLatestVimScripts is not vi-compatible; not loaded (you need to set nocp)" 16 finish 17endif 18let s:keepcpo = &cpo 19set cpo&vim 20"DechoTabOn 21 22if exists("g:loaded_getscript") 23 finish 24endif 25let g:loaded_getscript= "v25" 26 27" --------------------------------------------------------------------- 28" Global Variables: {{{1 29" allow user to change the command for obtaining scripts (does fetch work?) 30if !exists("g:GetLatestVimScripts_wget") 31 if executable("wget") 32 let g:GetLatestVimScripts_wget= "wget" 33 elseif executable("curl") 34 let g:GetLatestVimScripts_wget= "curl" 35 else 36 let g:GetLatestVimScripts_wget = 'echo "GetLatestVimScripts needs wget or curl"' 37 let g:GetLatestVimScripts_options = "" 38 endif 39endif 40 41" options that wget and curl require: 42if !exists("g:GetLatestVimScripts_options") 43 if g:GetLatestVimScripts_wget == "wget" 44 let g:GetLatestVimScripts_options= "-q -O" 45 elseif g:GetLatestVimScripts_wget == "curl" 46 let g:GetLatestVimScripts_options= "-s -O" 47 else 48 let g:GetLatestVimScripts_options= "" 49 endif 50endif 51 52" by default, allow autoinstall lines to work 53if !exists("g:GetLatestVimScripts_allowautoinstall") 54 let g:GetLatestVimScripts_allowautoinstall= 1 55endif 56 57"" For debugging: 58"let g:GetLatestVimScripts_wget = "echo" 59"let g:GetLatestVimScripts_options = "options" 60 61" --------------------------------------------------------------------- 62" Check If AutoInstall Capable: {{{1 63let s:autoinstall= "" 64if g:GetLatestVimScripts_allowautoinstall 65 66 if (has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")) && &shell != "bash" 67 " windows (but not cygwin/bash) 68 let s:dotvim= "vimfiles" 69 if !exists("g:GetLatestVimScripts_mv") 70 let g:GetLatestVimScripts_mv= "ren" 71 endif 72 73 else 74 " unix 75 let s:dotvim= ".vim" 76 if !exists("g:GetLatestVimScripts_mv") 77 let g:GetLatestVimScripts_mv= "mv" 78 endif 79 endif 80 81 if exists('$HOME') && isdirectory(expand("$HOME")."/".s:dotvim) 82 let s:autoinstall= $HOME."/".s:dotvim 83 endif 84" call Decho("s:autoinstall<".s:autoinstall.">") 85"else "Decho 86" call Decho("g:GetLatestVimScripts_allowautoinstall=".g:GetLatestVimScripts_allowautoinstall.": :AutoInstall: disabled") 87endif 88 89" --------------------------------------------------------------------- 90" Public Interface: {{{1 91com! -nargs=0 GetLatestVimScripts call getscript#GetLatestVimScripts() 92com! -nargs=0 GetScript call getscript#GetLatestVimScripts() 93silent! com -nargs=0 GLVS call getscript#GetLatestVimScripts() 94 95" --------------------------------------------------------------------- 96" GetOneScript: (Get Latest Vim Script) this function operates {{{1 97" on the current line, interpreting two numbers and text as 98" ScriptID, SourceID, and Filename. 99" It downloads any scripts that have newer versions from vim.sf.net. 100fun! s:GetOneScript(...) 101" call Dfunc("GetOneScript()") 102 103 " set options to allow progress to be shown on screen 104 let t_ti= &t_ti 105 let t_te= &t_te 106 let rs = &rs 107 set t_ti= t_te= nors 108 109 " put current line on top-of-screen and interpret it into 110 " a script identifer : used to obtain webpage 111 " source identifier : used to identify current version 112 " and an associated comment: used to report on what's being considered 113 if a:0 >= 3 114 let scriptid = a:1 115 let srcid = a:2 116 let fname = a:3 117 let cmmnt = "" 118" call Decho("scriptid<".scriptid.">") 119" call Decho("srcid <".srcid.">") 120" call Decho("fname <".fname.">") 121 else 122 let curline = getline(".") 123 if curline =~ '^\s*#' 124" call Dret("GetOneScript : skipping a pure comment line") 125 return 126 endif 127 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$' 128 try 129 let scriptid = substitute(curline,parsepat,'\1','e') 130 catch /^Vim\%((\a\+)\)\=:E486/ 131 let scriptid= 0 132 endtry 133 try 134 let srcid = substitute(curline,parsepat,'\2','e') 135 catch /^Vim\%((\a\+)\)\=:E486/ 136 let srcid= 0 137 endtry 138 try 139 let fname= substitute(curline,parsepat,'\3','e') 140 catch /^Vim\%((\a\+)\)\=:E486/ 141 let fname= "" 142 endtry 143 try 144 let cmmnt= substitute(curline,parsepat,'\4','e') 145 catch /^Vim\%((\a\+)\)\=:E486/ 146 let cmmnt= "" 147 endtry 148" call Decho("curline <".curline.">") 149" call Decho("parsepat<".parsepat.">") 150" call Decho("scriptid<".scriptid.">") 151" call Decho("srcid <".srcid.">") 152" call Decho("fname <".fname.">") 153 endif 154 155 if scriptid == 0 || srcid == 0 156 " When looking for :AutoInstall: lines, skip scripts that 157 " have 0 0 scriptname 158" call Dret("GetOneScript : skipping a scriptid==srcid==0 line") 159 return 160 endif 161 162 let doautoinstall= 0 163 if fname =~ ":AutoInstall:" 164" call Decho("fname<".fname."> has :AutoInstall:...") 165 let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','') 166" call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall) 167 if s:autoinstall != "" 168 let doautoinstall = g:GetLatestVimScripts_allowautoinstall 169 endif 170 else 171 let aicmmnt= fname 172 endif 173" call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall) 174 175 exe "norm z\<CR>" 176 redraw! 177" call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid) 178 echomsg 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid 179 180 " grab a copy of the plugin's vim.sf.net webpage 181 let scriptaddr = 'http://vim.sf.net/script.php?script_id='.scriptid 182 let tmpfile = tempname() 183 let v:errmsg = "" 184 185 " make three tries at downloading the description 186 let itry= 1 187 while itry <= 3 188" call Decho("try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr) 189 if has("win32") || has("win16") || has("win95") 190" call Decho("silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile.' "'.scriptaddr.'"') 191 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile.' "'.scriptaddr.'"' 192 else 193" call Decho("silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile." '".scriptaddr."'") 194 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile." '".scriptaddr."'" 195 endif 196 if itry == 1 197 exe "silent vsplit ".tmpfile 198 else 199 silent! e % 200 endif 201 202 " find the latest source-id in the plugin's webpage 203 silent! 1 204 let findpkg= search('Click on the package to download','W') 205 if findpkg > 0 206 break 207 endif 208 let itry= itry + 1 209 endwhile 210" call Decho(" --- end downloading tries while loop --- itry=".itry) 211 212 " testing: did finding /Click on the package.../ fail? 213 if findpkg == 0 || itry >= 4 214 silent q! 215 call delete(tmpfile) 216 " restore options 217 let &t_ti = t_ti 218 let &t_te = t_te 219 let &rs = rs 220 let s:downerrors = s:downerrors + 1 221" call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">") 222 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">" 223" call Dret("GetOneScript : srch for /Click on the package/ failed") 224 return 225 endif 226" call Decho('found "Click on the package to download"') 227 228 let findsrcid= search('src_id=','W') 229 if findsrcid == 0 230 silent q! 231 call delete(tmpfile) 232 " restore options 233 let &t_ti = t_ti 234 let &t_te = t_te 235 let &rs = rs 236 let s:downerrors = s:downerrors + 1 237" call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">") 238 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">" 239" call Dret("GetOneScript : srch for /src_id/ failed") 240 return 241 endif 242" call Decho('found "src_id=" in description page') 243 244 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$' 245 let latestsrcid= substitute(getline("."),srcidpat,'\1','') 246 let fname = substitute(getline("."),srcidpat,'\2','') 247" call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> fname<".fname.">") 248 silent q! 249 call delete(tmpfile) 250 251 " convert the strings-of-numbers into numbers 252 let srcid = srcid + 0 253 let latestsrcid = latestsrcid + 0 254" call Decho("srcid=".srcid." latestsrcid=".latestsrcid." fname<".fname.">") 255 256 " has the plugin's most-recent srcid increased, which indicates 257 " that it has been updated 258 if latestsrcid > srcid 259 let s:downloads= s:downloads + 1 260 if fname == bufname("%") 261 " GetLatestVimScript has to be careful about downloading itself 262 let fname= "NEW_".fname 263 endif 264 265 " the plugin has been updated since we last obtained it, so download a new copy 266" call Decho("...downloading new <".fname.">") 267 echomsg "...downloading new <".fname.">" 268 if has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95") 269" call Decho("windows: silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname.' "'.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.'"') 270 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname.' "'.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.'"' 271 else 272" call Decho("unix: silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname." '".'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid."'") 273 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname." '".'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid."'" 274 endif 275 276 " AutoInstall: only if doautoinstall is so indicating 277 if doautoinstall 278" call Decho("attempting to do autoinstall: getcwd<".getcwd()."> filereadable(".fname.")=".filereadable(fname)) 279 if filereadable(fname) 280" call Decho("move <".fname."> to ".s:autoinstall) 281 exe "silent !".g:GetLatestVimScripts_mv." ".fname." ".s:autoinstall 282 let curdir= escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #") 283" call Decho("exe cd ".s:autoinstall) 284 exe "cd ".s:autoinstall 285 286 " decompress 287 if fname =~ '\.bz2$' 288" call Decho("attempt to bunzip2 ".fname) 289 exe "silent !bunzip2 ".fname 290 let fname= substitute(fname,'\.bz2$','','') 291" call Decho("new fname<".fname."> after bunzip2") 292 elseif fname =~ '\.gz$' 293" call Decho("attempt to gunzip ".fname) 294 exe "silent !gunzip ".fname 295 let fname= substitute(fname,'\.gz$','','') 296" call Decho("new fname<".fname."> after gunzip") 297 endif 298 299 " distribute archive(.zip, .tar, .vba) contents 300 if fname =~ '\.zip$' 301" call Decho("attempt to unzip ".fname) 302 exe "silent !unzip -o ".fname 303 elseif fname =~ '\.tar$' 304" call Decho("attempt to untar ".fname) 305 exe "silent !tar -xvf ".fname 306 elseif fname =~ '\.vba$' 307" call Decho("attempt to handle a vimball: ".fname) 308 1split 309 exe "e ".fname 310 so % 311 q 312 endif 313 314 if fname =~ '.vim$' 315" call Decho("attempt to simply move ".fname." to plugin") 316 exe "silent !".g:GetLatestVimScripts_mv." ".fname." plugin" 317 endif 318 319 " helptags step 320 let docdir= substitute(&rtp,',.*','','e')."/doc" 321" call Decho("helptags docdir<".docdir.">") 322 exe "helptags ".docdir 323 exe "cd ".curdir 324 endif 325 endif 326 327 " update the data in the <GetLatestVimScripts.dat> file 328 let modline=scriptid." ".latestsrcid." ".fname.cmmnt 329 call setline(line("."),modline) 330" call Decho("update data in ".expand("%")."#".line(".").": modline<".modline.">") 331 endif 332 333 " restore options 334 let &t_ti= t_ti 335 let &t_te= t_te 336 let &rs = rs 337 338" call Dret("GetOneScript") 339endfun 340 341" --------------------------------------------------------------------- 342" GetLatestVimScripts: this function gets the latest versions of {{{1 343" scripts based on the list in 344" (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat 345fun! getscript#GetLatestVimScripts() 346" call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">") 347 348" insure that wget is executable 349 if executable(g:GetLatestVimScripts_wget) != 1 350 echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system" 351" call Dret("GetLatestVimScripts : wget not executable/availble") 352 return 353 endif 354 355 " Find the .../GetLatest subdirectory under the runtimepath 356 for datadir in split(&rtp,',') + [''] 357 if isdirectory(datadir."/GetLatest") 358" call Decho("found directory<".datadir.">") 359 let datadir= datadir . "/GetLatest" 360 break 361 endif 362 if filereadable(datadir."GetLatestVimScripts.dat") 363" call Decho("found ".datadir."/GetLatestVimScripts.dat") 364 break 365 endif 366 endfor 367 368 " Sanity checks: readability and writability 369 if datadir == "" 370 echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install' 371" call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory") 372 return 373 endif 374 375 if filewritable(datadir) != 2 376 echoerr "(getLatestVimScripts) Your ".datadir." isn't writable" 377" call Dret("GetLatestVimScripts : non-writable directory<".datadir.">") 378 return 379 endif 380 let datafile= datadir."/GetLatestVimScripts.dat" 381 if !filereadable(datafile) 382 echoerr "Your data file<".datafile."> isn't readable" 383" call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">") 384 return 385 endif 386 if !filewritable(datafile) 387 echoerr "Your data file<".datafile."> isn't writable" 388" call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">") 389 return 390 endif 391" call Decho("datadir <".datadir.">") 392" call Decho("datafile <".datafile.">") 393 394 " don't let any events interfere (like winmanager's, taglist's, etc) 395 let eikeep= &ei 396 set ei=all 397 398 " record current directory, change to datadir, open split window with 399 " datafile 400 let origdir= getcwd() 401 exe "cd ".escape(substitute(datadir,'\','/','ge'),"|[]*'\" #") 402 split 403 exe "e ".escape(substitute(datafile,'\','/','ge'),"|[]*'\" #") 404 res 1000 405 let s:downloads = 0 406 let s:downerrors= 0 407 408 " Check on dependencies mentioned in plugins 409" call Decho(" ") 410" call Decho("searching plugins for GetLatestVimScripts dependencies") 411 let lastline = line("$") 412" call Decho("lastline#".lastline) 413 let plugins = split(globpath(&rtp,"plugin/*.vim")) 414 let foundscript = 0 415 let firstdir= "" 416 417 for plugin in plugins 418 419 " don't process plugins in system directories 420 if firstdir == "" 421 let firstdir= substitute(plugin,'[/\\][^/\\]\+$','','') 422" call Decho("firstdir<".firstdir.">") 423 else 424 let curdir= substitute(plugin,'[/\\][^/\\]\+$','','') 425" call Decho("curdir<".curdir.">") 426 if curdir != firstdir 427 break 428 endif 429 endif 430 431 " read plugin in 432 $ 433" call Decho(" ") 434" call Decho(".dependency checking<".plugin."> line$=".line("$")) 435 exe "silent r ".plugin 436 437 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0 438 let newscript= substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e') 439 let llp1 = lastline+1 440" call Decho("..newscript<".newscript.">") 441 442 " don't process ""GetLatestVimScripts lines 443 if newscript !~ '^"' 444 " found a "GetLatestVimScripts: # #" line in the script; check if its already in the datafile 445 let curline = line(".") 446 let noai_script = substitute(newscript,'\s*:AutoInstall:\s*','','e') 447 exe llp1 448 let srchline = search('\<'.noai_script.'\>','bW') 449" call Decho("..noai_script<".noai_script."> srch=".srchline."curline#".line(".")." lastline#".lastline) 450 451 if srchline == 0 452 " found a new script to permanently include in the datafile 453 let keep_rega = @a 454 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','') 455 exe lastline."put a" 456 echomsg "Appending <".@a."> to ".datafile." for ".newscript 457" call Decho("..APPEND (".noai_script.")<".@a."> to GetLatestVimScripts.dat") 458 let @a = keep_rega 459 let lastline = llp1 460 let curline = curline + 1 461 let foundscript = foundscript + 1 462" else " Decho 463" call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")") 464 endif 465 466 let curline = curline + 1 467 exe curline 468 endif 469 endwhile 470 471 let llp1= lastline + 1 472" call Decho(".deleting lines: ".llp1.",$d") 473 exe "silent! ".llp1.",$d" 474 endfor 475" call Decho("--- end dependency checking loop --- foundscript=".foundscript) 476" call Decho(" ") 477 478 if foundscript == 0 479 set nomod 480 endif 481 482 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat 483" call Decho("begin: checking out-of-date scripts using datafile<".datafile.">") 484 set lz 485 1 486" /^-----/,$g/^\s*\d/call Decho(getline(".")) 487 1 488 /^-----/,$g/^\s*\d/call s:GetOneScript() 489" call Decho("--- end out-of-date checking --- ") 490 491 " Final report (an echomsg) 492 try 493 silent! ?^-------? 494 catch /^Vim\%((\a\+)\)\=:E114/ 495" call Dret("GetLatestVimScripts : nothing done!") 496 return 497 endtry 498 exe "norm! kz\<CR>" 499 redraw! 500 let s:msg = "" 501 if s:downloads == 1 502 let s:msg = "Downloaded one updated script to <".datadir.">" 503 elseif s:downloads == 2 504 let s:msg= "Downloaded two updated scripts to <".datadir.">" 505 elseif s:downloads > 1 506 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">" 507 else 508 let s:msg= "Everything was already current" 509 endif 510 if s:downerrors > 0 511 let s:msg= s:msg." (".s:downerrors." downloading errors)" 512 endif 513 echomsg s:msg 514 " save the file 515 if &mod 516 silent! w! 517 endif 518 q 519 520 " restore events and current directory 521 exe "cd ".escape(substitute(origdir,'\','/','ge'),"|[]*'\" #") 522 let &ei= eikeep 523 set nolz 524" call Dret("GetLatestVimScripts : did ".s:downloads." downloads") 525endfun 526" --------------------------------------------------------------------- 527 528" Restore Options: {{{1 529let &cpo= s:keepcpo 530 531" vim: ts=8 sts=2 fdm=marker nowrap 532