2012-01-31 23:12:06 +04:00
|
|
|
#include "editor.hh"
|
|
|
|
|
|
|
|
#include "exception.hh"
|
|
|
|
#include "utils.hh"
|
2012-02-10 03:47:55 +04:00
|
|
|
#include "register.hh"
|
|
|
|
#include "register_manager.hh"
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-10-08 16:27:43 +04:00
|
|
|
#include "utf8_iterator.hh"
|
|
|
|
|
2012-02-10 17:58:29 +04:00
|
|
|
#include <array>
|
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
Editor::Editor(Buffer& buffer)
|
2012-11-05 02:39:25 +04:00
|
|
|
: m_buffer(&buffer),
|
2012-02-08 03:41:10 +04:00
|
|
|
m_edition_level(0)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
m_selections.push_back(Selection(buffer.begin(), buffer.begin()));
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::erase()
|
|
|
|
{
|
2012-02-08 03:41:10 +04:00
|
|
|
scoped_edition edition(*this);
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_selections)
|
2012-08-15 20:20:02 +04:00
|
|
|
{
|
2012-11-05 02:39:25 +04:00
|
|
|
m_buffer->erase(sel.begin(), sel.end());
|
2012-09-26 22:33:47 +04:00
|
|
|
sel.selection.avoid_eol();
|
2012-08-15 20:20:02 +04:00
|
|
|
}
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 21:36:03 +04:00
|
|
|
static BufferIterator prepare_insert(Buffer& buffer, const Selection& sel,
|
|
|
|
InsertMode mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case InsertMode::Insert:
|
|
|
|
case InsertMode::Replace:
|
|
|
|
return sel.begin();
|
|
|
|
case InsertMode::Append:
|
2012-11-26 22:23:50 +04:00
|
|
|
{
|
|
|
|
// 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 pos+1;
|
|
|
|
}
|
2012-10-09 21:36:03 +04:00
|
|
|
case InsertMode::InsertAtLineBegin:
|
|
|
|
return buffer.iterator_at_line_begin(sel.begin());
|
|
|
|
case InsertMode::AppendAtLineEnd:
|
2012-11-26 22:23:50 +04:00
|
|
|
return buffer.iterator_at_line_end(sel.end()-1)-1;
|
|
|
|
case InsertMode::InsertAtNextLineBegin:
|
2012-10-09 21:36:03 +04:00
|
|
|
return buffer.iterator_at_line_end(sel.end()-1);
|
|
|
|
case InsertMode::OpenLineBelow:
|
|
|
|
{
|
2012-11-26 22:24:38 +04:00
|
|
|
LineCount line = (sel.end() - 1).line();
|
|
|
|
buffer.insert(buffer.iterator_at_line_end(line), "\n");
|
|
|
|
return buffer.iterator_at_line_begin(line + 1);
|
2012-10-09 21:36:03 +04:00
|
|
|
}
|
|
|
|
case InsertMode::OpenLineAbove:
|
|
|
|
{
|
|
|
|
auto pos = buffer.iterator_at_line_begin(sel.begin());
|
|
|
|
buffer.insert(pos, "\n");
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
return BufferIterator{};
|
|
|
|
}
|
|
|
|
|
2012-10-09 21:25:20 +04:00
|
|
|
void Editor::insert(const String& string, InsertMode mode)
|
2012-02-10 03:47:55 +04:00
|
|
|
{
|
2012-10-09 21:25:20 +04:00
|
|
|
scoped_edition edition(*this);
|
|
|
|
if (mode == InsertMode::Replace)
|
2012-11-23 22:13:47 +04:00
|
|
|
{
|
|
|
|
// do not call Editor::erase as we do not want to avoid end of lines
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
m_buffer->erase(sel.begin(), sel.end());
|
|
|
|
}
|
2012-10-09 21:25:20 +04:00
|
|
|
|
|
|
|
for (auto& sel : m_selections)
|
2012-02-10 03:47:55 +04:00
|
|
|
{
|
2012-11-05 02:39:25 +04:00
|
|
|
BufferIterator pos = prepare_insert(*m_buffer, sel.selection, mode);
|
|
|
|
m_buffer->insert(pos, string);
|
2012-11-23 22:13:47 +04:00
|
|
|
sel.selection.avoid_eol();
|
2012-02-10 03:47:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 21:25:20 +04:00
|
|
|
void Editor::insert(const memoryview<String>& strings, InsertMode mode)
|
2012-02-10 03:47:55 +04:00
|
|
|
{
|
2012-10-09 21:25:20 +04:00
|
|
|
scoped_edition edition(*this);
|
|
|
|
if (mode == InsertMode::Replace)
|
2012-11-23 22:13:47 +04:00
|
|
|
{
|
|
|
|
// do not call Editor::erase as we do not want to avoid end of lines
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
m_buffer->erase(sel.begin(), sel.end());
|
|
|
|
}
|
2012-02-10 03:47:55 +04:00
|
|
|
if (strings.empty())
|
|
|
|
return;
|
|
|
|
|
2012-10-09 21:25:20 +04:00
|
|
|
for (size_t i = 0; i < selections().size(); ++i)
|
2012-02-10 03:47:55 +04:00
|
|
|
{
|
2012-11-23 22:13:47 +04:00
|
|
|
Selection& sel = m_selections[i].selection;
|
|
|
|
BufferIterator pos = prepare_insert(*m_buffer, sel, mode);
|
2012-02-10 03:47:55 +04:00
|
|
|
size_t index = std::min(i, strings.size()-1);
|
2012-11-05 02:39:25 +04:00
|
|
|
m_buffer->insert(pos, strings[index]);
|
2012-11-23 22:13:47 +04:00
|
|
|
sel.avoid_eol();
|
2012-02-10 03:47:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 01:23:29 +04:00
|
|
|
std::vector<String> Editor::selections_content() const
|
2012-02-10 03:47:55 +04:00
|
|
|
{
|
|
|
|
std::vector<String> contents;
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_selections)
|
2012-11-05 02:39:25 +04:00
|
|
|
contents.push_back(m_buffer->string(sel.begin(),
|
2012-09-26 22:33:47 +04:00
|
|
|
sel.end()));
|
2012-02-10 03:47:55 +04:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2012-10-10 21:11:24 +04:00
|
|
|
static void merge_overlapping(SelectionAndCapturesList& selections)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < selections.size(); ++i)
|
|
|
|
{
|
|
|
|
for (size_t j = i+1; j < selections.size();)
|
|
|
|
{
|
|
|
|
if (overlaps(selections[i], selections[j]))
|
|
|
|
{
|
|
|
|
selections[i].selection.merge_with(selections[j].selection);
|
|
|
|
selections.erase(selections.begin() + j);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-02 16:10:00 +04:00
|
|
|
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();
|
2012-10-11 02:41:48 +04:00
|
|
|
auto limit = offset < 0 ? buffer().iterator_at_line_begin(last)
|
|
|
|
: utf8::previous(buffer().iterator_at_line_end(last));
|
|
|
|
last = utf8::advance(last, limit, offset);
|
2012-10-02 16:10:00 +04:00
|
|
|
sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last);
|
2012-11-08 16:37:29 +04:00
|
|
|
sel.selection.avoid_eol();
|
2012-10-02 16:10:00 +04:00
|
|
|
}
|
2012-10-10 21:11:24 +04:00
|
|
|
merge_overlapping(m_selections);
|
2012-10-02 16:10:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::move_selections(LineCount offset, SelectMode mode)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-09-07 16:29:29 +04:00
|
|
|
assert(mode == SelectMode::Replace or mode == SelectMode::Extend);
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_selections)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-11-22 21:54:37 +04:00
|
|
|
BufferCoord pos = sel.last().coord();
|
2012-10-02 16:10:00 +04:00
|
|
|
pos.line += offset;
|
2012-11-05 02:39:25 +04:00
|
|
|
BufferIterator last = utf8::finish(m_buffer->iterator_at(pos, true));
|
2012-09-26 22:33:47 +04:00
|
|
|
sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last);
|
2012-11-08 16:37:29 +04:00
|
|
|
sel.selection.avoid_eol();
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
2012-10-10 21:11:24 +04:00
|
|
|
merge_overlapping(m_selections);
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::clear_selections()
|
|
|
|
{
|
|
|
|
check_invariant();
|
2012-09-24 22:47:06 +04:00
|
|
|
BufferIterator pos = m_selections.back().last();
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-10-08 16:27:43 +04:00
|
|
|
if (*pos == '\n' and not pos.is_begin() and *utf8::previous(pos) != '\n')
|
|
|
|
pos = utf8::previous(pos);
|
2012-01-31 23:12:06 +04:00
|
|
|
|
|
|
|
Selection sel = Selection(pos, pos);
|
2012-09-24 22:47:06 +04:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(std::move(sel));
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-11-19 22:03:56 +04:00
|
|
|
void Editor::flip_selections()
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
for (auto& sel : m_selections)
|
|
|
|
std::swap(sel.selection.first(), sel.selection.last());
|
|
|
|
}
|
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
void Editor::keep_selection(int index)
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
2012-09-24 22:47:06 +04:00
|
|
|
if (index < m_selections.size())
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-09-26 22:33:47 +04:00
|
|
|
SelectionAndCaptures sel = std::move(m_selections[index]);
|
2012-09-24 22:47:06 +04:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(std::move(sel));
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 15:15:43 +04:00
|
|
|
void Editor::remove_selection(int index)
|
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
2012-09-24 22:47:06 +04:00
|
|
|
if (m_selections.size() > 1 and index < m_selections.size())
|
|
|
|
m_selections.erase(m_selections.begin() + index);
|
2012-05-29 15:15:43 +04:00
|
|
|
}
|
|
|
|
|
2012-01-31 23:12:06 +04:00
|
|
|
void Editor::select(const BufferIterator& iterator)
|
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
m_selections.clear();
|
|
|
|
m_selections.push_back(Selection(iterator, iterator));
|
|
|
|
}
|
|
|
|
|
2012-09-26 22:33:47 +04:00
|
|
|
void Editor::select(SelectionAndCapturesList selections)
|
2012-09-24 22:47:06 +04:00
|
|
|
{
|
|
|
|
if (selections.empty())
|
|
|
|
throw runtime_error("no selections");
|
|
|
|
m_selections = std::move(selections);
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:29:29 +04:00
|
|
|
void Editor::select(const Selector& selector, SelectMode mode)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
|
|
|
check_invariant();
|
|
|
|
|
2012-10-10 15:57:52 +04:00
|
|
|
if (mode == SelectMode::Append)
|
|
|
|
{
|
|
|
|
auto& sel = m_selections.back();
|
|
|
|
SelectionAndCaptures res = selector(sel.selection);
|
|
|
|
if (res.captures.empty())
|
|
|
|
res.captures = sel.captures;
|
|
|
|
m_selections.push_back(res);
|
|
|
|
}
|
2012-10-10 21:11:24 +04:00
|
|
|
else
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-10-10 21:11:24 +04:00
|
|
|
for (auto& sel : m_selections)
|
|
|
|
{
|
|
|
|
SelectionAndCaptures res = selector(sel.selection);
|
|
|
|
if (mode == SelectMode::Extend)
|
|
|
|
sel.selection.merge_with(res.selection);
|
|
|
|
else
|
|
|
|
sel.selection = std::move(res.selection);
|
2012-02-10 03:47:55 +04:00
|
|
|
|
2012-10-10 21:11:24 +04:00
|
|
|
if (not res.captures.empty())
|
|
|
|
sel.captures = std::move(res.captures);
|
|
|
|
}
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
2012-10-10 21:11:24 +04:00
|
|
|
merge_overlapping(m_selections);
|
2012-01-31 23:12:06 +04: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 22:33:47 +04:00
|
|
|
SelectionAndCapturesList new_selections;
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_selections)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-09-26 22:33:47 +04:00
|
|
|
SelectionAndCapturesList res = selector(sel.selection);
|
2012-02-10 03:47:55 +04:00
|
|
|
for (auto& sel_and_cap : res)
|
|
|
|
{
|
2012-09-26 22:43:42 +04: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 03:47:55 +04:00
|
|
|
}
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
if (new_selections.empty())
|
|
|
|
throw nothing_selected();
|
|
|
|
|
2012-10-10 21:11:24 +04:00
|
|
|
merge_overlapping(new_selections);
|
2012-09-24 22:47:06 +04:00
|
|
|
m_selections = std::move(new_selections);
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-07-16 23:53:00 +04: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;
|
2012-10-08 16:27:43 +04:00
|
|
|
m_last = utf8::previous(end);
|
2012-07-16 23:53:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_erase(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
|
|
|
m_first = begin;
|
|
|
|
if (m_first >= m_buffer.end())
|
2012-10-08 16:27:43 +04:00
|
|
|
m_first = utf8::previous(m_buffer.end());
|
2012-07-16 23:53:00 +04:00
|
|
|
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 23:12:06 +04:00
|
|
|
bool Editor::undo()
|
|
|
|
{
|
2012-07-16 23:53:00 +04:00
|
|
|
LastModifiedRangeListener listener(buffer());
|
2012-11-05 02:39:25 +04:00
|
|
|
bool res = m_buffer->undo();
|
2012-07-16 23:53:00 +04:00
|
|
|
if (res)
|
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
m_selections.clear();
|
2012-11-26 21:50:34 +04:00
|
|
|
m_selections.emplace_back(listener.first(), listener.last());
|
2012-07-16 23:53:00 +04:00
|
|
|
}
|
|
|
|
return res;
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Editor::redo()
|
|
|
|
{
|
2012-07-16 23:53:00 +04:00
|
|
|
LastModifiedRangeListener listener(buffer());
|
2012-11-05 02:39:25 +04:00
|
|
|
bool res = m_buffer->redo();
|
2012-07-16 23:53:00 +04:00
|
|
|
if (res)
|
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
m_selections.clear();
|
2012-11-26 21:50:34 +04:00
|
|
|
m_selections.emplace_back(listener.first(), listener.last());
|
2012-07-16 23:53:00 +04:00
|
|
|
}
|
|
|
|
return res;
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Editor::check_invariant() const
|
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
assert(not m_selections.empty());
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-02-08 03:41:10 +04:00
|
|
|
void Editor::begin_edition()
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-02-08 03:41:10 +04:00
|
|
|
++m_edition_level;
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-02-08 03:41:10 +04:00
|
|
|
if (m_edition_level == 1)
|
2012-11-05 02:39:25 +04:00
|
|
|
m_buffer->begin_undo_group();
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-02-08 03:41:10 +04:00
|
|
|
void Editor::end_edition()
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-02-08 03:41:10 +04:00
|
|
|
assert(m_edition_level > 0);
|
|
|
|
if (m_edition_level == 1)
|
2012-11-05 02:39:25 +04:00
|
|
|
m_buffer->end_undo_group();
|
2012-02-08 03:41:10 +04:00
|
|
|
|
|
|
|
--m_edition_level;
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-10-13 20:31:29 +04:00
|
|
|
using utf8_it = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pass>;
|
2012-10-08 16:27:43 +04:00
|
|
|
|
2012-09-26 16:22:24 +04:00
|
|
|
IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
|
2012-07-16 23:21:56 +04:00
|
|
|
: m_editor(editor), m_edition(editor), m_mode(mode)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-02-08 03:41:10 +04:00
|
|
|
m_editor.on_incremental_insertion_begin();
|
2012-11-05 02:39:25 +04:00
|
|
|
Buffer& buffer = *editor.m_buffer;
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-10-09 21:25:20 +04:00
|
|
|
if (mode == InsertMode::Replace)
|
2012-08-27 16:26:16 +04:00
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : editor.m_selections)
|
2012-10-08 16:27:43 +04:00
|
|
|
buffer.erase(sel.begin(), sel.end());
|
2012-08-27 16:26:16 +04:00
|
|
|
}
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-10-08 16:27:43 +04:00
|
|
|
utf8_it first, last;
|
2012-01-31 23:12:06 +04:00
|
|
|
switch (mode)
|
|
|
|
{
|
2012-10-09 21:25:20 +04:00
|
|
|
case InsertMode::Insert: first = utf8_it(sel.end()) - 1; last = sel.begin(); break;
|
|
|
|
case InsertMode::Replace: first = utf8_it(sel.end()) - 1; last = sel.begin(); break;
|
|
|
|
case InsertMode::Append: first = sel.begin(); last = sel.end(); break;
|
2012-01-31 23:12:06 +04:00
|
|
|
|
2012-09-26 16:22:24 +04:00
|
|
|
case InsertMode::OpenLineBelow:
|
|
|
|
case InsertMode::AppendAtLineEnd:
|
2012-10-08 16:27:43 +04:00
|
|
|
first = utf8_it(buffer.iterator_at_line_end(utf8::previous(sel.end()))) - 1;
|
2012-07-16 23:21:56 +04:00
|
|
|
last = first;
|
2012-01-31 23:12:06 +04:00
|
|
|
break;
|
|
|
|
|
2012-09-26 16:22:24 +04:00
|
|
|
case InsertMode::OpenLineAbove:
|
|
|
|
case InsertMode::InsertAtLineBegin:
|
2012-10-08 16:27:43 +04:00
|
|
|
first = buffer.iterator_at_line_begin(sel.begin());
|
2012-09-26 16:22:24 +04:00
|
|
|
if (mode == InsertMode::OpenLineAbove)
|
2012-07-16 23:21:56 +04:00
|
|
|
--first;
|
2012-03-07 23:19:33 +04:00
|
|
|
else
|
|
|
|
{
|
2012-07-16 23:21:56 +04:00
|
|
|
auto first_non_blank = first;
|
2012-03-07 23:19:33 +04:00
|
|
|
while (*first_non_blank == ' ' or *first_non_blank == '\t')
|
|
|
|
++first_non_blank;
|
|
|
|
if (*first_non_blank != '\n')
|
2012-07-16 23:21:56 +04:00
|
|
|
first = first_non_blank;
|
2012-03-07 23:19:33 +04:00
|
|
|
}
|
2012-07-16 23:21:56 +04:00
|
|
|
last = first;
|
2012-01-31 23:12:06 +04:00
|
|
|
break;
|
|
|
|
}
|
2012-10-08 16:27:43 +04:00
|
|
|
if (first.underlying_iterator().is_end())
|
2012-09-10 21:26:17 +04:00
|
|
|
--first;
|
2012-10-08 16:27:43 +04:00
|
|
|
if (last.underlying_iterator().is_end())
|
2012-09-10 21:26:17 +04:00
|
|
|
--last;
|
2012-10-08 16:27:43 +04:00
|
|
|
sel.selection = Selection(first.underlying_iterator(), last.underlying_iterator());
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
2012-09-26 16:22:24 +04:00
|
|
|
if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove)
|
2012-09-04 01:46:04 +04:00
|
|
|
{
|
2012-08-10 21:12:43 +04:00
|
|
|
insert("\n");
|
2012-09-26 16:22:24 +04:00
|
|
|
if (mode == InsertMode::OpenLineAbove)
|
2012-09-04 01:46:04 +04:00
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-09-04 01:46:04 +04:00
|
|
|
{
|
|
|
|
// special case, the --first line above did nothing, so we need to compensate now
|
2012-10-08 16:27:43 +04:00
|
|
|
if (sel.first() == utf8::next(buffer.begin()))
|
|
|
|
sel.selection = Selection(buffer.begin(), buffer.begin());
|
2012-09-04 01:46:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalInserter::~IncrementalInserter()
|
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-08-27 16:26:16 +04:00
|
|
|
{
|
2012-09-26 16:22:24 +04:00
|
|
|
if (m_mode == InsertMode::Append)
|
2012-10-08 16:27:43 +04:00
|
|
|
sel = Selection(sel.first(), utf8::previous(sel.last()));
|
2012-09-26 22:33:47 +04:00
|
|
|
sel.selection.avoid_eol();
|
2012-08-27 16:26:16 +04:00
|
|
|
}
|
2012-07-16 23:21:56 +04:00
|
|
|
|
2012-02-08 03:41:10 +04:00
|
|
|
m_editor.on_incremental_insertion_end();
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-11-19 16:40:23 +04:00
|
|
|
void IncrementalInserter::insert(String content)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-08-10 21:12:43 +04:00
|
|
|
Buffer& buffer = m_editor.buffer();
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-08-10 21:12:43 +04:00
|
|
|
{
|
2012-11-19 16:40:23 +04:00
|
|
|
m_editor.filters()(buffer, sel.selection, content);
|
|
|
|
buffer.insert(sel.last(), content);
|
2012-08-10 21:12:43 +04:00
|
|
|
}
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
2012-06-29 20:35:48 +04:00
|
|
|
void IncrementalInserter::insert(const memoryview<String>& strings)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-11-12 16:40:07 +04:00
|
|
|
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]);
|
|
|
|
}
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalInserter::erase()
|
|
|
|
{
|
2012-09-24 22:47:06 +04:00
|
|
|
for (auto& sel : m_editor.m_selections)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-07-16 23:21:56 +04:00
|
|
|
BufferIterator pos = sel.last();
|
2012-10-08 16:27:43 +04:00
|
|
|
m_editor.buffer().erase(utf8::previous(pos), pos);
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 22:38:07 +04:00
|
|
|
void IncrementalInserter::move_cursors(CharCount move)
|
2012-01-31 23:12:06 +04:00
|
|
|
{
|
2012-11-26 22:38:07 +04:00
|
|
|
m_editor.move_selections(move, SelectMode::Replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalInserter::move_cursors(LineCount move)
|
|
|
|
{
|
|
|
|
m_editor.move_selections(move, SelectMode::Replace);
|
2012-01-31 23:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|