Limit cursor position to buffer length