From f091ee659010b131ea2c58155eba5605a1dac079 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 24 Nov 2017 12:33:11 -0500 Subject: [PATCH] Implement dd command to delete line --- src/gui/BufferPane.cc | 35 +++++++++++++++++++++++++++++++++++ src/gui/BufferPane.h | 2 ++ src/gui/Window.cc | 5 +++++ 3 files changed, 42 insertions(+) diff --git a/src/gui/BufferPane.cc b/src/gui/BufferPane.cc index b773712..87d1018 100644 --- a/src/gui/BufferPane.cc +++ b/src/gui/BufferPane.cc @@ -478,6 +478,41 @@ void BufferPane::redo() m_window->request_redraw(); } +void BufferPane::delete_motion(const Command::Unit & motion) +{ + std::shared_ptr range = get_range_for_motion(motion); + if (range) + { + m_buffer->erase_range(*range); + m_buffer_view->update(); + } +} + +std::shared_ptr 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(start, end); +} + void BufferPane::set_command_mode() { m_command_mode = true; diff --git a/src/gui/BufferPane.h b/src/gui/BufferPane.h index 0fa0b39..62f2f8f 100644 --- a/src/gui/BufferPane.h +++ b/src/gui/BufferPane.h @@ -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 get_range_for_motion(const Command::Unit & motion); Window * m_window; std::shared_ptr m_buffer; diff --git a/src/gui/Window.cc b/src/gui/Window.cc index bc9a526..1ef5b96 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -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();