grep: Colorize selected, context, and function lines

Colorize non-matching text of selected lines, context lines, and
function name lines.  The default for all three is no color, but they
can be configured using color.grep.<slot>.  The first two are similar
to the corresponding options in GNU grep, except that GNU grep applies
the color to the entire line, not just non-matching text.

Signed-off-by: Mark Lodato <lodatom@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Mark Lodato
2010-03-07 11:52:47 -05:00
committed by Junio C Hamano
parent 55f638bdc6
commit 00588bb5cd
4 changed files with 27 additions and 2 deletions

11
grep.c
View File

@ -529,6 +529,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
const char *name, unsigned lno, char sign)
{
int rest = eol - bol;
char *line_color = NULL;
if (opt->pre_context || opt->post_context) {
if (opt->last_shown == 0) {
@ -560,12 +561,18 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
int ch = *eol;
int eflags = 0;
if (sign == ':')
line_color = opt->color_selected;
else if (sign == '-')
line_color = opt->color_context;
else if (sign == '=')
line_color = opt->color_function;
*eol = '\0';
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
if (match.rm_so == match.rm_eo)
break;
opt->output(opt, bol, match.rm_so);
output_color(opt, bol, match.rm_so, line_color);
output_color(opt, bol + match.rm_so,
match.rm_eo - match.rm_so,
opt->color_match);
@ -575,7 +582,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
}
*eol = ch;
}
opt->output(opt, bol, rest);
output_color(opt, bol, rest, line_color);
opt->output(opt, "\n", 1);
}