2018-10-19 13:24:58 +02:00
|
|
|
#include "search.h"
|
2018-10-25 12:17:28 +02:00
|
|
|
#include <stdio.h> // for NULL
|
|
|
|
#include <stdlib.h> // for free, malloc
|
|
|
|
#include <string.h> // for memcpy, memset, strlen, strstr
|
|
|
|
#include "fn.h" // for E, editorConfig, erow, editorPrompt, editorHighl...
|
|
|
|
#include "row.h" // for editorRowRxToCx
|
2019-01-22 11:52:00 +01:00
|
|
|
#include "hl.h"
|
2018-10-25 12:17:28 +02:00
|
|
|
|
2018-10-19 13:24:58 +02:00
|
|
|
|
2019-01-22 11:44:21 +01:00
|
|
|
void editorSearchCallback(char *query, int key) {
|
2018-10-19 13:24:58 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:52:00 +01:00
|
|
|
if (key == '\r' || key == '\x1b') {
|
2018-10-19 13:24:58 +02:00
|
|
|
last_match = -1;
|
|
|
|
direction = 1;
|
2019-01-22 11:52:00 +01:00
|
|
|
editorSelectSyntaxHighlight();
|
2018-10-19 13:24:58 +02:00
|
|
|
return;
|
|
|
|
} else if (key == ARROW_RIGHT || key == ARROW_DOWN) {
|
|
|
|
direction = 1;
|
|
|
|
} else if (key == ARROW_LEFT || key == ARROW_UP) {
|
|
|
|
direction = -1;
|
|
|
|
} else {
|
|
|
|
last_match = -1;
|
|
|
|
direction = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_match == -1) direction = 1;
|
|
|
|
int current = last_match;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i=0; i < E.numrows; i++) {
|
|
|
|
current += direction;
|
|
|
|
if (current == -1) current = E.numrows -1;
|
|
|
|
else if (current == E.numrows) current = 0;
|
|
|
|
|
|
|
|
erow *row = &E.row[current];
|
|
|
|
char *match = strstr(row->render, query);
|
|
|
|
if (match) {
|
|
|
|
last_match = current;
|
|
|
|
E.cy = current;
|
|
|
|
E.cx = editorRowRxToCx(row, match - row->render); // Substract pointers here
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:44:21 +01:00
|
|
|
void editorSearch() {
|
2018-10-19 13:24:58 +02:00
|
|
|
int saved_cx = E.cx;
|
|
|
|
int saved_cy = E.cy;
|
|
|
|
int saved_coloff = E.coloff;
|
|
|
|
int saved_rowoff = E.rowoff;
|
|
|
|
|
2019-01-22 11:44:21 +01:00
|
|
|
char *query = editorPrompt("Search: %s (Use ESC/Arrows/Enter)", editorSearchCallback);
|
2018-10-19 13:24:58 +02:00
|
|
|
if (query) {
|
|
|
|
free(query);
|
|
|
|
} else {
|
|
|
|
E.cx = saved_cx;
|
|
|
|
E.cy = saved_cy;
|
|
|
|
E.coloff = saved_coloff;
|
|
|
|
E.rowoff = saved_rowoff;
|
|
|
|
}
|
|
|
|
}
|