diff --git a/src/filters.cc b/src/filters.cc new file mode 100644 index 00000000..acb546d3 --- /dev/null +++ b/src/filters.cc @@ -0,0 +1,28 @@ +#include "filters.hh" + +namespace Kakoune +{ + +void colorize_regex(DisplayBuffer& display_buffer, + const boost::regex& ex, Color color) +{ + for (auto atom_it = display_buffer.begin(); + atom_it != display_buffer.end(); ++atom_it) + { + boost::smatch matches; + if (boost::regex_search(atom_it->content, matches, ex, boost::match_nosubs)) + { + size_t pos = matches.begin()->first - atom_it->content.begin(); + if (pos != 0) + atom_it = display_buffer.split(atom_it, pos) + 1; + + pos = matches.begin()->second - matches.begin()->first; + if (pos != atom_it->content.length()) + atom_it = display_buffer.split(atom_it, pos); + + atom_it->fg_color = color; + } + } +} + +} diff --git a/src/filters.hh b/src/filters.hh new file mode 100644 index 00000000..2366ac9a --- /dev/null +++ b/src/filters.hh @@ -0,0 +1,15 @@ +#ifndef filters_hh_INCLUDED +#define filters_hh_INCLUDED + +#include +#include "display_buffer.hh" + +namespace Kakoune +{ + +void colorize_regex(DisplayBuffer& display_buffer, + const boost::regex& ex, Color color); + +} + +#endif // filters_hh_INCLUDED diff --git a/src/window.cc b/src/window.cc index 2ba97048..82088435 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1,6 +1,7 @@ #include "window.hh" #include "assert.hh" +#include "filters.hh" #include @@ -106,26 +107,6 @@ private: const Window& m_window; }; -static void blink_void(DisplayBuffer& display_buffer) -{ - for (auto atom_it = display_buffer.begin(); - atom_it != display_buffer.end();) - { - size_t pos = atom_it->content.find("void"); - if (pos != std::string::npos) - { - if (pos != 0) - atom_it = display_buffer.split(atom_it, pos) + 1; - - atom_it = display_buffer.split(atom_it, 4); - atom_it->attribute |= Attributes::Blink; - ++atom_it; - } - else - ++atom_it; - } -} - Window::Window(Buffer& buffer) : m_buffer(buffer), m_position(0, 0), @@ -134,7 +115,17 @@ Window::Window(Buffer& buffer) m_current_inserter(nullptr) { m_selections.push_back(Selection(buffer.begin(), buffer.begin())); - m_filters.push_back(blink_void); + m_filters.push_back(std::bind(colorize_regex, std::placeholders::_1, + boost::regex("\\<(void|int|float|size_t)\\>"), Color::Yellow)); + m_filters.push_back(std::bind(colorize_regex, std::placeholders::_1, + boost::regex("\\<(while|for|if|else|do|switch|case|default|goto|return|using|namespace|try|catch|throw|class|struct|enum|union)\\>"), Color::Blue)); + m_filters.push_back(std::bind(colorize_regex, std::placeholders::_1, + boost::regex("\\<(const|auto|static|volatile)\\>"), Color::Green)); + m_filters.push_back(std::bind(colorize_regex, std::placeholders::_1, + boost::regex("\\<(true|false|NULL|nullptr|\\d+[fdiu]?)\\>"), Color::Red)); + m_filters.push_back(std::bind(colorize_regex, std::placeholders::_1, + boost::regex("//.*$"), Color::Cyan)); + //m_filters.push_back(std::bind(colorize_regex, std::placeholders::_1, boost::regex("^\\h*.\\w+"), Color::Yellow)); m_filters.push_back(HighlightSelections(*this)); }