@@ -65,6 +65,7 @@ void init_grep_defaults(void)
65
65
color_set (opt -> colors [GREP_COLOR_MATCH_SELECTED ], GIT_COLOR_BOLD_RED );
66
66
color_set (opt -> colors [GREP_COLOR_SELECTED ], "" );
67
67
color_set (opt -> colors [GREP_COLOR_SEP ], GIT_COLOR_CYAN );
68
+ opt -> only_matching = 0 ;
68
69
opt -> color = -1 ;
69
70
opt -> output = std_output ;
70
71
}
@@ -159,6 +160,7 @@ void grep_init(struct grep_opt *opt, const char *prefix)
159
160
opt -> pattern_tail = & opt -> pattern_list ;
160
161
opt -> header_tail = & opt -> header_list ;
161
162
163
+ opt -> only_matching = def -> only_matching ;
162
164
opt -> color = def -> color ;
163
165
opt -> extended_regexp_option = def -> extended_regexp_option ;
164
166
opt -> pattern_type_option = def -> pattern_type_option ;
@@ -1404,26 +1406,9 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
1404
1406
return hit ;
1405
1407
}
1406
1408
1407
- static void show_line (struct grep_opt * opt , char * bol , char * eol ,
1408
- const char * name , unsigned lno , ssize_t cno , char sign )
1409
+ static void show_line_header (struct grep_opt * opt , const char * name ,
1410
+ unsigned lno , ssize_t cno , char sign )
1409
1411
{
1410
- int rest = eol - bol ;
1411
- const char * match_color , * line_color = NULL ;
1412
-
1413
- if (opt -> file_break && opt -> last_shown == 0 ) {
1414
- if (opt -> show_hunk_mark )
1415
- opt -> output (opt , "\n" , 1 );
1416
- } else if (opt -> pre_context || opt -> post_context || opt -> funcbody ) {
1417
- if (opt -> last_shown == 0 ) {
1418
- if (opt -> show_hunk_mark ) {
1419
- output_color (opt , "--" , 2 , opt -> colors [GREP_COLOR_SEP ]);
1420
- opt -> output (opt , "\n" , 1 );
1421
- }
1422
- } else if (lno > opt -> last_shown + 1 ) {
1423
- output_color (opt , "--" , 2 , opt -> colors [GREP_COLOR_SEP ]);
1424
- opt -> output (opt , "\n" , 1 );
1425
- }
1426
- }
1427
1412
if (opt -> heading && opt -> last_shown == 0 ) {
1428
1413
output_color (opt , name , strlen (name ), opt -> colors [GREP_COLOR_FILENAME ]);
1429
1414
opt -> output (opt , "\n" , 1 );
@@ -1451,38 +1436,78 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1451
1436
output_color (opt , buf , strlen (buf ), opt -> colors [GREP_COLOR_COLUMNNO ]);
1452
1437
output_sep (opt , sign );
1453
1438
}
1454
- if (opt -> color ) {
1439
+ }
1440
+
1441
+ static void show_line (struct grep_opt * opt , char * bol , char * eol ,
1442
+ const char * name , unsigned lno , ssize_t cno , char sign )
1443
+ {
1444
+ int rest = eol - bol ;
1445
+ const char * match_color = NULL ;
1446
+ const char * line_color = NULL ;
1447
+
1448
+ if (opt -> file_break && opt -> last_shown == 0 ) {
1449
+ if (opt -> show_hunk_mark )
1450
+ opt -> output (opt , "\n" , 1 );
1451
+ } else if (opt -> pre_context || opt -> post_context || opt -> funcbody ) {
1452
+ if (opt -> last_shown == 0 ) {
1453
+ if (opt -> show_hunk_mark ) {
1454
+ output_color (opt , "--" , 2 , opt -> colors [GREP_COLOR_SEP ]);
1455
+ opt -> output (opt , "\n" , 1 );
1456
+ }
1457
+ } else if (lno > opt -> last_shown + 1 ) {
1458
+ output_color (opt , "--" , 2 , opt -> colors [GREP_COLOR_SEP ]);
1459
+ opt -> output (opt , "\n" , 1 );
1460
+ }
1461
+ }
1462
+ if (!opt -> only_matching ) {
1463
+ /*
1464
+ * In case the line we're being called with contains more than
1465
+ * one match, leave printing each header to the loop below.
1466
+ */
1467
+ show_line_header (opt , name , lno , cno , sign );
1468
+ }
1469
+ if (opt -> color || opt -> only_matching ) {
1455
1470
regmatch_t match ;
1456
1471
enum grep_context ctx = GREP_CONTEXT_BODY ;
1457
1472
int ch = * eol ;
1458
1473
int eflags = 0 ;
1459
1474
1460
- if (sign == ':' )
1461
- match_color = opt -> colors [GREP_COLOR_MATCH_SELECTED ];
1462
- else
1463
- match_color = opt -> colors [GREP_COLOR_MATCH_CONTEXT ];
1464
- if (sign == ':' )
1465
- line_color = opt -> colors [GREP_COLOR_SELECTED ];
1466
- else if (sign == '-' )
1467
- line_color = opt -> colors [GREP_COLOR_CONTEXT ];
1468
- else if (sign == '=' )
1469
- line_color = opt -> colors [GREP_COLOR_FUNCTION ];
1475
+ if (opt -> color ) {
1476
+ if (sign == ':' )
1477
+ match_color = opt -> colors [GREP_COLOR_MATCH_SELECTED ];
1478
+ else
1479
+ match_color = opt -> colors [GREP_COLOR_MATCH_CONTEXT ];
1480
+ if (sign == ':' )
1481
+ line_color = opt -> colors [GREP_COLOR_SELECTED ];
1482
+ else if (sign == '-' )
1483
+ line_color = opt -> colors [GREP_COLOR_CONTEXT ];
1484
+ else if (sign == '=' )
1485
+ line_color = opt -> colors [GREP_COLOR_FUNCTION ];
1486
+ }
1470
1487
* eol = '\0' ;
1471
1488
while (next_match (opt , bol , eol , ctx , & match , eflags )) {
1472
1489
if (match .rm_so == match .rm_eo )
1473
1490
break ;
1474
1491
1475
- output_color (opt , bol , match .rm_so , line_color );
1492
+ if (opt -> only_matching )
1493
+ show_line_header (opt , name , lno , cno , sign );
1494
+ else
1495
+ output_color (opt , bol , match .rm_so , line_color );
1476
1496
output_color (opt , bol + match .rm_so ,
1477
1497
match .rm_eo - match .rm_so , match_color );
1498
+ if (opt -> only_matching )
1499
+ opt -> output (opt , "\n" , 1 );
1478
1500
bol += match .rm_eo ;
1501
+ cno += match .rm_eo ;
1479
1502
rest -= match .rm_eo ;
1480
1503
eflags = REG_NOTBOL ;
1481
1504
}
1482
1505
* eol = ch ;
1483
1506
}
1484
- output_color (opt , bol , rest , line_color );
1485
- opt -> output (opt , "\n" , 1 );
1507
+ if (!opt -> only_matching ) {
1508
+ output_color (opt , bol , rest , line_color );
1509
+ opt -> output (opt , "\n" , 1 );
1510
+ }
1486
1511
}
1487
1512
1488
1513
#ifndef NO_PTHREADS
0 commit comments