Highlight only the current search match

This commit is contained in:
Timothy Warren 2019-08-20 16:04:39 -04:00
parent dc71f7f9a3
commit 600a5896d1

21
kilo.c
View File

@ -40,7 +40,8 @@ enum editorKey {
enum editorHighlight { enum editorHighlight {
HL_NORMAL = 0, HL_NORMAL = 0,
HL_NUMBER HL_NUMBER,
HL_MATCH
}; };
/*** data ***/ /*** data ***/
@ -280,6 +281,9 @@ int editorSyntaxToColor(int hl)
case HL_NUMBER: case HL_NUMBER:
return 31; return 31;
case HL_MATCH:
return 34;
default: default:
return 37; return 37;
} }
@ -599,6 +603,16 @@ void editorFindCallback(char *query, int key)
static int last_match = -1; static int last_match = -1;
static int direction = 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') if (key == '\r' || key == '\x1b')
{ {
last_match = -1; last_match = -1;
@ -645,6 +659,11 @@ void editorFindCallback(char *query, int key)
E.cy = current; E.cy = current;
E.cx = editorRowRxToCx(row, match - row->render); E.cx = editorRowRxToCx(row, match - row->render);
E.rowoff = E.numrows; 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; break;
} }
} }