@@ -13,6 +13,17 @@ static int grep_source_is_binary(struct grep_source *gs);
1313
1414static struct grep_opt grep_defaults ;
1515
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+
1627static void std_output (struct grep_opt * opt , const void * buf , size_t size )
1728{
1829 fwrite (buf , size , 1 , stdout );
@@ -42,14 +53,14 @@ void init_grep_defaults(void)
4253 opt -> pathname = 1 ;
4354 opt -> max_depth = -1 ;
4455 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 );
5364 opt -> color = -1 ;
5465 opt -> output = std_output ;
5566}
@@ -76,7 +87,7 @@ static int parse_pattern_type_arg(const char *opt, const char *arg)
7687int grep_config (const char * var , const char * value , void * cb )
7788{
7889 struct grep_opt * opt = & grep_defaults ;
79- char * color = NULL ;
90+ const char * slot ;
8091
8192 if (userdiff_config (var , value ) < 0 )
8293 return -1 ;
@@ -103,32 +114,18 @@ int grep_config(const char *var, const char *value, void *cb)
103114
104115 if (!strcmp (var , "color.grep" ))
105116 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 ];
132129 if (!value )
133130 return config_error_nonbool (var );
134131 return color_parse (value , color );
@@ -144,6 +141,7 @@ int grep_config(const char *var, const char *value, void *cb)
144141void grep_init (struct grep_opt * opt , const char * prefix )
145142{
146143 struct grep_opt * def = & grep_defaults ;
144+ int i ;
147145
148146 memset (opt , 0 , sizeof (* opt ));
149147 opt -> prefix = prefix ;
@@ -160,14 +158,8 @@ void grep_init(struct grep_opt *opt, const char *prefix)
160158 opt -> relative = def -> relative ;
161159 opt -> output = def -> output ;
162160
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 ]);
171163}
172164
173165static 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)
11001092 if (opt -> null_following_name )
11011093 opt -> output (opt , "\0" , 1 );
11021094 else
1103- output_color (opt , & sign , 1 , opt -> color_sep );
1095+ output_color (opt , & sign , 1 , opt -> colors [ GREP_COLOR_SEP ] );
11041096}
11051097
11061098static void show_name (struct grep_opt * opt , const char * name )
11071099{
1108- output_color (opt , name , strlen (name ), opt -> color_filename );
1100+ output_color (opt , name , strlen (name ), opt -> colors [ GREP_COLOR_FILENAME ] );
11091101 opt -> output (opt , opt -> null_following_name ? "\0" : "\n" , 1 );
11101102}
11111103
@@ -1372,28 +1364,28 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
13721364 } else if (opt -> pre_context || opt -> post_context || opt -> funcbody ) {
13731365 if (opt -> last_shown == 0 ) {
13741366 if (opt -> show_hunk_mark ) {
1375- output_color (opt , "--" , 2 , opt -> color_sep );
1367+ output_color (opt , "--" , 2 , opt -> colors [ GREP_COLOR_SEP ] );
13761368 opt -> output (opt , "\n" , 1 );
13771369 }
13781370 } 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 ] );
13801372 opt -> output (opt , "\n" , 1 );
13811373 }
13821374 }
13831375 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 ] );
13851377 opt -> output (opt , "\n" , 1 );
13861378 }
13871379 opt -> last_shown = lno ;
13881380
13891381 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 ] );
13911383 output_sep (opt , sign );
13921384 }
13931385 if (opt -> linenum ) {
13941386 char buf [32 ];
13951387 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 ] );
13971389 output_sep (opt , sign );
13981390 }
13991391 if (opt -> color ) {
@@ -1403,15 +1395,15 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
14031395 int eflags = 0 ;
14041396
14051397 if (sign == ':' )
1406- match_color = opt -> color_match_selected ;
1398+ match_color = opt -> colors [ GREP_COLOR_MATCH_SELECTED ] ;
14071399 else
1408- match_color = opt -> color_match_context ;
1400+ match_color = opt -> colors [ GREP_COLOR_MATCH_CONTEXT ] ;
14091401 if (sign == ':' )
1410- line_color = opt -> color_selected ;
1402+ line_color = opt -> colors [ GREP_COLOR_SELECTED ] ;
14111403 else if (sign == '-' )
1412- line_color = opt -> color_context ;
1404+ line_color = opt -> colors [ GREP_COLOR_CONTEXT ] ;
14131405 else if (sign == '=' )
1414- line_color = opt -> color_function ;
1406+ line_color = opt -> colors [ GREP_COLOR_FUNCTION ] ;
14151407 * eol = '\0' ;
14161408 while (next_match (opt , bol , eol , ctx , & match , eflags )) {
14171409 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
18181810 if (binary_match_only ) {
18191811 opt -> output (opt , "Binary file " , 12 );
18201812 output_color (opt , gs -> name , strlen (gs -> name ),
1821- opt -> color_filename );
1813+ opt -> colors [ GREP_COLOR_FILENAME ] );
18221814 opt -> output (opt , " matches\n" , 9 );
18231815 return 1 ;
18241816 }
@@ -1892,7 +1884,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
18921884 char buf [32 ];
18931885 if (opt -> pathname ) {
18941886 output_color (opt , gs -> name , strlen (gs -> name ),
1895- opt -> color_filename );
1887+ opt -> colors [ GREP_COLOR_FILENAME ] );
18961888 output_sep (opt , ':' );
18971889 }
18981890 xsnprintf (buf , sizeof (buf ), "%u\n" , count );
0 commit comments