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 | 6ee8d89 | 2012-01-10 14:55:01 +0100 | [diff] [blame] | 3 | " Last Change: 2012 Jan 08 |
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 |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 25 | let lang = tolower(a:lang) |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 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! |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 34 | if has_key(s:donedict, lang . &enc) |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 35 | if &verbose |
| 36 | echomsg 'spellfile#LoadFile(): Tried this language/encoding before.' |
| 37 | endif |
| 38 | return |
| 39 | endif |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 40 | let s:donedict[lang . &enc] = 1 |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 41 | |
| 42 | " Find spell directories we can write in. |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 43 | let [dirlist, dirchoices] = spellfile#GetDirChoices() |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 44 | if len(dirlist) == 0 |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 45 | let dir_to_create = spellfile#WritableSpellDir() |
| 46 | if &verbose || dir_to_create != '' |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 47 | echomsg 'spellfile#LoadFile(): There is no writable spell directory.' |
| 48 | endif |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 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 |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 59 | endif |
| 60 | |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 61 | let msg = 'Cannot find spell file for "' . lang . '" in ' . &enc |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 68 | let fname = lang . '.' . enc . '.spl' |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 69 | |
| 70 | " Split the window, read the file into a new buffer. |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 71 | " Remember the buffer number, we check it below. |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 72 | new |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 73 | let newbufnr = winbufnr(0) |
Bram Moolenaar | 6ee8d89 | 2012-01-10 14:55:01 +0100 | [diff] [blame] | 74 | setlocal bin fenc= |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 75 | echo 'Downloading ' . fname . '...' |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 76 | call spellfile#Nread(fname) |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 77 | if getline(2) !~ 'VIMspell' |
| 78 | " Didn't work, perhaps there is an ASCII one. |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 6ee8d89 | 2012-01-10 14:55:01 +0100 | [diff] [blame] | 87 | setlocal bin fenc= |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | a9604e6 | 2018-07-21 05:56:22 +0200 | [diff] [blame] | 99 | let fname = lang . '.ascii.spl' |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 100 | echo 'Could not find it, trying ' . fname . '...' |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 101 | call spellfile#Nread(fname) |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 102 | if getline(2) !~ 'VIMspell' |
| 103 | echo 'Sorry, downloading failed' |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 104 | exe newbufnr . "bwipe!" |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 119 | if exists('*fnameescape') |
| 120 | let dirname = fnameescape(dirlist[dirchoice]) |
| 121 | else |
| 122 | let dirname = escape(dirlist[dirchoice], ' ') |
| 123 | endif |
Bram Moolenaar | 6ee8d89 | 2012-01-10 14:55:01 +0100 | [diff] [blame] | 124 | setlocal fenc= |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 125 | exe "write " . dirname . '/' . fname |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 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 . '...' |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 135 | call spellfile#Nread(fname) |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 136 | if getline(2) =~ 'VIMsug' |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 137 | 1d |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 138 | exe "write " . dirname . '/' . fname |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 153 | endif |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 154 | endif |
| 155 | endif |
| 156 | |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 157 | " Wipe out the buffer we used. |
| 158 | exe newbufnr . "bwipe" |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 159 | endif |
| 160 | endfunc |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 161 | |
Bram Moolenaar | 9526a54 | 2006-08-29 20:31:34 +0000 | [diff] [blame] | 162 | " Read "fname" from the server. |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 163 | function! spellfile#Nread(fname) |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | 9526a54 | 2006-08-29 20:31:34 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 178 | |
| 179 | if exists("save_ew") |
| 180 | let g:netrw_use_errorwindow = save_ew |
| 181 | else |
| 182 | unlet g:netrw_use_errorwindow |
| 183 | endif |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 184 | endfunc |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 185 | |
| 186 | " Get a list of writable spell directories and choices for confirm(). |
| 187 | function! 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] |
| 197 | endfunc |
| 198 | |
| 199 | function! 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 '' |
| 210 | endfunction |