blob: 6b74d37da9eadb0e2ea20b73c918806d6ee7585c [file] [log] [blame]
Bram Moolenaar9964e462007-05-05 17:54:07 +00001" ---------------------------------------------------------------------
2" getscript.vim
Bram Moolenaarff034192013-04-24 18:51:19 +02003" Author: Charles E. Campbell
4" Date: Apr 17, 2013
5" Version: 35
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 Moolenaarff034192013-04-24 18:51:19 +020018let g:loaded_getscript= "v35"
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
Bram Moolenaarff034192013-04-24 18:51:19 +020077" set up default scriptaddr address
78if !exists("g:GetLatestVimScripts_scriptaddr")
79 let g:GetLatestVimScripts_scriptaddr = 'http://vim.sourceforge.net/script.php?script_id='
80endif
81
Bram Moolenaar9964e462007-05-05 17:54:07 +000082"" For debugging:
83"let g:GetLatestVimScripts_wget = "echo"
84"let g:GetLatestVimScripts_options = "options"
85
86" ---------------------------------------------------------------------
87" Check If AutoInstall Capable: {{{1
88let s:autoinstall= ""
89if g:GetLatestVimScripts_allowautoinstall
90
91 if (has("win32") || has("gui_win32") || has("gui_win32s") || has("win16") || has("win64") || has("win32unix") || has("win95")) && &shell != "bash"
92 " windows (but not cygwin/bash)
93 let s:dotvim= "vimfiles"
94 if !exists("g:GetLatestVimScripts_mv")
95 let g:GetLatestVimScripts_mv= "ren"
96 endif
97
98 else
99 " unix
100 let s:dotvim= ".vim"
101 if !exists("g:GetLatestVimScripts_mv")
102 let g:GetLatestVimScripts_mv= "mv"
103 endif
104 endif
105
Bram Moolenaar5c736222010-01-06 20:54:52 +0100106 if exists("g:GetLatestVimScripts_autoinstalldir") && isdirectory(g:GetLatestVimScripts_autoinstalldir)
107 let s:autoinstall= g:GetLatestVimScripts_autoinstalldir"
108 elseif exists('$HOME') && isdirectory(expand("$HOME")."/".s:dotvim)
Bram Moolenaar9964e462007-05-05 17:54:07 +0000109 let s:autoinstall= $HOME."/".s:dotvim
110 endif
111" call Decho("s:autoinstall<".s:autoinstall.">")
112"else "Decho
113" call Decho("g:GetLatestVimScripts_allowautoinstall=".g:GetLatestVimScripts_allowautoinstall.": :AutoInstall: disabled")
114endif
115
116" ---------------------------------------------------------------------
117" Public Interface: {{{1
118com! -nargs=0 GetLatestVimScripts call getscript#GetLatestVimScripts()
119com! -nargs=0 GetScript call getscript#GetLatestVimScripts()
120silent! com -nargs=0 GLVS call getscript#GetLatestVimScripts()
121
122" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +0000123" GetLatestVimScripts: this function gets the latest versions of {{{1
124" scripts based on the list in
125" (first dir in runtimepath)/GetLatest/GetLatestVimScripts.dat
126fun! getscript#GetLatestVimScripts()
127" call Dfunc("GetLatestVimScripts() autoinstall<".s:autoinstall.">")
128
129" insure that wget is executable
130 if executable(g:GetLatestVimScripts_wget) != 1
131 echoerr "GetLatestVimScripts needs ".g:GetLatestVimScripts_wget." which apparently is not available on your system"
132" call Dret("GetLatestVimScripts : wget not executable/availble")
133 return
134 endif
135
Bram Moolenaarc236c162008-07-13 17:41:49 +0000136 " insure that fnameescape() is available
137 if !exists("*fnameescape")
138 echoerr "GetLatestVimScripts needs fnameescape() (provided by 7.1.299 or later)"
139 return
140 endif
141
Bram Moolenaar9964e462007-05-05 17:54:07 +0000142 " Find the .../GetLatest subdirectory under the runtimepath
143 for datadir in split(&rtp,',') + ['']
144 if isdirectory(datadir."/GetLatest")
145" call Decho("found directory<".datadir.">")
146 let datadir= datadir . "/GetLatest"
147 break
148 endif
149 if filereadable(datadir."GetLatestVimScripts.dat")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000150" call Decho("found ".datadir."/GetLatestVimScripts.dat")
151 break
Bram Moolenaar9964e462007-05-05 17:54:07 +0000152 endif
153 endfor
Bram Moolenaar82038d72007-05-10 17:15:45 +0000154
Bram Moolenaar9964e462007-05-05 17:54:07 +0000155 " Sanity checks: readability and writability
156 if datadir == ""
157 echoerr 'Missing "GetLatest/" on your runtimepath - see :help glvs-dist-install'
158" call Dret("GetLatestVimScripts : unable to find a GetLatest subdirectory")
159 return
160 endif
Bram Moolenaar9964e462007-05-05 17:54:07 +0000161 if filewritable(datadir) != 2
162 echoerr "(getLatestVimScripts) Your ".datadir." isn't writable"
163" call Dret("GetLatestVimScripts : non-writable directory<".datadir.">")
164 return
165 endif
166 let datafile= datadir."/GetLatestVimScripts.dat"
167 if !filereadable(datafile)
168 echoerr "Your data file<".datafile."> isn't readable"
169" call Dret("GetLatestVimScripts : non-readable datafile<".datafile.">")
170 return
171 endif
172 if !filewritable(datafile)
173 echoerr "Your data file<".datafile."> isn't writable"
174" call Dret("GetLatestVimScripts : non-writable datafile<".datafile.">")
175 return
176 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100177 " --------------------
178 " Passed sanity checks
179 " --------------------
180
Bram Moolenaar9964e462007-05-05 17:54:07 +0000181" call Decho("datadir <".datadir.">")
182" call Decho("datafile <".datafile.">")
183
Bram Moolenaar5c736222010-01-06 20:54:52 +0100184 " don't let any event handlers interfere (like winmanager's, taglist's, etc)
185 let eikeep = &ei
186 let hlskeep = &hls
187 let acdkeep = &acd
188 set ei=all hls&vim noacd
Bram Moolenaar9964e462007-05-05 17:54:07 +0000189
Bram Moolenaar5c736222010-01-06 20:54:52 +0100190 " Edit the datafile (ie. GetLatestVimScripts.dat):
191 " 1. record current directory (origdir),
192 " 2. change directory to datadir,
193 " 3. split window
194 " 4. edit datafile
Bram Moolenaar9964e462007-05-05 17:54:07 +0000195 let origdir= getcwd()
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000196" call Decho("exe cd ".fnameescape(substitute(datadir,'\','/','ge')))
197 exe "cd ".fnameescape(substitute(datadir,'\','/','ge'))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000198 split
Bram Moolenaar5c736222010-01-06 20:54:52 +0100199" call Decho("exe e ".fnameescape(substitute(datafile,'\','/','ge')))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000200 exe "e ".fnameescape(substitute(datafile,'\','/','ge'))
Bram Moolenaar9964e462007-05-05 17:54:07 +0000201 res 1000
202 let s:downloads = 0
203 let s:downerrors= 0
204
205 " Check on dependencies mentioned in plugins
206" call Decho(" ")
207" call Decho("searching plugins for GetLatestVimScripts dependencies")
208 let lastline = line("$")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000209" call Decho("lastline#".lastline)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100210 let firstdir = substitute(&rtp,',.*$','','')
211 let plugins = split(globpath(firstdir,"plugin/*.vim"),'\n')
212 let plugins = plugins + split(globpath(firstdir,"AsNeeded/*.vim"),'\n')
Bram Moolenaar9964e462007-05-05 17:54:07 +0000213 let foundscript = 0
214
Bram Moolenaar5c736222010-01-06 20:54:52 +0100215 " this loop updates the GetLatestVimScripts.dat file
216 " with dependencies explicitly mentioned in the plugins
217 " via GetLatestVimScripts: ... lines
218 " It reads the plugin script at the end of the GetLatestVimScripts.dat
219 " file, examines it, and then removes it.
Bram Moolenaar82038d72007-05-10 17:15:45 +0000220 for plugin in plugins
Bram Moolenaar5c736222010-01-06 20:54:52 +0100221" call Decho(" ")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000222" call Decho("plugin<".plugin.">")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000223
Bram Moolenaar82038d72007-05-10 17:15:45 +0000224 " read plugin in
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000225 " evidently a :r creates a new buffer (the "#" buffer) that is subsequently unused -- bwiping it
Bram Moolenaar9964e462007-05-05 17:54:07 +0000226 $
227" call Decho(".dependency checking<".plugin."> line$=".line("$"))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100228" call Decho("..exe silent r ".fnameescape(plugin))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000229 exe "silent r ".fnameescape(plugin)
230 exe "silent bwipe ".bufnr("#")
Bram Moolenaar82038d72007-05-10 17:15:45 +0000231
Bram Moolenaar9964e462007-05-05 17:54:07 +0000232 while search('^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+','W') != 0
Bram Moolenaar5c736222010-01-06 20:54:52 +0100233 let depscript = substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\d\+\s\+\d\+\s\+\(.*\)$','\1','e')
234 let depscriptid = substitute(getline("."),'^"\s\+GetLatestVimScripts:\s\+\(\d\+\)\s\+.*$','\1','')
235 let llp1 = lastline+1
236" call Decho("..depscript<".depscript.">")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000237
Bram Moolenaar5c736222010-01-06 20:54:52 +0100238 " found a "GetLatestVimScripts: # #" line in the script;
239 " check if its already in the datafile by searching backwards from llp1,
240 " the (prior to reading in the plugin script) last line plus one of the GetLatestVimScripts.dat file,
241 " for the script-id with no wrapping allowed.
242 let curline = line(".")
243 let noai_script = substitute(depscript,'\s*:AutoInstall:\s*','','e')
244 exe llp1
245 let srchline = search('^\s*'.depscriptid.'\s\+\d\+\s\+.*$','bW')
246 if srchline == 0
247 " this second search is taken when, for example, a 0 0 scriptname is to be skipped over
248 let srchline= search('\<'.noai_script.'\>','bW')
Bram Moolenaar82038d72007-05-10 17:15:45 +0000249 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100250" call Decho("..noai_script<".noai_script."> depscriptid#".depscriptid." srchline#".srchline." curline#".line(".")." lastline#".lastline)
251
252 if srchline == 0
253 " found a new script to permanently include in the datafile
254 let keep_rega = @a
255 let @a = substitute(getline(curline),'^"\s\+GetLatestVimScripts:\s\+','','')
256 echomsg "Appending <".@a."> to ".datafile." for ".depscript
257" call Decho("..Appending <".@a."> to ".datafile." for ".depscript)
258 exe lastline."put a"
259 let @a = keep_rega
260 let lastline = llp1
261 let curline = curline + 1
262 let foundscript = foundscript + 1
263" else " Decho
264" call Decho("..found <".noai_script."> (already in datafile at line#".srchline.")")
265 endif
266
267 let curline = curline + 1
268 exe curline
Bram Moolenaar9964e462007-05-05 17:54:07 +0000269 endwhile
Bram Moolenaar82038d72007-05-10 17:15:45 +0000270
Bram Moolenaar5c736222010-01-06 20:54:52 +0100271 " llp1: last line plus one
Bram Moolenaar9964e462007-05-05 17:54:07 +0000272 let llp1= lastline + 1
273" call Decho(".deleting lines: ".llp1.",$d")
274 exe "silent! ".llp1.",$d"
Bram Moolenaar82038d72007-05-10 17:15:45 +0000275 endfor
276" call Decho("--- end dependency checking loop --- foundscript=".foundscript)
277" call Decho(" ")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000278" call Dredir("BUFFER TEST (GetLatestVimScripts 1)","ls!")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000279
280 if foundscript == 0
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000281 setlocal nomod
Bram Moolenaar9964e462007-05-05 17:54:07 +0000282 endif
283
Bram Moolenaar5c736222010-01-06 20:54:52 +0100284 " --------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +0000285 " Check on out-of-date scripts using GetLatest/GetLatestVimScripts.dat
Bram Moolenaar5c736222010-01-06 20:54:52 +0100286 " --------------------------------------------------------------------
Bram Moolenaar82038d72007-05-10 17:15:45 +0000287" call Decho("begin: checking out-of-date scripts using datafile<".datafile.">")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000288 setlocal lz
Bram Moolenaar9964e462007-05-05 17:54:07 +0000289 1
Bram Moolenaar82038d72007-05-10 17:15:45 +0000290" /^-----/,$g/^\s*\d/call Decho(getline("."))
291 1
292 /^-----/,$g/^\s*\d/call s:GetOneScript()
293" call Decho("--- end out-of-date checking --- ")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000294
295 " Final report (an echomsg)
296 try
297 silent! ?^-------?
298 catch /^Vim\%((\a\+)\)\=:E114/
299" call Dret("GetLatestVimScripts : nothing done!")
300 return
301 endtry
302 exe "norm! kz\<CR>"
303 redraw!
304 let s:msg = ""
305 if s:downloads == 1
306 let s:msg = "Downloaded one updated script to <".datadir.">"
307 elseif s:downloads == 2
308 let s:msg= "Downloaded two updated scripts to <".datadir.">"
309 elseif s:downloads > 1
310 let s:msg= "Downloaded ".s:downloads." updated scripts to <".datadir.">"
311 else
312 let s:msg= "Everything was already current"
313 endif
314 if s:downerrors > 0
315 let s:msg= s:msg." (".s:downerrors." downloading errors)"
316 endif
317 echomsg s:msg
318 " save the file
319 if &mod
320 silent! w!
321 endif
Bram Moolenaarff034192013-04-24 18:51:19 +0200322 q!
Bram Moolenaar9964e462007-05-05 17:54:07 +0000323
324 " restore events and current directory
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000325 exe "cd ".fnameescape(substitute(origdir,'\','/','ge'))
Bram Moolenaar5c736222010-01-06 20:54:52 +0100326 let &ei = eikeep
327 let &hls = hlskeep
328 let &acd = acdkeep
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000329 setlocal nolz
330" call Dredir("BUFFER TEST (GetLatestVimScripts 2)","ls!")
Bram Moolenaar9964e462007-05-05 17:54:07 +0000331" call Dret("GetLatestVimScripts : did ".s:downloads." downloads")
332endfun
Bram Moolenaar9964e462007-05-05 17:54:07 +0000333
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000334" ---------------------------------------------------------------------
335" GetOneScript: (Get Latest Vim Script) this function operates {{{1
336" on the current line, interpreting two numbers and text as
337" ScriptID, SourceID, and Filename.
Bram Moolenaar5c736222010-01-06 20:54:52 +0100338" It downloads any scripts that have newer versions from vim.sourceforge.net.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000339fun! s:GetOneScript(...)
340" call Dfunc("GetOneScript()")
341
342 " set options to allow progress to be shown on screen
343 let rega= @a
344 let t_ti= &t_ti
345 let t_te= &t_te
346 let rs = &rs
347 set t_ti= t_te= nors
348
349 " put current line on top-of-screen and interpret it into
350 " a script identifer : used to obtain webpage
351 " source identifier : used to identify current version
352 " and an associated comment: used to report on what's being considered
353 if a:0 >= 3
354 let scriptid = a:1
355 let srcid = a:2
356 let fname = a:3
357 let cmmnt = ""
358" call Decho("scriptid<".scriptid.">")
359" call Decho("srcid <".srcid.">")
360" call Decho("fname <".fname.">")
361 else
362 let curline = getline(".")
363 if curline =~ '^\s*#'
364 let @a= rega
365" call Dret("GetOneScript : skipping a pure comment line")
366 return
367 endif
368 let parsepat = '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(.\{-}\)\(\s*#.*\)\=$'
369 try
370 let scriptid = substitute(curline,parsepat,'\1','e')
371 catch /^Vim\%((\a\+)\)\=:E486/
372 let scriptid= 0
373 endtry
374 try
375 let srcid = substitute(curline,parsepat,'\2','e')
376 catch /^Vim\%((\a\+)\)\=:E486/
377 let srcid= 0
378 endtry
379 try
380 let fname= substitute(curline,parsepat,'\3','e')
381 catch /^Vim\%((\a\+)\)\=:E486/
382 let fname= ""
383 endtry
384 try
385 let cmmnt= substitute(curline,parsepat,'\4','e')
386 catch /^Vim\%((\a\+)\)\=:E486/
387 let cmmnt= ""
388 endtry
389" call Decho("curline <".curline.">")
390" call Decho("parsepat<".parsepat.">")
391" call Decho("scriptid<".scriptid.">")
392" call Decho("srcid <".srcid.">")
393" call Decho("fname <".fname.">")
394 endif
395
Bram Moolenaar5c736222010-01-06 20:54:52 +0100396 " plugin author protection from downloading his/her own scripts atop their latest work
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000397 if scriptid == 0 || srcid == 0
398 " When looking for :AutoInstall: lines, skip scripts that have 0 0 scriptname
399 let @a= rega
400" call Dret("GetOneScript : skipping a scriptid==srcid==0 line")
401 return
402 endif
403
404 let doautoinstall= 0
405 if fname =~ ":AutoInstall:"
406" call Decho("case AutoInstall: fname<".fname.">")
407 let aicmmnt= substitute(fname,'\s\+:AutoInstall:\s\+',' ','')
408" call Decho("aicmmnt<".aicmmnt."> s:autoinstall=".s:autoinstall)
409 if s:autoinstall != ""
410 let doautoinstall = g:GetLatestVimScripts_allowautoinstall
411 endif
412 else
413 let aicmmnt= fname
414 endif
415" call Decho("aicmmnt<".aicmmnt.">: doautoinstall=".doautoinstall)
416
417 exe "norm z\<CR>"
418 redraw!
419" call Decho('considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid)
420 echo 'considering <'.aicmmnt.'> scriptid='.scriptid.' srcid='.srcid
421
Bram Moolenaar5c736222010-01-06 20:54:52 +0100422 " grab a copy of the plugin's vim.sourceforge.net webpage
Bram Moolenaarff034192013-04-24 18:51:19 +0200423 let scriptaddr = g:GetLatestVimScripts_scriptaddr.scriptid
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000424 let tmpfile = tempname()
425 let v:errmsg = ""
426
427 " make up to three tries at downloading the description
428 let itry= 1
429 while itry <= 3
Bram Moolenaar5c736222010-01-06 20:54:52 +0100430" call Decho(".try#".itry." to download description of <".aicmmnt."> with addr=".scriptaddr)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000431 if has("win32") || has("win16") || has("win95")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100432" call Decho(".new|exe silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile).' '.shellescape(scriptaddr)."|bw!")
433 new|exe "silent r!".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile).' '.shellescape(scriptaddr)|bw!
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000434 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100435" call Decho(".exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile)." ".shellescape(scriptaddr))
436 exe "silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(tmpfile)." ".shellescape(scriptaddr)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000437 endif
438 if itry == 1
439 exe "silent vsplit ".fnameescape(tmpfile)
440 else
441 silent! e %
442 endif
443 setlocal bh=wipe
444
445 " find the latest source-id in the plugin's webpage
446 silent! 1
447 let findpkg= search('Click on the package to download','W')
448 if findpkg > 0
449 break
450 endif
451 let itry= itry + 1
452 endwhile
453" call Decho(" --- end downloading tries while loop --- itry=".itry)
454
455 " testing: did finding "Click on the package..." fail?
456 if findpkg == 0 || itry >= 4
457 silent q!
458 call delete(tmpfile)
459 " restore options
460 let &t_ti = t_ti
461 let &t_te = t_te
462 let &rs = rs
463 let s:downerrors = s:downerrors + 1
464" call Decho("***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">")
465 echomsg "***warning*** couldn'".'t find "Click on the package..." in description page for <'.aicmmnt.">"
466" call Dret("GetOneScript : srch for /Click on the package/ failed")
467 let @a= rega
468 return
469 endif
470" call Decho('found "Click on the package to download"')
471
472 let findsrcid= search('src_id=','W')
473 if findsrcid == 0
474 silent q!
475 call delete(tmpfile)
476 " restore options
477 let &t_ti = t_ti
478 let &t_te = t_te
479 let &rs = rs
480 let s:downerrors = s:downerrors + 1
481" call Decho("***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">")
482 echomsg "***warning*** couldn'".'t find "src_id=" in description page for <'.aicmmnt.">"
483 let @a= rega
484" call Dret("GetOneScript : srch for /src_id/ failed")
485 return
486 endif
487" call Decho('found "src_id=" in description page')
488
489 let srcidpat = '^\s*<td class.*src_id=\(\d\+\)">\([^<]\+\)<.*$'
490 let latestsrcid= substitute(getline("."),srcidpat,'\1','')
491 let sname = substitute(getline("."),srcidpat,'\2','') " script name actually downloaded
492" call Decho("srcidpat<".srcidpat."> latestsrcid<".latestsrcid."> sname<".sname.">")
493 silent q!
494 call delete(tmpfile)
495
496 " convert the strings-of-numbers into numbers
497 let srcid = srcid + 0
498 let latestsrcid = latestsrcid + 0
499" call Decho("srcid=".srcid." latestsrcid=".latestsrcid." sname<".sname.">")
500
Bram Moolenaar5c736222010-01-06 20:54:52 +0100501 " has the plugin's most-recent srcid increased, which indicates that it has been updated
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000502 if latestsrcid > srcid
503" call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."]: need to update <".sname.">")
504
505 let s:downloads= s:downloads + 1
506 if sname == bufname("%")
507 " GetLatestVimScript has to be careful about downloading itself
508 let sname= "NEW_".sname
509 endif
510
Bram Moolenaar5c736222010-01-06 20:54:52 +0100511 " -----------------------------------------------------------------------------
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000512 " the plugin has been updated since we last obtained it, so download a new copy
Bram Moolenaar5c736222010-01-06 20:54:52 +0100513 " -----------------------------------------------------------------------------
514" call Decho(".downloading new <".sname.">")
515 echomsg ".downloading new <".sname.">"
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000516 if has("win32") || has("win16") || has("win95")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100517" 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")
518 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 +0000519 else
Bram Moolenaar5c736222010-01-06 20:54:52 +0100520" call Decho(".exe silent !".g:GetLatestVimScripts_wget." ".g:GetLatestVimScripts_options." ".shellescape(sname)." ".shellescape('http://vim.sourceforge.net/scripts/download_script.php?src_id='))
521 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 +0000522 endif
523
Bram Moolenaar5c736222010-01-06 20:54:52 +0100524 " --------------------------------------------------------------------------
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000525 " AutoInstall: only if doautoinstall has been requested by the plugin itself
Bram Moolenaar5c736222010-01-06 20:54:52 +0100526 " --------------------------------------------------------------------------
Bram Moolenaar251e1912011-06-19 05:09:16 +0200527" call Decho("checking if plugin requested autoinstall: doautoinstall=".doautoinstall)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000528 if doautoinstall
Bram Moolenaar5c736222010-01-06 20:54:52 +0100529" call Decho(" ")
530" call Decho("Autoinstall: getcwd<".getcwd()."> filereadable(".sname.")=".filereadable(sname))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000531 if filereadable(sname)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100532" call Decho("<".sname."> is readable")
533" call Decho("exe silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".shellescape(s:autoinstall))
534 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".shellescape(s:autoinstall)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200535 let curdir = fnameescape(substitute(getcwd(),'\','/','ge'))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000536 let installdir= curdir."/Installed"
537 if !isdirectory(installdir)
538 call mkdir(installdir)
539 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +0100540" call Decho("curdir<".curdir."> installdir<".installdir.">")
541" call Decho("exe cd ".fnameescape(s:autoinstall))
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000542 exe "cd ".fnameescape(s:autoinstall)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100543
544 " determine target directory for moves
545 let firstdir= substitute(&rtp,',.*$','','')
546 let pname = substitute(sname,'\..*','.vim','')
547" call Decho("determine tgtdir: is <".firstdir.'/AsNeeded/'.pname." readable?")
548 if filereadable(firstdir.'/AsNeeded/'.pname)
549 let tgtdir= "AsNeeded"
550 else
551 let tgtdir= "plugin"
552 endif
553" call Decho("tgtdir<".tgtdir."> pname<".pname.">")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000554
555 " decompress
556 if sname =~ '\.bz2$'
557" call Decho("decompress: attempt to bunzip2 ".sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100558 exe "sil !bunzip2 ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000559 let sname= substitute(sname,'\.bz2$','','')
560" call Decho("decompress: new sname<".sname."> after bunzip2")
561 elseif sname =~ '\.gz$'
562" call Decho("decompress: attempt to gunzip ".sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100563 exe "sil !gunzip ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000564 let sname= substitute(sname,'\.gz$','','')
565" call Decho("decompress: new sname<".sname."> after gunzip")
Bram Moolenaar251e1912011-06-19 05:09:16 +0200566 elseif sname =~ '\.xz$'
567" call Decho("decompress: attempt to unxz ".sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100568 exe "sil !unxz ".shellescape(sname)
Bram Moolenaar251e1912011-06-19 05:09:16 +0200569 let sname= substitute(sname,'\.xz$','','')
570" call Decho("decompress: new sname<".sname."> after unxz")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100571 else
572" call Decho("no decompression needed")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000573 endif
574
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100575 " distribute archive(.zip, .tar, .vba, ...) contents
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000576 if sname =~ '\.zip$'
577" call Decho("dearchive: attempt to unzip ".sname)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100578 exe "silent !unzip -o ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000579 elseif sname =~ '\.tar$'
580" call Decho("dearchive: attempt to untar ".sname)
Bram Moolenaar5c736222010-01-06 20:54:52 +0100581 exe "silent !tar -xvf ".shellescape(sname)
Bram Moolenaar6be7f872012-01-20 21:08:56 +0100582 elseif sname =~ '\.tgz$'
583" call Decho("dearchive: attempt to untar+gunzip ".sname)
584 exe "silent !tar -zxvf ".shellescape(sname)
585 elseif sname =~ '\.taz$'
586" call Decho("dearchive: attempt to untar+uncompress ".sname)
587 exe "silent !tar -Zxvf ".shellescape(sname)
588 elseif sname =~ '\.tbz$'
589" call Decho("dearchive: attempt to untar+bunzip2 ".sname)
590 exe "silent !tar -jxvf ".shellescape(sname)
591 elseif sname =~ '\.txz$'
592" call Decho("dearchive: attempt to untar+xz ".sname)
593 exe "silent !tar -Jxvf ".shellescape(sname)
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000594 elseif sname =~ '\.vba$'
595" call Decho("dearchive: attempt to handle a vimball: ".sname)
596 silent 1split
Bram Moolenaar5c736222010-01-06 20:54:52 +0100597 if exists("g:vimball_home")
598 let oldvimballhome= g:vimball_home
599 endif
600 let g:vimball_home= s:autoinstall
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000601 exe "silent e ".fnameescape(sname)
602 silent so %
603 silent q
Bram Moolenaar5c736222010-01-06 20:54:52 +0100604 if exists("oldvimballhome")
605 let g:vimball_home= oldvimballhome
606 else
607 unlet g:vimball_home
608 endif
609 else
610" call Decho("no dearchiving needed")
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000611 endif
612
Bram Moolenaar5c736222010-01-06 20:54:52 +0100613 " ---------------------------------------------
614 " move plugin to plugin/ or AsNeeded/ directory
615 " ---------------------------------------------
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000616 if sname =~ '.vim$'
Bram Moolenaar5c736222010-01-06 20:54:52 +0100617" call Decho("dearchive: attempt to simply move ".sname." to ".tgtdir)
618 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".tgtdir
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000619 else
620" call Decho("dearchive: move <".sname."> to installdir<".installdir.">")
Bram Moolenaar5c736222010-01-06 20:54:52 +0100621 exe "silent !".g:GetLatestVimScripts_mv." ".shellescape(sname)." ".installdir
622 endif
623 if tgtdir != "plugin"
624" call Decho("exe silent !".g:GetLatestVimScripts_mv." plugin/".shellescape(pname)." ".tgtdir)
625 exe "silent !".g:GetLatestVimScripts_mv." plugin/".shellescape(pname)." ".tgtdir
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000626 endif
627
628 " helptags step
629 let docdir= substitute(&rtp,',.*','','e')."/doc"
630" call Decho("helptags: docdir<".docdir.">")
631 exe "helptags ".fnameescape(docdir)
632 exe "cd ".fnameescape(curdir)
633 endif
634 if fname !~ ':AutoInstall:'
635 let modline=scriptid." ".latestsrcid." :AutoInstall: ".fname.cmmnt
636 else
637 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
638 endif
639 else
640 let modline=scriptid." ".latestsrcid." ".fname.cmmnt
641 endif
642
643 " update the data in the <GetLatestVimScripts.dat> file
644 call setline(line("."),modline)
645" call Decho("update data in ".expand("%")."#".line(".").": modline<".modline.">")
646" else " Decho
647" call Decho("[latestsrcid=".latestsrcid."] <= [srcid=".srcid."], no need to update")
648 endif
649
650 " restore options
651 let &t_ti = t_ti
652 let &t_te = t_te
653 let &rs = rs
654 let @a = rega
655" call Dredir("BUFFER TEST (GetOneScript)","ls!")
656
657" call Dret("GetOneScript")
658endfun
659
660" ---------------------------------------------------------------------
Bram Moolenaar9964e462007-05-05 17:54:07 +0000661" Restore Options: {{{1
Bram Moolenaar9964e462007-05-05 17:54:07 +0000662let &cpo= s:keepcpo
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000663unlet s:keepcpo
Bram Moolenaar9964e462007-05-05 17:54:07 +0000664
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000665" ---------------------------------------------------------------------
666" Modelines: {{{1
Bram Moolenaar9964e462007-05-05 17:54:07 +0000667" vim: ts=8 sts=2 fdm=marker nowrap