@@ -13,6 +13,17 @@ static int grep_source_is_binary(struct grep_source *gs);
13
13
14
14
static struct grep_opt grep_defaults ;
15
15
16
+ static const char * color_grep_slots [] = {
17
+ [GREP_COLOR_CONTEXT ] = "context" ,
18
+ [GREP_COLOR_FILENAME ] = "filename" ,
19
+ [GREP_COLOR_FUNCTION ] = "function" ,
20
+ [GREP_COLOR_LINENO ] = "lineNumber" ,
21
+ [GREP_COLOR_MATCH_CONTEXT ] = "matchContext" ,
22
+ [GREP_COLOR_MATCH_SELECTED ] = "matchSelected" ,
23
+ [GREP_COLOR_SELECTED ] = "selected" ,
24
+ [GREP_COLOR_SEP ] = "separator" ,
25
+ };
26
+
16
27
static void std_output (struct grep_opt * opt , const void * buf , size_t size )
17
28
{
18
29
fwrite (buf , size , 1 , stdout );
@@ -42,14 +53,14 @@ void init_grep_defaults(void)
42
53
opt -> pathname = 1 ;
43
54
opt -> max_depth = -1 ;
44
55
opt -> pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED ;
45
- color_set (opt -> color_context , "" );
46
- color_set (opt -> color_filename , "" );
47
- color_set (opt -> color_function , "" );
48
- color_set (opt -> color_lineno , "" );
49
- color_set (opt -> color_match_context , GIT_COLOR_BOLD_RED );
50
- color_set (opt -> color_match_selected , GIT_COLOR_BOLD_RED );
51
- color_set (opt -> color_selected , "" );
52
- color_set (opt -> color_sep , GIT_COLOR_CYAN );
56
+ color_set (opt -> colors [ GREP_COLOR_CONTEXT ] , "" );
57
+ color_set (opt -> colors [ GREP_COLOR_FILENAME ] , "" );
58
+ color_set (opt -> colors [ GREP_COLOR_FUNCTION ] , "" );
59
+ color_set (opt -> colors [ GREP_COLOR_LINENO ] , "" );
60
+ color_set (opt -> colors [ GREP_COLOR_MATCH_CONTEXT ] , GIT_COLOR_BOLD_RED );
61
+ color_set (opt -> colors [ GREP_COLOR_MATCH_SELECTED ] , GIT_COLOR_BOLD_RED );
62
+ color_set (opt -> colors [ GREP_COLOR_SELECTED ] , "" );
63
+ color_set (opt -> colors [ GREP_COLOR_SEP ] , GIT_COLOR_CYAN );
53
64
opt -> color = -1 ;
54
65
opt -> output = std_output ;
55
66
}
@@ -76,7 +87,7 @@ static int parse_pattern_type_arg(const char *opt, const char *arg)
76
87
int grep_config (const char * var , const char * value , void * cb )
77
88
{
78
89
struct grep_opt * opt = & grep_defaults ;
79
- char * color = NULL ;
90
+ const char * slot ;
80
91
81
92
if (userdiff_config (var , value ) < 0 )
82
93
return -1 ;
@@ -103,32 +114,18 @@ int grep_config(const char *var, const char *value, void *cb)
103
114
104
115
if (!strcmp (var , "color.grep" ))
105
116
opt -> color = git_config_colorbool (var , value );
106
- else if (!strcmp (var , "color.grep.context" ))
107
- color = opt -> color_context ;
108
- else if (!strcmp (var , "color.grep.filename" ))
109
- color = opt -> color_filename ;
110
- else if (!strcmp (var , "color.grep.function" ))
111
- color = opt -> color_function ;
112
- else if (!strcmp (var , "color.grep.linenumber" ))
113
- color = opt -> color_lineno ;
114
- else if (!strcmp (var , "color.grep.matchcontext" ))
115
- color = opt -> color_match_context ;
116
- else if (!strcmp (var , "color.grep.matchselected" ))
117
- color = opt -> color_match_selected ;
118
- else if (!strcmp (var , "color.grep.selected" ))
119
- color = opt -> color_selected ;
120
- else if (!strcmp (var , "color.grep.separator" ))
121
- color = opt -> color_sep ;
122
- else if (!strcmp (var , "color.grep.match" )) {
123
- int rc = 0 ;
124
- if (!value )
125
- return config_error_nonbool (var );
126
- rc |= color_parse (value , opt -> color_match_context );
127
- rc |= color_parse (value , opt -> color_match_selected );
128
- return rc ;
129
- }
130
-
131
- if (color ) {
117
+ if (!strcmp (var , "color.grep.match" )) {
118
+ if (grep_config ("color.grep.matchcontext" , value , cb ) < 0 )
119
+ return -1 ;
120
+ if (grep_config ("color.grep.matchselected" , value , cb ) < 0 )
121
+ return -1 ;
122
+ } else if (skip_prefix (var , "color.grep." , & slot )) {
123
+ int i = LOOKUP_CONFIG (color_grep_slots , slot );
124
+ char * color ;
125
+
126
+ if (i < 0 )
127
+ return -1 ;
128
+ color = opt -> colors [i ];
132
129
if (!value )
133
130
return config_error_nonbool (var );
134
131
return color_parse (value , color );
@@ -144,6 +141,7 @@ int grep_config(const char *var, const char *value, void *cb)
144
141
void grep_init (struct grep_opt * opt , const char * prefix )
145
142
{
146
143
struct grep_opt * def = & grep_defaults ;
144
+ int i ;
147
145
148
146
memset (opt , 0 , sizeof (* opt ));
149
147
opt -> prefix = prefix ;
@@ -160,14 +158,8 @@ void grep_init(struct grep_opt *opt, const char *prefix)
160
158
opt -> relative = def -> relative ;
161
159
opt -> output = def -> output ;
162
160
163
- color_set (opt -> color_context , def -> color_context );
164
- color_set (opt -> color_filename , def -> color_filename );
165
- color_set (opt -> color_function , def -> color_function );
166
- color_set (opt -> color_lineno , def -> color_lineno );
167
- color_set (opt -> color_match_context , def -> color_match_context );
168
- color_set (opt -> color_match_selected , def -> color_match_selected );
169
- color_set (opt -> color_selected , def -> color_selected );
170
- color_set (opt -> color_sep , def -> color_sep );
161
+ for (i = 0 ; i < NR_GREP_COLORS ; i ++ )
162
+ color_set (opt -> colors [i ], def -> colors [i ]);
171
163
}
172
164
173
165
static void grep_set_pattern_type_option (enum grep_pattern_type pattern_type , struct grep_opt * opt )
@@ -1100,12 +1092,12 @@ static void output_sep(struct grep_opt *opt, char sign)
1100
1092
if (opt -> null_following_name )
1101
1093
opt -> output (opt , "\0" , 1 );
1102
1094
else
1103
- output_color (opt , & sign , 1 , opt -> color_sep );
1095
+ output_color (opt , & sign , 1 , opt -> colors [ GREP_COLOR_SEP ] );
1104
1096
}
1105
1097
1106
1098
static void show_name (struct grep_opt * opt , const char * name )
1107
1099
{
1108
- output_color (opt , name , strlen (name ), opt -> color_filename );
1100
+ output_color (opt , name , strlen (name ), opt -> colors [ GREP_COLOR_FILENAME ] );
1109
1101
opt -> output (opt , opt -> null_following_name ? "\0" : "\n" , 1 );
1110
1102
}
1111
1103
@@ -1372,28 +1364,28 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1372
1364
} else if (opt -> pre_context || opt -> post_context || opt -> funcbody ) {
1373
1365
if (opt -> last_shown == 0 ) {
1374
1366
if (opt -> show_hunk_mark ) {
1375
- output_color (opt , "--" , 2 , opt -> color_sep );
1367
+ output_color (opt , "--" , 2 , opt -> colors [ GREP_COLOR_SEP ] );
1376
1368
opt -> output (opt , "\n" , 1 );
1377
1369
}
1378
1370
} else if (lno > opt -> last_shown + 1 ) {
1379
- output_color (opt , "--" , 2 , opt -> color_sep );
1371
+ output_color (opt , "--" , 2 , opt -> colors [ GREP_COLOR_SEP ] );
1380
1372
opt -> output (opt , "\n" , 1 );
1381
1373
}
1382
1374
}
1383
1375
if (opt -> heading && opt -> last_shown == 0 ) {
1384
- output_color (opt , name , strlen (name ), opt -> color_filename );
1376
+ output_color (opt , name , strlen (name ), opt -> colors [ GREP_COLOR_FILENAME ] );
1385
1377
opt -> output (opt , "\n" , 1 );
1386
1378
}
1387
1379
opt -> last_shown = lno ;
1388
1380
1389
1381
if (!opt -> heading && opt -> pathname ) {
1390
- output_color (opt , name , strlen (name ), opt -> color_filename );
1382
+ output_color (opt , name , strlen (name ), opt -> colors [ GREP_COLOR_FILENAME ] );
1391
1383
output_sep (opt , sign );
1392
1384
}
1393
1385
if (opt -> linenum ) {
1394
1386
char buf [32 ];
1395
1387
xsnprintf (buf , sizeof (buf ), "%d" , lno );
1396
- output_color (opt , buf , strlen (buf ), opt -> color_lineno );
1388
+ output_color (opt , buf , strlen (buf ), opt -> colors [ GREP_COLOR_LINENO ] );
1397
1389
output_sep (opt , sign );
1398
1390
}
1399
1391
if (opt -> color ) {
@@ -1403,15 +1395,15 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1403
1395
int eflags = 0 ;
1404
1396
1405
1397
if (sign == ':' )
1406
- match_color = opt -> color_match_selected ;
1398
+ match_color = opt -> colors [ GREP_COLOR_MATCH_SELECTED ] ;
1407
1399
else
1408
- match_color = opt -> color_match_context ;
1400
+ match_color = opt -> colors [ GREP_COLOR_MATCH_CONTEXT ] ;
1409
1401
if (sign == ':' )
1410
- line_color = opt -> color_selected ;
1402
+ line_color = opt -> colors [ GREP_COLOR_SELECTED ] ;
1411
1403
else if (sign == '-' )
1412
- line_color = opt -> color_context ;
1404
+ line_color = opt -> colors [ GREP_COLOR_CONTEXT ] ;
1413
1405
else if (sign == '=' )
1414
- line_color = opt -> color_function ;
1406
+ line_color = opt -> colors [ GREP_COLOR_FUNCTION ] ;
1415
1407
* eol = '\0' ;
1416
1408
while (next_match (opt , bol , eol , ctx , & match , eflags )) {
1417
1409
if (match .rm_so == match .rm_eo )
@@ -1818,7 +1810,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1818
1810
if (binary_match_only ) {
1819
1811
opt -> output (opt , "Binary file " , 12 );
1820
1812
output_color (opt , gs -> name , strlen (gs -> name ),
1821
- opt -> color_filename );
1813
+ opt -> colors [ GREP_COLOR_FILENAME ] );
1822
1814
opt -> output (opt , " matches\n" , 9 );
1823
1815
return 1 ;
1824
1816
}
@@ -1892,7 +1884,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1892
1884
char buf [32 ];
1893
1885
if (opt -> pathname ) {
1894
1886
output_color (opt , gs -> name , strlen (gs -> name ),
1895
- opt -> color_filename );
1887
+ opt -> colors [ GREP_COLOR_FILENAME ] );
1896
1888
output_sep (opt , ':' );
1897
1889
}
1898
1890
xsnprintf (buf , sizeof (buf ), "%u\n" , count );
0 commit comments