2012-01-31 20:12:06 +01:00
|
|
|
#include "editor.hh"
|
|
|
|
|
|
|
|
#include "exception.hh"
|
|
|
|
#include "utils.hh"
|
2012-02-10 00:47:55 +01:00
|
|
|
#include "register.hh"
|
|
|
|
#include "register_manager.hh"
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-02-10 14:58:29 +01:00
|
|
|
#include <array>
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
Editor::Editor(Buffer& buffer)
|
|
|
|
: m_buffer(buffer),
|
2012-02-08 00:41:10 +01:00
|
|
|
m_edition_level(0)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
m_selections.push_back(Selection(buffer.begin(), buffer.begin()));
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::erase()
|
|
|
|
{
|
2012-02-08 00:41:10 +01:00
|
|
|
scoped_edition edition(*this);
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_selections)
|
2012-08-15 18:20:02 +02:00
|
|
|
{
|
2012-08-10 19:12:43 +02:00
|
|
|
m_buffer.erase(sel.begin(), sel.end());
|
2012-09-26 20:33:47 +02:00
|
|
|
sel.selection.avoid_eol();
|
2012-08-15 18:20:02 +02:00
|
|
|
}
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-02-10 00:47:55 +01:00
|
|
|
template<bool append>
|
2012-03-08 22:23:29 +01:00
|
|
|
static void do_insert(Editor& editor, const String& string)
|
2012-02-10 00:47:55 +01:00
|
|
|
{
|
|
|
|
scoped_edition edition(editor);
|
|
|
|
for (auto& sel : editor.selections())
|
|
|
|
{
|
2012-09-26 20:33:47 +02:00
|
|
|
BufferIterator pos = append ? sel.end()
|
|
|
|
: sel.begin();
|
2012-08-10 19:12:43 +02:00
|
|
|
editor.buffer().insert(pos, string);
|
2012-02-10 00:47:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool append>
|
2012-03-08 22:23:29 +01:00
|
|
|
static void do_insert(Editor& editor, const memoryview<String>& strings)
|
2012-02-10 00:47:55 +01:00
|
|
|
{
|
|
|
|
if (strings.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_edition edition(editor);
|
|
|
|
for (size_t i = 0; i < editor.selections().size(); ++i)
|
|
|
|
{
|
|
|
|
BufferIterator pos = append ? editor.selections()[i].end()
|
|
|
|
: editor.selections()[i].begin();
|
|
|
|
size_t index = std::min(i, strings.size()-1);
|
2012-08-10 19:12:43 +02:00
|
|
|
editor.buffer().insert(pos, strings[index]);
|
2012-02-10 00:47:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
void Editor::insert(const String& string)
|
|
|
|
{
|
2012-02-10 00:47:55 +01:00
|
|
|
do_insert<false>(*this, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::insert(const memoryview<String>& strings)
|
|
|
|
{
|
|
|
|
do_insert<false>(*this, strings);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::append(const String& string)
|
|
|
|
{
|
2012-02-10 00:47:55 +01:00
|
|
|
do_insert<true>(*this, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::append(const memoryview<String>& strings)
|
|
|
|
{
|
|
|
|
do_insert<true>(*this, strings);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
void Editor::replace(const String& string)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-02-08 00:41:10 +01:00
|
|
|
scoped_edition edition(*this);
|
|
|
|
erase();
|
|
|
|
insert(string);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-07-11 14:15:27 +02:00
|
|
|
void Editor::replace(const memoryview<String>& strings)
|
|
|
|
{
|
|
|
|
scoped_edition edition(*this);
|
|
|
|
erase();
|
|
|
|
insert(strings);
|
|
|
|
}
|
|
|
|
|
2012-03-08 22:23:29 +01:00
|
|
|
std::vector<String> Editor::selections_content() const
|
2012-02-10 00:47:55 +01:00
|
|
|
{
|
|
|
|
std::vector<String> contents;
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_selections)
|
2012-09-26 20:33:47 +02:00
|
|
|
contents.push_back(m_buffer.string(sel.begin(),
|
|
|
|
sel.end()));
|
2012-02-10 00:47:55 +01:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2012-09-07 14:29:29 +02:00
|
|
|
void Editor::move_selections(const BufferCoord& offset, SelectMode mode)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-09-07 14:29:29 +02:00
|
|
|
assert(mode == SelectMode::Replace or mode == SelectMode::Extend);
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_selections)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
|
|
|
BufferCoord pos = m_buffer.line_and_column_at(sel.last());
|
2012-08-15 18:06:59 +02:00
|
|
|
BufferIterator last = m_buffer.iterator_at(pos + offset, true);
|
2012-09-26 20:33:47 +02:00
|
|
|
sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::clear_selections()
|
|
|
|
{
|
|
|
|
check_invariant();
|
2012-09-24 20:47:06 +02:00
|
|
|
BufferIterator pos = m_selections.back().last();
|
2012-01-31 20:12:06 +01:00
|
|
|
|
|
|
|
if (*pos == '\n' and not pos.is_begin() and *(pos-1) != '\n')
|
|
|
|
--pos;
|
|
|
|
|
|
|
|
Selection sel = Selection(pos, pos);
|
2012-09-24 20:47:06 +02:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(std::move(sel));
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::keep_selection(int index)
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
2012-09-24 20:47:06 +02:00
|
|
|
if (index < m_selections.size())
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-09-26 20:33:47 +02:00
|
|
|
SelectionAndCaptures sel = std::move(m_selections[index]);
|
2012-09-24 20:47:06 +02:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(std::move(sel));
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 13:15:43 +02:00
|
|
|
void Editor::remove_selection(int index)
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
2012-09-24 20:47:06 +02:00
|
|
|
if (m_selections.size() > 1 and index < m_selections.size())
|
|
|
|
m_selections.erase(m_selections.begin() + index);
|
2012-05-29 13:15:43 +02:00
|
|
|
}
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
void Editor::select(const BufferIterator& iterator)
|
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(Selection(iterator, iterator));
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:33:47 +02:00
|
|
|
void Editor::select(SelectionAndCapturesList selections)
|
2012-09-24 20:47:06 +02:00
|
|
|
{
|
|
|
|
if (selections.empty())
|
|
|
|
throw runtime_error("no selections");
|
|
|
|
m_selections = std::move(selections);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-09-07 14:29:29 +02:00
|
|
|
void Editor::select(const Selector& selector, SelectMode mode)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_selections)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-09-26 20:33:47 +02:00
|
|
|
SelectionAndCaptures res = selector(sel.selection);
|
2012-09-07 14:29:29 +02:00
|
|
|
if (mode == SelectMode::Extend)
|
2012-09-26 20:33:47 +02:00
|
|
|
sel.selection.merge_with(res.selection);
|
2012-02-10 00:47:55 +01:00
|
|
|
else
|
2012-09-26 20:33:47 +02:00
|
|
|
sel.selection = std::move(res.selection);
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2012-09-26 20:43:42 +02:00
|
|
|
if (not res.captures.empty())
|
|
|
|
sel.captures = std::move(res.captures);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nothing_selected : public runtime_error
|
|
|
|
{
|
|
|
|
nothing_selected() : runtime_error("nothing was selected") {}
|
|
|
|
};
|
|
|
|
|
|
|
|
void Editor::multi_select(const MultiSelector& selector)
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
2012-09-26 20:33:47 +02:00
|
|
|
SelectionAndCapturesList new_selections;
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_selections)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-09-26 20:33:47 +02:00
|
|
|
SelectionAndCapturesList res = selector(sel.selection);
|
2012-02-10 00:47:55 +01:00
|
|
|
for (auto& sel_and_cap : res)
|
|
|
|
{
|
2012-09-26 20:43:42 +02:00
|
|
|
// preserve captures when selectors captures nothing.
|
|
|
|
if (sel_and_cap.captures.empty())
|
|
|
|
new_selections.emplace_back(sel_and_cap.selection, sel.captures);
|
|
|
|
else
|
|
|
|
new_selections.push_back(std::move(sel_and_cap));
|
2012-02-10 00:47:55 +01:00
|
|
|
}
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
if (new_selections.empty())
|
|
|
|
throw nothing_selected();
|
|
|
|
|
2012-09-24 20:47:06 +02:00
|
|
|
m_selections = std::move(new_selections);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-07-16 21:53:00 +02:00
|
|
|
class LastModifiedRangeListener : public BufferChangeListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LastModifiedRangeListener(Buffer& buffer)
|
|
|
|
: m_buffer(buffer)
|
|
|
|
{ m_buffer.add_change_listener(*this); }
|
|
|
|
|
|
|
|
~LastModifiedRangeListener()
|
|
|
|
{ m_buffer.remove_change_listener(*this); }
|
|
|
|
|
|
|
|
void on_insert(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
|
|
|
m_first = begin;
|
|
|
|
m_last = end-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_erase(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
|
|
|
m_first = begin;
|
|
|
|
if (m_first >= m_buffer.end())
|
|
|
|
m_first = m_buffer.end() - 1;
|
|
|
|
m_last = m_first;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BufferIterator& first() const { return m_first; }
|
|
|
|
const BufferIterator& last() const { return m_last; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BufferIterator m_first;
|
|
|
|
BufferIterator m_last;
|
|
|
|
Buffer& m_buffer;
|
|
|
|
};
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
bool Editor::undo()
|
|
|
|
{
|
2012-07-16 21:53:00 +02:00
|
|
|
LastModifiedRangeListener listener(buffer());
|
|
|
|
bool res = m_buffer.undo();
|
|
|
|
if (res)
|
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(Selection(listener.first(),
|
2012-09-26 20:43:42 +02:00
|
|
|
listener.last()));
|
2012-07-16 21:53:00 +02:00
|
|
|
}
|
|
|
|
return res;
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Editor::redo()
|
|
|
|
{
|
2012-07-16 21:53:00 +02:00
|
|
|
LastModifiedRangeListener listener(buffer());
|
|
|
|
bool res = m_buffer.redo();
|
|
|
|
if (res)
|
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(Selection(listener.first(),
|
2012-09-26 20:43:42 +02:00
|
|
|
listener.last()));
|
2012-07-16 21:53:00 +02:00
|
|
|
}
|
|
|
|
return res;
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::check_invariant() const
|
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
assert(not m_selections.empty());
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
void Editor::begin_edition()
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-02-08 00:41:10 +01:00
|
|
|
++m_edition_level;
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
if (m_edition_level == 1)
|
2012-02-03 14:55:22 +01:00
|
|
|
m_buffer.begin_undo_group();
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
void Editor::end_edition()
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-02-08 00:41:10 +01:00
|
|
|
assert(m_edition_level > 0);
|
|
|
|
if (m_edition_level == 1)
|
2012-02-03 14:55:22 +01:00
|
|
|
m_buffer.end_undo_group();
|
2012-02-08 00:41:10 +01:00
|
|
|
|
|
|
|
--m_edition_level;
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-09-26 14:22:24 +02:00
|
|
|
IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
|
2012-07-16 21:21:56 +02:00
|
|
|
: m_editor(editor), m_edition(editor), m_mode(mode)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-02-08 00:41:10 +01:00
|
|
|
m_editor.on_incremental_insertion_begin();
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-09-26 14:22:24 +02:00
|
|
|
if (mode == InsertMode::Change)
|
2012-08-27 14:26:16 +02:00
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : editor.m_selections)
|
2012-08-27 14:26:16 +02:00
|
|
|
editor.m_buffer.erase(sel.begin(), sel.end());
|
|
|
|
}
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-07-16 21:21:56 +02:00
|
|
|
BufferIterator first, last;
|
2012-01-31 20:12:06 +01:00
|
|
|
switch (mode)
|
|
|
|
{
|
2012-09-26 14:22:24 +02:00
|
|
|
case InsertMode::Insert: first = sel.end()-1; last = sel.begin(); break;
|
|
|
|
case InsertMode::Change: first = sel.end()-1; last = sel.begin(); break;
|
|
|
|
case InsertMode::Append: first = sel.begin(); last = sel.end(); break;
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-09-26 14:22:24 +02:00
|
|
|
case InsertMode::OpenLineBelow:
|
|
|
|
case InsertMode::AppendAtLineEnd:
|
2012-07-16 21:21:56 +02:00
|
|
|
first = m_editor.m_buffer.iterator_at_line_end(sel.end() - 1) - 1;
|
|
|
|
last = first;
|
2012-01-31 20:12:06 +01:00
|
|
|
break;
|
|
|
|
|
2012-09-26 14:22:24 +02:00
|
|
|
case InsertMode::OpenLineAbove:
|
|
|
|
case InsertMode::InsertAtLineBegin:
|
2012-07-16 21:21:56 +02:00
|
|
|
first = m_editor.m_buffer.iterator_at_line_begin(sel.begin());
|
2012-09-26 14:22:24 +02:00
|
|
|
if (mode == InsertMode::OpenLineAbove)
|
2012-07-16 21:21:56 +02:00
|
|
|
--first;
|
2012-03-07 20:19:33 +01:00
|
|
|
else
|
|
|
|
{
|
2012-07-16 21:21:56 +02:00
|
|
|
auto first_non_blank = first;
|
2012-03-07 20:19:33 +01:00
|
|
|
while (*first_non_blank == ' ' or *first_non_blank == '\t')
|
|
|
|
++first_non_blank;
|
|
|
|
if (*first_non_blank != '\n')
|
2012-07-16 21:21:56 +02:00
|
|
|
first = first_non_blank;
|
2012-03-07 20:19:33 +01:00
|
|
|
}
|
2012-07-16 21:21:56 +02:00
|
|
|
last = first;
|
2012-01-31 20:12:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-09-10 19:26:17 +02:00
|
|
|
if (first.is_end())
|
|
|
|
--first;
|
|
|
|
if (last.is_end())
|
|
|
|
--last;
|
2012-09-26 20:43:42 +02:00
|
|
|
sel.selection = Selection(first, last);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
2012-09-26 14:22:24 +02:00
|
|
|
if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove)
|
2012-09-03 23:46:04 +02:00
|
|
|
{
|
2012-08-10 19:12:43 +02:00
|
|
|
insert("\n");
|
2012-09-26 14:22:24 +02:00
|
|
|
if (mode == InsertMode::OpenLineAbove)
|
2012-09-03 23:46:04 +02:00
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-09-03 23:46:04 +02:00
|
|
|
{
|
|
|
|
// special case, the --first line above did nothing, so we need to compensate now
|
|
|
|
if (sel.first() == buffer().begin() + 1)
|
2012-09-26 20:43:42 +02:00
|
|
|
sel.selection = Selection(buffer().begin(), buffer().begin());
|
2012-09-03 23:46:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalInserter::~IncrementalInserter()
|
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-08-27 14:26:16 +02:00
|
|
|
{
|
2012-09-26 14:22:24 +02:00
|
|
|
if (m_mode == InsertMode::Append)
|
2012-07-16 21:21:56 +02:00
|
|
|
sel = Selection(sel.first(), sel.last()-1);
|
2012-09-26 20:33:47 +02:00
|
|
|
sel.selection.avoid_eol();
|
2012-08-27 14:26:16 +02:00
|
|
|
}
|
2012-07-16 21:21:56 +02:00
|
|
|
|
2012-02-08 00:41:10 +01:00
|
|
|
m_editor.on_incremental_insertion_end();
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-03-08 22:23:29 +01:00
|
|
|
void IncrementalInserter::insert(const String& string)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-08-10 19:12:43 +02:00
|
|
|
Buffer& buffer = m_editor.buffer();
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-08-10 19:12:43 +02:00
|
|
|
{
|
|
|
|
BufferIterator position = sel.last();
|
|
|
|
String content = string;
|
|
|
|
m_editor.filters()(buffer, position, content);
|
|
|
|
m_editor.buffer().insert(position, content);
|
|
|
|
}
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:35:48 +02:00
|
|
|
void IncrementalInserter::insert(const memoryview<String>& strings)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-06-29 18:35:48 +02:00
|
|
|
m_editor.insert(strings);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalInserter::erase()
|
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2012-07-16 21:21:56 +02:00
|
|
|
BufferIterator pos = sel.last();
|
2012-08-10 19:12:43 +02:00
|
|
|
m_editor.buffer().erase(pos-1, pos);
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalInserter::move_cursors(const BufferCoord& offset)
|
|
|
|
{
|
2012-09-24 20:47:06 +02:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
|
|
|
BufferCoord pos = m_editor.m_buffer.line_and_column_at(sel.last());
|
|
|
|
BufferIterator it = m_editor.m_buffer.iterator_at(pos + offset);
|
|
|
|
sel = Selection(it, it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|