blob: 1ca76a4aeb1a6d13dc45db92fbaa26d241c7659f [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 Moolenaar8c8de832008-06-24 22:58:06 +00003" Last Change: 2008 May 29
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00004
5if !exists('g:spellfile_URL')
Bram Moolenaarc9a99912006-05-13 12:25:55 +00006 let g:spellfile_URL = 'ftp://ftp.vim.org/pub/vim/runtime/spell'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00007endif
8let s:spellfile_URL = '' " Start with nothing so that s:donedict is reset.
9
10" This function is used for the spellfile plugin.
11function! 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 Moolenaar706cdeb2007-05-06 21:55:31 +000061 " Remember the buffer number, we check it below.
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000062 new
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000063 let newbufnr = winbufnr(0)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000064 setlocal bin
65 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +000066 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000067 if getline(2) !~ 'VIMspell'
68 " Didn't work, perhaps there is an ASCII one.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000069 " 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 Moolenaar1ef15e32006-02-01 21:56:25 +000089 let fname = a:lang . '.ascii.spl'
90 echo 'Could not find it, trying ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +000091 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000092 if getline(2) !~ 'VIMspell'
93 echo 'Sorry, downloading failed'
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000094 exe newbufnr . "bwipe!"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000095 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
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000109 if exists('*fnameescape')
110 let dirname = fnameescape(dirlist[dirchoice])
111 else
112 let dirname = escape(dirlist[dirchoice], ' ')
113 endif
114 exe "write " . dirname . '/' . fname
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000115
116 " Also download the .sug file, if the user wants to.
117 let msg = "Do you want me to try getting the .sug file?\n"
118 let msg .= "This will improve making suggestions for spelling mistakes,\n"
119 let msg .= "but it uses quite a bit of memory."
120 if confirm(msg, "&No\n&Yes") == 2
121 g/^/d
122 let fname = substitute(fname, '\.spl$', '.sug', '')
123 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000124 call spellfile#Nread(fname)
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000125 if getline(2) =~ 'VIMsug'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000126 1d
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000127 exe "write " . dirname . '/' . fname
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000128 set nomod
129 else
130 echo 'Sorry, downloading failed'
131 " Go back to our own buffer/window, Nread() may have taken us to
132 " another window.
133 if newbufnr != winbufnr(0)
134 let winnr = bufwinnr(newbufnr)
135 if winnr != -1
136 exe winnr . "wincmd w"
137 endif
138 endif
139 if newbufnr == winbufnr(0)
140 set nomod
141 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000142 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000143 endif
144 endif
145
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000146 " Wipe out the buffer we used.
147 exe newbufnr . "bwipe"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000148 endif
149endfunc
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000150
Bram Moolenaar9526a542006-08-29 20:31:34 +0000151" Read "fname" from the server.
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000152function! spellfile#Nread(fname)
Bram Moolenaar49325942007-05-10 19:19:59 +0000153 " We do our own error handling, don't want a window for it.
154 if exists("g:netrw_use_errorwindow")
155 let save_ew = g:netrw_use_errorwindow
156 endif
157 let g:netrw_use_errorwindow=0
158
Bram Moolenaar9526a542006-08-29 20:31:34 +0000159 if g:spellfile_URL =~ '^ftp://'
160 " for an ftp server use a default login and password to avoid a prompt
161 let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
162 let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
163 exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
164 else
165 exe 'Nread ' g:spellfile_URL . '/' . a:fname
166 endif
Bram Moolenaar49325942007-05-10 19:19:59 +0000167
168 if exists("save_ew")
169 let g:netrw_use_errorwindow = save_ew
170 else
171 unlet g:netrw_use_errorwindow
172 endif
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000173endfunc