git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/6] grep: move context hunk mark handling into show_line()

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Jul 1, 2009, 22:02 UTC
Message-ID
<4A4BDCFE.3050808@lsrfire.ath.cx>
In-Reply-To
<4A4BDC65.80504@lsrfire.ath.cx>

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>
---
 grep.c |   26 +++++++++++---------------
 grep.h |    1 +
 2 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/grep.c b/grep.c
index 92a47c7..6ee80f7 100644
--- a/grep.c
+++ b/grep.c
@@ -490,6 +490,12 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 {
 	int rest = eol - bol;
 
+	if (opt->pre_context || opt->post_context) {
+		if (opt->last_shown && lno > opt->last_shown + 1)
+			fputs("--\n", stdout);
+	}
+	opt->last_shown = lno;
+
 	if (opt->null_following_name)
 		sign = '\0';
 	if (opt->pathname)
@@ -531,12 +537,12 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 		char *eol;
 	} *prev = NULL, *pcl;
 	unsigned last_hit = 0;
-	unsigned last_shown = 0;
 	int binary_match_only = 0;
-	const char *hunk_mark = "";
 	unsigned count = 0;
 	enum grep_context ctx = GREP_CONTEXT_HEAD;
 
+	opt->last_shown = 0;
+
 	if (buffer_is_binary(buf, size)) {
 		switch (opt->binary) {
 		case GREP_BINARY_DEFAULT:
@@ -552,8 +558,6 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 
 	if (opt->pre_context)
 		prev = xcalloc(opt->pre_context, sizeof(*prev));
-	if (opt->pre_context || opt->post_context)
-		hunk_mark = "--\n";
 
 	while (left) {
 		char *eol, ch;
@@ -607,33 +611,25 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
 					from = lno - opt->pre_context;
 				else
 					from = 1;
-				if (from <= last_shown)
-					from = last_shown + 1;
-				if (last_shown && from != last_shown + 1)
-					fputs(hunk_mark, stdout);
+				if (from <= opt->last_shown)
+					from = opt->last_shown + 1;
 				while (from < lno) {
 					pcl = &prev[lno-from-1];
 					show_line(opt, pcl->bol, pcl->eol,
 						  name, from, '-');
 					from++;
 				}
-				last_shown = lno-1;
 			}
-			if (last_shown && lno != last_shown + 1)
-				fputs(hunk_mark, stdout);
 			if (!opt->count)
 				show_line(opt, bol, eol, name, lno, ':');
-			last_shown = last_hit = lno;
+			last_hit = lno;
 		}
 		else if (last_hit &&
 			 lno <= last_hit + opt->post_context) {
 			/* If the last hit is within the post context,
 			 * we need to show this line.
 			 */
-			if (last_shown && lno != last_shown + 1)
-				fputs(hunk_mark, stdout);
 			show_line(opt, bol, eol, name, lno, '-');
-			last_shown = lno;
 		}
 		if (opt->pre_context) {
 			memmove(prev+1, prev,
diff --git a/grep.h b/grep.h
index 464e272..0883214 100644
--- a/grep.h
+++ b/grep.h
@@ -84,6 +84,7 @@ struct grep_opt {
 	int regflags;
 	unsigned pre_context;
 	unsigned post_context;
+	unsigned last_shown;
 };
 
 extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
-- 
1.6.3.3
Previous: René ScharfeNext: Junio C Hamano
Message 3 of 15 in “grep: add option -p/--show-function, similar to diff's”
  1. 0/6 grep: add option -p/--show-function, similar to diff'sRené Scharfe, Jul 1, 2009
  2. 1/6 userdiff: add xdiff_clear_find_func()René Scharfe, Jul 1, 2009
  3. 2/6 grep: move context hunk mark handling into show_line()René Scharfe, Jul 1, 2009
  4. Junio C HamanoJul 1, 2009
  5. René ScharfeJul 2, 2009
  6. Junio C HamanoJul 2, 2009
  7. 3/6 grep: print context hunk marks between filesRené Scharfe, Jul 1, 2009
  8. 4/6 grep: handle pre context lines on demandRené Scharfe, Jul 1, 2009
  9. 5/6 grep: add option -p/--show-functionRené Scharfe, Jul 1, 2009
  10. Junio C HamanoJul 2, 2009
  11. René ScharfeJul 2, 2009
  12. Junio C HamanoJul 2, 2009
  13. René ScharfeJul 2, 2009
  14. René ScharfeJul 2, 2009
  15. 6/6 grep -p: support user defined regular expressionsRené Scharfe, Jul 1, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.