Implement dd command to delete line

This commit is contained in:
Josh Holtrop 2017-11-24 12:33:11 -05:00
parent b0fadeab98
commit f091ee6590
3 changed files with 42 additions and 0 deletions

View File

@ -478,6 +478,41 @@ void BufferPane::redo()
m_window->request_redraw();
}
void BufferPane::delete_motion(const Command::Unit & motion)
{
std::shared_ptr<Buffer::Range> range = get_range_for_motion(motion);
if (range)
{
m_buffer->erase_range(*range);
m_buffer_view->update();
}
}
std::shared_ptr<Buffer::Range> BufferPane::get_range_for_motion(const Command::Unit & motion)
{
if (!m_iterator->valid())
{
return nullptr;
}
auto start = m_iterator->clonep();
auto end = m_iterator->clonep();
switch (motion.id)
{
case Command::Motion::THIS_LINE:
start->go_start_of_line();
end->go_end_of_line(true);
end->go_forward();
break;
default:
return nullptr;
}
return std::make_shared<Buffer::Range>(start, end);
}
void BufferPane::set_command_mode()
{
m_command_mode = true;

View File

@ -42,6 +42,7 @@ public:
void scroll_window_down(Window::ScrollMode scroll_mode);
void undo();
void redo();
void delete_motion(const Command::Unit & motion);
void set_show_status_bar(bool show_status_bar)
{
m_show_status_bar = show_status_bar;
@ -77,6 +78,7 @@ protected:
void draw_status_bar();
int calculate_lines_to_scroll(Window::ScrollMode scroll_mode);
void resize_buffer_view();
std::shared_ptr<Buffer::Range> get_range_for_motion(const Command::Unit & motion);
Window * m_window;
std::shared_ptr<Buffer> m_buffer;

View File

@ -338,6 +338,11 @@ void Window::execute_command(const Command & command)
case Command::DELETE_MOTION:
break;
case Command::DELETE_LINE:
{
Command::Unit motion = command.motion;
motion.id = Command::Motion::THIS_LINE;
m_focused_buffer_pane->delete_motion(motion);
}
break;
case Command::DELETE_CHAR:
m_focused_buffer_pane->kill_character_forward();