From 706c94301dd7c6f3609667964b2e5b4c58cb7fe4 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 12 Jun 2020 20:43:24 +0200 Subject: [PATCH] Yet another cursor history cleanup --- src/notebook.cpp | 85 ++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/src/notebook.cpp b/src/notebook.cpp index 299b890..7f2fa63 100644 --- a/src/notebook.cpp +++ b/src/notebook.cpp @@ -327,27 +327,62 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position }); // Cursor history - auto update_cursor_locations = [this, view]() { - auto iter = view->get_buffer()->get_insert()->get_iter(); - - bool moved = false; + auto add_cursor_location = [this, view](const Gtk::TextIter &iter) { if(current_cursor_location != cursor_locations.end()) { - if(current_cursor_location->view == view && abs(current_cursor_location->mark->get_iter().get_line() - iter.get_line()) <= 2) { - current_cursor_location->view->get_buffer()->move_mark(current_cursor_location->mark, iter); // Move current cursor - moved = true; + // Remove history newer than current (create new history branch) + for(auto it = std::next(current_cursor_location); it != cursor_locations.end();) { + it->view->get_buffer()->delete_mark(it->mark); + it = cursor_locations.erase(it); + } + } + current_cursor_location = cursor_locations.emplace(cursor_locations.end(), view, view->get_buffer()->create_mark(iter, false)); + if(cursor_locations.size() > 10) { + auto it = cursor_locations.begin(); + it->view->get_buffer()->delete_mark(it->mark); + cursor_locations.erase(it); + } + }; + view->get_buffer()->signal_mark_set().connect([this, view, add_cursor_location](const Gtk::TextBuffer::iterator & /*iter*/, const Glib::RefPtr &mark) { + if(mark->get_name() == "insert") { + if(disable_next_update_cursor_locations) { + disable_next_update_cursor_locations = false; + return; } - if(!moved) { - // Remove history newer than current (create new history branch) - for(auto it = std::next(current_cursor_location); it != cursor_locations.end();) { - it->view->get_buffer()->delete_mark(it->mark); - it = cursor_locations.erase(it); + auto iter = mark->get_iter(); + if(current_cursor_location != cursor_locations.end()) { + if(current_cursor_location->view == view && abs(current_cursor_location->mark->get_iter().get_line() - iter.get_line()) <= 2) { + current_cursor_location->view->get_buffer()->move_mark(current_cursor_location->mark, iter); // Move current cursor + + // Combine cursor histories adjacent and similar to current cursor + auto it = std::next(current_cursor_location); + if(it != cursor_locations.end()) { + if(it->view == current_cursor_location->view && abs(it->mark->get_iter().get_line() - current_cursor_location->mark->get_iter().get_line()) <= 2) { + it->view->get_buffer()->delete_mark(it->mark); + cursor_locations.erase(it); + } + } + if(current_cursor_location != cursor_locations.begin()) { + it = std::prev(current_cursor_location); + if(it->view == current_cursor_location->view && abs(it->mark->get_iter().get_line() - current_cursor_location->mark->get_iter().get_line()) <= 2) { + it->view->get_buffer()->delete_mark(it->mark); + cursor_locations.erase(it); + } + } + + return; } } + add_cursor_location(iter); } - - if(!moved) - current_cursor_location = cursor_locations.emplace(cursor_locations.end(), view, view->get_buffer()->create_mark(iter, false)); + }); + view->get_buffer()->signal_insert().connect([this, view, add_cursor_location](const Gtk::TextBuffer::iterator & /*iter*/, const Glib::ustring & /*text*/, int /*bytes*/) { + if(current_cursor_location == cursor_locations.end() || current_cursor_location->view != view) + add_cursor_location(view->get_buffer()->get_insert()->get_iter()); + }); + view->get_buffer()->signal_erase().connect([this, view, add_cursor_location](const Gtk::TextBuffer::iterator & /*start*/, const Gtk::TextBuffer::iterator & /*end*/) { + if(current_cursor_location == cursor_locations.end() || current_cursor_location->view != view) + add_cursor_location(view->get_buffer()->get_insert()->get_iter()); // Combine adjacent cursor histories that are similar if(cursor_locations.size() > 1) { @@ -371,26 +406,6 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position } } } - - if(!moved) { - if(cursor_locations.size() > 10) { - auto it = cursor_locations.begin(); - it->view->get_buffer()->delete_mark(it->mark); - cursor_locations.erase(it); - } - } - }; - view->get_buffer()->signal_mark_set().connect([this, update_cursor_locations](const Gtk::TextBuffer::iterator & /*iter*/, const Glib::RefPtr &mark) { - if(mark->get_name() == "insert") { - if(disable_next_update_cursor_locations) { - disable_next_update_cursor_locations = false; - return; - } - update_cursor_locations(); - } - }); - view->get_buffer()->signal_changed().connect([update_cursor_locations] { - update_cursor_locations(); }); #ifdef JUCI_ENABLE_DEBUG