summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTaylor Blau <me@ttaylorr.com>2018-06-22 15:49:42 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-06-22 19:59:02 (GMT)
commit89252cd069d4acf1c6c407f00c7a1cc2c6ad3953 (patch)
tree5c86a2621a6d341aa27e8bbe8f9885f39cd2115b
parent017c0fcfdb21dd44e2c83f533e9a6d78513e7d8c (diff)
downloadgit-89252cd069d4acf1c6c407f00c7a1cc2c6ad3953.zip
git-89252cd069d4acf1c6c407f00c7a1cc2c6ad3953.tar.gz
git-89252cd069d4acf1c6c407f00c7a1cc2c6ad3953.tar.bz2
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>
-rw-r--r--grep.c33
1 files changed, 28 insertions, 5 deletions
diff --git a/grep.c b/grep.c
index c885101..83fe32a 100644
--- a/grep.c
+++ b/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,
- const char *name, unsigned lno, char sign)
+ const char *name, unsigned lno, ssize_t cno, char sign)
{
int rest = eol - bol;
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_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) {
regmatch_t match;
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;
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;
}
}
@@ -1610,7 +1621,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
while (*eol != '\n')
eol++;
- show_line(opt, bol, eol, gs->name, cur, sign);
+ show_line(opt, bol, eol, gs->name, cur, 0, sign);
bol = eol + 1;
cur++;
}
@@ -1809,6 +1820,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
while (left) {
char *eol, ch;
int hit;
+ ssize_t cno;
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);
else if (opt->funcname)
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;
if (opt->funcbody)
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,
* 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: