position to the cursor position. When `end_pos` is given, don't look past the position. r