Sunday, August 23, 2020

Per-directory word lists

Hi all,

I use Vim's built-in spell-checking a lot. I have a global personal word
list that's always in effect, but recently I felt the need for per-directory
word lists too. The idea is that one particular project might use a bunch of
proper nouns that I want to be able to check, but which don't really belong
in my global word list.

I came up with a configuration that will let me create a file named
"spellfile" in any directory. When I start to edit any other file in that
directory, Vim will notice the existence of this word list and append its
path to my 'spellfile' setting. Then I can add words to the list using
commands like "2zg". This is what I added to my vimrc:

function! LoadDirSpecificWordList()
let l:spell_file = expand('%:p:h') .. '/spellfile'
if !filereadable(l:spell_file)
return
endif

let l:spell_file_dir = systemlist('mktemp -t -d vim_XXXXXXXX')[0]
let l:spell_file_link = l:spell_file_dir .. '/en.utf-8.add'

let l:args = ['ln', '-s', '-r', shellescape(l:spell_file),
\ shellescape(l:spell_file_link)]
let l:output = system(join(l:args))
if v:shell_error != 0
echoerr 'Could not create symlink: ' .. l:output
endif

let &l:spellfile .= ',' .. l:spell_file_link
silent exe ':mkspell! ' .. l:spell_file_link
endfunction

autocmd BufReadPost * call LoadDirSpecificWordList()

Vim requires that word lists be named in a certain way ("en.utf-8.add", in
my case), but I'd rather use a different name ("spellfile"), and I also
don't want a .spl file to be left around in each directory where I use this.
So this function creates a temporary directory each time a per-directory
word list is found and it creates a symbolic link named "en.utf-8.add"
within that directory that points to the "real" file. [1] It also calls
":mkspell!" on the word list, because Vim doesn't seem to actually read the
file unless I do this--merely adding its path to 'spellfile' doesn't do it.

I have some questions about this approach:

1. Am I correct that Vim doesn't already have support for per-directory word
lists (or something similar)?
2. Am I also correct that word lists must be named something like
"en.utf-8.add"?
3. Is there a way to prompt Vim to read a word list without calling
":mkspell!" on it? I would have expected that appending the file's path
to 'spellfile' would trigger that to happen, but it seems not. I ask
because that command takes a small but noticeable amount of time to run,
which means a bit of a delay each time I start editing a file that has a
per-directory word list.

I'd appreciate any answers to these questions, or any comments on or
criticisms of the configuration I've come up with.

Benjamin


[1] My code makes no attempt to clean up that temporary directory when I'm
done editing a file. This works okay for me, but YMMV. Note also that
this code assumes a Unix-like system.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/ke7dw9r3.16ucll%40bdesham.net.

No comments:

Post a Comment