diff --git a/src/gui/BufferPane.cc b/src/gui/BufferPane.cc index ad5eca2..2d6d7b9 100644 --- a/src/gui/BufferPane.cc +++ b/src/gui/BufferPane.cc @@ -6,6 +6,7 @@ BufferPane::BufferPane(Window * window, std::shared_ptr buffer) m_cursor_screen_row = 0; m_scroll_offset = 5; m_iterator = buffer->add_iterator(); + m_target_column = 0; } void BufferPane::resize(int width, int height) @@ -359,7 +360,48 @@ void BufferPane::cursor_move(CursorMovement which) } if (moved) { + switch (which) + { + case CursorMovement::LEFT: + case CursorMovement::RIGHT: + { + int cursor_row_offset; + Buffer::Iterator start_of_line = *m_iterator; + start_of_line.go_start_of_line(); + rows_in_line_with_iterator_offset(start_of_line, *m_iterator, &cursor_row_offset); + m_target_column = m_cursor_virtual_column; + } + break; + case CursorMovement::UP: + forward_to_column(m_target_column, m_buffer->insert_mode()); + break; + case CursorMovement::DOWN: + forward_to_column(m_target_column, m_buffer->insert_mode()); + break; + case CursorMovement::SOL: + m_target_column = 0; + break; + case CursorMovement::EOL: + m_target_column = INT_MAX; + break; + } m_cursor_screen_row = determine_new_cursor_screen_row(); m_window->request_redraw(); } } + +void BufferPane::forward_to_column(int column, bool allow_eol) +{ + Buffer::Iterator start_of_line = *m_iterator; + start_of_line.go_start_of_line(); + walk_line(start_of_line, [this, &column, &allow_eol](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) { + uint32_t code_point = *i; + if ((code_point != '\n') || allow_eol) + { + if (virtual_column <= column) + { + *m_iterator = i; + } + } + }); +} diff --git a/src/gui/BufferPane.h b/src/gui/BufferPane.h index 1c1b6c9..6a84b0f 100644 --- a/src/gui/BufferPane.h +++ b/src/gui/BufferPane.h @@ -53,6 +53,7 @@ protected: return m_height - (row + 1) * m_window->font()->get_line_height(); } void cursor_move(CursorMovement which); + void forward_to_column(int column, bool allow_eol); Window * m_window; std::shared_ptr m_buffer; @@ -63,6 +64,7 @@ protected: int m_cursor_virtual_column; std::shared_ptr m_iterator; std::list> m_screen_lines; + int m_target_column; }; #endif