-
Notifications
You must be signed in to change notification settings - Fork 1
/
FindReplace.pas
317 lines (268 loc) · 9.76 KB
/
FindReplace.pas
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
unit FindReplace;
(*
TSynSearchOption = (ssoMatchCase, ssoWholeWord, ssoBackwards,
ssoEntireScope, ssoSelectedOnly, ssoReplace, ssoReplaceAll, ssoPrompt);
frDisableMatchCase
Disables (grays) the Match Case check box in a find dialog.
frDisableUpDown
Disables (grays) the Up and Down buttons, which determine the direction of the search.
frDisableWholeWord
Disables (grays) the Match Whole Word check box of find dialog.
frDown = not ssoBackwards
Selects the Down button by default when the dialog opens. If the frDown flags is off, Up is selected when the dialog opens. (By default, frDown is on.)
frFindNext
This flag is turned on when the user clicks the Find Next button and turned off when the dialog closes.
frHideMatchCase
Removes the Match Case check box from the dialog.
frHideWholeWord
Removes the Match Whole Word check box from the dialog.
frHideUpDown
Removes the Up and Down buttons from the dialog.
frMatchCase = ssoMatchCase
This flag is turned on (off) when the user selects (deselects) the Match Case check box. To select the check box by default when the dialog opens, set frMatchCase at design time.
frReplace = ssoReplace
Applies to TReplaceDialog only. This flag is set by the system to indicate that the application should replace the current occurrence (and only the current occurrence) of the FindText string with the ReplaceText string. Not used in search routines.
frReplaceAll = ssoReplaceAll
Applies to TReplaceDialog only. This flag is set by the system to indicate that the application should replace all occurrences of the FindText string with the ReplaceText string.
frShowHelp
Displays a Help button in the dialog.
frWholeWord = ssoWholeWord
This flag is turned on (off) when the user selects (deselects) the Match Whole Word check box. To select the check box by default when the dialog opens, set frWholeWord at design time.
*)
interface
uses
Windows, Messages, SysUtils, Classes, Dialogs, SynEdit, System.UITypes;
type
TSynEditFindReplace = class(TComponent)
private
fEditor: TSynEdit;
fReplaceDialog: TReplaceDialog;
fFindDialog: TFindDialog;
fAutofocus: boolean;
protected
type
TFindDirection = (sdDefault, sdForwards, sdBackwards);
procedure OnFind(Sender: TObject); virtual;
procedure OnReplace(Sender: TObject); virtual;
procedure DoFind(dialog: TFindDialog; direction: TFindDirection); overload;
procedure DoReplace(dialog: TReplaceDialog; direction: TFindDirection);
function GetDirection(dialog: TFindDialog): TFindDirection;
public
constructor Create(AOwner: TComponent); override;
property FindDialog: TFindDialog read fFindDialog;
property ReplaceDialog: TReplaceDialog read fReplaceDialog;
procedure CloseDialogs;
procedure FindExecute;
procedure ReplaceExecute;
procedure FindContinue;
procedure FindNext;
procedure FindPrev;
procedure GoToLine(LineNumber: integer);
published
property Editor: TSynEdit read fEditor write fEditor;
property Autofocus: boolean read fAutofocus write fAutofocus;
end;
implementation
uses
SynEditTypes;
constructor TSynEditFindReplace.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fFindDialog := TFindDialog.Create(Self);
fFindDialog.OnFind := OnFind;
fReplaceDialog := TReplaceDialog.Create(Self);
fReplaceDialog.OnReplace := OnReplace;
fReplaceDialog.OnFind := OnFind;
fReplaceDialog.Options := fReplaceDialog.Options + [frHideWholeWord]; // TODO: currently not supported (see below)
end;
function TSynEditFindReplace.GetDirection(dialog: TFindDialog): TFindDirection;
begin
if frDown in dialog.Options then
result := sdForwards
else
result := sdBackwards;
end;
procedure TSynEditFindReplace.DoFind(dialog: TFindDialog; direction: TFindDirection);
var
opt: TSynSearchOptions;
found: boolean;
begin
if direction = sdDefault then direction := GetDirection(dialog);
if fEditor.SelAvail then
begin
if direction = sdForwards then
begin
fEditor.SelStart := fEditor.SelStart + 1;
fEditor.SelLength := 0;
end
else
begin
// Links von Selektion springen
fEditor.SelLength := 0;
end;
end;
opt := [];
if frMatchCase in dialog.Options then Include(opt, ssoMatchCase);
if frWholeWord in dialog.Options then Include(opt, ssoWholeWord);
//if frReplace in dialog.Options then Include(opt, ssoReplace);
//if frReplaceAll in dialog.Options then Include(opt, ssoReplaceAll);
if direction = sdBackwards then Include(opt, ssoBackwards);
//Include(opt, ssoPrompt); // TODO: test. geht nicht?
//if fEditor.SelAvail then Include(opt, ssoSelectedOnly); // TODO: geht nicht, weil er bei einer suche ja dann etwas selektirert und dann nicht weitergeht
Exclude(opt, ssoEntireScope); // TODO: ok?
found := fEditor.SearchReplace(dialog.FindText, '', opt) > 0;
if not found then
begin
// TODO: If single replace was chosen, behave like Notepad and select the last replaced word
if direction = sdForwards then
MessageDlg('End of document reached.', mtInformation, [mbOk], 0)
else
MessageDlg('Begin of document reached.', mtInformation, [mbOk], 0);
end;
if fAutofocus and fEditor.CanFocus then fEditor.SetFocus;
end;
procedure TSynEditFindReplace.DoReplace(dialog: TReplaceDialog; direction: TFindDirection);
var
opt: TSynSearchOptions;
numReplacements: integer;
bakSelLenght: Integer;
stmp: string;
bakSelStart: Integer;
begin
try
if direction = sdDefault then direction := GetDirection(dialog);
opt := [];
if frMatchCase in dialog.Options then Include(opt, ssoMatchCase);
if frWholeWord in dialog.Options then Include(opt, ssoWholeWord);
if frReplace in dialog.Options then Include(opt, ssoReplace);
if frReplaceAll in dialog.Options then Include(opt, ssoReplaceAll);
if direction = sdBackwards then Include(opt, ssoBackwards);
Include(opt, ssoPrompt); // TODO: test. geht nicht?
if fEditor.SelAvail then Include(opt, ssoSelectedOnly);
Exclude(opt, ssoEntireScope); // TODO: ok?
if not (ssoReplaceAll in opt) then
begin
if fEditor.SelLength = 0 then
begin
DoFind(dialog, sdForwards);
exit;
end;
end;
fEditor.BeginUpdate; // TODO: geht nicht?
//fEditor.BeginUndoBlock;
try
bakSelLenght := 0; // avoid compiler warning
if ssoReplaceAll in opt then
begin
// Remember the selection length
bakSelLenght := fEditor.SelLength;
//bakSelStart := fEditor.SelStart;
// Remember the selection start (we don't backup fEditor.SelStart, since the replacement might change the location)!
// We assume that character #1 will not be in a text file!
stmp := fEditor.Text;
Insert(chr(1), stmp, fEditor.SelStart+1);
//showmessage(inttostr(ord(stmp[fEditor.SelStart-1])));
//showmessage(inttostr(ord(stmp[fEditor.SelStart])));
//showmessage(inttostr(ord(stmp[fEditor.SelStart+1])));
fEditor.Text := stmp;
// When the user presses "Replace all", then we want to replace from the very beginning!
fEditor.SelStart := 0;
fEditor.SelLength := 0;
end;
numReplacements := fEditor.SearchReplace(dialog.FindText, dialog.ReplaceText, opt);
if ssoReplaceAll in opt then
begin
stmp := fEditor.Text;
bakSelStart := AnsiPos(chr(1), stmp)-1;
Delete(stmp, bakSelStart+1, 1);
fEditor.Text := stmp;
fEditor.SelStart := bakSelStart;
fEditor.SelLength := bakSelLenght;
// TODO: The SelStart and SelLength were kept, but the scrollposition did not. What can we do?
end;
finally
//fEditor.EndUndoBlock;
fEditor.EndUpdate;
end;
if not (ssoReplaceAll in opt) then
begin
DoFind(dialog, sdForwards);
end
else
begin
ShowMessageFmt('%d replaced.', [numReplacements]);
end;
finally
if fAutofocus and fEditor.CanFocus then fEditor.SetFocus;
end;
end;
procedure TSynEditFindReplace.OnFind(Sender: TObject);
begin
DoFind(Sender as TFindDialog, sdDefault);
end;
procedure TSynEditFindReplace.OnReplace(Sender: TObject);
begin
DoReplace(Sender as TReplaceDialog, sdDefault);
end;
procedure TSynEditFindReplace.FindExecute;
begin
fFindDialog.Execute;
end;
procedure TSynEditFindReplace.ReplaceExecute;
begin
fReplaceDialog.Execute;
end;
procedure TSynEditFindReplace.FindContinue;
begin
if fFindDialog.FindText = '' then
begin
fFindDialog.Options := fFindDialog.Options + [frDown]; // Default direction: down
FindExecute;
end
else
DoFind(fFindDialog, sdDefault);
end;
procedure TSynEditFindReplace.FindNext;
begin
if fFindDialog.FindText = '' then
begin
fFindDialog.Options := fFindDialog.Options + [frDown];
FindExecute;
end
else
DoFind(fFindDialog, sdForwards);
end;
procedure TSynEditFindReplace.FindPrev;
begin
if fFindDialog.FindText = '' then
begin
fFindDialog.Options := fFindDialog.Options - [frDown];
FindExecute;
end
else
DoFind(fFindDialog, sdBackwards);
end;
procedure TSynEditFindReplace.GoToLine(LineNumber: integer);
var
currentLine: integer;
i: integer;
begin
currentLine := 1;
for i := 1 to fEditor.GetTextLen do
begin
if currentLine = LineNumber then
begin
fEditor.selStart := i;
fEditor.SetFocus;
Exit;
end
else if fEditor.Text = #$D then
inc(currentLine);
end;
end;
procedure TSynEditFindReplace.CloseDialogs;
begin
fFindDialog.CloseDialog;
fReplaceDialog.CloseDialog;
end;
end.