1" Vim script to download a missing spell file
2" Maintainer:	Bram Moolenaar <[email protected]>
3" Last Change:	2012 Jan 08
4
5if !exists('g:spellfile_URL')
6  " Prefer using http:// when netrw should be able to use it, since
7  " more firewalls let this through.
8  if executable("curl") || executable("wget") || executable("fetch")
9    let g:spellfile_URL = 'http://ftp.vim.org/pub/vim/runtime/spell'
10  else
11    let g:spellfile_URL = 'ftp://ftp.vim.org/pub/vim/runtime/spell'
12  endif
13endif
14let s:spellfile_URL = ''    " Start with nothing so that s:donedict is reset.
15
16" This function is used for the spellfile plugin.
17function! spellfile#LoadFile(lang)
18  " If the netrw plugin isn't loaded we silently skip everything.
19  if !exists(":Nread")
20    if &verbose
21      echomsg 'spellfile#LoadFile(): Nread command is not available.'
22    endif
23    return
24  endif
25  let lang = tolower(a:lang)
26
27  " If the URL changes we try all files again.
28  if s:spellfile_URL != g:spellfile_URL
29    let s:donedict = {}
30    let s:spellfile_URL = g:spellfile_URL
31  endif
32
33  " I will say this only once!
34  if has_key(s:donedict, lang . &enc)
35    if &verbose
36      echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
37    endif
38    return
39  endif
40  let s:donedict[lang . &enc] = 1
41
42  " Find spell directories we can write in.
43  let [dirlist, dirchoices] = spellfile#GetDirChoices()
44  if len(dirlist) == 0
45    let dir_to_create = spellfile#WritableSpellDir()
46    if &verbose || dir_to_create != ''
47      echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
48    endif
49    if dir_to_create != ''
50      if confirm("Shall I create " . dir_to_create, "&Yes\n&No", 2) == 1
51	" After creating the directory it should show up in the list.
52	call mkdir(dir_to_create, "p")
53	let [dirlist, dirchoices] = spellfile#GetDirChoices()
54      endif
55    endif
56    if len(dirlist) == 0
57      return
58    endif
59  endif
60
61  let msg = 'Cannot find spell file for "' . lang . '" in ' . &enc
62  let msg .= "\nDo you want me to try downloading it?"
63  if confirm(msg, "&Yes\n&No", 2) == 1
64    let enc = &encoding
65    if enc == 'iso-8859-15'
66      let enc = 'latin1'
67    endif
68    let fname = lang . '.' . enc . '.spl'
69
70    " Split the window, read the file into a new buffer.
71    " Remember the buffer number, we check it below.
72    new
73    let newbufnr = winbufnr(0)
74    setlocal bin fenc=
75    echo 'Downloading ' . fname . '...'
76    call spellfile#Nread(fname)
77    if getline(2) !~ 'VIMspell'
78      " Didn't work, perhaps there is an ASCII one.
79      " Careful: Nread() may have opened a new window for the error message,
80      " we need to go back to our own buffer and window.
81      if newbufnr != winbufnr(0)
82	let winnr = bufwinnr(newbufnr)
83	if winnr == -1
84	  " Our buffer has vanished!?  Open a new window.
85	  echomsg "download buffer disappeared, opening a new one"
86	  new
87	  setlocal bin fenc=
88	else
89	  exe winnr . "wincmd w"
90	endif
91      endif
92      if newbufnr == winbufnr(0)
93	" We are back the old buffer, remove any (half-finished) download.
94        g/^/d
95      else
96	let newbufnr = winbufnr(0)
97      endif
98
99      let fname = lang . '.ascii.spl'
100      echo 'Could not find it, trying ' . fname . '...'
101      call spellfile#Nread(fname)
102      if getline(2) !~ 'VIMspell'
103	echo 'Sorry, downloading failed'
104	exe newbufnr . "bwipe!"
105	return
106      endif
107    endif
108
109    " Delete the empty first line and mark the file unmodified.
110    1d
111    set nomod
112
113    let msg = "In which directory do you want to write the file:"
114    for i in range(len(dirlist))
115      let msg .= "\n" . (i + 1) . '. ' . dirlist[i]
116    endfor
117    let dirchoice = confirm(msg, dirchoices) - 2
118    if dirchoice >= 0
119      if exists('*fnameescape')
120	let dirname = fnameescape(dirlist[dirchoice])
121      else
122	let dirname = escape(dirlist[dirchoice], ' ')
123      endif
124      setlocal fenc=
125      exe "write " . dirname . '/' . fname
126
127      " Also download the .sug file, if the user wants to.
128      let msg = "Do you want me to try getting the .sug file?\n"
129      let msg .= "This will improve making suggestions for spelling mistakes,\n"
130      let msg .= "but it uses quite a bit of memory."
131      if confirm(msg, "&No\n&Yes") == 2
132	g/^/d
133	let fname = substitute(fname, '\.spl$', '.sug', '')
134	echo 'Downloading ' . fname . '...'
135	call spellfile#Nread(fname)
136	if getline(2) =~ 'VIMsug'
137	  1d
138	  exe "write " . dirname . '/' . fname
139	  set nomod
140	else
141	  echo 'Sorry, downloading failed'
142	  " Go back to our own buffer/window, Nread() may have taken us to
143	  " another window.
144	  if newbufnr != winbufnr(0)
145	    let winnr = bufwinnr(newbufnr)
146	    if winnr != -1
147	      exe winnr . "wincmd w"
148	    endif
149	  endif
150	  if newbufnr == winbufnr(0)
151	    set nomod
152	  endif
153	endif
154      endif
155    endif
156
157    " Wipe out the buffer we used.
158    exe newbufnr . "bwipe"
159  endif
160endfunc
161
162" Read "fname" from the server.
163function! spellfile#Nread(fname)
164  " We do our own error handling, don't want a window for it.
165  if exists("g:netrw_use_errorwindow")
166    let save_ew = g:netrw_use_errorwindow
167  endif
168  let g:netrw_use_errorwindow=0
169
170  if g:spellfile_URL =~ '^ftp://'
171    " for an ftp server use a default login and password to avoid a prompt
172    let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
173    let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
174    exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
175  else
176    exe 'Nread ' g:spellfile_URL . '/' . a:fname
177  endif
178
179  if exists("save_ew")
180    let g:netrw_use_errorwindow = save_ew
181  else
182    unlet g:netrw_use_errorwindow
183  endif
184endfunc
185
186" Get a list of writable spell directories and choices for confirm().
187function! spellfile#GetDirChoices()
188  let dirlist = []
189  let dirchoices = '&Cancel'
190  for dir in split(globpath(&rtp, 'spell'), "\n")
191    if filewritable(dir) == 2
192      call add(dirlist, dir)
193      let dirchoices .= "\n&" . len(dirlist)
194    endif
195  endfor
196  return [dirlist, dirchoices]
197endfunc
198
199function! spellfile#WritableSpellDir()
200  if has("unix")
201    " For Unix always use the $HOME/.vim directory
202    return $HOME . "/.vim/spell"
203  endif
204  for dir in split(&rtp, ',')
205    if filewritable(dir) == 2
206      return dir . "/spell"
207    endif
208  endfor
209  return ''
210endfunction
211