@@ -490,14 +490,18 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
490490{
491491 int rest = eol - bol ;
492492
493- if (opt -> pre_context || opt -> post_context ) {
493+ if (opt -> pre_context || opt -> post_context || opt -> funcname ) {
494494 if (opt -> last_shown == 0 ) {
495495 if (opt -> show_hunk_mark )
496- fputs ("--\n" , stdout );
496+ fputs (opt -> funcname ? "==\n" : "--\n" , stdout );
497497 else
498498 opt -> show_hunk_mark = 1 ;
499- } else if (lno > opt -> last_shown + 1 )
500- fputs ("--\n" , stdout );
499+ } else if (lno > opt -> last_shown + 1 ) {
500+ if (opt -> pre_context || opt -> post_context )
501+ fputs ((sign == '=' ) ? "==\n" : "--\n" , stdout );
502+ else if (sign == '=' )
503+ fputs ("==\n" , stdout );
504+ }
501505 }
502506 opt -> last_shown = lno ;
503507
@@ -531,10 +535,40 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
531535 printf ("%.*s\n" , rest , bol );
532536}
533537
538+ static int match_funcname (char * bol , char * eol )
539+ {
540+ if (bol == eol )
541+ return 0 ;
542+ if (isalpha (* bol ) || * bol == '_' || * bol == '$' )
543+ return 1 ;
544+ return 0 ;
545+ }
546+
547+ static void show_funcname_line (struct grep_opt * opt , const char * name ,
548+ char * buf , char * bol , unsigned lno )
549+ {
550+ while (bol > buf ) {
551+ char * eol = -- bol ;
552+
553+ while (bol > buf && bol [-1 ] != '\n' )
554+ bol -- ;
555+ lno -- ;
556+
557+ if (lno <= opt -> last_shown )
558+ break ;
559+
560+ if (match_funcname (bol , eol )) {
561+ show_line (opt , bol , eol , name , lno , '=' );
562+ break ;
563+ }
564+ }
565+ }
566+
534567static void show_pre_context (struct grep_opt * opt , const char * name , char * buf ,
535568 char * bol , unsigned lno )
536569{
537- unsigned cur = lno , from = 1 ;
570+ unsigned cur = lno , from = 1 , funcname_lno = 0 ;
571+ int funcname_needed = opt -> funcname ;
538572
539573 if (opt -> pre_context < lno )
540574 from = lno - opt -> pre_context ;
@@ -543,19 +577,28 @@ static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
543577
544578 /* Rewind. */
545579 while (bol > buf && cur > from ) {
546- bol -- ;
580+ char * eol = -- bol ;
581+
547582 while (bol > buf && bol [-1 ] != '\n' )
548583 bol -- ;
549584 cur -- ;
585+ if (funcname_needed && match_funcname (bol , eol )) {
586+ funcname_lno = cur ;
587+ funcname_needed = 0 ;
588+ }
550589 }
551590
591+ /* We need to look even further back to find a function signature. */
592+ if (opt -> funcname && funcname_needed )
593+ show_funcname_line (opt , name , buf , bol , cur );
594+
552595 /* Back forward. */
553596 while (cur < lno ) {
554- char * eol = bol ;
597+ char * eol = bol , sign = ( cur == funcname_lno ) ? '=' : '-' ;
555598
556599 while (* eol != '\n' )
557600 eol ++ ;
558- show_line (opt , bol , eol , name , cur , '-' );
601+ show_line (opt , bol , eol , name , cur , sign );
559602 bol = eol + 1 ;
560603 cur ++ ;
561604 }
@@ -635,6 +678,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
635678 */
636679 if (opt -> pre_context )
637680 show_pre_context (opt , name , buf , bol , lno );
681+ else if (opt -> funcname )
682+ show_funcname_line (opt , name , buf , bol , lno );
638683 if (!opt -> count )
639684 show_line (opt , bol , eol , name , lno , ':' );
640685 last_hit = lno ;
0 commit comments