new regex highlighter parameters syntax, support per capture highlight

This commit is contained in:
Maxime Coste 2012-08-07 00:13:54 +02:00
parent a38a9c3bf2
commit b69134c36f
6 changed files with 59 additions and 38 deletions

View File

@ -52,22 +52,29 @@ void highlight_range(DisplayBuffer& display_buffer,
}
}
typedef std::unordered_map<size_t, std::pair<Color, Color>> 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,
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 = fg_color;
atom.bg_color = bg_color;
atom.fg_color = colors[n].first;
atom.bg_color = colors[n].second;
});
}
}
}
Color parse_color(const String& color)
{
@ -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<String::iterator> res;
if (not boost::regex_match(it->begin(), it->end(), res, color_spec_ex))
throw runtime_error("wrong colorspec: '" + *it +
"' expected <capture>:<fgcolor>[,<bgcolor>]");
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)

View File

@ -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

View File

@ -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 "(?<!')\"(\\\"|[^\"])*\"" magenta default;
addhl -group cpp-highlight regex "(\`|(?<=\n))\h*#\h*[^\n]*" magenta default;
addhl -group cpp-highlight regex "(//[^\n]*\n)|(/\*.*?(\*/|\'))" cyan default;
addhl -group cpp-highlight regex "\<(this|true|false|NULL|nullptr|)\>|\<-?\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 "(?<!')\"(\\\"|[^\"])*\"" 0:magenta,default;
addhl -group cpp-highlight regex "(\`|(?<=\n))\h*#\h*[^\n]*" 0:magenta,default;
addhl -group cpp-highlight regex "(//[^\n]*\n)|(/\*.*?(\*/|\'))" 0:cyan,default;
addfilter group cpp-filters;
addfilter -group cpp-filters preserve_indent;
addfilter -group cpp-filters cleanup_whitespaces;

View File

@ -4,9 +4,9 @@ hook global BufCreate .*\.(diff|patch) %{
hook global WinSetOption filetype=diff %{
addhl group diff-highlight
addhl -group diff-highlight regex "^\+[^\n]*\n" green default
addhl -group diff-highlight regex "^-[^\n]*\n" red default
addhl -group diff-highlight regex "^@@[^\n]*@@" cyan default
addhl -group diff-highlight regex "^\+[^\n]*\n" 0:green,default
addhl -group diff-highlight regex "^-[^\n]*\n" 0:red,default
addhl -group diff-highlight regex "^@@[^\n]*@@" 0:cyan,default
}
hook global WinSetOption filetype=(?!diff).* %{

View File

@ -4,9 +4,9 @@ hook global BufCreate .*COMMIT_EDITMSG %{
hook global WinSetOption filetype=git-commit %{
addhl group git-commit-highlight
addhl -group git-commit-highlight regex "#[^\n]*\n" cyan default
addhl -group git-commit-highlight regex "\<(modified|deleted|new file):[^\n]*\n" magenta default
addhl -group git-commit-highlight regex "\<(modified|deleted|new file):" red default
addhl -group git-commit-highlight regex "#[^\n]*\n" 0:cyan,default
addhl -group git-commit-highlight regex "\<(modified|deleted|new file):[^\n]*\n" 0:magenta,default
addhl -group git-commit-highlight regex "\<(modified|deleted|new file):" 0:red,default
}
hook global WinSetOption filetype=(?!git-commit).* %{
@ -19,9 +19,9 @@ hook global BufCreate .*git-rebase-todo %{
hook global WinSetOption filetype=git-rebase %{
addhl group git-rebase-highlight
addhl -group git-rebase-highlight regex "#[^\n]*\n" cyan default
addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx]) \w+" magenta default
addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx])" green default
addhl -group git-rebase-highlight regex "#[^\n]*\n" 0:cyan,default
addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx]) \w+" 0:magenta,default
addhl -group git-rebase-highlight regex "^(pick|edit|reword|squash|fixup|exec|[persfx])" 0:green,default
}
hook global WinSetOption filetype=(?!git-rebase).* %{

View File

@ -4,15 +4,15 @@ hook global BufCreate (.*/)?(kakrc|.*.kak) %{
hook global WinSetOption filetype=kak %{
addhl group kak-highlight
addhl -group kak-highlight regex \<(hook|addhl|rmhl|addfilter|rmfilter|exec|source|runtime|def|echo|edit|set[gbw])\> green default
addhl -group kak-highlight regex \<(default|black|red|green|yellow|blue|magenta|cyan|white)\> yellow default
addhl -group kak-highlight regex (?<=\<hook)(\h+\w+){2}\h+\H+ magenta default
addhl -group kak-highlight regex (?<=\<hook)(\h+\w+){2} cyan default
addhl -group kak-highlight regex (?<=\<hook)(\h+\w+) red default
addhl -group kak-highlight regex (?<=\<hook)(\h+(global|buffer|window)) blue default
addhl -group kak-highlight regex (?<=\<regex)\h+\S+ magenta default
addhl -group kak-highlight regex (?<=\<set[gbw])\h+\S+\h+\S+ magenta default
addhl -group kak-highlight regex (?<=\<set[gbw])\h+\S+ red default
addhl -group kak-highlight regex \<(hook|addhl|rmhl|addfilter|rmfilter|exec|source|runtime|def|echo|edit|set[gbw])\> 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 (?<=\<hook)(\h+\w+){2}\h+\H+ 0:magenta,default
addhl -group kak-highlight regex (?<=\<hook)(\h+\w+){2} 0:cyan,default
addhl -group kak-highlight regex (?<=\<hook)(\h+\w+) 0:red,default
addhl -group kak-highlight regex (?<=\<hook)(\h+(global|buffer|window)) 0:blue,default
addhl -group kak-highlight regex (?<=\<regex)\h+\S+ 0:magenta,default
addhl -group kak-highlight regex (?<=\<set[gbw])\h+\S+\h+\S+ 0:magenta,default
addhl -group kak-highlight regex (?<=\<set[gbw])\h+\S+ 0:red,default
}
hook global WinSetOption filetype=(?!kak).* %{