@move_cursor new_cursor
return false
when KEY_DOWN
+ if @cursor?
+ new_cursor = @cursor
+ # go next until we move on the y axis
+ while new_cursor.y <= @cursor.y
+ new_cursor = find_next_cursor_position @tree, new_cursor
+ return false unless new_cursor?
+ # done early if we're already right of old cursor position
+ if new_cursor.x >= @cursor.x
+ # this would be strange, but could happen due to runaround
+ @move_cursor new_cursor
+ return false
+ target_y = new_cursor.y
+ # search rightward, until we find the closest position
+ # new_cursor is the next-most position we've checked
+ # prev_cursor is the older value, so it's not as next as new_cursor
+ while new_cursor.x < @cursor.x and new_cursor.y is target_y
+ prev_cursor = new_cursor
+ new_cursor = find_next_cursor_position @tree, new_cursor
+ break unless new_cursor?
+ # move cursor to prev_cursor or new_cursor
+ if new_cursor?
+ if new_cursor.y is target_y
+ # both valid, and on the same line, use closest
+ if (new_cursor.x - @cursor.x) < (@cursor.x - prev_cursor.x)
+ @move_cursor new_cursor
+ else
+ @move_cursor prev_cursor
+ else
+ # new_cursor on wrong line, use prev_cursor
+ @move_cursor prev_cursor
+ else
+ # can't go any further prev, use prev_cursor
+ @move_cursor prev_cursor
+ else
+ # move cursor to first position in document
+ new_cursor = last_cursor_position @tree
+ if new_cursor?
+ @move_cursor new_cursor
return false
when KEY_END
return false