Editor::move_selection now either takes a LineCount or a CharCount

This commit is contained in:
Maxime Coste 2012-10-02 14:10:00 +02:00
parent 669d2e456f
commit 9ae8f5e47b
3 changed files with 28 additions and 12 deletions

View File

@ -98,13 +98,27 @@ std::vector<String> 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);
}
}

View File

@ -47,7 +47,9 @@ public:
void replace(const String& string);
void replace(const memoryview<String>& 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);

View File

@ -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<bool inner>
@ -340,15 +340,15 @@ String runtime_directory()
std::unordered_map<Key, std::function<void (Context& context)>> 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<SelectFlags::None> },
{ { Key::Modifiers::None, 'f' }, select_to_next_char<SelectFlags::Inclusive> },