diff --git a/src/display_buffer.cc b/src/display_buffer.cc index 4b4847cb1..5b8aaff33 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -17,4 +17,26 @@ DisplayLine::iterator DisplayLine::split(iterator it, BufferIterator pos) return m_atoms.insert(it, std::move(atom)); } +void DisplayBuffer::compute_range() +{ + m_range.first = BufferIterator(); + m_range.second = BufferIterator(); + for (auto& line : m_lines) + { + for (auto& atom : line) + { + if (not atom.content.has_buffer_range()) + continue; + + if (not m_range.first.is_valid() or m_range.first > atom.content.begin()) + m_range.first = atom.content.begin(); + + if (not m_range.second.is_valid() or m_range.second < atom.content.end()) + m_range.second = atom.content.end(); + } + } + assert(m_range.first.is_valid() and m_range.second.is_valid()); + assert(m_range.first <= m_range.second); +} + } diff --git a/src/display_buffer.hh b/src/display_buffer.hh index 685532e93..f0a2bc1ab 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -144,6 +144,8 @@ private: AtomList m_atoms; }; +using BufferRange = std::pair; + class DisplayBuffer { public: @@ -152,8 +154,15 @@ public: LineList& lines() { return m_lines; } const LineList& lines() const { return m_lines; } + + // returns the smallest BufferIterator range which contains every DisplayAtoms + const BufferRange& range() const { return m_range; } + void compute_range(); + private: LineList m_lines; + + BufferRange m_range; }; } diff --git a/src/highlighters.cc b/src/highlighters.cc index c34635f49..25cfbfe49 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -18,6 +18,9 @@ void highlight_range(DisplayBuffer& display_buffer, BufferIterator begin, BufferIterator end, bool skip_replaced, T func) { + if (end <= display_buffer.range().first or begin >= display_buffer.range().second) + return; + for (auto& line : display_buffer.lines()) { if (line.buffer_line() >= begin.line() and line.buffer_line() <= end.line()) @@ -50,11 +53,11 @@ void highlight_range(DisplayBuffer& display_buffer, } void colorize_regex(DisplayBuffer& display_buffer, - const Buffer& buffer, const Regex& ex, Color fg_color, Color bg_color = Color::Default) { - RegexIterator re_it(buffer.begin(), buffer.end(), ex, boost::match_nosubs); + const BufferRange& range = display_buffer.range(); + RegexIterator re_it(range.first, range.second, ex, boost::match_nosubs); RegexIterator re_end; for (; re_it != re_end; ++re_it) { @@ -94,8 +97,7 @@ HighlighterAndId colorize_regex_factory(Window& window, String id = "colre'" + params[0] + "'"; return HighlighterAndId(id, std::bind(colorize_regex, - _1, std::ref(window.buffer()), - ex, fg_color, bg_color)); + _1, ex, fg_color, bg_color)); } void expand_tabulations(Window& window, DisplayBuffer& display_buffer) diff --git a/src/window.cc b/src/window.cc index c2cf356c9..9959e31ab 100644 --- a/src/window.cc +++ b/src/window.cc @@ -61,6 +61,7 @@ void Window::update_display_buffer() lines.back().push_back(DisplayAtom(AtomContent(pos,end))); } + m_display_buffer.compute_range(); m_highlighters(m_display_buffer); }