You could have look at the spellcheck plugin, which does a similar job
The code* looks pretty arcane to me - I'm afraid can't really make sense of it. I'll take another look later, but I'm not very hopeful...
* http://tinyurl.com/4dtw7s (https://geany-plugins.svn.sourceforge.net/svnroot/geany-plugins/trunk/spellc... src/spellcheck.c)