51
color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
52
color_set(opt->color_selected, "");
53
color_set(opt->color_sep, GIT_COLOR_CYAN);
54
+ opt->only_matching = 0;
55
opt->color = -1;
56
opt->output = std_output;
57
}
159
opt->pattern_tail = &opt->pattern_list;
160
opt->header_tail = &opt->header_list;
161
162
+ opt->only_matching = def->only_matching;
163
opt->color = def->color;
164
opt->extended_regexp_option = def->extended_regexp_option;
165
opt->pattern_type_option = def->pattern_type_option;
1448
const char *name, unsigned lno, ssize_t cno, char sign)
1449
{
1450
int rest = eol - bol;
1449
- const char *match_color, *line_color = NULL;
1451
+ const char *match_color = NULL;
1452
+ const char *line_color = NULL;
1453
1454
if (opt->file_break && opt->last_shown == 0) {
1455
if (opt->show_hunk_mark)
1465
opt->output(opt, "\n", 1);
1466
}
1467
}
1465
- show_line_header(opt, name, lno, cno, sign);
1466
- if (opt->color) {
1468
+ if (!opt->only_matching) {
1469
+ /*
1470
+ * In case the line we're being called with contains more than
1471
+ * one match, leave printing each header to the loop below.
1472
+ */
1473
+ show_line_header(opt, name, lno, cno, sign);
1474
+ }
1475
+ if (opt->color || opt->only_matching) {
1476
regmatch_t match;
1477
enum grep_context ctx = GREP_CONTEXT_BODY;
1478
int ch = *eol;
1479
int eflags = 0;
1480
1472
- if (sign == ':')
1473
- match_color = opt->color_match_selected;
1474
- else
1475
- match_color = opt->color_match_context;
1476
- if (sign == ':')
1477
- line_color = opt->color_selected;
1478
- else if (sign == '-')
1479
- line_color = opt->color_context;
1480
- else if (sign == '=')
1481
- line_color = opt->color_function;
1481
+ if (opt->color) {
1482
+ if (sign == ':')
1483
+ match_color = opt->color_match_selected;
1484
+ else
1485
+ match_color = opt->color_match_context;
1486
+ if (sign == ':')
1487
+ line_color = opt->color_selected;
1488
+ else if (sign == '-')
1489
+ line_color = opt->color_context;
1490
+ else if (sign == '=')
1491
+ line_color = opt->color_function;
1492
+ }
1493
*eol = '\0';
1494
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1495
if (match.rm_so == match.rm_eo)
1496
break;
1497
1487
- output_color(opt, bol, match.rm_so, line_color);
1498
+ if (opt->only_matching)
1499
+ show_line_header(opt, name, lno, cno, sign);
1500
+ else
1501
+ output_color(opt, bol, match.rm_so, line_color);
1502
output_color(opt, bol + match.rm_so,
1503
match.rm_eo - match.rm_so, match_color);
1504
+ if (opt->only_matching)
1505
+ opt->output(opt, "\n", 1);
1506
bol += match.rm_eo;
1507
+ cno += match.rm_eo;
1508
rest -= match.rm_eo;
1509
eflags = REG_NOTBOL;
1510
}
1511
*eol = ch;
1512
}
1496
- output_color(opt, bol, rest, line_color);
1497
- opt->output(opt, "\n", 1);
1513
+ if (!opt->only_matching) {
1514
+ output_color(opt, bol, rest, line_color);
1515
+ opt->output(opt, "\n", 1);
1516
+ }
1517
}
1518
1519
#ifndef NO_PTHREADS