1 /* ---------------- search.c ------------- */
4 extern DBOX SearchTextDB
;
5 extern DBOX ReplaceTextDB
;
6 static int CheckCase
= TRUE
;
7 static int Replacing
= FALSE
;
9 /* - case-insensitive, white-space-normalized char compare - */
10 static BOOL
SearchCmp(int a
, int b
)
16 return tolower(a
) != tolower(b
);
19 /* ----- replace a matching block of text ----- */
20 static void replacetext(DFWINDOW wnd
, char *cp1
, DBOX
*db
)
22 char *cr
= GetEditBoxText(db
, ID_REPLACEWITH
);
23 char *cp
= GetEditBoxText(db
, ID_SEARCHFOR
);
24 int oldlen
= strlen(cp
); /* length of text being replaced */
25 int newlen
= strlen(cr
); /* length of replacing text */
27 if (oldlen
< newlen
) {
28 /* ---- new text expands text size ---- */
30 if (wnd
->textlen
< strlen(wnd
->text
)+dif
) {
31 /* ---- need to reallocate the text buffer ---- */
32 int offset
= (int)((int)cp1
-(int)wnd
->text
);
34 wnd
->text
= DFrealloc(wnd
->text
, wnd
->textlen
+2);
35 cp1
= wnd
->text
+ offset
;
37 memmove(cp1
+dif
, cp1
, strlen(cp1
)+1);
39 else if (oldlen
> newlen
) {
40 /* ---- new text collapses text size ---- */
42 memmove(cp1
, cp1
+dif
, strlen(cp1
)+1);
44 strncpy(cp1
, cr
, newlen
);
47 /* ------- search for the occurrance of a string ------- */
48 static void SearchTextBox(DFWINDOW wnd
, int incr
)
50 char *s1
= NULL
, *s2
, *cp1
;
51 DBOX
*db
= Replacing
? &ReplaceTextDB
: &SearchTextDB
;
52 char *cp
= GetEditBoxText(db
, ID_SEARCHFOR
);
53 BOOL rpl
= TRUE
, FoundOne
= FALSE
;
55 while (rpl
== TRUE
&& cp
!= NULL
&& *cp
) {
57 CheckBoxSetting(&ReplaceTextDB
, ID_REPLACEALL
) :
59 if (TextBlockMarked(wnd
)) {
61 DfSendMessage(wnd
, PAINT
, 0, 0);
63 /* search for a match starting at cursor position */
66 cp1
++; /* start past the last hit */
67 /* --- compare at each character position --- */
71 while (*s1
&& *s1
!= '\n') {
72 if (SearchCmp(*s1
, *s2
))
76 if (*s1
== '\0' || *s1
== '\n')
80 if (s1
!= NULL
&& (*s1
== 0 || *s1
== '\n')) {
81 /* ----- match at *cp1 ------- */
84 /* mark a block at beginning of matching text */
85 wnd
->BlkEndLine
= TextLineNumber(wnd
, s2
);
86 wnd
->BlkBegLine
= TextLineNumber(wnd
, cp1
);
87 if (wnd
->BlkEndLine
< wnd
->BlkBegLine
)
88 wnd
->BlkEndLine
= wnd
->BlkBegLine
;
90 (int)((int)s2
- (int)TextLine(wnd
, wnd
->BlkEndLine
));
92 (int)((int)cp1
- (int)TextLine(wnd
, wnd
->BlkBegLine
));
94 /* position the cursor at the matching text */
95 wnd
->CurrCol
= wnd
->BlkBegCol
;
96 wnd
->CurrLine
= wnd
->BlkBegLine
;
97 wnd
->WndRow
= wnd
->CurrLine
- wnd
->wtop
;
99 /* align the window scroll to matching text */
100 if (WndCol
> ClientWidth(wnd
)-1)
101 wnd
->wleft
= wnd
->CurrCol
;
102 if (wnd
->WndRow
> ClientHeight(wnd
)-1) {
103 wnd
->wtop
= wnd
->CurrLine
;
107 DfSendMessage(wnd
, PAINT
, 0, 0);
108 DfSendMessage(wnd
, KEYBOARD_CURSOR
,
109 WndCol
, wnd
->WndRow
);
112 if (rpl
|| DfYesNoBox("Replace the text?")) {
113 replacetext(wnd
, cp1
, db
);
114 wnd
->TextChanged
= TRUE
;
115 BuildTextPointers(wnd
);
122 DfSendMessage(wnd
, PAINT
, 0, 0);
129 DfMessageBox("Search/Replace Text", "No match found");
132 /* ------- search for the occurrance of a string,
133 replace it with a specified string ------- */
134 void DfReplaceText(DFWINDOW wnd
)
138 SetCheckBox(&ReplaceTextDB
, ID_MATCHCASE
);
139 if (DfDialogBox(NULL
, &ReplaceTextDB
, TRUE
, NULL
)) {
140 CheckCase
=CheckBoxSetting(&ReplaceTextDB
,ID_MATCHCASE
);
141 SearchTextBox(wnd
, FALSE
);
145 /* ------- search for the first occurrance of a string ------ */
146 void DfSearchText(DFWINDOW wnd
)
150 SetCheckBox(&SearchTextDB
, ID_MATCHCASE
);
151 if (DfDialogBox(NULL
, &SearchTextDB
, TRUE
, NULL
)) {
152 CheckCase
=CheckBoxSetting(&SearchTextDB
,ID_MATCHCASE
);
153 SearchTextBox(wnd
, FALSE
);
157 /* ------- search for the next occurrance of a string ------- */
158 void DfSearchNext(DFWINDOW wnd
)
160 SearchTextBox(wnd
, TRUE
);