From 9ae8f5e47bf95375c1c864ef6964681a1e182833 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 2 Oct 2012 14:10:00 +0200 Subject: [PATCH] Editor::move_selection now either takes a LineCount or a CharCount --- src/editor.cc | 18 ++++++++++++++++-- src/editor.hh | 4 +++- src/main.cc | 18 +++++++++--------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 439cc9d3..14f71aa2 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -98,13 +98,27 @@ std::vector Editor::selections_content() const return contents; } -void Editor::move_selections(const BufferCoord& offset, SelectMode mode) +void Editor::move_selections(CharCount offset, SelectMode mode) +{ + assert(mode == SelectMode::Replace or mode == SelectMode::Extend); + for (auto& sel : m_selections) + { + auto last = sel.last(); + last = clamp(last + offset, + buffer().iterator_at_line_begin(last), + buffer().iterator_at_line_end(last)-1); + sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last); + } +} + +void Editor::move_selections(LineCount offset, SelectMode mode) { assert(mode == SelectMode::Replace or mode == SelectMode::Extend); for (auto& sel : m_selections) { BufferCoord pos = m_buffer.line_and_column_at(sel.last()); - BufferIterator last = m_buffer.iterator_at(pos + offset, true); + pos.line += offset; + BufferIterator last = m_buffer.iterator_at(pos, true); sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last); } } diff --git a/src/editor.hh b/src/editor.hh index 9d3732da..dd9c98e7 100644 --- a/src/editor.hh +++ b/src/editor.hh @@ -47,7 +47,9 @@ public: void replace(const String& string); void replace(const memoryview& strings); - void move_selections(const BufferCoord& offset, + void move_selections(LineCount move, + SelectMode mode = SelectMode::Replace); + void move_selections(CharCount move, SelectMode mode = SelectMode::Replace); void clear_selections(); void keep_selection(int index); diff --git a/src/main.cc b/src/main.cc index 642800d6..3b27acaf 100644 --- a/src/main.cc +++ b/src/main.cc @@ -219,7 +219,7 @@ void do_join(Context& context) editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*")); editor.replace(" "); editor.clear_selections(); - editor.move_selections({0, -1}); + editor.move_selections(-1_char); } template @@ -340,15 +340,15 @@ String runtime_directory() std::unordered_map> keymap = { - { { Key::Modifiers::None, 'h' }, [](Context& context) { context.editor().move_selections({ 0, -std::max(context.numeric_param(),1) }); } }, - { { Key::Modifiers::None, 'j' }, [](Context& context) { context.editor().move_selections({ std::max(context.numeric_param(),1), 0 }); } }, - { { Key::Modifiers::None, 'k' }, [](Context& context) { context.editor().move_selections({ -std::max(context.numeric_param(),1), 0 }); } }, - { { Key::Modifiers::None, 'l' }, [](Context& context) { context.editor().move_selections({ 0, std::max(context.numeric_param(),1) }); } }, + { { Key::Modifiers::None, 'h' }, [](Context& context) { context.editor().move_selections(-CharCount(std::max(context.numeric_param(),1))); } }, + { { Key::Modifiers::None, 'j' }, [](Context& context) { context.editor().move_selections( LineCount(std::max(context.numeric_param(),1))); } }, + { { Key::Modifiers::None, 'k' }, [](Context& context) { context.editor().move_selections(-LineCount(std::max(context.numeric_param(),1))); } }, + { { Key::Modifiers::None, 'l' }, [](Context& context) { context.editor().move_selections( CharCount(std::max(context.numeric_param(),1))); } }, - { { Key::Modifiers::None, 'H' }, [](Context& context) { context.editor().move_selections({ 0, -std::max(context.numeric_param(),1) }, SelectMode::Extend); } }, - { { Key::Modifiers::None, 'J' }, [](Context& context) { context.editor().move_selections({ std::max(context.numeric_param(),1), 0 }, SelectMode::Extend); } }, - { { Key::Modifiers::None, 'K' }, [](Context& context) { context.editor().move_selections({ -std::max(context.numeric_param(),1), 0 }, SelectMode::Extend); } }, - { { Key::Modifiers::None, 'L' }, [](Context& context) { context.editor().move_selections({ 0, std::max(context.numeric_param(),1) }, SelectMode::Extend); } }, + { { Key::Modifiers::None, 'H' }, [](Context& context) { context.editor().move_selections(-CharCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, + { { Key::Modifiers::None, 'J' }, [](Context& context) { context.editor().move_selections( LineCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, + { { Key::Modifiers::None, 'K' }, [](Context& context) { context.editor().move_selections(-LineCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, + { { Key::Modifiers::None, 'L' }, [](Context& context) { context.editor().move_selections( CharCount(std::max(context.numeric_param(),1)), SelectMode::Extend); } }, { { Key::Modifiers::None, 't' }, select_to_next_char }, { { Key::Modifiers::None, 'f' }, select_to_next_char },