You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all thank you for the amazing tutorial! I came across an issue in search feature. When we press the arrow keys to find the next match, we move to the next or previous rows. But, there might be matches in the same row. Also, when we search in the reverse direction the match in the last position must be found. I tried implementing this by adding a new function editorFindRowLastMatch( ) and making changes to editorFindCallback( ) and it seems to work.
/*** find ***/inteditorFindRowLastMatch(erow*row, intstart, intend, char*query) //find position of last match in a row
{
intqlen=strlen(query);
while (end >= start)
{
if (row->render[end] ==query[0])
{
if (!strncmp(&row->render[end], query, qlen))
returnend;
}
end--;
}
return-1;
}
voideditorFindCallback(char*query, intkey) {
staticintlast_match=-1;
staticintdirection=1;
staticintsaved_hl_line;
staticchar*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;
direction=1;
return;
}
elseif (key==ARROW_RIGHT||key==ARROW_DOWN) {
direction=1;
}
elseif (key==ARROW_LEFT||key==ARROW_UP) {
direction=-1;
}
else {
last_match=-1;
direction=1;
}
if (last_match==-1) direction=1;
intcurrent=last_match;
intrx=-1; //this variable will contain the postion of the matchintsame_row=0; //if the match is found in the same rowif (current!=-1) //we check in the same row if there was a previous match
{
erow*row=&E.row[current];
if (direction==1)
{
char*match=strstr(&row->render[E.rx+1], query); //find a match after the previous match position(E.rx) in the same row if (match)
{
rx=match-row->render;
E.cx=editorRowRxToCx(row, rx);
same_row=1;
}
}
else//if in reverse direction
{
intlfound=editorFindRowLastMatch(row, 0, E.rx-1, query); //find the first match before previous match in the same rowif (lfound!=-1)
{
rx=lfound;
E.cx=editorRowRxToCx(row, rx);
same_row=1;
}
}
}
if (!same_row) // if match was not found in the same_row
{
inti;
for (i=0; i<E.numrows; i++)
{
current+=direction;
if (current==-1) current=E.numrows-1;
elseif (current==E.numrows) current=0;
erow*row=&E.row[current];
char*match=strstr(row->render, query);
if (match)
{
last_match=current;
E.cy=current;
rx=match-row->render;
if (direction==-1) //if match found and the direction is reverse we find the last match in that row
{
intlfound=editorFindRowLastMatch(row, rx+1, row->rsize-strlen(query), query);
if (lfound!=-1)
{
rx=lfound;
}
}
E.cx=editorRowRxToCx(row, rx);
E.rowoff=E.numrows;
break;
}
}
}
if (rx!=-1) // highlight only when match is found
{
erow*row=&E.row[current];
saved_hl_line=current;
saved_hl=malloc(row->rsize);
memcpy(saved_hl, row->hl, row->rsize);
memset(&row->hl[rx], HL_MATCH, strlen(query));
}
}
The text was updated successfully, but these errors were encountered:
First of all thank you for the amazing tutorial! I came across an issue in search feature. When we press the arrow keys to find the next match, we move to the next or previous rows. But, there might be matches in the same row. Also, when we search in the reverse direction the match in the last position must be found. I tried implementing this by adding a new function
editorFindRowLastMatch( )
and making changes toeditorFindCallback( )
and it seems to work.The text was updated successfully, but these errors were encountered: