grep.c: extract show_line_header()

The grep code invokes show_line() to display the contents of a matched or context line in its output. Part of this execution is to print a line header that includes information such as the kind, the line- and column-number and etc. of that match. To prepare for the addition of an option to print only the matching component(s) of a non-context line, we must prepare for the possibility that a single line may contain multiple matching parts, and thus will need multiple headers printed for a single line. Extracting show_line_header allows us to do just that. In the subsequent commit, it will be used within the colorization loop to print out only the matching parts of a line, optionally with LFs delimiting sub-matches. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jul 3, 2018 at 16:51 UTC c707ded332375a5b51c1acf9b7ce1c29cbd4536d
1 file changed +25 -19
grep.c
+25 -19
@@ -1410,26 +1410,9 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
1410 return hit;
1411 }
1412
1413 -static void show_line(struct grep_opt *opt, char *bol, char *eol,
1414 - const char *name, unsigned lno, ssize_t cno, char sign)
1413 +static void show_line_header(struct grep_opt *opt, const char *name,
1414 + unsigned lno, ssize_t cno, char sign)
1415 {
1416 - int rest = eol - bol;
1417 - const char *match_color, *line_color = NULL;
1418 -
1419 - if (opt->file_break && opt->last_shown == 0) {
1420 - if (opt->show_hunk_mark)
1421 - opt->output(opt, "\n", 1);
1422 - } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1423 - if (opt->last_shown == 0) {
1424 - if (opt->show_hunk_mark) {
1425 - output_color(opt, "--", 2, opt->color_sep);
1426 - opt->output(opt, "\n", 1);
1427 - }
1428 - } else if (lno > opt->last_shown + 1) {
1429 - output_color(opt, "--", 2, opt->color_sep);
1430 - opt->output(opt, "\n", 1);
1431 - }
1432 - }
1416 if (opt->heading && opt->last_shown == 0) {
1417 output_color(opt, name, strlen(name), opt->color_filename);
1418 opt->output(opt, "\n", 1);
@@ -1457,6 +1440,29 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1440 output_color(opt, buf, strlen(buf), opt->color_columnno);
1441 output_sep(opt, sign);
1442 }
1443 +}
1444 +
1445 +static void show_line(struct grep_opt *opt, char *bol, char *eol,
1446 + const char *name, unsigned lno, ssize_t cno, char sign)
1447 +{
1448 + int rest = eol - bol;
1449 + const char *match_color, *line_color = NULL;
1450 +
1451 + if (opt->file_break && opt->last_shown == 0) {
1452 + if (opt->show_hunk_mark)
1453 + opt->output(opt, "\n", 1);
1454 + } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1455 + if (opt->last_shown == 0) {
1456 + if (opt->show_hunk_mark) {
1457 + output_color(opt, "--", 2, opt->color_sep);
1458 + opt->output(opt, "\n", 1);
1459 + }
1460 + } else if (lno > opt->last_shown + 1) {
1461 + output_color(opt, "--", 2, opt->color_sep);
1462 + opt->output(opt, "\n", 1);
1463 + }
1464 + }
1465 + show_line_header(opt, name, lno, cno, sign);
1466 if (opt->color) {
1467 regmatch_t match;
1468 enum grep_context ctx = GREP_CONTEXT_BODY;