1 " Vim script to download a missing spell file
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
3 " Last Change: 2007 May 08
5 if !exists('g:spellfile_URL')
6 let g:spellfile_URL = 'ftp://ftp.vim.org/pub/vim/runtime/spell'
8 let s:spellfile_URL = '' " Start with nothing so that s:donedict is reset.
10 " This function is used for the spellfile plugin.
11 function! spellfile#LoadFile(lang)
12 " If the netrw plugin isn't loaded we silently skip everything.
15 echomsg 'spellfile#LoadFile(): Nread command is not available.'
20 " If the URL changes we try all files again.
21 if s:spellfile_URL != g:spellfile_URL
23 let s:spellfile_URL = g:spellfile_URL
26 " I will say this only once!
27 if has_key(s:donedict, a:lang . &enc)
29 echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
33 let s:donedict[a:lang . &enc] = 1
35 " Find spell directories we can write in.
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)
46 echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
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
55 if enc == 'iso-8859-15'
58 let fname = a:lang . '.' . enc . '.spl'
60 " Split the window, read the file into a new buffer.
61 " Remember the buffer number, we check it below.
63 let newbufnr = winbufnr(0)
65 echo 'Downloading ' . fname . '...'
66 call spellfile#Nread(fname)
67 if getline(2) !~ 'VIMspell'
68 " Didn't work, perhaps there is an ASCII one.
69 " Careful: Nread() may have opened a new window for the error message,
70 " we need to go back to our own buffer and window.
71 if newbufnr != winbufnr(0)
72 let winnr = bufwinnr(newbufnr)
74 " Our buffer has vanished!? Open a new window.
75 echomsg "download buffer disappeared, opening a new one"
79 exe winnr . "wincmd w"
82 if newbufnr == winbufnr(0)
83 " We are back the old buffer, remove any (half-finished) download.
86 let newbufnr = winbufnr(0)
89 let fname = a:lang . '.ascii.spl'
90 echo 'Could not find it, trying ' . fname . '...'
91 call spellfile#Nread(fname)
92 if getline(2) !~ 'VIMspell'
93 echo 'Sorry, downloading failed'
94 exe newbufnr . "bwipe!"
99 " Delete the empty first line and mark the file unmodified.
103 let msg = "In which directory do you want to write the file:"
104 for i in range(len(dirlist))
105 let msg .= "\n" . (i + 1) . '. ' . dirlist[i]
107 let dirchoice = confirm(msg, dirchoices) - 2
109 exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname
111 " Also download the .sug file, if the user wants to.
112 let msg = "Do you want me to try getting the .sug file?\n"
113 let msg .= "This will improve making suggestions for spelling mistakes,\n"
114 let msg .= "but it uses quite a bit of memory."
115 if confirm(msg, "&No\n&Yes") == 2
117 let fname = substitute(fname, '\.spl$', '.sug', '')
118 echo 'Downloading ' . fname . '...'
119 call spellfile#Nread(fname)
120 if getline(2) =~ 'VIMsug'
122 exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname
125 echo 'Sorry, downloading failed'
126 " Go back to our own buffer/window, Nread() may have taken us to
128 if newbufnr != winbufnr(0)
129 let winnr = bufwinnr(newbufnr)
131 exe winnr . "wincmd w"
134 if newbufnr == winbufnr(0)
141 " Wipe out the buffer we used.
142 exe newbufnr . "bwipe"
146 " Read "fname" from the server.
147 function! spellfile#Nread(fname)
148 " We do our own error handling, don't want a window for it.
149 if exists("g:netrw_use_errorwindow")
150 let save_ew = g:netrw_use_errorwindow
152 let g:netrw_use_errorwindow=0
154 if g:spellfile_URL =~ '^ftp://'
155 " for an ftp server use a default login and password to avoid a prompt
156 let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
157 let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
158 exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
160 exe 'Nread ' g:spellfile_URL . '/' . a:fname
164 let g:netrw_use_errorwindow = save_ew
166 unlet g:netrw_use_errorwindow