/usr/lib/lazarus/0.9.30.4/ide/findinfilesdlg.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | {
*****************************************************************************
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
*****************************************************************************
Author: Mattias Gaertner
Abstract:
Find in files dialog form.
}
unit FindInFilesDlg;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc, LCLIntf, Controls, StdCtrls, Forms, Buttons,
ExtCtrls, FileUtil, LazarusIDEStrConsts, Dialogs, SynEditTypes, MacroIntf,
IDEDialogs, IDEWindowIntf, InputHistory, IDEContextHelpEdit, ButtonPanel,
SrcEditorIntf, EditorOptions, SearchFrm, Project, SynEdit, SearchResultView;
type
{ TLazFindInFilesDialog }
TLazFindInFilesDialog = class(TForm)
ButtonPanel1: TButtonPanel;
ReplaceCheckBox: TCheckBox;
ReplaceTextComboBox: TComboBox;
IncludeSubDirsCheckBox: TCheckBox;
FileMaskComboBox: TComboBox;
DirectoryBrowse: TBitBtn;
DirectoryComboBox: TComboBox;
DirectoryLabel: TLabel;
FileMaskLabel: TLabel;
DirectoryOptionsGroupBox: TGroupBox;
OptionsCheckGroupBox: TCheckGroup;
SelectDirectoryDialog: TSelectDirectoryDialog;
TextToFindComboBox: TComboBox;
TextToFindLabel: TLabel;
WhereRadioGroup: TRadioGroup;
procedure DirectoryBrowseClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure HelpButtonClick(Sender: TObject);
procedure OKButtonClick(Sender : TObject);
procedure ReplaceCheckBoxChange(Sender: TObject);
procedure WhereRadioGroupClick(Sender: TObject);
private
function GetFindText: string;
function GetOptions: TLazFindInFileSearchOptions;
function GetReplaceText: string;
function GetSynOptions: TSynSearchOptions;
procedure SetFindText(const NewFindText: string);
procedure SetOptions(NewOptions: TLazFindInFileSearchOptions);
procedure SetReplaceText(const AValue: string);
procedure SetSynOptions(NewOptions: TSynSearchOptions);
procedure UpdateReplaceCheck;
public
property Options: TLazFindInFileSearchOptions read GetOptions
write SetOptions;
property FindText: string read GetFindText write SetFindText;
property ReplaceText: string read GetReplaceText write SetReplaceText;
property SynSearchOptions: TSynSearchOptions read GetSynOptions
write SetSynOptions;
procedure LoadHistory;
procedure SaveHistory;
procedure FindInFilesPerDialog(AProject: TProject);
procedure InitFromLazSearch(Sender: TObject);
procedure FindInFiles(AProject: TProject; const AFindText: string);
function GetResolvedDirectory: string;
end;
function FindInFilesDialog: TLazFindInFilesDialog;
implementation
var
FindInFilesDialogSingleton: TLazFindInFilesDialog = nil;
function FindInFilesDialog: TLazFindInFilesDialog;
begin
Result := FindInFilesDialogSingleton;
if FindInFilesDialogSingleton <> nil then exit;
FindInFilesDialogSingleton := TLazFindInFilesDialog.Create(Application);
Result := FindInFilesDialogSingleton;
end;
{$R *.lfm}
{ TLazFindInFilesDialog }
procedure TLazFindInFilesDialog.SetFindText(const NewFindText: string);
begin
TextToFindComboBox.Text := NewFindText;
TextToFindComboBox.SelectAll;
ActiveControl := TextToFindComboBox;
end;
function TLazFindInFilesDialog.GetFindText: string;
begin
Result := TextToFindComboBox.Text;
end;
function TLazFindInFilesDialog.GetReplaceText: string;
begin
Result:=ReplaceTextComboBox.Text;
end;
procedure TLazFindInFilesDialog.WhereRadioGroupClick(Sender: TObject);
begin
DirectoryOptionsGroupBox.Enabled := (WhereRadioGroup.ItemIndex = 2)
end;
procedure TLazFindInFilesDialog.DirectoryBrowseClick(Sender: TObject);
var
Dir: String;
begin
InitIDEFileDialog(SelectDirectoryDialog);
Dir:=GetResolvedDirectory;
if DirectoryExistsUTF8(Dir) then
SelectDirectoryDialog.InitialDir := Dir
else
SelectDirectoryDialog.InitialDir := GetCurrentDirUTF8;
if SelectDirectoryDialog.Execute
and (CompareFilenames(SelectDirectoryDialog.FileName,Dir)<>0) then begin
DirectoryComboBox.Text := SelectDirectoryDialog.FileName;
end;
StoreIDEFileDialog(SelectDirectoryDialog);
end;
procedure TLazFindInFilesDialog.FormClose(Sender: TObject;
var CloseAction: TCloseAction);
begin
IDEDialogLayoutList.SaveLayout(Self);
end;
procedure TLazFindInFilesDialog.FormCreate(Sender: TObject);
begin
Caption := srkmecFindInFiles;
TextToFindLabel.Caption := lisFindFileTextToFind;
ReplaceCheckBox.Caption := lisMenuReplace;
OptionsCheckGroupBox.Caption := dlgFROpts;
OptionsCheckGroupBox.Items[0] := lisFindFileCaseSensitive;
OptionsCheckGroupBox.Items[1] := lisFindFileWholeWordsOnly;
OptionsCheckGroupBox.Items[2] := lisFindFileRegularExpressions;
OptionsCheckGroupBox.Items[3] := lisFindFileMultiLinePattern;
WhereRadioGroup.Caption:=lisFindFileWhere;
WhereRadioGroup.Items[0] := lisFindFilesearchAllFilesInProject;
WhereRadioGroup.Items[1] := lisFindFilesearchAllOpenFiles;
WhereRadioGroup.Items[2] := lisFindFilesearchInDirectories;
DirectoryOptionsGroupBox.Caption := lisFindFileDirectoryOptions;
DirectoryLabel.Caption := lisCodeToolsDefsInsertBehindDirectory;
FileMaskLabel.Caption := lisFindFileFileMaskBak;
IncludeSubDirsCheckBox.Caption := lisFindFileIncludeSubDirectories;
ButtonPanel1.OkButton.Caption := lisLazBuildOk;
ButtonPanel1.OKButton.OnClick := @OKButtonClick;
ButtonPanel1.HelpButton.Caption := lisMenuHelp;
ButtonPanel1.HelpButton.OnClick := @HelpButtonClick;
ButtonPanel1.CancelButton.Caption := dlgCancel;
ReplaceCheckBox.Enabled:=true;
UpdateReplaceCheck;
DirectoryOptionsGroupBox.Enabled:=WhereRadioGroup.ItemIndex=2;
AutoSize:=IDEDialogLayoutList.Find(Self,false)=nil;
IDEDialogLayoutList.ApplyLayout(Self);
end;
procedure TLazFindInFilesDialog.HelpButtonClick(Sender: TObject);
begin
ShowContextHelpForIDE(Self);
end;
procedure TLazFindInFilesDialog.OKButtonClick(Sender : TObject);
var
Dir: String;
begin
Dir:=GetResolvedDirectory;
if (WhereRadioGroup.ItemIndex=2) and (not DirectoryExistsUTF8(Dir)) then
begin
MessageDlg(lisEnvOptDlgDirectoryNotFound,
Format(dlgSeachDirectoryNotFound,[Dir]),
mtWarning, [mbOk],0);
ModalResult:=mrNone;
end
end;
procedure TLazFindInFilesDialog.ReplaceCheckBoxChange(Sender: TObject);
begin
UpdateReplaceCheck;
end;
procedure TLazFindInFilesDialog.SetOptions(NewOptions: TLazFindInFileSearchOptions);
begin
OptionsCheckGroupBox.Checked[0] := fifMatchCase in NewOptions;
OptionsCheckGroupBox.Checked[1] := fifWholeWord in NewOptions;
OptionsCheckGroupBox.Checked[2] := fifRegExpr in NewOptions;
OptionsCheckGroupBox.Checked[3] := fifMultiLine in NewOptions;
DirectoryOptionsGroupBox.Enabled := fifSearchDirectories in NewOptions;
IncludeSubDirsCheckBox.Checked := fifIncludeSubDirs in NewOptions;
ReplaceCheckBox.Checked := [fifReplace,fifReplaceAll]*NewOptions<>[];
if fifSearchProject in NewOptions then WhereRadioGroup.ItemIndex := 0;
if fifSearchOpen in NewOptions then WhereRadioGroup.ItemIndex := 1;
if fifSearchDirectories in NewOptions then WhereRadioGroup.ItemIndex := 2;
UpdateReplaceCheck;
end;
function TLazFindInFilesDialog.GetOptions: TLazFindInFileSearchOptions;
begin
Result := [];
if OptionsCheckGroupBox.Checked[0] then Include(Result, fifMatchCase);
if OptionsCheckGroupBox.Checked[1] then Include(Result, fifWholeWord);
if OptionsCheckGroupBox.Checked[2] then Include(Result, fifRegExpr);
if OptionsCheckGroupBox.Checked[3] then Include(Result, fifMultiLine);
if IncludeSubDirsCheckBox.Checked then Include(Result, fifIncludeSubDirs);
if ReplaceCheckBox.Checked then Include(Result, fifReplace);
case WhereRadioGroup.ItemIndex of
0: Include(Result, fifSearchProject);
1: Include(Result, fifSearchOpen);
2: Include(Result, fifSearchDirectories);
end;//case
end;
function TLazFindInFilesDialog.GetSynOptions: TSynSearchOptions;
begin
Result := [];
if OptionsCheckGroupBox.Checked[0] then Include(Result, ssoMatchCase);
if OptionsCheckGroupBox.Checked[1] then Include(Result, ssoWholeWord);
if OptionsCheckGroupBox.Checked[2] then Include(Result, ssoRegExpr);
if OptionsCheckGroupBox.Checked[3] then Include(Result, ssoRegExprMultiLine);
if ReplaceCheckBox.Checked then Include(Result, ssoReplace);
end;//GetSynOptions
procedure TLazFindInFilesDialog.SetReplaceText(const AValue: string);
begin
ReplaceTextComboBox.Text := AValue;
end;
procedure TLazFindInFilesDialog.SetSynOptions(NewOptions: TSynSearchOptions);
begin
OptionsCheckGroupBox.Checked[0] := ssoMatchCase in NewOptions;
OptionsCheckGroupBox.Checked[1] := ssoWholeWord in NewOptions;
OptionsCheckGroupBox.Checked[2] := ssoRegExpr in NewOptions;
OptionsCheckGroupBox.Checked[3] := ssoRegExprMultiLine in NewOptions;
ReplaceCheckBox.Checked := ([ssoReplace,ssoReplaceAll]*NewOptions <> []);
UpdateReplaceCheck;
end;//SetSynOptions
procedure TLazFindInFilesDialog.UpdateReplaceCheck;
begin
ReplaceTextComboBox.Enabled:=ReplaceCheckBox.Checked;
if ReplaceCheckBox.Checked then
ButtonPanel1.OKButton.Caption := lisBtnReplace
else
ButtonPanel1.OKButton.Caption := lisBtnFind;
end;
procedure TLazFindInFilesDialog.LoadHistory;
procedure AssignToComboBox(AComboBox: TComboBox; Strings: TStrings);
begin
AComboBox.Items.Assign(Strings);
if AComboBox.Items.Count>0 then
AComboBox.ItemIndex := 0;
end;
procedure AddFileToComboBox(AComboBox: TComboBox; const Filename: string);
var
i: Integer;
begin
if Filename='' then exit;
for i:=0 to AComboBox.Items.Count-1 do begin
if CompareFilenames(Filename,AComboBox.Items[i])=0 then begin
// move to front (but not top, top should be the last used directory)
if i>2 then
AComboBox.Items.Move(i,1);
exit;
end;
end;
// insert in front (but not top, top should be the last used directory)
if AComboBox.Items.Count>0 then
i:=1
else
i:=0;
AComboBox.Items.Insert(i,Filename);
end;
var
SrcEdit: TSourceEditorInterface;
begin
SrcEdit := SourceEditorManagerIntf.ActiveEditor;
//DebugLn('TSourceNotebook.LoadFindInFilesHistory ',dbgsName(TextToFindComboBox),' ',dbgsName(FindHistory));
TextToFindComboBox.Items.Assign(InputHistories.FindHistory);
ReplaceTextComboBox.Items.Assign(InputHistories.ReplaceHistory);
if not EditorOpts.FindTextAtCursor then begin
if TextToFindComboBox.Items.Count>0 then begin
//debugln('TSourceNotebook.LoadFindInFilesHistory A TextToFindComboBox.Text=',TextToFindComboBox.Text);
TextToFindComboBox.ItemIndex:=0;
TextToFindComboBox.SelectAll;
//debugln('TSourceNotebook.LoadFindInFilesHistory B TextToFindComboBox.Text=',TextToFindComboBox.Text);
end;
end;
// show last used directories and directory of current file
AssignToComboBox(DirectoryComboBox, InputHistories.FindInFilesPathHistory);
if (SrcEdit<>nil) and (FilenameIsAbsolute(SrcEdit.FileName)) then
AddFileToComboBox(DirectoryComboBox, ExtractFilePath(SrcEdit.FileName));
// show last used file masks
AssignToComboBox(FileMaskComboBox, InputHistories.FindInFilesMaskHistory);
Options := InputHistories.FindInFilesSearchOptions;
end;
procedure TLazFindInFilesDialog.SaveHistory;
begin
InputHistories.AddToFindHistory(FindText);
InputHistories.AddToFindInFilesPathHistory(DirectoryComboBox.Text);
InputHistories.AddToFindInFilesMaskHistory(FileMaskComboBox.Text);
InputHistories.FindInFilesSearchOptions:=Options;
InputHistories.Save;
end;
procedure TLazFindInFilesDialog.FindInFilesPerDialog(AProject: TProject);
var
TempEditor: TSourceEditorInterface;
Begin
FindText:='';
TempEditor := SourceEditorManagerIntf.ActiveEditor;
if TempEditor <> nil
then //with TempEditor.EditorComponent do
begin
if EditorOpts.FindTextAtCursor
then begin
if TempEditor.SelectionAvailable and (TempEditor.BlockBegin.Y = TempEditor.BlockEnd.Y)
then FindText := TempEditor.Selection
else FindText := TSynEdit(TempEditor.EditorControl).GetWordAtRowCol(TempEditor.CursorTextXY);
end else begin
if InputHistories.FindHistory.Count>0 then
FindText:=InputHistories.FindHistory[0];
end;
end;
FindInFiles(AProject, FindText);
end;
procedure TLazFindInFilesDialog.InitFromLazSearch(Sender: TObject);
begin
DirectoryComboBox.Text:= TLazSearch(Sender).SearchDirectory;
Options:= TLazSearch(Sender).SearchOptions;
FileMaskComboBox.Text:= TLazSearch(Sender).SearchMask;
end;
procedure TLazFindInFilesDialog.FindInFiles(AProject: TProject;
const AFindText: string);
var
SearchForm: TSearchForm;
begin
LoadHistory;
// if there is no FindText, use the most recently used FindText
FindText:= AFindText;
if (FindText = '') and (InputHistories.FindHistory.Count > 0) then
FindText := InputHistories.FindHistory[0];
// disable replace. Find in files is often called,
// but almost never to replace with the same parameters
Options := Options-[fifReplace,fifReplaceAll];
if ShowModal=mrOk then
begin
SaveHistory;
SearchForm:= TSearchForm.Create(SearchResultsView);
with SearchForm do begin
SearchOptions := self.Options;
SearchText := self.FindText;
ReplaceText := self.ReplaceText;
SearchMask := self.FileMaskComboBox.Text;
SearchDirectory := self.GetResolvedDirectory;
end;
try
if FindText <> '' then
begin
case WhereRadioGroup.ItemIndex of
0: SearchForm.DoSearchProject(AProject);
1: SearchForm.DoSearchOpenFiles;
2: SearchForm.DoSearchDir;
end;
end;
finally
FreeAndNil(SearchForm);
end;
end;
end;
function TLazFindInFilesDialog.GetResolvedDirectory: string;
begin
Result:=DirectoryComboBox.Text;
IDEMacros.SubstituteMacros(Result);
if Result<>'' then
Result:=CleanAndExpandDirectory(Result);
end;
end.
|