From 6c2430ce08d8fb1ce9b266a7be1607cc1fd432fe Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 31 Mar 2013 13:52:15 +0200 Subject: [PATCH] FlagLines updates the lines in the option according to buffer modifications --- src/highlighter.hh | 2 +- src/highlighters.cc | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/highlighter.hh b/src/highlighter.hh index 185a2b9d..274ce182 100644 --- a/src/highlighter.hh +++ b/src/highlighter.hh @@ -25,7 +25,7 @@ typedef std::pair HighlighterAndId; typedef memoryview HighlighterParameters; using HighlighterFactory = std::function; + Window& window)>; using HighlighterGroup = FunctionGroup; diff --git a/src/highlighters.cc b/src/highlighters.cc index 2aee8bd6..6b55cbb2 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -331,11 +331,12 @@ void expand_unprintable(DisplayBuffer& display_buffer) } } -class FlagLines +class FlagLines : public BufferChangeListener_AutoRegister { public: - FlagLines(Color bg, String option_name, const OptionManager& options) - : m_bg(bg), m_option_name(std::move(option_name)), + FlagLines(Color bg, String option_name, OptionManager& options, const Buffer& buffer) + : BufferChangeListener_AutoRegister(buffer), + m_bg(bg), m_option_name(std::move(option_name)), m_options(options) { // trigger an exception if option is not of right type. @@ -360,18 +361,48 @@ public: } } + void on_insert(const BufferIterator& begin, const BufferIterator& end) override + { + LineCount new_lines = end.line() - begin.line(); + if (new_lines == 0) + return; + + auto lines = m_options[m_option_name].get>(); + for (auto& line : lines) + { + if (std::get<0>(line) > begin.line()) + std::get<0>(line) += new_lines; + } + m_options.get_local_option(m_option_name).set(lines); + } + + void on_erase(const BufferIterator& begin, const BufferIterator& end) override + { + LineCount removed_lines = end.line() - begin.line(); + if (removed_lines == 0) + return; + + auto lines = m_options[m_option_name].get>(); + for (auto& line : lines) + { + if (std::get<0>(line) > begin.line()) + std::get<0>(line) -= removed_lines; + } + m_options.get_local_option(m_option_name).set(lines); + } + private: Color m_bg; String m_option_name; - const OptionManager& m_options; + OptionManager& m_options; }; -HighlighterAndId flag_lines_factory(const HighlighterParameters& params, const Window& window) +HighlighterAndId flag_lines_factory(const HighlighterParameters& params, Window& window) { if (params.size() != 2) throw runtime_error("wrong parameter count"); - return {"hlflags_" + params[1], FlagLines{str_to_color(params[0]), params[1], window.options()}}; + return {"hlflags_" + params[1], FlagLines{str_to_color(params[0]), params[1], window.options(), window.buffer()}}; } template