/usr/lib/lazarus/0.9.30.4/ide/findreplacedialog.pp 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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | {
/***************************************************************************
findreplacedialog.pp
--------------------
***************************************************************************/
Author: Mattias Gaertner
*****************************************************************************
* *
* 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. *
* *
*****************************************************************************
Abstract:
Find and replace dialog form.
Usage:
Add to program
"Application.CreateForm(TLazFindReplaceDialog, FindReplaceDlg);"
Set the FindReplaceDlg.Options poperty
then do MResult:=FindReplaceDlg.ShowModal
ShowModal can have three possible results:
- mrOk for Find/Replace.
- mrAll for ReplaceAll
- mrCancel for Cancel
}
unit FindReplaceDialog;
{$mode objfpc}{$H+}
interface
uses
Classes, Math, SysUtils, LCLProc, LCLType, Controls, StdCtrls, Forms, Buttons,
ExtCtrls, Dialogs, Graphics,
SynEditTypes, SynRegExpr, SynEdit,
IDEImagesIntf, IDEWindowIntf, LazarusIdeStrConsts, IDEContextHelpEdit,
InputHistory;
type
TFindDlgComponent = (fdcText, fdcReplace);
TOnFindDlgKey = procedure(Sender: TObject; var Key: Word; Shift:TShiftState;
FindDlgComponent: TFindDlgComponent) of Object;
{ TLazFindReplaceDialog }
TLazFindReplaceDialog = class(TForm)
BackwardRadioButton: TRadioButton;
HelpButton: TBitBtn;
BtnPanel: TPanel;
CancelButton: TBitBtn;
CaseSensitiveCheckBox: TCheckBox;
DirectionGroupBox: TGroupBox;
EntireScopeRadioButton: TRadioButton;
ForwardRadioButton: TRadioButton;
FromCursorRadioButton: TRadioButton;
GlobalRadioButton: TRadioButton;
MultiLineCheckBox: TCheckBox;
OKButton: TBitBtn;
OptionsGroupBox: TGroupBox;
OriginGroupBox: TGroupBox;
PromptOnReplaceCheckBox: TCheckBox;
RegularExpressionsCheckBox: TCheckBox;
ReplaceAllButton: TBitBtn;
ReplaceTextComboBox: TComboBox;
ReplaceWithCheckbox: TCheckBox;
ScopeGroupBox: TGroupBox;
SelectedRadioButton: TRadioButton;
EnableAutoCompleteSpeedButton: TSpeedButton;
TextToFindComboBox: TComboBox;
TextToFindLabel: TLabel;
WholeWordsOnlyCheckBox: TCheckBox;
procedure EnableAutoCompleteSpeedButtonClick(Sender: TObject);
procedure FormChangeBounds(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure HelpButtonClick(Sender: TObject);
procedure OptionsGroupBoxResize(Sender: TObject);
procedure ReplaceWithCheckboxChange(Sender: TObject);
procedure TextToFindComboboxKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure OkButtonClick(Sender: TObject);
procedure ReplaceAllButtonClick(Sender: TObject);
procedure CancelButtonClick(Sender: TObject);
private
FOnKey: TOnFindDlgKey;
fReplaceAllClickedLast: boolean;
RegExpr: TRegExpr;
DlgHistoryIndex: array[TFindDlgComponent] of integer;
DlgUserText: array[TFindDlgComponent] of string;
function CheckInput: boolean;
function GetComponentText(c: TFindDlgComponent): string;
function GetEnableAutoComplete: boolean;
procedure SetComponentText(c: TFindDlgComponent; const AValue: string);
procedure SetEnableAutoComplete(const AValue: boolean);
procedure SetOnKey(const AValue: TOnFindDlgKey);
procedure SetOptions(NewOptions: TSynSearchOptions);
function GetOptions: TSynSearchOptions;
function GetFindText: AnsiString;
procedure SetFindText(const NewFindText: AnsiString);
function GetReplaceText: AnsiString;
procedure SetReplaceText(const NewReplaceText: AnsiString);
procedure SetComboBoxText(AComboBox: TComboBox; const AText: AnsiString);
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure UpdateHints;
procedure ResetUserHistory;
public
property Options: TSynSearchOptions read GetOptions write SetOptions;
property EnableAutoComplete: boolean read GetEnableAutoComplete
write SetEnableAutoComplete;
property FindText:AnsiString read GetFindText write SetFindText;
property ReplaceText:AnsiString read GetReplaceText write SetReplaceText;
property OnKey: TOnFindDlgKey read FOnKey write SetOnKey;
property ComponentText[c: TFindDlgComponent]: string
read GetComponentText write SetComponentText;
end;
var
LazFindReplaceDialog: TLazFindReplaceDialog = nil;
implementation
{$R *.lfm}
{ TLazFindReplaceDialog }
constructor TLazFindReplaceDialog.Create(TheOwner:TComponent);
begin
inherited Create(TheOwner);
IDEDialogLayoutList.ApplyLayout(Self,420,350);
Caption:='';
TextToFindComboBox.Text:='';
TextToFindLabel.Caption:=dlgTextToFing;
ReplaceTextComboBox.Text:='';
ReplaceWithCheckbox.Caption:=dlgReplaceWith;
EnableAutoCompleteSpeedButton.LoadGlyphFromLazarusResource('menu_stepinto');
OptionsGroupBox.Caption:=dlgFROpts;
with CaseSensitiveCheckBox do begin
Caption:=dlgCaseSensitive;
Hint:=lisDistinguishBigAndSmallLettersEGAAndA;
end;
with WholeWordsOnlyCheckBox do begin
Caption:=dlgWholeWordsOnly;
Hint:=lisOnlySearchForWholeWords;
end;
with RegularExpressionsCheckBox do begin
Caption:=dlgRegularExpressions;
Hint:=lisActivateRegularExpressionSyntaxForTextAndReplaceme;
end;
with MultiLineCheckBox do begin
Caption:=lisFindFileMultiLinePattern;
Hint:=lisAllowSearchingForMultipleLines;
end;
with PromptOnReplaceCheckBox do begin
Caption:=dlgPromptOnReplace;
Hint:=lisAskBeforeReplacingEachFoundText;
end;
OriginGroupBox.Caption:=dlgSROrigin;
FromCursorRadioButton.Caption := dlgFromCursor;
EntireScopeRadioButton.Caption := dlgEntireScope;
ScopeGroupBox.Caption:=dlgScope;
GlobalRadioButton.Caption := dlgGlobal;
SelectedRadioButton.Caption := dlgSelectedText;
DirectionGroupBox.Caption:=dlgDirection;
ForwardRadioButton.Caption := lisFRForwardSearch;
BackwardRadioButton.Caption := lisFRBackwardSearch;
HelpButton.Caption:=lisMenuHelp;
ReplaceAllButton.Caption:=dlgReplaceAll;
OKButton.Caption:=lisOk;
CancelButton.Caption:=dlgCancel;
fReplaceAllClickedLast:=false;
UpdateHints;
end;
destructor TLazFindReplaceDialog.Destroy;
begin
RegExpr.Free;
inherited Destroy;
if LazFindReplaceDialog=Self then
LazFindReplaceDialog:=nil;
end;
procedure TLazFindReplaceDialog.UpdateHints;
begin
if EnableAutoComplete then
EnableAutoCompleteSpeedButton.Hint:=lisAutoCompletionOn
else
EnableAutoCompleteSpeedButton.Hint:=lisAutoCompletionOff;
end;
procedure TLazFindReplaceDialog.ResetUserHistory;
var
c: TFindDlgComponent;
begin
for c := Low(TFindDlgComponent) to High(TFindDlgComponent) do
DlgHistoryIndex[c] := -1;
end;
procedure TLazFindReplaceDialog.TextToFindComboBoxKeyDown(
Sender: TObject; var Key:Word; Shift:TShiftState);
var
Component: TFindDlgComponent;
HistoryList: TStringList;
CurText: string;
CurIndex: integer;
procedure SetHistoryText;
var s: string;
begin
if DlgHistoryIndex[Component]>=0 then
s := HistoryList[DlgHistoryIndex[Component]]
else
s := DlgUserText[Component];
//writeln(' SetHistoryText "',s,'"');
ComponentText[Component]:=s
end;
procedure FetchFocus;
begin
if Sender is TWinControl then
TWinControl(Sender).SetFocus;
end;
begin
//debugln('TLazFindReplaceDialog.TextToFindComboBoxKeyDown Key=',Key,' RETURN=',VK_RETURN,' TAB=',VK_TAB,' DOWN=',VK_DOWN,' UP=',VK_UP);
if Sender=TextToFindComboBox then
Component:=fdcText
else
Component:=fdcReplace;
if Assigned(OnKey) then begin
OnKey(Sender, Key, Shift, Component);
end
else
begin
if Component=fdcText then
HistoryList:= InputHistories.FindHistory
else
HistoryList:= InputHistories.ReplaceHistory;
CurIndex := DlgHistoryIndex[Component];
CurText := ComponentText[Component];
if Key=VK_UP then begin
// go forward in history
if CurIndex >= 0 then begin
if (HistoryList[CurIndex] <> CurText) then
DlgUserText[Component] := CurText; // save user text
dec(DlgHistoryIndex[Component]);
SetHistoryText;
end;
FetchFocus;
Key:=VK_UNKNOWN;
end
else if Key=VK_DOWN then begin
// go back in history
if (CurIndex<0)
or (HistoryList[CurIndex] <> CurText) then
DlgUserText[Component] := CurText; // save user text
if CurIndex < HistoryList.Count-1 then
begin
inc(DlgHistoryIndex[Component]);
SetHistoryText;
end;
FetchFocus;
Key:=VK_UNKNOWN;
end;
end;
end;
procedure TLazFindReplaceDialog.FormClose(Sender: TObject;
var CloseAction: TCloseAction);
begin
IDEDialogLayoutList.SaveLayout(Self);
end;
procedure TLazFindReplaceDialog.HelpButtonClick(Sender: TObject);
begin
ShowContextHelpForIDE(Self);
end;
procedure TLazFindReplaceDialog.OptionsGroupBoxResize(Sender: TObject);
var
h: integer;
begin
DisableAlign;
h := (OptionsGroupBox.Height-12) div 3;
OriginGroupBox.Height := h;
ScopeGroupBox.Height := h;
DirectionGroupBox.Height := OptionsGroupBox.Height-2*h-12;
EnableAlign;
end;
procedure TLazFindReplaceDialog.ReplaceWithCheckboxChange(Sender: TObject);
begin
ReplaceAllButton.Visible:=ReplaceWithCheckbox.Checked;
ReplaceTextComboBox.Enabled:=ReplaceAllButton.Visible;
PromptOnReplaceCheckBox.Enabled:=ReplaceAllButton.Visible;
if ReplaceAllButton.Visible then
begin
Caption := lisMenuReplace;
OkButton.Caption := lisBtnReplace;
end else
begin
Caption := lisMenuFind;
OkButton.Caption := lisBtnFind;
end;
end;
procedure TLazFindReplaceDialog.FormChangeBounds(Sender: TObject);
var
w: integer;
begin
DisableAlign;
w := (ClientWidth - 18) div 2;
OptionsGroupBox.Width := w;
OriginGroupBox.Width := w;
ScopeGroupBox.Width := w;
DirectionGroupBox.Width := w;
EnableAlign;
end;
procedure TLazFindReplaceDialog.EnableAutoCompleteSpeedButtonClick(
Sender: TObject);
begin
TextToFindComboBox.AutoComplete:=EnableAutoCompleteSpeedButton.Down;
ReplaceTextComboBox.AutoComplete:=EnableAutoCompleteSpeedButton.Down;
UpdateHints;
end;
procedure TLazFindReplaceDialog.OkButtonClick(Sender:TObject);
begin
if not CheckInput then exit;
fReplaceAllClickedLast:=false;
ActiveControl:=TextToFindComboBox;
ModalResult:=mrOk;
end;
procedure TLazFindReplaceDialog.ReplaceAllButtonClick(Sender:TObject);
begin
if not CheckInput then exit;
fReplaceAllClickedLast:=true;
ActiveControl:=TextToFindComboBox;
ModalResult:=mrAll;
end;
procedure TLazFindReplaceDialog.CancelButtonClick(Sender:TObject);
begin
ActiveControl:=TextToFindComboBox;
end;
function TLazFindReplaceDialog.CheckInput: boolean;
begin
Result:=false;
if RegularExpressionsCheckBox.Checked then begin
if RegExpr=nil then RegExpr:=TRegExpr.Create;
try
RegExpr.Expression:=FindText;
RegExpr.Exec('test');
except
on E: ERegExpr do begin
MessageDlg(lisUEErrorInRegularExpression,
E.Message,mtError,[mbCancel],0);
exit;
end;
end;
if ReplaceTextComboBox.Enabled then begin
try
RegExpr.Substitute(ReplaceText);
except
on E: ERegExpr do begin
MessageDlg(lisUEErrorInRegularExpression,
E.Message,mtError,[mbCancel],0);
exit;
end;
end;
end;
end;
Result:=true;
end;
function TLazFindReplaceDialog.GetComponentText(c: TFindDlgComponent): string;
begin
case c of
fdcText: Result:=FindText;
else
Result:=Replacetext;
end;
end;
function TLazFindReplaceDialog.GetEnableAutoComplete: boolean;
begin
Result:=EnableAutoCompleteSpeedButton.Down;
end;
procedure TLazFindReplaceDialog.SetComponentText(c: TFindDlgComponent;
const AValue: string);
begin
case c of
fdcText: FindText:=AValue;
else
Replacetext:=AValue;
end;
end;
procedure TLazFindReplaceDialog.SetEnableAutoComplete(const AValue: boolean);
begin
EnableAutoCompleteSpeedButton.Down:=AValue;
TextToFindComboBox.AutoComplete:=AValue;
ReplaceTextComboBox.AutoComplete:=AValue;
UpdateHints;
end;
procedure TLazFindReplaceDialog.SetOnKey(const AValue: TOnFindDlgKey);
begin
FOnKey:=AValue;
end;
procedure TLazFindReplaceDialog.SetOptions(NewOptions:TSynSearchOptions);
begin
CaseSensitiveCheckBox.Checked:=ssoMatchCase in NewOptions;
WholeWordsOnlyCheckBox.Checked:=ssoWholeWord in NewOptions;
RegularExpressionsCheckBox.Checked:=ssoRegExpr in NewOptions;
MultiLineCheckBox.Checked:=ssoRegExprMultiLine in NewOptions;
PromptOnReplaceCheckBox.Checked:=ssoPrompt in NewOptions;
if ssoEntireScope in NewOptions
then EntireScopeRadioButton.Checked:=True
else FromCursorRadioButton.Checked:=True;
if ssoSelectedOnly in NewOptions
then SelectedRadioButton.Checked:=True
else GlobalRadioButton.Checked:=True;
if ssoBackwards in NewOptions
then BackwardRadioButton.Checked:=True
else ForwardRadioButton.Checked:=True;
ReplaceAllButton.Visible:=ssoReplace in NewOptions;
ReplaceWithCheckbox.Checked:=ssoReplace in NewOptions;
ReplaceTextComboBox.Enabled:=ReplaceAllButton.Visible;
PromptOnReplaceCheckBox.Enabled:=ReplaceAllButton.Visible;
if ssoReplace in NewOptions then
begin
Caption := lisMenuReplace;
OkButton.Caption := lisBtnReplace;
end else
begin
Caption := lisMenuFind;
OkButton.Caption := lisBtnFind;
end;
//DebugLn(['TLazFindReplaceDialog.SetOptions END ssoSelectedOnly=',ssoSelectedOnly in NewOptions,' SelectedRadioButton.Checked=',SelectedRadioButton.Checked]);
end;
function TLazFindReplaceDialog.GetOptions:TSynSearchOptions;
begin
Result:=[];
if CaseSensitiveCheckBox.Checked then Include(Result,ssoMatchCase);
if WholeWordsOnlyCheckBox.Checked then Include(Result,ssoWholeWord);
if RegularExpressionsCheckBox.Checked then Include(Result,ssoRegExpr);
if MultiLineCheckBox.Checked then Include(Result,ssoRegExprMultiLine);
if PromptOnReplaceCheckBox.Checked then Include(Result,ssoPrompt);
if EntireScopeRadioButton.Checked then Include(Result,ssoEntireScope);
if SelectedRadioButton.Checked then include(Result,ssoSelectedOnly);
if BackwardRadioButton.Checked then include(Result,ssoBackwards);
if ReplaceAllButton.Visible then include(Result,ssoReplace);
if fReplaceAllClickedLast then include(Result,ssoReplaceAll);
end;
function TLazFindReplaceDialog.GetFindText:AnsiString;
begin
Result:=TextToFindComboBox.Text;
end;
procedure TLazFindReplaceDialog.SetFindText(const NewFindText: AnsiString);
begin
// SetComboBoxText(TextToFindComboBox,NewFindText);
TextToFindComboBox.Text:=NewFindText;
TextToFindComboBox.SelectAll;
//debugln('TLazFindReplaceDialog.SetFindText A TextToFindComboBox.SelStart=',dbgs(TextToFindComboBox.SelStart),' TextToFindComboBox.SelLength=',dbgs(TextToFindComboBox.SelLength),' TextToFindComboBox.Text="',TextToFindComboBox.Text,'" NewFindText="',DbgStr(NewFindText),'"');
end;
function TLazFindReplaceDialog.GetReplaceText:AnsiString;
begin
Result:=ReplaceTextComboBox.Text;
end;
procedure TLazFindReplaceDialog.SetReplaceText(const NewReplaceText:AnsiString);
begin
SetComboBoxText(ReplaceTextComboBox,NewReplaceText);
end;
procedure TLazFindReplaceDialog.SetComboBoxText(AComboBox:TComboBox;
const AText:AnsiString);
var a:integer;
begin
a:=AComboBox.Items.IndexOf(AText);
//debugln('TLazFindReplaceDialog.SetComboBoxText ',AText,' ',a);
if a>=0 then
AComboBox.ItemIndex:=a
else begin
AComboBox.Items.Add(AText);
AComboBox.ItemIndex:=AComboBox.Items.IndexOf(AText);
end;
end;
end.
|