Keep modified selections in non-draft exec/eval -itersel

Fixes #727
This commit is contained in:
Maxime Coste 2016-07-08 09:52:10 +01:00
parent fa9169fe97
commit 88a9607552
3 changed files with 35 additions and 16 deletions

View File

@ -1469,6 +1469,9 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
if (parser.get_switch("itersel"))
{
SelectionList sels{base_context->selections()};
Vector<Selection> new_sels;
size_t main = 0;
size_t timestamp = c.buffer().timestamp();
ScopedEdition edition{c};
for (auto& sel : sels)
{
@ -1479,12 +1482,20 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
if (&sels.buffer() != &c.buffer())
throw runtime_error("the buffer has changed while iterating on selections");
if (not draft)
{
update_selections(new_sels, main, c.buffer(), timestamp);
timestamp = c.buffer().timestamp();
for (auto& sel : c.selections())
new_sels.push_back(sel);
}
}
if (not draft)
{
sels.update();
c.selections_write_only() = std::move(sels);
c.selections_write_only() = SelectionList(c.buffer(), new_sels);
c.selections().sort_and_merge_overlapping();
}
}
else

View File

@ -337,12 +337,12 @@ Vector<Selection> compute_modified_ranges(Buffer& buffer, size_t timestamp)
return ranges;
}
void SelectionList::update()
void update_selections(Vector<Selection>& selections, size_t& main, Buffer& buffer, size_t timestamp)
{
if (m_timestamp == m_buffer->timestamp())
if (timestamp == buffer.timestamp())
return;
auto changes = m_buffer->changes_since(m_timestamp);
auto changes = buffer.changes_since(timestamp);
auto change_it = changes.begin();
while (change_it != changes.end())
{
@ -351,28 +351,33 @@ void SelectionList::update()
if (forward_end >= backward_end)
{
update_forward({ change_it, forward_end }, m_selections);
update_forward({ change_it, forward_end }, selections);
change_it = forward_end;
}
else
{
update_backward({ change_it, backward_end }, m_selections);
update_backward({ change_it, backward_end }, selections);
change_it = backward_end;
}
m_selections.erase(
merge_overlapping(m_selections.begin(), m_selections.end(),
m_main, overlaps), m_selections.end());
kak_assert(std::is_sorted(m_selections.begin(), m_selections.end(),
selections.erase(
merge_overlapping(selections.begin(), selections.end(),
main, overlaps), selections.end());
kak_assert(std::is_sorted(selections.begin(), selections.end(),
compare_selections));
}
for (auto& sel : m_selections)
for (auto& sel : selections)
{
sel.anchor() = m_buffer->clamp(sel.anchor());
sel.cursor() = m_buffer->clamp(sel.cursor());
sel.anchor() = buffer.clamp(sel.anchor());
sel.cursor() = buffer.clamp(sel.cursor());
}
m_selections.erase(merge_overlapping(begin(), end(), m_main, overlaps), end());
check_invariant();
selections.erase(merge_overlapping(selections.begin(), selections.end(),
main, overlaps), selections.end());
}
void SelectionList::update()
{
update_selections(m_selections, m_main, *m_buffer, m_timestamp);
check_invariant();
m_timestamp = m_buffer->timestamp();
}

View File

@ -55,6 +55,9 @@ inline bool overlaps(const Selection& lhs, const Selection& rhs)
: lhs.min() <= rhs.max();
}
void update_selections(Vector<Selection>& selections, size_t& main,
Buffer& buffer, size_t timestamp);
enum class InsertMode : unsigned
{
Insert,