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!
16 echoerr "GetLatestVimScripts is not vi-compatible; not loaded (you need to set nocp)"
23 if exists("g:loaded_getscript")
26 let g:loaded_getscript= "v29"
28 " ---------------------------
29 " Global Variables: {{{1
30 " ---------------------------
31 " Cygwin Detection ------- {{{2
32 if !exists("g:getscript_cygwin")
33 if has("win32") || has("win95") || has("win64") || has("win16")
34 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
35 let g:getscript_cygwin= 1
37 let g:getscript_cygwin= 0
40 let g:getscript_cygwin= 0
43 " shell quoting character {{{2
44 if exists("g:netrw_shq") && !exists("g:getscript_shq")
45 let g:getscript_shq= g:netrw_shq
46 elseif !exists("g:getscript_shq")
47 if exists("&shq") && &shq != ""
48 let g:getscript_shq= &shq
49 elseif exists("&sxq") && &sxq != ""
50 let g:getscript_shq= &sxq
51 elseif has("win32") || has("win95") || has("win64") || has("win16")
53 let g:getscript_shq= "'"
55 let g:getscript_shq= '"'
58 let g:getscript_shq= "'"
60 " call Decho("g:getscript_shq<".g:getscript_shq.">")
64 if !exists("g:GetLatestVimScripts_wget")
66 let g:GetLatestVimScripts_wget= "wget"
67 elseif executable("curl")
68 let g:GetLatestVimScripts_wget= "curl"
70 let g:GetLatestVimScripts_wget = 'echo "GetLatestVimScripts needs wget or curl"'
71 let g:GetLatestVimScripts_options = ""
75 " options that wget and curl require:
76 if !exists("g:GetLatestVimScripts_options")
77 if g:GetLatestVimScripts_wget == "wget"
78 let g:GetLatestVimScripts_options= "-q -O"
79 elseif g:GetLatestVimScripts_wget == "curl"
80 let g:GetLatestVimScripts_options= "-s -O"
82 let g:GetLatestVimScripts_options= ""
86 " by default, allow autoinstall lines to work
87 if !exists("g:GetLatestVimScripts_allowautoinstall")
88 let g:GetLatestVimScripts_allowautoinstall= 1
92 "let g:GetLatestVimScripts_wget = "echo"
93 "let g:GetLatestVimScripts_options = "options"
95 " ---------------------------------------------------------------------
96 " Check If AutoInstall Capable: {{{1
98 if g:GetLatestVimScripts_allowautoinstall
100 if (has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")) && &shell != "bash"
101 " windows (but not cygwin/bash)
102 let s:dotvim= "vimfiles"
103 if !exists("g:GetLatestVimScripts_mv")
104 let g:GetLatestVimScripts_mv= "ren"
110 if !exists("g:GetLatestVimScripts_mv")
111 let g:GetLatestVimScripts_mv= "mv"
115 if exists('$HOME') && isdirectory(expand("$HOME")."/".s:dotvim)
116 let s:autoinstall= $HOME."/".s:dotvim
118 " call Decho("s:autoinstall<".s:autoinstall.">")
120 " call Decho("g:GetLatestVimScripts_allowautoinstall=".g:GetLatestVimScripts_allowautoinstall.": :AutoInstall: disabled")
123 " ---------------------------------------------------------------------
124 " Public Interface: {{{1
125 com! -nargs=0 GetLatestVimScripts call getscript#GetLatestVimScripts()
126 com! -nargs=0 GetScript call getscript#GetLatestVimScripts()
127 silent! com -nargs=0 GLVS call getscript#GetLatestVimScripts()
129 " ---------------------------------------------------------------------
130 " GetOneScript: (Get Latest Vim Script) this function operates {{{1
131 " on the current line, interpreting two numbers and text as
132 " ScriptID, SourceID, and Filename.
133 " It downloads any scripts that have newer versions from vim.sf.net.
134 fun! s:GetOneScript(...)
135 " call Dfunc("GetOneScript()")
137 " set options to allow progress to be shown on screen
144 " put current line on top-of-screen and interpret it into
145 " a script identifer : used to obtain webpage
146 " source identifier : used to identify current version
147 " and an associated comment: used to report on what's being considered
153 " call Decho("scriptid<".scriptid.">")
154 " call Decho("srcid <".srcid.">")
155 " call Decho("fname <".fname.">")
157 let curline = getline(".")
158 if curline =~ '^\s*#'
160 " call Dret("GetOneScript : skipping a pure comment line")
163 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$'
165 let scriptid = substitute(curline,parsepat,'\1','e')
166 catch /^Vim\%((\a\+)\)\=:E486/
170 let srcid = substitute(curline,parsepat,'\2','e')
171 catch /^Vim\%((\a\+)\)\=:E486/
175 let fname= substitute(curline,parsepat,'\3','e')
176 catch /^Vim\%((\a\+)\)\=:E486/
180 let cmmnt= substitute(curline,parsepat,'\4','e')
181 catch /^Vim\%((\a\+)\)\=:E486/
184 " call Decho("curline <".curline.">")
185 " call Decho("parsepat<".parsepat.">")
186 " call Decho("scriptid<".scriptid.">")
187 " call Decho("srcid <".srcid.">")
188 " call Decho("fname <".fname.">")
191 if scriptid == 0 || srcid == 0
192 " When looking for :AutoInstall: lines, skip scripts that have 0 0 scriptname
194 " call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
199 if fname =~ ":AutoInstall:"
200 " call Decho("case AutoInstall: fname<".fname.">")
201 let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','')
202 " call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
203 if s:autoinstall != ""
204 let doautoinstall = g:GetLatestVimScripts_allowautoinstall
209 " call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
213 " call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
214 echomsg 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
216 " grab a copy of the plugin's vim.sf.net webpage
217 let scriptaddr = 'http://vim.sf.net/script.php?script_id='.scriptid
218 let tmpfile = tempname()
221 " make up to three tries at downloading the description
224 " call Decho("try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
225 if has("win32") || has("win16") || has("win95")
226 " call Decho("new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.tmpfile.g:getscript_shq.' '.g:getscript_shq.scriptaddr.g:getscript_shq."|q!")
227 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.tmpfile.g:getscript_shq.' '.g:getscript_shq.scriptaddr.g:getscript_shq|q!
229 " call Decho("exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.tmpfile.g:getscript_shq." ".g:getscript_shq.scriptaddr.g:getscript_shq)
230 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.tmpfile.g:getscript_shq." ".g:getscript_shq.scriptaddr.g:getscript_shq
233 exe "silent vsplit ".tmpfile
238 " find the latest source-id in the plugin's webpage
240 let findpkg= search('Click on the package to download','W')
246 " call Decho(" --- end downloading tries while loop --- itry=".itry)
248 " testing: did finding "Click on the package..." fail?
249 if findpkg == 0 || itry >= 4
256 let s:downerrors = s:downerrors + 1
257 " call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
258 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
259 " call Dret("GetOneScript : srch for /Click on the package/ failed")
263 " call Decho('found "Click on the package to download"')
265 let findsrcid= search('src_id=','W')
273 let s:downerrors = s:downerrors + 1
274 " call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
275 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
277 " call Dret("GetOneScript : srch for /src_id/ failed")
280 " call Decho('found "src_id=" in description page')
282 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
283 let latestsrcid= substitute(getline("."),srcidpat,'\1','')
284 let sname = substitute(getline("."),srcidpat,'\2','') " script name actually downloaded
285 " call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> sname<".sname.">")
289 " convert the strings-of-numbers into numbers
290 let srcid = srcid + 0
291 let latestsrcid = latestsrcid + 0
292 " call Decho("srcid=".srcid." latestsrcid=".latestsrcid." sname<".sname.">")
294 " has the plugin's most-recent srcid increased, which indicates
295 " that it has been updated
296 if latestsrcid > srcid
297 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."]: need to update <".sname.">")
299 let s:downloads= s:downloads + 1
300 if sname == bufname("%")
301 " GetLatestVimScript has to be careful about downloading itself
302 let sname= "NEW_".sname
305 " the plugin has been updated since we last obtained it, so download a new copy
306 " call Decho("...downloading new <".sname.">")
307 echomsg "...downloading new <".sname.">"
308 if has("win32") || has("win16") || has("win95")
309 " call Decho("new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.sname.g:getscript_shq." ".g:getscript_shq.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.g:getscript_shq."|q")
310 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.sname.g:getscript_shq." ".g:getscript_shq.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.g:getscript_shq|q
312 " call Decho("silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.sname.g:getscript_shq." ".g:getscript_shq.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.g:getscript_shq)
313 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".g:getscript_shq.sname.g:getscript_shq." ".g:getscript_shq.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.g:getscript_shq
316 " AutoInstall: only if doautoinstall has been requested by the plugin itself
318 " call Decho("attempting to do autoinstall: getcwd<".getcwd()."> filereadable(".sname.")=".filereadable(sname))
319 if filereadable(sname)
320 " call Decho("silent !".g:GetLatestVimScripts_mv." ".g:getscript_shq.sname.g:getscript_shq." ".g:getscript_shq.s:autoinstall.g:getscript_shq)
321 exe "silent !".g:GetLatestVimScripts_mv." ".g:getscript_shq.sname.g:getscript_shq." ".g:getscript_shq.s:autoinstall.g:getscript_shq
322 let curdir = escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #")
323 let installdir= curdir."/Installed"
324 if !isdirectory(installdir)
325 call mkdir(installdir)
327 " call Decho("exe cd ".s:autoinstall)
328 exe "cd ".escape(s:autoinstall,' ')
332 " call Decho("decompress: attempt to bunzip2 ".sname)
333 exe "silent !bunzip2 ".g:getscript_shq.sname.g:getscript_shq
334 let sname= substitute(sname,'\.bz2$','','')
335 " call Decho("decompress: new sname<".sname."> after bunzip2")
336 elseif sname =~ '\.gz$'
337 " call Decho("decompress: attempt to gunzip ".sname)
338 exe "silent !gunzip ".g:getscript_shq.sname.g:getscript_shq
339 let sname= substitute(sname,'\.gz$','','')
340 " call Decho("decompress: new sname<".sname."> after gunzip")
343 " distribute archive(.zip, .tar, .vba) contents
345 " call Decho("dearchive: attempt to unzip ".sname)
346 exe "silent !unzip -o ".g:getscript_shq.sname.g:getscript_shq
347 elseif sname =~ '\.tar$'
348 " call Decho("dearchive: attempt to untar ".sname)
349 exe "silent !tar -xvf ".g:getscript_shq.sname.g:getscript_shq
350 elseif sname =~ '\.vba$'
351 " call Decho("dearchive: attempt to handle a vimball: ".sname)
353 exe "silent e ".escape(sname,' ')
359 " call Decho("dearchive: attempt to simply move ".sname." to plugin")
360 exe "silent !".g:GetLatestVimScripts_mv." ".g:getscript_shq.sname.g:getscript_shq." plugin"
362 " call Decho("dearchive: move <".sname."> to installdir<".installdir.">")
363 exe "silent !".g:GetLatestVimScripts_mv." ".g:getscript_shq.sname.g:getscript_shq." ".installdir
367 let docdir= substitute(&rtp,',.*','','e')."/doc"
368 " call Decho("helptags: docdir<".docdir.">")
369 exe "helptags ".docdir
372 if fname !~ ':AutoInstall:'
373 let modline=scriptid." ".latestsrcid." :AutoInstall: ".fname.cmmnt
375 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
378 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
381 " update the data in the <GetLatestVimScripts.dat> file
382 call setline(line("."),modline)
383 " call Decho("update data in ".expand("%")."#".line(".").": modline<".modline.">")
385 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."], no need to update")
394 " call Dret("GetOneScript")
397 " ---------------------------------------------------------------------
398 " GetLatestVimScripts: this function gets the latest versions of {{{1
399 " scripts based on the list in
400 " (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat
401 fun! getscript#GetLatestVimScripts()
402 " call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">")
404 " insure that wget is executable
405 if executable(g:GetLatestVimScripts_wget) != 1
406 echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system"
407 " call Dret("GetLatestVimScripts : wget not executable/availble")
411 " Find the .../GetLatest subdirectory under the runtimepath
412 for datadir in split(&rtp,',') + ['']
413 if isdirectory(datadir."/GetLatest")
414 " call Decho("found directory<".datadir.">")
415 let datadir= datadir . "/GetLatest"
418 if filereadable(datadir."GetLatestVimScripts.dat")
419 " call Decho("found ".datadir."/GetLatestVimScripts.dat")
424 " Sanity checks: readability and writability
426 echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install'
427 " call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
431 if filewritable(datadir) != 2
432 echoerr "(getLatestVimScripts) Your ".datadir." isn't writable"
433 " call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
436 let datafile= datadir."/GetLatestVimScripts.dat"
437 if !filereadable(datafile)
438 echoerr "Your data file<".datafile."> isn't readable"
439 " call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
442 if !filewritable(datafile)
443 echoerr "Your data file<".datafile."> isn't writable"
444 " call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
447 " call Decho("datadir <".datadir.">")
448 " call Decho("datafile <".datafile.">")
450 " don't let any events interfere (like winmanager's, taglist's, etc)
454 " record current directory, change to datadir, open split window with
456 let origdir= getcwd()
457 exe "cd ".escape(substitute(datadir,'\','/','ge'),"|[]*'\" #")
459 exe "e ".escape(substitute(datafile,'\','/','ge'),"|[]*'\" #")
464 " Check on dependencies mentioned in plugins
466 " call Decho("searching plugins for GetLatestVimScripts dependencies")
467 let lastline = line("$")
468 " call Decho("lastline#".lastline)
469 let plugins = split(globpath(&rtp,"plugin/*.vim"),'\n')
473 for plugin in plugins
475 " don't process plugins in system directories
477 let firstdir= substitute(plugin,'[/\\][^/\\]\+$','','')
478 " call Decho("setting firstdir<".firstdir.">")
480 let curdir= substitute(plugin,'[/\\][^/\\]\+$','','')
481 " call Decho("curdir<".curdir.">")
482 if curdir != firstdir
483 " call Decho("skipping subsequent plugins: curdir<".curdir."> != firstdir<".firstdir.">")
491 " call Decho(".dependency checking<".plugin."> line$=".line("$"))
492 exe "silent r ".escape(plugin,"[]#*$%'\" ?`!&();<>\\")
494 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
495 let newscript= substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
496 let llp1 = lastline+1
497 " call Decho("..newscript<".newscript.">")
499 " don't process ""GetLatestVimScripts lines -- those that have been doubly-commented out
501 " found a "GetLatestVimScripts: # #" line in the script; check if its already in the datafile
502 let curline = line(".")
503 let noai_script = substitute(newscript,'\s*:AutoInstall:\s*','','e')
505 let srchline = search('\<'.noai_script.'\>','bW')
506 " call Decho("..noai_script<".noai_script."> srch=".srchline."curline#".line(".")." lastline#".lastline)
509 " found a new script to permanently include in the datafile
511 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
513 echomsg "Appending <".@a."> to ".datafile." for ".newscript
514 " call Decho("..APPEND (".noai_script.")<".@a."> to GetLatestVimScripts.dat")
517 let curline = curline + 1
518 let foundscript = foundscript + 1
520 " call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
523 let curline = curline + 1
528 let llp1= lastline + 1
529 " call Decho(".deleting lines: ".llp1.",$d")
530 exe "silent! ".llp1.",$d"
532 " call Decho("--- end dependency checking loop --- foundscript=".foundscript)
539 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
540 " call Decho("begin: checking out-of-date scripts using datafile<".datafile.">")
543 " /^-----/,$g/^\s*\d/call Decho(getline("."))
545 /^-----/,$g/^\s*\d/call s:GetOneScript()
546 " call Decho("--- end out-of-date checking --- ")
548 " Final report (an echomsg)
551 catch /^Vim\%((\a\+)\)\=:E114/
552 " call Dret("GetLatestVimScripts : nothing done!")
559 let s:msg = "Downloaded one updated script to <".datadir.">"
560 elseif s:downloads == 2
561 let s:msg= "Downloaded two updated scripts to <".datadir.">"
562 elseif s:downloads > 1
563 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
565 let s:msg= "Everything was already current"
568 let s:msg= s:msg." (".s:downerrors." downloading errors)"
577 " restore events and current directory
578 exe "cd ".escape(substitute(origdir,'\','/','ge'),"|[]*'\" #")
581 " call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
584 " ---------------------------------------------------------------------
585 " Restore Options: {{{1
589 " ---------------------------------------------------------------------
591 " vim: ts=8 sts=2 fdm=marker nowrap