1" ---------------------------------------------------------------------
2" getscript.vim
3"  Author:	Charles E. Campbell, Jr.
4"  Date:	Nov 27, 2006
5"  Version:	23
6"  Installing:	:help glvs-install
7"  Usage:	:help glvs
8"
9" GetLatestVimScripts: 642 1 :AutoInstall: getscript.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
14if &cp
15 echoerr "GetLatestVimScripts is not vi-compatible; not loaded (you need to set nocp)"
16 finish
17endif
18let s:keepfo  = &fo
19let s:keepcpo = &cpo
20set cpo&vim
21
22if exists("g:loaded_getscript")
23 finish
24endif
25let g:loaded_getscript= "v23"
26
27" ---------------------------------------------------------------------
28"  Global Variables: {{{1
29" allow user to change the command for obtaining scripts (does fetch work?)
30if !exists("g:GetLatestVimScripts_wget")
31 if executable("wget")
32  let g:GetLatestVimScripts_wget= "wget"
33 elseif executable("curl")
34  let g:GetLatestVimScripts_wget= "curl"
35 else
36  let g:GetLatestVimScripts_wget    = 'echo "GetLatestVimScripts needs wget or curl"'
37  let g:GetLatestVimScripts_options = ""
38 endif
39endif
40
41" options that wget and curl require:
42if !exists("g:GetLatestVimScripts_options")
43 if g:GetLatestVimScripts_wget == "wget"
44  let g:GetLatestVimScripts_options= "-q -O"
45 elseif g:GetLatestVimScripts_wget == "curl"
46  let g:GetLatestVimScripts_options= "-s -O"
47 else
48  let g:GetLatestVimScripts_options= ""
49 endif
50endif
51
52" by default, allow autoinstall lines to work
53if !exists("g:GetLatestVimScripts_allowautoinstall")
54 let g:GetLatestVimScripts_allowautoinstall= 1
55endif
56
57"" For debugging:
58"let g:GetLatestVimScripts_wget    = "echo"
59"let g:GetLatestVimScripts_options = "options"
60
61" ---------------------------------------------------------------------
62" Check If AutoInstall Capable: {{{1
63let s:autoinstall= ""
64if g:GetLatestVimScripts_allowautoinstall
65
66 if (has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")) && &shell != "bash"
67  " windows (but not cygwin/bash)
68  let s:dotvim= "vimfiles"
69  if !exists("g:GetLatestVimScripts_mv")
70   let g:GetLatestVimScripts_mv= "ren"
71  endif
72
73 else
74  " unix
75  let s:dotvim= ".vim"
76  if !exists("g:GetLatestVimScripts_mv")
77   let g:GetLatestVimScripts_mv= "mv"
78  endif
79 endif
80
81 if exists('$HOME') && isdirectory(expand("$HOME")."/".s:dotvim)
82  let s:autoinstall= $HOME."/".s:dotvim
83 endif
84" call Decho("s:autoinstall<".s:autoinstall.">")
85"else "Decho
86" call Decho("g:GetLatestVimScripts_allowautoinstall=".g:GetLatestVimScripts_allowautoinstall.": :AutoInstall: disabled")
87endif
88
89" ---------------------------------------------------------------------
90"  Public Interface: {{{1
91com!        -nargs=0 GetLatestVimScripts call getscript#GetLatestVimScripts()
92com!        -nargs=0 GetScript           call getscript#GetLatestVimScripts()
93silent! com -nargs=0 GLVS                call getscript#GetLatestVimScripts()
94
95" ---------------------------------------------------------------------
96"  GetOneScript: (Get Latest Vim Script) this function operates {{{1
97"    on the current line, interpreting two numbers and text as
98"    ScriptID, SourceID, and Filename.
99"    It downloads any scripts that have newer versions from vim.sf.net.
100fun! s:GetOneScript(...)
101"   call Dfunc("GetOneScript()")
102
103 " set options to allow progress to be shown on screen
104  let t_ti= &t_ti
105  let t_te= &t_te
106  let rs  = &rs
107  set t_ti= t_te= nors
108
109 " put current line on top-of-screen and interpret it into
110 " a      script identifer  : used to obtain webpage
111 "        source identifier : used to identify current version
112 " and an associated comment: used to report on what's being considered
113  if a:0 >= 3
114   let scriptid = a:1
115   let srcid    = a:2
116   let fname    = a:3
117   let cmmnt    = ""
118"   call Decho("scriptid<".scriptid.">")
119"   call Decho("srcid   <".srcid.">")
120"   call Decho("fname   <".fname.">")
121  else
122   let curline  = getline(".")
123   if curline =~ '^\s*#'
124"    call Dret("GetOneScript : skipping a pure comment line")
125    return
126   endif
127   let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$'
128   try
129    let scriptid = substitute(curline,parsepat,'\1','e')
130   catch /^Vim\%((\a\+)\)\=:E486/
131    let scriptid= 0
132   endtry
133   try
134    let srcid    = substitute(curline,parsepat,'\2','e')
135   catch /^Vim\%((\a\+)\)\=:E486/
136    let srcid= 0
137   endtry
138   try
139    let fname= substitute(curline,parsepat,'\3','e')
140   catch /^Vim\%((\a\+)\)\=:E486/
141    let fname= ""
142   endtry
143   try
144    let cmmnt= substitute(curline,parsepat,'\4','e')
145   catch /^Vim\%((\a\+)\)\=:E486/
146    let cmmnt= ""
147   endtry
148"   call Decho("curline <".curline.">")
149"   call Decho("parsepat<".parsepat.">")
150"   call Decho("scriptid<".scriptid.">")
151"   call Decho("srcid   <".srcid.">")
152"   call Decho("fname   <".fname.">")
153  endif
154
155  if scriptid == 0 || srcid == 0
156   " When looking for :AutoInstall: lines, skip scripts that
157   " have  0 0 scriptname
158"   call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
159   return
160  endif
161
162  let doautoinstall= 0
163  if fname =~ ":AutoInstall:"
164"   call Decho("fname<".fname."> has :AutoInstall:...")
165   let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','')
166"   call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
167   if s:autoinstall != ""
168    let doautoinstall = g:GetLatestVimScripts_allowautoinstall
169   endif
170  else
171   let aicmmnt= fname
172  endif
173"  call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
174
175  exe "norm z\<CR>"
176  redraw!
177"  call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
178  echomsg 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
179
180  " grab a copy of the plugin's vim.sf.net webpage
181  let scriptaddr = 'http://vim.sf.net/script.php?script_id='.scriptid
182  let tmpfile    = tempname()
183  let v:errmsg   = ""
184
185  " make three tries at downloading the description
186  let itry= 1
187  while itry <= 3
188"   	call Decho("try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
189  	if has("win32") || has("win16") || has("win95")
190"     call Decho("silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile.' "'.scriptaddr.'"')
191     exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile.' "'.scriptaddr.'"'
192	else
193"     call Decho("silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile." '".scriptaddr."'")
194     exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".tmpfile." '".scriptaddr."'"
195	endif
196	if itry == 1
197    exe "silent vsplit ".tmpfile
198	else
199	 silent! e %
200	endif
201
202   " find the latest source-id in the plugin's webpage
203   silent! 1
204   let findpkg= search('Click on the package to download','W')
205   if findpkg > 0
206    break
207   endif
208   let itry= itry + 1
209  endwhile
210"  call Decho(" --- end downloading tries while loop --- itry=".itry)
211
212  " testing: did finding /Click on the package.../ fail?
213  if findpkg == 0 || itry >= 4
214    silent q!
215    call delete(tmpfile)
216   " restore options
217	let &t_ti        = t_ti
218	let &t_te        = t_te
219	let &rs          = rs
220	let s:downerrors = s:downerrors + 1
221"  	call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
222  	echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
223"	call Dret("GetOneScript : srch for /Click on the package/ failed")
224  	return
225  endif
226"  call Decho('found "Click on the package to download"')
227
228  let findsrcid= search('src_id=','W')
229  if findsrcid == 0
230    silent q!
231    call delete(tmpfile)
232   " restore options
233	let &t_ti        = t_ti
234	let &t_te        = t_te
235	let &rs          = rs
236	let s:downerrors = s:downerrors + 1
237"  	call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
238  	echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
239"	call Dret("GetOneScript : srch for /src_id/ failed")
240  	return
241  endif
242"  call Decho('found "src_id=" in description page')
243
244  let srcidpat   = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
245  let latestsrcid= substitute(getline("."),srcidpat,'\1','')
246  let fname      = substitute(getline("."),srcidpat,'\2','')
247"  call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> fname<".fname.">")
248  silent q!
249  call delete(tmpfile)
250
251  " convert the strings-of-numbers into numbers
252  let srcid       = srcid       + 0
253  let latestsrcid = latestsrcid + 0
254"   call Decho("srcid=".srcid." latestsrcid=".latestsrcid." fname<".fname.">")
255
256  " has the plugin's most-recent srcid increased, which indicates
257  " that it has been updated
258  if latestsrcid > srcid
259   let s:downloads= s:downloads + 1
260   if fname == bufname("%")
261    " GetLatestVimScript has to be careful about downloading itself
262    let fname= "NEW_".fname
263   endif
264
265   " the plugin has been updated since we last obtained it, so download a new copy
266"   call Decho("...downloading new <".fname.">")
267   echomsg "...downloading new <".fname.">"
268   if has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")
269"    call Decho("windows: silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname.' "'.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.'"')
270    exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname.' "'.'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid.'"'
271   else
272"    call Decho("unix: silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname." '".'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid."'")
273    exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".fname." '".'http://vim.sf.net/scripts/download_script.php?src_id='.latestsrcid."'"
274   endif
275
276   " AutoInstall: only if doautoinstall is so indicating
277   if doautoinstall
278"   	call Decho("attempting to do autoinstall: getcwd<".getcwd()."> filereadable(".fname.")=".filereadable(fname))
279	if filereadable(fname)
280"	 call Decho("move <".fname."> to ".s:autoinstall)
281"	 call Decho("DISABLED for testing")
282   	 exe "silent !".g:GetLatestVimScripts_mv." ".fname." ".s:autoinstall
283	 let curdir= escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #")
284	 exe "cd ".s:autoinstall
285	 if fname =~ '\.bz2$'
286"	  call Decho("attempt to bunzip2 ".fname)
287	  exe "silent !bunzip2 ".fname
288	  let fname= substitute(fname,'\.bz2$','','')
289	 elseif fname =~ '\.gz$'
290"	  call Decho("attempt to gunzip ".fname)
291	  exe "silent !gunzip ".fname
292	  let fname= substitute(fname,'\.gz$','','')
293	 endif
294	 if fname =~ '\.zip$'
295"	  call Decho("attempt to unzip ".fname)
296	  exe "silent !unzip -o".fname
297	 elseif fname =~ '\.tar$'
298"	  call Decho("attempt to untar ".fname)
299	  exe "silent !tar -xvf ".fname
300	 elseif fname =~ '\.vba$'
301"	  call Decho("attempt to handle a vimball: ".fname)
302          1split
303	  exe "e ".fname
304	  so %
305	  q
306	 endif
307	 if fname =~ '.vim$'
308"	  call Decho("attempt to simply move ".fname." to plugin")
309	  exe "silent !".g:GetLatestVimScripts_mv." ".fname." plugin"
310	 endif
311	 let docdir= substitute(&rtp,',.*','','e')."/doc"
312"	 call Decho("helptags docdir<".docdir.">")
313	 exe "helptags ".docdir
314	 exe "cd ".curdir
315	endif
316   endif
317
318   " update the data in the <GetLatestVimScripts.dat> file
319   let modline=scriptid." ".latestsrcid." ".fname.cmmnt
320   call setline(line("."),modline)
321"   call Decho("modline<".modline."> (updated GetLatestVimScripts.dat file)")
322  endif
323
324 " restore options
325  let &t_ti= t_ti
326  let &t_te= t_te
327  let &rs  = rs
328
329"  call Dret("GetOneScript")
330endfun
331
332" ---------------------------------------------------------------------
333" GetLatestVimScripts: this function gets the latest versions of {{{1
334"                      scripts based on the list in
335"   (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat
336fun! getscript#GetLatestVimScripts()
337"  call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">")
338
339" insure that wget is executable
340  if executable(g:GetLatestVimScripts_wget) != 1
341   echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system"
342"   call Dret("GetLatestVimScripts : wget not executable/availble")
343   return
344  endif
345
346  " Find the .../GetLatest subdirectory under the runtimepath
347  for datadir in split(&rtp,',') + ['']
348   if isdirectory(datadir."/GetLatest")
349"    call Decho("found directory<".datadir.">")
350    let datadir= datadir . "/GetLatest"
351    break
352   endif
353   if filereadable(datadir."GetLatestVimScripts.dat")
354"   	call Decho("found ".datadir."/GetLatestVimScripts.dat")
355   	break
356   endif
357  endfor
358  " Sanity checks: readability and writability
359  if datadir == ""
360   echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install'
361"   call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
362   return
363  endif
364
365  if filewritable(datadir) != 2
366   echoerr "(getLatestVimScripts) Your ".datadir." isn't writable"
367"   call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
368   return
369  endif
370  let datafile= datadir."/GetLatestVimScripts.dat"
371  if !filereadable(datafile)
372   echoerr "Your data file<".datafile."> isn't readable"
373"   call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
374   return
375  endif
376  if !filewritable(datafile)
377   echoerr "Your data file<".datafile."> isn't writable"
378"   call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
379   return
380  endif
381"  call Decho("datadir  <".datadir.">")
382"  call Decho("datafile <".datafile.">")
383
384  " don't let any events interfere (like winmanager's, taglist's, etc)
385  let eikeep= &ei
386  set ei=all
387
388  " record current directory, change to datadir, open split window with
389  " datafile
390  let origdir= getcwd()
391  exe "cd ".escape(substitute(datadir,'\','/','ge'),"|[]*'\" #")
392  split
393  exe "e ".escape(substitute(datafile,'\','/','ge'),"|[]*'\" #")
394  res 1000
395  let s:downloads = 0
396  let s:downerrors= 0
397
398  " Check on dependencies mentioned in plugins
399"  call Decho(" ")
400"  call Decho("searching plugins for GetLatestVimScripts dependencies")
401  let lastline    = line("$")
402  let plugins     = globpath(&rtp,"plugin/*.vim")
403  let foundscript = 0
404
405"  call Decho("plugins<".plugins."> lastline#".lastline)
406  while plugins != ""
407   let plugin = substitute(plugins,'\n.*$','','e')
408   let plugins= (plugins =~ '\n')? substitute(plugins,'^.\{-}\n\(.*\)$','\1','e') : ""
409   $
410"   call Decho(".dependency checking<".plugin."> line$=".line("$"))
411   exe "silent r ".plugin
412   while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
413    let newscript= substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
414    let llp1     = lastline+1
415
416	if newscript !~ '^"'
417	 " found a "GetLatestVimScripts: # #" line in the script; check if its already in the datafile
418	 let curline     = line(".")
419	 let noai_script = substitute(newscript,'\s*:AutoInstall:\s*','','e')
420	 exe llp1
421	 let srchline    = search('\<'.noai_script.'\>','bW')
422"	 call Decho("..newscript<".newscript."> noai_script<".noai_script."> srch=".srchline." lastline=".lastline)
423
424	 if srchline == 0
425	  " found a new script to permanently include in the datafile
426	  let keep_rega   = @a
427	  let @a          = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
428	  exe lastline."put a"
429	  echomsg "Appending <".@a."> to ".datafile." for ".newscript
430"	  call Decho("..APPEND (".noai_script.")<".@a."> to GetLatestVimScripts.dat")
431	  let @a          = keep_rega
432	  let lastline    = llp1
433	  let curline     = curline     + 1
434	  let foundscript = foundscript + 1
435"	 else	" Decho
436"	  call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
437	 endif
438
439	 let curline = curline + 1
440	 exe curline
441	endif
442
443   endwhile
444   let llp1= lastline + 1
445"   call Decho(".deleting lines: ".llp1.",$d")
446   exe "silent! ".llp1.",$d"
447  endwhile
448
449  if foundscript == 0
450   set nomod
451  endif
452
453  " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
454  set lz
455"  call Decho(" --- end of dependency checking loop --- ")
456"  call Decho("call GetOneScript on lines at end of datafile<".datafile.">")
457  1
458  /^-----/,$g/^\s*\d/call <SID>GetOneScript()
459
460  " Final report (an echomsg)
461  try
462   silent! ?^-------?
463  catch /^Vim\%((\a\+)\)\=:E114/
464"   call Dret("GetLatestVimScripts : nothing done!")
465   return
466  endtry
467  exe "norm! kz\<CR>"
468  redraw!
469  let s:msg = ""
470  if s:downloads == 1
471  let s:msg = "Downloaded one updated script to <".datadir.">"
472  elseif s:downloads == 2
473   let s:msg= "Downloaded two updated scripts to <".datadir.">"
474  elseif s:downloads > 1
475   let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
476  else
477   let s:msg= "Everything was already current"
478  endif
479  if s:downerrors > 0
480   let s:msg= s:msg." (".s:downerrors." downloading errors)"
481  endif
482  echomsg s:msg
483  " save the file
484  if &mod
485   silent! w!
486  endif
487  q
488
489  " restore events and current directory
490  exe "cd ".escape(substitute(origdir,'\','/','ge'),"|[]*'\" #")
491  let &ei= eikeep
492  set nolz
493"  call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
494endfun
495" ---------------------------------------------------------------------
496
497" Restore Options: {{{1
498let &fo = s:keepfo
499let &cpo= s:keepcpo
500
501" vim: ts=8 sts=2 fdm=marker nowrap
502