diff --git a/src/core/GapBuffer.cc b/src/core/GapBuffer.cc index db436f2..dd229c0 100644 --- a/src/core/GapBuffer.cc +++ b/src/core/GapBuffer.cc @@ -184,6 +184,35 @@ bool GapBuffer::Cursor::go_right() return false; } +bool GapBuffer::Cursor::go_up(size_t target_column) +{ + Cursor c2 = *this; + c2.go_start_of_line(); + if (!c2.m_iterator.check_go_back()) + { + return false; + } + c2.go_start_of_line(); + *this = c2; + m_line--; + forward_to_column(target_column); + return true; +} + +bool GapBuffer::Cursor::go_down(size_t target_column) +{ + Cursor c2 = *this; + c2.go_end_of_line(); + if (!c2.m_iterator.check_go_forward()) + { + return false; + } + *this = c2; + m_line++; + forward_to_column(target_column); + return true; +} + void GapBuffer::Cursor::init_column() { if (*m_iterator == '\t') @@ -206,3 +235,20 @@ void GapBuffer::Cursor::calculate_column() } m_column = tmp_cursor.m_column; } + +void GapBuffer::Cursor::forward_to_column(size_t target_column) +{ + int32_t diff = abs((int32_t)(target_column - m_column)); + if (diff == 0) + return; + while (go_right()) + { + int32_t new_diff = abs((int32_t)(target_column - m_column)); + if (new_diff > diff) + { + go_left(); + return; + } + diff = new_diff; + } +} diff --git a/src/core/GapBuffer.h b/src/core/GapBuffer.h index 0523fc9..78c3699 100644 --- a/src/core/GapBuffer.h +++ b/src/core/GapBuffer.h @@ -70,6 +70,8 @@ public: bool go_end_of_line(); bool go_left(); bool go_right(); + bool go_up(size_t target_column); + bool go_down(size_t target_column); bool operator<(const Cursor & other) { return m_iterator < other.m_iterator; @@ -82,6 +84,7 @@ public: void init_column(); void calculate_column(); + void forward_to_column(size_t target_column); }; uint8_t tabstop;