diff --git a/src/main.cc b/src/main.cc index 09bd3239..e5aa3b65 100644 --- a/src/main.cc +++ b/src/main.cc @@ -221,10 +221,10 @@ struct scoped_status } }; -void do_insert(Window& window, bool append = false) +void do_insert(Window& window, IncrementalInserter::Mode mode) { scoped_status("-- INSERT --"); - Kakoune::IncrementalInserter inserter(window, append); + Kakoune::IncrementalInserter inserter(window, mode); draw_window(window); while(true) { @@ -399,8 +399,7 @@ void do_erase(Window& window, int count) void do_change(Window& window, int count) { do_yank(window, 0); - window.erase(); - do_insert(window); + do_insert(window, IncrementalInserter::Mode::Change); } template @@ -424,8 +423,8 @@ std::unordered_map> keymap { 'd', do_erase }, { 'c', do_change }, - { 'i', [](Window& window, int count) { do_insert(window); } }, - { 'a', [](Window& window, int count) { do_insert(window, true); } }, + { 'i', [](Window& window, int count) { do_insert(window, IncrementalInserter::Mode::Insert); } }, + { 'a', [](Window& window, int count) { do_insert(window, IncrementalInserter::Mode::Append); } }, { 'g', do_go }, diff --git a/src/window.cc b/src/window.cc index 0631416f..720cfb73 100644 --- a/src/window.cc +++ b/src/window.cc @@ -270,19 +270,23 @@ void Window::scroll_to_keep_cursor_visible_ifn() } } -IncrementalInserter::IncrementalInserter(Window& window, bool append) +IncrementalInserter::IncrementalInserter(Window& window, Mode mode) : m_window(window) { assert(not m_window.m_current_inserter); m_window.m_current_inserter = this; m_window.check_invariant(); + m_window.m_buffer.begin_undo_group(); + + if (mode == Mode::Change) + window.erase_noundo(); + for (auto& sel : m_window.m_selections) { - const BufferIterator& pos = append ? sel.end() : sel.begin(); + const BufferIterator& pos = mode == Mode::Append ? sel.end() : sel.begin(); sel = Selection(pos, pos); } - m_window.m_buffer.begin_undo_group(); } IncrementalInserter::~IncrementalInserter() diff --git a/src/window.hh b/src/window.hh index e953cf60..db244db4 100644 --- a/src/window.hh +++ b/src/window.hh @@ -113,7 +113,14 @@ private: class IncrementalInserter { public: - IncrementalInserter(Window& window, bool append = false); + enum class Mode + { + Insert, + Append, + Change + }; + + IncrementalInserter(Window& window, Mode mode = Mode::Insert); ~IncrementalInserter(); void insert(const Window::String& string);