-
Notifications
You must be signed in to change notification settings - Fork 14
/
search.c
168 lines (154 loc) · 5.24 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ---------------- search.c ------------- */
#include "dflat.h"
extern DBOX SearchTextDB;
extern DBOX ReplaceTextDB;
static int CheckCase = TRUE;
static int Replacing = FALSE;
static int lastsize;
/* - case-insensitive, white-space-normalized char compare - */
static BOOL SearchCmp(int a, int b)
{
if (b == '\n')
b = ' ';
if (CheckCase)
return a != b;
return tolower(a) != tolower(b);
}
/* ----- replace a matching block of text ----- */
static void replacetext(WINDOW wnd, char *cp1, DBOX *db)
{
char *cr = GetEditBoxText(db, ID_REPLACEWITH);
char *cp = GetEditBoxText(db, ID_SEARCHFOR);
int oldlen = strlen(cp); /* length of text being replaced */
int newlen = strlen(cr); /* length of replacing text */
int dif;
lastsize = newlen;
if (oldlen < newlen) {
/* ---- new text expands text size ---- */
dif = newlen-oldlen;
if (wnd->textlen < strlen(wnd->text)+dif) {
/* ---- need to reallocate the text buffer ---- */
int offset = (int)(cp1-wnd->text);
wnd->textlen += dif;
wnd->text = DFrealloc(wnd->text, wnd->textlen+2);
cp1 = wnd->text + offset;
}
memmove(cp1+dif, cp1, strlen(cp1)+1);
}
else if (oldlen > newlen) {
/* ---- new text collapses text size ---- */
dif = oldlen-newlen;
memmove(cp1, cp1+dif, strlen(cp1)+1);
}
strncpy(cp1, cr, newlen);
}
/* ------- search for the occurrance of a string ------- */
static void SearchTextBox(WINDOW wnd, int incr)
{
char *s1 = NULL, *s2, *cp1;
DBOX *db = Replacing ? &ReplaceTextDB : &SearchTextDB;
char *cp = GetEditBoxText(db, ID_SEARCHFOR);
BOOL rpl = TRUE, FoundOne = FALSE;
while (rpl == TRUE && cp != NULL && *cp) {
rpl = Replacing ?
CheckBoxSetting(&ReplaceTextDB, ID_REPLACEALL) :
FALSE;
if (TextBlockMarked(wnd)) {
ClearTextBlock(wnd);
SendMessage(wnd, PAINT, 0, 0);
}
/* search for a match starting at cursor position */
cp1 = CurrChar;
if (incr)
cp1 += lastsize; /* start past the last hit */
/* --- compare at each character position --- */
while (*cp1) {
s1 = cp;
s2 = cp1;
while (*s1 && *s1 != '\n') {
if (SearchCmp(*s1, *s2))
break;
s1++, s2++;
}
if (*s1 == '\0' || *s1 == '\n')
break;
cp1++;
}
if (s1 != NULL && (*s1 == 0 || *s1 == '\n')) {
/* ----- match at *cp1 ------- */
FoundOne = TRUE;
/* mark a block at beginning of matching text */
wnd->BlkEndLine = TextLineNumber(wnd, s2);
wnd->BlkBegLine = TextLineNumber(wnd, cp1);
if (wnd->BlkEndLine < wnd->BlkBegLine)
wnd->BlkEndLine = wnd->BlkBegLine;
wnd->BlkEndCol =
(int)(s2 - TextLine(wnd, wnd->BlkEndLine));
wnd->BlkBegCol =
(int)(cp1 - TextLine(wnd, wnd->BlkBegLine));
/* position the cursor at the matching text */
wnd->CurrCol = wnd->BlkBegCol;
wnd->CurrLine = wnd->BlkBegLine;
wnd->WndRow = wnd->CurrLine - wnd->wtop;
/* -- remember the size of the matching text -- */
lastsize = strlen(cp);
/* align the window scroll to matching text */
if (WndCol > ClientWidth(wnd)-1)
wnd->wleft = wnd->CurrCol;
if (wnd->WndRow > ClientHeight(wnd)-1) {
wnd->wtop = wnd->CurrLine;
wnd->WndRow = 0;
}
SendMessage(wnd, PAINT, 0, 0);
SendMessage(wnd, KEYBOARD_CURSOR,
WndCol, wnd->WndRow);
if (Replacing) {
if (rpl || YesNoBox("Replace the text?")) {
replacetext(wnd, cp1, db);
wnd->TextChanged = TRUE;
BuildTextPointers(wnd);
if (rpl) {
incr = TRUE;
continue;
}
}
ClearTextBlock(wnd);
SendMessage(wnd, PAINT, 0, 0);
}
return;
}
break;
}
if (!FoundOne)
MessageBox("Search/Replace Text", "No match found");
}
/* ------- search for the occurrance of a string,
replace it with a specified string ------- */
void ReplaceText(WINDOW wnd)
{
Replacing = TRUE;
lastsize = 0;
if (CheckCase)
SetCheckBox(&ReplaceTextDB, ID_MATCHCASE);
if (DialogBox(NULL, &ReplaceTextDB, TRUE, NULL)) {
CheckCase=CheckBoxSetting(&ReplaceTextDB,ID_MATCHCASE);
SearchTextBox(wnd, FALSE);
}
}
/* ------- search for the first occurrance of a string ------ */
void SearchText(WINDOW wnd)
{
Replacing = FALSE;
lastsize = 0;
if (CheckCase)
SetCheckBox(&SearchTextDB, ID_MATCHCASE);
if (DialogBox(NULL, &SearchTextDB, TRUE, NULL)) {
CheckCase=CheckBoxSetting(&SearchTextDB,ID_MATCHCASE);
SearchTextBox(wnd, FALSE);
}
}
/* ------- search for the next occurrance of a string ------- */
void SearchNext(WINDOW wnd)
{
SearchTextBox(wnd, TRUE);
}