| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  | " Vim script to download a missing spell file | 
					
						
							|  |  |  | " Maintainer:	Bram Moolenaar <Bram@vim.org> | 
					
						
							| 
									
										
										
										
											2012-01-10 14:55:01 +01:00
										 |  |  | " Last Change:	2012 Jan 08 | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if !exists('g:spellfile_URL') | 
					
						
							| 
									
										
										
										
											2008-06-27 18:58:11 +00:00
										 |  |  |   " Prefer using http:// when netrw should be able to use it, since | 
					
						
							|  |  |  |   " more firewalls let this through. | 
					
						
							|  |  |  |   if executable("curl") || executable("wget") || executable("fetch") | 
					
						
							|  |  |  |     let g:spellfile_URL = 'http://ftp.vim.org/pub/vim/runtime/spell' | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     let g:spellfile_URL = 'ftp://ftp.vim.org/pub/vim/runtime/spell' | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  | endif | 
					
						
							|  |  |  | let s:spellfile_URL = ''    " Start with nothing so that s:donedict is reset. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | " This function is used for the spellfile plugin. | 
					
						
							|  |  |  | function! spellfile#LoadFile(lang) | 
					
						
							|  |  |  |   " If the netrw plugin isn't loaded we silently skip everything. | 
					
						
							|  |  |  |   if !exists(":Nread") | 
					
						
							|  |  |  |     if &verbose | 
					
						
							|  |  |  |       echomsg 'spellfile#LoadFile(): Nread command is not available.' | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " If the URL changes we try all files again. | 
					
						
							|  |  |  |   if s:spellfile_URL != g:spellfile_URL | 
					
						
							|  |  |  |     let s:donedict = {} | 
					
						
							|  |  |  |     let s:spellfile_URL = g:spellfile_URL | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " I will say this only once! | 
					
						
							|  |  |  |   if has_key(s:donedict, a:lang . &enc) | 
					
						
							|  |  |  |     if &verbose | 
					
						
							|  |  |  |       echomsg 'spellfile#LoadFile(): Tried this language/encoding before.' | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  |   let s:donedict[a:lang . &enc] = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " Find spell directories we can write in. | 
					
						
							| 
									
										
										
										
											2010-01-06 20:54:52 +01:00
										 |  |  |   let [dirlist, dirchoices] = spellfile#GetDirChoices() | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |   if len(dirlist) == 0 | 
					
						
							| 
									
										
										
										
											2010-01-06 20:54:52 +01:00
										 |  |  |     let dir_to_create = spellfile#WritableSpellDir() | 
					
						
							|  |  |  |     if &verbose || dir_to_create != '' | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |       echomsg 'spellfile#LoadFile(): There is no writable spell directory.' | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2010-01-06 20:54:52 +01:00
										 |  |  |     if dir_to_create != '' | 
					
						
							|  |  |  |       if confirm("Shall I create " . dir_to_create, "&Yes\n&No", 2) == 1 | 
					
						
							|  |  |  | 	" After creating the directory it should show up in the list. | 
					
						
							|  |  |  | 	call mkdir(dir_to_create, "p") | 
					
						
							|  |  |  | 	let [dirlist, dirchoices] = spellfile#GetDirChoices() | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     if len(dirlist) == 0 | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let msg = 'Cannot find spell file for "' . a:lang . '" in ' . &enc | 
					
						
							|  |  |  |   let msg .= "\nDo you want me to try downloading it?" | 
					
						
							|  |  |  |   if confirm(msg, "&Yes\n&No", 2) == 1 | 
					
						
							|  |  |  |     let enc = &encoding | 
					
						
							|  |  |  |     if enc == 'iso-8859-15' | 
					
						
							|  |  |  |       let enc = 'latin1' | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     let fname = a:lang . '.' . enc . '.spl' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     " Split the window, read the file into a new buffer. | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  |     " Remember the buffer number, we check it below. | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |     new | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  |     let newbufnr = winbufnr(0) | 
					
						
							| 
									
										
										
										
											2012-01-10 14:55:01 +01:00
										 |  |  |     setlocal bin fenc= | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |     echo 'Downloading ' . fname . '...' | 
					
						
							| 
									
										
										
										
											2006-05-13 12:25:55 +00:00
										 |  |  |     call spellfile#Nread(fname) | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |     if getline(2) !~ 'VIMspell' | 
					
						
							|  |  |  |       " Didn't work, perhaps there is an ASCII one. | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  |       " Careful: Nread() may have opened a new window for the error message, | 
					
						
							|  |  |  |       " we need to go back to our own buffer and window. | 
					
						
							|  |  |  |       if newbufnr != winbufnr(0) | 
					
						
							|  |  |  | 	let winnr = bufwinnr(newbufnr) | 
					
						
							|  |  |  | 	if winnr == -1 | 
					
						
							|  |  |  | 	  " Our buffer has vanished!?  Open a new window. | 
					
						
							|  |  |  | 	  echomsg "download buffer disappeared, opening a new one" | 
					
						
							|  |  |  | 	  new | 
					
						
							| 
									
										
										
										
											2012-01-10 14:55:01 +01:00
										 |  |  | 	  setlocal bin fenc= | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 	  exe winnr . "wincmd w" | 
					
						
							|  |  |  | 	endif | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |       if newbufnr == winbufnr(0) | 
					
						
							|  |  |  | 	" We are back the old buffer, remove any (half-finished) download. | 
					
						
							|  |  |  |         g/^/d | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | 	let newbufnr = winbufnr(0) | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |       let fname = a:lang . '.ascii.spl' | 
					
						
							|  |  |  |       echo 'Could not find it, trying ' . fname . '...' | 
					
						
							| 
									
										
										
										
											2006-05-13 12:25:55 +00:00
										 |  |  |       call spellfile#Nread(fname) | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |       if getline(2) !~ 'VIMspell' | 
					
						
							|  |  |  | 	echo 'Sorry, downloading failed' | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  | 	exe newbufnr . "bwipe!" | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  | 	return | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     " Delete the empty first line and mark the file unmodified. | 
					
						
							|  |  |  |     1d | 
					
						
							|  |  |  |     set nomod | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let msg = "In which directory do you want to write the file:" | 
					
						
							|  |  |  |     for i in range(len(dirlist)) | 
					
						
							|  |  |  |       let msg .= "\n" . (i + 1) . '. ' . dirlist[i] | 
					
						
							|  |  |  |     endfor | 
					
						
							|  |  |  |     let dirchoice = confirm(msg, dirchoices) - 2 | 
					
						
							|  |  |  |     if dirchoice >= 0 | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |       if exists('*fnameescape') | 
					
						
							|  |  |  | 	let dirname = fnameescape(dirlist[dirchoice]) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | 	let dirname = escape(dirlist[dirchoice], ' ') | 
					
						
							|  |  |  |       endif | 
					
						
							| 
									
										
										
										
											2012-01-10 14:55:01 +01:00
										 |  |  |       setlocal fenc= | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  |       exe "write " . dirname . '/' . fname | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       " Also download the .sug file, if the user wants to. | 
					
						
							|  |  |  |       let msg = "Do you want me to try getting the .sug file?\n" | 
					
						
							|  |  |  |       let msg .= "This will improve making suggestions for spelling mistakes,\n" | 
					
						
							|  |  |  |       let msg .= "but it uses quite a bit of memory." | 
					
						
							|  |  |  |       if confirm(msg, "&No\n&Yes") == 2 | 
					
						
							|  |  |  | 	g/^/d | 
					
						
							|  |  |  | 	let fname = substitute(fname, '\.spl$', '.sug', '') | 
					
						
							|  |  |  | 	echo 'Downloading ' . fname . '...' | 
					
						
							| 
									
										
										
										
											2006-05-13 12:25:55 +00:00
										 |  |  | 	call spellfile#Nread(fname) | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  | 	if getline(2) =~ 'VIMsug' | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  | 	  1d | 
					
						
							| 
									
										
										
										
											2008-06-24 22:58:06 +00:00
										 |  |  | 	  exe "write " . dirname . '/' . fname | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  | 	  set nomod | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	  echo 'Sorry, downloading failed' | 
					
						
							|  |  |  | 	  " Go back to our own buffer/window, Nread() may have taken us to | 
					
						
							|  |  |  | 	  " another window. | 
					
						
							|  |  |  | 	  if newbufnr != winbufnr(0) | 
					
						
							|  |  |  | 	    let winnr = bufwinnr(newbufnr) | 
					
						
							|  |  |  | 	    if winnr != -1 | 
					
						
							|  |  |  | 	      exe winnr . "wincmd w" | 
					
						
							|  |  |  | 	    endif | 
					
						
							|  |  |  | 	  endif | 
					
						
							|  |  |  | 	  if newbufnr == winbufnr(0) | 
					
						
							|  |  |  | 	    set nomod | 
					
						
							|  |  |  | 	  endif | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  | 	endif | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-06 21:55:31 +00:00
										 |  |  |     " Wipe out the buffer we used. | 
					
						
							|  |  |  |     exe newbufnr . "bwipe" | 
					
						
							| 
									
										
										
										
											2006-02-01 21:56:25 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | endfunc | 
					
						
							| 
									
										
										
										
											2006-05-13 12:25:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-29 20:31:34 +00:00
										 |  |  | " Read "fname" from the server. | 
					
						
							| 
									
										
										
										
											2006-05-13 12:25:55 +00:00
										 |  |  | function! spellfile#Nread(fname) | 
					
						
							| 
									
										
										
										
											2007-05-10 19:19:59 +00:00
										 |  |  |   " We do our own error handling, don't want a window for it. | 
					
						
							|  |  |  |   if exists("g:netrw_use_errorwindow") | 
					
						
							|  |  |  |     let save_ew = g:netrw_use_errorwindow | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  |   let g:netrw_use_errorwindow=0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-29 20:31:34 +00:00
										 |  |  |   if g:spellfile_URL =~ '^ftp://' | 
					
						
							|  |  |  |     " for an ftp server use a default login and password to avoid a prompt | 
					
						
							|  |  |  |     let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '') | 
					
						
							|  |  |  |     let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '') | 
					
						
							|  |  |  |     exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"' | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     exe 'Nread ' g:spellfile_URL . '/' . a:fname | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2007-05-10 19:19:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if exists("save_ew") | 
					
						
							|  |  |  |     let g:netrw_use_errorwindow = save_ew | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     unlet g:netrw_use_errorwindow | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2006-05-13 12:25:55 +00:00
										 |  |  | endfunc | 
					
						
							| 
									
										
										
										
											2010-01-06 20:54:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | " Get a list of writable spell directories and choices for confirm(). | 
					
						
							|  |  |  | function! spellfile#GetDirChoices() | 
					
						
							|  |  |  |   let dirlist = [] | 
					
						
							|  |  |  |   let dirchoices = '&Cancel' | 
					
						
							|  |  |  |   for dir in split(globpath(&rtp, 'spell'), "\n") | 
					
						
							|  |  |  |     if filewritable(dir) == 2 | 
					
						
							|  |  |  |       call add(dirlist, dir) | 
					
						
							|  |  |  |       let dirchoices .= "\n&" . len(dirlist) | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |   endfor | 
					
						
							|  |  |  |   return [dirlist, dirchoices] | 
					
						
							|  |  |  | endfunc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function! spellfile#WritableSpellDir() | 
					
						
							|  |  |  |   if has("unix") | 
					
						
							|  |  |  |     " For Unix always use the $HOME/.vim directory | 
					
						
							|  |  |  |     return $HOME . "/.vim/spell" | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  |   for dir in split(&rtp, ',') | 
					
						
							|  |  |  |     if filewritable(dir) == 2 | 
					
						
							|  |  |  |       return dir . "/spell" | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |   endfor | 
					
						
							|  |  |  |   return '' | 
					
						
							|  |  |  | endfunction |