blob: 43f7dffa1a893095cd5bc3f2ea9269e036bfdbef [file] [log] [blame]
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00001" Vim script to download a missing spell file
Christian Brabandte978b452023-08-13 10:33:05 +02002" Maintainer: The Vim Project <https://github.com/vim/vim>
3" Last Change: 2023 Aug 10
4" Former Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005
6if !exists('g:spellfile_URL')
Bram Moolenaar7ff78462020-07-10 22:00:53 +02007 " Always use https:// because it's secure. The certificate is for nluug.nl,
8 " thus we can't use the alias ftp.vim.org here.
9 let g:spellfile_URL = 'https://ftp.nluug.nl/pub/vim/runtime/spell'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000010endif
11let s:spellfile_URL = '' " Start with nothing so that s:donedict is reset.
12
13" This function is used for the spellfile plugin.
14function! spellfile#LoadFile(lang)
15 " If the netrw plugin isn't loaded we silently skip everything.
16 if !exists(":Nread")
17 if &verbose
18 echomsg 'spellfile#LoadFile(): Nread command is not available.'
19 endif
20 return
21 endif
Bram Moolenaara9604e62018-07-21 05:56:22 +020022 let lang = tolower(a:lang)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000023
24 " If the URL changes we try all files again.
25 if s:spellfile_URL != g:spellfile_URL
26 let s:donedict = {}
27 let s:spellfile_URL = g:spellfile_URL
28 endif
29
30 " I will say this only once!
Bram Moolenaara9604e62018-07-21 05:56:22 +020031 if has_key(s:donedict, lang . &enc)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000032 if &verbose
33 echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
34 endif
35 return
36 endif
Bram Moolenaara9604e62018-07-21 05:56:22 +020037 let s:donedict[lang . &enc] = 1
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000038
39 " Find spell directories we can write in.
Bram Moolenaar5c736222010-01-06 20:54:52 +010040 let [dirlist, dirchoices] = spellfile#GetDirChoices()
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000041 if len(dirlist) == 0
Bram Moolenaar5c736222010-01-06 20:54:52 +010042 let dir_to_create = spellfile#WritableSpellDir()
43 if &verbose || dir_to_create != ''
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000044 echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
45 endif
Bram Moolenaar5c736222010-01-06 20:54:52 +010046 if dir_to_create != ''
47 if confirm("Shall I create " . dir_to_create, "&Yes\n&No", 2) == 1
48 " After creating the directory it should show up in the list.
49 call mkdir(dir_to_create, "p")
50 let [dirlist, dirchoices] = spellfile#GetDirChoices()
51 endif
52 endif
53 if len(dirlist) == 0
54 return
55 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000056 endif
57
Bram Moolenaara9604e62018-07-21 05:56:22 +020058 let msg = 'Cannot find spell file for "' . lang . '" in ' . &enc
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000059 let msg .= "\nDo you want me to try downloading it?"
60 if confirm(msg, "&Yes\n&No", 2) == 1
61 let enc = &encoding
62 if enc == 'iso-8859-15'
63 let enc = 'latin1'
64 endif
Bram Moolenaara9604e62018-07-21 05:56:22 +020065 let fname = lang . '.' . enc . '.spl'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000066
67 " Split the window, read the file into a new buffer.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000068 " Remember the buffer number, we check it below.
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000069 new
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000070 let newbufnr = winbufnr(0)
Bram Moolenaar6ee8d892012-01-10 14:55:01 +010071 setlocal bin fenc=
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000072 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +000073 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000074 if getline(2) !~ 'VIMspell'
75 " Didn't work, perhaps there is an ASCII one.
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000076 " Careful: Nread() may have opened a new window for the error message,
77 " we need to go back to our own buffer and window.
78 if newbufnr != winbufnr(0)
79 let winnr = bufwinnr(newbufnr)
80 if winnr == -1
81 " Our buffer has vanished!? Open a new window.
82 echomsg "download buffer disappeared, opening a new one"
83 new
Bram Moolenaar6ee8d892012-01-10 14:55:01 +010084 setlocal bin fenc=
Bram Moolenaar706cdeb2007-05-06 21:55:31 +000085 else
86 exe winnr . "wincmd w"
87 endif
88 endif
89 if newbufnr == winbufnr(0)
90 " We are back the old buffer, remove any (half-finished) download.
91 g/^/d
92 else
93 let newbufnr = winbufnr(0)
94 endif
95
Bram Moolenaara9604e62018-07-21 05:56:22 +020096 let fname = lang . '.ascii.spl'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000097 echo 'Could not find it, trying ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +000098 call spellfile#Nread(fname)
Bram Moolenaar1ef15e32006-02-01 21:56:25 +000099 if getline(2) !~ 'VIMspell'
100 echo 'Sorry, downloading failed'
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000101 exe newbufnr . "bwipe!"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000102 return
103 endif
104 endif
105
106 " Delete the empty first line and mark the file unmodified.
107 1d
108 set nomod
109
110 let msg = "In which directory do you want to write the file:"
111 for i in range(len(dirlist))
112 let msg .= "\n" . (i + 1) . '. ' . dirlist[i]
113 endfor
114 let dirchoice = confirm(msg, dirchoices) - 2
115 if dirchoice >= 0
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116 if exists('*fnameescape')
117 let dirname = fnameescape(dirlist[dirchoice])
118 else
119 let dirname = escape(dirlist[dirchoice], ' ')
120 endif
Bram Moolenaar6ee8d892012-01-10 14:55:01 +0100121 setlocal fenc=
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000122 exe "write " . dirname . '/' . fname
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000123
124 " Also download the .sug file, if the user wants to.
125 let msg = "Do you want me to try getting the .sug file?\n"
126 let msg .= "This will improve making suggestions for spelling mistakes,\n"
127 let msg .= "but it uses quite a bit of memory."
128 if confirm(msg, "&No\n&Yes") == 2
129 g/^/d
130 let fname = substitute(fname, '\.spl$', '.sug', '')
131 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000132 call spellfile#Nread(fname)
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000133 if getline(2) =~ 'VIMsug'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000134 1d
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000135 exe "write " . dirname . '/' . fname
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000136 set nomod
137 else
138 echo 'Sorry, downloading failed'
139 " Go back to our own buffer/window, Nread() may have taken us to
140 " another window.
141 if newbufnr != winbufnr(0)
142 let winnr = bufwinnr(newbufnr)
143 if winnr != -1
144 exe winnr . "wincmd w"
145 endif
146 endif
147 if newbufnr == winbufnr(0)
148 set nomod
149 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000150 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000151 endif
152 endif
153
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000154 " Wipe out the buffer we used.
155 exe newbufnr . "bwipe"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000156 endif
157endfunc
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000158
Bram Moolenaar9526a542006-08-29 20:31:34 +0000159" Read "fname" from the server.
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000160function! spellfile#Nread(fname)
Bram Moolenaar49325942007-05-10 19:19:59 +0000161 " We do our own error handling, don't want a window for it.
162 if exists("g:netrw_use_errorwindow")
163 let save_ew = g:netrw_use_errorwindow
164 endif
165 let g:netrw_use_errorwindow=0
166
Bram Moolenaar9526a542006-08-29 20:31:34 +0000167 if g:spellfile_URL =~ '^ftp://'
168 " for an ftp server use a default login and password to avoid a prompt
169 let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
170 let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
171 exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
172 else
173 exe 'Nread ' g:spellfile_URL . '/' . a:fname
174 endif
Bram Moolenaar49325942007-05-10 19:19:59 +0000175
176 if exists("save_ew")
177 let g:netrw_use_errorwindow = save_ew
178 else
179 unlet g:netrw_use_errorwindow
180 endif
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000181endfunc
Bram Moolenaar5c736222010-01-06 20:54:52 +0100182
183" Get a list of writable spell directories and choices for confirm().
184function! spellfile#GetDirChoices()
185 let dirlist = []
186 let dirchoices = '&Cancel'
187 for dir in split(globpath(&rtp, 'spell'), "\n")
188 if filewritable(dir) == 2
189 call add(dirlist, dir)
190 let dirchoices .= "\n&" . len(dirlist)
191 endif
192 endfor
193 return [dirlist, dirchoices]
194endfunc
195
196function! spellfile#WritableSpellDir()
197 if has("unix")
198 " For Unix always use the $HOME/.vim directory
199 return $HOME . "/.vim/spell"
200 endif
201 for dir in split(&rtp, ',')
202 if filewritable(dir) == 2
203 return dir . "/spell"
204 endif
205 endfor
206 return ''
207endfunction