Factorize mode switching in put_line().