From 62868e596d073c5774d1e4712bd5136456747e2f Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 19 Mar 2018 23:20:32 -0400 Subject: [PATCH] Jump to line number for : commands --- src/core/BufferView.cc | 36 ++++++++++++++++++++++++++++++++++++ src/core/BufferView.h | 1 + src/gui/BufferPane.cc | 10 ++++++++++ src/gui/BufferPane.h | 1 + src/gui/Window.cc | 22 ++++++++++++++++++++++ 5 files changed, 70 insertions(+) diff --git a/src/core/BufferView.cc b/src/core/BufferView.cc index f461301..82d2ad7 100644 --- a/src/core/BufferView.cc +++ b/src/core/BufferView.cc @@ -229,6 +229,42 @@ bool BufferView::cursor_move(CursorMovement which, uint32_t c, bool allow_eol) return moved; } +bool BufferView::cursor_move_to_line(size_t target_line) +{ + size_t current_line = m_iterator->line(); + std::shared_ptr it; + if (target_line <= current_line / 2u) + { + it = m_buffer->beginp(); + } + else if (target_line <= ((current_line + m_buffer->end().line()) / 2u)) + { + it = m_iterator->clonep(); + } + else + { + it = m_buffer->endp(); + it->go_back(); + it->go_start_of_line(); + } + while (it->line() < target_line) + { + if (!it->go_next_line()) + break; + } + while (it->line() > target_line) + { + if (!it->go_previous_line()) + break; + } + if (current_line != it->line()) + { + *m_iterator = *it; + return true; + } + return false; +} + void BufferView::scroll_view_up(int n_lines, bool allow_eol) { int orig_cursor_screen_row = m_cursor_screen_row; diff --git a/src/core/BufferView.h b/src/core/BufferView.h index e9b09c1..1a71fcd 100644 --- a/src/core/BufferView.h +++ b/src/core/BufferView.h @@ -83,6 +83,7 @@ public: return BufferLineWalker(m_buffer, start_of_line, m_width, m_character_width_determiner); } bool cursor_move(CursorMovement which, uint32_t c, bool allow_eol); + bool cursor_move_to_line(size_t line); int cursor_screen_row() const { return m_cursor_screen_row; } int cursor_screen_column() const { return m_cursor_screen_column; } int cursor_virtual_column() const { return m_cursor_virtual_column; } diff --git a/src/gui/BufferPane.cc b/src/gui/BufferPane.cc index 0662a28..95ac3f8 100644 --- a/src/gui/BufferPane.cc +++ b/src/gui/BufferPane.cc @@ -377,6 +377,16 @@ void BufferPane::cursor_move(BufferPane::CursorMovement which, uint32_t c) } } +void BufferPane::cursor_move_to_line(size_t target_line) +{ + bool moved = m_buffer_view->cursor_move_to_line(target_line); + if (moved) + { + m_buffer_view->update(); + m_window->request_redraw(); + } +} + void BufferPane::enter_insert_mode(Window::EnterInsertModeMode which) { if (insert_mode()) diff --git a/src/gui/BufferPane.h b/src/gui/BufferPane.h index 5c79edc..50a5174 100644 --- a/src/gui/BufferPane.h +++ b/src/gui/BufferPane.h @@ -32,6 +32,7 @@ public: void resize(int width, int height) override; void draw(); void cursor_move(CursorMovement which, uint32_t c); + void cursor_move_to_line(size_t target_line); void enter_insert_mode(Window::EnterInsertModeMode which); void insert_code_point(uint32_t code_point); void exit_insert_mode(); diff --git a/src/gui/Window.cc b/src/gui/Window.cc index 1366e71..1710129 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -504,6 +504,19 @@ void Window::change_focus(std::shared_ptr buffer_pane) request_redraw(); } +static bool is_number(const EncodedString & s) +{ + size_t size = s.size(); + if (size < 1u) + return false; + for (size_t i = 0u; i < size; i++) + { + if ((s[i] < '0') || (s[i] > '9')) + return false; + } + return true; +} + void Window::handle_command(const EncodedString & command) { CommandParser cp; @@ -519,6 +532,15 @@ void Window::handle_command(const EncodedString & command) { command_quit(cp); } + else if (is_number(cp[0])) + { + size_t target_line = (size_t)atoi((const char *)&cp[0][0]); + if (target_line != 0u) + { + target_line--; + } + m_focused_buffer_pane->cursor_move_to_line(target_line); + } } } else