From dbe5843d77132546e69c33d2b645868013109130 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 28 Dec 2016 14:41:44 -0500 Subject: [PATCH] add 'A' and 'I' commands --- src/gui/BufferPane.cc | 52 ++++++++++++++++++++++++++++++++++++------- src/gui/BufferPane.h | 11 +++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/gui/BufferPane.cc b/src/gui/BufferPane.cc index a2c4657..7e59c59 100644 --- a/src/gui/BufferPane.cc +++ b/src/gui/BufferPane.cc @@ -333,16 +333,17 @@ void BufferPane::handle_key(uint32_t keyval) case '$': cursor_move(CursorMovement::EOL); break; + case 'A': + enter_insert_mode(EnterInsertModeMode::END_OF_LINE); + break; case 'G': cursor_move(CursorMovement::LAST_LINE); break; + case 'I': + enter_insert_mode(EnterInsertModeMode::START_OF_LINE); + break; case 'a': - if (**m_iterator != '\n') - { - m_iterator->go_forward(); - } - m_buffer->enter_insert_mode(*m_iterator); - m_window->request_redraw(); + enter_insert_mode(EnterInsertModeMode::END_OF_CHAR); break; case 'g': cursor_move(CursorMovement::FIRST_LINE); @@ -351,8 +352,7 @@ void BufferPane::handle_key(uint32_t keyval) cursor_move(CursorMovement::LEFT); break; case 'i': - m_buffer->enter_insert_mode(*m_iterator); - m_window->request_redraw(); + enter_insert_mode(EnterInsertModeMode::START_OF_CHAR); break; case 'j': cursor_move(CursorMovement::DOWN); @@ -486,6 +486,42 @@ void BufferPane::cursor_move(CursorMovement which) } } +void BufferPane::enter_insert_mode(EnterInsertModeMode which) +{ + if (m_buffer->insert_mode()) + return; + + switch (which) + { + case EnterInsertModeMode::START_OF_CHAR: + m_buffer->enter_insert_mode(*m_iterator); + break; + case EnterInsertModeMode::END_OF_CHAR: + if (**m_iterator != '\n') + { + m_iterator->go_forward(); + } + m_buffer->enter_insert_mode(*m_iterator); + break; + case EnterInsertModeMode::START_OF_LINE: + m_iterator->go_start_of_line(); + enter_insert_mode(EnterInsertModeMode::START_OF_CHAR); + break; + case EnterInsertModeMode::END_OF_LINE: + m_iterator->go_end_of_line(false); + enter_insert_mode(EnterInsertModeMode::END_OF_CHAR); + break; + case EnterInsertModeMode::NEW_LINE_BEFORE: + /* TODO */ + break; + case EnterInsertModeMode::NEW_LINE_AFTER: + /* TODO */ + break; + } + + m_window->request_redraw(); +} + void BufferPane::forward_to_column(int column, bool allow_eol) { Buffer::Iterator start_of_line = *m_iterator; diff --git a/src/gui/BufferPane.h b/src/gui/BufferPane.h index 8657104..135a03f 100644 --- a/src/gui/BufferPane.h +++ b/src/gui/BufferPane.h @@ -29,6 +29,16 @@ protected: LAST_LINE, }; + enum class EnterInsertModeMode : uint8_t + { + START_OF_CHAR, + END_OF_CHAR, + START_OF_LINE, + END_OF_LINE, + NEW_LINE_BEFORE, + NEW_LINE_AFTER, + }; + int effective_scroll_offset() { return std::min(m_scroll_offset, (m_rows - 1) / 2); @@ -53,6 +63,7 @@ protected: return m_height - (row + 1) * m_window->font()->get_line_height(); } void cursor_move(CursorMovement which); + void enter_insert_mode(EnterInsertModeMode which); void forward_to_column(int column, bool allow_eol); size_t display_line() const { return m_iterator->line() + 1u; } size_t display_column() const;