Skip to content

Commit

Permalink
Optimizations in Replace All
Browse files Browse the repository at this point in the history
  • Loading branch information
dpradov committed Jan 27, 2024
1 parent 517c835 commit 96021e8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
77 changes: 44 additions & 33 deletions editor/kn_FindReplaceMng.pas
Expand Up @@ -1339,7 +1339,7 @@ function RunFindNext (Is_ReplacingAll: Boolean= False): boolean;
TTreeNote( ActiveNote ).TV.Selected := myTreeNode;
end;

SearchCaretPos (myNote, myTreeNode, PatternPos, length( Text_To_Find) + SizeInternalHiddenText, true);
SearchCaretPos (myNote, myTreeNode, PatternPos, length( Text_To_Find) + SizeInternalHiddenText, true, false);
end;


Expand Down Expand Up @@ -1513,6 +1513,38 @@ procedure ReplaceEventProc( ReplaceAll : boolean );
AppliedBeginUpdate: Boolean;


procedure BeginUpdateOnNotes;
var
i: integer;
note: TTabNote;
begin
AppliedBeginUpdate:= True;
for i := 0 to Form_Main.Pages.PageCount - 1 do
begin
note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject);
note.Editor.BeginUpdate;
note.Editor.OnSelectionChange := nil;
note.Editor.Visible:= False;
end;
end;

procedure EndUpdateOnNotes;
var
i: integer;
note: TTabNote;
begin
if not AppliedBeginUpdate then exit;

for i := 0 to Form_Main.Pages.PageCount - 1 do
begin
note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject);
note.Editor.EndUpdate;
note.Editor.OnSelectionChange := Form_Main.RxRTFSelectionChange;
note.Editor.Visible:= True;
end;
AppliedBeginUpdate:= False;
end;

function GetReplacementConfirmation: Boolean;
begin
Result := false;
Expand All @@ -1525,6 +1557,8 @@ procedure ReplaceEventProc( ReplaceAll : boolean );
mrAll: begin
Result := true;
FindOptions.ReplaceConfirm := false;
BeginUpdateOnNotes;
screen.Cursor := crHourGlass;
end;
mrCancel: begin
Result := false;
Expand Down Expand Up @@ -1558,33 +1592,6 @@ procedure ReplaceEventProc( ReplaceAll : boolean );
end;
end;

procedure BeginUpdateOnNotes;
var
i: integer;
note: TTabNote;
begin
AppliedBeginUpdate:= True;
for i := 0 to Form_Main.Pages.PageCount - 1 do
begin
note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject);
note.Editor.EndUpdate;
end;
end;

procedure EndUpdateOnNotes;
var
i: integer;
note: TTabNote;
begin
if not AppliedBeginUpdate then exit;

for i := 0 to Form_Main.Pages.PageCount - 1 do
begin
note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject);
note.Editor.EndUpdate;
end;
AppliedBeginUpdate:= False;
end;

begin
if assigned( Form_FindReplace ) then begin
Expand Down Expand Up @@ -1619,7 +1626,13 @@ procedure ReplaceEventProc( ReplaceAll : boolean );
// directamente.
SelectedTextToReplace:= False;
if not ReplaceAll then
SelectedTextToReplace:= IdentifySelectedTextToReplace;
SelectedTextToReplace:= IdentifySelectedTextToReplace
else
if not FindOptions.ReplaceConfirm then begin
BeginUpdateOnNotes;
screen.Cursor := crHourGlass;
end;


if not SelectedTextToReplace then begin
SelectedTextToReplace:= RunFindNext(ReplaceAll);
Expand All @@ -1631,8 +1644,6 @@ procedure ReplaceEventProc( ReplaceAll : boolean );


if DoReplace then begin
if ReplaceAll and not FindOptions.ReplaceConfirm then
BeginUpdateOnNotes;

while SelectedTextToReplace do
begin
Expand All @@ -1645,8 +1656,7 @@ procedure ReplaceEventProc( ReplaceAll : boolean );

if GetReplacementConfirmation then begin
inc(ReplaceCnt);
ActiveNote.Editor.SelText := FindOptions.ReplaceWith;
ActiveNote.Editor.SelStart := ActiveNote.Editor.SelStart + length( ActiveNote.Editor.SelText );
ActiveNote.Editor.AddText(FindOptions.ReplaceWith);
end;

Application.ProcessMessages;
Expand All @@ -1667,6 +1677,7 @@ procedure ReplaceEventProc( ReplaceAll : boolean );
end;

finally
screen.Cursor := crDefault;
EndUpdateOnNotes;
Is_Replacing := false;
UserBreak := false;
Expand Down
15 changes: 11 additions & 4 deletions editor/kn_LinksMng.pas
Expand Up @@ -60,7 +60,9 @@ interface
function BuildKNTLocationText( const aLocation : TLocation) : string;
procedure JumpToKNTLocation( LocationStr : string );
function JumpToLocation( Location: TLocation; IgnoreOtherFiles: boolean = true): boolean;
function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean): integer;
function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode;
CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean;
AdjustVisiblePosition: boolean = true): integer;
function PositionInImLinkTextPlain (myNote: TTabNote; myTreeNode: TTreeNTNode; CaretPosition: integer; ForceCalc: boolean = false): integer;

procedure ClickOnURL(const URLstr: string; chrgURL: TCharRange; myURLAction: TURLAction; EnsureAsk: boolean = false);
Expand Down Expand Up @@ -991,7 +993,9 @@ function GetPositionOffset (myNote : TTabNote; myTreeNode: TTreeNTNode; Pos_ImLi
end;


function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean): integer;
function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode;
CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean;
AdjustVisiblePosition: boolean = true): integer;
var
Offset: integer;
Pos_ImLinkTextPlain: integer;
Expand All @@ -1007,8 +1011,11 @@ function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; CaretPositi
SelStart := CaretPosition - Offset;
if SelectionLength >= 0 then
SelLength := SelectionLength;
myNote.Editor.ScrollLinesBy(80);
Perform( EM_SCROLLCARET, 0, 0 );
if AdjustVisiblePosition then begin
myNote.Editor.ScrollLinesBy(80);
Perform( EM_SCROLLCARET, 0, 0 );
end;

finally
EndUpdate;
end;
Expand Down

0 comments on commit 96021e8

Please sign in to comment.