It could be a pretty challenging task to handle all the options properly. But if all you ask is just a simple and very incomplete example code then, perhaps, this small snippet could be of some help:
inoremap <expr>( Paren()
function! Paren() abort
return printf("()%s<Left>",
search('v(if|while)s*%#', 'bn', line('.')) ? "" : ";<Left>")
endfunction
The idea is to match the current line (just before the cursor) against regex. Depending on the result, we build an expression to be inserted.
See :h search()
, :h :map-expression
etc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…