summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2012-04-03 13:22:07 +0000
committerMaxime Coste <frrrwww@gmail.com>2012-04-03 13:22:07 +0000
commit30bc1bad8feec8cfdc96aae5b57b6806076cb029 (patch)
tree45fe63407d3b039769da3bd273ac98d66850de61 /src
parentd1cc86d95daa1caa34ea83984453f6abb1afec2f (diff)
WindowHighlighterFactory class which passes the Window to the higlighter function
Diffstat (limited to 'src')
-rw-r--r--src/highlighters.cc197
1 files changed, 97 insertions, 100 deletions
diff --git a/src/highlighters.cc b/src/highlighters.cc
index ac15efa3..eef24701 100644
--- a/src/highlighters.cc
+++ b/src/highlighters.cc
@@ -98,7 +98,7 @@ HighlighterAndId colorize_regex_factory(Window& window,
std::string id = "colre'" + params[0] + "'";
return HighlighterAndId(id, std::bind(colorize_regex, std::placeholders::_1,
- ex, fg_color, bg_color));
+ ex, fg_color, bg_color));
}
void expand_tabulations(DisplayBuffer& display_buffer)
@@ -178,6 +178,93 @@ void show_line_numbers(DisplayBuffer& display_buffer)
}
}
+void highlight_selections(Window& window, DisplayBuffer& display_buffer)
+{
+ typedef std::pair<BufferIterator, BufferIterator> BufferRange;
+
+ std::vector<BufferRange> selections;
+ for (auto& sel : window.selections())
+ selections.push_back(BufferRange(sel.begin(), sel.end()));
+
+ std::sort(selections.begin(), selections.end(),
+ [](const BufferRange& lhs, const BufferRange& rhs)
+ { return lhs.first < rhs.first; });
+
+ auto atom_it = display_buffer.begin();
+ auto sel_it = selections.begin();
+
+ // underline each selections
+ while (atom_it != display_buffer.end()
+ and sel_it != selections.end())
+ {
+ BufferRange& sel = *sel_it;
+ DisplayAtom& atom = *atom_it;
+
+ if (atom.attribute() & Attributes::Final)
+ {
+ ++atom_it;
+ continue;
+ }
+ // [###------]
+ if (atom.begin() >= sel.first and atom.begin() < sel.second and
+ atom.end() > sel.second)
+ {
+ atom_it = display_buffer.split(atom_it, sel.second);
+ atom_it->attribute() |= Attributes::Underline;
+ ++atom_it;
+ ++sel_it;
+ }
+ // [---###---]
+ else if (atom.begin() < sel.first and atom.end() > sel.second)
+ {
+ atom_it = display_buffer.split(atom_it, sel.first);
+ atom_it = display_buffer.split(++atom_it, sel.second);
+ atom_it->attribute() |= Attributes::Underline;
+ ++atom_it;
+ ++sel_it;
+ }
+ // [------###]
+ else if (atom.begin() < sel.first and atom.end() > sel.first)
+ {
+ atom_it = ++display_buffer.split(atom_it, sel.first);
+ atom_it->attribute() |= Attributes::Underline;
+ ++atom_it;
+ }
+ // [#########]
+ else if (atom.begin() >= sel.first and atom.end() <= sel.second)
+ {
+ atom_it->attribute() |= Attributes::Underline;
+ ++atom_it;
+ }
+ // [---------]
+ else if (atom.begin() >= sel.second)
+ ++sel_it;
+ // [---------]
+ else if (atom.end() <= sel.first)
+ ++atom_it;
+ else
+ assert(false);
+ }
+
+ // invert selection last char
+ for (auto& sel : window.selections())
+ {
+ const BufferIterator& last = sel.last();
+
+ DisplayBuffer::iterator atom_it = display_buffer.atom_containing(last);
+ if (atom_it == display_buffer.end())
+ continue;
+
+ if (atom_it->begin() < last)
+ atom_it = ++display_buffer.split(atom_it, last);
+ if (atom_it->end() > last + 1)
+ atom_it = display_buffer.split(atom_it, last + 1);
+
+ atom_it->attribute() |= Attributes::Reverse;
+ }
+}
+
+
template<void (*highlighter_func)(DisplayBuffer&)>
class SimpleHighlighterFactory
{
@@ -193,110 +280,20 @@ private:
std::string m_id;
};
-class SelectionsHighlighter
+template<void (*highlighter_func)(Window&, DisplayBuffer&)>
+class WindowHighlighterFactory
{
public:
- SelectionsHighlighter(Window& window)
- : m_window(window)
- {
- }
-
- void operator()(DisplayBuffer& display_buffer)
- {
- typedef std::pair<BufferIterator, BufferIterator> BufferRange;
+ WindowHighlighterFactory(const std::string& id) : m_id(id) {}
- std::vector<BufferRange> selections;
- for (auto& sel : m_window.selections())
- selections.push_back(BufferRange(sel.begin(), sel.end()));
-
- std::sort(selections.begin(), selections.end(),
- [](const BufferRange& lhs, const BufferRange& rhs)
- { return lhs.first < rhs.first; });
-
- auto atom_it = display_buffer.begin();
- auto sel_it = selections.begin();
-
- // underline each selections
- while (atom_it != display_buffer.end()
- and sel_it != selections.end())
- {
- BufferRange& sel = *sel_it;
- DisplayAtom& atom = *atom_it;
-
- if (atom.attribute() & Attributes::Final)
- {
- ++atom_it;
- continue;
- }
- // [###------]
- if (atom.begin() >= sel.first and atom.begin() < sel.second and
- atom.end() > sel.second)
- {
- atom_it = display_buffer.split(atom_it, sel.second);
- atom_it->attribute() |= Attributes::Underline;
- ++atom_it;
- ++sel_it;
- }
- // [---###---]
- else if (atom.begin() < sel.first and atom.end() > sel.second)
- {
- atom_it = display_buffer.split(atom_it, sel.first);
- atom_it = display_buffer.split(++atom_it, sel.second);
- atom_it->attribute() |= Attributes::Underline;
- ++atom_it;
- ++sel_it;
- }
- // [------###]
- else if (atom.begin() < sel.first and atom.end() > sel.first)
- {
- atom_it = ++display_buffer.split(atom_it, sel.first);
- atom_it->attribute() |= Attributes::Underline;
- ++atom_it;
- }
- // [#########]
- else if (atom.begin() >= sel.first and atom.end() <= sel.second)
- {
- atom_it->attribute() |= Attributes::Underline;
- ++atom_it;
- }
- // [---------]
- else if (atom.begin() >= sel.second)
- ++sel_it;
- // [---------]
- else if (atom.end() <= sel.first)
- ++atom_it;
- else
- assert(false);
- }
-
- // invert selection last char
- for (auto& sel : m_window.selections())
- {
- const BufferIterator& last = sel.last();
-
- DisplayBuffer::iterator atom_it = display_buffer.atom_containing(last);
- if (atom_it == display_buffer.end())
- continue;
-
- if (atom_it->begin() < last)
- atom_it = ++display_buffer.split(atom_it, last);
- if (atom_it->end() > last + 1)
- atom_it = display_buffer.split(atom_it, last + 1);
-
- atom_it->attribute() |= Attributes::Reverse;
- }
-
- }
-
- static HighlighterAndId create(Window& window,
- const HighlighterParameters& params)
+ HighlighterAndId operator()(Window& window,
+ const HighlighterParameters& params) const
{
- return HighlighterAndId("highlight_selections",
- SelectionsHighlighter(window));
+ using namespace std::placeholders;
+ return HighlighterAndId(m_id, std::bind(highlighter_func, std::ref(window), _1));
}
-
private:
- const Window& m_window;
+ std::string m_id;
};
HighlighterAndId highlighter_group_factory(Window& window,
@@ -312,7 +309,7 @@ void register_highlighters()
{
HighlighterRegistry& registry = HighlighterRegistry::instance();
- registry.register_factory("highlight_selections", SelectionsHighlighter::create);
+ registry.register_factory("highlight_selections", WindowHighlighterFactory<highlight_selections>("highlight_selections"));
registry.register_factory("expand_tabs", SimpleHighlighterFactory<expand_tabulations>("expand_tabs"));
registry.register_factory("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines"));
registry.register_factory("regex", colorize_regex_factory);