diff --git a/src/core/BufferView.cc b/src/core/BufferView.cc index f4fd123..9ea5f1e 100644 --- a/src/core/BufferView.cc +++ b/src/core/BufferView.cc @@ -192,13 +192,25 @@ bool BufferView::cursor_move(CursorMovement which, uint32_t c, bool allow_eol) moved = m_iterator->go_backward_in_line_on_to_char(c); break; case CursorMovement::TOP_OF_SCREEN: - moved = cursor_move_to_screen_position(0, effective_scroll_offset(), allow_eol); + { + int n_lines = n_screen_rows_with_content(); + int y = (n_lines < m_height) ? 0 : effective_scroll_offset(); + moved = cursor_move_to_screen_position(0, y, allow_eol); + } break; case CursorMovement::MIDDLE_OF_SCREEN: - moved = cursor_move_to_screen_position(0, m_height / 2, allow_eol); + { + int n_lines = n_screen_rows_with_content(); + int y = (n_lines < m_height) ? (n_lines / 2) : (m_height / 2); + moved = cursor_move_to_screen_position(0, y, allow_eol); + } break; case CursorMovement::BOTTOM_OF_SCREEN: - moved = cursor_move_to_screen_position(0, m_height - effective_scroll_offset() - 1, allow_eol); + { + int n_lines = n_screen_rows_with_content(); + int y = (n_lines < m_height) ? (n_lines - 1) : (m_height - effective_scroll_offset() - 1); + moved = cursor_move_to_screen_position(0, y, allow_eol); + } break; } if (moved) @@ -582,3 +594,13 @@ int BufferView::calculate_rows_below_screen(int stop_at) } return lines; } + +int BufferView::n_screen_rows_with_content() const +{ + if (m_lines.size() > 0u) + { + auto line_desc = m_lines.rbegin(); + return line_desc->row_offset + line_desc->n_rows; + } + return 0; +} diff --git a/src/core/BufferView.h b/src/core/BufferView.h index ea01835..12af785 100644 --- a/src/core/BufferView.h +++ b/src/core/BufferView.h @@ -142,6 +142,7 @@ protected: bool allow_eol); int calculate_rows_above_screen(int stop_at); int calculate_rows_below_screen(int stop_at); + int n_screen_rows_with_content() const; }; #endif