spellcheck: Fix crash when closing a document quickly after adding data to it

When using timeout callbacks, we need to check the documents are still valid
in the callbacks, in the case they e.g. have been closed since the timeout
started.

----

(the diff below only adds the `DOC_VALID()` test, other changes are whitespace changes -- check with `git diff -b`)