find.c (2018B)
1 #include"find.h" 2 #include"jim.h" 3 #include"output.h" 4 #include"input.h" 5 #include"row.h" 6 #include"syntax.h" 7 #include<string.h> 8 #include<stdlib.h> 9 10 void editorFindCallback(char* query, int key) { 11 static int l_match = -1; 12 static int direction = 1; 13 14 static int saved_hl_line; 15 static char* saved_hl = NULL; 16 17 if(saved_hl) { 18 memcpy(E.row[saved_hl_line].hl, saved_hl, E.row[saved_hl_line].rsize); 19 free(saved_hl); 20 saved_hl = NULL; 21 } 22 23 switch(key) { 24 case ESCAPE: 25 case RETURN: 26 l_match = -1; 27 direction = 1; 28 return; 29 case CTRL_KEY(NEXT): 30 direction = 1; 31 break; 32 case CTRL_KEY(PREVIOUS): 33 direction = -1; 34 break; 35 default: 36 l_match = -1; 37 direction = 1; 38 break; 39 } 40 41 if(l_match == -1) 42 direction = 1; 43 44 int current = l_match; 45 int i; 46 for(i = 0; i < E.numrows; i++) { 47 current += direction; 48 if(current == -1) 49 current = E.numrows - 1; 50 else if(current == E.numrows) 51 current = 0; 52 53 erow* row = &E.row[current]; 54 char* match = strstr(row->render, query); 55 if(match) { 56 l_match = current; 57 E.cy = current; 58 E.cx = editorRowRxToCx(row, match - row->render); 59 E.rowoff = E.numrows; 60 61 saved_hl_line = current; 62 saved_hl = malloc(row->rsize); 63 memcpy(saved_hl, row->hl, row->rsize); 64 memset(&row->hl[match - row->render], HL_MATCH, strlen(query)); 65 break; 66 } 67 } 68 } 69 70 void editorFind() { 71 int s_cx = E.cx; 72 int s_cy = E.cy; 73 int s_coloff = E.coloff; 74 int s_rowoff = E.rowoff; 75 76 char* query = editorPrompt("/%s", editorFindCallback); 77 78 if(query) { 79 free(query); 80 } else { 81 E.cx = s_cx; 82 E.cy = s_cy; 83 E.coloff = s_coloff; 84 E.rowoff = s_rowoff; 85 } 86 87 editorSetDefaultStatusMessage(); 88 }