Skip to content

Commit 5dd06d3

Browse files
René Scharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8cfe5f1 commit 5dd06d3

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

grep.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,12 @@ 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) {
494+
if (opt->last_shown && lno > opt->last_shown + 1)
495+
fputs("--\n", stdout);
496+
}
497+
opt->last_shown = lno;
498+
493499
if (opt->null_following_name)
494500
sign = '\0';
495501
if (opt->pathname)
@@ -531,12 +537,12 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
531537
char *eol;
532538
} *prev = NULL, *pcl;
533539
unsigned last_hit = 0;
534-
unsigned last_shown = 0;
535540
int binary_match_only = 0;
536-
const char *hunk_mark = "";
537541
unsigned count = 0;
538542
enum grep_context ctx = GREP_CONTEXT_HEAD;
539543

544+
opt->last_shown = 0;
545+
540546
if (buffer_is_binary(buf, size)) {
541547
switch (opt->binary) {
542548
case GREP_BINARY_DEFAULT:
@@ -552,8 +558,6 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
552558

553559
if (opt->pre_context)
554560
prev = xcalloc(opt->pre_context, sizeof(*prev));
555-
if (opt->pre_context || opt->post_context)
556-
hunk_mark = "--\n";
557561

558562
while (left) {
559563
char *eol, ch;
@@ -607,33 +611,25 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
607611
from = lno - opt->pre_context;
608612
else
609613
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;
614616
while (from < lno) {
615617
pcl = &prev[lno-from-1];
616618
show_line(opt, pcl->bol, pcl->eol,
617619
name, from, '-');
618620
from++;
619621
}
620-
last_shown = lno-1;
621622
}
622-
if (last_shown && lno != last_shown + 1)
623-
fputs(hunk_mark, stdout);
624623
if (!opt->count)
625624
show_line(opt, bol, eol, name, lno, ':');
626-
last_shown = last_hit = lno;
625+
last_hit = lno;
627626
}
628627
else if (last_hit &&
629628
lno <= last_hit + opt->post_context) {
630629
/* If the last hit is within the post context,
631630
* we need to show this line.
632631
*/
633-
if (last_shown && lno != last_shown + 1)
634-
fputs(hunk_mark, stdout);
635632
show_line(opt, bol, eol, name, lno, '-');
636-
last_shown = lno;
637633
}
638634
if (opt->pre_context) {
639635
memmove(prev+1, prev,

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct grep_opt {
8484
int regflags;
8585
unsigned pre_context;
8686
unsigned post_context;
87+
unsigned last_shown;
8788
};
8889

8990
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);

0 commit comments

Comments
 (0)