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 | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 3 | " Last Change: 2007 May 06 |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 4 | |
| 5 | if !exists('g:spellfile_URL') |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 6 | let g:spellfile_URL = 'ftp://ftp.vim.org/pub/vim/runtime/spell' |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 7 | endif |
| 8 | let s:spellfile_URL = '' " Start with nothing so that s:donedict is reset. |
| 9 | |
| 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. |
| 13 | if !exists(":Nread") |
| 14 | if &verbose |
| 15 | echomsg 'spellfile#LoadFile(): Nread command is not available.' |
| 16 | endif |
| 17 | return |
| 18 | endif |
| 19 | |
| 20 | " If the URL changes we try all files again. |
| 21 | if s:spellfile_URL != g:spellfile_URL |
| 22 | let s:donedict = {} |
| 23 | let s:spellfile_URL = g:spellfile_URL |
| 24 | endif |
| 25 | |
| 26 | " I will say this only once! |
| 27 | if has_key(s:donedict, a:lang . &enc) |
| 28 | if &verbose |
| 29 | echomsg 'spellfile#LoadFile(): Tried this language/encoding before.' |
| 30 | endif |
| 31 | return |
| 32 | endif |
| 33 | let s:donedict[a:lang . &enc] = 1 |
| 34 | |
| 35 | " Find spell directories we can write in. |
| 36 | let dirlist = [] |
| 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) |
| 42 | endif |
| 43 | endfor |
| 44 | if len(dirlist) == 0 |
| 45 | if &verbose |
| 46 | echomsg 'spellfile#LoadFile(): There is no writable spell directory.' |
| 47 | endif |
| 48 | return |
| 49 | endif |
| 50 | |
| 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 |
| 54 | let enc = &encoding |
| 55 | if enc == 'iso-8859-15' |
| 56 | let enc = 'latin1' |
| 57 | endif |
| 58 | let fname = a:lang . '.' . enc . '.spl' |
| 59 | |
| 60 | " Split the window, read the file into a new buffer. |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 61 | " Remember the buffer number, we check it below. |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 62 | new |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 63 | let newbufnr = winbufnr(0) |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 64 | setlocal bin |
| 65 | echo 'Downloading ' . fname . '...' |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 66 | call spellfile#Nread(fname) |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 67 | if getline(2) !~ 'VIMspell' |
| 68 | " Didn't work, perhaps there is an ASCII one. |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 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) |
| 73 | if winnr == -1 |
| 74 | " Our buffer has vanished!? Open a new window. |
| 75 | echomsg "download buffer disappeared, opening a new one" |
| 76 | new |
| 77 | setlocal bin |
| 78 | else |
| 79 | exe winnr . "wincmd w" |
| 80 | endif |
| 81 | endif |
| 82 | if newbufnr == winbufnr(0) |
| 83 | " We are back the old buffer, remove any (half-finished) download. |
| 84 | g/^/d |
| 85 | else |
| 86 | let newbufnr = winbufnr(0) |
| 87 | endif |
| 88 | |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 89 | let fname = a:lang . '.ascii.spl' |
| 90 | echo 'Could not find it, trying ' . fname . '...' |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 91 | call spellfile#Nread(fname) |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 92 | if getline(2) !~ 'VIMspell' |
| 93 | echo 'Sorry, downloading failed' |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 94 | exe newbufnr . "bwipe!" |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 95 | return |
| 96 | endif |
| 97 | endif |
| 98 | |
| 99 | " Delete the empty first line and mark the file unmodified. |
| 100 | 1d |
| 101 | set nomod |
| 102 | |
| 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] |
| 106 | endfor |
| 107 | let dirchoice = confirm(msg, dirchoices) - 2 |
| 108 | if dirchoice >= 0 |
| 109 | exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname |
| 110 | |
| 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 |
| 116 | g/^/d |
| 117 | let fname = substitute(fname, '\.spl$', '.sug', '') |
| 118 | echo 'Downloading ' . fname . '...' |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 119 | call spellfile#Nread(fname) |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 120 | if getline(2) =~ 'VIMsug' |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 121 | 1d |
| 122 | exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 123 | set nomod |
| 124 | else |
| 125 | echo 'Sorry, downloading failed' |
| 126 | " Go back to our own buffer/window, Nread() may have taken us to |
| 127 | " another window. |
| 128 | if newbufnr != winbufnr(0) |
| 129 | let winnr = bufwinnr(newbufnr) |
| 130 | if winnr != -1 |
| 131 | exe winnr . "wincmd w" |
| 132 | endif |
| 133 | endif |
| 134 | if newbufnr == winbufnr(0) |
| 135 | set nomod |
| 136 | endif |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 137 | endif |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 138 | endif |
| 139 | endif |
| 140 | |
Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 141 | " Wipe out the buffer we used. |
| 142 | exe newbufnr . "bwipe" |
Bram Moolenaar | 1ef15e3 | 2006-02-01 21:56:25 +0000 | [diff] [blame] | 143 | endif |
| 144 | endfunc |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 145 | |
Bram Moolenaar | 9526a54 | 2006-08-29 20:31:34 +0000 | [diff] [blame] | 146 | " Read "fname" from the server. |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 147 | function! spellfile#Nread(fname) |
Bram Moolenaar | 9526a54 | 2006-08-29 20:31:34 +0000 | [diff] [blame] | 148 | if g:spellfile_URL =~ '^ftp://' |
| 149 | " for an ftp server use a default login and password to avoid a prompt |
| 150 | let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '') |
| 151 | let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '') |
| 152 | exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"' |
| 153 | else |
| 154 | exe 'Nread ' g:spellfile_URL . '/' . a:fname |
| 155 | endif |
Bram Moolenaar | c9a9991 | 2006-05-13 12:25:55 +0000 | [diff] [blame] | 156 | endfunc |