You patch seems to work but I do not really understand what is going on. Could you explain a bit?
I found this notifyCursorLeaves(old, new) function and see what is going on. If Abdel does not see any side effect of calling too many notifyCursorLeaves, your patch has my OK. I, starting from the very beginning, dislike the fact that latex_code() is called every time a cursor leave a mathed. But again, your patch is simpler and fixes the problem. I may revert yours and the whole notifyCursorLeaves stuff, and replace them with inset::markDirty() after 1.5.0. Cheers, Bo