1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-28 09:07:19 +03:00

Editor::insert takes an InsertMode param, remove Editor::{append,replace}

This commit is contained in:
Maxime Coste 2012-10-09 19:25:20 +02:00
parent 7a8366da2b
commit d574ae864d
3 changed files with 48 additions and 102 deletions

View File

@ -29,68 +29,38 @@ void Editor::erase()
}
}
template<bool append>
static void do_insert(Editor& editor, const String& string)
void Editor::insert(const String& string, InsertMode mode)
{
scoped_edition edition(editor);
for (auto& sel : editor.selections())
scoped_edition edition(*this);
if (mode == InsertMode::Replace)
erase();
for (auto& sel : m_selections)
{
BufferIterator pos = append ? sel.end()
: sel.begin();
editor.buffer().insert(pos, string);
BufferIterator pos = (mode == InsertMode::Append) ?
sel.end() : sel.begin();
m_buffer.insert(pos, string);
}
}
template<bool append>
static void do_insert(Editor& editor, const memoryview<String>& strings)
void Editor::insert(const memoryview<String>& strings, InsertMode mode)
{
scoped_edition edition(*this);
if (mode == InsertMode::Replace)
erase();
if (strings.empty())
return;
scoped_edition edition(editor);
for (size_t i = 0; i < editor.selections().size(); ++i)
for (size_t i = 0; i < selections().size(); ++i)
{
BufferIterator pos = append ? editor.selections()[i].end()
: editor.selections()[i].begin();
BufferIterator pos = (mode == InsertMode::Append) ?
m_selections[i].end() : m_selections[i].begin();
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> contents;
@ -305,7 +275,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
m_editor.on_incremental_insertion_begin();
Buffer& buffer = editor.m_buffer;
if (mode == InsertMode::Change)
if (mode == InsertMode::Replace)
{
for (auto& sel : editor.m_selections)
buffer.erase(sel.begin(), sel.end());
@ -316,9 +286,9 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
utf8_it first, last;
switch (mode)
{
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::Append: first = sel.begin(); last = sel.end(); break;
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;
case InsertMode::OpenLineBelow:
case InsertMode::AppendAtLineEnd:

View File

@ -20,6 +20,17 @@ enum class SelectMode
Append,
};
enum class InsertMode : unsigned
{
Insert,
Append,
Replace,
InsertAtLineBegin,
AppendAtLineEnd,
OpenLineBelow,
OpenLineAbove
};
// An Editor is a buffer mutator
//
// The Editor class provides methods to manipulate a set of selections
@ -38,14 +49,10 @@ public:
void erase();
void insert(const String& string);
void insert(const memoryview<String>& strings);
void append(const String& string);
void append(const memoryview<String>& strings);
void replace(const String& string);
void replace(const memoryview<String>& strings);
void insert(const String& string,
InsertMode mode = InsertMode::Insert);
void insert(const memoryview<String>& strings,
InsertMode mode = InsertMode::Insert);
void move_selections(LineCount move,
SelectMode mode = SelectMode::Replace);
@ -103,17 +110,6 @@ private:
Editor& m_editor;
};
enum class InsertMode : unsigned
{
Insert,
Append,
Change,
InsertAtLineBegin,
AppendAtLineEnd,
OpenLineBelow,
OpenLineAbove
};
// An IncrementalInserter manage insert mode
class IncrementalInserter
{

View File

@ -93,7 +93,7 @@ void do_go(Context& context)
void do_replace_with_char(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())
strings.push_back(ShellManager::instance().pipe(String(sel.begin(), sel.end()),
cmdline, context, {}, {}));
editor.replace(strings);
editor.insert(strings, InsertMode::Replace);
}, context);
}
@ -159,40 +159,20 @@ void do_erase(Context& context)
void do_change(Context& context)
{
RegisterManager::instance()['"'] = context.editor().selections_content();
do_insert<InsertMode::Change>(context);
do_insert<InsertMode::Replace>(context);
}
enum class PasteMode
{
Before,
After,
Replace
};
template<PasteMode paste_mode>
// todo linewise paste
template<InsertMode insert_mode>
void do_paste(Context& context)
{
Editor& editor = context.editor();
int count = context.numeric_param();
Register& reg = RegisterManager::instance()['"'];
if (count == 0)
{
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));
}
editor.insert(reg.values(context), insert_mode);
else
{
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]);
}
editor.insert(reg.values(context)[count-1], insert_mode);
}
void do_select_regex(Context& context)
@ -217,7 +197,7 @@ void do_join(Context& context)
editor.select(select_whole_lines);
editor.select(select_to_eol, SelectMode::Extend);
editor.multi_select(std::bind(select_all_matches, _1, "\n\\h*"));
editor.replace(" ");
editor.insert(" ", InsertMode::Replace);
editor.clear_selections();
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, 'y' }, do_yank },
{ { Key::Modifiers::None, 'p' }, do_paste<PasteMode::After> },
{ { Key::Modifiers::None, 'P' }, do_paste<PasteMode::Before> },
{ { Key::Modifiers::Alt, 'p' }, do_paste<PasteMode::Replace> },
{ { Key::Modifiers::None, 'p' }, do_paste<InsertMode::Append> },
{ { Key::Modifiers::None, 'P' }, do_paste<InsertMode::Insert> },
{ { Key::Modifiers::Alt, 'p' }, do_paste<InsertMode::Replace> },
{ { Key::Modifiers::None, 's' }, do_select_regex },