blob: 34f5970c3ed481a9e74d00ab84991b8e7deeaac3 [file] [log] [blame]
Bram Moolenaar9964e462007-05-05 17:54:07 +00001" ---------------------------------------------------------------------
2" getscript.vim
3" Author: Charles E. Campbell, Jr.
Bram Moolenaar6be7f872012-01-20 21:08:56 +01004" Date: Jan 17, 2012
5" Version: 34
Bram Moolenaar9964e462007-05-05 17:54:07 +00006" Installing: :help glvs-install
7" Usage: :help glvs
8"
9" GetLatestVimScripts: 642 1 :AutoInstall: getscript.vim
Bram Moolenaar9e368db2007-05-12 13:25:01 +000010"redraw!|call inputsave()|call input("Press <cr> to continue")|call inputrestore()
Bram Moolenaar9964e462007-05-05 17:54:07 +000011" ---------------------------------------------------------------------
12" Initialization: {{{1
13" if you're sourcing this file, surely you can't be
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000014" expecting vim to be in its vi-compatible mode!
Bram Moolenaar5c736222010-01-06 20:54:52 +010015if exists("g:loaded_getscript")
16 finish
17endif
Bram Moolenaar6be7f872012-01-20 21:08:56 +010018let g:loaded_getscript= "v34"
Bram Moolenaar9964e462007-05-05 17:54:07 +000019if &cp
20 echoerr "GetLatestVimScripts is not vi-compatible; not loaded (you need to set nocp)"
21 finish
22endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010023if v:version < 702
24 echohl WarningMsg
25 echo "***warning*** this version of getscript needs vim 7.2"
26 echohl Normal
27 finish
28endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000029let s:keepcpo = &cpo
30set cpo&vim
Bram Moolenaar82038d72007-05-10 17:15:45 +000031"DechoTabOn
Bram Moolenaar9964e462007-05-05 17:54:07 +000032
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000033" ---------------------------
34" Global Variables: {{{1
35" ---------------------------
36" Cygwin Detection ------- {{{2
37if !exists("g:getscript_cygwin")
38 if has("win32") || has("win95") || has("win64") || has("win16")
39 if &shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$'
40 let g:getscript_cygwin= 1
41 else
42 let g:getscript_cygwin= 0
43 endif
44 else
45 let g:getscript_cygwin= 0
46 endif
47endif
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000048
49" wget vs curl {{{2
Bram Moolenaar9964e462007-05-05 17:54:07 +000050if !exists("g:GetLatestVimScripts_wget")
51 if executable("wget")
52 let g:GetLatestVimScripts_wget= "wget"
53 elseif executable("curl")
54 let g:GetLatestVimScripts_wget= "curl"
55 else
56 let g:GetLatestVimScripts_wget = 'echo "GetLatestVimScripts needs wget or curl"'
57 let g:GetLatestVimScripts_options = ""
58 endif
59endif
60
61" options that wget and curl require:
62if !exists("g:GetLatestVimScripts_options")
63 if g:GetLatestVimScripts_wget == "wget"
64 let g:GetLatestVimScripts_options= "-q -O"
65 elseif g:GetLatestVimScripts_wget == "curl"
66 let g:GetLatestVimScripts_options= "-s -O"
67 else
68 let g:GetLatestVimScripts_options= ""
69 endif
70endif
71
72" by default, allow autoinstall lines to work
73if !exists("g:GetLatestVimScripts_allowautoinstall")
74 let g:GetLatestVimScripts_allowautoinstall= 1
75endif
76
77"" For debugging:
78"let g:GetLatestVimScripts_wget = "echo"
79"let g:GetLatestVimScripts_options = "options"
80
81" ---------------------------------------------------------------------
82" Check If AutoInstall Capable: {{{1
83let s:autoinstall= ""
84if g:GetLatestVimScripts_allowautoinstall
85
86 if (has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")) && &shell != "bash"
87 " windows (but not cygwin/bash)
88 let s:dotvim= "vimfiles"
89 if !exists("g:GetLatestVimScripts_mv")
90 let g:GetLatestVimScripts_mv= "ren"
91 endif
92
93 else
94 " unix
95 let s:dotvim= ".vim"
96 if !exists("g:GetLatestVimScripts_mv")
97 let g:GetLatestVimScripts_mv= "mv"
98 endif
99 endif
100
Bram Moolenaar5c736222010-01-06 20:54:52 +0100101 if exists("g:GetLatestVimScripts_autoinstalldir") && isdirectory(g:GetLatestVimScripts_autoinstalldir)
102 let s:autoinstall= g:GetLatestVimScripts_autoinstalldir"
103 elseif exists('$HOME') && isdirectory(expand("$HOME")."/".s:dotvim)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000104 let s:autoinstall= $HOME."/".s:dotvim
105 endif
106" call Decho("s:autoinstall<".s:autoinstall.">")
107"else "Decho
108" call Decho("g:GetLatestVimScripts_allowautoinstall=".g:GetLatestVimScripts_allowautoinstall.": :AutoInstall: disabled")
109endif
110
111" ---------------------------------------------------------------------
112" Public Interface: {{{1
113com! -nargs=0 GetLatestVimScripts call getscript#GetLatestVimScripts()
114com! -nargs=0 GetScript call getscript#GetLatestVimScripts()
115silent! com -nargs=0 GLVS call getscript#GetLatestVimScripts()
116
117" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +0000118" GetLatestVimScripts: this function gets the latest versions of {{{1
119" scripts based on the list in
120" (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat
121fun! getscript#GetLatestVimScripts()
122" call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">")
123
124" insure that wget is executable
125 if executable(g:GetLatestVimScripts_wget) != 1
126 echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system"
127" call Dret("GetLatestVimScripts : wget not executable/availble")
128 return
129 endif
130
Bram Moolenaarc236c162008-07-13 17:41:49 +0000131 " insure that fnameescape() is available
132 if !exists("*fnameescape")
133 echoerr "GetLatestVimScripts needs fnameescape() (provided by 7.1.299 or later)"
134 return
135 endif
136
Bram Moolenaar9964e462007-05-05 17:54:07 +0000137 " Find the .../GetLatest subdirectory under the runtimepath
138 for datadir in split(&rtp,',') + ['']
139 if isdirectory(datadir."/GetLatest")
140" call Decho("found directory<".datadir.">")
141 let datadir= datadir . "/GetLatest"
142 break
143 endif
144 if filereadable(datadir."GetLatestVimScripts.dat")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000145" call Decho("found ".datadir."/GetLatestVimScripts.dat")
146 break
Bram Moolenaar9964e462007-05-05 17:54:07 +0000147 endif
148 endfor
Bram Moolenaar82038d72007-05-10 17:15:45 +0000149
Bram Moolenaar9964e462007-05-05 17:54:07 +0000150 " Sanity checks: readability and writability
151 if datadir == ""
152 echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install'
153" call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
154 return
155 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000156 if filewritable(datadir) != 2
157 echoerr "(getLatestVimScripts) Your ".datadir." isn't writable"
158" call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
159 return
160 endif
161 let datafile= datadir."/GetLatestVimScripts.dat"
162 if !filereadable(datafile)
163 echoerr "Your data file<".datafile."> isn't readable"
164" call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
165 return
166 endif
167 if !filewritable(datafile)
168 echoerr "Your data file<".datafile."> isn't writable"
169" call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
170 return
171 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100172 " --------------------
173 " Passed sanity checks
174 " --------------------
175
Bram Moolenaar9964e462007-05-05 17:54:07 +0000176" call Decho("datadir <".datadir.">")
177" call Decho("datafile <".datafile.">")
178
Bram Moolenaar5c736222010-01-06 20:54:52 +0100179 " don't let any event handlers interfere (like winmanager's, taglist's, etc)
180 let eikeep = &ei
181 let hlskeep = &hls
182 let acdkeep = &acd
183 set ei=all hls&vim noacd
Bram Moolenaar9964e462007-05-05 17:54:07 +0000184
Bram Moolenaar5c736222010-01-06 20:54:52 +0100185 " Edit the datafile (ie. GetLatestVimScripts.dat):
186 " 1. record current directory (origdir),
187 " 2. change directory to datadir,
188 " 3. split window
189 " 4. edit datafile
Bram Moolenaar9964e462007-05-05 17:54:07 +0000190 let origdir= getcwd()
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000191" call Decho("exe cd ".fnameescape(substitute(datadir,'\','/','ge')))
192 exe "cd ".fnameescape(substitute(datadir,'\','/','ge'))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000193 split
Bram Moolenaar5c736222010-01-06 20:54:52 +0100194" call Decho("exe e ".fnameescape(substitute(datafile,'\','/','ge')))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000195 exe "e ".fnameescape(substitute(datafile,'\','/','ge'))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000196 res 1000
197 let s:downloads = 0
198 let s:downerrors= 0
199
200 " Check on dependencies mentioned in plugins
201" call Decho(" ")
202" call Decho("searching plugins for GetLatestVimScripts dependencies")
203 let lastline = line("$")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000204" call Decho("lastline#".lastline)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100205 let firstdir = substitute(&rtp,',.*$','','')
206 let plugins = split(globpath(firstdir,"plugin/*.vim"),'\n')
207 let plugins = plugins + split(globpath(firstdir,"AsNeeded/*.vim"),'\n')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000208 let foundscript = 0
209
Bram Moolenaar5c736222010-01-06 20:54:52 +0100210 " this loop updates the GetLatestVimScripts.dat file
211 " with dependencies explicitly mentioned in the plugins
212 " via GetLatestVimScripts: ... lines
213 " It reads the plugin script at the end of the GetLatestVimScripts.dat
214 " file, examines it, and then removes it.
Bram Moolenaar82038d72007-05-10 17:15:45 +0000215 for plugin in plugins
Bram Moolenaar5c736222010-01-06 20:54:52 +0100216" call Decho(" ")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000217" call Decho("plugin<".plugin.">")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000218
Bram Moolenaar82038d72007-05-10 17:15:45 +0000219 " read plugin in
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000220 " evidently a :r creates a new buffer (the "#" buffer) that is subsequently unused -- bwiping it
Bram Moolenaar9964e462007-05-05 17:54:07 +0000221 $
222" call Decho(".dependency checking<".plugin."> line$=".line("$"))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100223" call Decho("..exe silent r ".fnameescape(plugin))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000224 exe "silent r ".fnameescape(plugin)
225 exe "silent bwipe ".bufnr("#")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000226
Bram Moolenaar9964e462007-05-05 17:54:07 +0000227 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
Bram Moolenaar5c736222010-01-06 20:54:52 +0100228 let depscript = substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
229 let depscriptid = substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\(\d\+\)\s\+.*$','\1','')
230 let llp1 = lastline+1
231" call Decho("..depscript<".depscript.">")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000232
Bram Moolenaar5c736222010-01-06 20:54:52 +0100233 " found a "GetLatestVimScripts: # #" line in the script;
234 " check if its already in the datafile by searching backwards from llp1,
235 " the (prior to reading in the plugin script) last line plus one of the GetLatestVimScripts.dat file,
236 " for the script-id with no wrapping allowed.
237 let curline = line(".")
238 let noai_script = substitute(depscript,'\s*:AutoInstall:\s*','','e')
239 exe llp1
240 let srchline = search('^\s*'.depscriptid.'\s\+\d\+\s\+.*$','bW')
241 if srchline == 0
242 " this second search is taken when, for example, a 0 0 scriptname is to be skipped over
243 let srchline= search('\<'.noai_script.'\>','bW')
Bram Moolenaar82038d72007-05-10 17:15:45 +0000244 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100245" call Decho("..noai_script<".noai_script."> depscriptid#".depscriptid." srchline#".srchline." curline#".line(".")." lastline#".lastline)
246
247 if srchline == 0
248 " found a new script to permanently include in the datafile
249 let keep_rega = @a
250 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
251 echomsg "Appending <".@a."> to ".datafile." for ".depscript
252" call Decho("..Appending <".@a."> to ".datafile." for ".depscript)
253 exe lastline."put a"
254 let @a = keep_rega
255 let lastline = llp1
256 let curline = curline + 1
257 let foundscript = foundscript + 1
258" else " Decho
259" call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
260 endif
261
262 let curline = curline + 1
263 exe curline
Bram Moolenaar9964e462007-05-05 17:54:07 +0000264 endwhile
Bram Moolenaar82038d72007-05-10 17:15:45 +0000265
Bram Moolenaar5c736222010-01-06 20:54:52 +0100266 " llp1: last line plus one
Bram Moolenaar9964e462007-05-05 17:54:07 +0000267 let llp1= lastline + 1
268" call Decho(".deleting lines: ".llp1.",$d")
269 exe "silent! ".llp1.",$d"
Bram Moolenaar82038d72007-05-10 17:15:45 +0000270 endfor
271" call Decho("--- end dependency checking loop --- foundscript=".foundscript)
272" call Decho(" ")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000273" call Dredir("BUFFER TEST (GetLatestVimScripts 1)","ls!")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000274
275 if foundscript == 0
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000276 setlocal nomod
Bram Moolenaar9964e462007-05-05 17:54:07 +0000277 endif
278
Bram Moolenaar5c736222010-01-06 20:54:52 +0100279 " --------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +0000280 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
Bram Moolenaar5c736222010-01-06 20:54:52 +0100281 " --------------------------------------------------------------------
Bram Moolenaar82038d72007-05-10 17:15:45 +0000282" call Decho("begin: checking out-of-date scripts using datafile<".datafile.">")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000283 setlocal lz
Bram Moolenaar9964e462007-05-05 17:54:07 +0000284 1
Bram Moolenaar82038d72007-05-10 17:15:45 +0000285" /^-----/,$g/^\s*\d/call Decho(getline("."))
286 1
287 /^-----/,$g/^\s*\d/call s:GetOneScript()
288" call Decho("--- end out-of-date checking --- ")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000289
290 " Final report (an echomsg)
291 try
292 silent! ?^-------?
293 catch /^Vim\%((\a\+)\)\=:E114/
294" call Dret("GetLatestVimScripts : nothing done!")
295 return
296 endtry
297 exe "norm! kz\<CR>"
298 redraw!
299 let s:msg = ""
300 if s:downloads == 1
301 let s:msg = "Downloaded one updated script to <".datadir.">"
302 elseif s:downloads == 2
303 let s:msg= "Downloaded two updated scripts to <".datadir.">"
304 elseif s:downloads > 1
305 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
306 else
307 let s:msg= "Everything was already current"
308 endif
309 if s:downerrors > 0
310 let s:msg= s:msg." (".s:downerrors." downloading errors)"
311 endif
312 echomsg s:msg
313 " save the file
314 if &mod
315 silent! w!
316 endif
317 q
318
319 " restore events and current directory
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000320 exe "cd ".fnameescape(substitute(origdir,'\','/','ge'))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100321 let &ei = eikeep
322 let &hls = hlskeep
323 let &acd = acdkeep
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000324 setlocal nolz
325" call Dredir("BUFFER TEST (GetLatestVimScripts 2)","ls!")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000326" call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
327endfun
Bram Moolenaar9964e462007-05-05 17:54:07 +0000328
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000329" ---------------------------------------------------------------------
330" GetOneScript: (Get Latest Vim Script) this function operates {{{1
331" on the current line, interpreting two numbers and text as
332" ScriptID, SourceID, and Filename.
Bram Moolenaar5c736222010-01-06 20:54:52 +0100333" It downloads any scripts that have newer versions from vim.sourceforge.net.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000334fun! s:GetOneScript(...)
335" call Dfunc("GetOneScript()")
336
337 " set options to allow progress to be shown on screen
338 let rega= @a
339 let t_ti= &t_ti
340 let t_te= &t_te
341 let rs = &rs
342 set t_ti= t_te= nors
343
344 " put current line on top-of-screen and interpret it into
345 " a script identifer : used to obtain webpage
346 " source identifier : used to identify current version
347 " and an associated comment: used to report on what's being considered
348 if a:0 >= 3
349 let scriptid = a:1
350 let srcid = a:2
351 let fname = a:3
352 let cmmnt = ""
353" call Decho("scriptid<".scriptid.">")
354" call Decho("srcid <".srcid.">")
355" call Decho("fname <".fname.">")
356 else
357 let curline = getline(".")
358 if curline =~ '^\s*#'
359 let @a= rega
360" call Dret("GetOneScript : skipping a pure comment line")
361 return
362 endif
363 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$'
364 try
365 let scriptid = substitute(curline,parsepat,'\1','e')
366 catch /^Vim\%((\a\+)\)\=:E486/
367 let scriptid= 0
368 endtry
369 try
370 let srcid = substitute(curline,parsepat,'\2','e')
371 catch /^Vim\%((\a\+)\)\=:E486/
372 let srcid= 0
373 endtry
374 try
375 let fname= substitute(curline,parsepat,'\3','e')
376 catch /^Vim\%((\a\+)\)\=:E486/
377 let fname= ""
378 endtry
379 try
380 let cmmnt= substitute(curline,parsepat,'\4','e')
381 catch /^Vim\%((\a\+)\)\=:E486/
382 let cmmnt= ""
383 endtry
384" call Decho("curline <".curline.">")
385" call Decho("parsepat<".parsepat.">")
386" call Decho("scriptid<".scriptid.">")
387" call Decho("srcid <".srcid.">")
388" call Decho("fname <".fname.">")
389 endif
390
Bram Moolenaar5c736222010-01-06 20:54:52 +0100391 " plugin author protection from downloading his/her own scripts atop their latest work
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000392 if scriptid == 0 || srcid == 0
393 " When looking for :AutoInstall: lines, skip scripts that have 0 0 scriptname
394 let @a= rega
395" call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
396 return
397 endif
398
399 let doautoinstall= 0
400 if fname =~ ":AutoInstall:"
401" call Decho("case AutoInstall: fname<".fname.">")
402 let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','')
403" call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
404 if s:autoinstall != ""
405 let doautoinstall = g:GetLatestVimScripts_allowautoinstall
406 endif
407 else
408 let aicmmnt= fname
409 endif
410" call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
411
412 exe "norm z\<CR>"
413 redraw!
414" call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
415 echo 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
416
Bram Moolenaar5c736222010-01-06 20:54:52 +0100417 " grab a copy of the plugin's vim.sourceforge.net webpage
418 let scriptaddr = 'http://vim.sourceforge.net/script.php?script_id='.scriptid
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000419 let tmpfile = tempname()
420 let v:errmsg = ""
421
422 " make up to three tries at downloading the description
423 let itry= 1
424 while itry <= 3
Bram Moolenaar5c736222010-01-06 20:54:52 +0100425" call Decho(".try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000426 if has("win32") || has("win16") || has("win95")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100427" call Decho(".new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile).' '.shellescape(scriptaddr)."|bw!")
428 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile).' '.shellescape(scriptaddr)|bw!
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000429 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100430" call Decho(".exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile)." ".shellescape(scriptaddr))
431 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile)." ".shellescape(scriptaddr)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000432 endif
433 if itry == 1
434 exe "silent vsplit ".fnameescape(tmpfile)
435 else
436 silent! e %
437 endif
438 setlocal bh=wipe
439
440 " find the latest source-id in the plugin's webpage
441 silent! 1
442 let findpkg= search('Click on the package to download','W')
443 if findpkg > 0
444 break
445 endif
446 let itry= itry + 1
447 endwhile
448" call Decho(" --- end downloading tries while loop --- itry=".itry)
449
450 " testing: did finding "Click on the package..." fail?
451 if findpkg == 0 || itry >= 4
452 silent q!
453 call delete(tmpfile)
454 " restore options
455 let &t_ti = t_ti
456 let &t_te = t_te
457 let &rs = rs
458 let s:downerrors = s:downerrors + 1
459" call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
460 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
461" call Dret("GetOneScript : srch for /Click on the package/ failed")
462 let @a= rega
463 return
464 endif
465" call Decho('found "Click on the package to download"')
466
467 let findsrcid= search('src_id=','W')
468 if findsrcid == 0
469 silent q!
470 call delete(tmpfile)
471 " restore options
472 let &t_ti = t_ti
473 let &t_te = t_te
474 let &rs = rs
475 let s:downerrors = s:downerrors + 1
476" call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
477 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
478 let @a= rega
479" call Dret("GetOneScript : srch for /src_id/ failed")
480 return
481 endif
482" call Decho('found "src_id=" in description page')
483
484 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
485 let latestsrcid= substitute(getline("."),srcidpat,'\1','')
486 let sname = substitute(getline("."),srcidpat,'\2','') " script name actually downloaded
487" call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> sname<".sname.">")
488 silent q!
489 call delete(tmpfile)
490
491 " convert the strings-of-numbers into numbers
492 let srcid = srcid + 0
493 let latestsrcid = latestsrcid + 0
494" call Decho("srcid=".srcid." latestsrcid=".latestsrcid." sname<".sname.">")
495
Bram Moolenaar5c736222010-01-06 20:54:52 +0100496 " has the plugin's most-recent srcid increased, which indicates that it has been updated
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000497 if latestsrcid > srcid
498" call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."]: need to update <".sname.">")
499
500 let s:downloads= s:downloads + 1
501 if sname == bufname("%")
502 " GetLatestVimScript has to be careful about downloading itself
503 let sname= "NEW_".sname
504 endif
505
Bram Moolenaar5c736222010-01-06 20:54:52 +0100506 " -----------------------------------------------------------------------------
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000507 " the plugin has been updated since we last obtained it, so download a new copy
Bram Moolenaar5c736222010-01-06 20:54:52 +0100508 " -----------------------------------------------------------------------------
509" call Decho(".downloading new <".sname.">")
510 echomsg ".downloading new <".sname.">"
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000511 if has("win32") || has("win16") || has("win95")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100512" call Decho(".new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id='.latestsrcid)."|q")
513 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id='.latestsrcid)|q
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000514 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100515" call Decho(".exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id='))
516 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id=').latestsrcid
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000517 endif
518
Bram Moolenaar5c736222010-01-06 20:54:52 +0100519 " --------------------------------------------------------------------------
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000520 " AutoInstall: only if doautoinstall has been requested by the plugin itself
Bram Moolenaar5c736222010-01-06 20:54:52 +0100521 " --------------------------------------------------------------------------
Bram Moolenaar251e1912011-06-19 05:09:16 +0200522" call Decho("checking if plugin requested autoinstall: doautoinstall=".doautoinstall)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000523 if doautoinstall
Bram Moolenaar5c736222010-01-06 20:54:52 +0100524" call Decho(" ")
525" call Decho("Autoinstall: getcwd<".getcwd()."> filereadable(".sname.")=".filereadable(sname))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000526 if filereadable(sname)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100527" call Decho("<".sname."> is readable")
528" call Decho("exe silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".shellescape(s:autoinstall))
529 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".shellescape(s:autoinstall)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200530 let curdir = fnameescape(substitute(getcwd(),'\','/','ge'))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000531 let installdir= curdir."/Installed"
532 if !isdirectory(installdir)
533 call mkdir(installdir)
534 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100535" call Decho("curdir<".curdir."> installdir<".installdir.">")
536" call Decho("exe cd ".fnameescape(s:autoinstall))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000537 exe "cd ".fnameescape(s:autoinstall)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100538
539 " determine target directory for moves
540 let firstdir= substitute(&rtp,',.*$','','')
541 let pname = substitute(sname,'\..*','.vim','')
542" call Decho("determine tgtdir: is <".firstdir.'/AsNeeded/'.pname." readable?")
543 if filereadable(firstdir.'/AsNeeded/'.pname)
544 let tgtdir= "AsNeeded"
545 else
546 let tgtdir= "plugin"
547 endif
548" call Decho("tgtdir<".tgtdir."> pname<".pname.">")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000549
550 " decompress
551 if sname =~ '\.bz2$'
552" call Decho("decompress: attempt to bunzip2 ".sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100553 exe "sil !bunzip2 ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000554 let sname= substitute(sname,'\.bz2$','','')
555" call Decho("decompress: new sname<".sname."> after bunzip2")
556 elseif sname =~ '\.gz$'
557" call Decho("decompress: attempt to gunzip ".sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100558 exe "sil !gunzip ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000559 let sname= substitute(sname,'\.gz$','','')
560" call Decho("decompress: new sname<".sname."> after gunzip")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200561 elseif sname =~ '\.xz$'
562" call Decho("decompress: attempt to unxz ".sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100563 exe "sil !unxz ".shellescape(sname)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200564 let sname= substitute(sname,'\.xz$','','')
565" call Decho("decompress: new sname<".sname."> after unxz")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100566 else
567" call Decho("no decompression needed")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000568 endif
569
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100570 " distribute archive(.zip, .tar, .vba, ...) contents
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000571 if sname =~ '\.zip$'
572" call Decho("dearchive: attempt to unzip ".sname)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100573 exe "silent !unzip -o ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000574 elseif sname =~ '\.tar$'
575" call Decho("dearchive: attempt to untar ".sname)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100576 exe "silent !tar -xvf ".shellescape(sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100577 elseif sname =~ '\.tgz$'
578" call Decho("dearchive: attempt to untar+gunzip ".sname)
579 exe "silent !tar -zxvf ".shellescape(sname)
580 elseif sname =~ '\.taz$'
581" call Decho("dearchive: attempt to untar+uncompress ".sname)
582 exe "silent !tar -Zxvf ".shellescape(sname)
583 elseif sname =~ '\.tbz$'
584" call Decho("dearchive: attempt to untar+bunzip2 ".sname)
585 exe "silent !tar -jxvf ".shellescape(sname)
586 elseif sname =~ '\.txz$'
587" call Decho("dearchive: attempt to untar+xz ".sname)
588 exe "silent !tar -Jxvf ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000589 elseif sname =~ '\.vba$'
590" call Decho("dearchive: attempt to handle a vimball: ".sname)
591 silent 1split
Bram Moolenaar5c736222010-01-06 20:54:52 +0100592 if exists("g:vimball_home")
593 let oldvimballhome= g:vimball_home
594 endif
595 let g:vimball_home= s:autoinstall
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000596 exe "silent e ".fnameescape(sname)
597 silent so %
598 silent q
Bram Moolenaar5c736222010-01-06 20:54:52 +0100599 if exists("oldvimballhome")
600 let g:vimball_home= oldvimballhome
601 else
602 unlet g:vimball_home
603 endif
604 else
605" call Decho("no dearchiving needed")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000606 endif
607
Bram Moolenaar5c736222010-01-06 20:54:52 +0100608 " ---------------------------------------------
609 " move plugin to plugin/ or AsNeeded/ directory
610 " ---------------------------------------------
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000611 if sname =~ '.vim$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100612" call Decho("dearchive: attempt to simply move ".sname." to ".tgtdir)
613 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".tgtdir
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000614 else
615" call Decho("dearchive: move <".sname."> to installdir<".installdir.">")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100616 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".installdir
617 endif
618 if tgtdir != "plugin"
619" call Decho("exe silent !".g:GetLatestVimScripts_mv." plugin/".shellescape(pname)." ".tgtdir)
620 exe "silent !".g:GetLatestVimScripts_mv." plugin/".shellescape(pname)." ".tgtdir
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000621 endif
622
623 " helptags step
624 let docdir= substitute(&rtp,',.*','','e')."/doc"
625" call Decho("helptags: docdir<".docdir.">")
626 exe "helptags ".fnameescape(docdir)
627 exe "cd ".fnameescape(curdir)
628 endif
629 if fname !~ ':AutoInstall:'
630 let modline=scriptid." ".latestsrcid." :AutoInstall: ".fname.cmmnt
631 else
632 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
633 endif
634 else
635 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
636 endif
637
638 " update the data in the <GetLatestVimScripts.dat> file
639 call setline(line("."),modline)
640" call Decho("update data in ".expand("%")."#".line(".").": modline<".modline.">")
641" else " Decho
642" call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."], no need to update")
643 endif
644
645 " restore options
646 let &t_ti = t_ti
647 let &t_te = t_te
648 let &rs = rs
649 let @a = rega
650" call Dredir("BUFFER TEST (GetOneScript)","ls!")
651
652" call Dret("GetOneScript")
653endfun
654
655" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +0000656" Restore Options: {{{1
Bram Moolenaar9964e462007-05-05 17:54:07 +0000657let &cpo= s:keepcpo
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000658unlet s:keepcpo
Bram Moolenaar9964e462007-05-05 17:54:07 +0000659
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000660" ---------------------------------------------------------------------
661" Modelines: {{{1
Bram Moolenaar9964e462007-05-05 17:54:07 +0000662" vim: ts=8 sts=2 fdm=marker nowrap