Skip to content

Commit 89252cd

Browse files
ttaylorrgitster
authored andcommitted
grep.c: display column number of first match
To prepare for 'git grep' learning '--column', teach grep.c's show_line() how to show the column of the first match on non-context lines. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 017c0fc commit 89252cd

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

grep.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
14051405
}
14061406

14071407
static void show_line(struct grep_opt *opt, char *bol, char *eol,
1408-
const char *name, unsigned lno, char sign)
1408+
const char *name, unsigned lno, ssize_t cno, char sign)
14091409
{
14101410
int rest = eol - bol;
14111411
const char *match_color, *line_color = NULL;
@@ -1440,6 +1440,17 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
14401440
output_color(opt, buf, strlen(buf), opt->color_lineno);
14411441
output_sep(opt, sign);
14421442
}
1443+
/*
1444+
* Treat 'cno' as the 1-indexed offset from the start of a non-context
1445+
* line to its first match. Otherwise, 'cno' is 0 indicating that we are
1446+
* being called with a context line.
1447+
*/
1448+
if (opt->columnnum && cno) {
1449+
char buf[32];
1450+
xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno);
1451+
output_color(opt, buf, strlen(buf), opt->color_columnno);
1452+
output_sep(opt, sign);
1453+
}
14431454
if (opt->color) {
14441455
regmatch_t match;
14451456
enum grep_context ctx = GREP_CONTEXT_BODY;
@@ -1545,7 +1556,7 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
15451556
break;
15461557

15471558
if (match_funcname(opt, gs, bol, eol)) {
1548-
show_line(opt, bol, eol, gs->name, lno, '=');
1559+
show_line(opt, bol, eol, gs->name, lno, 0, '=');
15491560
break;
15501561
}
15511562
}
@@ -1610,7 +1621,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
16101621

16111622
while (*eol != '\n')
16121623
eol++;
1613-
show_line(opt, bol, eol, gs->name, cur, sign);
1624+
show_line(opt, bol, eol, gs->name, cur, 0, sign);
16141625
bol = eol + 1;
16151626
cur++;
16161627
}
@@ -1809,6 +1820,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
18091820
while (left) {
18101821
char *eol, ch;
18111822
int hit;
1823+
ssize_t cno;
18121824
ssize_t col = -1, icol = -1;
18131825

18141826
/*
@@ -1874,7 +1886,18 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
18741886
show_pre_context(opt, gs, bol, eol, lno);
18751887
else if (opt->funcname)
18761888
show_funcname_line(opt, gs, bol, lno);
1877-
show_line(opt, bol, eol, gs->name, lno, ':');
1889+
cno = opt->invert ? icol : col;
1890+
if (cno < 0) {
1891+
/*
1892+
* A negative cno indicates that there was no
1893+
* match on the line. We are thus inverted and
1894+
* being asked to show all lines that _don't_
1895+
* match a given expression. Therefore, set cno
1896+
* to 0 to suggest the whole line matches.
1897+
*/
1898+
cno = 0;
1899+
}
1900+
show_line(opt, bol, eol, gs->name, lno, cno + 1, ':');
18781901
last_hit = lno;
18791902
if (opt->funcbody)
18801903
show_function = 1;
@@ -1903,7 +1926,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
19031926
/* If the last hit is within the post context,
19041927
* we need to show this line.
19051928
*/
1906-
show_line(opt, bol, eol, gs->name, lno, '-');
1929+
show_line(opt, bol, eol, gs->name, lno, col + 1, '-');
19071930
}
19081931

19091932
next_line:

0 commit comments

Comments
 (0)