Editor::insert takes an InsertMode param, remove Editor::{append,replace}
This commit is contained in:
parent
7a8366da2b
commit
d574ae864d
|
@ -29,68 +29,38 @@ void Editor::erase()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<bool append>
|
void Editor::insert(const String& string, InsertMode mode)
|
||||||
static void do_insert(Editor& editor, const String& string)
|
|
||||||
{
|
{
|
||||||
scoped_edition edition(editor);
|
scoped_edition edition(*this);
|
||||||
for (auto& sel : editor.selections())
|
if (mode == InsertMode::Replace)
|
||||||
|
erase();
|
||||||
|
|
||||||
|
for (auto& sel : m_selections)
|
||||||
{
|
{
|
||||||
BufferIterator pos = append ? sel.end()
|
BufferIterator pos = (mode == InsertMode::Append) ?
|
||||||
: sel.begin();
|
sel.end() : sel.begin();
|
||||||
editor.buffer().insert(pos, string);
|
m_buffer.insert(pos, string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<bool append>
|
void Editor::insert(const memoryview<String>& strings, InsertMode mode)
|
||||||
static void do_insert(Editor& editor, const memoryview<String>& strings)
|
|
||||||
{
|
{
|
||||||
|
scoped_edition edition(*this);
|
||||||
|
if (mode == InsertMode::Replace)
|
||||||
|
erase();
|
||||||
|
|
||||||
if (strings.empty())
|
if (strings.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
scoped_edition edition(editor);
|
for (size_t i = 0; i < selections().size(); ++i)
|
||||||
for (size_t i = 0; i < editor.selections().size(); ++i)
|
|
||||||
{
|
{
|
||||||
BufferIterator pos = append ? editor.selections()[i].end()
|
BufferIterator pos = (mode == InsertMode::Append) ?
|
||||||
: editor.selections()[i].begin();
|
m_selections[i].end() : m_selections[i].begin();
|
||||||
size_t index = std::min(i, strings.size()-1);
|
size_t index = std::min(i, strings.size()-1);
|
||||||
editor.buffer().insert(pos, strings[index]);
|
m_buffer.insert(pos, strings[index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Editor::insert(const String& string)
|
|
||||||
{
|
|
||||||
do_insert<false>(*this, string);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Editor::insert(const memoryview<String>& strings)
|
|
||||||
{
|
|
||||||
do_insert<false>(*this, strings);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Editor::append(const String& string)
|
|
||||||
{
|
|
||||||
do_insert<true>(*this, string);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Editor::append(const memoryview<String>& strings)
|
|
||||||
{
|
|
||||||
do_insert<true>(*this, strings);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Editor::replace(const String& string)
|
|
||||||
{
|
|
||||||
scoped_edition edition(*this);
|
|
||||||
erase();
|
|
||||||
insert(string);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Editor::replace(const memoryview<String>& strings)
|
|
||||||
{
|
|
||||||
scoped_edition edition(*this);
|
|
||||||
erase();
|
|
||||||
insert(strings);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<String> Editor::selections_content() const
|
std::vector<String> Editor::selections_content() const
|
||||||
{
|
{
|
||||||
std::vector<String> contents;
|
std::vector<String> contents;
|
||||||
|
@ -305,7 +275,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
|
||||||
m_editor.on_incremental_insertion_begin();
|
m_editor.on_incremental_insertion_begin();
|
||||||
Buffer& buffer = editor.m_buffer;
|
Buffer& buffer = editor.m_buffer;
|
||||||
|
|
||||||
if (mode == InsertMode::Change)
|
if (mode == InsertMode::Replace)
|
||||||
{
|
{
|
||||||
for (auto& sel : editor.m_selections)
|
for (auto& sel : editor.m_selections)
|
||||||
buffer.erase(sel.begin(), sel.end());
|
buffer.erase(sel.begin(), sel.end());
|
||||||
|
@ -316,9 +286,9 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
|
||||||
utf8_it first, last;
|
utf8_it first, last;
|
||||||
switch (mode)
|
switch (mode)
|
||||||
{
|
{
|
||||||
case InsertMode::Insert: first = utf8_it(sel.end()) - 1; last = sel.begin(); break;
|
case InsertMode::Insert: first = utf8_it(sel.end()) - 1; last = sel.begin(); break;
|
||||||
case InsertMode::Change: 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;
|
case InsertMode::Append: first = sel.begin(); last = sel.end(); break;
|
||||||
|
|
||||||
case InsertMode::OpenLineBelow:
|
case InsertMode::OpenLineBelow:
|
||||||
case InsertMode::AppendAtLineEnd:
|
case InsertMode::AppendAtLineEnd:
|
||||||
|
|
|
@ -20,6 +20,17 @@ enum class SelectMode
|
||||||
Append,
|
Append,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class InsertMode : unsigned
|
||||||
|
{
|
||||||
|
Insert,
|
||||||
|
Append,
|
||||||
|
Replace,
|
||||||
|
InsertAtLineBegin,
|
||||||
|
AppendAtLineEnd,
|
||||||
|
OpenLineBelow,
|
||||||
|
OpenLineAbove
|
||||||
|
};
|
||||||
|
|
||||||
// An Editor is a buffer mutator
|
// An Editor is a buffer mutator
|
||||||
//
|
//
|
||||||
// The Editor class provides methods to manipulate a set of selections
|
// The Editor class provides methods to manipulate a set of selections
|
||||||
|
@ -38,14 +49,10 @@ public:
|
||||||
|
|
||||||
void erase();
|
void erase();
|
||||||
|
|
||||||
void insert(const String& string);
|
void insert(const String& string,
|
||||||
void insert(const memoryview<String>& strings);
|
InsertMode mode = InsertMode::Insert);
|
||||||
|
void insert(const memoryview<String>& strings,
|
||||||
void append(const String& string);
|
InsertMode mode = InsertMode::Insert);
|
||||||
void append(const memoryview<String>& strings);
|
|
||||||
|
|
||||||
void replace(const String& string);
|
|
||||||
void replace(const memoryview<String>& strings);
|
|
||||||
|
|
||||||
void move_selections(LineCount move,
|
void move_selections(LineCount move,
|
||||||
SelectMode mode = SelectMode::Replace);
|
SelectMode mode = SelectMode::Replace);
|
||||||
|
@ -103,17 +110,6 @@ private:
|
||||||
Editor& m_editor;
|
Editor& m_editor;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class InsertMode : unsigned
|
|
||||||
{
|
|
||||||
Insert,
|
|
||||||
Append,
|
|
||||||
Change,
|
|
||||||
InsertAtLineBegin,
|
|
||||||
AppendAtLineEnd,
|
|
||||||
OpenLineBelow,
|
|
||||||
OpenLineAbove
|
|
||||||
};
|
|
||||||
|
|
||||||
// An IncrementalInserter manage insert mode
|
// An IncrementalInserter manage insert mode
|
||||||
class IncrementalInserter
|
class IncrementalInserter
|
||||||
{
|
{
|
||||||
|
|
42
src/main.cc
42
src/main.cc
|
@ -93,7 +93,7 @@ void do_go(Context& context)
|
||||||
void do_replace_with_char(Context& context)
|
void do_replace_with_char(Context& context)
|
||||||
{
|
{
|
||||||
context.client().on_next_key([](const Key& key, Context& context) {
|
context.client().on_next_key([](const Key& key, Context& context) {
|
||||||
context.editor().replace(String() + key.key);
|
context.editor().insert(String() + key.key, InsertMode::Replace);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ void do_pipe(Context& context)
|
||||||
for (auto& sel : const_cast<const Editor&>(context.editor()).selections())
|
for (auto& sel : const_cast<const Editor&>(context.editor()).selections())
|
||||||
strings.push_back(ShellManager::instance().pipe(String(sel.begin(), sel.end()),
|
strings.push_back(ShellManager::instance().pipe(String(sel.begin(), sel.end()),
|
||||||
cmdline, context, {}, {}));
|
cmdline, context, {}, {}));
|
||||||
editor.replace(strings);
|
editor.insert(strings, InsertMode::Replace);
|
||||||
}, context);
|
}, context);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -159,40 +159,20 @@ void do_erase(Context& context)
|
||||||
void do_change(Context& context)
|
void do_change(Context& context)
|
||||||
{
|
{
|
||||||
RegisterManager::instance()['"'] = context.editor().selections_content();
|
RegisterManager::instance()['"'] = context.editor().selections_content();
|
||||||
do_insert<InsertMode::Change>(context);
|
do_insert<InsertMode::Replace>(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class PasteMode
|
// todo linewise paste
|
||||||
{
|
template<InsertMode insert_mode>
|
||||||
Before,
|
|
||||||
After,
|
|
||||||
Replace
|
|
||||||
};
|
|
||||||
|
|
||||||
template<PasteMode paste_mode>
|
|
||||||
void do_paste(Context& context)
|
void do_paste(Context& context)
|
||||||
{
|
{
|
||||||
Editor& editor = context.editor();
|
Editor& editor = context.editor();
|
||||||
int count = context.numeric_param();
|
int count = context.numeric_param();
|
||||||
Register& reg = RegisterManager::instance()['"'];
|
Register& reg = RegisterManager::instance()['"'];
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
{
|
editor.insert(reg.values(context), insert_mode);
|
||||||
if (paste_mode == PasteMode::Before)
|
|
||||||
editor.insert(reg.values(context));
|
|
||||||
else if (paste_mode == PasteMode::After)
|
|
||||||
editor.append(reg.values(context));
|
|
||||||
else if (paste_mode == PasteMode::Replace)
|
|
||||||
editor.replace(reg.values(context));
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
editor.insert(reg.values(context)[count-1], insert_mode);
|
||||||
if (paste_mode == PasteMode::Before)
|
|
||||||
editor.insert(reg.values(context)[count-1]);
|
|
||||||
else if (paste_mode == PasteMode::After)
|
|
||||||
editor.append(reg.values(context)[count-1]);
|
|
||||||
else if (paste_mode == PasteMode::Replace)
|
|
||||||
editor.replace(reg.values(context)[count-1]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_select_regex(Context& context)
|
void do_select_regex(Context& context)
|
||||||
|
@ -217,7 +197,7 @@ void do_join(Context& context)
|
||||||
editor.select(select_whole_lines);
|
editor.select(select_whole_lines);
|
||||||
editor.select(select_to_eol, SelectMode::Extend);
|
editor.select(select_to_eol, SelectMode::Extend);
|
||||||
editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*"));
|
editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*"));
|
||||||
editor.replace(" ");
|
editor.insert(" ", InsertMode::Replace);
|
||||||
editor.clear_selections();
|
editor.clear_selections();
|
||||||
editor.move_selections(-1_char);
|
editor.move_selections(-1_char);
|
||||||
}
|
}
|
||||||
|
@ -373,9 +353,9 @@ std::unordered_map<Key, std::function<void (Context& context)>> keymap =
|
||||||
{ { Key::Modifiers::None, 'G' }, do_go<SelectMode::Extend> },
|
{ { Key::Modifiers::None, 'G' }, do_go<SelectMode::Extend> },
|
||||||
|
|
||||||
{ { Key::Modifiers::None, 'y' }, do_yank },
|
{ { Key::Modifiers::None, 'y' }, do_yank },
|
||||||
{ { Key::Modifiers::None, 'p' }, do_paste<PasteMode::After> },
|
{ { Key::Modifiers::None, 'p' }, do_paste<InsertMode::Append> },
|
||||||
{ { Key::Modifiers::None, 'P' }, do_paste<PasteMode::Before> },
|
{ { Key::Modifiers::None, 'P' }, do_paste<InsertMode::Insert> },
|
||||||
{ { Key::Modifiers::Alt, 'p' }, do_paste<PasteMode::Replace> },
|
{ { Key::Modifiers::Alt, 'p' }, do_paste<InsertMode::Replace> },
|
||||||
|
|
||||||
{ { Key::Modifiers::None, 's' }, do_select_regex },
|
{ { Key::Modifiers::None, 's' }, do_select_regex },
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user