Fix assertion failure when deleting all lines

Lines need to be deselected before they're deleted, but new lines need
to be committed before they can be selected, so reorder things to
remove -> insert new line if needed -> commit -> update selection -> delete.