aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2019-06-17 00:47:52 +1200
committerTom Ryder <tom@sanctum.geek.nz>2019-06-17 00:47:52 +1200
commit13d56da45a47f73b55aa5544aafe68d36774b51d (patch)
tree67507f0f7862fa639c8f5eb93e7410b879405ac6
parentRefactor select_old_files.vim to allow count (diff)
downloaddotfiles-13d56da45a47f73b55aa5544aafe68d36774b51d.tar.gz
dotfiles-13d56da45a47f73b55aa5544aafe68d36774b51d.zip
Overhaul local Vim spellfile word list plugin
-rw-r--r--vim/autoload/spellfile_local.vim79
-rw-r--r--vim/plugin/spellfile_local.vim13
-rw-r--r--vim/vimrc16
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, '\\\@<!,[, ]*')
+ \,"substitute(v:val, '\\\\,', '', 'g')"
+ \)
+endfunction
+
+function! s:JoinOption(list) abort
+ return join(map(
+ \ a:list
+ \,"substitute(v:val, '\\\\\\@<!,', '\\\\,', 'g')"
+ \), ',')
+endfunction
+
+function! s:Establish(path) abort
+ return isdirectory(a:path)
+ \ || exists('*mkdir') && mkdir(a:path, 'p', 0700)
+endfunction
+
function! spellfile_local#() abort
- let spellfile = join([
- \ substitute(expand('%:p'), '[^0-9A-Za-z_.-]', '%', 'g'),
- \ substitute(v:lang, '_.*', '', ''),
- \ &encoding
- \ ], '.') . '.add'
- Establish $MYVIM/cache/spell/local
- execute 'setlocal spellfile+=$MYVIM/cache/spell/local/'.spellfile
- nnoremap <buffer> zG 2zg
- xnoremap <buffer> 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,