-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.c
95 lines (82 loc) · 2.63 KB
/
search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "search.h"
#include "input.h"
#include "rowOperations.h"
void microSearch()
{
int savedCursorPosX, savedCursorPosY, savedColOffset, savedRowOffset;
savedCursorPosX = microConfig.cursorPosX;
savedCursorPosY = microConfig.cursorPosY;
savedColOffset = microConfig.colOffset;
savedRowOffset = microConfig.rowOffset;
char *query = microPrompt("Search: %s (ESC to cancel/Arrow keys to move/Enter to finish search)", microSearchCallback);
if (query)
{
free(query);
microConfig.cursorPosX = savedCursorPosX;
microConfig.cursorPosY = savedCursorPosY;
microConfig.colOffset = savedColOffset;
microConfig.rowOffset = savedRowOffset;
}
}
void microSearchCallback(char *query, int key)
{
static int previousMatch = -1;
static int direction = 1;
static int savedHighlightLine;
static char *savedHighlight = NULL;
if (savedHighlight)
{
memcpy(microConfig.row[savedHighlightLine].highlight, savedHighlight, microConfig.row[savedHighlightLine].rsize);
free(savedHighlight);
savedHighlight = NULL;
}
if (key == '\r' || key == '\x1b')
{
previousMatch = -1;
direction = 1;
return;
}
else if (key == RIGHT_ARROW || key == DOWN_ARROW)
{
direction = 1;
}
else if (key == LEFT_ARROW || key == UP_ARROW)
{
direction = -1;
}
else
{
previousMatch = -1;
direction = 1;
}
if (previousMatch == -1)
direction = 1;
int current = previousMatch;
for (int i = 0; i < microConfig.numRows; i++)
{
current += direction;
if (current == -1)
current = microConfig.numRows - 1;
else if (current == microConfig.numRows)
current = 0;
microRow *row = µConfig.row[current];
// row = µConfig.row[i];
char *match = strstr(row->render, query);
if (match)
{
previousMatch = current;
microConfig.cursorPosY = current;
// microConfig.cursorPosY = i;
microConfig.cursorPosX = microRowRenderPosXtoCursorPosX(row, match - row->render);
microConfig.rowOffset = microConfig.numRows;
savedHighlightLine = current;
savedHighlight = malloc(row->size);
memcpy(savedHighlight, row->highlight, row->size);
memset(&row->highlight[match - row->render], HL_MATCH, strlen(query));
break;
}
}
}