From 06dd3f8aaded9460d998a7f5c3ae0e2b46170d2f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 11 Jul 2012 14:15:27 +0200 Subject: [PATCH] Add Editor::replace(memoryview) and bind alt-P to replace paste --- src/editor.cc | 7 +++++++ src/editor.hh | 1 + src/main.cc | 30 +++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 2567d599..a174ead0 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -79,6 +79,13 @@ void Editor::replace(const String& string) insert(string); } +void Editor::replace(const memoryview& strings) +{ + scoped_edition edition(*this); + erase(); + insert(strings); +} + std::vector Editor::selections_content() const { std::vector contents; diff --git a/src/editor.hh b/src/editor.hh index 7cf7031a..48fb8ae3 100644 --- a/src/editor.hh +++ b/src/editor.hh @@ -37,6 +37,7 @@ public: void append(const memoryview& strings); void replace(const String& string); + void replace(const memoryview& strings); void push_selections(); void pop_selections(); diff --git a/src/main.cc b/src/main.cc index 39fdc25f..d1bb6359 100644 --- a/src/main.cc +++ b/src/main.cc @@ -246,23 +246,34 @@ void do_change(Editor& editor, int count) do_insert(editor, IncrementalInserter::Mode::Change); } -template +enum class PasteMode +{ + Before, + After, + Replace +}; + +template void do_paste(Editor& editor, int count) { Register& reg = RegisterManager::instance()['"']; if (count == 0) { - if (append) - editor.append(reg); - else + if (paste_mode == PasteMode::Before) editor.insert(reg); + else if (paste_mode == PasteMode::After) + editor.append(reg); + else if (paste_mode == PasteMode::Replace) + editor.replace(reg); } else { - if (append) - editor.append(reg[count-1]); - else + if (paste_mode == PasteMode::Before) editor.insert(reg[count-1]); + else if (paste_mode == PasteMode::After) + editor.append(reg[count-1]); + else if (paste_mode == PasteMode::Replace) + editor.replace(reg[count-1]); } } @@ -352,8 +363,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::None, 'p' }, do_paste }, + { { Key::Modifiers::None, 'P' }, do_paste }, + { { Key::Modifiers::Alt, 'p' }, do_paste }, { { Key::Modifiers::None, 's' }, do_select_regex },