Better quote+wrap algorithm. Fixes NB#148667.