Make pasting to the REPL more robust to syntax errors

All text including and following a syntax error is output together on one line.
Merge pull request #6689 from mbauman/smarter-paste

Make pasting to the REPL more robust to syntax errors