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