1 " ---------------------------------------------------------------------
3 " Author: Charles E. Campbell, Jr.
6 " Installing: :help glvs-install
9 " GetLatestVimScripts: 642 1 :AutoInstall: getscript.vim
10 "redraw!|call inputsave()|call input("Press <cr> to continue")|call inputrestore()
11 " ---------------------------------------------------------------------
12 " Initialization: {{{1
13 " if you're sourcing this file, surely you can't be
14 " expecting vim to be in its vi-compatible mode!
15 if exists("g:loaded_getscript")
18 let g:loaded_getscript= "v32"
20 echoerr "GetLatestVimScripts is not vi-compatible; not loaded (you need to set nocp)"
25 echo "***warning*** this version of getscript needs vim 7.2"
33 " ---------------------------
34 " Global Variables: {{{1
35 " ---------------------------
36 " Cygwin Detection ------- {{{2
37 if !exists("g:getscript_cygwin")
38 if has("win32") || has("win95") || has("win64") || has("win16")
39 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
40 let g:getscript_cygwin= 1
42 let g:getscript_cygwin= 0
45 let g:getscript_cygwin= 0
50 if !exists("g:GetLatestVimScripts_wget")
52 let g:GetLatestVimScripts_wget= "wget"
53 elseif executable("curl")
54 let g:GetLatestVimScripts_wget= "curl"
56 let g:GetLatestVimScripts_wget = 'echo "GetLatestVimScripts needs wget or curl"'
57 let g:GetLatestVimScripts_options = ""
61 " options that wget and curl require:
62 if !exists("g:GetLatestVimScripts_options")
63 if g:GetLatestVimScripts_wget == "wget"
64 let g:GetLatestVimScripts_options= "-q -O"
65 elseif g:GetLatestVimScripts_wget == "curl"
66 let g:GetLatestVimScripts_options= "-s -O"
68 let g:GetLatestVimScripts_options= ""
72 " by default, allow autoinstall lines to work
73 if !exists("g:GetLatestVimScripts_allowautoinstall")
74 let g:GetLatestVimScripts_allowautoinstall= 1
78 "let g:GetLatestVimScripts_wget = "echo"
79 "let g:GetLatestVimScripts_options = "options"
81 " ---------------------------------------------------------------------
82 " Check If AutoInstall Capable: {{{1
84 if g:GetLatestVimScripts_allowautoinstall
86 if (has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")) && &shell != "bash"
87 " windows (but not cygwin/bash)
88 let s:dotvim= "vimfiles"
89 if !exists("g:GetLatestVimScripts_mv")
90 let g:GetLatestVimScripts_mv= "ren"
96 if !exists("g:GetLatestVimScripts_mv")
97 let g:GetLatestVimScripts_mv= "mv"
101 if exists("g:GetLatestVimScripts_autoinstalldir") && isdirectory(g:GetLatestVimScripts_autoinstalldir)
102 let s:autoinstall= g:GetLatestVimScripts_autoinstalldir"
103 elseif exists('$HOME') && isdirectory(expand("$HOME")."/".s:dotvim)
104 let s:autoinstall= $HOME."/".s:dotvim
106 " call Decho("s:autoinstall<".s:autoinstall.">")
108 " call Decho("g:GetLatestVimScripts_allowautoinstall=".g:GetLatestVimScripts_allowautoinstall.": :AutoInstall: disabled")
111 " ---------------------------------------------------------------------
112 " Public Interface: {{{1
113 com! -nargs=0 GetLatestVimScripts call getscript#GetLatestVimScripts()
114 com! -nargs=0 GetScript call getscript#GetLatestVimScripts()
115 silent! com -nargs=0 GLVS call getscript#GetLatestVimScripts()
117 " ---------------------------------------------------------------------
118 " GetLatestVimScripts: this function gets the latest versions of {{{1
119 " scripts based on the list in
120 " (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat
121 fun! getscript#GetLatestVimScripts()
122 " call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">")
124 " insure that wget is executable
125 if executable(g:GetLatestVimScripts_wget) != 1
126 echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system"
127 " call Dret("GetLatestVimScripts : wget not executable/availble")
131 " insure that fnameescape() is available
132 if !exists("*fnameescape")
133 echoerr "GetLatestVimScripts needs fnameescape() (provided by 7.1.299 or later)"
137 " Find the .../GetLatest subdirectory under the runtimepath
138 for datadir in split(&rtp,',') + ['']
139 if isdirectory(datadir."/GetLatest")
140 " call Decho("found directory<".datadir.">")
141 let datadir= datadir . "/GetLatest"
144 if filereadable(datadir."GetLatestVimScripts.dat")
145 " call Decho("found ".datadir."/GetLatestVimScripts.dat")
150 " Sanity checks: readability and writability
152 echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install'
153 " call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
156 if filewritable(datadir) != 2
157 echoerr "(getLatestVimScripts) Your ".datadir." isn't writable"
158 " call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
161 let datafile= datadir."/GetLatestVimScripts.dat"
162 if !filereadable(datafile)
163 echoerr "Your data file<".datafile."> isn't readable"
164 " call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
167 if !filewritable(datafile)
168 echoerr "Your data file<".datafile."> isn't writable"
169 " call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
172 " --------------------
173 " Passed sanity checks
174 " --------------------
176 " call Decho("datadir <".datadir.">")
177 " call Decho("datafile <".datafile.">")
179 " don't let any event handlers interfere (like winmanager's, taglist's, etc)
183 set ei=all hls&vim noacd
185 " Edit the datafile (ie. GetLatestVimScripts.dat):
186 " 1. record current directory (origdir),
187 " 2. change directory to datadir,
190 let origdir= getcwd()
191 " call Decho("exe cd ".fnameescape(substitute(datadir,'\','/','ge')))
192 exe "cd ".fnameescape(substitute(datadir,'\','/','ge'))
194 " call Decho("exe e ".fnameescape(substitute(datafile,'\','/','ge')))
195 exe "e ".fnameescape(substitute(datafile,'\','/','ge'))
200 " Check on dependencies mentioned in plugins
202 " call Decho("searching plugins for GetLatestVimScripts dependencies")
203 let lastline = line("$")
204 " call Decho("lastline#".lastline)
205 let firstdir = substitute(&rtp,',.*$','','')
206 let plugins = split(globpath(firstdir,"plugin/*.vim"),'\n')
207 let plugins = plugins + split(globpath(firstdir,"AsNeeded/*.vim"),'\n')
210 " this loop updates the GetLatestVimScripts.dat file
211 " with dependencies explicitly mentioned in the plugins
212 " via GetLatestVimScripts: ... lines
213 " It reads the plugin script at the end of the GetLatestVimScripts.dat
214 " file, examines it, and then removes it.
215 for plugin in plugins
217 " call Decho("plugin<".plugin.">")
220 " evidently a :r creates a new buffer (the "#" buffer) that is subsequently unused -- bwiping it
222 " call Decho(".dependency checking<".plugin."> line$=".line("$"))
223 " call Decho("..exe silent r ".fnameescape(plugin))
224 exe "silent r ".fnameescape(plugin)
225 exe "silent bwipe ".bufnr("#")
227 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
228 let depscript = substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
229 let depscriptid = substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\(\d\+\)\s\+.*$','\1','')
230 let llp1 = lastline+1
231 " call Decho("..depscript<".depscript.">")
233 " found a "GetLatestVimScripts: # #" line in the script;
234 " check if its already in the datafile by searching backwards from llp1,
235 " the (prior to reading in the plugin script) last line plus one of the GetLatestVimScripts.dat file,
236 " for the script-id with no wrapping allowed.
237 let curline = line(".")
238 let noai_script = substitute(depscript,'\s*:AutoInstall:\s*','','e')
240 let srchline = search('^\s*'.depscriptid.'\s\+\d\+\s\+.*$','bW')
242 " this second search is taken when, for example, a 0 0 scriptname is to be skipped over
243 let srchline= search('\<'.noai_script.'\>','bW')
245 " call Decho("..noai_script<".noai_script."> depscriptid#".depscriptid." srchline#".srchline." curline#".line(".")." lastline#".lastline)
248 " found a new script to permanently include in the datafile
250 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
251 echomsg "Appending <".@a."> to ".datafile." for ".depscript
252 " call Decho("..Appending <".@a."> to ".datafile." for ".depscript)
256 let curline = curline + 1
257 let foundscript = foundscript + 1
259 " call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
262 let curline = curline + 1
266 " llp1: last line plus one
267 let llp1= lastline + 1
268 " call Decho(".deleting lines: ".llp1.",$d")
269 exe "silent! ".llp1.",$d"
271 " call Decho("--- end dependency checking loop --- foundscript=".foundscript)
273 " call Dredir("BUFFER TEST (GetLatestVimScripts 1)","ls!")
279 " --------------------------------------------------------------------
280 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
281 " --------------------------------------------------------------------
282 " call Decho("begin: checking out-of-date scripts using datafile<".datafile.">")
285 " /^-----/,$g/^\s*\d/call Decho(getline("."))
287 /^-----/,$g/^\s*\d/call s:GetOneScript()
288 " call Decho("--- end out-of-date checking --- ")
290 " Final report (an echomsg)
293 catch /^Vim\%((\a\+)\)\=:E114/
294 " call Dret("GetLatestVimScripts : nothing done!")
301 let s:msg = "Downloaded one updated script to <".datadir.">"
302 elseif s:downloads == 2
303 let s:msg= "Downloaded two updated scripts to <".datadir.">"
304 elseif s:downloads > 1
305 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
307 let s:msg= "Everything was already current"
310 let s:msg= s:msg." (".s:downerrors." downloading errors)"
319 " restore events and current directory
320 exe "cd ".fnameescape(substitute(origdir,'\','/','ge'))
325 " call Dredir("BUFFER TEST (GetLatestVimScripts 2)","ls!")
326 " call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
329 " ---------------------------------------------------------------------
330 " GetOneScript: (Get Latest Vim Script) this function operates {{{1
331 " on the current line, interpreting two numbers and text as
332 " ScriptID, SourceID, and Filename.
333 " It downloads any scripts that have newer versions from vim.sourceforge.net.
334 fun! s:GetOneScript(...)
335 " call Dfunc("GetOneScript()")
337 " set options to allow progress to be shown on screen
344 " put current line on top-of-screen and interpret it into
345 " a script identifer : used to obtain webpage
346 " source identifier : used to identify current version
347 " and an associated comment: used to report on what's being considered
353 " call Decho("scriptid<".scriptid.">")
354 " call Decho("srcid <".srcid.">")
355 " call Decho("fname <".fname.">")
357 let curline = getline(".")
358 if curline =~ '^\s*#'
360 " call Dret("GetOneScript : skipping a pure comment line")
363 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$'
365 let scriptid = substitute(curline,parsepat,'\1','e')
366 catch /^Vim\%((\a\+)\)\=:E486/
370 let srcid = substitute(curline,parsepat,'\2','e')
371 catch /^Vim\%((\a\+)\)\=:E486/
375 let fname= substitute(curline,parsepat,'\3','e')
376 catch /^Vim\%((\a\+)\)\=:E486/
380 let cmmnt= substitute(curline,parsepat,'\4','e')
381 catch /^Vim\%((\a\+)\)\=:E486/
384 " call Decho("curline <".curline.">")
385 " call Decho("parsepat<".parsepat.">")
386 " call Decho("scriptid<".scriptid.">")
387 " call Decho("srcid <".srcid.">")
388 " call Decho("fname <".fname.">")
391 " plugin author protection from downloading his/her own scripts atop their latest work
392 if scriptid == 0 || srcid == 0
393 " When looking for :AutoInstall: lines, skip scripts that have 0 0 scriptname
395 " call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
400 if fname =~ ":AutoInstall:"
401 " call Decho("case AutoInstall: fname<".fname.">")
402 let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','')
403 " call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
404 if s:autoinstall != ""
405 let doautoinstall = g:GetLatestVimScripts_allowautoinstall
410 " call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
414 " call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
415 echo 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
417 " grab a copy of the plugin's vim.sourceforge.net webpage
418 let scriptaddr = 'http://vim.sourceforge.net/script.php?script_id='.scriptid
419 let tmpfile = tempname()
422 " make up to three tries at downloading the description
425 " call Decho(".try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
426 if has("win32") || has("win16") || has("win95")
427 " call Decho(".new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile).' '.shellescape(scriptaddr)."|bw!")
428 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile).' '.shellescape(scriptaddr)|bw!
430 " call Decho(".exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile)." ".shellescape(scriptaddr))
431 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile)." ".shellescape(scriptaddr)
434 exe "silent vsplit ".fnameescape(tmpfile)
440 " find the latest source-id in the plugin's webpage
442 let findpkg= search('Click on the package to download','W')
448 " call Decho(" --- end downloading tries while loop --- itry=".itry)
450 " testing: did finding "Click on the package..." fail?
451 if findpkg == 0 || itry >= 4
458 let s:downerrors = s:downerrors + 1
459 " call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
460 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
461 " call Dret("GetOneScript : srch for /Click on the package/ failed")
465 " call Decho('found "Click on the package to download"')
467 let findsrcid= search('src_id=','W')
475 let s:downerrors = s:downerrors + 1
476 " call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
477 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
479 " call Dret("GetOneScript : srch for /src_id/ failed")
482 " call Decho('found "src_id=" in description page')
484 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
485 let latestsrcid= substitute(getline("."),srcidpat,'\1','')
486 let sname = substitute(getline("."),srcidpat,'\2','') " script name actually downloaded
487 " call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> sname<".sname.">")
491 " convert the strings-of-numbers into numbers
492 let srcid = srcid + 0
493 let latestsrcid = latestsrcid + 0
494 " call Decho("srcid=".srcid." latestsrcid=".latestsrcid." sname<".sname.">")
496 " has the plugin's most-recent srcid increased, which indicates that it has been updated
497 if latestsrcid > srcid
498 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."]: need to update <".sname.">")
500 let s:downloads= s:downloads + 1
501 if sname == bufname("%")
502 " GetLatestVimScript has to be careful about downloading itself
503 let sname= "NEW_".sname
506 " -----------------------------------------------------------------------------
507 " the plugin has been updated since we last obtained it, so download a new copy
508 " -----------------------------------------------------------------------------
509 " call Decho(".downloading new <".sname.">")
510 echomsg ".downloading new <".sname.">"
511 if has("win32") || has("win16") || has("win95")
512 " call Decho(".new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id='.latestsrcid)."|q")
513 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id='.latestsrcid)|q
515 " call Decho(".exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id='))
516 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id=').latestsrcid
519 " --------------------------------------------------------------------------
520 " AutoInstall: only if doautoinstall has been requested by the plugin itself
521 " --------------------------------------------------------------------------
524 " call Decho("Autoinstall: getcwd<".getcwd()."> filereadable(".sname.")=".filereadable(sname))
525 if filereadable(sname)
526 " call Decho("<".sname."> is readable")
527 " call Decho("exe silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".shellescape(s:autoinstall))
528 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".shellescape(s:autoinstall)
529 let curdir = escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #")
530 let installdir= curdir."/Installed"
531 if !isdirectory(installdir)
532 call mkdir(installdir)
534 " call Decho("curdir<".curdir."> installdir<".installdir.">")
535 " call Decho("exe cd ".fnameescape(s:autoinstall))
536 exe "cd ".fnameescape(s:autoinstall)
538 " determine target directory for moves
539 let firstdir= substitute(&rtp,',.*$','','')
540 let pname = substitute(sname,'\..*','.vim','')
541 " call Decho("determine tgtdir: is <".firstdir.'/AsNeeded/'.pname." readable?")
542 if filereadable(firstdir.'/AsNeeded/'.pname)
543 let tgtdir= "AsNeeded"
547 " call Decho("tgtdir<".tgtdir."> pname<".pname.">")
551 " call Decho("decompress: attempt to bunzip2 ".sname)
552 exe "silent !bunzip2 ".shellescape(sname)
553 let sname= substitute(sname,'\.bz2$','','')
554 " call Decho("decompress: new sname<".sname."> after bunzip2")
555 elseif sname =~ '\.gz$'
556 " call Decho("decompress: attempt to gunzip ".sname)
557 exe "silent !gunzip ".shellescape(sname)
558 let sname= substitute(sname,'\.gz$','','')
559 " call Decho("decompress: new sname<".sname."> after gunzip")
561 " call Decho("no decompression needed")
564 " distribute archive(.zip, .tar, .vba) contents
566 " call Decho("dearchive: attempt to unzip ".sname)
567 exe "silent !unzip -o ".shellescape(sname)
568 elseif sname =~ '\.tar$'
569 " call Decho("dearchive: attempt to untar ".sname)
570 exe "silent !tar -xvf ".shellescape(sname)
571 elseif sname =~ '\.vba$'
572 " call Decho("dearchive: attempt to handle a vimball: ".sname)
574 if exists("g:vimball_home")
575 let oldvimballhome= g:vimball_home
577 let g:vimball_home= s:autoinstall
578 exe "silent e ".fnameescape(sname)
581 if exists("oldvimballhome")
582 let g:vimball_home= oldvimballhome
587 " call Decho("no dearchiving needed")
590 " ---------------------------------------------
591 " move plugin to plugin/ or AsNeeded/ directory
592 " ---------------------------------------------
594 " call Decho("dearchive: attempt to simply move ".sname." to ".tgtdir)
595 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".tgtdir
597 " call Decho("dearchive: move <".sname."> to installdir<".installdir.">")
598 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".installdir
600 if tgtdir != "plugin"
601 " call Decho("exe silent !".g:GetLatestVimScripts_mv." plugin/".shellescape(pname)." ".tgtdir)
602 exe "silent !".g:GetLatestVimScripts_mv." plugin/".shellescape(pname)." ".tgtdir
606 let docdir= substitute(&rtp,',.*','','e')."/doc"
607 " call Decho("helptags: docdir<".docdir.">")
608 exe "helptags ".fnameescape(docdir)
609 exe "cd ".fnameescape(curdir)
611 if fname !~ ':AutoInstall:'
612 let modline=scriptid." ".latestsrcid." :AutoInstall: ".fname.cmmnt
614 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
617 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
620 " update the data in the <GetLatestVimScripts.dat> file
621 call setline(line("."),modline)
622 " call Decho("update data in ".expand("%")."#".line(".").": modline<".modline.">")
624 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."], no need to update")
632 " call Dredir("BUFFER TEST (GetOneScript)","ls!")
634 " call Dret("GetOneScript")
637 " ---------------------------------------------------------------------
638 " Restore Options: {{{1
642 " ---------------------------------------------------------------------
644 " vim: ts=8 sts=2 fdm=marker nowrap