Fix selecting past last line

Continuation of #3556

Fixes #3527