Maybe this will work,
until ((((mode = 1) and (button1 = buttonpress)) or ((mode = 1) and (button2 = buttonpress))) or ((mode = 2) and (button3 = buttonpress)))
You could also use a variable to store the intermediate results and finally arrive at the end result.
Then you use this variable as the until's condition (either true or false).