blob: 9979f1bd09d42d5eb1fae17512a7e21259bc21ea [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 Moolenaar4a630f62008-06-27 18:58:11 +00003" Last Change: 2008 Jun 27
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
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.
42 let dirlist = []
43 let dirchoices = '&Cancel'
44 for dir in split(globpath(&rtp, 'spell'), "\n")
45 if filewritable(dir) == 2
46 call add(dirlist, dir)
47 let dirchoices .= "\n&" . len(dirlist)
48 endif
49 endfor
50 if len(dirlist) == 0
51 if &verbose
52 echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
53 endif
54 return
55 endif
56
57 let msg = 'Cannot find spell file for "' . a:lang . '" in ' . &enc
58 let msg .= "\nDo you want me to try downloading it?"
59 if confirm(msg, "&Yes\n&No", 2) == 1
60 let enc = &encoding
61 if enc == 'iso-8859-15'
62 let enc = 'latin1'
63 endif
64 let fname = a:lang . '.' . enc . '.spl'
65
66 " Split the window, read the file into a new buffer.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000067 " Remember the buffer number, we check it below.
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000068 new
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000069 let newbufnr = winbufnr(0)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000070 setlocal bin
71 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +000072 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000073 if getline(2) !~ 'VIMspell'
74 " Didn't work, perhaps there is an ASCII one.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000075 " Careful: Nread() may have opened a new window for the error message,
76 " we need to go back to our own buffer and window.
77 if newbufnr != winbufnr(0)
78 let winnr = bufwinnr(newbufnr)
79 if winnr == -1
80 " Our buffer has vanished!? Open a new window.
81 echomsg "download buffer disappeared, opening a new one"
82 new
83 setlocal bin
84 else
85 exe winnr . "wincmd w"
86 endif
87 endif
88 if newbufnr == winbufnr(0)
89 " We are back the old buffer, remove any (half-finished) download.
90 g/^/d
91 else
92 let newbufnr = winbufnr(0)
93 endif
94
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000095 let fname = a:lang . '.ascii.spl'
96 echo 'Could not find it, trying ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +000097 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000098 if getline(2) !~ 'VIMspell'
99 echo 'Sorry, downloading failed'
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000100 exe newbufnr . "bwipe!"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000101 return
102 endif
103 endif
104
105 " Delete the empty first line and mark the file unmodified.
106 1d
107 set nomod
108
109 let msg = "In which directory do you want to write the file:"
110 for i in range(len(dirlist))
111 let msg .= "\n" . (i + 1) . '. ' . dirlist[i]
112 endfor
113 let dirchoice = confirm(msg, dirchoices) - 2
114 if dirchoice >= 0
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115 if exists('*fnameescape')
116 let dirname = fnameescape(dirlist[dirchoice])
117 else
118 let dirname = escape(dirlist[dirchoice], ' ')
119 endif
120 exe "write " . dirname . '/' . fname
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000121
122 " Also download the .sug file, if the user wants to.
123 let msg = "Do you want me to try getting the .sug file?\n"
124 let msg .= "This will improve making suggestions for spelling mistakes,\n"
125 let msg .= "but it uses quite a bit of memory."
126 if confirm(msg, "&No\n&Yes") == 2
127 g/^/d
128 let fname = substitute(fname, '\.spl$', '.sug', '')
129 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000130 call spellfile#Nread(fname)
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000131 if getline(2) =~ 'VIMsug'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000132 1d
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000133 exe "write " . dirname . '/' . fname
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000134 set nomod
135 else
136 echo 'Sorry, downloading failed'
137 " Go back to our own buffer/window, Nread() may have taken us to
138 " another window.
139 if newbufnr != winbufnr(0)
140 let winnr = bufwinnr(newbufnr)
141 if winnr != -1
142 exe winnr . "wincmd w"
143 endif
144 endif
145 if newbufnr == winbufnr(0)
146 set nomod
147 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000148 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000149 endif
150 endif
151
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000152 " Wipe out the buffer we used.
153 exe newbufnr . "bwipe"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000154 endif
155endfunc
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000156
Bram Moolenaar9526a542006-08-29 20:31:34 +0000157" Read "fname" from the server.
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000158function! spellfile#Nread(fname)
Bram Moolenaar49325942007-05-10 19:19:59 +0000159 " We do our own error handling, don't want a window for it.
160 if exists("g:netrw_use_errorwindow")
161 let save_ew = g:netrw_use_errorwindow
162 endif
163 let g:netrw_use_errorwindow=0
164
Bram Moolenaar9526a542006-08-29 20:31:34 +0000165 if g:spellfile_URL =~ '^ftp://'
166 " for an ftp server use a default login and password to avoid a prompt
167 let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
168 let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
169 exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
170 else
171 exe 'Nread ' g:spellfile_URL . '/' . a:fname
172 endif
Bram Moolenaar49325942007-05-10 19:19:59 +0000173
174 if exists("save_ew")
175 let g:netrw_use_errorwindow = save_ew
176 else
177 unlet g:netrw_use_errorwindow
178 endif
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000179endfunc