From b69134c36fb1585404776c668ad6d7d72975a210 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 7 Aug 2012 00:13:54 +0200 Subject: [PATCH] new regex highlighter parameters syntax, support per capture highlight --- src/highlighters.cc | 45 +++++++++++++++++++++++++++++++++------------ src/kakrc | 2 +- src/rc/cpp.kak | 14 +++++++------- src/rc/diff.kak | 6 +++--- src/rc/git.kak | 12 ++++++------ src/rc/kakrc.kak | 18 +++++++++--------- 6 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index 955d0f0c..757de227 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -52,20 +52,27 @@ void highlight_range(DisplayBuffer& display_buffer, } } +typedef std::unordered_map> ColorSpec; void colorize_regex(DisplayBuffer& display_buffer, const Regex& ex, - Color fg_color, Color bg_color = Color::Default) + ColorSpec colors) { const BufferRange& range = display_buffer.range(); - RegexIterator re_it(range.first, range.second, ex, boost::match_nosubs); + RegexIterator re_it(range.first, range.second, ex); RegexIterator re_end; for (; re_it != re_end; ++re_it) { - highlight_range(display_buffer, (*re_it)[0].first, (*re_it)[0].second, true, - [&](DisplayAtom& atom) { - atom.fg_color = fg_color; - atom.bg_color = bg_color; - }); + for (size_t n = 0; n < re_it->size(); ++n) + { + if (colors.find(n) == colors.end()) + continue; + + highlight_range(display_buffer, (*re_it)[n].first, (*re_it)[n].second, true, + [&](DisplayAtom& atom) { + atom.fg_color = colors[n].first; + atom.bg_color = colors[n].second; + }); + } } } @@ -86,19 +93,33 @@ Color parse_color(const String& color) HighlighterAndId colorize_regex_factory(Window& window, const HighlighterParameters params) { - if (params.size() != 3) + if (params.size() < 2) throw runtime_error("wrong parameter count"); Regex ex(params[0].begin(), params[0].end(), boost::regex::perl | boost::regex::optimize); - Color fg_color = parse_color(params[1]); - Color bg_color = parse_color(params[2]); + static Regex color_spec_ex(LR"((\d+):(\w+)(,(\w+))?)"); + ColorSpec colors; + for (auto it = params.begin() + 1; it != params.end(); ++it) + { + boost::match_results res; + if (not boost::regex_match(it->begin(), it->end(), res, color_spec_ex)) + throw runtime_error("wrong colorspec: '" + *it + + "' expected :[,]"); + + int capture = str_to_int(String(res[1].first, res[1].second)); + Color fg_color = parse_color(String(res[2].first, res[2].second)); + Color bg_color = res[4].matched ? + parse_color(String(res[4].first, res[4].second)) + : Color::Default; + colors[capture] = { fg_color, bg_color }; + } String id = "colre'" + params[0] + "'"; - return HighlighterAndId(id, std::bind(colorize_regex, - _1, ex, fg_color, bg_color)); + return HighlighterAndId(id, std::bind(colorize_regex, _1, ex, + std::move(colors))); } void expand_tabulations(Window& window, DisplayBuffer& display_buffer) diff --git a/src/kakrc b/src/kakrc index 27e4e289..34efbf76 100644 --- a/src/kakrc +++ b/src/kakrc @@ -1,4 +1,4 @@ -hook global WinCreate .* %{ addhl regex \h+(?=\n) default red } +hook global WinCreate .* %{ addhl regex \h+(?=\n) 0:default,red } hook global WinCreate .* %{ addhl number_lines } runtime rc/cpp.kak diff --git a/src/rc/cpp.kak b/src/rc/cpp.kak index 3bfc5f73..0e7a5bf5 100644 --- a/src/rc/cpp.kak +++ b/src/rc/cpp.kak @@ -4,13 +4,13 @@ hook global BufCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) %{ hook global WinSetOption filetype=cpp %{ addhl group cpp-highlight; - addhl -group cpp-highlight regex "\<(this|true|false|NULL|nullptr|)\>|\<-?\d+[fdiu]?|'((\\.)?|[^'\\])'" red default; - addhl -group cpp-highlight regex "\<(void|int|char|unsigned|float|bool|size_t)\>" yellow default; - addhl -group cpp-highlight regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not)\>" blue default; - addhl -group cpp-highlight regex "\<(const|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename)\>" green default; - addhl -group cpp-highlight regex "(?|\<-?\d+[fdiu]?|'((\\.)?|[^'\\])'" 0:red,default; + addhl -group cpp-highlight regex "\<(void|int|char|unsigned|float|bool|size_t)\>" 0:yellow,default; + addhl -group cpp-highlight regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not)\>" 0:blue,default; + addhl -group cpp-highlight regex "\<(const|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename)\>" 0:green,default; + addhl -group cpp-highlight regex "(? green default - addhl -group kak-highlight regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> yellow default - addhl -group kak-highlight regex (?<=\ 0:green,default + addhl -group kak-highlight regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> 0:yellow,default + addhl -group kak-highlight regex (?<=\