651
|
1 " Vim script to download a missing spell file
|
|
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
|
3256
|
3 " Last Change: 2012 Jan 08
|
651
|
4
|
|
5 if !exists('g:spellfile_URL')
|
1652
|
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
|
651
|
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
|
14372
|
25 let lang = tolower(a:lang)
|
651
|
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!
|
14372
|
34 if has_key(s:donedict, lang . &enc)
|
651
|
35 if &verbose
|
|
36 echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
|
|
37 endif
|
|
38 return
|
|
39 endif
|
14372
|
40 let s:donedict[lang . &enc] = 1
|
651
|
41
|
|
42 " Find spell directories we can write in.
|
2034
|
43 let [dirlist, dirchoices] = spellfile#GetDirChoices()
|
651
|
44 if len(dirlist) == 0
|
2034
|
45 let dir_to_create = spellfile#WritableSpellDir()
|
|
46 if &verbose || dir_to_create != ''
|
651
|
47 echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
|
|
48 endif
|
2034
|
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
|
651
|
59 endif
|
|
60
|
14372
|
61 let msg = 'Cannot find spell file for "' . lang . '" in ' . &enc
|
651
|
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
|
14372
|
68 let fname = lang . '.' . enc . '.spl'
|
651
|
69
|
|
70 " Split the window, read the file into a new buffer.
|
1185
|
71 " Remember the buffer number, we check it below.
|
651
|
72 new
|
1185
|
73 let newbufnr = winbufnr(0)
|
3256
|
74 setlocal bin fenc=
|
651
|
75 echo 'Downloading ' . fname . '...'
|
884
|
76 call spellfile#Nread(fname)
|
651
|
77 if getline(2) !~ 'VIMspell'
|
|
78 " Didn't work, perhaps there is an ASCII one.
|
1185
|
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
|
3256
|
87 setlocal bin fenc=
|
1185
|
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
|
14372
|
99 let fname = lang . '.ascii.spl'
|
651
|
100 echo 'Could not find it, trying ' . fname . '...'
|
884
|
101 call spellfile#Nread(fname)
|
651
|
102 if getline(2) !~ 'VIMspell'
|
|
103 echo 'Sorry, downloading failed'
|
1185
|
104 exe newbufnr . "bwipe!"
|
651
|
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
|
1624
|
119 if exists('*fnameescape')
|
|
120 let dirname = fnameescape(dirlist[dirchoice])
|
|
121 else
|
|
122 let dirname = escape(dirlist[dirchoice], ' ')
|
|
123 endif
|
3256
|
124 setlocal fenc=
|
1624
|
125 exe "write " . dirname . '/' . fname
|
651
|
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 . '...'
|
884
|
135 call spellfile#Nread(fname)
|
1185
|
136 if getline(2) =~ 'VIMsug'
|
651
|
137 1d
|
1624
|
138 exe "write " . dirname . '/' . fname
|
1185
|
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
|
651
|
153 endif
|
|
154 endif
|
|
155 endif
|
|
156
|
1185
|
157 " Wipe out the buffer we used.
|
|
158 exe newbufnr . "bwipe"
|
651
|
159 endif
|
|
160 endfunc
|
884
|
161
|
950
|
162 " Read "fname" from the server.
|
884
|
163 function! spellfile#Nread(fname)
|
1226
|
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
|
950
|
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
|
1226
|
178
|
|
179 if exists("save_ew")
|
|
180 let g:netrw_use_errorwindow = save_ew
|
|
181 else
|
|
182 unlet g:netrw_use_errorwindow
|
|
183 endif
|
884
|
184 endfunc
|
2034
|
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
|