Solved!
TNZ's idea did work up to a point. Islam factions were prevented from joining the jihad but it had no effect on any armies that were already joined (I was hoping that it would cancel it for them but no such luck). Worse yet, if the target's owner is allied with all of these jihading army factions then they don't attack the settlement. They all just stand about. After 50 turns I stopped testing; clearly nothing was going to happen. (In one test two of those factions actually went to war with each other, though their homelands were nowhere near each other.)
Also, TNZ made the excellent point (elsewhere) that even if the target was originally 'valid' (settlement owner wise) if an 'invalid' faction captures it then they are now the target faction. As soon as a jihad army attacks the settlement there will be war between these two factions.
The solution handles both situations.
1) During the turn of islam factions, change the religion of 'invalid' factions also to islam. This eliminates all of their settlements as possible jihad targets, because they are now islam-owned. At the end of the turn change them back to whatever they should be. This is only done during AI turns, not the player's, otherwise all of those other factions will appear to the player as being islam. So this only prevents the AI from calling "wrong jihads", not the player (which is fine).
2) When an 'invalid' faction captures a jihad target, change their religion to islam. This instantly cancels the jihad and their religion can now be switched back.
3) If the target settlement is gifted to an 'invalid' faction then do the same as 2).
As an alternative to 1) I tried switching the religion to islam when the jihad was called. This didn't work. The religion did switch but it did not cancel the jihad. Even if it did work it would mean that the jihad call is still wasted; a wait before they can try again... and that might be a wasted one too... and so on...
On a side note, I found that this...
Code:
monitor_event ArmyTakesCrusadeTarget IsJihad
...only fires when the army - all units within it - is on jihad. It does not fire if the army is a mixture of jihad and non-jihad. And does not fire if the army is not on jihad at all. And therefore does not fire if the army is of another faction (non-islam).
This works just fine...
Code:
monitor_event GeneralCaptureSettlement IsJihadTarget