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= "v30"
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 " GetLatestVimScripts: this function gets the latest versions of {{{1
131 " scripts based on the list in
132 " (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat
133 fun! getscript#GetLatestVimScripts()
134 " call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">")
136 " insure that wget is executable
137 if executable(g:GetLatestVimScripts_wget) != 1
138 echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system"
139 " call Dret("GetLatestVimScripts : wget not executable/availble")
143 " Find the .../GetLatest subdirectory under the runtimepath
144 for datadir in split(&rtp,',') + ['']
145 if isdirectory(datadir."/GetLatest")
146 " call Decho("found directory<".datadir.">")
147 let datadir= datadir . "/GetLatest"
150 if filereadable(datadir."GetLatestVimScripts.dat")
151 " call Decho("found ".datadir."/GetLatestVimScripts.dat")
156 " Sanity checks: readability and writability
158 echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install'
159 " call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
163 if filewritable(datadir) != 2
164 echoerr "(getLatestVimScripts) Your ".datadir." isn't writable"
165 " call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
168 let datafile= datadir."/GetLatestVimScripts.dat"
169 if !filereadable(datafile)
170 echoerr "Your data file<".datafile."> isn't readable"
171 " call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
174 if !filewritable(datafile)
175 echoerr "Your data file<".datafile."> isn't writable"
176 " call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
179 " call Decho("datadir <".datadir.">")
180 " call Decho("datafile <".datafile.">")
182 " don't let any events interfere (like winmanager's, taglist's, etc)
186 " record current directory, change to datadir, open split window with
188 let origdir= getcwd()
189 " call Decho("exe cd ".fnameescape(substitute(datadir,'\','/','ge')))
190 exe "cd ".fnameescape(substitute(datadir,'\','/','ge'))
192 " call Decho("exe e ".fnameescape(substitute(datafile,'\','/','ge')))
193 exe "e ".fnameescape(substitute(datafile,'\','/','ge'))
198 " Check on dependencies mentioned in plugins
200 " call Decho("searching plugins for GetLatestVimScripts dependencies")
201 let lastline = line("$")
202 " call Decho("lastline#".lastline)
203 let plugins = split(globpath(&rtp,"plugin/*.vim"),'\n')
207 for plugin in plugins
208 " call Decho("plugin<".plugin.">")
210 " don't process plugins in system directories
212 let firstdir= substitute(plugin,'[/\\][^/\\]\+$','','')
213 " call Decho("setting firstdir<".firstdir.">")
215 let curdir= substitute(plugin,'[/\\][^/\\]\+$','','')
216 " call Decho("curdir<".curdir.">")
217 if curdir != firstdir
218 " call Decho("skipping subsequent plugins: curdir<".curdir."> != firstdir<".firstdir.">")
224 " evidently a :r creates a new buffer (the "#" buffer) that is subsequently unused -- bwiping it
227 " call Decho(".dependency checking<".plugin."> line$=".line("$"))
228 " call Decho("exe silent r ".fnameescape(plugin))
229 exe "silent r ".fnameescape(plugin)
230 exe "silent bwipe ".bufnr("#")
232 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
233 let newscript= substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
234 let llp1 = lastline+1
235 " call Decho("..newscript<".newscript.">")
237 " don't process ""GetLatestVimScripts lines -- those that have been doubly-commented out
239 " found a "GetLatestVimScripts: # #" line in the script; check if its already in the datafile
240 let curline = line(".")
241 let noai_script = substitute(newscript,'\s*:AutoInstall:\s*','','e')
243 let srchline = search('\<'.noai_script.'\>','bW')
244 " call Decho("..noai_script<".noai_script."> srch=".srchline."curline#".line(".")." lastline#".lastline)
247 " found a new script to permanently include in the datafile
249 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
251 echomsg "Appending <".@a."> to ".datafile." for ".newscript
252 " call Decho("..APPEND (".noai_script.")<".@a."> to GetLatestVimScripts.dat")
255 let curline = curline + 1
256 let foundscript = foundscript + 1
258 " call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
261 let curline = curline + 1
266 let llp1= lastline + 1
267 " call Decho(".deleting lines: ".llp1.",$d")
268 exe "silent! ".llp1.",$d"
270 " call Decho("--- end dependency checking loop --- foundscript=".foundscript)
272 " call Dredir("BUFFER TEST (GetLatestVimScripts 1)","ls!")
278 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
279 " call Decho("begin: checking out-of-date scripts using datafile<".datafile.">")
282 " /^-----/,$g/^\s*\d/call Decho(getline("."))
284 /^-----/,$g/^\s*\d/call s:GetOneScript()
285 " call Decho("--- end out-of-date checking --- ")
287 " Final report (an echomsg)
290 catch /^Vim\%((\a\+)\)\=:E114/
291 " call Dret("GetLatestVimScripts : nothing done!")
298 let s:msg = "Downloaded one updated script to <".datadir.">"
299 elseif s:downloads == 2
300 let s:msg= "Downloaded two updated scripts to <".datadir.">"
301 elseif s:downloads > 1
302 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
304 let s:msg= "Everything was already current"
307 let s:msg= s:msg." (".s:downerrors." downloading errors)"
316 " restore events and current directory
317 exe "cd ".fnameescape(substitute(origdir,'\','/','ge'))
320 " call Dredir("BUFFER TEST (GetLatestVimScripts 2)","ls!")
321 " call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
324 " ---------------------------------------------------------------------
325 " GetOneScript: (Get Latest Vim Script) this function operates {{{1
326 " on the current line, interpreting two numbers and text as
327 " ScriptID, SourceID, and Filename.
328 " It downloads any scripts that have newer versions from vim.sf.net.
329 fun! s:GetOneScript(...)
330 " call Dfunc("GetOneScript()")
332 " set options to allow progress to be shown on screen
339 " put current line on top-of-screen and interpret it into
340 " a script identifer : used to obtain webpage
341 " source identifier : used to identify current version
342 " and an associated comment: used to report on what's being considered
348 " call Decho("scriptid<".scriptid.">")
349 " call Decho("srcid <".srcid.">")
350 " call Decho("fname <".fname.">")
352 let curline = getline(".")
353 if curline =~ '^\s*#'
355 " call Dret("GetOneScript : skipping a pure comment line")
358 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$'
360 let scriptid = substitute(curline,parsepat,'\1','e')
361 catch /^Vim\%((\a\+)\)\=:E486/
365 let srcid = substitute(curline,parsepat,'\2','e')
366 catch /^Vim\%((\a\+)\)\=:E486/
370 let fname= substitute(curline,parsepat,'\3','e')
371 catch /^Vim\%((\a\+)\)\=:E486/
375 let cmmnt= substitute(curline,parsepat,'\4','e')
376 catch /^Vim\%((\a\+)\)\=:E486/
379 " call Decho("curline <".curline.">")
380 " call Decho("parsepat<".parsepat.">")
381 " call Decho("scriptid<".scriptid.">")
382 " call Decho("srcid <".srcid.">")
383 " call Decho("fname <".fname.">")
386 if scriptid == 0 || srcid == 0
387 " When looking for :AutoInstall: lines, skip scripts that have 0 0 scriptname
389 " call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
394 if fname =~ ":AutoInstall:"
395 " call Decho("case AutoInstall: fname<".fname.">")
396 let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','')
397 " call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
398 if s:autoinstall != ""
399 let doautoinstall = g:GetLatestVimScripts_allowautoinstall
404 " call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
408 " call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
409 echo 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
411 " grab a copy of the plugin's vim.sf.net webpage
412 let scriptaddr = 'http://vim.sf.net/script.php?script_id='.scriptid
413 let tmpfile = tempname()
416 " make up to three tries at downloading the description
419 " call Decho("try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
420 if has("win32") || has("win16") || has("win95")
421 " call Decho("new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile).' '.s:Escape(scriptaddr)."|bw!")
422 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile).' '.s:Escape(scriptaddr)|bw!
424 " call Decho("exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile)." ".s:Escape(scriptaddr))
425 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile)." ".s:Escape(scriptaddr)
428 exe "silent vsplit ".fnameescape(tmpfile)
434 " find the latest source-id in the plugin's webpage
436 let findpkg= search('Click on the package to download','W')
442 " call Decho(" --- end downloading tries while loop --- itry=".itry)
444 " testing: did finding "Click on the package..." fail?
445 if findpkg == 0 || itry >= 4
452 let s:downerrors = s:downerrors + 1
453 " call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
454 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
455 " call Dret("GetOneScript : srch for /Click on the package/ failed")
459 " call Decho('found "Click on the package to download"')
461 let findsrcid= search('src_id=','W')
469 let s:downerrors = s:downerrors + 1
470 " call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
471 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
473 " call Dret("GetOneScript : srch for /src_id/ failed")
476 " call Decho('found "src_id=" in description page')
478 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
479 let latestsrcid= substitute(getline("."),srcidpat,'\1','')
480 let sname = substitute(getline("."),srcidpat,'\2','') " script name actually downloaded
481 " call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> sname<".sname.">")
485 " convert the strings-of-numbers into numbers
486 let srcid = srcid + 0
487 let latestsrcid = latestsrcid + 0
488 " call Decho("srcid=".srcid." latestsrcid=".latestsrcid." sname<".sname.">")
490 " has the plugin's most-recent srcid increased, which indicates
491 " that it has been updated
492 if latestsrcid > srcid
493 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."]: need to update <".sname.">")
495 let s:downloads= s:downloads + 1
496 if sname == bufname("%")
497 " GetLatestVimScript has to be careful about downloading itself
498 let sname= "NEW_".sname
501 " the plugin has been updated since we last obtained it, so download a new copy
502 " call Decho("...downloading new <".sname.">")
503 echomsg "...downloading new <".sname.">"
504 if has("win32") || has("win16") || has("win95")
505 " call Decho("new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(sname)." ".s:Escape('http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid)."|q")
506 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(sname)." ".s:Escape('http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid)|q
508 " call Decho("exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(sname)." ".s:Escape('http://vim.sf.net/scripts/download_script.php?src_id='))
509 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(sname)." ".s:Escape('http://vim.sf.net/scripts/download_script.php?src_id=')
512 " AutoInstall: only if doautoinstall has been requested by the plugin itself
514 " call Decho("attempting to do autoinstall: getcwd<".getcwd()."> filereadable(".sname.")=".filereadable(sname))
515 if filereadable(sname)
516 call Decho("exe silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." ".s:Escape(s:autoinstall))
517 exe "silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." ".s:Escape(s:autoinstall)
518 let curdir = escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #")
519 let installdir= curdir."/Installed"
520 if !isdirectory(installdir)
521 call mkdir(installdir)
523 " call Decho("exe cd ".fnameescape(s:autoinstall))
524 exe "cd ".fnameescape(s:autoinstall)
528 " call Decho("decompress: attempt to bunzip2 ".sname)
529 exe "silent !bunzip2 ".s:Escape(sname)
530 let sname= substitute(sname,'\.bz2$','','')
531 " call Decho("decompress: new sname<".sname."> after bunzip2")
532 elseif sname =~ '\.gz$'
533 " call Decho("decompress: attempt to gunzip ".sname)
534 exe "silent !gunzip ".s:Escape(sname)
535 let sname= substitute(sname,'\.gz$','','')
536 " call Decho("decompress: new sname<".sname."> after gunzip")
539 " distribute archive(.zip, .tar, .vba) contents
541 " call Decho("dearchive: attempt to unzip ".sname)
542 exe "silent !unzip -o ".s:Escape(sname)
543 elseif sname =~ '\.tar$'
544 " call Decho("dearchive: attempt to untar ".sname)
545 exe "silent !tar -xvf ".s:Escape(sname)
546 elseif sname =~ '\.vba$'
547 " call Decho("dearchive: attempt to handle a vimball: ".sname)
549 exe "silent e ".fnameescape(sname)
555 " call Decho("dearchive: attempt to simply move ".sname." to plugin")
556 exe "silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." plugin"
558 " call Decho("dearchive: move <".sname."> to installdir<".installdir.">")
559 exe "silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." ".installdir
563 let docdir= substitute(&rtp,',.*','','e')."/doc"
564 " call Decho("helptags: docdir<".docdir.">")
565 exe "helptags ".fnameescape(docdir)
566 exe "cd ".fnameescape(curdir)
568 if fname !~ ':AutoInstall:'
569 let modline=scriptid." ".latestsrcid." :AutoInstall: ".fname.cmmnt
571 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
574 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
577 " update the data in the <GetLatestVimScripts.dat> file
578 call setline(line("."),modline)
579 " call Decho("update data in ".expand("%")."#".line(".").": modline<".modline.">")
581 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."], no need to update")
589 " call Dredir("BUFFER TEST (GetOneScript)","ls!")
591 " call Dret("GetOneScript")
594 " ---------------------------------------------------------------------
595 " s:Escape: makes a string safe&suitable for the shell {{{2
597 " call Dfunc("s:Escape(name<".a:name.">)")
598 if exists("*shellescape")
599 " shellescape() was added by patch 7.0.111
600 let name= shellescape(a:name)
602 let name= g:getscript_shq . a:name . g:getscript_shq
604 " call Dret("s:Escape ".name)
608 " ---------------------------------------------------------------------
609 " Restore Options: {{{1
613 " ---------------------------------------------------------------------
615 " vim: ts=8 sts=2 fdm=marker nowrap