Fix selecting past the last line

Continuation of #3556

Fixes #3527