Hi brave, I don’t have the answer but this discuss.kakoune thread might point you in the right direction or NOT. Branching on a boolean option -- without calling the shell. Feel free to just reply NOT or wait for someone smarter .
Thought I better come back and clarify the NOT meaning and purpose in the above paragraph.