Skip to content

Commit bf1f639

Browse files
committed
Merge branch 'rs/grep-color-words'
Allow painting or not painting (partial) matches in context lines when showing "grep -C<num>" output in color. * rs/grep-color-words: grep: add color.grep.matchcontext and color.grep.matchselected
2 parents 81d645d + 79a7710 commit bf1f639

File tree

4 files changed

+123
-9
lines changed

4 files changed

+123
-9
lines changed

Documentation/config.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,11 @@ color.grep.<slot>::
885885
`linenumber`;;
886886
line number prefix (when using `-n`)
887887
`match`;;
888-
matching text
888+
matching text (same as setting `matchContext` and `matchSelected`)
889+
`matchContext`;;
890+
matching text in context lines
891+
`matchSelected`;;
892+
matching text in selected lines
889893
`selected`;;
890894
non-matching text in selected lines
891895
`separator`;;

grep.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ void init_grep_defaults(void)
3535
strcpy(opt->color_filename, "");
3636
strcpy(opt->color_function, "");
3737
strcpy(opt->color_lineno, "");
38-
strcpy(opt->color_match, GIT_COLOR_BOLD_RED);
38+
strcpy(opt->color_match_context, GIT_COLOR_BOLD_RED);
39+
strcpy(opt->color_match_selected, GIT_COLOR_BOLD_RED);
3940
strcpy(opt->color_selected, "");
4041
strcpy(opt->color_sep, GIT_COLOR_CYAN);
4142
opt->color = -1;
@@ -101,12 +102,22 @@ int grep_config(const char *var, const char *value, void *cb)
101102
color = opt->color_function;
102103
else if (!strcmp(var, "color.grep.linenumber"))
103104
color = opt->color_lineno;
104-
else if (!strcmp(var, "color.grep.match"))
105-
color = opt->color_match;
105+
else if (!strcmp(var, "color.grep.matchcontext"))
106+
color = opt->color_match_context;
107+
else if (!strcmp(var, "color.grep.matchselected"))
108+
color = opt->color_match_selected;
106109
else if (!strcmp(var, "color.grep.selected"))
107110
color = opt->color_selected;
108111
else if (!strcmp(var, "color.grep.separator"))
109112
color = opt->color_sep;
113+
else if (!strcmp(var, "color.grep.match")) {
114+
int rc = 0;
115+
if (!value)
116+
return config_error_nonbool(var);
117+
rc |= color_parse(value, opt->color_match_context);
118+
rc |= color_parse(value, opt->color_match_selected);
119+
return rc;
120+
}
110121

111122
if (color) {
112123
if (!value)
@@ -144,7 +155,8 @@ void grep_init(struct grep_opt *opt, const char *prefix)
144155
strcpy(opt->color_filename, def->color_filename);
145156
strcpy(opt->color_function, def->color_function);
146157
strcpy(opt->color_lineno, def->color_lineno);
147-
strcpy(opt->color_match, def->color_match);
158+
strcpy(opt->color_match_context, def->color_match_context);
159+
strcpy(opt->color_match_selected, def->color_match_selected);
148160
strcpy(opt->color_selected, def->color_selected);
149161
strcpy(opt->color_sep, def->color_sep);
150162
}
@@ -1084,7 +1096,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
10841096
const char *name, unsigned lno, char sign)
10851097
{
10861098
int rest = eol - bol;
1087-
char *line_color = NULL;
1099+
const char *match_color, *line_color = NULL;
10881100

10891101
if (opt->file_break && opt->last_shown == 0) {
10901102
if (opt->show_hunk_mark)
@@ -1122,6 +1134,10 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
11221134
int ch = *eol;
11231135
int eflags = 0;
11241136

1137+
if (sign == ':')
1138+
match_color = opt->color_match_selected;
1139+
else
1140+
match_color = opt->color_match_context;
11251141
if (sign == ':')
11261142
line_color = opt->color_selected;
11271143
else if (sign == '-')
@@ -1135,8 +1151,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
11351151

11361152
output_color(opt, bol, match.rm_so, line_color);
11371153
output_color(opt, bol + match.rm_so,
1138-
match.rm_eo - match.rm_so,
1139-
opt->color_match);
1154+
match.rm_eo - match.rm_so, match_color);
11401155
bol += match.rm_eo;
11411156
rest -= match.rm_eo;
11421157
eflags = REG_NOTBOL;

grep.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ struct grep_opt {
124124
char color_filename[COLOR_MAXLEN];
125125
char color_function[COLOR_MAXLEN];
126126
char color_lineno[COLOR_MAXLEN];
127-
char color_match[COLOR_MAXLEN];
127+
char color_match_context[COLOR_MAXLEN];
128+
char color_match_selected[COLOR_MAXLEN];
128129
char color_selected[COLOR_MAXLEN];
129130
char color_sep[COLOR_MAXLEN];
130131
int regflags;

t/t7810-grep.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,4 +1202,98 @@ test_expect_success LIBPCRE 'grep -P "^ "' '
12021202
test_cmp expected actual
12031203
'
12041204

1205+
cat >expected <<EOF
1206+
space-line without leading space1
1207+
space: line <RED>with <RESET>leading space1
1208+
space: line <RED>with <RESET>leading <RED>space2<RESET>
1209+
space: line <RED>with <RESET>leading space3
1210+
space:line without leading <RED>space2<RESET>
1211+
EOF
1212+
1213+
test_expect_success 'grep --color -e A -e B with context' '
1214+
test_config color.grep.context normal &&
1215+
test_config color.grep.filename normal &&
1216+
test_config color.grep.function normal &&
1217+
test_config color.grep.linenumber normal &&
1218+
test_config color.grep.matchContext normal &&
1219+
test_config color.grep.matchSelected red &&
1220+
test_config color.grep.selected normal &&
1221+
test_config color.grep.separator normal &&
1222+
1223+
git grep --color=always -C2 -e "with " -e space2 space |
1224+
test_decode_color >actual &&
1225+
test_cmp expected actual
1226+
'
1227+
1228+
cat >expected <<EOF
1229+
space-line without leading space1
1230+
space- line with leading space1
1231+
space: line <RED>with <RESET>leading <RED>space2<RESET>
1232+
space- line with leading space3
1233+
space-line without leading space2
1234+
EOF
1235+
1236+
test_expect_success 'grep --color -e A --and -e B with context' '
1237+
test_config color.grep.context normal &&
1238+
test_config color.grep.filename normal &&
1239+
test_config color.grep.function normal &&
1240+
test_config color.grep.linenumber normal &&
1241+
test_config color.grep.matchContext normal &&
1242+
test_config color.grep.matchSelected red &&
1243+
test_config color.grep.selected normal &&
1244+
test_config color.grep.separator normal &&
1245+
1246+
git grep --color=always -C2 -e "with " --and -e space2 space |
1247+
test_decode_color >actual &&
1248+
test_cmp expected actual
1249+
'
1250+
1251+
cat >expected <<EOF
1252+
space-line without leading space1
1253+
space: line <RED>with <RESET>leading space1
1254+
space- line with leading space2
1255+
space: line <RED>with <RESET>leading space3
1256+
space-line without leading space2
1257+
EOF
1258+
1259+
test_expect_success 'grep --color -e A --and --not -e B with context' '
1260+
test_config color.grep.context normal &&
1261+
test_config color.grep.filename normal &&
1262+
test_config color.grep.function normal &&
1263+
test_config color.grep.linenumber normal &&
1264+
test_config color.grep.matchContext normal &&
1265+
test_config color.grep.matchSelected red &&
1266+
test_config color.grep.selected normal &&
1267+
test_config color.grep.separator normal &&
1268+
1269+
git grep --color=always -C2 -e "with " --and --not -e space2 space |
1270+
test_decode_color >actual &&
1271+
test_cmp expected actual
1272+
'
1273+
1274+
cat >expected <<EOF
1275+
hello.c-#include <stdio.h>
1276+
hello.c=int main(int argc, const char **argv)
1277+
hello.c-{
1278+
hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1279+
hello.c- return 0;
1280+
hello.c- /* char ?? */
1281+
hello.c-}
1282+
EOF
1283+
1284+
test_expect_success 'grep --color -e A --and -e B -p with context' '
1285+
test_config color.grep.context normal &&
1286+
test_config color.grep.filename normal &&
1287+
test_config color.grep.function normal &&
1288+
test_config color.grep.linenumber normal &&
1289+
test_config color.grep.matchContext normal &&
1290+
test_config color.grep.matchSelected red &&
1291+
test_config color.grep.selected normal &&
1292+
test_config color.grep.separator normal &&
1293+
1294+
git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1295+
test_decode_color >actual &&
1296+
test_cmp expected actual
1297+
'
1298+
12051299
test_done

0 commit comments

Comments
 (0)