spellfile.vim 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. " Vim script to download a missing spell file
  2. " Maintainer: Bram Moolenaar <Bram@vim.org>
  3. " Last Change: 2020 Jul 10
  4. if !exists('g:spellfile_URL')
  5. " Always use https:// because it's secure. The certificate is for nluug.nl,
  6. " thus we can't use the alias ftp.vim.org here.
  7. let g:spellfile_URL = 'https://ftp.nluug.nl/pub/vim/runtime/spell'
  8. endif
  9. let s:spellfile_URL = '' " Start with nothing so that s:donedict is reset.
  10. " This function is used for the spellfile plugin.
  11. function! 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. let lang = tolower(a:lang)
  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. " I will say this only once!
  26. if has_key(s:donedict, lang . &enc)
  27. if &verbose
  28. echomsg 'spellfile#LoadFile(): Tried this language/encoding before.'
  29. endif
  30. return
  31. endif
  32. let s:donedict[lang . &enc] = 1
  33. " Find spell directories we can write in.
  34. let [dirlist, dirchoices] = spellfile#GetDirChoices()
  35. if len(dirlist) == 0
  36. let dir_to_create = spellfile#WritableSpellDir()
  37. if &verbose || dir_to_create != ''
  38. echomsg 'spellfile#LoadFile(): There is no writable spell directory.'
  39. endif
  40. if dir_to_create != ''
  41. if confirm("Shall I create " . dir_to_create, "&Yes\n&No", 2) == 1
  42. " After creating the directory it should show up in the list.
  43. call mkdir(dir_to_create, "p")
  44. let [dirlist, dirchoices] = spellfile#GetDirChoices()
  45. endif
  46. endif
  47. if len(dirlist) == 0
  48. return
  49. endif
  50. endif
  51. let msg = 'Cannot find spell file for "' . 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 = lang . '.' . enc . '.spl'
  59. " Split the window, read the file into a new buffer.
  60. " Remember the buffer number, we check it below.
  61. new
  62. let newbufnr = winbufnr(0)
  63. setlocal bin fenc=
  64. echo 'Downloading ' . fname . '...'
  65. call spellfile#Nread(fname)
  66. if getline(2) !~ 'VIMspell'
  67. " Didn't work, perhaps there is an ASCII one.
  68. " Careful: Nread() may have opened a new window for the error message,
  69. " we need to go back to our own buffer and window.
  70. if newbufnr != winbufnr(0)
  71. let winnr = bufwinnr(newbufnr)
  72. if winnr == -1
  73. " Our buffer has vanished!? Open a new window.
  74. echomsg "download buffer disappeared, opening a new one"
  75. new
  76. setlocal bin fenc=
  77. else
  78. exe winnr . "wincmd w"
  79. endif
  80. endif
  81. if newbufnr == winbufnr(0)
  82. " We are back the old buffer, remove any (half-finished) download.
  83. g/^/d
  84. else
  85. let newbufnr = winbufnr(0)
  86. endif
  87. let fname = lang . '.ascii.spl'
  88. echo 'Could not find it, trying ' . fname . '...'
  89. call spellfile#Nread(fname)
  90. if getline(2) !~ 'VIMspell'
  91. echo 'Sorry, downloading failed'
  92. exe newbufnr . "bwipe!"
  93. return
  94. endif
  95. endif
  96. " Delete the empty first line and mark the file unmodified.
  97. 1d
  98. set nomod
  99. let msg = "In which directory do you want to write the file:"
  100. for i in range(len(dirlist))
  101. let msg .= "\n" . (i + 1) . '. ' . dirlist[i]
  102. endfor
  103. let dirchoice = confirm(msg, dirchoices) - 2
  104. if dirchoice >= 0
  105. if exists('*fnameescape')
  106. let dirname = fnameescape(dirlist[dirchoice])
  107. else
  108. let dirname = escape(dirlist[dirchoice], ' ')
  109. endif
  110. setlocal fenc=
  111. exe "write " . dirname . '/' . fname
  112. " Also download the .sug file, if the user wants to.
  113. let msg = "Do you want me to try getting the .sug file?\n"
  114. let msg .= "This will improve making suggestions for spelling mistakes,\n"
  115. let msg .= "but it uses quite a bit of memory."
  116. if confirm(msg, "&No\n&Yes") == 2
  117. g/^/d
  118. let fname = substitute(fname, '\.spl$', '.sug', '')
  119. echo 'Downloading ' . fname . '...'
  120. call spellfile#Nread(fname)
  121. if getline(2) =~ 'VIMsug'
  122. 1d
  123. exe "write " . dirname . '/' . fname
  124. set nomod
  125. else
  126. echo 'Sorry, downloading failed'
  127. " Go back to our own buffer/window, Nread() may have taken us to
  128. " another window.
  129. if newbufnr != winbufnr(0)
  130. let winnr = bufwinnr(newbufnr)
  131. if winnr != -1
  132. exe winnr . "wincmd w"
  133. endif
  134. endif
  135. if newbufnr == winbufnr(0)
  136. set nomod
  137. endif
  138. endif
  139. endif
  140. endif
  141. " Wipe out the buffer we used.
  142. exe newbufnr . "bwipe"
  143. endif
  144. endfunc
  145. " Read "fname" from the server.
  146. function! spellfile#Nread(fname)
  147. " We do our own error handling, don't want a window for it.
  148. if exists("g:netrw_use_errorwindow")
  149. let save_ew = g:netrw_use_errorwindow
  150. endif
  151. let g:netrw_use_errorwindow=0
  152. if g:spellfile_URL =~ '^ftp://'
  153. " for an ftp server use a default login and password to avoid a prompt
  154. let machine = substitute(g:spellfile_URL, 'ftp://\([^/]*\).*', '\1', '')
  155. let dir = substitute(g:spellfile_URL, 'ftp://[^/]*/\(.*\)', '\1', '')
  156. exe 'Nread "' . machine . ' anonymous vim7user ' . dir . '/' . a:fname . '"'
  157. else
  158. exe 'Nread ' g:spellfile_URL . '/' . a:fname
  159. endif
  160. if exists("save_ew")
  161. let g:netrw_use_errorwindow = save_ew
  162. else
  163. unlet g:netrw_use_errorwindow
  164. endif
  165. endfunc
  166. " Get a list of writable spell directories and choices for confirm().
  167. function! spellfile#GetDirChoices()
  168. let dirlist = []
  169. let dirchoices = '&Cancel'
  170. for dir in split(globpath(&rtp, 'spell'), "\n")
  171. if filewritable(dir) == 2
  172. call add(dirlist, dir)
  173. let dirchoices .= "\n&" . len(dirlist)
  174. endif
  175. endfor
  176. return [dirlist, dirchoices]
  177. endfunc
  178. function! spellfile#WritableSpellDir()
  179. if has("unix")
  180. " For Unix always use the $HOME/.vim directory
  181. return $HOME . "/.vim/spell"
  182. endif
  183. for dir in split(&rtp, ',')
  184. if filewritable(dir) == 2
  185. return dir . "/spell"
  186. endif
  187. endfor
  188. return ''
  189. endfunction