@@ -304,9 +304,28 @@ static int word_char(char ch)
304304 return isalnum (ch ) || ch == '_' ;
305305}
306306
307+ static void output_color (struct grep_opt * opt , const void * data , size_t size ,
308+ const char * color )
309+ {
310+ if (opt -> color && color && color [0 ]) {
311+ opt -> output (opt , color , strlen (color ));
312+ opt -> output (opt , data , size );
313+ opt -> output (opt , GIT_COLOR_RESET , strlen (GIT_COLOR_RESET ));
314+ } else
315+ opt -> output (opt , data , size );
316+ }
317+
318+ static void output_sep (struct grep_opt * opt , char sign )
319+ {
320+ if (opt -> null_following_name )
321+ opt -> output (opt , "\0" , 1 );
322+ else
323+ output_color (opt , & sign , 1 , opt -> color_sep );
324+ }
325+
307326static void show_name (struct grep_opt * opt , const char * name )
308327{
309- opt -> output (opt , name , strlen (name ));
328+ output_color (opt , name , strlen (name ), opt -> color_filename );
310329 opt -> output (opt , opt -> null_following_name ? "\0" : "\n" , 1 );
311330}
312331
@@ -544,57 +563,60 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
544563 const char * name , unsigned lno , char sign )
545564{
546565 int rest = eol - bol ;
547- char sign_str [ 1 ] ;
566+ char * line_color = NULL ;
548567
549- sign_str [0 ] = sign ;
550568 if (opt -> pre_context || opt -> post_context ) {
551569 if (opt -> last_shown == 0 ) {
552- if (opt -> show_hunk_mark )
553- opt -> output (opt , "--\n" , 3 );
554- else
570+ if (opt -> show_hunk_mark ) {
571+ output_color (opt , "--" , 2 , opt -> color_sep );
572+ opt -> output (opt , "\n" , 1 );
573+ } else
555574 opt -> show_hunk_mark = 1 ;
556- } else if (lno > opt -> last_shown + 1 )
557- opt -> output (opt , "--\n" , 3 );
575+ } else if (lno > opt -> last_shown + 1 ) {
576+ output_color (opt , "--" , 2 , opt -> color_sep );
577+ opt -> output (opt , "\n" , 1 );
578+ }
558579 }
559580 opt -> last_shown = lno ;
560581
561- if (opt -> null_following_name )
562- sign_str [0 ] = '\0' ;
563582 if (opt -> pathname ) {
564- opt -> output (opt , name , strlen (name ));
565- opt -> output (opt , sign_str , 1 );
583+ output_color (opt , name , strlen (name ), opt -> color_filename );
584+ output_sep (opt , sign );
566585 }
567586 if (opt -> linenum ) {
568587 char buf [32 ];
569588 snprintf (buf , sizeof (buf ), "%d" , lno );
570- opt -> output (opt , buf , strlen (buf ));
571- opt -> output (opt , sign_str , 1 );
589+ output_color (opt , buf , strlen (buf ), opt -> color_lineno );
590+ output_sep (opt , sign );
572591 }
573592 if (opt -> color ) {
574593 regmatch_t match ;
575594 enum grep_context ctx = GREP_CONTEXT_BODY ;
576595 int ch = * eol ;
577596 int eflags = 0 ;
578597
598+ if (sign == ':' )
599+ line_color = opt -> color_selected ;
600+ else if (sign == '-' )
601+ line_color = opt -> color_context ;
602+ else if (sign == '=' )
603+ line_color = opt -> color_function ;
579604 * eol = '\0' ;
580605 while (next_match (opt , bol , eol , ctx , & match , eflags )) {
581606 if (match .rm_so == match .rm_eo )
582607 break ;
583608
584- opt -> output (opt , bol , match .rm_so );
585- opt -> output (opt , opt -> color_match ,
586- strlen (opt -> color_match ));
587- opt -> output (opt , bol + match .rm_so ,
588- (int )(match .rm_eo - match .rm_so ));
589- opt -> output (opt , GIT_COLOR_RESET ,
590- strlen (GIT_COLOR_RESET ));
609+ output_color (opt , bol , match .rm_so , line_color );
610+ output_color (opt , bol + match .rm_so ,
611+ match .rm_eo - match .rm_so ,
612+ opt -> color_match );
591613 bol += match .rm_eo ;
592614 rest -= match .rm_eo ;
593615 eflags = REG_NOTBOL ;
594616 }
595617 * eol = ch ;
596618 }
597- opt -> output (opt , bol , rest );
619+ output_color (opt , bol , rest , line_color );
598620 opt -> output (opt , "\n" , 1 );
599621}
600622
@@ -857,7 +879,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
857879 return 1 ;
858880 if (binary_match_only ) {
859881 opt -> output (opt , "Binary file " , 12 );
860- opt -> output (opt , name , strlen (name ));
882+ output_color (opt , name , strlen (name ),
883+ opt -> color_filename );
861884 opt -> output (opt , " matches\n" , 9 );
862885 return 1 ;
863886 }
@@ -916,9 +939,9 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
916939 */
917940 if (opt -> count && count ) {
918941 char buf [32 ];
919- opt -> output (opt , name , strlen (name ));
920- snprintf ( buf , sizeof ( buf ), "%c%u\n" ,
921- opt -> null_following_name ? '\0' : ':' , count );
942+ output_color (opt , name , strlen (name ), opt -> color_filename );
943+ output_sep ( opt , ':' );
944+ snprintf ( buf , sizeof ( buf ), "%u\n" , count );
922945 opt -> output (opt , buf , strlen (buf ));
923946 }
924947 return !!last_hit ;
0 commit comments