From 600a5896d1ce6706f480a6346f3e3fe1c74c1b90 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 20 Aug 2019 16:04:39 -0400 Subject: [PATCH] Highlight only the current search match --- kilo.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/kilo.c b/kilo.c index cd23af4..ba59e67 100644 --- a/kilo.c +++ b/kilo.c @@ -40,7 +40,8 @@ enum editorKey { enum editorHighlight { HL_NORMAL = 0, - HL_NUMBER + HL_NUMBER, + HL_MATCH }; /*** data ***/ @@ -280,6 +281,9 @@ int editorSyntaxToColor(int hl) case HL_NUMBER: return 31; + case HL_MATCH: + return 34; + default: return 37; } @@ -599,6 +603,16 @@ void editorFindCallback(char *query, int key) static int last_match = -1; static int direction = 1; + static int saved_hl_line; + static char *saved_hl = NULL; + + if (saved_hl) + { + memcpy(E.row[saved_hl_line].hl, saved_hl, E.row[saved_hl_line].rsize); + free(saved_hl); + saved_hl = NULL; + } + if (key == '\r' || key == '\x1b') { last_match = -1; @@ -645,6 +659,11 @@ void editorFindCallback(char *query, int key) E.cy = current; E.cx = editorRowRxToCx(row, match - row->render); E.rowoff = E.numrows; + + saved_hl_line = current; + saved_hl = malloc(row->rsize); + memcpy(saved_hl, row->hl, row->rsize); + memset(&row->hl[match - row->render], HL_MATCH, strlen(query)); break; } }