1 " ---------------------------------------------------------------------
2 " GetLatestVimScripts.vim
3 " Author: Charles E. Campbell, Jr.
6 " Installing: :help glvs-install
9 " GetLatestVimScripts: 642 1 :AutoInstall: GetLatestVimScripts.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
16 echo "GetLatestVimScripts is not vi-compatible; not loaded (you need to set nocp)"
24 if exists("loaded_GetLatestVimScripts")
27 let g:loaded_GetLatestVimScripts= "v20"
29 " ---------------------------------------------------------------------
30 " Global Variables: {{{1
31 " allow user to change the command for obtaining scripts (does fetch work?)
32 if !exists("g:GetLatestVimScripts_wget")
33 let g:GetLatestVimScripts_wget= "wget"
35 if !exists("g:GetLatestVimScripts_options")
36 let g:GetLatestVimScripts_options= "-q -O"
38 if !exists("g:GetLatestVimScripts_allowautoinstall")
39 let g:GetLatestVimScripts_allowautoinstall= 1
43 "let g:GetLatestVimScripts_wget = "echo"
44 "let g:GetLatestVimScripts_options = "options"
46 " check if s:autoinstall is possible
48 if g:GetLatestVimScripts_allowautoinstall
50 if (has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")) && &shell != "bash"
51 " windows (but not cygwin/bash)
52 let s:dotvim= "vimfiles"
53 if !exists("g:GetLatestVimScripts_mv")
54 let g:GetLatestVimScripts_mv= "ren"
60 if !exists("g:GetLatestVimScripts_mv")
61 let g:GetLatestVimScripts_mv= "mv"
65 if exists('$HOME') && isdirectory(expand("$HOME")."/".s:dotvim)
66 let s:autoinstall= $HOME."/".s:dotvim
68 " call Decho("s:autoinstall<".s:autoinstall.">")
70 " call Decho("g:GetLatestVimScripts_allowautoinstall=".g:GetLatestVimScripts_allowautoinstall.": :AutoInstall: disabled")
73 " ---------------------------------------------------------------------
74 " Public Interface: {{{1
75 com! -nargs=0 GetLatestVimScripts call <SID>GetLatestVimScripts()
76 silent! com -nargs=0 GLVS call <SID>GetLatestVimScripts()
78 " ---------------------------------------------------------------------
79 " GetOneScript: (Get Latest Vim Script) this function operates {{{1
80 " on the current line, interpreting two numbers and text as
81 " ScriptID, SourceID, and Filename.
82 " It downloads any scripts that have newer versions from vim.sf.net.
83 fun! <SID>GetOneScript(...)
84 " call Dfunc("GetOneScript()")
86 " set options to allow progress to be shown on screen
92 " put current line on top-of-screen and interpret it into
93 " a script identifer : used to obtain webpage
94 " source identifier : used to identify current version
95 " and an associated comment: used to report on what's being considered
100 " call Decho("scriptid<".scriptid.">")
101 " call Decho("srcid <".srcid.">")
102 " call Decho("cmmnt <".cmmnt.">")
104 let curline = getline(".")
105 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)$'
107 let scriptid = substitute(curline,parsepat,'\1','e')
108 catch /^Vim\%((\a\+)\)\=:E486/
112 let srcid = substitute(curline,parsepat,'\2','e')
113 catch /^Vim\%((\a\+)\)\=:E486/
117 let cmmnt = substitute(curline,parsepat,'\3','e')
118 catch /^Vim\%((\a\+)\)\=:E486/
121 " call Decho("curline <".curline.">")
122 " call Decho("parsepat<".parsepat.">")
123 " call Decho("scriptid<".scriptid.">")
124 " call Decho("srcid <".srcid.">")
125 " call Decho("cmmnt <".cmmnt.">")
128 if scriptid == 0 || srcid == 0
129 " When looking for :AutoInstall: lines, skip scripts that
130 " have 0 0 scriptname
131 " call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
136 if cmmnt =~ ":AutoInstall:"
137 " call Decho("cmmnt<".cmmnt."> has :AutoInstall:...")
138 let aicmmnt= substitute(cmmnt,'\s\+:AutoInstall:\s\+',' ','')
139 " call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
140 if s:autoinstall != ""
141 let doautoinstall = g:GetLatestVimScripts_allowautoinstall
146 " call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
150 " call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
151 echomsg 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
153 " grab a copy of the plugin's vim.sf.net webpage
154 let scriptaddr = 'http://vim.sf.net/script.php?script_id='.scriptid
155 let tmpfile = tempname()
158 " make three tries at downloading the description
161 " call Decho("try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
162 if has("win32") || has("win16") || has("win95")
163 " call Decho("silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile.' "'.scriptaddr.'"')
164 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile.' "'.scriptaddr.'"'
166 " call Decho("silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile." '".scriptaddr."'")
167 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile." '".scriptaddr."'"
170 exe "silent vsplit ".tmpfile
175 " find the latest source-id in the plugin's webpage
177 let findpkg= search('Click on the package to download','W')
183 " call Decho(" --- end downloading tries while loop --- itry=".itry)
185 " testing: did finding /Click on the package.../ fail?
186 if findpkg == 0 || itry >= 4
193 let s:downerrors = s:downerrors + 1
194 " call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
195 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
196 " call Dret("GetOneScript : srch for /Click on the package/ failed")
199 " call Decho('found "Click on the package to download"')
201 let findsrcid= search('src_id=','W')
209 let s:downerrors = s:downerrors + 1
210 " call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
211 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
212 " call Dret("GetOneScript : srch for /src_id/ failed")
215 " call Decho('found "src_id=" in description page')
217 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
218 let latestsrcid= substitute(getline("."),srcidpat,'\1','')
219 let fname = substitute(getline("."),srcidpat,'\2','')
220 " call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> fname<".fname.">")
224 " convert the strings-of-numbers into numbers
225 let srcid = srcid + 0
226 let latestsrcid = latestsrcid + 0
227 " call Decho("srcid=".srcid." latestsrcid=".latestsrcid." fname<".fname.">")
229 " has the plugin's most-recent srcid increased, which indicates
230 " that it has been updated
231 if latestsrcid > srcid
232 let s:downloads= s:downloads + 1
233 if fname == bufname("%")
234 " GetLatestVimScript has to be careful about downloading itself
235 let fname= "NEW_".fname
238 " the plugin has been updated since we last obtained it, so download a new copy
239 " call Decho("...downloading new <".fname.">")
240 echomsg "...downloading new <".fname.">"
241 if has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")
242 " call Decho("windows: silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname.' "'.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.'"')
243 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname.' "'.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.'"'
245 " call Decho("unix: silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname." '".'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid."'")
246 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname." '".'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid."'"
249 " AutoInstall: only if doautoinstall is so indicating
251 " call Decho("attempting to do autoinstall: getcwd<".getcwd()."> filereadable(".fname.")=".filereadable(fname))
252 if filereadable(fname)
253 " call Decho("move <".fname."> to ".s:autoinstall)
254 " call Decho("DISABLED for testing")
255 exe "silent !"g:GetLatestVimScripts_mv." ".fname." ".s:autoinstall
256 let curdir= escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #")
257 exe "cd ".s:autoinstall
259 " call Decho("attempt to bunzip2 ".fname)
260 exe "silent !bunzip2 ".fname
261 let fname= substitute(fname,'\.bz2$','','')
262 elseif fname =~ '\.gz$'
263 " call Decho("attempt to gunzip ".fname)
264 exe "silent !gunzip ".fname
265 let fname= substitute(fname,'\.gz$','','')
268 " call Decho("attempt to unzip ".fname)
269 exe "silent !unzip -o".fname
270 elseif fname =~ '\.tar$'
271 " call Decho("attempt to untar ".fname)
272 exe "silent !tar -oxvf ".fname
275 " call Decho("attempt to simply move ".fname." to plugin")
276 exe "silent !".g:GetLatestVimScripts_mv." ".fname." plugin"
278 exe "helptags ../".s:dotvim."/doc"
283 " update the data in the <GetLatestVimScripts.dat> file
284 let modline=scriptid." ".latestsrcid." ".cmmnt
285 call setline(line("."),modline)
286 " call Decho("modline<".modline."> (updated GetLatestVimScripts.dat file)")
294 " call Dret("GetOneScript")
297 " ---------------------------------------------------------------------
298 " GetLatestVimScripts: this function gets the latest versions of {{{1
299 " scripts based on the list in
301 " (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat
302 fun! <SID>GetLatestVimScripts()
303 " call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">")
305 " insure that wget is executable
306 if executable(g:GetLatestVimScripts_wget) != 1
307 echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system"
308 " call Dret("GetLatestVimScripts : wget not executable/availble")
312 " Find the .../GetLatest sudirectory under the runtimepath
315 let datadir= substitute(rtplist,',.*$','','e')."/GetLatest"
316 if isdirectory(datadir)
317 " call Decho("found directory<".datadir.">")
322 let rtplist= substitute(rtplist,'^.\{-},','','e')
328 " Sanity checks: readability and writability
329 if !exists("datadir")
330 echoerr "Unable to find a GetLatest subdirectory on your runtimepath"
331 " call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
334 if filewritable(datadir) != 2
335 echoerr "Your ".datadir." isn't writable"
336 " call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
339 let datafile= datadir."/GetLatestVimScripts.dat"
340 if !filereadable(datafile)
341 echoerr "Your data file<".datafile."> isn't readable"
342 " call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
345 if !filewritable(datafile)
346 echoerr "Your data file<".datafile."> isn't writable"
347 " call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
350 " call Decho("datadir <".datadir.">")
351 " call Decho("datafile <".datafile.">")
353 " don't let any events interfere (like winmanager's, taglist's, etc)
357 " record current directory, change to datadir, open split window with
359 let origdir= getcwd()
360 exe "cd ".escape(substitute(datadir,'\','/','ge'),"|[]*'\" #")
362 exe "e ".escape(substitute(datafile,'\','/','ge'),"|[]*'\" #")
367 " Check on dependencies mentioned in plugins
369 " call Decho("searching plugins for GetLatestVimScripts dependencies")
370 let lastline = line("$")
371 let plugins = globpath(&rtp,"plugin/*.vim")
374 " call Decho("plugins<".plugins."> lastline#".lastline)
376 let plugin = substitute(plugins,'\n.*$','','e')
377 let plugins= (plugins =~ '\n')? substitute(plugins,'^.\{-}\n\(.*\)$','\1','e') : ""
379 " call Decho(".dependency checking<".plugin."> line$=".line("$"))
380 exe "silent r ".plugin
381 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
382 let newscript= substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
383 let llp1 = lastline+1
386 " found a "GetLatestVimScripts: # #" line in the script; check if its already in the datafile
387 let curline = line(".")
388 let noai_script = substitute(newscript,'\s*:AutoInstall:\s*','','e')
390 let srchline = search('\<'.noai_script.'\>','bW')
391 " call Decho("..newscript<".newscript."> noai_script<".noai_script."> srch=".srchline." lastline=".lastline)
394 " found a new script to permanently include in the datafile
396 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
398 echomsg "Appending <".@a."> to ".datafile." for ".newscript
399 " call Decho("..APPEND (".noai_script.")<".@a."> to GetLatestVimScripts.dat")
402 let curline = curline + 1
403 let foundscript = foundscript + 1
405 " call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
408 let curline = curline + 1
413 let llp1= lastline + 1
414 " call Decho(".deleting lines: ".llp1.",$d")
415 exe "silent! ".llp1.",$d"
422 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
424 " call Decho(" --- end of dependency checking loop --- ")
425 " call Decho("call GetOneScript on lines at end of datafile<".datafile.">")
427 /^-----/,$g/^\s*\d/call <SID>GetOneScript()
429 " Final report (an echomsg)
432 catch /^Vim\%((\a\+)\)\=:E114/
433 " call Dret("GetLatestVimScripts : nothing done!")
439 let s:msg = "Downloaded one updated script to <".datadir.">"
440 elseif s:downloads == 2
441 let s:msg= "Downloaded two updated scripts to <".datadir.">"
442 elseif s:downloads > 1
443 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
445 let s:msg= "Everything was already current"
448 let s:msg= s:msg." (".s:downerrors." downloading errors)"
458 " restore events and current directory
459 exe "cd ".escape(substitute(origdir,'\','/','ge'),"|[]*'\" #")
462 " call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
464 " ---------------------------------------------------------------------
466 " Restore Options: {{{1
470 " vim: ts=8 sts=2 fdm=marker nowrap