blob: 6fedac190270ea966215476ba7c7e2c47db421de [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 Moolenaar706cdeb2007-05-06 21:55:31 +00003" Last Change: 2007 May 06
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
109 exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname
110
111 " Also download the .sug file, if the user wants to.
112 let msg = "Do you want me to try getting the .sug file?\n"
113 let msg .= "This will improve making suggestions for spelling mistakes,\n"
114 let msg .= "but it uses quite a bit of memory."
115 if confirm(msg, "&No\n&Yes") == 2
116 g/^/d
117 let fname = substitute(fname, '\.spl$', '.sug', '')
118 echo 'Downloading ' . fname . '...'
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000119 call spellfile#Nread(fname)
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000120 if getline(2) =~ 'VIMsug'
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000121 1d
122 exe "write " . escape(dirlist[dirchoice], ' ') . '/' . fname
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000123 set nomod
124 else
125 echo 'Sorry, downloading failed'
126 " Go back to our own buffer/window, Nread() may have taken us to
127 " another window.
128 if newbufnr != winbufnr(0)
129 let winnr = bufwinnr(newbufnr)
130 if winnr != -1
131 exe winnr . "wincmd w"
132 endif
133 endif
134 if newbufnr == winbufnr(0)
135 set nomod
136 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000137 endif
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000138 endif
139 endif
140
Bram Moolenaar706cdeb2007-05-06 21:55:31 +0000141 " Wipe out the buffer we used.
142 exe newbufnr . "bwipe"
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000143 endif
144endfunc
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000145
Bram Moolenaar9526a542006-08-29 20:31:34 +0000146" Read "fname" from the server.
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000147function! spellfile#Nread(fname)
Bram Moolenaar9526a542006-08-29 20:31:34 +0000148 if g:spellfile_URL =~ '^ftp://'
149 " for an ftp server use a default login and password to avoid a prompt
150 let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
151 let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
152 exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
153 else
154 exe 'Nread ' g:spellfile_URL . '/' . a:fname
155 endif
Bram Moolenaarc9a99912006-05-13 12:25:55 +0000156endfunc