From 0859b20bcfd47d28fbd1e4b5e1aa6873f7bf617f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 29 Nov 2011 22:37:20 +0000 Subject: Rename Filter to Highlighter to be more explicit --- src/filter.hh | 16 --- src/filter_registry.cc | 45 ------- src/filter_registry.hh | 40 ------ src/filters.cc | 305 -------------------------------------------- src/filters.hh | 11 -- src/highlighter.hh | 16 +++ src/highlighter_registry.cc | 45 +++++++ src/highlighter_registry.hh | 40 ++++++ src/highlighters.cc | 305 ++++++++++++++++++++++++++++++++++++++++++++ src/highlighters.hh | 11 ++ src/kakrc | 2 +- src/main.cc | 38 +++--- src/window.cc | 36 +++--- src/window.hh | 20 +-- 14 files changed, 465 insertions(+), 465 deletions(-) delete mode 100644 src/filter.hh delete mode 100644 src/filter_registry.cc delete mode 100644 src/filter_registry.hh delete mode 100644 src/filters.cc delete mode 100644 src/filters.hh create mode 100644 src/highlighter.hh create mode 100644 src/highlighter_registry.cc create mode 100644 src/highlighter_registry.hh create mode 100644 src/highlighters.cc create mode 100644 src/highlighters.hh (limited to 'src') diff --git a/src/filter.hh b/src/filter.hh deleted file mode 100644 index 3efa664e..00000000 --- a/src/filter.hh +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef filter_hh_INCLUDED -#define filter_hh_INCLUDED - -#include - -namespace Kakoune -{ - -class DisplayBuffer; - -typedef std::function FilterFunc; -typedef std::pair FilterAndId; - -} - -#endif // filter_hh_INCLUDED diff --git a/src/filter_registry.cc b/src/filter_registry.cc deleted file mode 100644 index a4f1d817..00000000 --- a/src/filter_registry.cc +++ /dev/null @@ -1,45 +0,0 @@ -#include "filter_registry.hh" - -#include "exception.hh" -#include "window.hh" - -namespace Kakoune -{ - -struct factory_not_found : public runtime_error -{ - factory_not_found() : runtime_error("factory not found") {} -}; - -void FilterRegistry::register_factory(const std::string& name, - const FilterFactory& factory) -{ - assert(m_factories.find(name) == m_factories.end()); - m_factories[name] = factory; -} - -void FilterRegistry::add_filter_to_window(Window& window, - const std::string& name, - const FilterParameters& parameters) -{ - auto it = m_factories.find(name); - if (it == m_factories.end()) - throw factory_not_found(); - - window.add_filter(it->second(window, parameters)); -} - -CandidateList FilterRegistry::complete_filter(const std::string& prefix, - size_t cursor_pos) -{ - std::string real_prefix = prefix.substr(0, cursor_pos); - CandidateList result; - for (auto& filter : m_factories) - { - if (filter.first.substr(0, real_prefix.length()) == real_prefix) - result.push_back(filter.first); - } - return result; -} - -} diff --git a/src/filter_registry.hh b/src/filter_registry.hh deleted file mode 100644 index 572e30af..00000000 --- a/src/filter_registry.hh +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef filter_registry_h_INCLUDED -#define filter_registry_h_INCLUDED - -#include -#include - -#include "filter.hh" -#include "utils.hh" -#include "completion.hh" - -namespace Kakoune -{ - -class Window; - -typedef std::vector FilterParameters; - -typedef std::function FilterFactory; - -class FilterRegistry : public Singleton -{ -public: - void register_factory(const std::string& name, - const FilterFactory& factory); - - void add_filter_to_window(Window& window, - const std::string& factory_name, - const FilterParameters& parameters); - - CandidateList complete_filter(const std::string& prefix, - size_t cursor_pos); - -private: - std::unordered_map m_factories; -}; - -} - -#endif // filter_registry_h_INCLUDED diff --git a/src/filters.cc b/src/filters.cc deleted file mode 100644 index 668a582a..00000000 --- a/src/filters.cc +++ /dev/null @@ -1,305 +0,0 @@ -#include "filters.hh" - -#include "assert.hh" -#include "window.hh" -#include "display_buffer.hh" -#include "filter_registry.hh" -#include - -namespace Kakoune -{ - -void colorize_regex_range(DisplayBuffer& display_buffer, - const BufferIterator& range_begin, - const BufferIterator& range_end, - const boost::regex& ex, - Color fg_color, Color bg_color = Color::Default) -{ - assert(range_begin <= range_end); - - if (range_begin >= display_buffer.back().end() or - range_end <= display_buffer.front().begin()) - return; - - BufferIterator display_begin = std::max(range_begin, - display_buffer.front().begin()); - BufferIterator display_end = std::min(range_end, - display_buffer.back().end()); - - boost::regex_iterator re_it(display_begin, display_end, - ex, boost::match_nosubs); - boost::regex_iterator re_end; - DisplayBuffer::iterator atom_it = display_buffer.begin(); - for (; re_it != re_end; ++re_it) - { - BufferIterator begin = (*re_it)[0].first; - BufferIterator end = (*re_it)[0].second; - assert(begin != end); - - auto begin_atom_it = display_buffer.atom_containing(begin, atom_it); - assert(begin_atom_it != display_buffer.end()); - if (begin_atom_it->begin() != begin) - begin_atom_it = ++display_buffer.split(begin_atom_it, begin); - - auto end_atom_it = display_buffer.atom_containing(end, begin_atom_it); - if (end_atom_it != display_buffer.end() and - end_atom_it->begin() != end) - end_atom_it = ++display_buffer.split(end_atom_it, end); - - assert(begin_atom_it != end_atom_it); - - for (auto it = begin_atom_it; it != end_atom_it; ++it) - { - it->fg_color() = fg_color; - it->bg_color() = bg_color; - } - - atom_it = end_atom_it; - } -} - -void colorize_regex(DisplayBuffer& display_buffer, - const boost::regex& ex, - Color fg_color, Color bg_color = Color::Default) -{ - colorize_regex_range(display_buffer, display_buffer.front().begin(), - display_buffer.back().end(), ex, fg_color, bg_color); -} - -Color parse_color(const std::string& color) -{ - if (color == "default") return Color::Default; - if (color == "black") return Color::Black; - if (color == "red") return Color::Red; - if (color == "green") return Color::Green; - if (color == "yellow") return Color::Yellow; - if (color == "blue") return Color::Blue; - if (color == "magenta") return Color::Magenta; - if (color == "cyan") return Color::Cyan; - if (color == "white") return Color::White; - return Color::Default; -} - -FilterAndId colorize_regex_factory(Window& window, - const FilterParameters params) -{ - if (params.size() != 3) - throw runtime_error("wrong parameter count"); - - boost::regex ex(params[0]); - - Color fg_color = parse_color(params[1]); - Color bg_color = parse_color(params[2]); - - std::string id = "colre'" + params[0] + "'"; - - return FilterAndId(id, std::bind(colorize_regex, std::placeholders::_1, - ex, fg_color, bg_color)); -} - -void colorize_cplusplus(DisplayBuffer& display_buffer) -{ - static boost::regex preprocessor("(\\`|(?<=\\n))\\h*#\\h*[^\\n]*"); - colorize_regex(display_buffer, preprocessor, Color::Magenta); - - static boost::regex comments("//[^\\n]*\\n"); - colorize_regex(display_buffer, comments, Color::Cyan); - - static boost::regex strings("(?|\\<-?\\d+[fdiu]?|'\\\\?[^']?'"); - colorize_regex(display_buffer, values, Color::Red); - - static boost::regex builtin_types("\\<(void|int|char|unsigned|float|bool|size_t)\\>"); - colorize_regex(display_buffer, builtin_types, Color::Yellow); - - static boost::regex control_keywords("\\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw)\\>"); - colorize_regex(display_buffer, control_keywords, Color::Blue); - - //static boost::regex operators("->|\\+|\\-|\\*|/|\\\\|\\&|\\|\\^|[<>=!+-]=|=|\\(|\\)|\\[|\\]|\\{|\\}|\\<(not|and|or|xor)\\>"); - //colorize_regex(display_buffer, operators, Color::Green); - - static boost::regex types_keywords("\\<(const|auto|namespace|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual)\\>"); - colorize_regex(display_buffer, types_keywords, Color::Green); -} - -void expand_tabulations(DisplayBuffer& display_buffer) -{ - const int tabstop = 8; - for (auto atom_it = display_buffer.begin(); - atom_it != display_buffer.end(); ++atom_it) - { - for (BufferIterator it = atom_it->begin(); it != atom_it->end(); ++it) - { - if (*it == '\t') - { - if (it != atom_it->begin()) - atom_it = ++display_buffer.split(atom_it, it); - - if (it+1 != atom_it->end()) - atom_it = display_buffer.split(atom_it, it+1); - - BufferCoord pos = it.buffer().line_and_column_at(it); - - int column = 0; - for (auto line_it = it.buffer().iterator_at({pos.line, 0}); - line_it != it; ++line_it) - { - assert(*line_it != '\n'); - if (*line_it == '\t') - column += tabstop - (column % tabstop); - else - ++column; - } - - int count = tabstop - (column % tabstop); - display_buffer.replace_atom_content(atom_it, - std::string(count, ' ')); - } - } - } -} - -void show_line_numbers(DisplayBuffer& display_buffer) -{ - const Buffer& buffer = display_buffer.front().begin().buffer(); - BufferCoord coord = buffer.line_and_column_at(display_buffer.begin()->begin()); - - int last_line = buffer.line_and_column_at(display_buffer.back().end()-1).line; - - for (; coord.line <= last_line; ++coord.line) - { - BufferIterator line_start = buffer.iterator_at(coord); - DisplayBuffer::iterator atom_it = display_buffer.atom_containing(line_start); - if (atom_it != display_buffer.end()) - { - if (atom_it->begin() != line_start) - { - if (not atom_it->splitable()) - continue; - - atom_it = ++display_buffer.split(atom_it, line_start); - } - atom_it = display_buffer.insert( - atom_it, - DisplayAtom(atom_it->coord(), - atom_it->begin(), atom_it->begin(), - Color::Black, Color::White)); - - char buffer[6]; - snprintf(buffer, 6, "%3d ", coord.line + 1); - display_buffer.replace_atom_content(atom_it, buffer); - } - } -} - -template -class SimpleFilterFactory -{ -public: - SimpleFilterFactory(const std::string& id) : m_id(id) {} - - FilterAndId operator()(Window& window, - const FilterParameters& params) const - { - return FilterAndId(m_id, FilterFunc(filter_func)); - } -private: - std::string m_id; -}; - -class SelectionsHighlighter -{ -public: - SelectionsHighlighter(Window& window) - : m_window(window) - { - } - - void operator()(DisplayBuffer& display_buffer) - { - SelectionList sorted_selections = m_window.selections(); - - std::sort(sorted_selections.begin(), sorted_selections.end(), - [](const Selection& lhs, const Selection& rhs) { return lhs.begin() < rhs.begin(); }); - - auto atom_it = display_buffer.begin(); - auto sel_it = sorted_selections.begin(); - - while (atom_it != display_buffer.end() - and sel_it != sorted_selections.end()) - { - Selection& sel = *sel_it; - DisplayAtom& atom = *atom_it; - - // [###------] - if (atom.begin() >= sel.begin() and atom.begin() < sel.end() and atom.end() > sel.end()) - { - atom_it = display_buffer.split(atom_it, sel.end()); - atom_it->attribute() |= Attributes::Underline; - ++atom_it; - ++sel_it; - } - // [---###---] - else if (atom.begin() < sel.begin() and atom.end() > sel.end()) - { - atom_it = display_buffer.split(atom_it, sel.begin()); - atom_it = display_buffer.split(++atom_it, sel.end()); - atom_it->attribute() |= Attributes::Underline; - ++atom_it; - ++sel_it; - } - // [------###] - else if (atom.begin() < sel.begin() and atom.end() > sel.begin()) - { - atom_it = ++display_buffer.split(atom_it, sel.begin()); - atom_it->attribute() |= Attributes::Underline; - ++atom_it; - } - // [#########] - else if (atom.begin() >= sel.begin() and atom.end() <= sel.end()) - { - atom_it->attribute() |= Attributes::Underline; - ++atom_it; - } - // [---------] - else if (atom.begin() >= sel.end()) - ++sel_it; - // [---------] - else if (atom.end() <= sel.begin()) - ++atom_it; - else - assert(false); - } - - boost::regex ex("\n"); - for (auto& sel : sorted_selections) - colorize_regex_range(display_buffer, sel.begin(), sel.end(), - ex, Color::Default, Color::Yellow); - - } - - static FilterAndId create(Window& window, - const FilterParameters& params) - { - return FilterAndId("highlight_selections", - SelectionsHighlighter(window)); - } - -private: - const Window& m_window; -}; - -void register_filters() -{ - FilterRegistry& registry = FilterRegistry::instance(); - - registry.register_factory("highlight_selections", SelectionsHighlighter::create); - registry.register_factory("expand_tabs", SimpleFilterFactory("expand_tabs")); - registry.register_factory("number_lines", SimpleFilterFactory("number_lines")); - registry.register_factory("hlcpp", SimpleFilterFactory("hlcpp")); - registry.register_factory("regex", colorize_regex_factory); -} - -} diff --git a/src/filters.hh b/src/filters.hh deleted file mode 100644 index 0d88da46..00000000 --- a/src/filters.hh +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef filters_hh_INCLUDED -#define filters_hh_INCLUDED - -namespace Kakoune -{ - -void register_filters(); - -} - -#endif // filters_hh_INCLUDED diff --git a/src/highlighter.hh b/src/highlighter.hh new file mode 100644 index 00000000..3000dd27 --- /dev/null +++ b/src/highlighter.hh @@ -0,0 +1,16 @@ +#ifndef highlighter_hh_INCLUDED +#define highlighter_hh_INCLUDED + +#include + +namespace Kakoune +{ + +class DisplayBuffer; + +typedef std::function HighlighterFunc; +typedef std::pair HighlighterAndId; + +} + +#endif // highlighter_hh_INCLUDED diff --git a/src/highlighter_registry.cc b/src/highlighter_registry.cc new file mode 100644 index 00000000..fdf8d15f --- /dev/null +++ b/src/highlighter_registry.cc @@ -0,0 +1,45 @@ +#include "highlighter_registry.hh" + +#include "exception.hh" +#include "window.hh" + +namespace Kakoune +{ + +struct factory_not_found : public runtime_error +{ + factory_not_found() : runtime_error("factory not found") {} +}; + +void HighlighterRegistry::register_factory(const std::string& name, + const HighlighterFactory& factory) +{ + assert(m_factories.find(name) == m_factories.end()); + m_factories[name] = factory; +} + +void HighlighterRegistry::add_highlighter_to_window(Window& window, + const std::string& name, + const HighlighterParameters& parameters) +{ + auto it = m_factories.find(name); + if (it == m_factories.end()) + throw factory_not_found(); + + window.add_highlighter(it->second(window, parameters)); +} + +CandidateList HighlighterRegistry::complete_highlighter(const std::string& prefix, + size_t cursor_pos) +{ + std::string real_prefix = prefix.substr(0, cursor_pos); + CandidateList result; + for (auto& highlighter : m_factories) + { + if (highlighter.first.substr(0, real_prefix.length()) == real_prefix) + result.push_back(highlighter.first); + } + return result; +} + +} diff --git a/src/highlighter_registry.hh b/src/highlighter_registry.hh new file mode 100644 index 00000000..19a285ea --- /dev/null +++ b/src/highlighter_registry.hh @@ -0,0 +1,40 @@ +#ifndef highlighter_registry_h_INCLUDED +#define highlighter_registry_h_INCLUDED + +#include +#include + +#include "highlighter.hh" +#include "utils.hh" +#include "completion.hh" + +namespace Kakoune +{ + +class Window; + +typedef std::vector HighlighterParameters; + +typedef std::function HighlighterFactory; + +class HighlighterRegistry : public Singleton +{ +public: + void register_factory(const std::string& name, + const HighlighterFactory& factory); + + void add_highlighter_to_window(Window& window, + const std::string& factory_name, + const HighlighterParameters& parameters); + + CandidateList complete_highlighter(const std::string& prefix, + size_t cursor_pos); + +private: + std::unordered_map m_factories; +}; + +} + +#endif // highlighter_registry_h_INCLUDED diff --git a/src/highlighters.cc b/src/highlighters.cc new file mode 100644 index 00000000..7c4d9bb0 --- /dev/null +++ b/src/highlighters.cc @@ -0,0 +1,305 @@ +#include "highlighters.hh" + +#include "assert.hh" +#include "window.hh" +#include "display_buffer.hh" +#include "highlighter_registry.hh" +#include + +namespace Kakoune +{ + +void colorize_regex_range(DisplayBuffer& display_buffer, + const BufferIterator& range_begin, + const BufferIterator& range_end, + const boost::regex& ex, + Color fg_color, Color bg_color = Color::Default) +{ + assert(range_begin <= range_end); + + if (range_begin >= display_buffer.back().end() or + range_end <= display_buffer.front().begin()) + return; + + BufferIterator display_begin = std::max(range_begin, + display_buffer.front().begin()); + BufferIterator display_end = std::min(range_end, + display_buffer.back().end()); + + boost::regex_iterator re_it(display_begin, display_end, + ex, boost::match_nosubs); + boost::regex_iterator re_end; + DisplayBuffer::iterator atom_it = display_buffer.begin(); + for (; re_it != re_end; ++re_it) + { + BufferIterator begin = (*re_it)[0].first; + BufferIterator end = (*re_it)[0].second; + assert(begin != end); + + auto begin_atom_it = display_buffer.atom_containing(begin, atom_it); + assert(begin_atom_it != display_buffer.end()); + if (begin_atom_it->begin() != begin) + begin_atom_it = ++display_buffer.split(begin_atom_it, begin); + + auto end_atom_it = display_buffer.atom_containing(end, begin_atom_it); + if (end_atom_it != display_buffer.end() and + end_atom_it->begin() != end) + end_atom_it = ++display_buffer.split(end_atom_it, end); + + assert(begin_atom_it != end_atom_it); + + for (auto it = begin_atom_it; it != end_atom_it; ++it) + { + it->fg_color() = fg_color; + it->bg_color() = bg_color; + } + + atom_it = end_atom_it; + } +} + +void colorize_regex(DisplayBuffer& display_buffer, + const boost::regex& ex, + Color fg_color, Color bg_color = Color::Default) +{ + colorize_regex_range(display_buffer, display_buffer.front().begin(), + display_buffer.back().end(), ex, fg_color, bg_color); +} + +Color parse_color(const std::string& color) +{ + if (color == "default") return Color::Default; + if (color == "black") return Color::Black; + if (color == "red") return Color::Red; + if (color == "green") return Color::Green; + if (color == "yellow") return Color::Yellow; + if (color == "blue") return Color::Blue; + if (color == "magenta") return Color::Magenta; + if (color == "cyan") return Color::Cyan; + if (color == "white") return Color::White; + return Color::Default; +} + +HighlighterAndId colorize_regex_factory(Window& window, + const HighlighterParameters params) +{ + if (params.size() != 3) + throw runtime_error("wrong parameter count"); + + boost::regex ex(params[0]); + + Color fg_color = parse_color(params[1]); + Color bg_color = parse_color(params[2]); + + std::string id = "colre'" + params[0] + "'"; + + return HighlighterAndId(id, std::bind(colorize_regex, std::placeholders::_1, + ex, fg_color, bg_color)); +} + +void colorize_cplusplus(DisplayBuffer& display_buffer) +{ + static boost::regex preprocessor("(\\`|(?<=\\n))\\h*#\\h*[^\\n]*"); + colorize_regex(display_buffer, preprocessor, Color::Magenta); + + static boost::regex comments("//[^\\n]*\\n"); + colorize_regex(display_buffer, comments, Color::Cyan); + + static boost::regex strings("(?|\\<-?\\d+[fdiu]?|'\\\\?[^']?'"); + colorize_regex(display_buffer, values, Color::Red); + + static boost::regex builtin_types("\\<(void|int|char|unsigned|float|bool|size_t)\\>"); + colorize_regex(display_buffer, builtin_types, Color::Yellow); + + static boost::regex control_keywords("\\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw)\\>"); + colorize_regex(display_buffer, control_keywords, Color::Blue); + + //static boost::regex operators("->|\\+|\\-|\\*|/|\\\\|\\&|\\|\\^|[<>=!+-]=|=|\\(|\\)|\\[|\\]|\\{|\\}|\\<(not|and|or|xor)\\>"); + //colorize_regex(display_buffer, operators, Color::Green); + + static boost::regex types_keywords("\\<(const|auto|namespace|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual)\\>"); + colorize_regex(display_buffer, types_keywords, Color::Green); +} + +void expand_tabulations(DisplayBuffer& display_buffer) +{ + const int tabstop = 8; + for (auto atom_it = display_buffer.begin(); + atom_it != display_buffer.end(); ++atom_it) + { + for (BufferIterator it = atom_it->begin(); it != atom_it->end(); ++it) + { + if (*it == '\t') + { + if (it != atom_it->begin()) + atom_it = ++display_buffer.split(atom_it, it); + + if (it+1 != atom_it->end()) + atom_it = display_buffer.split(atom_it, it+1); + + BufferCoord pos = it.buffer().line_and_column_at(it); + + int column = 0; + for (auto line_it = it.buffer().iterator_at({pos.line, 0}); + line_it != it; ++line_it) + { + assert(*line_it != '\n'); + if (*line_it == '\t') + column += tabstop - (column % tabstop); + else + ++column; + } + + int count = tabstop - (column % tabstop); + display_buffer.replace_atom_content(atom_it, + std::string(count, ' ')); + } + } + } +} + +void show_line_numbers(DisplayBuffer& display_buffer) +{ + const Buffer& buffer = display_buffer.front().begin().buffer(); + BufferCoord coord = buffer.line_and_column_at(display_buffer.begin()->begin()); + + int last_line = buffer.line_and_column_at(display_buffer.back().end()-1).line; + + for (; coord.line <= last_line; ++coord.line) + { + BufferIterator line_start = buffer.iterator_at(coord); + DisplayBuffer::iterator atom_it = display_buffer.atom_containing(line_start); + if (atom_it != display_buffer.end()) + { + if (atom_it->begin() != line_start) + { + if (not atom_it->splitable()) + continue; + + atom_it = ++display_buffer.split(atom_it, line_start); + } + atom_it = display_buffer.insert( + atom_it, + DisplayAtom(atom_it->coord(), + atom_it->begin(), atom_it->begin(), + Color::Black, Color::White)); + + char buffer[6]; + snprintf(buffer, 6, "%3d ", coord.line + 1); + display_buffer.replace_atom_content(atom_it, buffer); + } + } +} + +template +class SimpleHighlighterFactory +{ +public: + SimpleHighlighterFactory(const std::string& id) : m_id(id) {} + + HighlighterAndId operator()(Window& window, + const HighlighterParameters& params) const + { + return HighlighterAndId(m_id, HighlighterFunc(highlighter_func)); + } +private: + std::string m_id; +}; + +class SelectionsHighlighter +{ +public: + SelectionsHighlighter(Window& window) + : m_window(window) + { + } + + void operator()(DisplayBuffer& display_buffer) + { + SelectionList sorted_selections = m_window.selections(); + + std::sort(sorted_selections.begin(), sorted_selections.end(), + [](const Selection& lhs, const Selection& rhs) { return lhs.begin() < rhs.begin(); }); + + auto atom_it = display_buffer.begin(); + auto sel_it = sorted_selections.begin(); + + while (atom_it != display_buffer.end() + and sel_it != sorted_selections.end()) + { + Selection& sel = *sel_it; + DisplayAtom& atom = *atom_it; + + // [###------] + if (atom.begin() >= sel.begin() and atom.begin() < sel.end() and atom.end() > sel.end()) + { + atom_it = display_buffer.split(atom_it, sel.end()); + atom_it->attribute() |= Attributes::Underline; + ++atom_it; + ++sel_it; + } + // [---###---] + else if (atom.begin() < sel.begin() and atom.end() > sel.end()) + { + atom_it = display_buffer.split(atom_it, sel.begin()); + atom_it = display_buffer.split(++atom_it, sel.end()); + atom_it->attribute() |= Attributes::Underline; + ++atom_it; + ++sel_it; + } + // [------###] + else if (atom.begin() < sel.begin() and atom.end() > sel.begin()) + { + atom_it = ++display_buffer.split(atom_it, sel.begin()); + atom_it->attribute() |= Attributes::Underline; + ++atom_it; + } + // [#########] + else if (atom.begin() >= sel.begin() and atom.end() <= sel.end()) + { + atom_it->attribute() |= Attributes::Underline; + ++atom_it; + } + // [---------] + else if (atom.begin() >= sel.end()) + ++sel_it; + // [---------] + else if (atom.end() <= sel.begin()) + ++atom_it; + else + assert(false); + } + + boost::regex ex("\n"); + for (auto& sel : sorted_selections) + colorize_regex_range(display_buffer, sel.begin(), sel.end(), + ex, Color::Default, Color::Yellow); + + } + + static HighlighterAndId create(Window& window, + const HighlighterParameters& params) + { + return HighlighterAndId("highlight_selections", + SelectionsHighlighter(window)); + } + +private: + const Window& m_window; +}; + +void register_highlighters() +{ + HighlighterRegistry& registry = HighlighterRegistry::instance(); + + registry.register_factory("highlight_selections", SelectionsHighlighter::create); + registry.register_factory("expand_tabs", SimpleHighlighterFactory("expand_tabs")); + registry.register_factory("number_lines", SimpleHighlighterFactory("number_lines")); + registry.register_factory("hlcpp", SimpleHighlighterFactory("hlcpp")); + registry.register_factory("regex", colorize_regex_factory); +} + +} diff --git a/src/highlighters.hh b/src/highlighters.hh new file mode 100644 index 00000000..53738e94 --- /dev/null +++ b/src/highlighters.hh @@ -0,0 +1,11 @@ +#ifndef highlighters_hh_INCLUDED +#define highlighters_hh_INCLUDED + +namespace Kakoune +{ + +void register_highlighters(); + +} + +#endif // highlighters_hh_INCLUDED diff --git a/src/kakrc b/src/kakrc index b6362512..23e04966 100644 --- a/src/kakrc +++ b/src/kakrc @@ -1,2 +1,2 @@ -hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addfilter hlcpp +hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addhl hlcpp diff --git a/src/main.cc b/src/main.cc index 2b1ca02e..bd1775fb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,8 +7,8 @@ #include "selectors.hh" #include "assert.hh" #include "debug.hh" -#include "filters.hh" -#include "filter_registry.hh" +#include "highlighters.hh" +#include "highlighter_registry.hh" #include "hooks_manager.hh" #include @@ -433,17 +433,17 @@ void show_buffer(const CommandParameters& params, const Context& context) main_context = Context(*buffer->get_or_create_window()); } -void add_filter(const CommandParameters& params, const Context& context) +void add_highlighter(const CommandParameters& params, const Context& context) { if (params.size() < 1) throw wrong_argument_count(); try { - FilterRegistry& registry = FilterRegistry::instance(); - FilterParameters filter_params(params.begin()+1, params.end()); - registry.add_filter_to_window(*context.window, params[0], - filter_params); + HighlighterRegistry& registry = HighlighterRegistry::instance(); + HighlighterParameters highlighter_params(params.begin()+1, params.end()); + registry.add_highlighter_to_window(*context.window, params[0], + highlighter_params); } catch (runtime_error& err) { @@ -451,12 +451,12 @@ void add_filter(const CommandParameters& params, const Context& context) } } -void rm_filter(const CommandParameters& params, const Context& context) +void rm_highlighter(const CommandParameters& params, const Context& context) { if (params.size() != 1) throw wrong_argument_count(); - context.window->remove_filter(params[0]); + context.window->remove_highlighter(params[0]); } void add_hook(const CommandParameters& params, const Context& context) @@ -679,11 +679,11 @@ int main(int argc, char* argv[]) { init_ncurses(); - CommandManager command_manager; - BufferManager buffer_manager; - RegisterManager register_manager; - FilterRegistry filter_registry; - HooksManager hooks_manager; + CommandManager command_manager; + BufferManager buffer_manager; + RegisterManager register_manager; + HighlighterRegistry highlighter_registry; + HooksManager hooks_manager; command_manager.register_command(std::vector{ "e", "edit" }, edit, PerArgumentCommandCompleter{ complete_filename }); @@ -697,21 +697,21 @@ int main(int argc, char* argv[]) PerArgumentCommandCompleter { std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2) }); - command_manager.register_command(std::vector{ "af", "addfilter" }, add_filter, + command_manager.register_command(std::vector{ "ah", "addhl" }, add_highlighter, PerArgumentCommandCompleter { - std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2) + std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2) }); - command_manager.register_command(std::vector{ "rf", "rmfilter" }, rm_filter, + command_manager.register_command(std::vector{ "rh", "rmhl" }, rm_highlighter, PerArgumentCommandCompleter { [&](const std::string& prefix, size_t cursor_pos) - { return main_context.window->complete_filterid(prefix, cursor_pos); } + { return main_context.window->complete_highlighterid(prefix, cursor_pos); } }); command_manager.register_command(std::vector{ "hook" }, add_hook); command_manager.register_command(std::vector{ "source" }, exec_commands_in_file, PerArgumentCommandCompleter{ complete_filename }); - register_filters(); + register_highlighters(); try { diff --git a/src/window.cc b/src/window.cc index 69a91d97..02625fc5 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1,7 +1,7 @@ #include "window.hh" #include "assert.hh" -#include "filter_registry.hh" +#include "highlighter_registry.hh" #include "hooks_manager.hh" #include @@ -54,13 +54,13 @@ Window::Window(Buffer& buffer) { m_selections.push_back(Selection(buffer.begin(), buffer.begin())); - FilterRegistry& registry = FilterRegistry::instance(); + HighlighterRegistry& registry = HighlighterRegistry::instance(); HooksManager::instance().run_hook("WinCreate", buffer.name(), Context(*this)); - registry.add_filter_to_window(*this, "expand_tabs", FilterParameters()); - registry.add_filter_to_window(*this, "highlight_selections", FilterParameters()); + registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters()); + registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters()); } void Window::check_invariant() const @@ -293,9 +293,9 @@ void Window::update_display_buffer() m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end)); - for (auto& filter : m_filters) + for (auto& highlighter : m_highlighters) { - filter.second(m_display_buffer); + highlighter.second(m_display_buffer); m_display_buffer.check_invariant(); } } @@ -343,37 +343,37 @@ std::string Window::status_line() const return oss.str(); } -void Window::add_filter(FilterAndId&& filter) +void Window::add_highlighter(HighlighterAndId&& highlighter) { - for (auto it = m_filters.begin(); it != m_filters.end(); ++it) + for (auto it = m_highlighters.begin(); it != m_highlighters.end(); ++it) { - if (it->first == filter.first) - throw filter_id_not_unique(filter.first); + if (it->first == highlighter.first) + throw highlighter_id_not_unique(highlighter.first); } - m_filters.push_back(filter); + m_highlighters.push_back(highlighter); } -void Window::remove_filter(const std::string& id) +void Window::remove_highlighter(const std::string& id) { - for (auto it = m_filters.begin(); it != m_filters.end(); ++it) + for (auto it = m_highlighters.begin(); it != m_highlighters.end(); ++it) { if (it->first == id) { - m_filters.erase(it); + m_highlighters.erase(it); return; } } } -CandidateList Window::complete_filterid(const std::string& prefix, +CandidateList Window::complete_highlighterid(const std::string& prefix, size_t cursor_pos) { std::string real_prefix = prefix.substr(0, cursor_pos); CandidateList result; - for (auto& filter : m_filters) + for (auto& highlighter : m_highlighters) { - if (filter.first.substr(0, real_prefix.length()) == real_prefix) - result.push_back(filter.first); + if (highlighter.first.substr(0, real_prefix.length()) == real_prefix) + result.push_back(highlighter.first); } return result; } diff --git a/src/window.hh b/src/window.hh index aa8c066e..0f989ece 100644 --- a/src/window.hh +++ b/src/window.hh @@ -7,7 +7,7 @@ #include "dynamic_buffer_iterator.hh" #include "display_buffer.hh" #include "completion.hh" -#include "filter.hh" +#include "highlighter.hh" namespace Kakoune { @@ -87,17 +87,17 @@ public: std::string status_line() const; - struct filter_id_not_unique : public runtime_error + struct highlighter_id_not_unique : public runtime_error { - filter_id_not_unique(const std::string& id) - : runtime_error("filter id not unique: " + id) {} + highlighter_id_not_unique(const std::string& id) + : runtime_error("highlighter id not unique: " + id) {} }; - void add_filter(FilterAndId&& filter); - void remove_filter(const std::string& id); + void add_highlighter(HighlighterAndId&& highlighter); + void remove_highlighter(const std::string& id); - CandidateList complete_filterid(const std::string& prefix, - size_t cursor_pos = std::string::npos); + CandidateList complete_highlighterid(const std::string& prefix, + size_t cursor_pos = std::string::npos); private: friend class Buffer; @@ -121,8 +121,8 @@ private: SelectionList m_selections; DisplayBuffer m_display_buffer; - typedef std::vector FilterList; - FilterList m_filters; + typedef std::vector HighlighterList; + HighlighterList m_highlighters; }; class IncrementalInserter -- cgit v1.2.3