Fix crash if document closed while custom command is executing

Custom command callback never checked that the doc was still valid.
Can still paste in the wrong doc if user closes and opens while
the command is running, but not crash.