1" Vim script to download a missing spell file
2" Maintainer:	Bram Moolenaar <[email protected]>
3" Last Change:	2006 Feb 01
4
5if !exists('g:spellfile_URL')
6  let g:spellfile_URL = 'ftp://ftp.vim.org/pub/vim/unstable/runtime/spell'
7endif
8let s:spellfile_URL = ''    " Start with nothing so that s:donedict is reset.
9
10" This function is used for the spellfile plugin.
11function! spellfile#LoadFile(lang)
12  " If the netrw plugin isn't loaded we silently skip everything.
13  if !exists(":Nread")
14    if &verbose
15      echomsg 'spellfile#LoadFile(): Nread command is not available.'
16    endif
17    return
18  endif
19
20  " If the URL changes we try all files again.
21  if s:spellfile_URL != g:spellfile_URL
22    let s:donedict = {}
23    let s:spellfile_URL = g:spellfile_URL
24  endif
25
26  " I will say this only once!
27  if has_key(s:donedict, a:lang . &enc)
28    if &verbose
29      echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
30    endif
31    return
32  endif
33  let s:donedict[a:lang . &enc] = 1
34
35  " Find spell directories we can write in.
36  let dirlist = []
37  let dirchoices = '&Cancel'
38  for dir in split(globpath(&rtp, 'spell'), "\n")
39    if filewritable(dir) == 2
40      call add(dirlist, dir)
41      let dirchoices .= "\n&" . len(dirlist)
42    endif
43  endfor
44  if len(dirlist) == 0
45    if &verbose
46      echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
47    endif
48    return
49  endif
50
51  let msg = 'Cannot find spell file for "' . a:lang . '" in ' . &enc
52  let msg .= "\nDo you want me to try downloading it?"
53  if confirm(msg, "&Yes\n&No", 2) == 1
54    let enc = &encoding
55    if enc == 'iso-8859-15'
56      let enc = 'latin1'
57    endif
58    let fname = a:lang . '.' . enc . '.spl'
59
60    " Split the window, read the file into a new buffer.
61    new
62    setlocal bin
63    echo 'Downloading ' . fname . '...'
64    exe 'Nread ' g:spellfile_URL . '/' . fname
65    if getline(2) !~ 'VIMspell'
66      " Didn't work, perhaps there is an ASCII one.
67      g/^/d
68      let fname = a:lang . '.ascii.spl'
69      echo 'Could not find it, trying ' . fname . '...'
70      exe 'Nread ' g:spellfile_URL . '/' . fname
71      if getline(2) !~ 'VIMspell'
72	echo 'Sorry, downloading failed'
73	bwipe!
74	return
75      endif
76    endif
77
78    " Delete the empty first line and mark the file unmodified.
79    1d
80    set nomod
81
82    let msg = "In which directory do you want to write the file:"
83    for i in range(len(dirlist))
84      let msg .= "\n" . (i + 1) . '. ' . dirlist[i]
85    endfor
86    let dirchoice = confirm(msg, dirchoices) - 2
87    if dirchoice >= 0
88      exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname
89
90      " Also download the .sug file, if the user wants to.
91      let msg = "Do you want me to try getting the .sug file?\n"
92      let msg .= "This will improve making suggestions for spelling mistakes,\n"
93      let msg .= "but it uses quite a bit of memory."
94      if confirm(msg, "&No\n&Yes") == 2
95	g/^/d
96	let fname = substitute(fname, '\.spl$', '.sug', '')
97	echo 'Downloading ' . fname . '...'
98	exe 'Nread ' g:spellfile_URL . '/' . fname
99	if getline(2) !~ 'VIMsug'
100	  echo 'Sorry, downloading failed'
101	else
102	  1d
103	  exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname
104	endif
105	set nomod
106      endif
107    endif
108
109    bwipe
110  endif
111endfunc
112