1 " Vim script to download a missing spell file
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
3 " Last Change: 2008 Jun 27
5 if !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'
11 let g:spellfile_URL = 'ftp://ftp.vim.org/pub/vim/runtime/spell'
14 let s:spellfile_URL = '' " Start with nothing so that s:donedict is reset.
16 " This function is used for the spellfile plugin.
17 function! spellfile#LoadFile(lang)
18 " If the netrw plugin isn't loaded we silently skip everything.
21 echomsg 'spellfile#LoadFile(): Nread command is not available.'
26 " If the URL changes we try all files again.
27 if s:spellfile_URL != g:spellfile_URL
29 let s:spellfile_URL = g:spellfile_URL
32 " I will say this only once!
33 if has_key(s:donedict, a:lang . &enc)
35 echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
39 let s:donedict[a:lang . &enc] = 1
41 " Find spell directories we can write in.
43 let dirchoices = '&Cancel'
44 for dir in split(globpath(&rtp, 'spell'), "\n")
45 if filewritable(dir) == 2
46 call add(dirlist, dir)
47 let dirchoices .= "\n&" . len(dirlist)
52 echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
57 let msg = 'Cannot find spell file for "' . a:lang . '" in ' . &enc
58 let msg .= "\nDo you want me to try downloading it?"
59 if confirm(msg, "&Yes\n&No", 2) == 1
61 if enc == 'iso-8859-15'
64 let fname = a:lang . '.' . enc . '.spl'
66 " Split the window, read the file into a new buffer.
67 " Remember the buffer number, we check it below.
69 let newbufnr = winbufnr(0)
71 echo 'Downloading ' . fname . '...'
72 call spellfile#Nread(fname)
73 if getline(2) !~ 'VIMspell'
74 " Didn't work, perhaps there is an ASCII one.
75 " Careful: Nread() may have opened a new window for the error message,
76 " we need to go back to our own buffer and window.
77 if newbufnr != winbufnr(0)
78 let winnr = bufwinnr(newbufnr)
80 " Our buffer has vanished!? Open a new window.
81 echomsg "download buffer disappeared, opening a new one"
85 exe winnr . "wincmd w"
88 if newbufnr == winbufnr(0)
89 " We are back the old buffer, remove any (half-finished) download.
92 let newbufnr = winbufnr(0)
95 let fname = a:lang . '.ascii.spl'
96 echo 'Could not find it, trying ' . fname . '...'
97 call spellfile#Nread(fname)
98 if getline(2) !~ 'VIMspell'
99 echo 'Sorry, downloading failed'
100 exe newbufnr . "bwipe!"
105 " Delete the empty first line and mark the file unmodified.
109 let msg = "In which directory do you want to write the file:"
110 for i in range(len(dirlist))
111 let msg .= "\n" . (i + 1) . '. ' . dirlist[i]
113 let dirchoice = confirm(msg, dirchoices) - 2
115 if exists('*fnameescape')
116 let dirname = fnameescape(dirlist[dirchoice])
118 let dirname = escape(dirlist[dirchoice], ' ')
120 exe "write " . dirname . '/' . fname
122 " Also download the .sug file, if the user wants to.
123 let msg = "Do you want me to try getting the .sug file?\n"
124 let msg .= "This will improve making suggestions for spelling mistakes,\n"
125 let msg .= "but it uses quite a bit of memory."
126 if confirm(msg, "&No\n&Yes") == 2
128 let fname = substitute(fname, '\.spl$', '.sug', '')
129 echo 'Downloading ' . fname . '...'
130 call spellfile#Nread(fname)
131 if getline(2) =~ 'VIMsug'
133 exe "write " . dirname . '/' . fname
136 echo 'Sorry, downloading failed'
137 " Go back to our own buffer/window, Nread() may have taken us to
139 if newbufnr != winbufnr(0)
140 let winnr = bufwinnr(newbufnr)
142 exe winnr . "wincmd w"
145 if newbufnr == winbufnr(0)
152 " Wipe out the buffer we used.
153 exe newbufnr . "bwipe"
157 " Read "fname" from the server.
158 function! spellfile#Nread(fname)
159 " We do our own error handling, don't want a window for it.
160 if exists("g:netrw_use_errorwindow")
161 let save_ew = g:netrw_use_errorwindow
163 let g:netrw_use_errorwindow=0
165 if g:spellfile_URL =~ '^ftp://'
166 " for an ftp server use a default login and password to avoid a prompt
167 let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
168 let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
169 exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
171 exe 'Nread ' g:spellfile_URL . '/' . a:fname
175 let g:netrw_use_errorwindow = save_ew
177 unlet g:netrw_use_errorwindow