Fix problem where a change to the selection causes unwanted history event merging

Issue #2717