Jump to line number for :<n> commands

This commit is contained in:
Josh Holtrop 2018-03-19 23:20:32 -04:00
parent 7aa3c3b17f
commit 62868e596d
5 changed files with 70 additions and 0 deletions

View File

@ -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<Buffer::Iterator> 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;

View File

@ -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; }

View File

@ -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())

View File

@ -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();

View File

@ -504,6 +504,19 @@ void Window::change_focus(std::shared_ptr<BufferPane> 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