@matheusbsbs / matheus-bagliano / commits / 5dd06d3879
Fork 0

grep: move context hunk mark handling into show_line()

Move last_shown into struct grep_opt, to make it available in show_line(), and then make the function handle the printing of hunk marks for context lines in a central place. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 2, 2009 at 00:02 UTC 5dd06d38795b2f6d05f851b25c814732238f8df1
2 files changed +12 -15
grep.c
+11 -15
@@ -490,6 +490,12 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
490 {
491 int rest = eol - bol;
492
493 + if (opt->pre_context || opt->post_context) {
494 + if (opt->last_shown && lno > opt->last_shown + 1)
495 + fputs("--\n", stdout);
496 + }
497 + opt->last_shown = lno;
498 +
499 if (opt->null_following_name)
500 sign = '\0';
501 if (opt->pathname)
@@ -531,12 +537,12 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
537 char *eol;
538 } *prev = NULL, *pcl;
539 unsigned last_hit = 0;
534 - unsigned last_shown = 0;
540 int binary_match_only = 0;
536 - const char *hunk_mark = "";
541 unsigned count = 0;
542 enum grep_context ctx = GREP_CONTEXT_HEAD;
543
544 + opt->last_shown = 0;
545 +
546 if (buffer_is_binary(buf, size)) {
547 switch (opt->binary) {
548 case GREP_BINARY_DEFAULT:
@@ -552,8 +558,6 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
558
559 if (opt->pre_context)
560 prev = xcalloc(opt->pre_context, sizeof(*prev));
555 - if (opt->pre_context || opt->post_context)
556 - hunk_mark = "--\n";
561
562 while (left) {
563 char *eol, ch;
@@ -607,33 +611,25 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
611 from = lno - opt->pre_context;
612 else
613 from = 1;
610 - if (from <= last_shown)
611 - from = last_shown + 1;
612 - if (last_shown && from != last_shown + 1)
613 - fputs(hunk_mark, stdout);
614 + if (from <= opt->last_shown)
615 + from = opt->last_shown + 1;
616 while (from < lno) {
617 pcl = &prev[lno-from-1];
618 show_line(opt, pcl->bol, pcl->eol,
619 name, from, '-');
620 from++;
621 }
620 - last_shown = lno-1;
622 }
622 - if (last_shown && lno != last_shown + 1)
623 - fputs(hunk_mark, stdout);
623 if (!opt->count)
624 show_line(opt, bol, eol, name, lno, ':');
626 - last_shown = last_hit = lno;
625 + last_hit = lno;
626 }
627 else if (last_hit &&
628 lno <= last_hit + opt->post_context) {
629 /* If the last hit is within the post context,
630 * we need to show this line.
631 */
633 - if (last_shown && lno != last_shown + 1)
634 - fputs(hunk_mark, stdout);
632 show_line(opt, bol, eol, name, lno, '-');
636 - last_shown = lno;
633 }
634 if (opt->pre_context) {
635 memmove(prev+1, prev,
grep.h
+1
@@ -84,6 +84,7 @@ struct grep_opt {
84 int regflags;
85 unsigned pre_context;
86 unsigned post_context;
87 + unsigned last_shown;
88 };
89
90 extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);