diff --git a/src/main.cc b/src/main.cc index ec526658..57aa4492 100644 --- a/src/main.cc +++ b/src/main.cc @@ -378,10 +378,10 @@ bool insert_char(Window& window, IncrementalInserter& inserter, const Key& key) inserter.insert(std::string() + '\n'); break; case 'd': - inserter.move_cursor({0, -1}); + inserter.move_cursors({0, -1}); break; case 'e': - inserter.move_cursor({0, 1}); + inserter.move_cursors({0, 1}); break; case 'g': inserter.erase(); @@ -859,7 +859,7 @@ void do_join(Window& window, int count) window.multi_select(std::bind(select_all_matches, _1, "\n\\h*")); window.replace(" "); window.clear_selections(); - window.move_cursor({0, -1}); + window.move_selections({0, -1}); } template @@ -888,15 +888,15 @@ void do_select_surrounding(Window& window, int count) std::unordered_map> keymap = { - { { Key::Modifiers::None, 'h' }, [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1))); } }, - { { Key::Modifiers::None, 'j' }, [](Window& window, int count) { window.move_cursor(DisplayCoord( std::max(count,1), 0)); } }, - { { Key::Modifiers::None, 'k' }, [](Window& window, int count) { window.move_cursor(DisplayCoord(-std::max(count,1), 0)); } }, - { { Key::Modifiers::None, 'l' }, [](Window& window, int count) { window.move_cursor(DisplayCoord(0, std::max(count,1))); } }, + { { Key::Modifiers::None, 'h' }, [](Window& window, int count) { window.move_selections(DisplayCoord(0, -std::max(count,1))); } }, + { { Key::Modifiers::None, 'j' }, [](Window& window, int count) { window.move_selections(DisplayCoord( std::max(count,1), 0)); } }, + { { Key::Modifiers::None, 'k' }, [](Window& window, int count) { window.move_selections(DisplayCoord(-std::max(count,1), 0)); } }, + { { Key::Modifiers::None, 'l' }, [](Window& window, int count) { window.move_selections(DisplayCoord(0, std::max(count,1))); } }, - { { Key::Modifiers::None, 'H' }, [](Window& window, int count) { window.move_cursor(DisplayCoord(0, -std::max(count,1)), true); } }, - { { Key::Modifiers::None, 'J' }, [](Window& window, int count) { window.move_cursor(DisplayCoord( std::max(count,1), 0), true); } }, - { { Key::Modifiers::None, 'K' }, [](Window& window, int count) { window.move_cursor(DisplayCoord(-std::max(count,1), 0), true); } }, - { { Key::Modifiers::None, 'L' }, [](Window& window, int count) { window.move_cursor(DisplayCoord(0, std::max(count,1)), true); } }, + { { Key::Modifiers::None, 'H' }, [](Window& window, int count) { window.move_selections(DisplayCoord(0, -std::max(count,1)), true); } }, + { { Key::Modifiers::None, 'J' }, [](Window& window, int count) { window.move_selections(DisplayCoord( std::max(count,1), 0), true); } }, + { { Key::Modifiers::None, 'K' }, [](Window& window, int count) { window.move_selections(DisplayCoord(-std::max(count,1), 0), true); } }, + { { Key::Modifiers::None, 'L' }, [](Window& window, int count) { window.move_selections(DisplayCoord(0, std::max(count,1)), true); } }, { { Key::Modifiers::None, 't' }, [](Window& window, int count) { window.select(std::bind(select_to, _1, getch(), count, false)); } }, { { Key::Modifiers::None, 'f' }, [](Window& window, int count) { window.select(std::bind(select_to, _1, getch(), count, true)); } }, diff --git a/src/window.cc b/src/window.cc index ff583ea3..2ab34423 100644 --- a/src/window.cc +++ b/src/window.cc @@ -258,16 +258,13 @@ void Window::select(const Selector& selector, bool append) if (not append) { - Selection sel = selector(selections().back().last()); - selections().clear(); - selections().push_back(std::move(sel)); + for (auto& sel : selections()) + sel = selector(sel.last()); } else { for (auto& sel : selections()) - { sel.merge_with(selector(sel.last())); - } } scroll_to_keep_cursor_visible_ifn(); } @@ -303,22 +300,15 @@ BufferString Window::selection_content() const selections().back().end()); } -void Window::move_cursor(const DisplayCoord& offset, bool append) +void Window::move_selections(const DisplayCoord& offset, bool append) { - if (not append) + for (auto& sel : selections()) { - BufferCoord pos = m_buffer.line_and_column_at(cursor_iterator()); - move_cursor_to(m_buffer.iterator_at(pos + BufferCoord(offset))); - } - else - { - for (auto& sel : selections()) - { - BufferCoord pos = m_buffer.line_and_column_at(sel.last()); - sel = Selection(sel.first(), m_buffer.iterator_at(pos + BufferCoord(offset))); - } - scroll_to_keep_cursor_visible_ifn(); + BufferCoord pos = m_buffer.line_and_column_at(sel.last()); + BufferIterator last = m_buffer.iterator_at(pos + BufferCoord(offset)); + sel = Selection(append ? sel.first() : last, last); } + scroll_to_keep_cursor_visible_ifn(); } void Window::move_cursor_to(const BufferIterator& iterator) @@ -461,7 +451,7 @@ IncrementalInserter::IncrementalInserter(Window& window, Mode mode) IncrementalInserter::~IncrementalInserter() { - move_cursor(DisplayCoord(0, -1)); + move_cursors(DisplayCoord(0, -1)); m_window.push_selections(); m_window.hooks_manager().run_hook("InsertEnd", "", Context(m_window)); @@ -505,7 +495,7 @@ void IncrementalInserter::erase() m_window.scroll_to_keep_cursor_visible_ifn(); } -void IncrementalInserter::move_cursor(const DisplayCoord& offset) +void IncrementalInserter::move_cursors(const DisplayCoord& offset) { for (auto& sel : m_window.selections()) { diff --git a/src/window.hh b/src/window.hh index 05fbc159..62873054 100644 --- a/src/window.hh +++ b/src/window.hh @@ -77,7 +77,7 @@ public: BufferIterator iterator_at(const DisplayCoord& window_pos) const; DisplayCoord line_and_column_at(const BufferIterator& iterator) const; - void move_cursor(const DisplayCoord& offset, bool append = false); + void move_selections(const DisplayCoord& offset, bool append = false); void move_cursor_to(const BufferIterator& iterator); void clear_selections(); @@ -167,7 +167,7 @@ public: void insert(const Window::String& string); void insert_capture(size_t index); void erase(); - void move_cursor(const DisplayCoord& offset); + void move_cursors(const DisplayCoord& offset); private: void apply(Modification&& modification) const;