summaryrefslogtreecommitdiff
path: root/src/input_handler.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-05-14 20:56:27 +0100
committerMaxime Coste <frrrwww@gmail.com>2014-05-14 20:56:49 +0100
commit4e280977a2fcd7ca423b5909b435b896b256cdaf (patch)
treecaf34647ab26f749f5c63f5af71f34302bc5e261 /src/input_handler.cc
parentc3f4ef9ba2e8ca58acc6cf56e552341a5af62f5d (diff)
Iterate in reversed order on selections when modifing buffer
This way, update only needs to be called once everything is done as we always modify after the next selection to be used.
Diffstat (limited to 'src/input_handler.cc')
-rw-r--r--src/input_handler.cc28
1 files changed, 14 insertions, 14 deletions
diff --git a/src/input_handler.cc b/src/input_handler.cc
index 08cd7f44..459f6751 100644
--- a/src/input_handler.cc
+++ b/src/input_handler.cc
@@ -667,7 +667,7 @@ public:
}
else if (key == Key::Backspace)
{
- for (auto& sel : context().selections())
+ for (auto& sel : reversed(context().selections()))
{
if (sel.cursor() == ByteCoord{0,0})
continue;
@@ -677,7 +677,7 @@ public:
}
else if (key == Key::Erase)
{
- for (auto& sel : context().selections())
+ for (auto& sel : reversed(context().selections()))
{
auto pos = buffer.iterator_at(sel.cursor());
buffer.erase(pos, utf8::next(pos));
@@ -761,11 +761,12 @@ private:
auto& selections = context().selections();
for (size_t i = 0; i < selections.size(); ++i)
{
- size_t index = std::min(i, strings.size()-1);
- buffer.insert(buffer.iterator_at(selections[i].cursor()),
- strings[index]);
- selections.update();
+ size_t index = selections.size() - 1 - i;
+ const String& str = strings[std::min(index, strings.size()-1)];
+ buffer.insert(buffer.iterator_at(selections[index].cursor()),
+ str);
}
+ selections.update();
}
void insert(Codepoint key)
@@ -773,11 +774,9 @@ private:
auto str = codepoint_to_str(key);
auto& buffer = context().buffer();
auto& selections = context().selections();
- for (auto& sel : selections)
- {
+ for (auto& sel : reversed(selections))
buffer.insert(buffer.iterator_at(sel.cursor()), str);
- selections.update();
- }
+ selections.update();
context().hooks().run_hook("InsertChar", str, context());
}
@@ -786,7 +785,7 @@ private:
SelectionList& selections = context().selections();
Buffer& buffer = context().buffer();
- for (auto& sel : selections)
+ for (auto& sel : reversed(selections))
{
ByteCoord anchor, cursor;
switch (mode)
@@ -835,10 +834,10 @@ private:
if (buffer.is_end(cursor))
cursor = buffer.char_prev(cursor);
- selections.update();
sel.anchor() = anchor;
sel.cursor() = cursor;
}
+ selections.update();
if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove)
{
insert('\n');
@@ -859,12 +858,13 @@ private:
void on_disabled() override
{
- for (auto& sel : context().selections())
+ auto& selections = context().selections();
+ for (auto& sel : selections)
{
if (m_insert_mode == InsertMode::Append and sel.cursor().column > 0)
sel.cursor() = context().buffer().char_prev(sel.cursor());
- avoid_eol(context().buffer(), sel);
}
+ selections.avoid_eol();
}
enum class Mode { Default, Complete, InsertReg };