From 13d56da45a47f73b55aa5544aafe68d36774b51d Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 17 Jun 2019 00:47:52 +1200 Subject: Overhaul local Vim spellfile word list plugin --- vim/autoload/spellfile_local.vim | 79 +++++++++++++++++++++++++++++++++++----- vim/plugin/spellfile_local.vim | 13 ++----- vim/vimrc | 16 ++++++++ 3 files changed, 89 insertions(+), 19 deletions(-) diff --git a/vim/autoload/spellfile_local.vim b/vim/autoload/spellfile_local.vim index aafe64ef..081b2970 100644 --- a/vim/autoload/spellfile_local.vim +++ b/vim/autoload/spellfile_local.vim @@ -1,11 +1,72 @@ +function! s:SplitOption(string) abort + return map( + \ split(a:string, '\\\@ zG 2zg - xnoremap zG 2zg + + set spellfile< + + let spelllangs = s:SplitOption(&spelllang) + if !len(spelllangs) || &spelllang[0] ==# '' + echoerr 'Blank ''spelllang''' + endif + let spelllang = substitute(spelllangs[0], '_.*', '', '') + + if !len(&encoding) + echoerr 'Blank ''encoding''' + endif + + let spellfiles = s:SplitOption(&spellfile) + if len(spellfiles) != 1 || spellfiles[0] ==# '' + return + endif + + let spelldir = fnamemodify(spellfiles[0], ':h') + if spelldir ==# '' + echoerr 'Blank directory' + endif + + try + let path = substitute(expand('%:p'), '/', '%', 'g') + if path ==# '' + echoerr 'Blank path' + endif + call s:Establish(spelldir.'/path') + call add(spellfiles, spelldir.'/path/'.join([ + \ path + \,spelllang + \,&encoding + \,'add' + \], '.')) + + if &filetype ==# '' + echoerr 'Blank filetype' + endif + call s:Establish(spelldir.'/filetype') + call add(spellfiles, spelldir.'/filetype/'.join([ + \ &filetype + \,spelllang + \,&encoding + \,'add' + \], '.')) + catch + endtry + + let &l:spellfile = s:JoinOption(spellfiles) + endfunction diff --git a/vim/plugin/spellfile_local.vim b/vim/plugin/spellfile_local.vim index c026d626..f6918bfb 100644 --- a/vim/plugin/spellfile_local.vim +++ b/vim/plugin/spellfile_local.vim @@ -1,19 +1,12 @@ -if exists('loaded_spellfile_local') +if exists('loaded_spellfile_local') || &compatible finish endif let loaded_spellfile_local = 1 -let s:spellfile = join([ - \ substitute(v:lang, '_.*', '', ''), - \ &encoding - \ ], '.') . '.add' -Establish $MYVIM/cache/spell -execute 'set spellfile=$MYVIM/cache/spell/'.s:spellfile - -command! -bar AddLocalSpellFile +command! -bar SetLocalSpellFiles \ call spellfile_local#() augroup spellfile_local autocmd BufNew,BufRead * - \ AddLocalSpellFile + \ SetLocalSpellFiles augroup END diff --git a/vim/vimrc b/vim/vimrc index 7f2d9ae8..dc9430e1 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -398,6 +398,22 @@ if has('persistent_undo') set undodir^=$MYVIM/cache/undo// endif +" For spelling, use New Zealand English by default, but later on we'll +" configure a leader mapping to switch to United States English, since I so +" often have to write for Yankees. We'll set the 'spellfile' option too, to +" place it in the cache directory into which we've been putting everything. +" We'll follow Vim's standard naming convention for the file itself, though. +" If available, my plugin spellfile_local.vim will extend this later to add +" more spelling word lists per filetype and per file. +" +set spelllang=en_nz +Establish $MYVIM/cache/spell +let &spellfile = $MYVIM.'/cache/spell/'.join([ + \ substitute(&spelllang, '_.*', '', '') + \,&encoding + \,'add' + \], '.') + " For word completion in insert mode with CTRL-X CTRL-K, or if 'complete' " includes the 'k' flag, the 'dictionary' option specifies the path to the " system word list. This makes the dictionary completion work consistently, -- cgit v1.2.3