blob: 886fd539391a498798095af74e358ab818b09059 [file] [log] [blame]
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00001" Vim script to download a missing spell file
2" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar6ee8d892012-01-10 14:55:01 +01003" Last Change: 2012 Jan 08
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00004
5if !exists('g:spellfile_URL')
Bram Moolenaar4a630f62008-06-27 18:58:11 +00006 " 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 Moolenaar1ef15e32006-02-01 21:56:25 +000013endif
14let s:spellfile_URL = '' " Start with nothing so that s:donedict is reset.
15
16" This function is used for the spellfile plugin.
17function! 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 Moolenaara9604e62018-07-21 05:56:22 +020025 let lang = tolower(a:lang)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000026
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 Moolenaara9604e62018-07-21 05:56:22 +020034 if has_key(s:donedict, lang . &enc)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000035 if &verbose
36 echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
37 endif
38 return
39 endif
Bram Moolenaara9604e62018-07-21 05:56:22 +020040 let s:donedict[lang . &enc] = 1
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000041
42 " Find spell directories we can write in.
Bram Moolenaar5c736222010-01-06 20:54:52 +010043 let [dirlist, dirchoices] = spellfile#GetDirChoices()
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000044 if len(dirlist) == 0
Bram Moolenaar5c736222010-01-06 20:54:52 +010045 let dir_to_create = spellfile#WritableSpellDir()
46 if &verbose || dir_to_create != ''
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000047 echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
48 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010049 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 Moolenaar1ef15e32006-02-01 21:56:25 +000059 endif
60
Bram Moolenaara9604e62018-07-21 05:56:22 +020061 let msg = 'Cannot find spell file for "' . lang . '" in ' . &enc
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000062 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 Moolenaara9604e62018-07-21 05:56:22 +020068 let fname = lang . '.' . enc . '.spl'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000069
70 " Split the window, read the file into a new buffer.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000071 " Remember the buffer number, we check it below.
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000072 new
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000073 let newbufnr = winbufnr(0)
Bram Moolenaar6ee8d892012-01-10 14:55:01 +010074 setlocal bin fenc=
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000075 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +000076 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000077 if getline(2) !~ 'VIMspell'
78 " Didn't work, perhaps there is an ASCII one.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000079 " 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 Moolenaar6ee8d892012-01-10 14:55:01 +010087 setlocal bin fenc=
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000088 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 Moolenaara9604e62018-07-21 05:56:22 +020099 let fname = lang . '.ascii.spl'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000100 echo 'Could not find it, trying ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000101 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000102 if getline(2) !~ 'VIMspell'
103 echo 'Sorry, downloading failed'
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000104 exe newbufnr . "bwipe!"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000105 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 Moolenaar8c8de832008-06-24 22:58:06 +0000119 if exists('*fnameescape')
120 let dirname = fnameescape(dirlist[dirchoice])
121 else
122 let dirname = escape(dirlist[dirchoice], ' ')
123 endif
Bram Moolenaar6ee8d892012-01-10 14:55:01 +0100124 setlocal fenc=
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000125 exe "write " . dirname . '/' . fname
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000126
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 Moolenaarc9a99912006-05-13 12:25:55 +0000135 call spellfile#Nread(fname)
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000136 if getline(2) =~ 'VIMsug'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000137 1d
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000138 exe "write " . dirname . '/' . fname
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000139 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 Moolenaar1ef15e32006-02-01 21:56:25 +0000153 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000154 endif
155 endif
156
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000157 " Wipe out the buffer we used.
158 exe newbufnr . "bwipe"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000159 endif
160endfunc
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000161
Bram Moolenaar9526a542006-08-29 20:31:34 +0000162" Read "fname" from the server.
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000163function! spellfile#Nread(fname)
Bram Moolenaar49325942007-05-10 19:19:59 +0000164 " 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 Moolenaar9526a542006-08-29 20:31:34 +0000170 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 Moolenaar49325942007-05-10 19:19:59 +0000178
179 if exists("save_ew")
180 let g:netrw_use_errorwindow = save_ew
181 else
182 unlet g:netrw_use_errorwindow
183 endif
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000184endfunc
Bram Moolenaar5c736222010-01-06 20:54:52 +0100185
186" Get a list of writable spell directories and choices for confirm().
187function! 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]
197endfunc
198
199function! 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 ''
210endfunction