kakoune/src/editor.cc

586 lines
17 KiB
C++
Raw Normal View History

#include "editor.hh"
#include "exception.hh"
#include "register.hh"
#include "register_manager.hh"
#include "utf8_iterator.hh"
2013-04-09 20:05:40 +02:00
#include "utils.hh"
#include <array>
namespace Kakoune
{
Editor::Editor(Buffer& buffer)
: m_buffer(&buffer),
m_edition_level(0),
m_selections(buffer)
{
2012-09-24 20:47:06 +02:00
m_selections.push_back(Selection(buffer.begin(), buffer.begin()));
m_main_sel = 0;
}
static void avoid_eol(const Buffer& buffer, BufferCoord& coord)
{
const auto column = coord.column;
if (column != 0 and column == buffer.line_length(coord.line) - 1)
coord = buffer.char_prev(coord);
}
static void avoid_eol(const Buffer& buffer, Range& sel)
{
avoid_eol(buffer, sel.first());
avoid_eol(buffer, sel.last());
}
void Editor::erase()
{
scoped_edition edition(*this);
2012-09-24 20:47:06 +02:00
for (auto& sel : m_selections)
{
Kakoune::erase(*m_buffer, sel);
avoid_eol(*m_buffer, sel);
}
}
static BufferCoord prepare_insert(Buffer& buffer, const Selection& sel,
InsertMode mode)
2012-10-09 19:36:03 +02:00
{
switch (mode)
{
case InsertMode::Insert:
return sel.min();
2013-01-23 18:58:43 +01:00
case InsertMode::Replace:
{
BufferCoord pos = sel.min();
Kakoune::erase(buffer, sel);
2013-01-23 18:58:43 +01:00
return pos;
}
2012-10-09 19:36:03 +02:00
case InsertMode::Append:
{
// special case for end of lines, append to current line instead
auto pos = std::max(sel.first(), sel.last());
if (pos.column == buffer.line_length(pos.line) - 1)
return pos;
else
return buffer.char_next(pos);
}
2012-10-09 19:36:03 +02:00
case InsertMode::InsertAtLineBegin:
return sel.min().line;
2012-10-09 19:36:03 +02:00
case InsertMode::AppendAtLineEnd:
return buffer.char_prev(sel.max().line+1);
case InsertMode::InsertAtNextLineBegin:
return sel.max().line+1;
2012-10-09 19:36:03 +02:00
case InsertMode::OpenLineBelow:
case InsertMode::OpenLineAbove:
{
auto line = mode == InsertMode::OpenLineAbove ?
sel.min().line : sel.max().line + 1;
buffer.insert(line, "\n");
return line;
2012-10-09 19:36:03 +02:00
}
2012-10-09 19:36:03 +02:00
}
kak_assert(false);
return {};
2012-10-09 19:36:03 +02:00
}
void Editor::insert(const String& str, InsertMode mode)
{
scoped_edition edition(*this);
for (auto& sel : m_selections)
{
BufferCoord pos = prepare_insert(*m_buffer, sel, mode);
m_buffer->insert(pos, str);
if (mode == InsertMode::Replace and not m_buffer->is_end(pos))
{
sel.first() = pos;
sel.last() = str.empty() ?
pos : m_buffer->char_advance(pos, str.char_length() - 1);
}
avoid_eol(*m_buffer, sel);
}
check_invariant();
}
void Editor::insert(const memoryview<String>& strings, InsertMode mode)
{
scoped_edition edition(*this);
if (strings.empty())
return;
for (size_t i = 0; i < selections().size(); ++i)
{
auto& sel = m_selections[i];
BufferCoord pos = prepare_insert(*m_buffer, sel, mode);
const String& str = strings[std::min(i, strings.size()-1)];
m_buffer->insert(pos, str);
if (mode == InsertMode::Replace and not m_buffer->is_end(pos))
{
sel.first() = pos;
sel.last() = str.empty() ?
pos : m_buffer->char_advance(pos, str.char_length() - 1);
}
avoid_eol(*m_buffer, sel);
}
check_invariant();
}
2012-03-08 22:23:29 +01:00
std::vector<String> Editor::selections_content() const
{
std::vector<String> contents;
2012-09-24 20:47:06 +02:00
for (auto& sel : m_selections)
contents.push_back(m_buffer->string(sel.min(), m_buffer->char_next(sel.max())));
return contents;
}
static bool compare_selections(const Selection& lhs, const Selection& rhs)
{
return lhs.min() < rhs.min();
}
template<typename OverlapsFunc>
void merge_overlapping(SelectionList& selections, size_t& main_selection,
OverlapsFunc overlaps)
{
kak_assert(std::is_sorted(selections.begin(), selections.end(), compare_selections));
for (size_t i = 0; i+1 < selections.size() and selections.size() > 1;)
{
if (overlaps(selections[i], selections[i+1]))
{
selections[i].merge_with(selections[i+1]);
selections.erase(selections.begin() + i + 1);
if (i + 1 <= main_selection)
--main_selection;
}
else
++i;
}
}
void sort_and_merge_overlapping(SelectionList& selections, size_t& main_selection)
{
if (selections.size() == 1)
return;
const auto& main = selections[main_selection];
const auto main_begin = main.min();
main_selection = std::count_if(selections.begin(), selections.end(),
[&](const Selection& sel) {
auto begin = sel.min();
if (begin == main_begin)
return &sel < &main;
else
return begin < main_begin;
});
std::stable_sort(selections.begin(), selections.end(), compare_selections);
merge_overlapping(selections, main_selection, overlaps);
}
void Editor::move_selections(CharCount offset, SelectMode mode)
{
kak_assert(mode == SelectMode::Replace or mode == SelectMode::Extend);
for (auto& sel : m_selections)
{
auto last = sel.last();
last = clamp<BufferCoord>(m_buffer->char_advance(last, offset),
last.line, m_buffer->char_prev(last.line+1));
sel.first() = mode == SelectMode::Extend ? sel.first() : last;
sel.last() = last;
avoid_eol(*m_buffer, sel);
}
sort_and_merge_overlapping(m_selections, m_main_sel);
}
void Editor::move_selections(LineCount offset, SelectMode mode)
{
kak_assert(mode == SelectMode::Replace or mode == SelectMode::Extend);
2012-09-24 20:47:06 +02:00
for (auto& sel : m_selections)
{
auto pos = sel.last();
CharCount column = m_buffer->char_distance(pos.line, pos);
pos.line += offset;
auto last = std::min(m_buffer->char_advance(pos.line, column),
m_buffer->char_prev(pos.line+1));
sel.first() = mode == SelectMode::Extend ? sel.first() : last;
sel.last() = last;
avoid_eol(*m_buffer, sel);
}
sort_and_merge_overlapping(m_selections, m_main_sel);
}
void Editor::clear_selections()
{
auto& sel = m_selections[m_main_sel];
2013-01-03 18:47:02 +01:00
auto& pos = sel.last();
if (pos.column != 0 and pos.column == m_buffer->line_length(pos.line) - 1)
pos = m_buffer->char_prev(pos);
2013-01-03 18:47:02 +01:00
sel.first() = pos;
m_selections.erase(m_selections.begin(), m_selections.begin() + m_main_sel);
m_selections.erase(m_selections.begin() + 1, m_selections.end());
m_main_sel = 0;
2012-12-31 18:36:28 +01:00
check_invariant();
}
void Editor::flip_selections()
{
for (auto& sel : m_selections)
std::swap(sel.first(), sel.last());
2012-12-31 18:36:28 +01:00
check_invariant();
}
void Editor::keep_selection(int index)
{
2012-09-24 20:47:06 +02:00
if (index < m_selections.size())
{
size_t real_index = (index + m_main_sel + 1) % m_selections.size();
m_selections = SelectionList{ std::move(m_selections[real_index]) };
m_main_sel = 0;
}
2012-12-31 18:36:28 +01:00
check_invariant();
}
void Editor::remove_selection(int index)
{
2012-09-24 20:47:06 +02:00
if (m_selections.size() > 1 and index < m_selections.size())
{
size_t real_index = (index + m_main_sel + 1) % m_selections.size();
m_selections.erase(m_selections.begin() + real_index);
if (real_index <= m_main_sel)
--m_main_sel;
}
2012-12-31 18:36:28 +01:00
check_invariant();
}
void Editor::select(const Selection& selection, SelectMode mode)
{
if (mode == SelectMode::Replace)
{
m_selections = SelectionList{ selection };
m_main_sel = 0;
}
else if (mode == SelectMode::Extend)
{
m_selections[m_main_sel].merge_with(selection);
m_selections = SelectionList{ std::move(m_selections[m_main_sel]) };
m_main_sel = 0;
}
else if (mode == SelectMode::Append)
{
m_main_sel = m_selections.size();
m_selections.push_back(selection);
sort_and_merge_overlapping(m_selections, m_main_sel);
}
2013-01-03 14:00:31 +01:00
else
kak_assert(false);
2012-12-31 18:36:28 +01:00
check_invariant();
2012-09-24 20:47:06 +02:00
}
void Editor::select(SelectionList selections)
2012-09-24 20:47:06 +02:00
{
if (selections.empty())
throw runtime_error("no selections");
m_selections = std::move(selections);
m_main_sel = m_selections.size() - 1;
2012-12-31 18:36:28 +01:00
check_invariant();
}
void Editor::select(const Selector& selector, SelectMode mode)
{
if (mode == SelectMode::Append)
{
auto& sel = m_selections[m_main_sel];
2013-06-01 14:22:57 +02:00
auto res = selector(*m_buffer, sel);
if (res.captures().empty())
res.captures() = sel.captures();
m_main_sel = m_selections.size();
m_selections.push_back(res);
}
else if (mode == SelectMode::ReplaceMain)
2013-01-03 14:00:31 +01:00
{
auto& sel = m_selections[m_main_sel];
2013-06-01 14:22:57 +02:00
auto res = selector(*m_buffer, sel);
2013-01-03 18:47:02 +01:00
sel.first() = res.first();
sel.last() = res.last();
if (not res.captures().empty())
sel.captures() = std::move(res.captures());
2013-01-03 14:00:31 +01:00
}
else
{
for (auto& sel : m_selections)
{
2013-06-01 14:22:57 +02:00
auto res = selector(*m_buffer, sel);
if (mode == SelectMode::Extend)
sel.merge_with(res);
else
2013-01-03 18:47:02 +01:00
{
sel.first() = res.first();
sel.last() = res.last();
}
if (not res.captures().empty())
sel.captures() = std::move(res.captures());
}
}
sort_and_merge_overlapping(m_selections, m_main_sel);
2012-12-31 18:36:28 +01:00
check_invariant();
}
struct nothing_selected : public runtime_error
{
nothing_selected() : runtime_error("nothing was selected") {}
};
void Editor::multi_select(const MultiSelector& selector)
{
SelectionList new_selections;
2012-09-24 20:47:06 +02:00
for (auto& sel : m_selections)
{
2013-06-01 14:22:57 +02:00
SelectionList res = selector(*m_buffer, sel);
new_selections.reserve(new_selections.size() + res.size());
for (auto& new_sel : res)
{
// preserve captures when selectors captures nothing.
if (new_sel.captures().empty())
new_selections.emplace_back(new_sel.first(), new_sel.last(),
sel.captures());
else
new_selections.push_back(std::move(new_sel));
}
}
if (new_selections.empty())
throw nothing_selected();
m_main_sel = new_selections.size() - 1;
sort_and_merge_overlapping(new_selections, m_main_sel);
m_selections = std::move(new_selections);
2012-12-31 18:36:28 +01:00
check_invariant();
}
class ModifiedRangesListener : public BufferChangeListener_AutoRegister
{
public:
ModifiedRangesListener(Buffer& buffer)
: BufferChangeListener_AutoRegister(buffer) {}
void on_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
{
m_ranges.update_insert(buffer, begin, end);
auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), begin,
[](const BufferCoord& c, const Selection& sel)
{ return c < sel.min(); });
m_ranges.emplace(it, registry().iterator_at(begin),
utf8::previous(registry().iterator_at(end)));
}
void on_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
{
m_ranges.update_erase(buffer, begin, end);
BufferIterator pos{registry(), begin};
if (pos >= buffer.end())
pos = utf8::previous(buffer.end());
auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), begin,
[](const BufferCoord& c, const Selection& sel)
{ return c < sel.min(); });
m_ranges.emplace(it, pos, pos);
}
SelectionList& ranges() { return m_ranges; }
private:
SelectionList m_ranges;
};
inline bool touches(const Buffer& buffer, const Range& lhs, const Range& rhs)
{
return lhs.min() <= rhs.min() ? buffer.char_next(lhs.max()) >= rhs.min()
: lhs.min() <= buffer.char_next(rhs.max());
}
bool Editor::undo()
{
using namespace std::placeholders;
ModifiedRangesListener listener(buffer());
bool res = m_buffer->undo();
if (res and not listener.ranges().empty())
{
m_selections = std::move(listener.ranges());
m_main_sel = m_selections.size() - 1;
merge_overlapping(m_selections, m_main_sel, std::bind(touches, std::ref(buffer()), _1, _2));
}
2012-12-31 18:36:28 +01:00
check_invariant();
return res;
}
bool Editor::redo()
{
using namespace std::placeholders;
ModifiedRangesListener listener(buffer());
bool res = m_buffer->redo();
if (res and not listener.ranges().empty())
{
m_selections = std::move(listener.ranges());
m_main_sel = m_selections.size() - 1;
merge_overlapping(m_selections, m_main_sel, std::bind(touches, std::ref(buffer()), _1, _2));
}
2012-12-31 18:36:28 +01:00
check_invariant();
return res;
}
void Editor::check_invariant() const
{
#ifdef KAK_DEBUG
kak_assert(not m_selections.empty());
kak_assert(m_main_sel < m_selections.size());
m_selections.check_invariant();
buffer().check_invariant();
kak_assert(std::is_sorted(m_selections.begin(), m_selections.end(), compare_selections));
#endif
}
void Editor::begin_edition()
{
++m_edition_level;
}
void Editor::end_edition()
{
kak_assert(m_edition_level > 0);
if (m_edition_level == 1)
m_buffer->commit_undo_group();
--m_edition_level;
}
using utf8_it = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pass>;
IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
: m_editor(editor), m_edition(editor), m_mode(mode)
{
Buffer& buffer = *editor.m_buffer;
2012-09-24 20:47:06 +02:00
for (auto& sel : m_editor.m_selections)
{
utf8_it first, last;
switch (mode)
{
case InsertMode::Insert: first = buffer.iterator_at(sel.max()); last = buffer.iterator_at(sel.min()); break;
2013-01-23 18:58:43 +01:00
case InsertMode::Replace:
{
Kakoune::erase(buffer, sel);
first = last = buffer.iterator_at(sel.min());
2013-01-23 18:58:43 +01:00
break;
}
case InsertMode::Append:
{
first = buffer.iterator_at(sel.min());
last = buffer.iterator_at(sel.max());
// special case for end of lines, append to current line instead
auto coord = last.underlying_iterator().coord();
if (coord.column != buffer.line_length(coord.line) - 1)
++last;
break;
}
case InsertMode::OpenLineBelow:
case InsertMode::AppendAtLineEnd:
first = utf8_it(buffer.iterator_at(sel.max().line+1)) - 1;
last = first;
break;
case InsertMode::OpenLineAbove:
case InsertMode::InsertAtLineBegin:
first = buffer.iterator_at(sel.min().line);
if (mode == InsertMode::OpenLineAbove)
--first;
else
{
auto first_non_blank = first;
while (*first_non_blank == ' ' or *first_non_blank == '\t')
++first_non_blank;
if (*first_non_blank != '\n')
first = first_non_blank;
}
last = first;
break;
2013-02-07 13:37:22 +01:00
case InsertMode::InsertAtNextLineBegin:
kak_assert(false); // not implemented
2013-02-07 13:37:22 +01:00
break;
}
if (first.underlying_iterator().is_end())
--first;
if (last.underlying_iterator().is_end())
--last;
2012-12-27 19:11:42 +01:00
sel.first() = first.underlying_iterator();
sel.last() = last.underlying_iterator();
}
if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove)
{
insert("\n");
if (mode == InsertMode::OpenLineAbove)
{
2012-09-24 20:47:06 +02:00
for (auto& sel : m_editor.m_selections)
{
// special case, the --first line above did nothing, so we need to compensate now
if (sel.first() == utf8::next(buffer.begin()))
2012-12-27 19:11:42 +01:00
sel.first() = sel.last() = buffer.begin();
}
}
}
sort_and_merge_overlapping(editor.m_selections, editor.m_main_sel);
2012-12-31 18:36:28 +01:00
editor.check_invariant();
}
IncrementalInserter::~IncrementalInserter()
{
2012-09-24 20:47:06 +02:00
for (auto& sel : m_editor.m_selections)
{
if (m_mode == InsertMode::Append and sel.last().column > 0)
sel.last() = m_editor.buffer().char_prev(sel.last());
avoid_eol(m_editor.buffer(), sel);
}
}
void IncrementalInserter::insert(String content)
{
Buffer& buffer = m_editor.buffer();
2012-09-24 20:47:06 +02:00
for (auto& sel : m_editor.m_selections)
{
m_editor.filters()(buffer, sel, content);
buffer.insert(sel.last(), content);
}
}
void IncrementalInserter::insert(const memoryview<String>& strings)
{
for (size_t i = 0; i < m_editor.m_selections.size(); ++i)
{
size_t index = std::min(i, strings.size()-1);
m_editor.buffer().insert(m_editor.m_selections[i].last(),
strings[index]);
}
}
void IncrementalInserter::erase()
{
2012-09-24 20:47:06 +02:00
for (auto& sel : m_editor.m_selections)
{
BufferCoord pos = sel.last();
m_editor.buffer().erase(m_editor.buffer().char_prev(pos), pos);
}
}
void IncrementalInserter::move_cursors(CharCount move)
{
m_editor.move_selections(move, SelectMode::Replace);
}
void IncrementalInserter::move_cursors(LineCount move)
{
m_editor.move_selections(move, SelectMode::Replace);
}
}