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= "v31"
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 " insure that fnameescape() is available
144 if !exists("*fnameescape")
145 echoerr "GetLatestVimScripts needs fnameescape() (provided by 7.1.299 or later)"
149 " Find the .../GetLatest subdirectory under the runtimepath
150 for datadir in split(&rtp,',') + ['']
151 if isdirectory(datadir."/GetLatest")
152 " call Decho("found directory<".datadir.">")
153 let datadir= datadir . "/GetLatest"
156 if filereadable(datadir."GetLatestVimScripts.dat")
157 " call Decho("found ".datadir."/GetLatestVimScripts.dat")
162 " Sanity checks: readability and writability
164 echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install'
165 " call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
169 if filewritable(datadir) != 2
170 echoerr "(getLatestVimScripts) Your ".datadir." isn't writable"
171 " call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
174 let datafile= datadir."/GetLatestVimScripts.dat"
175 if !filereadable(datafile)
176 echoerr "Your data file<".datafile."> isn't readable"
177 " call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
180 if !filewritable(datafile)
181 echoerr "Your data file<".datafile."> isn't writable"
182 " call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
185 " call Decho("datadir <".datadir.">")
186 " call Decho("datafile <".datafile.">")
188 " don't let any events interfere (like winmanager's, taglist's, etc)
193 " record current directory, change to datadir, open split window with
195 let origdir= getcwd()
196 " call Decho("exe cd ".fnameescape(substitute(datadir,'\','/','ge')))
197 exe "cd ".fnameescape(substitute(datadir,'\','/','ge'))
199 " call Decho("exe e ".fnameescape(substitute(datafile,'\','/','ge')))
200 exe "e ".fnameescape(substitute(datafile,'\','/','ge'))
205 " Check on dependencies mentioned in plugins
207 " call Decho("searching plugins for GetLatestVimScripts dependencies")
208 let lastline = line("$")
209 " call Decho("lastline#".lastline)
210 let plugins = split(globpath(&rtp,"plugin/*.vim"),'\n')
214 for plugin in plugins
215 " call Decho("plugin<".plugin.">")
217 " don't process plugins in system directories
219 let firstdir= substitute(plugin,'[/\\][^/\\]\+$','','')
220 " call Decho("setting firstdir<".firstdir.">")
222 let curdir= substitute(plugin,'[/\\][^/\\]\+$','','')
223 " call Decho("curdir<".curdir.">")
224 if curdir != firstdir
225 " call Decho("skipping subsequent plugins: curdir<".curdir."> != firstdir<".firstdir.">")
231 " evidently a :r creates a new buffer (the "#" buffer) that is subsequently unused -- bwiping it
234 " call Decho(".dependency checking<".plugin."> line$=".line("$"))
235 " call Decho("exe silent r ".fnameescape(plugin))
236 exe "silent r ".fnameescape(plugin)
237 exe "silent bwipe ".bufnr("#")
239 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
240 let newscript= substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
241 let llp1 = lastline+1
242 " call Decho("..newscript<".newscript.">")
244 " don't process ""GetLatestVimScripts lines -- those that have been doubly-commented out
246 " found a "GetLatestVimScripts: # #" line in the script; check if its already in the datafile
247 let curline = line(".")
248 let noai_script = substitute(newscript,'\s*:AutoInstall:\s*','','e')
250 let srchline = search('\<'.noai_script.'\>','bW')
251 " call Decho("..noai_script<".noai_script."> srch=".srchline."curline#".line(".")." lastline#".lastline)
254 " found a new script to permanently include in the datafile
256 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
258 echomsg "Appending <".@a."> to ".datafile." for ".newscript
259 " call Decho("..APPEND (".noai_script.")<".@a."> to GetLatestVimScripts.dat")
262 let curline = curline + 1
263 let foundscript = foundscript + 1
265 " call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
268 let curline = curline + 1
273 let llp1= lastline + 1
274 " call Decho(".deleting lines: ".llp1.",$d")
275 exe "silent! ".llp1.",$d"
277 " call Decho("--- end dependency checking loop --- foundscript=".foundscript)
279 " call Dredir("BUFFER TEST (GetLatestVimScripts 1)","ls!")
285 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
286 " call Decho("begin: checking out-of-date scripts using datafile<".datafile.">")
289 " /^-----/,$g/^\s*\d/call Decho(getline("."))
291 /^-----/,$g/^\s*\d/call s:GetOneScript()
292 " call Decho("--- end out-of-date checking --- ")
294 " Final report (an echomsg)
297 catch /^Vim\%((\a\+)\)\=:E114/
298 " call Dret("GetLatestVimScripts : nothing done!")
305 let s:msg = "Downloaded one updated script to <".datadir.">"
306 elseif s:downloads == 2
307 let s:msg= "Downloaded two updated scripts to <".datadir.">"
308 elseif s:downloads > 1
309 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
311 let s:msg= "Everything was already current"
314 let s:msg= s:msg." (".s:downerrors." downloading errors)"
323 " restore events and current directory
324 exe "cd ".fnameescape(substitute(origdir,'\','/','ge'))
328 " call Dredir("BUFFER TEST (GetLatestVimScripts 2)","ls!")
329 " call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
332 " ---------------------------------------------------------------------
333 " GetOneScript: (Get Latest Vim Script) this function operates {{{1
334 " on the current line, interpreting two numbers and text as
335 " ScriptID, SourceID, and Filename.
336 " It downloads any scripts that have newer versions from vim.sf.net.
337 fun! s:GetOneScript(...)
338 " call Dfunc("GetOneScript()")
340 " set options to allow progress to be shown on screen
347 " put current line on top-of-screen and interpret it into
348 " a script identifer : used to obtain webpage
349 " source identifier : used to identify current version
350 " and an associated comment: used to report on what's being considered
356 " call Decho("scriptid<".scriptid.">")
357 " call Decho("srcid <".srcid.">")
358 " call Decho("fname <".fname.">")
360 let curline = getline(".")
361 if curline =~ '^\s*#'
363 " call Dret("GetOneScript : skipping a pure comment line")
366 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$'
368 let scriptid = substitute(curline,parsepat,'\1','e')
369 catch /^Vim\%((\a\+)\)\=:E486/
373 let srcid = substitute(curline,parsepat,'\2','e')
374 catch /^Vim\%((\a\+)\)\=:E486/
378 let fname= substitute(curline,parsepat,'\3','e')
379 catch /^Vim\%((\a\+)\)\=:E486/
383 let cmmnt= substitute(curline,parsepat,'\4','e')
384 catch /^Vim\%((\a\+)\)\=:E486/
387 " call Decho("curline <".curline.">")
388 " call Decho("parsepat<".parsepat.">")
389 " call Decho("scriptid<".scriptid.">")
390 " call Decho("srcid <".srcid.">")
391 " call Decho("fname <".fname.">")
394 if scriptid == 0 || srcid == 0
395 " When looking for :AutoInstall: lines, skip scripts that have 0 0 scriptname
397 " call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
402 if fname =~ ":AutoInstall:"
403 " call Decho("case AutoInstall: fname<".fname.">")
404 let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','')
405 " call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
406 if s:autoinstall != ""
407 let doautoinstall = g:GetLatestVimScripts_allowautoinstall
412 " call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
416 " call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
417 echo 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
419 " grab a copy of the plugin's vim.sf.net webpage
420 let scriptaddr = 'http://vim.sf.net/script.php?script_id='.scriptid
421 let tmpfile = tempname()
424 " make up to three tries at downloading the description
427 " call Decho("try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
428 if has("win32") || has("win16") || has("win95")
429 " call Decho("new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile).' '.s:Escape(scriptaddr)."|bw!")
430 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile).' '.s:Escape(scriptaddr)|bw!
432 " call Decho("exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile)." ".s:Escape(scriptaddr))
433 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(tmpfile)." ".s:Escape(scriptaddr)
436 exe "silent vsplit ".fnameescape(tmpfile)
442 " find the latest source-id in the plugin's webpage
444 let findpkg= search('Click on the package to download','W')
450 " call Decho(" --- end downloading tries while loop --- itry=".itry)
452 " testing: did finding "Click on the package..." fail?
453 if findpkg == 0 || itry >= 4
460 let s:downerrors = s:downerrors + 1
461 " call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
462 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
463 " call Dret("GetOneScript : srch for /Click on the package/ failed")
467 " call Decho('found "Click on the package to download"')
469 let findsrcid= search('src_id=','W')
477 let s:downerrors = s:downerrors + 1
478 " call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
479 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
481 " call Dret("GetOneScript : srch for /src_id/ failed")
484 " call Decho('found "src_id=" in description page')
486 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
487 let latestsrcid= substitute(getline("."),srcidpat,'\1','')
488 let sname = substitute(getline("."),srcidpat,'\2','') " script name actually downloaded
489 " call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> sname<".sname.">")
493 " convert the strings-of-numbers into numbers
494 let srcid = srcid + 0
495 let latestsrcid = latestsrcid + 0
496 " call Decho("srcid=".srcid." latestsrcid=".latestsrcid." sname<".sname.">")
498 " has the plugin's most-recent srcid increased, which indicates
499 " that it has been updated
500 if latestsrcid > srcid
501 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."]: need to update <".sname.">")
503 let s:downloads= s:downloads + 1
504 if sname == bufname("%")
505 " GetLatestVimScript has to be careful about downloading itself
506 let sname= "NEW_".sname
509 " the plugin has been updated since we last obtained it, so download a new copy
510 " call Decho("...downloading new <".sname.">")
511 echomsg "...downloading new <".sname.">"
512 if has("win32") || has("win16") || has("win95")
513 " 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")
514 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
516 " 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='))
517 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".s:Escape(sname)." ".s:Escape('http://vim.sf.net/scripts/download_script.php?src_id=').latestsrcid
520 " AutoInstall: only if doautoinstall has been requested by the plugin itself
522 " call Decho("attempting to do autoinstall: getcwd<".getcwd()."> filereadable(".sname.")=".filereadable(sname))
523 if filereadable(sname)
524 " call Decho("exe silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." ".s:Escape(s:autoinstall))
525 exe "silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." ".s:Escape(s:autoinstall)
526 let curdir = escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #")
527 let installdir= curdir."/Installed"
528 if !isdirectory(installdir)
529 call mkdir(installdir)
531 " call Decho("exe cd ".fnameescape(s:autoinstall))
532 exe "cd ".fnameescape(s:autoinstall)
536 " call Decho("decompress: attempt to bunzip2 ".sname)
537 exe "silent !bunzip2 ".s:Escape(sname)
538 let sname= substitute(sname,'\.bz2$','','')
539 " call Decho("decompress: new sname<".sname."> after bunzip2")
540 elseif sname =~ '\.gz$'
541 " call Decho("decompress: attempt to gunzip ".sname)
542 exe "silent !gunzip ".s:Escape(sname)
543 let sname= substitute(sname,'\.gz$','','')
544 " call Decho("decompress: new sname<".sname."> after gunzip")
547 " distribute archive(.zip, .tar, .vba) contents
549 " call Decho("dearchive: attempt to unzip ".sname)
550 exe "silent !unzip -o ".s:Escape(sname)
551 elseif sname =~ '\.tar$'
552 " call Decho("dearchive: attempt to untar ".sname)
553 exe "silent !tar -xvf ".s:Escape(sname)
554 elseif sname =~ '\.vba$'
555 " call Decho("dearchive: attempt to handle a vimball: ".sname)
557 exe "silent e ".fnameescape(sname)
563 " call Decho("dearchive: attempt to simply move ".sname." to plugin")
564 exe "silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." plugin"
566 " call Decho("dearchive: move <".sname."> to installdir<".installdir.">")
567 exe "silent !".g:GetLatestVimScripts_mv." ".s:Escape(sname)." ".installdir
571 let docdir= substitute(&rtp,',.*','','e')."/doc"
572 " call Decho("helptags: docdir<".docdir.">")
573 exe "helptags ".fnameescape(docdir)
574 exe "cd ".fnameescape(curdir)
576 if fname !~ ':AutoInstall:'
577 let modline=scriptid." ".latestsrcid." :AutoInstall: ".fname.cmmnt
579 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
582 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
585 " update the data in the <GetLatestVimScripts.dat> file
586 call setline(line("."),modline)
587 " call Decho("update data in ".expand("%")."#".line(".").": modline<".modline.">")
589 " call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."], no need to update")
597 " call Dredir("BUFFER TEST (GetOneScript)","ls!")
599 " call Dret("GetOneScript")
602 " ---------------------------------------------------------------------
603 " s:Escape: makes a string safe&suitable for the shell {{{2
605 " call Dfunc("s:Escape(name<".a:name.">)")
606 if exists("*shellescape")
607 " shellescape() was added by patch 7.0.111
608 let name= shellescape(a:name)
610 let name= g:getscript_shq . a:name . g:getscript_shq
612 " call Dret("s:Escape ".name)
616 " ---------------------------------------------------------------------
617 " Restore Options: {{{1
621 " ---------------------------------------------------------------------
623 " vim: ts=8 sts=2 fdm=marker nowrap