86 lines
1.9 KiB
C
86 lines
1.9 KiB
C
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/types.h>
|
|
#include <termios.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
|
|
#include "fn.h"
|
|
#include "search.h"
|
|
#include "row.h"
|
|
|
|
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 == '\xb') {
|
|
last_match = -1;
|
|
direction = 1;
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
void editorFind() {
|
|
int saved_cx = E.cx;
|
|
int saved_cy = E.cy;
|
|
int saved_coloff = E.coloff;
|
|
int saved_rowoff = E.rowoff;
|
|
|
|
char *query = editorPrompt("Search: %s (Use ESC/Arrows/Enter)", editorFindCallback);
|
|
if (query) {
|
|
free(query);
|
|
} else {
|
|
E.cx = saved_cx;
|
|
E.cy = saved_cy;
|
|
E.coloff = saved_coloff;
|
|
E.rowoff = saved_rowoff;
|
|
}
|
|
}
|