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 <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
017c0fcfdb
commit
89252cd069
33
grep.c
33
grep.c
@ -1405,7 +1405,7 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void show_line(struct grep_opt *opt, char *bol, char *eol,
|
static void show_line(struct grep_opt *opt, char *bol, char *eol,
|
||||||
const char *name, unsigned lno, char sign)
|
const char *name, unsigned lno, ssize_t cno, char sign)
|
||||||
{
|
{
|
||||||
int rest = eol - bol;
|
int rest = eol - bol;
|
||||||
const char *match_color, *line_color = NULL;
|
const char *match_color, *line_color = NULL;
|
||||||
@ -1440,6 +1440,17 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
|
|||||||
output_color(opt, buf, strlen(buf), opt->color_lineno);
|
output_color(opt, buf, strlen(buf), opt->color_lineno);
|
||||||
output_sep(opt, sign);
|
output_sep(opt, sign);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* Treat 'cno' as the 1-indexed offset from the start of a non-context
|
||||||
|
* line to its first match. Otherwise, 'cno' is 0 indicating that we are
|
||||||
|
* being called with a context line.
|
||||||
|
*/
|
||||||
|
if (opt->columnnum && cno) {
|
||||||
|
char buf[32];
|
||||||
|
xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno);
|
||||||
|
output_color(opt, buf, strlen(buf), opt->color_columnno);
|
||||||
|
output_sep(opt, sign);
|
||||||
|
}
|
||||||
if (opt->color) {
|
if (opt->color) {
|
||||||
regmatch_t match;
|
regmatch_t match;
|
||||||
enum grep_context ctx = GREP_CONTEXT_BODY;
|
enum grep_context ctx = GREP_CONTEXT_BODY;
|
||||||
@ -1545,7 +1556,7 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
if (match_funcname(opt, gs, bol, eol)) {
|
if (match_funcname(opt, gs, bol, eol)) {
|
||||||
show_line(opt, bol, eol, gs->name, lno, '=');
|
show_line(opt, bol, eol, gs->name, lno, 0, '=');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1610,7 +1621,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
|
|||||||
|
|
||||||
while (*eol != '\n')
|
while (*eol != '\n')
|
||||||
eol++;
|
eol++;
|
||||||
show_line(opt, bol, eol, gs->name, cur, sign);
|
show_line(opt, bol, eol, gs->name, cur, 0, sign);
|
||||||
bol = eol + 1;
|
bol = eol + 1;
|
||||||
cur++;
|
cur++;
|
||||||
}
|
}
|
||||||
@ -1809,6 +1820,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
|
|||||||
while (left) {
|
while (left) {
|
||||||
char *eol, ch;
|
char *eol, ch;
|
||||||
int hit;
|
int hit;
|
||||||
|
ssize_t cno;
|
||||||
ssize_t col = -1, icol = -1;
|
ssize_t col = -1, icol = -1;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1874,7 +1886,18 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
|
|||||||
show_pre_context(opt, gs, bol, eol, lno);
|
show_pre_context(opt, gs, bol, eol, lno);
|
||||||
else if (opt->funcname)
|
else if (opt->funcname)
|
||||||
show_funcname_line(opt, gs, bol, lno);
|
show_funcname_line(opt, gs, bol, lno);
|
||||||
show_line(opt, bol, eol, gs->name, lno, ':');
|
cno = opt->invert ? icol : col;
|
||||||
|
if (cno < 0) {
|
||||||
|
/*
|
||||||
|
* A negative cno indicates that there was no
|
||||||
|
* match on the line. We are thus inverted and
|
||||||
|
* being asked to show all lines that _don't_
|
||||||
|
* match a given expression. Therefore, set cno
|
||||||
|
* to 0 to suggest the whole line matches.
|
||||||
|
*/
|
||||||
|
cno = 0;
|
||||||
|
}
|
||||||
|
show_line(opt, bol, eol, gs->name, lno, cno + 1, ':');
|
||||||
last_hit = lno;
|
last_hit = lno;
|
||||||
if (opt->funcbody)
|
if (opt->funcbody)
|
||||||
show_function = 1;
|
show_function = 1;
|
||||||
@ -1903,7 +1926,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
|
|||||||
/* If the last hit is within the post context,
|
/* If the last hit is within the post context,
|
||||||
* we need to show this line.
|
* we need to show this line.
|
||||||
*/
|
*/
|
||||||
show_line(opt, bol, eol, gs->name, lno, '-');
|
show_line(opt, bol, eol, gs->name, lno, col + 1, '-');
|
||||||
}
|
}
|
||||||
|
|
||||||
next_line:
|
next_line:
|
||||||
|
Reference in New Issue
Block a user