preserve cursor column when moving up/down lines

This commit is contained in:
Josh Holtrop 2016-12-25 18:07:45 -05:00
parent e21c24e2cc
commit 59b8fc5e31
2 changed files with 44 additions and 0 deletions

View File

@ -6,6 +6,7 @@ BufferPane::BufferPane(Window * window, std::shared_ptr<Buffer> 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;
}
}
});
}

View File

@ -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<Buffer> m_buffer;
@ -63,6 +64,7 @@ protected:
int m_cursor_virtual_column;
std::shared_ptr<Buffer::Iterator> m_iterator;
std::list<std::pair<int, Buffer::Iterator>> m_screen_lines;
int m_target_column;
};
#endif