forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlgFindInFiles.pas
352 lines (313 loc) · 11.6 KB
/
dlgFindInFiles.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
{-----------------------------------------------------------------------------
Unit Name: dlgFindInFiles
Author: Kiriakos Vlahos
Date: 20-May-2005
Purpose: Find in Files dialog
History: Based on code from GExperts and covered by its licence
GExperts License Agreement
GExperts is copyright 1996-2005 by GExperts, Inc, Erik Berry, and several other
authors who have submitted their code for inclusion. This license agreement only covers code written by GExperts, Inc and Erik Berry. You should contact the other authors concerning their respective copyrights and conditions.
The rules governing the use of GExperts and the GExperts source code are derived
from the official Open Source Definition, available at http://www.opensource.org.
The conditions and limitations are as follows:
* Usage of GExperts binary distributions is permitted for all developers.
You may not use the GExperts source code to develop proprietary or
commercial products including plugins or libraries for those products.
You may use the GExperts source code in an Open Source project, under the
terms listed below.
* You may not use the GExperts source code to create and distribute custom
versions of GExperts under the "GExperts" name. If you do modify and
distribute custom versions of GExperts, the binary distribution must be
named differently and clearly marked so users can tell they are not using
the official GExperts distribution. A visible and unmodified version of
this license must appear in any modified distribution of GExperts.
* Custom distributions of GExperts must include all of the custom changes
as a patch file that can be applied to the original source code. This
restriction is in place to protect the integrity of the original author's
source code. No support for modified versions of GExperts will be provided
by the original authors or on the GExperts mailing lists.
* All works derived from GExperts must be distributed under a license
compatible with this license and the official Open Source Definition,
which can be obtained from http://www.opensource.org/.
* Please note that GExperts, Inc. and the other contributing authors hereby
state that this package is provided "as is" and without any express or
implied warranties, including, but not without limitation, the implied
warranties of merchantability and fitness for a particular purpose. In
other words, we accept no liability for any damage that may result from
using GExperts or programs that use the GExperts source code.
-----------------------------------------------------------------------------}
unit dlgFindInFiles;
interface
uses
Classes, Controls, Graphics, Forms, StdCtrls,
cFindInFiles, SpTBXControls,
SpTBXEditors, dlgPyIDEBase, SpTBXItem;
type
TFindInFilesDialog = class(TPyIDEDlgBase)
gbxOptions: TSpTBXGroupBox;
gbxWhere: TSpTBXGroupBox;
gbxDirectories: TSpTBXGroupBox;
btnBrowse: TSpTBXButton;
btnOK: TSpTBXButton;
btnCancel: TSpTBXButton;
btnHelp: TSpTBXButton;
cbCaseSensitive: TSpTBXCheckBox;
cbNoComments: TSpTBXCheckBox;
cbWholeWord: TSpTBXCheckBox;
cbRegEx: TSpTBXCheckBox;
cbInclude: TSpTBXCheckBox;
rbOpenFiles: TSpTBXRadioButton;
rbProject: TSpTBXRadioButton;
rbCurrentOnly: TSpTBXRadioButton;
rbDirectories: TSpTBXRadioButton;
cbMasks: TSpTBXComboBox;
cbDirectory: TSpTBXComboBox;
cbText: TSpTBXComboBox;
lblFind: TSpTBXLabel;
lblMasks: TSpTBXLabel;
lblDirectory: TSpTBXLabel;
SpTBXPanel1: TSpTBXPanel;
procedure btnBrowseClick(Sender: TObject);
procedure rbProjectClick(Sender: TObject);
procedure btnHelpClick(Sender: TObject);
procedure cbDirectoryDropDown(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
FFindInFilesExpert: TFindInFilesExpert;
procedure EnableDirectoryControls(New: Boolean);
procedure LoadFormSettings;
procedure SaveFormSettings;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure RetrieveSettings(var Value: TGrepSettings);
property FindInFilesExpert: TFindInFilesExpert read FFindInFilesExpert;
end;
implementation
{$R *.dfm}
uses
SysUtils, Windows, Messages, FileCtrl,
{GX_GrepResults, GX_GrepOptions,} Math,
uEditAppIntfs, frmFindResults, dmCommands,
Dialogs, gnugettext, StringResources;
function GetScrollbarWidth: Integer;
begin
Result := GetSystemMetrics(SM_CXVSCROLL);
end;
procedure TFindInFilesDialog.btnBrowseClick(Sender: TObject);
var
NewDir : string;
begin
NewDir := cbDirectory.Text;
if SelectDirectory('Directory To Search', '', NewDir) then
cbDirectory.Text := NewDir;
end;
procedure TFindInFilesDialog.EnableDirectoryControls(New: Boolean);
begin
cbDirectory.Enabled := New;
cbMasks.Enabled := New;
cbInclude.Enabled := New;
btnBrowse.Enabled := New;
if not New then
begin
cbDirectory.Color := clBtnface;
cbMasks.Color := clBtnface;
end
else
begin
cbDirectory.Color := clWindow;
cbMasks.Color := clWindow;
end
end;
procedure TFindInFilesDialog.rbProjectClick(Sender: TObject);
begin
EnableDirectoryControls(rbDirectories.Checked);
end;
procedure TFindInFilesDialog.btnHelpClick(Sender: TObject);
begin
Application.HelpContext(HelpContext);
end;
procedure TFindInFilesDialog.cbDirectoryDropDown(Sender: TObject);
var
i: Integer;
MaxWidth: Integer;
Bitmap: Graphics.TBitmap;
begin
MaxWidth := cbDirectory.Width;
Bitmap := Graphics.TBitmap.Create;
try
Bitmap.Canvas.Font.Assign(cbDirectory.Font);
for i := 0 to cbDirectory.Items.Count - 1 do
MaxWidth := Max(MaxWidth, Bitmap.Canvas.TextWidth(cbDirectory.Items[i]) + 10);
finally;
FreeAndNil(Bitmap);
end;
if cbDirectory.Items.Count > cbDirectory.DropDownCount then
Inc(MaxWidth, GetScrollbarWidth);
MaxWidth := Min(400, MaxWidth);
if MaxWidth > cbDirectory.Width then
SendMessage(cbDirectory.Handle, CB_SETDROPPEDWIDTH, MaxWidth, 0)
else
SendMessage(cbDirectory.Handle, CB_SETDROPPEDWIDTH, 0, 0)
end;
procedure TFindInFilesDialog.btnOKClick(Sender: TObject);
var
i: Integer;
Dirs: TStringList;
begin
if rbDirectories.Checked then
begin
if Trim(cbDirectory.Text) = '' then
cbDirectory.Text := GetCurrentDir;
Dirs := TStringList.Create;
try
Dirs.StrictDelimiter:= True;
Dirs.Delimiter := ';';
Dirs.DelimitedText := cbDirectory.Text;
for i := 0 to Dirs.Count - 1 do
begin
if Dirs[i] = '' then continue;
Dirs[i] := ExpandFileName(IncludeTrailingPathDelimiter(Dirs[i]));
if not SysUtils.DirectoryExists(Dirs[i]) then begin
Dialogs.MessageDlg(Format(_(SSearchDirectoryDoesNotExist), [Dirs[i]]), mtError, [mbOK], 0);
Abort;
end;
end;
cbDirectory.Text := Dirs.DelimitedText;
finally
FreeAndNil(Dirs);
end;
end;
ModalResult := mrOk;
end;
constructor TFindInFilesDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
LoadFormSettings;
end;
destructor TFindInFilesDialog.Destroy;
begin
SaveFormSettings;
inherited Destroy;
end;
procedure TFindInFilesDialog.SaveFormSettings;
begin
AddMRUString(cbText.Text, FFindInFilesExpert.SearchList, False);
AddMRUString(cbDirectory.Text, FFindInFilesExpert.DirList, True);
AddMRUString(cbMasks.Text, FFindInFilesExpert.MaskList, False);
FFindInFilesExpert.GrepCaseSensitive := cbCaseSensitive.Checked;
FFindInFilesExpert.GrepNoComments := cbNoComments.Checked;
FFindInFilesExpert.GrepSub := cbInclude.Checked;
FFindInFilesExpert.GrepWholeWord := cbWholeWord.Checked;
FFindInFilesExpert.GrepRegEx := cbRegEx.Checked;
if rbCurrentOnly.Checked then
FFindInFilesExpert.GrepSearch := 0
else if rbOpenFiles.Checked then
FFindInFilesExpert.GrepSearch := 1
else if rbProject.Checked then
FFindInFilesExpert.GrepSearch := 2
else if rbDirectories.Checked then
FFindInFilesExpert.GrepSearch := 3;
end;
procedure TFindInFilesDialog.LoadFormSettings;
function RetrieveEditorBlockSelection: string;
var
Temp: string;
i: Integer;
begin
if Assigned(GI_ActiveEditor) then
Temp := GI_ActiveEditor.SynEdit.SelText
else
Temp := '';
// Only use the currently selected text if the length is between 1 and 80
if (Length(Trim(Temp)) >= 1) and (Length(Trim(Temp)) <= 80) then
begin
i := Min(Pos(#13, Temp), Pos(#10, Temp));
if i > 0 then
Temp := Copy(Temp, 1, i - 1);
Temp := Temp;
end else
Temp := '';
Result := Temp;
end;
procedure SetSearchPattern(Str: string);
begin
cbText.Text := Str;
cbText.SelectAll;
end;
procedure SetupPattern;
var
Selection: string;
begin
Selection := RetrieveEditorBlockSelection;
if (Trim(Selection) = '') and CommandsDataModule.PyIDEOptions.SearchTextAtCaret then begin
if Assigned(GI_ActiveEditor) then with GI_ActiveEditor.SynEdit do
Selection := GetWordAtRowCol(CaretXY)
else
Selection := '';
end;
if (Selection = '') and (cbText.Items.Count > 0) then
Selection := cbText.Items[0];
SetSearchPattern(Selection);
end;
begin
FFindInFilesExpert := FindResultsWindow.FindInFilesExpert;
cbText.Items.Assign(FFindInFilesExpert.SearchList);
cbDirectory.Items.Assign(FFindInFilesExpert.DirList);
cbMasks.Items.Assign(FFindInFilesExpert.MaskList);
if FFindInFilesExpert.GrepSave then
begin
cbCaseSensitive.Checked := FFindInFilesExpert.GrepCaseSensitive;
cbNoComments.Checked := FFindInFilesExpert.GrepNoComments;
cbInclude.Checked := FFindInFilesExpert.GrepSub;
cbWholeWord.Checked := FFindInFilesExpert.GrepWholeWord;
cbRegEx.Checked := FFindInFilesExpert.GrepRegEx;
case FFindInFilesExpert.GrepSearch of
0: rbCurrentOnly.Checked := True;
1: rbOpenFiles.Checked := True;
2: rbProject.Checked := True;
3: rbDirectories.Checked := True;
end;
if cbText.Items.Count > 0 then
cbText.Text := cbText.Items[0];
if cbDirectory.Items.Count > 0 then
cbDirectory.Text := cbDirectory.Items[0];
if cbMasks.Items.Count > 0 then
cbMasks.Text := cbMasks.Items[0];
end;
SetupPattern;
rbOpenFiles.Enabled := GI_EditorFactory.Count > 0;
rbCurrentOnly.Enabled := GI_EditorFactory.Count > 0;
EnableDirectoryControls(rbDirectories.Checked);
end;
procedure TFindInFilesDialog.RetrieveSettings(var Value: TGrepSettings);
begin
Value.NoComments := cbNoComments.Checked;
Value.CaseSensitive := cbCaseSensitive.Checked;
Value.WholeWord := cbWholeWord.Checked;
Value.RegEx := cbRegEx.Checked;
Value.Pattern := cbText.Text;
Value.IncludeSubdirs := cbInclude.Checked;
Value.Mask := '';
Value.Directories := '';
if rbCurrentOnly.Checked then
Value.FindInFilesAction := gaCurrentOnlyGrep
else if rbOpenFiles.Checked then
Value.FindInFilesAction := gaOpenFilesGrep
else if rbProject.Checked then
Value.FindInFilesAction := gaProjectGrep
else
begin
Value.FindInFilesAction := gaDirGrep;
Value.Mask := cbMasks.Text;
Value.Directories := cbDirectory.Text;
end;
end;
procedure TFindInFilesDialog.FormShow(Sender: TObject);
begin
Constraints.MaxHeight := Height;
Constraints.MinHeight := Height;
Constraints.MinWidth := Width;
end;
end.