From d574ae864d828fb743c9bebf90f968e26508204c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 9 Oct 2012 19:25:20 +0200 Subject: [PATCH] Editor::insert takes an InsertMode param, remove Editor::{append,replace} --- src/editor.cc | 74 +++++++++++++++------------------------------------ src/editor.hh | 34 +++++++++++------------ src/main.cc | 42 ++++++++--------------------- 3 files changed, 48 insertions(+), 102 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 34ab9c87..69ee81e9 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -29,68 +29,38 @@ void Editor::erase() } } -template -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 -static void do_insert(Editor& editor, const memoryview& strings) +void Editor::insert(const memoryview& 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(*this, string); -} - -void Editor::insert(const memoryview& strings) -{ - do_insert(*this, strings); -} - -void Editor::append(const String& string) -{ - do_insert(*this, string); -} - -void Editor::append(const memoryview& strings) -{ - do_insert(*this, strings); -} - -void Editor::replace(const String& string) -{ - scoped_edition edition(*this); - erase(); - insert(string); -} - -void Editor::replace(const memoryview& strings) -{ - scoped_edition edition(*this); - erase(); - insert(strings); -} - std::vector Editor::selections_content() const { std::vector 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: diff --git a/src/editor.hh b/src/editor.hh index dd9c98e7..1cf4979c 100644 --- a/src/editor.hh +++ b/src/editor.hh @@ -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& strings); - - void append(const String& string); - void append(const memoryview& strings); - - void replace(const String& string); - void replace(const memoryview& strings); + void insert(const String& string, + InsertMode mode = InsertMode::Insert); + void insert(const memoryview& 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 { diff --git a/src/main.cc b/src/main.cc index 3b27acaf..7978a289 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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(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(context); + do_insert(context); } -enum class PasteMode -{ - Before, - After, - Replace -}; - -template +// todo linewise paste +template 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> keymap = { { Key::Modifiers::None, 'G' }, do_go }, { { Key::Modifiers::None, 'y' }, do_yank }, - { { Key::Modifiers::None, 'p' }, do_paste }, - { { Key::Modifiers::None, 'P' }, do_paste }, - { { Key::Modifiers::Alt, 'p' }, do_paste }, + { { Key::Modifiers::None, 'p' }, do_paste }, + { { Key::Modifiers::None, 'P' }, do_paste }, + { { Key::Modifiers::Alt, 'p' }, do_paste }, { { Key::Modifiers::None, 's' }, do_select_regex },